chroma_db.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import logging
  2. import chromadb
  3. from embedchain.vectordb.base_vector_db import BaseVectorDB
  4. class ChromaDB(BaseVectorDB):
  5. """Vector database using ChromaDB."""
  6. def __init__(self, db_dir=None, ef=None, host=None, port=None):
  7. self.ef = ef
  8. if host and port:
  9. logging.info(f"Connecting to ChromaDB server: {host}:{port}")
  10. self.client_settings = chromadb.config.Settings(
  11. chroma_api_impl="rest",
  12. chroma_server_host=host,
  13. chroma_server_http_port=port,
  14. )
  15. else:
  16. if db_dir is None:
  17. db_dir = "db"
  18. self.client_settings = chromadb.config.Settings(
  19. chroma_db_impl="duckdb+parquet",
  20. persist_directory=db_dir,
  21. anonymized_telemetry=False,
  22. )
  23. super().__init__()
  24. def _get_or_create_db(self):
  25. """Get or create the database."""
  26. return chromadb.Client(self.client_settings)
  27. def _get_or_create_collection(self):
  28. """Get or create the collection."""
  29. return self.client.get_or_create_collection(
  30. "embedchain_store",
  31. embedding_function=self.ef,
  32. )