rag-visualizer / backend /storage /vector_store.py
Vasanth6's picture
RAG retreival updates
bd53034
Raw
History Blame Contribute Delete
1.29 kB
from pathlib import Path
from chromadb import PersistentClient
import asyncio
class VectorStore:
def __init__(self):
base_dir = Path(__file__).resolve().parent.parent.parent
store_path = base_dir / "store"
self.client = PersistentClient(path=str(store_path))
def get_collection(self, name: str):
return self.client.get_or_create_collection(
name=name, metadata={"hnsw:space": "cosine"}
)
async def upsert(self, collection_name, ids, documents, embeddings, metadatas=None):
collection = self.get_collection(collection_name)
await asyncio.to_thread(
collection.upsert,
ids=ids,
documents=documents,
embeddings=embeddings,
metadatas=metadatas,
)
async def retrieve(self, collection_name, embeddings, where, n_results=3):
collection = self.get_collection(collection_name)
return await asyncio.to_thread(
collection.query,
where=where,
query_embeddings=embeddings,
n_results=n_results,
)
async def get_all_documents(self, collection_name: str):
collection = self.get_collection(collection_name)
return await asyncio.to_thread(collection.get)