OpenAIDocumentEmbedder
Compute document embeddings using OpenAI models. Use this component in indexes to embed documents before storing them in a vector database.
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
- Computes dense vector embeddings for documents using OpenAI embedding models.
- Supports all OpenAI embedding models, including the
text-embedding-3series. - Embeds documents in configurable batch sizes for efficient processing.
- Optionally embeds metadata fields along with document content.
- Configurable error handling: raises or logs errors on failure.
Configuration
- Drag the
OpenAIDocumentEmbeddercomponent onto the canvas from the Component Library. - Click on the component to open the configuration panel.
- On the General tab:
- Make sure Haystack Platform is connected to your OpenAI account. For help, see Use OpenAI Models.
- Select the embedding model to use.
- Go to the Advanced tab to configure additional settings such as
timeout,max_retries,http_client_kwargs,prefix,suffix, and metadata embedding options.
Connections
OpenAIDocumentEmbedder receives documents to embed from PreProcessors like DocumentSplitter. It sends the embedded documents to DocumentWriter, which writes them into a document store.
Source Code
To check this component's source code, open openai_document_embedder.py in the Haystack repository.
Usage Examples
Basic Configuration
document_embedder:
type: haystack.components.embedders.openai_document_embedder.OpenAIDocumentEmbedder
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: text-embedding-ada-002
This is an index that uses OpenAIDocumentEmbedder to embed documents before storing them:
components:
document_embedder:
type: haystack.components.embedders.openai_document_embedder.OpenAIDocumentEmbedder
init_parameters:
api_key:
type: env_var
env_vars:
- OPENAI_API_KEY
strict: false
model: text-embedding-ada-002
document_writer:
type: haystack.components.writers.document_writer.DocumentWriter
init_parameters:
policy: NONE
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
index: my_index
embedding_dim: 1536
connections:
- sender: document_embedder.documents
receiver: document_writer.documents
inputs:
documents:
- document_embedder.documents
max_runs_per_component: 100
metadata: {}
Parameters
Inputs
| Parameter | Type | Description |
|---|---|---|
documents | List[Document] | A list of documents to embed. |
Outputs
| Parameter | Type | Description |
|---|---|---|
documents | List[Document] | A list of documents with embeddings. |
meta | Dict[str, Any] | Information about the usage of the model, including model name and token usage. |
Init Parameters
These are the parameters you can configure in Pipeline Builder:
| Parameter | Type | Default | Description |
|---|---|---|---|
| api_key | Secret | Secret.from_env_var('OPENAI_API_KEY') | The OpenAI API key. You can set it with an environment variable OPENAI_API_KEY, or pass with this parameter during initialization. |
| model | str | text-embedding-ada-002 | The name of the model to use for calculating embeddings. The default model is text-embedding-ada-002. |
| dimensions | Optional[int] | None | The number of dimensions of the resulting embeddings. Only text-embedding-3 and later models support this parameter. |
| api_base_url | Optional[str] | None | Overrides the default base URL for all HTTP requests. |
| organization | Optional[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 | 32 | Number of documents to embed at once. |
| progress_bar | bool | True | If True, shows a progress bar when running. |
| meta_fields_to_embed | Optional[List[str]] | None | List of metadata fields to embed along with the document text. |
| embedding_separator | str | \n | Separator used to concatenate the metadata fields to the document text. |
| timeout | Optional[float] | None | Timeout for OpenAI client calls. If not set, it defaults to either the OPENAI_TIMEOUT environment variable, or 30 seconds. |
| max_retries | Optional[int] | None | Maximum number of retries to contact OpenAI after an internal error. If not set, it defaults to either the OPENAI_MAX_RETRIES environment variable, or 5 retries. |
| http_client_kwargs | Optional[Dict[str, Any]] | None | A dictionary of keyword arguments to configure a custom httpx.Clientor httpx.AsyncClient. For more information, see the HTTPX documentation. |
| raise_on_failure | bool | False | Whether to raise an exception if the embedding request fails. If False, the component will log the error and continue processing the remaining documents. If True, it will raise an exception on failure. |
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] | A list of documents to embed. |
Related Information
Was this page helpful?