Overview
Theembed() function generates vector embeddings for text input using embedding models. Embeddings are useful for semantic search, clustering, recommendations, and other AI tasks that require numerical representations of text.
Function Signature
Parameters
EmbeddingModel
required
The embedding model instance to use for generating embeddings.
string | string[]
required
Text input to embed. Can be a single string or an array of strings. Must not be empty.
number
Optional dimension size for the output embeddings. Not all models support this parameter.
EmbedProviderOptions
Provider-specific options, namespaced by provider name (e.g.
{ openai: { encodingFormat: 'float' } }).Return Value
Returns aPromise<EmbedResult> with the following properties:
number[][]
Array of embedding vectors. Each vector is an array of numbers representing the embedding dimensions.
- For single string input: Returns array with one embedding
- For array input: Returns array with one embedding per input string
EmbeddingUsage | undefined
Optional token usage metadata. Some providers/models do not expose token usage for embedding calls.
Examples
Single String Embedding
Batch Embedding
With Custom Dimensions
Checking Token Usage
Semantic Search Use Case
Clustering Documents
Error Handling
ThrowsValidationError if:
- Input is an empty string
- Input is an empty array
ProviderErrorif the provider returns an error during embedding
Provider Support
Different providers have different embedding models and capabilities:Performance Tips
Batch multiple inputs in a single call instead of making separate calls for each input. This is more efficient and faster.
Common Use Cases
- Semantic Search: Find documents similar to a query
- Clustering: Group similar documents together
- Recommendations: Recommend items based on similarity
- Classification: Use embeddings as features for ML models
- Anomaly Detection: Identify outliers based on embedding distance
- Deduplication: Find and remove duplicate or near-duplicate content