Embeddings
Learn how to embed values with the AI SDK.
Embeddings are a way to represent words, phrases, or images as vectors in a high-dimensional space. In this space, similar words are close to each other, and the distance between words can be used to measure their similarity.
Embedding a Single Value
The AI SDK provides the embed function to embed single values, which is useful for tasks such as finding similar words
or phrases or clustering text.
You can use it with embeddings models, e.g. openai.embeddingModel('text-embedding-3-large') or mistral.embeddingModel('mistral-embed').
Embedding Many Values
When loading data, e.g. when preparing a data store for retrieval-augmented generation (RAG), it is often useful to embed many values at once (batch embedding).
The AI SDK provides the embedMany function for this purpose.
Similar to embed, you can use it with embeddings models,
e.g. openai.embeddingModel('text-embedding-3-large') or mistral.embeddingModel('mistral-embed').
Embedding Similarity
After embedding values, you can calculate the similarity between them using the cosineSimilarity function.
This is useful to e.g. find similar words or phrases in a dataset.
You can also rank and filter related items based on their similarity.
Token Usage
Many providers charge based on the number of tokens used to generate embeddings.
Both embed and embedMany provide token usage information in the usage property of the result object:
Settings
Output Dimensions
Both embed and embedMany accept an optional dimensions parameter to request
an output vector size. It must be a positive integer. When omitted, the model's
existing behavior is unchanged. For embedMany, the same value is forwarded to
every batch.
Providers map dimensions to their native output dimension setting. An explicit
dimension setting in providerOptions takes precedence over dimensions.
Other provider options can be combined with the top-level setting.
Support and allowed dimension values depend on the model. The SDK forwards the
requested size to the provider API without truncating or resizing the returned
embeddings. AI Gateway forwards dimensions and providerOptions for resolution
by the selected provider.
Provider Options
Embedding model settings can be configured using providerOptions for provider-specific parameters:
Google's gemini-embedding-2 model (also available as gemini-embedding-2-preview) supports multimodal embedding content through providerOptions.google.content. Each entry corresponds to the value at the same index and can contain { text: string }, { inlineData: { mimeType: string; data: string } }, or { fileData: { fileUri: string; mimeType: string } } parts. fileUri can reference remote content such as HTTP URLs or Google Cloud Storage URIs (gs://...).
Parallel Requests
The embedMany function now supports parallel processing with configurable maxParallelCalls to optimize performance:
Retries
Both embed and embedMany accept an optional maxRetries parameter of type number
that you can use to set the maximum number of retries for the embedding process.
It defaults to 2 retries (3 attempts in total). You can set it to 0 to disable retries.
Abort Signals and Timeouts
Both embed and embedMany accept an optional abortSignal parameter of
type AbortSignal
that you can use to abort the embedding process or set a timeout.
Custom Headers
Both embed and embedMany accept an optional headers parameter of type Record<string, string>
that you can use to add custom headers to the embedding request.
Response Information
Both embed and embedMany return response information that includes the raw provider response:
Embedding Middleware
You can enhance embedding models, e.g. to set default values, using
wrapEmbeddingModel and EmbeddingModelMiddleware.
Here is an example that uses the built-in defaultEmbeddingSettingsMiddleware:
Embedding Providers & Models
Several providers offer embedding models:
| Provider | Model | Embedding Dimensions | Multimodal |
|---|---|---|---|
| OpenAI | text-embedding-3-large | 3072 | |
| OpenAI | text-embedding-3-small | 1536 | |
| OpenAI | text-embedding-ada-002 | 1536 | |
gemini-embedding-001 | 3072 | ||
gemini-embedding-2 | 3072 | ||
gemini-embedding-2-preview | 3072 | ||
| Mistral | mistral-embed | 1024 | |
| Cohere | embed-english-v3.0 | 1024 | |
| Cohere | embed-multilingual-v3.0 | 1024 | |
| Cohere | embed-english-light-v3.0 | 384 | |
| Cohere | embed-multilingual-light-v3.0 | 384 | |
| Cohere | embed-english-v2.0 | 4096 | |
| Cohere | embed-english-light-v2.0 | 1024 | |
| Cohere | embed-multilingual-v2.0 | 768 | |
| Amazon Bedrock | amazon.titan-embed-text-v1 | 1536 | |
| Amazon Bedrock | amazon.titan-embed-text-v2:0 | 1024 |