FastembedDocumentEmbedder
Compute document embeddings using Fastembed embedding models.
Key Features
- Uses Fastembed, a lightweight, fast Python library for embedding generation.
- Stores the computed embedding in the
embeddingfield of each document. - Supports most state-of-the-art embedding models.
- Configurable batch size and parallel processing for large datasets.
- Supports embedding metadata fields alongside document content.
Configuration
- Drag the
FastembedDocumentEmbeddercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Select the embedding model. You can find supported models in the FastEmbed documentation.
- Go to the Advanced tab to configure additional settings such as
batch_size,parallel,meta_fields_to_embed, andembedding_separator.
Connections
FastembedDocumentEmbedder receives a list of documents from converters or DocumentSplitter in an indexing pipeline. It outputs the same documents with their embedding field populated. Connect its output to DocumentWriter to store embedded documents in a document store.
Source Code
To check this component's source code, open fastembed_document_embedder.py in the Haystack Core Integrations repository.
Connections
FastembedDocumentEmbedder accepts a list of documents as input. In an indexing pipeline, connect it to converters such as TextFileToDocument or preprocessors such as DocumentSplitter.
It outputs a list of documents with the embedding field populated. Connect its documents output to DocumentWriter to store the embedded documents in a document store.
Usage Examples
Basic Configuration
FastembedDocumentEmbedder:
type: haystack_integrations.components.embedders.fastembed.fastembed_document_embedder.FastembedDocumentEmbedder
init_parameters:
model: BAAI/bge-small-en-v1.5
prefix: ''
suffix: ''
batch_size: 256
progress_bar: true
local_files_only: false
embedding_separator: "\n"
This index uses FastembedDocumentEmbedder to embed documents before storing them:
components:
TextFileToDocument:
type: haystack.components.converters.txt.TextFileToDocument
init_parameters:
encoding: utf-8
store_full_path: false
DocumentSplitter:
type: haystack.components.preprocessors.document_splitter.DocumentSplitter
init_parameters:
split_by: sentence
split_length: 5
split_overlap: 1
FastembedDocumentEmbedder:
type: haystack_integrations.components.embedders.fastembed.fastembed_document_embedder.FastembedDocumentEmbedder
init_parameters:
model: BAAI/bge-small-en-v1.5
cache_dir:
threads:
prefix: ""
suffix: ""
batch_size: 256
progress_bar: true
parallel:
local_files_only: false
meta_fields_to_embed:
embedding_separator: "\n"
DocumentWriter:
type: haystack.components.writers.document_writer.DocumentWriter
init_parameters:
document_store:
type: haystack_integrations.document_stores.opensearch.document_store.OpenSearchDocumentStore
init_parameters:
hosts:
index: 'default'
max_chunk_bytes: 104857600
embedding_dim: 384
return_embedding: false
method:
mappings:
settings:
create_index: true
http_auth:
use_ssl:
verify_certs:
timeout:
policy: OVERWRITE
connections:
- sender: TextFileToDocument.documents
receiver: DocumentSplitter.documents
- sender: DocumentSplitter.documents
receiver: FastembedDocumentEmbedder.documents
- sender: FastembedDocumentEmbedder.documents
receiver: DocumentWriter.documents
inputs:
files:
- TextFileToDocument.sources
max_runs_per_component: 100
metadata: {}
Parameters
Inputs
| Parameter | Type | Default | Description |
|---|---|---|---|
documents | List[Document] | List of Documents to embed. |
Outputs
| Parameter | Type | Default | Description |
|---|---|---|---|
documents | List[Document] | List of Documents with each Document's embedding field set to the computed embeddings. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
model | str | BAAI/bge-small-en-v1.5 | Local path or name of the model in Hugging Face's model hub, such as BAAI/bge-small-en-v1.5. |
cache_dir | Optional[str] | None | The path to the cache directory. Can be set using the FASTEMBED_CACHE_PATH env variable. Defaults to fastembed_cache in the system's temp directory. |
threads | Optional[int] | None | The number of threads single onnxruntime session can use. |
prefix | str | "" | A string to add to the beginning of each text. |
suffix | str | "" | A string to add to the end of each text. |
batch_size | int | 256 | Number of strings to encode at once. |
progress_bar | bool | True | If True, displays progress bar during embedding. |
parallel | Optional[int] | None | If > 1, data-parallel encoding is used, recommended for offline encoding of large datasets. If 0, use all available cores. If None, don't use data-parallel processing, use default onnxruntime threading instead. |
local_files_only | bool | False | If True, only use the model files in the cache_dir. |
meta_fields_to_embed | Optional[List[str]] | None | List of meta fields that should be embedded along with the Document content. |
embedding_separator | str | \n | Separator used to concatenate the meta fields to the Document content. |
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 |
|---|---|---|---|
documents | List[Document] | List of Documents to embed. |
Was this page helpful?