| title | Embedders |
|---|---|
| id | embedders-api |
| description | Transforms queries into vectors to look for similar or relevant Documents. |
| slug | /embedders-api |
Bases: OpenAIDocumentEmbedder
Calculates document embeddings using OpenAI models deployed on Azure.
{/* test-ignore */}
from haystack import Document
from haystack.components.embedders import AzureOpenAIDocumentEmbedder
doc = Document(content="I love pizza!")
document_embedder = AzureOpenAIDocumentEmbedder()
result = document_embedder.run([doc])
print(result['documents'][0].embedding)
# [0.017020374536514282, -0.023255806416273117, ...]__init__(
azure_endpoint: str | None = None,
api_version: str | None = "2023-05-15",
azure_deployment: str = "text-embedding-ada-002",
dimensions: int | None = None,
api_key: Secret | None = Secret.from_env_var(
"AZURE_OPENAI_API_KEY", strict=False
),
azure_ad_token: Secret | None = Secret.from_env_var(
"AZURE_OPENAI_AD_TOKEN", strict=False
),
organization: str | None = None,
prefix: str = "",
suffix: str = "",
batch_size: int = 32,
progress_bar: bool = True,
meta_fields_to_embed: list[str] | None = None,
embedding_separator: str = "\n",
timeout: float | None = None,
max_retries: int | None = None,
*,
default_headers: dict[str, str] | None = None,
azure_ad_token_provider: AzureADTokenProvider | None = None,
http_client_kwargs: dict[str, Any] | None = None,
raise_on_failure: bool = False
) -> NoneCreates an AzureOpenAIDocumentEmbedder component.
Parameters:
- azure_endpoint (
str | None) – The endpoint of the model deployed on Azure. - api_version (
str | None) – The version of the API to use. - azure_deployment (
str) – The name of the model deployed on Azure. The default model is text-embedding-ada-002. - dimensions (
int | None) – The number of dimensions of the resulting embeddings. Only supported in text-embedding-3 and later models. - api_key (
Secret | None) – The Azure OpenAI API key. You can set it with an environment variableAZURE_OPENAI_API_KEY, or pass with this parameter during initialization. - azure_ad_token (
Secret | None) – Microsoft Entra ID token, see Microsoft's Entra ID documentation for more information. You can set it with an environment variableAZURE_OPENAI_AD_TOKEN, or pass with this parameter during initialization. Previously called Azure Active Directory. - organization (
str | None) – Your organization ID. See OpenAI's Setting Up Your Organization for more information. - prefix (
str) – A string to add at the beginning of each text. - suffix (
str) – A string to add at the end of each text. - batch_size (
int) – Number of documents to embed at once. - progress_bar (
bool) – IfTrue, shows a progress bar when running. - meta_fields_to_embed (
list[str] | None) – List of metadata fields to embed along with the document text. - embedding_separator (
str) – Separator used to concatenate the metadata fields to the document text. - timeout (
float | None) – The timeout forAzureOpenAIclient calls, in seconds. If not set, defaults to either theOPENAI_TIMEOUTenvironment variable, or 30 seconds. - max_retries (
int | None) – Maximum number of retries to contact AzureOpenAI after an internal error. If not set, defaults to either theOPENAI_MAX_RETRIESenvironment variable or to 5 retries. - default_headers (
dict[str, str] | None) – Default headers to send to the AzureOpenAI client. - azure_ad_token_provider (
AzureADTokenProvider | None) – A function that returns an Azure Active Directory token, will be invoked on every request. - http_client_kwargs (
dict[str, Any] | None) – A dictionary of keyword arguments to configure a customhttpx.Clientorhttpx.AsyncClient. For more information, see the HTTPX documentation. - raise_on_failure (
bool) – Whether to raise an exception if the embedding request fails. IfFalse, the component will log the error and continue processing the remaining documents. IfTrue, it will raise an exception on failure.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> AzureOpenAIDocumentEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
AzureOpenAIDocumentEmbedder– Deserialized component.
Bases: OpenAITextEmbedder
Embeds strings using OpenAI models deployed on Azure.
{/* test-ignore */}
from haystack.components.embedders import AzureOpenAITextEmbedder
text_to_embed = "I love pizza!"
text_embedder = AzureOpenAITextEmbedder()
print(text_embedder.run(text_to_embed))
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
# 'meta': {'model': 'text-embedding-ada-002-v2',
# 'usage': {'prompt_tokens': 4, 'total_tokens': 4}}}__init__(
azure_endpoint: str | None = None,
api_version: str | None = "2023-05-15",
azure_deployment: str = "text-embedding-ada-002",
dimensions: int | None = None,
api_key: Secret | None = Secret.from_env_var(
"AZURE_OPENAI_API_KEY", strict=False
),
azure_ad_token: Secret | None = Secret.from_env_var(
"AZURE_OPENAI_AD_TOKEN", strict=False
),
organization: str | None = None,
timeout: float | None = None,
max_retries: int | None = None,
prefix: str = "",
suffix: str = "",
*,
default_headers: dict[str, str] | None = None,
azure_ad_token_provider: AzureADTokenProvider | None = None,
http_client_kwargs: dict[str, Any] | None = None
) -> NoneCreates an AzureOpenAITextEmbedder component.
Parameters:
- azure_endpoint (
str | None) – The endpoint of the model deployed on Azure. - api_version (
str | None) – The version of the API to use. - azure_deployment (
str) – The name of the model deployed on Azure. The default model is text-embedding-ada-002. - dimensions (
int | None) – The number of dimensions the resulting output embeddings should have. Only supported in text-embedding-3 and later models. - api_key (
Secret | None) – The Azure OpenAI API key. You can set it with an environment variableAZURE_OPENAI_API_KEY, or pass with this parameter during initialization. - azure_ad_token (
Secret | None) – Microsoft Entra ID token, see Microsoft's Entra ID documentation for more information. You can set it with an environment variableAZURE_OPENAI_AD_TOKEN, or pass with this parameter during initialization. Previously called Azure Active Directory. - organization (
str | None) – Your organization ID. See OpenAI's Setting Up Your Organization for more information. - timeout (
float | None) – The timeout forAzureOpenAIclient calls, in seconds. If not set, defaults to either theOPENAI_TIMEOUTenvironment variable, or 30 seconds. - max_retries (
int | None) – Maximum number of retries to contact AzureOpenAI after an internal error. If not set, defaults to either theOPENAI_MAX_RETRIESenvironment variable, or to 5 retries. - prefix (
str) – A string to add at the beginning of each text. - suffix (
str) – A string to add at the end of each text. - default_headers (
dict[str, str] | None) – Default headers to send to the AzureOpenAI client. - azure_ad_token_provider (
AzureADTokenProvider | None) – A function that returns an Azure Active Directory token, will be invoked on every request. - http_client_kwargs (
dict[str, Any] | None) – A dictionary of keyword arguments to configure a customhttpx.Clientorhttpx.AsyncClient. For more information, see the HTTPX documentation.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> AzureOpenAITextEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
AzureOpenAITextEmbedder– Deserialized component.
Embeds documents using Hugging Face APIs.
Use it with the following Hugging Face APIs:
{/* test-ignore */}
from haystack.components.embedders import HuggingFaceAPIDocumentEmbedder
from haystack.utils import Secret
from haystack.dataclasses import Document
doc = Document(content="I love pizza!")
doc_embedder = HuggingFaceAPIDocumentEmbedder(api_type="serverless_inference_api",
api_params={"model": "BAAI/bge-small-en-v1.5"},
token=Secret.from_token("<your-api-key>"))
result = document_embedder.run([doc])
print(result["documents"][0].embedding)
# [0.017020374536514282, -0.023255806416273117, ...]{/* test-ignore */}
from haystack.components.embedders import HuggingFaceAPIDocumentEmbedder
from haystack.utils import Secret
from haystack.dataclasses import Document
doc = Document(content="I love pizza!")
doc_embedder = HuggingFaceAPIDocumentEmbedder(api_type="inference_endpoints",
api_params={"url": "<your-inference-endpoint-url>"},
token=Secret.from_token("<your-api-key>"))
result = document_embedder.run([doc])
print(result["documents"][0].embedding)
# [0.017020374536514282, -0.023255806416273117, ...]{/* test-ignore */}
from haystack.components.embedders import HuggingFaceAPIDocumentEmbedder
from haystack.dataclasses import Document
doc = Document(content="I love pizza!")
doc_embedder = HuggingFaceAPIDocumentEmbedder(api_type="text_embeddings_inference",
api_params={"url": "http://localhost:8080"})
result = document_embedder.run([doc])
print(result["documents"][0].embedding)
# [0.017020374536514282, -0.023255806416273117, ...]__init__(
api_type: HFEmbeddingAPIType | str,
api_params: dict[str, str],
token: Secret | None = Secret.from_env_var(
["HF_API_TOKEN", "HF_TOKEN"], strict=False
),
prefix: str = "",
suffix: str = "",
truncate: bool | None = True,
normalize: bool | None = False,
batch_size: int = 32,
progress_bar: bool = True,
meta_fields_to_embed: list[str] | None = None,
embedding_separator: str = "\n",
concurrency_limit: int = 4,
) -> NoneCreates a HuggingFaceAPIDocumentEmbedder component.
Parameters:
- api_type (
HFEmbeddingAPIType | str) – The type of Hugging Face API to use. - api_params (
dict[str, str]) – A dictionary with the following keys: model: Hugging Face model ID. Required whenapi_typeisSERVERLESS_INFERENCE_API.url: URL of the inference endpoint. Required whenapi_typeisINFERENCE_ENDPOINTSorTEXT_EMBEDDINGS_INFERENCE.- token (
Secret | None) – The Hugging Face token to use as HTTP bearer authorization. Check your HF token in your account settings. - prefix (
str) – A string to add at the beginning of each text. - suffix (
str) – A string to add at the end of each text. - truncate (
bool | None) – Truncates the input text to the maximum length supported by the model. Applicable whenapi_typeisTEXT_EMBEDDINGS_INFERENCE, orINFERENCE_ENDPOINTSif the backend uses Text Embeddings Inference. Ifapi_typeisSERVERLESS_INFERENCE_API, this parameter is ignored. - normalize (
bool | None) – Normalizes the embeddings to unit length. Applicable whenapi_typeisTEXT_EMBEDDINGS_INFERENCE, orINFERENCE_ENDPOINTSif the backend uses Text Embeddings Inference. Ifapi_typeisSERVERLESS_INFERENCE_API, this parameter is ignored. - batch_size (
int) – Number of documents to process at once. - progress_bar (
bool) – IfTrue, shows a progress bar when running. - meta_fields_to_embed (
list[str] | None) – List of metadata fields to embed along with the document text. - embedding_separator (
str) – Separator used to concatenate the metadata fields to the document text. - concurrency_limit (
int) – The maximum number of requests that should be allowed to run concurrently. This parameter is only used in therun_asyncmethod.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> HuggingFaceAPIDocumentEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
HuggingFaceAPIDocumentEmbedder– Deserialized component.
run(documents: list[Document]) -> dict[str, list[Document]]Embeds a list of documents.
Parameters:
- documents (
list[Document]) – Documents to embed.
Returns:
dict[str, list[Document]]– A dictionary with the following keys:documents: A list of documents with embeddings.
run_async(documents: list[Document]) -> dict[str, list[Document]]Embeds a list of documents asynchronously.
Parameters:
- documents (
list[Document]) – Documents to embed.
Returns:
dict[str, list[Document]]– A dictionary with the following keys:documents: A list of documents with embeddings.
Embeds strings using Hugging Face APIs.
Use it with the following Hugging Face APIs:
{/* test-ignore */}
from haystack.components.embedders import HuggingFaceAPITextEmbedder
from haystack.utils import Secret
text_embedder = HuggingFaceAPITextEmbedder(api_type="serverless_inference_api",
api_params={"model": "BAAI/bge-small-en-v1.5"},
token=Secret.from_token("<your-api-key>"))
print(text_embedder.run("I love pizza!"))
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],{/* test-ignore */}
from haystack.components.embedders import HuggingFaceAPITextEmbedder
from haystack.utils import Secret
text_embedder = HuggingFaceAPITextEmbedder(api_type="inference_endpoints",
api_params={"model": "BAAI/bge-small-en-v1.5"},
token=Secret.from_token("<your-api-key>"))
print(text_embedder.run("I love pizza!"))
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],{/* test-ignore */}
from haystack.components.embedders import HuggingFaceAPITextEmbedder
from haystack.utils import Secret
text_embedder = HuggingFaceAPITextEmbedder(api_type="text_embeddings_inference",
api_params={"url": "http://localhost:8080"})
print(text_embedder.run("I love pizza!"))
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],__init__(
api_type: HFEmbeddingAPIType | str,
api_params: dict[str, str],
token: Secret | None = Secret.from_env_var(
["HF_API_TOKEN", "HF_TOKEN"], strict=False
),
prefix: str = "",
suffix: str = "",
truncate: bool | None = True,
normalize: bool | None = False,
) -> NoneCreates a HuggingFaceAPITextEmbedder component.
Parameters:
- api_type (
HFEmbeddingAPIType | str) – The type of Hugging Face API to use. - api_params (
dict[str, str]) – A dictionary with the following keys: model: Hugging Face model ID. Required whenapi_typeisSERVERLESS_INFERENCE_API.url: URL of the inference endpoint. Required whenapi_typeisINFERENCE_ENDPOINTSorTEXT_EMBEDDINGS_INFERENCE.- token (
Secret | None) – The Hugging Face token to use as HTTP bearer authorization. Check your HF token in your account settings. - prefix (
str) – A string to add at the beginning of each text. - suffix (
str) – A string to add at the end of each text. - truncate (
bool | None) – Truncates the input text to the maximum length supported by the model. Applicable whenapi_typeisTEXT_EMBEDDINGS_INFERENCE, orINFERENCE_ENDPOINTSif the backend uses Text Embeddings Inference. Ifapi_typeisSERVERLESS_INFERENCE_API, this parameter is ignored. - normalize (
bool | None) – Normalizes the embeddings to unit length. Applicable whenapi_typeisTEXT_EMBEDDINGS_INFERENCE, orINFERENCE_ENDPOINTSif the backend uses Text Embeddings Inference. Ifapi_typeisSERVERLESS_INFERENCE_API, this parameter is ignored.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> HuggingFaceAPITextEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
HuggingFaceAPITextEmbedder– Deserialized component.
run(text: str) -> dict[str, Any]Embeds a single string.
Parameters:
- text (
str) – Text to embed.
Returns:
dict[str, Any]– A dictionary with the following keys:embedding: The embedding of the input text.
run_async(text: str) -> dict[str, Any]Embeds a single string asynchronously.
Parameters:
- text (
str) – Text to embed.
Returns:
dict[str, Any]– A dictionary with the following keys:embedding: The embedding of the input text.
A component for computing Document embeddings based on images using Sentence Transformers models.
The embedding of each Document is stored in the embedding field of the Document.
{/* test-ignore */}
from haystack import Document
from haystack.components.embedders.image import SentenceTransformersDocumentImageEmbedder
embedder = SentenceTransformersDocumentImageEmbedder(model="sentence-transformers/clip-ViT-B-32")
documents = [
Document(content="A photo of a cat", meta={"file_path": "cat.jpg"}),
Document(content="A photo of a dog", meta={"file_path": "dog.jpg"}),
]
result = embedder.run(documents=documents)
documents_with_embeddings = result["documents"]
print(documents_with_embeddings)
# [Document(id=...,
# content='A photo of a cat',
# meta={'file_path': 'cat.jpg',
# 'embedding_source': {'type': 'image', 'file_path_meta_field': 'file_path'}},
# embedding=vector of size 512),
# ...]__init__(
*,
file_path_meta_field: str = "file_path",
root_path: str | None = None,
model: str = "sentence-transformers/clip-ViT-B-32",
device: ComponentDevice | None = None,
token: Secret | None = Secret.from_env_var(
["HF_API_TOKEN", "HF_TOKEN"], strict=False
),
batch_size: int = 32,
progress_bar: bool = True,
normalize_embeddings: bool = False,
trust_remote_code: bool = False,
local_files_only: bool = False,
model_kwargs: dict[str, Any] | None = None,
tokenizer_kwargs: dict[str, Any] | None = None,
config_kwargs: dict[str, Any] | None = None,
precision: Literal[
"float32", "int8", "uint8", "binary", "ubinary"
] = "float32",
encode_kwargs: dict[str, Any] | None = None,
backend: Literal["torch", "onnx", "openvino"] = "torch"
) -> NoneCreates a SentenceTransformersDocumentEmbedder component.
Parameters:
- file_path_meta_field (
str) – The metadata field in the Document that contains the file path to the image or PDF. - root_path (
str | None) – The root directory path where document files are located. If provided, file paths in document metadata will be resolved relative to this path. If None, file paths are treated as absolute paths. - model (
str) – The Sentence Transformers model to use for calculating embeddings. Pass a local path or ID of the model on Hugging Face. To be used with this component, the model must be able to embed images and text into the same vector space. Compatible models include: - "sentence-transformers/clip-ViT-B-32"
- "sentence-transformers/clip-ViT-L-14"
- "sentence-transformers/clip-ViT-B-16"
- "sentence-transformers/clip-ViT-B-32-multilingual-v1"
- "jinaai/jina-embeddings-v4"
- "jinaai/jina-clip-v1"
- "jinaai/jina-clip-v2".
- device (
ComponentDevice | None) – The device to use for loading the model. Overrides the default device. - token (
Secret | None) – The API token to download private models from Hugging Face. - batch_size (
int) – Number of documents to embed at once. - progress_bar (
bool) – IfTrue, shows a progress bar when embedding documents. - normalize_embeddings (
bool) – IfTrue, the embeddings are normalized using L2 normalization, so that each embedding has a norm of 1. - trust_remote_code (
bool) – IfFalse, allows only Hugging Face verified model architectures. IfTrue, allows custom models and scripts. - local_files_only (
bool) – IfTrue, does not attempt to download the model from Hugging Face Hub and only looks at local files. - model_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoModelForSequenceClassification.from_pretrainedwhen loading the model. Refer to specific model documentation for available kwargs. - tokenizer_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoTokenizer.from_pretrainedwhen loading the tokenizer. Refer to specific model documentation for available kwargs. - config_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoConfig.from_pretrainedwhen loading the model configuration. - precision (
Literal['float32', 'int8', 'uint8', 'binary', 'ubinary']) – The precision to use for the embeddings. All non-float32 precisions are quantized embeddings. Quantized embeddings are smaller and faster to compute, but may have a lower accuracy. They are useful for reducing the size of the embeddings of a corpus for semantic search, among other tasks. - encode_kwargs (
dict[str, Any] | None) – Additional keyword arguments forSentenceTransformer.encodewhen embedding documents. This parameter is provided for fine customization. Be careful not to clash with already set parameters and avoid passing parameters that change the output type. - backend (
Literal['torch', 'onnx', 'openvino']) – The backend to use for the Sentence Transformers model. Choose from "torch", "onnx", or "openvino". Refer to the Sentence Transformers documentation for more information on acceleration and quantization options.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> SentenceTransformersDocumentImageEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
SentenceTransformersDocumentImageEmbedder– Deserialized component.
warm_up() -> NoneInitializes the component.
run(documents: list[Document]) -> dict[str, list[Document]]Embed a list of documents.
Parameters:
- documents (
list[Document]) – Documents to embed.
Returns:
dict[str, list[Document]]– A dictionary with the following keys:documents: Documents with embeddings.
Computes document embeddings using OpenAI models.
{/* test-ignore */}
from haystack import Document
from haystack.components.embedders import OpenAIDocumentEmbedder
doc = Document(content="I love pizza!")
document_embedder = OpenAIDocumentEmbedder()
result = document_embedder.run([doc])
print(result['documents'][0].embedding)
# [0.017020374536514282, -0.023255806416273117, ...]__init__(
api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"),
model: str = "text-embedding-ada-002",
dimensions: int | None = None,
api_base_url: str | None = None,
organization: str | None = None,
prefix: str = "",
suffix: str = "",
batch_size: int = 32,
progress_bar: bool = True,
meta_fields_to_embed: list[str] | None = None,
embedding_separator: str = "\n",
timeout: float | None = None,
max_retries: int | None = None,
http_client_kwargs: dict[str, Any] | None = None,
*,
raise_on_failure: bool = False
) -> NoneCreates an OpenAIDocumentEmbedder component.
Before initializing the component, you can set the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES'
environment variables to override the timeout and max_retries parameters respectively
in the OpenAI client.
Parameters:
- api_key (
Secret) – The OpenAI API key. You can set it with an environment variableOPENAI_API_KEY, or pass with this parameter during initialization. - model (
str) – The name of the model to use for calculating embeddings. The default model istext-embedding-ada-002. - dimensions (
int | None) – The number of dimensions of the resulting embeddings. Onlytext-embedding-3and later models support this parameter. - api_base_url (
str | None) – Overrides the default base URL for all HTTP requests. - organization (
str | None) – Your OpenAI organization ID. See OpenAI's Setting Up Your Organization for more information. - prefix (
str) – A string to add at the beginning of each text. - suffix (
str) – A string to add at the end of each text. - batch_size (
int) – Number of documents to embed at once. - progress_bar (
bool) – IfTrue, shows a progress bar when running. - meta_fields_to_embed (
list[str] | None) – List of metadata fields to embed along with the document text. - embedding_separator (
str) – Separator used to concatenate the metadata fields to the document text. - timeout (
float | None) – Timeout for OpenAI client calls. If not set, it defaults to either theOPENAI_TIMEOUTenvironment variable, or 30 seconds. - max_retries (
int | None) – Maximum number of retries to contact OpenAI after an internal error. If not set, it defaults to either theOPENAI_MAX_RETRIESenvironment variable, or 5 retries. - http_client_kwargs (
dict[str, Any] | None) – A dictionary of keyword arguments to configure a customhttpx.Clientorhttpx.AsyncClient. For more information, see the HTTPX documentation. - raise_on_failure (
bool) – Whether to raise an exception if the embedding request fails. IfFalse, the component will log the error and continue processing the remaining documents. IfTrue, it will raise an exception on failure.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> OpenAIDocumentEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
OpenAIDocumentEmbedder– Deserialized component.
run(documents: list[Document]) -> dict[str, Any]Embeds a list of documents.
Parameters:
- documents (
list[Document]) – A list of documents to embed.
Returns:
dict[str, Any]– A dictionary with the following keys:documents: A list of documents with embeddings.meta: Information about the usage of the model.
run_async(documents: list[Document]) -> dict[str, Any]Embeds a list of documents asynchronously.
Parameters:
- documents (
list[Document]) – A list of documents to embed.
Returns:
dict[str, Any]– A dictionary with the following keys:documents: A list of documents with embeddings.meta: Information about the usage of the model.
Embeds strings using OpenAI models.
You can use it to embed user query and send it to an embedding Retriever.
{/* test-ignore */}
from haystack.components.embedders import OpenAITextEmbedder
text_to_embed = "I love pizza!"
text_embedder = OpenAITextEmbedder()
print(text_embedder.run(text_to_embed))
# {'embedding': [0.017020374536514282, -0.023255806416273117, ...],
# 'meta': {'model': 'text-embedding-ada-002-v2',
# 'usage': {'prompt_tokens': 4, 'total_tokens': 4}}}__init__(
api_key: Secret = Secret.from_env_var("OPENAI_API_KEY"),
model: str = "text-embedding-ada-002",
dimensions: int | None = None,
api_base_url: str | None = None,
organization: str | None = None,
prefix: str = "",
suffix: str = "",
timeout: float | None = None,
max_retries: int | None = None,
http_client_kwargs: dict[str, Any] | None = None,
) -> NoneCreates an OpenAITextEmbedder component.
Before initializing the component, you can set the 'OPENAI_TIMEOUT' and 'OPENAI_MAX_RETRIES'
environment variables to override the timeout and max_retries parameters respectively
in the OpenAI client.
Parameters:
- api_key (
Secret) – The OpenAI API key. You can set it with an environment variableOPENAI_API_KEY, or pass with this parameter during initialization. - model (
str) – The name of the model to use for calculating embeddings. The default model istext-embedding-ada-002. - dimensions (
int | None) – The number of dimensions of the resulting embeddings. Onlytext-embedding-3and later models support this parameter. - api_base_url (
str | None) – Overrides default base URL for all HTTP requests. - organization (
str | None) – Your organization ID. See OpenAI's production best practices for more information. - prefix (
str) – A string to add at the beginning of each text to embed. - suffix (
str) – A string to add at the end of each text to embed. - timeout (
float | None) – Timeout for OpenAI client calls. If not set, it defaults to either theOPENAI_TIMEOUTenvironment variable, or 30 seconds. - max_retries (
int | None) – Maximum number of retries to contact OpenAI after an internal error. If not set, it defaults to either theOPENAI_MAX_RETRIESenvironment variable, or set to 5. - http_client_kwargs (
dict[str, Any] | None) – A dictionary of keyword arguments to configure a customhttpx.Clientorhttpx.AsyncClient. For more information, see the HTTPX documentation.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> OpenAITextEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
OpenAITextEmbedder– Deserialized component.
run(text: str) -> dict[str, Any]Embeds a single string.
Parameters:
- text (
str) – Text to embed.
Returns:
dict[str, Any]– A dictionary with the following keys:embedding: The embedding of the input text.meta: Information about the usage of the model.
run_async(text: str) -> dict[str, Any]Asynchronously embed a single string.
This is the asynchronous version of the run method. It has the same parameters and return values
but can be used with await in async code.
Parameters:
- text (
str) – Text to embed.
Returns:
dict[str, Any]– A dictionary with the following keys:embedding: The embedding of the input text.meta: Information about the usage of the model.
Calculates document embeddings using Sentence Transformers models.
It stores the embeddings in the embedding metadata field of each document.
You can also embed documents' metadata.
Use this component in indexing pipelines to embed input documents
and send them to DocumentWriter to write into a Document Store.
{/* test-ignore */}
from haystack import Document
from haystack.components.embedders import SentenceTransformersDocumentEmbedder
doc = Document(content="I love pizza!")
doc_embedder = SentenceTransformersDocumentEmbedder()
result = doc_embedder.run([doc])
print(result['documents'][0].embedding)
# [-0.07804739475250244, 0.1498992145061493, ...]__init__(
model: str = "sentence-transformers/all-mpnet-base-v2",
device: ComponentDevice | None = None,
token: Secret | None = Secret.from_env_var(
["HF_API_TOKEN", "HF_TOKEN"], strict=False
),
prefix: str = "",
suffix: str = "",
batch_size: int = 32,
progress_bar: bool = True,
normalize_embeddings: bool = False,
meta_fields_to_embed: list[str] | None = None,
embedding_separator: str = "\n",
trust_remote_code: bool = False,
local_files_only: bool = False,
truncate_dim: int | None = None,
model_kwargs: dict[str, Any] | None = None,
tokenizer_kwargs: dict[str, Any] | None = None,
config_kwargs: dict[str, Any] | None = None,
precision: Literal[
"float32", "int8", "uint8", "binary", "ubinary"
] = "float32",
encode_kwargs: dict[str, Any] | None = None,
backend: Literal["torch", "onnx", "openvino"] = "torch",
revision: str | None = None,
) -> NoneCreates a SentenceTransformersDocumentEmbedder component.
Parameters:
- model (
str) – The model to use for calculating embeddings. Pass a local path or ID of the model on Hugging Face. - device (
ComponentDevice | None) – The device to use for loading the model. Overrides the default device. - token (
Secret | None) – The API token to download private models from Hugging Face. - prefix (
str) – A string to add at the beginning of each document text. Can be used to prepend the text with an instruction, as required by some embedding models, such as E5 and bge. - suffix (
str) – A string to add at the end of each document text. - batch_size (
int) – Number of documents to embed at once. - progress_bar (
bool) – IfTrue, shows a progress bar when embedding documents. - normalize_embeddings (
bool) – IfTrue, the embeddings are normalized using L2 normalization, so that each embedding has a norm of 1. - meta_fields_to_embed (
list[str] | None) – List of metadata fields to embed along with the document text. - embedding_separator (
str) – Separator used to concatenate the metadata fields to the document text. - trust_remote_code (
bool) – IfFalse, allows only Hugging Face verified model architectures. IfTrue, allows custom models and scripts. - local_files_only (
bool) – IfTrue, does not attempt to download the model from Hugging Face Hub and only looks at local files. - truncate_dim (
int | None) – The dimension to truncate sentence embeddings to.Nonedoes no truncation. If the model wasn't trained with Matryoshka Representation Learning, truncating embeddings can significantly affect performance. - model_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoModelForSequenceClassification.from_pretrainedwhen loading the model. Refer to specific model documentation for available kwargs. - tokenizer_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoTokenizer.from_pretrainedwhen loading the tokenizer. Refer to specific model documentation for available kwargs. - config_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoConfig.from_pretrainedwhen loading the model configuration. - precision (
Literal['float32', 'int8', 'uint8', 'binary', 'ubinary']) – The precision to use for the embeddings. All non-float32 precisions are quantized embeddings. Quantized embeddings are smaller and faster to compute, but may have a lower accuracy. They are useful for reducing the size of the embeddings of a corpus for semantic search, among other tasks. - encode_kwargs (
dict[str, Any] | None) – Additional keyword arguments forSentenceTransformer.encodewhen embedding documents. This parameter is provided for fine customization. Be careful not to clash with already set parameters and avoid passing parameters that change the output type. - backend (
Literal['torch', 'onnx', 'openvino']) – The backend to use for the Sentence Transformers model. Choose from "torch", "onnx", or "openvino". Refer to the Sentence Transformers documentation for more information on acceleration and quantization options. - revision (
str | None) – The specific model version to use. It can be a branch name, a tag name, or a commit id, for a stored model on Hugging Face.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> SentenceTransformersDocumentEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
SentenceTransformersDocumentEmbedder– Deserialized component.
warm_up() -> NoneInitializes the component.
run(documents: list[Document]) -> dict[str, list[Document]]Embed a list of documents.
Parameters:
- documents (
list[Document]) – Documents to embed.
Returns:
dict[str, list[Document]]– A dictionary with the following keys:documents: Documents with embeddings.
Calculates document sparse embeddings using sparse embedding models from Sentence Transformers.
It stores the sparse embeddings in the sparse_embedding metadata field of each document.
You can also embed documents' metadata.
Use this component in indexing pipelines to embed input documents
and send them to DocumentWriter to write a into a Document Store.
{/* test-ignore */}
from haystack import Document
from haystack.components.embedders import SentenceTransformersSparseDocumentEmbedder
doc = Document(content="I love pizza!")
doc_embedder = SentenceTransformersSparseDocumentEmbedder()
result = doc_embedder.run([doc])
print(result['documents'][0].sparse_embedding)
# SparseEmbedding(indices=[999, 1045, ...], values=[0.918, 0.867, ...])__init__(
*,
model: str = "prithivida/Splade_PP_en_v2",
device: ComponentDevice | None = None,
token: Secret | None = Secret.from_env_var(
["HF_API_TOKEN", "HF_TOKEN"], strict=False
),
prefix: str = "",
suffix: str = "",
batch_size: int = 32,
progress_bar: bool = True,
meta_fields_to_embed: list[str] | None = None,
embedding_separator: str = "\n",
trust_remote_code: bool = False,
local_files_only: bool = False,
model_kwargs: dict[str, Any] | None = None,
tokenizer_kwargs: dict[str, Any] | None = None,
config_kwargs: dict[str, Any] | None = None,
backend: Literal["torch", "onnx", "openvino"] = "torch",
revision: str | None = None
) -> NoneCreates a SentenceTransformersSparseDocumentEmbedder component.
Parameters:
- model (
str) – The model to use for calculating sparse embeddings. Pass a local path or ID of the model on Hugging Face. - device (
ComponentDevice | None) – The device to use for loading the model. Overrides the default device. - token (
Secret | None) – The API token to download private models from Hugging Face. - prefix (
str) – A string to add at the beginning of each document text. - suffix (
str) – A string to add at the end of each document text. - batch_size (
int) – Number of documents to embed at once. - progress_bar (
bool) – IfTrue, shows a progress bar when embedding documents. - meta_fields_to_embed (
list[str] | None) – List of metadata fields to embed along with the document text. - embedding_separator (
str) – Separator used to concatenate the metadata fields to the document text. - trust_remote_code (
bool) – IfFalse, allows only Hugging Face verified model architectures. IfTrue, allows custom models and scripts. - local_files_only (
bool) – IfTrue, does not attempt to download the model from Hugging Face Hub and only looks at local files. - model_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoModelForSequenceClassification.from_pretrainedwhen loading the model. Refer to specific model documentation for available kwargs. - tokenizer_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoTokenizer.from_pretrainedwhen loading the tokenizer. Refer to specific model documentation for available kwargs. - config_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoConfig.from_pretrainedwhen loading the model configuration. - backend (
Literal['torch', 'onnx', 'openvino']) – The backend to use for the Sentence Transformers model. Choose from "torch", "onnx", or "openvino". Refer to the Sentence Transformers documentation for more information on acceleration and quantization options. - revision (
str | None) – The specific model version to use. It can be a branch name, a tag name, or a commit id, for a stored model on Hugging Face.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> SentenceTransformersSparseDocumentEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
SentenceTransformersSparseDocumentEmbedder– Deserialized component.
warm_up() -> NoneInitializes the component.
run(documents: list[Document]) -> dict[str, list[Document]]Embed a list of documents.
Parameters:
- documents (
list[Document]) – Documents to embed.
Returns:
dict[str, list[Document]]– A dictionary with the following keys:documents: Documents with sparse embeddings under thesparse_embeddingfield.
Embeds strings using sparse embedding models from Sentence Transformers.
You can use it to embed user query and send it to a sparse embedding retriever.
Usage example: {/* test-ignore */}
from haystack.components.embedders import SentenceTransformersSparseTextEmbedder
text_to_embed = "I love pizza!"
text_embedder = SentenceTransformersSparseTextEmbedder()
print(text_embedder.run(text_to_embed))
# {'sparse_embedding': SparseEmbedding(indices=[999, 1045, ...], values=[0.918, 0.867, ...])}__init__(
*,
model: str = "prithivida/Splade_PP_en_v2",
device: ComponentDevice | None = None,
token: Secret | None = Secret.from_env_var(
["HF_API_TOKEN", "HF_TOKEN"], strict=False
),
prefix: str = "",
suffix: str = "",
trust_remote_code: bool = False,
local_files_only: bool = False,
model_kwargs: dict[str, Any] | None = None,
tokenizer_kwargs: dict[str, Any] | None = None,
config_kwargs: dict[str, Any] | None = None,
backend: Literal["torch", "onnx", "openvino"] = "torch",
revision: str | None = None
) -> NoneCreate a SentenceTransformersSparseTextEmbedder component.
Parameters:
- model (
str) – The model to use for calculating sparse embeddings. Specify the path to a local model or the ID of the model on Hugging Face. - device (
ComponentDevice | None) – Overrides the default device used to load the model. - token (
Secret | None) – An API token to use private models from Hugging Face. - prefix (
str) – A string to add at the beginning of each text to be embedded. - suffix (
str) – A string to add at the end of each text to embed. - trust_remote_code (
bool) – IfFalse, permits only Hugging Face verified model architectures. IfTrue, permits custom models and scripts. - local_files_only (
bool) – IfTrue, does not attempt to download the model from Hugging Face Hub and only looks at local files. - model_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoModelForSequenceClassification.from_pretrainedwhen loading the model. Refer to specific model documentation for available kwargs. - tokenizer_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoTokenizer.from_pretrainedwhen loading the tokenizer. Refer to specific model documentation for available kwargs. - config_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoConfig.from_pretrainedwhen loading the model configuration. - backend (
Literal['torch', 'onnx', 'openvino']) – The backend to use for the Sentence Transformers model. Choose from "torch", "onnx", or "openvino". Refer to the Sentence Transformers documentation for more information on acceleration and quantization options. - revision (
str | None) – The specific model version to use. It can be a branch name, a tag name, or a commit id, for a stored model on Hugging Face.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> SentenceTransformersSparseTextEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
SentenceTransformersSparseTextEmbedder– Deserialized component.
warm_up() -> NoneInitializes the component.
run(text: str) -> dict[str, Any]Embed a single string.
Parameters:
- text (
str) – Text to embed.
Returns:
dict[str, Any]– A dictionary with the following keys:sparse_embedding: The sparse embedding of the input text.
Embeds strings using Sentence Transformers models.
You can use it to embed user query and send it to an embedding retriever.
Usage example: {/* test-ignore */}
from haystack.components.embedders import SentenceTransformersTextEmbedder
text_to_embed = "I love pizza!"
text_embedder = SentenceTransformersTextEmbedder()
print(text_embedder.run(text_to_embed))
# {'embedding': [-0.07804739475250244, 0.1498992145061493,, ...]}__init__(
model: str = "sentence-transformers/all-mpnet-base-v2",
device: ComponentDevice | None = None,
token: Secret | None = Secret.from_env_var(
["HF_API_TOKEN", "HF_TOKEN"], strict=False
),
prefix: str = "",
suffix: str = "",
batch_size: int = 32,
progress_bar: bool = True,
normalize_embeddings: bool = False,
trust_remote_code: bool = False,
local_files_only: bool = False,
truncate_dim: int | None = None,
model_kwargs: dict[str, Any] | None = None,
tokenizer_kwargs: dict[str, Any] | None = None,
config_kwargs: dict[str, Any] | None = None,
precision: Literal[
"float32", "int8", "uint8", "binary", "ubinary"
] = "float32",
encode_kwargs: dict[str, Any] | None = None,
backend: Literal["torch", "onnx", "openvino"] = "torch",
revision: str | None = None,
) -> NoneCreate a SentenceTransformersTextEmbedder component.
Parameters:
- model (
str) – The model to use for calculating embeddings. Specify the path to a local model or the ID of the model on Hugging Face. - device (
ComponentDevice | None) – Overrides the default device used to load the model. - token (
Secret | None) – An API token to use private models from Hugging Face. - prefix (
str) – A string to add at the beginning of each text to be embedded. You can use it to prepend the text with an instruction, as required by some embedding models, such as E5 and bge. - suffix (
str) – A string to add at the end of each text to embed. - batch_size (
int) – Number of texts to embed at once. - progress_bar (
bool) – IfTrue, shows a progress bar for calculating embeddings. IfFalse, disables the progress bar. - normalize_embeddings (
bool) – IfTrue, the embeddings are normalized using L2 normalization, so that the embeddings have a norm of 1. - trust_remote_code (
bool) – IfFalse, permits only Hugging Face verified model architectures. IfTrue, permits custom models and scripts. - local_files_only (
bool) – IfTrue, does not attempt to download the model from Hugging Face Hub and only looks at local files. - truncate_dim (
int | None) – The dimension to truncate sentence embeddings to.Nonedoes no truncation. If the model has not been trained with Matryoshka Representation Learning, truncation of embeddings can significantly affect performance. - model_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoModelForSequenceClassification.from_pretrainedwhen loading the model. Refer to specific model documentation for available kwargs. - tokenizer_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoTokenizer.from_pretrainedwhen loading the tokenizer. Refer to specific model documentation for available kwargs. - config_kwargs (
dict[str, Any] | None) – Additional keyword arguments forAutoConfig.from_pretrainedwhen loading the model configuration. - precision (
Literal['float32', 'int8', 'uint8', 'binary', 'ubinary']) – The precision to use for the embeddings. All non-float32 precisions are quantized embeddings. Quantized embeddings are smaller in size and faster to compute, but may have a lower accuracy. They are useful for reducing the size of the embeddings of a corpus for semantic search, among other tasks. - encode_kwargs (
dict[str, Any] | None) – Additional keyword arguments forSentenceTransformer.encodewhen embedding texts. This parameter is provided for fine customization. Be careful not to clash with already set parameters and avoid passing parameters that change the output type. - backend (
Literal['torch', 'onnx', 'openvino']) – The backend to use for the Sentence Transformers model. Choose from "torch", "onnx", or "openvino". Refer to the Sentence Transformers documentation for more information on acceleration and quantization options. - revision (
str | None) – The specific model version to use. It can be a branch name, a tag name, or a commit id, for a stored model on Hugging Face.
to_dict() -> dict[str, Any]Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict(data: dict[str, Any]) -> SentenceTransformersTextEmbedderDeserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
SentenceTransformersTextEmbedder– Deserialized component.
warm_up() -> NoneInitializes the component.
run(text: str) -> dict[str, Any]Embed a single string.
Parameters:
- text (
str) – Text to embed.
Returns:
dict[str, Any]– A dictionary with the following keys:embedding: The embedding of the input text.