CustomAppConfig.py 3.7 KB

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