AppConfig.py 2.2 KB

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