LiteLLMEmbeddings class, which provides unified access to 100+ embedding providers through the LiteLLM library. This includes OpenAI, VoyageAI, Cohere, AWS Bedrock, Azure, and many more.
Why LiteLLM?
LiteLLM provides several advantages over using provider-specific implementations:- Unified API: Single interface for all providers
- Robust Error Handling: Built-in retry logic and rate limiting
- No Provider Bugs: Avoids provider-specific issues like VoyageAI’s circular import bug
- Easy Switching: Change providers without code changes
- Future-Proof: LiteLLM team maintains provider integrations
Installation
Embeddings require thelitellm and tiktoken libraries. The tokenizers library is also recommended for better tokenizer support across providers.
Supported Providers
LiteLLM supports 100+ providers. Here are some popular ones:
For a complete list, see the LiteLLM documentation.
Usage
Basic Usage
Using VoyageAI
Using Cohere
Using AWS Bedrock
Configuration Options
Using with AutoEmbeddings
Using with Pipeline
API Key Configuration
LiteLLM automatically looks for API keys in environment variables. The variable name depends on the provider:- OpenAI:
OPENAI_API_KEY - VoyageAI:
VOYAGE_API_KEY - Cohere:
COHERE_API_KEYorCO_API_KEY - AWS Bedrock:
AWS_ACCESS_KEY_IDandAWS_SECRET_ACCESS_KEY - Azure:
AZURE_API_KEYandAZURE_API_BASE
Error Handling
LiteLLM provides robust error handling with automatic retries:Tokenizer Support
LiteLLMEmbeddings automatically selects the appropriate tokenizer based on the provider:- OpenAI models: Uses
tiktoken - VoyageAI models: Attempts to load HuggingFace tokenizer, falls back to
tiktoken - Other models: Uses
cl100k_basefromtiktokenas fallback
Performance Tips
- Batch Processing: Use
embed_batch()for multiple texts to reduce API calls - Adjust Batch Size: Tune
batch_sizebased on your text lengths and provider limits - Connection Pooling: LiteLLM handles connection pooling automatically
- Async Support: LiteLLM supports async operations (may be added in future versions)
Comparison with Provider-Specific Classes
Common Use Cases
Migrating from VoyageAI to avoid circular import
Multi-provider experimentation
Cost optimization
Troubleshooting
Import Error
API Key Not Found
api_key parameter.
