Skip to main content
For the complete documentation index for agents and LLMs, see llms.txt.

SentenceTransformersTextEmbedder

Embed strings, such as user queries, using Sentence Transformers models. The model runs locally, so no external API calls are made during embedding. Use this component in query pipelines to embed user queries for semantic search.

Nvidia Models Recommendation

We recommend using models available through the DeepsetNvidia components instead of the Sentence Transformers models.. Add a DeepsetNvidia component to your pipeline and choose an appropriate model from the list.

Embedding Models in Query Pipelines and Indexes

The embedding model you use to embed documents in your indexing pipeline must be the same as the embedding model you use to embed the query in your query pipeline.

This means the embedders for your indexing and query pipelines must match. For example, if you use CohereDocumentEmbedder to embed your documents, you should use CohereTextEmbedder with the same model to embed your queries.

Key Features

  • Embeds text strings into dense vectors using Sentence Transformers models.
  • Supports L2 normalization for consistent embedding comparison.
  • Configurable batch size for efficient processing.
  • Works with OpenSearchEmbeddingRetriever and other embedding-based retrievers.

Configuration

  1. Drag the SentenceTransformersTextEmbedder component onto the canvas from the Component Library.
  2. Click on the component to open the configuration panel.
  3. On the General tab:
    • Set model to the Sentence Transformers model to use (for example, sentence-transformers/all-mpnet-base-v2).
    • Toggle normalize_embeddings to enable L2 normalization.
    • Set batch_size to control how many texts are embedded at once.
  4. Go to the Advanced tab to configure prefix, suffix, progress_bar, trust_remote_code, token, and device.

Embedding Models in Query Pipelines and Indexes

The embedding model you use to embed documents in your indexing pipeline must be the same as the embedding model you use to embed the query in your query pipeline.

This means the embedders for your indexing and query pipelines must match. For example, if you use CohereDocumentEmbedder to embed your documents, you should use CohereTextEmbedder with the same model to embed your queries.

Connections

SentenceTransformersTextEmbedder receives a text string, typically from the Input component. It outputs an embedding (list of floats) that you can send to an embedding-based retriever such as OpenSearchEmbeddingRetriever.

Source Code

To check this component's source code, open sentence_transformers_text_embedder.py in the Haystack repository.

Usage Examples

Basic Configuration

  query_embedder:
type: haystack.components.embedders.sentence_transformers_text_embedder.SentenceTransformersTextEmbedder
init_parameters:
model: intfloat/e5-base-v2

This is a query pipeline that uses SentenceTransformersTextEmbedder to embed a query and retrieve documents:

components:
query_embedder:
type: haystack.components.embedders.sentence_transformers_text_embedder.SentenceTransformersTextEmbedder
init_parameters:
model: intfloat/e5-base-v2

embedding_retriever:
type: haystack_integrations.components.retrievers.opensearch.embedding_retriever.OpenSearchEmbeddingRetriever
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
- ${OPENSEARCH_HOST}
http_auth:
- ${OPENSEARCH_USER}
- ${OPENSEARCH_PASSWORD}
use_ssl: true
verify_certs: false
top_k: 20

connections:
- sender: query_embedder.embedding
receiver: embedding_retriever.query_embedding

inputs:
query:
- query_embedder.text
- embedding_retriever.query
filters:
- embedding_retriever.filters

outputs:
documents: embedding_retriever.documents

max_runs_per_component: 100

metadata: {}

Parameters

Inputs

ParameterTypeDefaultDescription
textstrText to embed.

Outputs

ParameterTypeDefaultDescription
embeddingList[float]The embedding of the input text.

Init Parameters

These are the parameters you can configure in Pipeline Builder:

ParameterTypeDefaultDescription
modelstrsentence-transformers/all-mpnet-base-v2The model to use for calculating embeddings. Specify the path to a local model or the ID of the model on Hugging Face.
deviceOptional[ComponentDevice]NoneOverrides the default device used to load the model.
tokenOptional[Secret]Secret.from_env_var(['HF_API_TOKEN', 'HF_TOKEN'], strict=False)An API token to use private models from Hugging Face.
prefixstrA string to add at the beginning of each text to be embedded. You can use it to prepend an instruction, as required by some embedding models such as E5 and bge.
suffixstrA string to add at the end of each text to embed.
batch_sizeint32Number of texts to embed at once.
progress_barboolTrueIf True, shows a progress bar for calculating embeddings. If False, disables the progress bar.
normalize_embeddingsboolFalseIf True, normalizes the embeddings using L2 normalization so that the embeddings have a norm of 1.
trust_remote_codeboolFalseIf False, permits only Hugging Face verified model architectures. If True, permits custom models and scripts.
local_files_onlyboolFalseIf True, does not attempt to download the model from Hugging Face Hub and only looks at local files.
truncate_dimOptional[int]NoneThe dimension to truncate sentence embeddings to. None does no truncation. If the model has not been trained with Matryoshka Representation Learning, truncation can significantly affect performance.
model_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments for AutoModelForSequenceClassification.from_pretrained when loading the model.
tokenizer_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments for AutoTokenizer.from_pretrained when loading the tokenizer.
config_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments for AutoConfig.from_pretrained when loading the model configuration.
precisionLiteral['float32', 'int8', 'uint8', 'binary', 'ubinary']float32The precision to use for the embeddings. All non-float32 precisions are quantized embeddings. Quantized embeddings are smaller and faster to compute, but may have lower accuracy.
encode_kwargsOptional[Dict[str, Any]]NoneAdditional keyword arguments for SentenceTransformer.encode when embedding texts.
backendLiteral['torch', 'onnx', 'openvino']torchThe backend to use for the Sentence Transformers model. Refer to the Sentence Transformers documentation for more information.
revisionOptional[str]NoneThe 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.

Run Method Parameters

These are the parameters you can configure for the component's run() method. This means you can pass these parameters at query time through the API, in Playground, or when running a job. For details, see Modify Pipeline Parameters at Query Time.

ParameterTypeDefaultDescription
textstrText to embed.