Skip to main content

Overview

The embedding() function generates vector embeddings from text, supporting 100+ embedding providers through a unified interface.

Basic Usage

Function Signature

Parameters

string
required
The embedding model to use. Examples: text-embedding-ada-002, text-embedding-3-small, text-embedding-3-large
Union[str, List[str]]
required
Text or list of texts to generate embeddings for.
int
Number of dimensions for the embedding. Only supported by some models (e.g., text-embedding-3 series).
str
Format for the embeddings. Options: "float" (default) or "base64"
float
Request timeout in seconds. Default: 600 (10 minutes)
str
API key for the provider. If not provided, reads from environment variables.

Response Format

Examples

Single Text Embedding

Batch Embeddings

Different Providers

Using Different Dimensions

Semantic Search Example

Async Embeddings

Concurrent Async Embeddings

Use Cases

Document Clustering

Duplicate Detection

Question Answering System

Error Handling

Best Practices

  1. Batch processing: Send multiple texts in one request when possible
  2. Choose appropriate dimensions: Smaller dimensions for faster search, larger for accuracy
  3. Normalize vectors: For cosine similarity, normalize embeddings
  4. Cache embeddings: Store embeddings to avoid repeated API calls
  5. Handle rate limits: Use async processing with appropriate delays
  6. Monitor token usage: Track costs, especially with large batches

Performance Tips