BaseAppConfig.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import logging
  2. from embedchain.config.BaseConfig import BaseConfig
  3. class BaseAppConfig(BaseConfig):
  4. """
  5. Parent config to initialize an instance of `App`, `OpenSourceApp` or `CustomApp`.
  6. """
  7. def __init__(self, log_level=None, embedding_fn=None, db=None, host=None, port=None, id=None):
  8. """
  9. :param log_level: Optional. (String) Debug level
  10. ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'].
  11. :param embedding_fn: Embedding function to use.
  12. :param db: Optional. (Vector) database instance to use for embeddings.
  13. :param id: Optional. ID of the app. Document metadata will have this id.
  14. :param host: Optional. Hostname for the database server.
  15. :param port: Optional. Port for the database server.
  16. """
  17. self._setup_logging(log_level)
  18. self.db = db if db else BaseAppConfig.default_db(embedding_fn=embedding_fn, host=host, port=port)
  19. self.id = id
  20. return
  21. @staticmethod
  22. def default_db(embedding_fn, host, port):
  23. """
  24. Sets database to default (`ChromaDb`).
  25. :param embedding_fn: Embedding function to use in database.
  26. :param host: Optional. Hostname for the database server.
  27. :param port: Optional. Port for the database server.
  28. :returns: Default database
  29. :raises ValueError: BaseAppConfig knows no default embedding function.
  30. """
  31. if embedding_fn is None:
  32. raise ValueError("ChromaDb cannot be instantiated without an embedding function")
  33. from embedchain.vectordb.chroma_db import ChromaDB
  34. return ChromaDB(embedding_fn=embedding_fn, host=host, port=port)
  35. def _setup_logging(self, debug_level):
  36. level = logging.WARNING # Default level
  37. if debug_level is not None:
  38. level = getattr(logging, debug_level.upper(), None)
  39. if not isinstance(level, int):
  40. raise ValueError(f"Invalid log level: {debug_level}")
  41. logging.basicConfig(format="%(asctime)s [%(name)s] [%(levelname)s] %(message)s", level=level)
  42. self.logger = logging.getLogger(__name__)
  43. return