Skip to main content

Overview

The embed() 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 a Promise<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

Throws ValidationError if:
  • Input is an empty string
  • Input is an empty array
May also throw:
  • ProviderError if 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.
Use smaller dimension sizes when possible to reduce storage and computation costs. The text-embedding-3-small and text-embedding-3-large models support custom dimensions.

Common Use Cases

  1. Semantic Search: Find documents similar to a query
  2. Clustering: Group similar documents together
  3. Recommendations: Recommend items based on similarity
  4. Classification: Use embeddings as features for ML models
  5. Anomaly Detection: Identify outliers based on embedding distance
  6. Deduplication: Find and remove duplicate or near-duplicate content