chroma_db.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import chromadb
  2. import os
  3. from chromadb.utils import embedding_functions
  4. from embedchain.vectordb.base_vector_db import BaseVectorDB
  5. class ChromaDB(BaseVectorDB):
  6. ''' Vector database using ChromaDB. '''
  7. def __init__(self, db_dir=None, ef=None):
  8. if ef:
  9. self.ef = ef
  10. else:
  11. self.ef = embedding_functions.OpenAIEmbeddingFunction(
  12. api_key=os.getenv("OPENAI_API_KEY"),
  13. organization_id=os.getenv("OPENAI_ORGANIZATION"),
  14. model_name="text-embedding-ada-002"
  15. )
  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', embedding_function=self.ef,
  31. )