AppConfig.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import os
  2. try:
  3. from chromadb.utils import embedding_functions
  4. except RuntimeError:
  5. from embedchain.utils import use_pysqlite3
  6. use_pysqlite3()
  7. from chromadb.utils import embedding_functions
  8. from .BaseAppConfig import BaseAppConfig
  9. class AppConfig(BaseAppConfig):
  10. """
  11. Config to initialize an embedchain custom `App` instance, with extra config options.
  12. """
  13. def __init__(self, log_level=None, host=None, port=None, id=None):
  14. """
  15. :param log_level: Optional. (String) Debug level
  16. ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'].
  17. :param host: Optional. Hostname for the database server.
  18. :param port: Optional. Port for the database server.
  19. :param id: Optional. ID of the app. Document metadata will have this id.
  20. """
  21. super().__init__(
  22. log_level=log_level, embedding_fn=AppConfig.default_embedding_function(), host=host, port=port, id=id
  23. )
  24. @staticmethod
  25. def default_embedding_function():
  26. """
  27. Sets embedding function to default (`text-embedding-ada-002`).
  28. :raises ValueError: If the template is not valid as template should contain
  29. $context and $query
  30. :returns: The default embedding function for the app class.
  31. """
  32. if os.getenv("OPENAI_API_KEY") is None and os.getenv("OPENAI_ORGANIZATION") is None:
  33. raise ValueError("OPENAI_API_KEY or OPENAI_ORGANIZATION environment variables not provided") # noqa:E501
  34. return embedding_functions.OpenAIEmbeddingFunction(
  35. api_key=os.getenv("OPENAI_API_KEY"),
  36. organization_id=os.getenv("OPENAI_ORGANIZATION"),
  37. model_name="text-embedding-ada-002",
  38. )