Spaces:
Sleeping
Sleeping
Download src/rag/vector_store.py from AishaSurve/codebase-agent: direct link, hf CLI and curl.
- Browser
- Download file 880 Bytes
-
https://huggingface.co/spaces/AishaSurve/codebase-agent/resolve/main/src/rag/vector_store.py
- Command line
-
hf download hf://spaces/AishaSurve/codebase-agent/src/rag/vector_store.py
-
curl -L -o vector_store.py https://huggingface.co/spaces/AishaSurve/codebase-agent/resolve/main/src/rag/vector_store.py
880 Bytes
| import faiss | |
| import numpy as np | |
| class VectorStore: | |
| def __init__(self): | |
| self.index = None | |
| self.documents = [] | |
| def build(self, embeddings, documents): | |
| dimension = embeddings.shape[1] | |
| self.index = faiss.IndexFlatIP( | |
| dimension | |
| ) | |
| self.index.add( | |
| np.array(embeddings) | |
| ) | |
| self.documents = documents | |
| def search(self, query_embedding, k=3): | |
| scores, indexes = self.index.search( | |
| np.array([query_embedding]), | |
| k | |
| ) | |
| results=[] | |
| for score, idx in zip( | |
| scores[0], | |
| indexes[0] | |
| ): | |
| results.append( | |
| { | |
| "score":float(score), | |
| "document": | |
| self.documents[idx] | |
| } | |
| ) | |
| return results |