SentenceTransformersTextEmbedder
Embed strings using Sentence Transformers models. Use this component in query pipelines to transform user queries into vectors for embedding-based retrieval.
Key Features
- Works with any model available on Hugging Face that is compatible with Sentence Transformers.
- Outputs a float vector embedding suitable for use with embedding retrievers.
- Supports multiple embedding precisions including float32, int8, uint8, binary, and ubinary.
- Supports model quantization and custom backends (PyTorch, ONNX, OpenVINO).
- The embedding model must match the one used by
SentenceTransformersDocumentEmbedderin the indexing pipeline.
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.
Configuration
- Drag the
SentenceTransformersTextEmbeddercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Set the
modelto the Sentence Transformers model you want to use (for example,sentence-transformers/all-mpnet-base-v2). - Optionally set a Hugging Face token if using private or gated models.
- Set the
- Go to the Advanced tab to configure
prefix,suffix,normalize_embeddings,truncate_dim,precision, andbackend.
Connections
SentenceTransformersTextEmbedder receives the user query as a text string, typically from the Input component. It outputs a float vector through its embedding output, which you connect to an embedding retriever such as OpenSearchEmbeddingRetriever.
Source Code
To check this component's source code, open sentence_transformers_text_embedder.py in the Haystack Core Integrations repository.
Usage Examples
Basic Configuration
SentenceTransformersTextEmbedder:
type: haystack_integrations.components.embedders.sentence_transformers.SentenceTransformersTextEmbedder
init_parameters:
model: sentence-transformers/all-mpnet-base-v2
normalize_embeddings: false
progress_bar: true
Using the Component in a Pipeline
# haystack-pipeline
components:
SentenceTransformersTextEmbedder:
type: haystack_integrations.components.embedders.sentence_transformers.SentenceTransformersTextEmbedder
init_parameters:
model: sentence-transformers/all-mpnet-base-v2
normalize_embeddings: false
token:
type: env_var
env_vars:
- HF_API_TOKEN
- HF_TOKEN
strict: false
OpenSearchEmbeddingRetriever:
type: haystack_integrations.components.retrievers.opensearch.embedding_retriever.OpenSearchEmbeddingRetriever
init_parameters:
top_k: 10
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: my-index
embedding_dim: 768
connections:
- sender: SentenceTransformersTextEmbedder.embedding
receiver: OpenSearchEmbeddingRetriever.query_embedding
max_runs_per_component: 100
metadata: {}
inputs:
query:
- SentenceTransformersTextEmbedder.text
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
text | str | The text to embed. |
Outputs
| Parameter | Type | Description |
|---|---|---|
embedding | List[float] | The embedding of the text. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
model | str | sentence-transformers/all-mpnet-base-v2 | The name of the Sentence Transformers model from Hugging Face. |
device | Optional[ComponentDevice] | None | The device to run the model on. If not set, automatically detected. |
token | Optional[Secret] | Secret.from_env_var(["HF_API_TOKEN", "HF_TOKEN"]) | A Hugging Face token for accessing private or gated models. |
prefix | str | "" | A string to add at the beginning of the text before embedding. |
suffix | str | "" | A string to add at the end of the text before embedding. |
batch_size | int | 32 | The number of texts to process in each batch. |
progress_bar | bool | True | Whether to show a progress bar during embedding. |
normalize_embeddings | bool | False | Whether to normalize embedding vectors to unit length. |
trust_remote_code | bool | False | Whether to trust remote code when loading models with custom code. |
local_files_only | bool | False | Whether to use only local files, without downloading from Hugging Face. |
truncate_dim | Optional[int] | None | The dimension to truncate the embedding to. |
model_kwargs | Optional[Dict[str, Any]] | None | Additional keyword arguments passed to the model constructor. |
tokenizer_kwargs | Optional[Dict[str, Any]] | None | Additional keyword arguments for the tokenizer. |
config_kwargs | Optional[Dict[str, Any]] | None | Additional keyword arguments for the model configuration. |
precision | Literal["float32", "int8", "uint8", "binary", "ubinary"] | "float32" | The precision of the output embeddings. |
encode_kwargs | Optional[Dict[str, Any]] | None | Additional keyword arguments for the encode() method. |
backend | Literal["torch", "onnx", "openvino"] | "torch" | The backend to use for inference. |
revision | Optional[str] | None | The model revision to use. |
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.
| Parameter | Type | Default | Description |
|---|---|---|---|
text | str | The text to embed. |
Related Information
Was this page helpful?