Embeddings
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 theembed 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 theembedMany 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 thecosineSimilarity 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. Bothembed and embedMany provide token usage information in the usage property of the result object:
Settings
Provider Options
Embedding model settings can be configured usingproviderOptions for provider-specific parameters:
Parallel Requests
TheembedMany function supports parallel processing with configurable maxParallelCalls to optimize performance:
Retries
Bothembed 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
Bothembed 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
Bothembed 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
Bothembed and embedMany return response information that includes the raw provider response:
Embedding Middleware
You can enhance embedding models, e.g. to set default values, usingwrapEmbeddingModel and EmbeddingModelMiddleware.
Here is an example that uses the built-in defaultEmbeddingSettingsMiddleware:
Embedding Providers & Models
Several providers offer embedding models:| Provider | Model | Embedding Dimensions |
|---|---|---|
| OpenAI | text-embedding-3-large | 3072 |
| OpenAI | text-embedding-3-small | 1536 |
| OpenAI | text-embedding-ada-002 | 1536 |
| Google Generative AI | gemini-embedding-001 | 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 |