-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchromadb_handler.py
27 lines (24 loc) · 968 Bytes
/
chromadb_handler.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import chromadb
from fastapi import HTTPException
class ChromaDBClient:
def __init__(self):
self.client = chromadb.PersistentClient(path="models/chroma_db")
self.collection = self.client.get_or_create_collection("documents")
def store_document(self, doc_name, embeddings, doc_text):
self.collection.add(
embeddings=[embeddings],
documents=[doc_text],
metadatas=[{"name": doc_name}],
ids=[doc_name]
)
def query_similar_documents(self, query_embedding, n_results):
try:
results = self.collection.query(
query_embeddings=[query_embedding],
n_results=n_results
)
return results
except Exception as e:
if "Collection is empty" in str(e):
raise HTTPException(status_code=404, detail="No documents have been ingested yet")
raise