From 20dd03b4a112616a5e1248fde61ddb0bf1d66fa0 Mon Sep 17 00:00:00 2001 From: Julian Risch Date: Tue, 17 Dec 2024 17:11:47 +0100 Subject: [PATCH] lazy import builders, embedders, (chat)generators, retrievers --- haystack/components/builders/__init__.py | 22 ++++-- haystack/components/embedders/__init__.py | 71 +++++++++++++++---- haystack/components/generators/__init__.py | 40 ++++++++--- .../components/generators/chat/__init__.py | 33 +++++++-- haystack/components/retrievers/__init__.py | 29 ++++++-- 5 files changed, 156 insertions(+), 39 deletions(-) diff --git a/haystack/components/builders/__init__.py b/haystack/components/builders/__init__.py index 5e1ebf6d11..092a004ee2 100644 --- a/haystack/components/builders/__init__.py +++ b/haystack/components/builders/__init__.py @@ -2,8 +2,22 @@ # # SPDX-License-Identifier: Apache-2.0 -from haystack.components.builders.answer_builder import AnswerBuilder -from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder -from haystack.components.builders.prompt_builder import PromptBuilder - __all__ = ["AnswerBuilder", "PromptBuilder", "ChatPromptBuilder"] + + +def AnswerBuilder(): # noqa: D103 + from haystack.components.builders.answer_builder import AnswerBuilder + + return AnswerBuilder + + +def ChatPromptBuilder(): # noqa: D103 + from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder + + return ChatPromptBuilder + + +def PromptBuilder(): # noqa: D103 + from haystack.components.builders.prompt_builder import PromptBuilder + + return PromptBuilder diff --git a/haystack/components/embedders/__init__.py b/haystack/components/embedders/__init__.py index 2b6cf4301e..066fefd286 100644 --- a/haystack/components/embedders/__init__.py +++ b/haystack/components/embedders/__init__.py @@ -2,22 +2,63 @@ # # SPDX-License-Identifier: Apache-2.0 -from haystack.components.embedders.azure_document_embedder import AzureOpenAIDocumentEmbedder -from haystack.components.embedders.azure_text_embedder import AzureOpenAITextEmbedder -from haystack.components.embedders.hugging_face_api_document_embedder import HuggingFaceAPIDocumentEmbedder -from haystack.components.embedders.hugging_face_api_text_embedder import HuggingFaceAPITextEmbedder -from haystack.components.embedders.openai_document_embedder import OpenAIDocumentEmbedder -from haystack.components.embedders.openai_text_embedder import OpenAITextEmbedder -from haystack.components.embedders.sentence_transformers_document_embedder import SentenceTransformersDocumentEmbedder -from haystack.components.embedders.sentence_transformers_text_embedder import SentenceTransformersTextEmbedder - __all__ = [ - "HuggingFaceAPITextEmbedder", + "AzureOpenAIDocumentEmbedder", + "AzureOpenAITextEmbedder", "HuggingFaceAPIDocumentEmbedder", - "SentenceTransformersTextEmbedder", - "SentenceTransformersDocumentEmbedder", - "OpenAITextEmbedder", + "HuggingFaceAPITextEmbedder", "OpenAIDocumentEmbedder", - "AzureOpenAITextEmbedder", - "AzureOpenAIDocumentEmbedder", + "OpenAITextEmbedder", + "SentenceTransformersDocumentEmbedder", + "SentenceTransformersTextEmbedder", ] + + +def AzureOpenAIDocumentEmbedder(): # noqa: D103 + from haystack.components.embedders.azure_document_embedder import AzureOpenAIDocumentEmbedder + + return AzureOpenAIDocumentEmbedder + + +def AzureOpenAITextEmbedder(): # noqa: D103 + from haystack.components.embedders.azure_text_embedder import AzureOpenAITextEmbedder + + return AzureOpenAITextEmbedder + + +def HuggingFaceAPIDocumentEmbedder(): # noqa: D103 + from haystack.components.embedders.hugging_face_api_document_embedder import HuggingFaceAPIDocumentEmbedder + + return HuggingFaceAPIDocumentEmbedder + + +def HuggingFaceAPITextEmbedder(): # noqa: D103 + from haystack.components.embedders.hugging_face_api_text_embedder import HuggingFaceAPITextEmbedder + + return HuggingFaceAPITextEmbedder + + +def OpenAIDocumentEmbedder(): # noqa: D103 + from haystack.components.embedders.openai_document_embedder import OpenAIDocumentEmbedder + + return OpenAIDocumentEmbedder + + +def OpenAITextEmbedder(): # noqa: D103 + from haystack.components.embedders.openai_text_embedder import OpenAITextEmbedder + + return OpenAITextEmbedder + + +def SentenceTransformersDocumentEmbedder(): # noqa: D103 + from haystack.components.embedders.sentence_transformers_document_embedder import ( + SentenceTransformersDocumentEmbedder, + ) + + return SentenceTransformersDocumentEmbedder + + +def SentenceTransformersTextEmbedder(): # noqa: D103 + from haystack.components.embedders.sentence_transformers_text_embedder import SentenceTransformersTextEmbedder + + return SentenceTransformersTextEmbedder diff --git a/haystack/components/generators/__init__.py b/haystack/components/generators/__init__.py index 952c2dadd2..ad62661402 100644 --- a/haystack/components/generators/__init__.py +++ b/haystack/components/generators/__init__.py @@ -2,14 +2,6 @@ # # SPDX-License-Identifier: Apache-2.0 -from haystack.components.generators.openai import ( # noqa: I001 (otherwise we end up with partial imports) - OpenAIGenerator, -) -from haystack.components.generators.azure import AzureOpenAIGenerator -from haystack.components.generators.hugging_face_local import HuggingFaceLocalGenerator -from haystack.components.generators.hugging_face_api import HuggingFaceAPIGenerator -from haystack.components.generators.openai_dalle import DALLEImageGenerator - __all__ = [ "HuggingFaceLocalGenerator", "HuggingFaceAPIGenerator", @@ -17,3 +9,35 @@ "AzureOpenAIGenerator", "DALLEImageGenerator", ] + + +def HuggingFaceLocalGenerator(): # noqa: D103 + from haystack.components.generators.hugging_face_local import HuggingFaceLocalGenerator + + return HuggingFaceLocalGenerator + + +def HuggingFaceAPIGenerator(): # noqa: D103 + from haystack.components.generators.hugging_face_api import HuggingFaceAPIGenerator + + return HuggingFaceAPIGenerator + + +def OpenAIGenerator(): # noqa: D103 + from haystack.components.generators.openai import ( # noqa: I001 (otherwise we end up with partial imports) + OpenAIGenerator, + ) + + return OpenAIGenerator + + +def AzureOpenAIGenerator(): # noqa: D103 + from haystack.components.generators.azure import AzureOpenAIGenerator + + return AzureOpenAIGenerator + + +def DALLEImageGenerator(): # noqa: D103 + from haystack.components.generators.openai_dalle import DALLEImageGenerator + + return DALLEImageGenerator diff --git a/haystack/components/generators/chat/__init__.py b/haystack/components/generators/chat/__init__.py index 1c7ecbad8d..7da5245735 100644 --- a/haystack/components/generators/chat/__init__.py +++ b/haystack/components/generators/chat/__init__.py @@ -2,16 +2,35 @@ # # SPDX-License-Identifier: Apache-2.0 -from haystack.components.generators.chat.openai import ( # noqa: I001 (otherwise we end up with partial imports) - OpenAIChatGenerator, -) -from haystack.components.generators.chat.azure import AzureOpenAIChatGenerator -from haystack.components.generators.chat.hugging_face_local import HuggingFaceLocalChatGenerator -from haystack.components.generators.chat.hugging_face_api import HuggingFaceAPIChatGenerator - __all__ = [ "HuggingFaceLocalChatGenerator", "HuggingFaceAPIChatGenerator", "OpenAIChatGenerator", "AzureOpenAIChatGenerator", ] + + +def AzureOpenAIChatGenerator(): # noqa: D103 + from haystack.components.generators.chat.azure import AzureOpenAIChatGenerator + + return AzureOpenAIChatGenerator + + +def HuggingFaceLocalChatGenerator(): # noqa: D103 + from haystack.components.generators.chat.hugging_face_local import HuggingFaceLocalChatGenerator + + return HuggingFaceLocalChatGenerator + + +def HuggingFaceAPIChatGenerator(): # noqa: D103 + from haystack.components.generators.chat.hugging_face_api import HuggingFaceAPIChatGenerator + + return HuggingFaceAPIChatGenerator + + +def OpenAIChatGenerator(): # noqa: D103 + from haystack.components.generators.chat.openai import ( # noqa: I001 (otherwise we end up with partial imports) + OpenAIChatGenerator, + ) + + return OpenAIChatGenerator diff --git a/haystack/components/retrievers/__init__.py b/haystack/components/retrievers/__init__.py index 91d1288a19..372d47990e 100644 --- a/haystack/components/retrievers/__init__.py +++ b/haystack/components/retrievers/__init__.py @@ -2,9 +2,28 @@ # # SPDX-License-Identifier: Apache-2.0 -from haystack.components.retrievers.filter_retriever import FilterRetriever -from haystack.components.retrievers.in_memory.bm25_retriever import InMemoryBM25Retriever -from haystack.components.retrievers.in_memory.embedding_retriever import InMemoryEmbeddingRetriever -from haystack.components.retrievers.sentence_window_retriever import SentenceWindowRetriever - __all__ = ["FilterRetriever", "InMemoryEmbeddingRetriever", "InMemoryBM25Retriever", "SentenceWindowRetriever"] + + +def FilterRetriever(): # noqa: D103 + from haystack.components.retrievers.filter_retriever import FilterRetriever + + return FilterRetriever + + +def InMemoryBM25Retriever(): # noqa: D103 + from haystack.components.retrievers.in_memory.bm25_retriever import InMemoryBM25Retriever + + return InMemoryBM25Retriever + + +def InMemoryEmbeddingRetriever(): # noqa: D103 + from haystack.components.retrievers.in_memory.embedding_retriever import InMemoryEmbeddingRetriever + + return InMemoryEmbeddingRetriever + + +def SentenceWindowRetriever(): # noqa: D103 + from haystack.components.retrievers.sentence_window_retriever import SentenceWindowRetriever + + return SentenceWindowRetriever