CustomAppConfig.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. from typing import Any
  2. from chromadb.api.types import Documents, Embeddings
  3. from dotenv import load_dotenv
  4. from embedchain.models import EmbeddingFunctions, Providers
  5. from .BaseAppConfig import BaseAppConfig
  6. load_dotenv()
  7. class CustomAppConfig(BaseAppConfig):
  8. """
  9. Config to initialize an embedchain custom `App` instance, with extra config options.
  10. """
  11. def __init__(
  12. self,
  13. log_level=None,
  14. embedding_fn: EmbeddingFunctions = None,
  15. embedding_fn_model=None,
  16. db=None,
  17. host=None,
  18. port=None,
  19. id=None,
  20. collection_name=None,
  21. provider: Providers = None,
  22. open_source_app_config=None,
  23. deployment_name=None,
  24. ):
  25. """
  26. :param log_level: Optional. (String) Debug level
  27. ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'].
  28. :param embedding_fn: Optional. Embedding function to use.
  29. :param embedding_fn_model: Optional. Model name to use for embedding function.
  30. :param db: Optional. (Vector) database to use for embeddings.
  31. :param host: Optional. Hostname for the database server.
  32. :param port: Optional. Port for the database server.
  33. :param id: Optional. ID of the app. Document metadata will have this id.
  34. :param collection_name: Optional. Collection name for the database.
  35. :param provider: Optional. (Providers): LLM Provider to use.
  36. :param open_source_app_config: Optional. Config instance needed for open source apps.
  37. """
  38. if provider:
  39. self.provider = provider
  40. else:
  41. raise ValueError("CustomApp must have a provider assigned.")
  42. self.open_source_app_config = open_source_app_config
  43. super().__init__(
  44. log_level=log_level,
  45. embedding_fn=CustomAppConfig.embedding_function(
  46. embedding_function=embedding_fn, model=embedding_fn_model,
  47. deployment_name=deployment_name
  48. ),
  49. db=db,
  50. host=host,
  51. port=port,
  52. id=id,
  53. collection_name=collection_name,
  54. )
  55. @staticmethod
  56. def langchain_default_concept(embeddings: Any):
  57. """
  58. Langchains default function layout for embeddings.
  59. """
  60. def embed_function(texts: Documents) -> Embeddings:
  61. return embeddings.embed_documents(texts)
  62. return embed_function
  63. @staticmethod
  64. def embedding_function(embedding_function: EmbeddingFunctions, model: str = None, deployment_name: str = None):
  65. if not isinstance(embedding_function, EmbeddingFunctions):
  66. raise ValueError(
  67. f"Invalid option: '{embedding_function}'. Expecting one of the following options: {list(map(lambda x: x.value, EmbeddingFunctions))}" # noqa: E501
  68. )
  69. if embedding_function == EmbeddingFunctions.OPENAI:
  70. from langchain.embeddings import OpenAIEmbeddings
  71. if model:
  72. embeddings = OpenAIEmbeddings(model=model)
  73. else:
  74. if deployment_name:
  75. embeddings = OpenAIEmbeddings(deployment=deployment_name)
  76. else:
  77. embeddings = OpenAIEmbeddings()
  78. return CustomAppConfig.langchain_default_concept(embeddings)
  79. elif embedding_function == EmbeddingFunctions.HUGGING_FACE:
  80. from langchain.embeddings import HuggingFaceEmbeddings
  81. embeddings = HuggingFaceEmbeddings(model_name=model)
  82. return CustomAppConfig.langchain_default_concept(embeddings)
  83. elif embedding_function == EmbeddingFunctions.VERTEX_AI:
  84. from langchain.embeddings import VertexAIEmbeddings
  85. embeddings = VertexAIEmbeddings(model_name=model)
  86. return CustomAppConfig.langchain_default_concept(embeddings)
  87. elif embedding_function == EmbeddingFunctions.GPT4ALL:
  88. # Note: We could use langchains GPT4ALL embedding, but it's not available in all versions.
  89. from chromadb.utils import embedding_functions
  90. return embedding_functions.SentenceTransformerEmbeddingFunction(model_name=model)