CustomAppConfig.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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, deployment_name=deployment_name
  47. ),
  48. db=db,
  49. host=host,
  50. port=port,
  51. id=id,
  52. collection_name=collection_name,
  53. )
  54. @staticmethod
  55. def langchain_default_concept(embeddings: Any):
  56. """
  57. Langchains default function layout for embeddings.
  58. """
  59. def embed_function(texts: Documents) -> Embeddings:
  60. return embeddings.embed_documents(texts)
  61. return embed_function
  62. @staticmethod
  63. def embedding_function(embedding_function: EmbeddingFunctions, model: str = None, deployment_name: str = None):
  64. if not isinstance(embedding_function, EmbeddingFunctions):
  65. raise ValueError(
  66. f"Invalid option: '{embedding_function}'. Expecting one of the following options: {list(map(lambda x: x.value, EmbeddingFunctions))}" # noqa: E501
  67. )
  68. if embedding_function == EmbeddingFunctions.OPENAI:
  69. from langchain.embeddings import OpenAIEmbeddings
  70. if model:
  71. embeddings = OpenAIEmbeddings(model=model)
  72. else:
  73. if deployment_name:
  74. embeddings = OpenAIEmbeddings(deployment=deployment_name)
  75. else:
  76. embeddings = OpenAIEmbeddings()
  77. return CustomAppConfig.langchain_default_concept(embeddings)
  78. elif embedding_function == EmbeddingFunctions.HUGGING_FACE:
  79. from langchain.embeddings import HuggingFaceEmbeddings
  80. embeddings = HuggingFaceEmbeddings(model_name=model)
  81. return CustomAppConfig.langchain_default_concept(embeddings)
  82. elif embedding_function == EmbeddingFunctions.VERTEX_AI:
  83. from langchain.embeddings import VertexAIEmbeddings
  84. embeddings = VertexAIEmbeddings(model_name=model)
  85. return CustomAppConfig.langchain_default_concept(embeddings)
  86. elif embedding_function == EmbeddingFunctions.GPT4ALL:
  87. # Note: We could use langchains GPT4ALL embedding, but it's not available in all versions.
  88. from chromadb.utils import embedding_functions
  89. return embedding_functions.SentenceTransformerEmbeddingFunction(model_name=model)