CustomAppConfig.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. provider: Providers = None,
  21. model=None,
  22. open_source_app_config=None,
  23. ):
  24. """
  25. :param log_level: Optional. (String) Debug level
  26. ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'].
  27. :param embedding_fn: Optional. Embedding function to use.
  28. :param embedding_fn_model: Optional. Model name to use for embedding function.
  29. :param db: Optional. (Vector) database to use for embeddings.
  30. :param id: Optional. ID of the app. Document metadata will have this id.
  31. :param host: Optional. Hostname for the database server.
  32. :param port: Optional. Port for the database server.
  33. :param provider: Optional. (Providers): LLM Provider to use.
  34. :param open_source_app_config: Optional. Config instance needed for open source apps.
  35. """
  36. if provider:
  37. self.provider = provider
  38. else:
  39. raise ValueError("CustomApp must have a provider assigned.")
  40. self.open_source_app_config = open_source_app_config
  41. super().__init__(
  42. log_level=log_level,
  43. embedding_fn=CustomAppConfig.embedding_function(embedding_function=embedding_fn, model=embedding_fn_model),
  44. db=db,
  45. host=host,
  46. port=port,
  47. id=id,
  48. )
  49. @staticmethod
  50. def langchain_default_concept(embeddings: Any):
  51. """
  52. Langchains default function layout for embeddings.
  53. """
  54. def embed_function(texts: Documents) -> Embeddings:
  55. return embeddings.embed_documents(texts)
  56. return embed_function
  57. @staticmethod
  58. def embedding_function(embedding_function: EmbeddingFunctions, model: str = None):
  59. if not isinstance(embedding_function, EmbeddingFunctions):
  60. raise ValueError(
  61. f"Invalid option: '{embedding_function}'. Expecting one of the following options: {list(map(lambda x: x.value, EmbeddingFunctions))}" # noqa: E501
  62. )
  63. if embedding_function == EmbeddingFunctions.OPENAI:
  64. from langchain.embeddings import OpenAIEmbeddings
  65. if model:
  66. embeddings = OpenAIEmbeddings(model=model)
  67. else:
  68. embeddings = OpenAIEmbeddings()
  69. return CustomAppConfig.langchain_default_concept(embeddings)
  70. elif embedding_function == EmbeddingFunctions.HUGGING_FACE:
  71. from langchain.embeddings import HuggingFaceEmbeddings
  72. embeddings = HuggingFaceEmbeddings(model_name=model)
  73. return CustomAppConfig.langchain_default_concept(embeddings)
  74. elif embedding_function == EmbeddingFunctions.VERTEX_AI:
  75. from langchain.embeddings import VertexAIEmbeddings
  76. embeddings = VertexAIEmbeddings(model_name=model)
  77. return CustomAppConfig.langchain_default_concept(embeddings)
  78. elif embedding_function == EmbeddingFunctions.GPT4ALL:
  79. # Note: We could use langchains GPT4ALL embedding, but it's not available in all versions.
  80. from chromadb.utils import embedding_functions
  81. return embedding_functions.SentenceTransformerEmbeddingFunction(model_name=model)