factory.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import importlib
  2. def load_class(class_type):
  3. module_path, class_name = class_type.rsplit(".", 1)
  4. module = importlib.import_module(module_path)
  5. return getattr(module, class_name)
  6. class LlmFactory:
  7. provider_to_class = {
  8. "anthropic": "embedchain.llm.anthropic.AnthropicLlm",
  9. "azure_openai": "embedchain.llm.azure_openai.AzureOpenAILlm",
  10. "cohere": "embedchain.llm.cohere.CohereLlm",
  11. "together": "embedchain.llm.together.TogetherLlm",
  12. "gpt4all": "embedchain.llm.gpt4all.GPT4ALLLlm",
  13. "ollama": "embedchain.llm.ollama.OllamaLlm",
  14. "huggingface": "embedchain.llm.huggingface.HuggingFaceLlm",
  15. "jina": "embedchain.llm.jina.JinaLlm",
  16. "llama2": "embedchain.llm.llama2.Llama2Llm",
  17. "openai": "embedchain.llm.openai.OpenAILlm",
  18. "vertexai": "embedchain.llm.vertex_ai.VertexAILlm",
  19. "google": "embedchain.llm.google.GoogleLlm",
  20. "aws_bedrock": "embedchain.llm.aws_bedrock.AWSBedrockLlm",
  21. "mistralai": "embedchain.llm.mistralai.MistralAILlm",
  22. "groq": "embedchain.llm.groq.GroqLlm",
  23. }
  24. provider_to_config_class = {
  25. "embedchain": "embedchain.config.llm.base.BaseLlmConfig",
  26. "openai": "embedchain.config.llm.base.BaseLlmConfig",
  27. "anthropic": "embedchain.config.llm.base.BaseLlmConfig",
  28. }
  29. @classmethod
  30. def create(cls, provider_name, config_data):
  31. class_type = cls.provider_to_class.get(provider_name)
  32. # Default to embedchain base config if the provider is not in the config map
  33. config_name = "embedchain" if provider_name not in cls.provider_to_config_class else provider_name
  34. config_class_type = cls.provider_to_config_class.get(config_name)
  35. if class_type:
  36. llm_class = load_class(class_type)
  37. llm_config_class = load_class(config_class_type)
  38. return llm_class(config=llm_config_class(**config_data))
  39. else:
  40. raise ValueError(f"Unsupported Llm provider: {provider_name}")
  41. class EmbedderFactory:
  42. provider_to_class = {
  43. "azure_openai": "embedchain.embedder.openai.OpenAIEmbedder",
  44. "gpt4all": "embedchain.embedder.gpt4all.GPT4AllEmbedder",
  45. "huggingface": "embedchain.embedder.huggingface.HuggingFaceEmbedder",
  46. "openai": "embedchain.embedder.openai.OpenAIEmbedder",
  47. "vertexai": "embedchain.embedder.vertexai.VertexAIEmbedder",
  48. "google": "embedchain.embedder.google.GoogleAIEmbedder",
  49. "mistralai": "embedchain.embedder.mistralai.MistralAIEmbedder",
  50. }
  51. provider_to_config_class = {
  52. "azure_openai": "embedchain.config.embedder.base.BaseEmbedderConfig",
  53. "openai": "embedchain.config.embedder.base.BaseEmbedderConfig",
  54. "gpt4all": "embedchain.config.embedder.base.BaseEmbedderConfig",
  55. "google": "embedchain.config.embedder.google.GoogleAIEmbedderConfig",
  56. "huggingface": "embedchain.config.embedder.base.BaseEmbedderConfig",
  57. }
  58. @classmethod
  59. def create(cls, provider_name, config_data):
  60. class_type = cls.provider_to_class.get(provider_name)
  61. # Default to openai config if the provider is not in the config map
  62. config_name = "openai" if provider_name not in cls.provider_to_config_class else provider_name
  63. config_class_type = cls.provider_to_config_class.get(config_name)
  64. if class_type:
  65. embedder_class = load_class(class_type)
  66. embedder_config_class = load_class(config_class_type)
  67. return embedder_class(config=embedder_config_class(**config_data))
  68. else:
  69. raise ValueError(f"Unsupported Embedder provider: {provider_name}")
  70. class VectorDBFactory:
  71. provider_to_class = {
  72. "chroma": "embedchain.vectordb.chroma.ChromaDB",
  73. "elasticsearch": "embedchain.vectordb.elasticsearch.ElasticsearchDB",
  74. "opensearch": "embedchain.vectordb.opensearch.OpenSearchDB",
  75. "pinecone": "embedchain.vectordb.pinecone.PineconeDB",
  76. "qdrant": "embedchain.vectordb.qdrant.QdrantDB",
  77. "weaviate": "embedchain.vectordb.weaviate.WeaviateDB",
  78. "zilliz": "embedchain.vectordb.zilliz.ZillizVectorDB",
  79. }
  80. provider_to_config_class = {
  81. "chroma": "embedchain.config.vectordb.chroma.ChromaDbConfig",
  82. "elasticsearch": "embedchain.config.vectordb.elasticsearch.ElasticsearchDBConfig",
  83. "opensearch": "embedchain.config.vectordb.opensearch.OpenSearchDBConfig",
  84. "pinecone": "embedchain.config.vectordb.pinecone.PineconeDBConfig",
  85. "qdrant": "embedchain.config.vectordb.qdrant.QdrantDBConfig",
  86. "weaviate": "embedchain.config.vectordb.weaviate.WeaviateDBConfig",
  87. "zilliz": "embedchain.config.vectordb.zilliz.ZillizDBConfig",
  88. }
  89. @classmethod
  90. def create(cls, provider_name, config_data):
  91. class_type = cls.provider_to_class.get(provider_name)
  92. config_class_type = cls.provider_to_config_class.get(provider_name)
  93. if class_type:
  94. embedder_class = load_class(class_type)
  95. embedder_config_class = load_class(config_class_type)
  96. return embedder_class(config=embedder_config_class(**config_data))
  97. else:
  98. raise ValueError(f"Unsupported Embedder provider: {provider_name}")