base_app_config.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import logging
  2. from typing import Optional
  3. from embedchain.config.base_config import BaseConfig
  4. from embedchain.helpers.json_serializable import JSONSerializable
  5. from embedchain.vectordb.base import BaseVectorDB
  6. class BaseAppConfig(BaseConfig, JSONSerializable):
  7. """
  8. Parent config to initialize an instance of `App`.
  9. """
  10. def __init__(
  11. self,
  12. log_level: str = "WARNING",
  13. db: Optional[BaseVectorDB] = None,
  14. id: Optional[str] = None,
  15. collect_metrics: bool = True,
  16. collection_name: Optional[str] = None,
  17. ):
  18. """
  19. Initializes a configuration class instance for an App.
  20. Most of the configuration is done in the `App` class itself.
  21. :param log_level: Debug level ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'], defaults to "WARNING"
  22. :type log_level: str, optional
  23. :param db: A database class. It is recommended to set this directly in the `App` class, not this config,
  24. defaults to None
  25. :type db: Optional[BaseVectorDB], optional
  26. :param id: ID of the app. Document metadata will have this id., defaults to None
  27. :type id: Optional[str], optional
  28. :param collect_metrics: Send anonymous telemetry to improve embedchain, defaults to True
  29. :type collect_metrics: Optional[bool], optional
  30. :param collection_name: Default collection name. It's recommended to use app.db.set_collection_name() instead,
  31. defaults to None
  32. :type collection_name: Optional[str], optional
  33. """
  34. self._setup_logging(log_level)
  35. self.id = id
  36. self.collect_metrics = True if (collect_metrics is True or collect_metrics is None) else False
  37. self.collection_name = collection_name
  38. if db:
  39. self._db = db
  40. logging.warning(
  41. "DEPRECATION WARNING: Please supply the database as the second parameter during app init. "
  42. "Such as `app(config=config, db=db)`."
  43. )
  44. if collection_name:
  45. logging.warning("DEPRECATION WARNING: Please supply the collection name to the database config.")
  46. return
  47. def _setup_logging(self, debug_level):
  48. level = logging.WARNING # Default level
  49. if debug_level is not None:
  50. level = getattr(logging, debug_level.upper(), None)
  51. if not isinstance(level, int):
  52. raise ValueError(f"Invalid log level: {debug_level}")
  53. logging.basicConfig(format="%(asctime)s [%(name)s] [%(levelname)s] %(message)s", level=level)
  54. self.logger = logging.getLogger(__name__)
  55. return