Overview
Theembedding() 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-largeUnion[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
- Batch processing: Send multiple texts in one request when possible
- Choose appropriate dimensions: Smaller dimensions for faster search, larger for accuracy
- Normalize vectors: For cosine similarity, normalize embeddings
- Cache embeddings: Store embeddings to avoid repeated API calls
- Handle rate limits: Use async processing with appropriate delays
- Monitor token usage: Track costs, especially with large batches