Skip to content

Commit

Permalink
Merge pull request #1164 from phidatahq/aamirahmed2004/main
Browse files Browse the repository at this point in the history
Aamirahmed2004/main
  • Loading branch information
ashpreetbedi authored Oct 3, 2024
2 parents f223cf9 + b7122df commit 31f73b3
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions phi/vectordb/chroma/chromadb.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def create(self) -> None:
name=self.collection, metadata={"hnsw:space": self.distance.value}
)

else:
logger.debug(f"Collection already exists: {self.collection}")
self._collection = self.client.get_collection(name=self.collection)

def doc_exists(self, document: Document) -> bool:
"""Check if a document exists in the collection.
Args:
Expand Down Expand Up @@ -125,10 +129,11 @@ def insert(self, documents: List[Document]) -> None:
docs_embeddings.append(document.embedding)
docs.append(cleaned_content)
ids.append(doc_id)
logger.debug(f"Inserted document: {document.id} | {document.name} | {document.meta_data}")

if len(docs) > 0 and self._collection is not None:
self._collection.add(ids=ids, embeddings=docs_embeddings, documents=docs)
logger.debug(f"Inserted {len(docs)} documents")
logger.debug(f"Committed {len(docs)} documents")
else:
logger.error("Collection does not exist")

Expand All @@ -137,7 +142,7 @@ def upsert(self, documents: List[Document]) -> None:
Args:
documents (List[Document]): List of documents to upsert
"""
logger.debug(f"Inserting {len(documents)} documents")
logger.debug(f"Upserting {len(documents)} documents")
ids: List = []
docs: List = []
docs_embeddings: List = []
Expand All @@ -149,10 +154,12 @@ def upsert(self, documents: List[Document]) -> None:
docs_embeddings.append(document.embedding)
docs.append(cleaned_content)
ids.append(doc_id)
logger.debug(f"Upserted document: {document.id} | {document.name} | {document.meta_data}")

if len(docs) > 0 and self._collection is not None:
self._collection.upsert(ids=ids, embeddings=docs_embeddings, documents=docs)
logger.debug(f"Inserted {len(docs)} documents")
logger.debug(f"Committed {len(docs)} documents")

else:
logger.error("Collection does not exist")

Expand Down Expand Up @@ -233,7 +240,12 @@ def get_count(self) -> int:
return 0

def optimize(self) -> None:
pass
raise NotImplementedError

def clear(self) -> bool:
return False
try:
self.client.delete_collection(name=self.collection)
return True
except Exception as e:
logger.error(f"Error clearing collection: {e}")
return False

0 comments on commit 31f73b3

Please sign in to comment.