Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Lazy import builders, embedders, (chat)generators, retrievers #8655

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions haystack/components/builders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
71 changes: 56 additions & 15 deletions haystack/components/embedders/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
40 changes: 32 additions & 8 deletions haystack/components/generators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,42 @@
#
# 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",
"OpenAIGenerator",
"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
33 changes: 26 additions & 7 deletions haystack/components/generators/chat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 24 additions & 5 deletions haystack/components/retrievers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading