Overview
OpenAIDenseEmbedding provides text-to-vector embedding capabilities using OpenAI’s embedding models. It supports various models with configurable dimensions and includes automatic result caching for improved performance.
Location: python/zvec/extension/openai_embedding_function.py:24
Installation
Class Definition
Constructor
Parameters
OpenAI embedding model identifier. Common options:
text-embedding-3-small: 1536 dims, cost-efficient, good performancetext-embedding-3-large: 3072 dims, highest qualitytext-embedding-ada-002: 1536 dims, legacy model
Desired output embedding dimension. If
None, uses model’s default dimension. For text-embedding-3 models, you can specify custom dimensions (e.g., 256, 512, 1024, 1536).OpenAI API authentication key. If
None, reads from OPENAI_API_KEY environment variable. Obtain your key from: https://platform.openai.com/api-keysCustom API base URL for OpenAI-compatible services (e.g., Azure OpenAI). Defaults to official OpenAI endpoint.
Additional parameters for API calls:
encoding_format(str): Format of embeddings, “float” or “base64”user(str): User identifier for tracking
Properties
dimension
extra_params
Methods
embed()
input(str): Input text string to embed. Must be non-empty. Maximum length is 8191 tokens for most models.
DenseVectorType: A list of floats representing the embedding vector. Length equalsself.dimension.
TypeError: If input is not a stringValueError: If input is empty/whitespace-only or API returns errorRuntimeError: If network connectivity issues or OpenAI service errors occur
The
embed() method is cached with LRU cache (maxsize=10). Identical inputs return cached results.Usage Examples
Basic Usage
Custom Model and Dimension
Azure OpenAI
Batch Processing with Caching
Callable Interface
Semantic Similarity
Error Handling
Model Comparison
text-embedding-3-small
Dimensions: 1536
Cost: Low
Performance: Good
Cost: Low
Performance: Good
text-embedding-3-large
Dimensions: 3072
Cost: Medium
Performance: Highest
Cost: Medium
Performance: Highest
text-embedding-ada-002
Dimensions: 1536
Cost: Low
Performance: Legacy
Cost: Low
Performance: Legacy
Best Practices
Caching: Results are cached automatically. For better cache hits, consider:
- Normalizing text (lowercasing, whitespace trimming)
- Pre-processing inputs consistently
- Using the same embedding instance for similar queries
Notes
- Requires Python 3.10, 3.11, or 3.12
- Requires the
openaipackage:pip install openai - Network connectivity to OpenAI API endpoints is required
- API usage incurs costs based on your OpenAI subscription plan
- Rate limits apply based on your OpenAI account tier
- Embedding results are cached (LRU cache, maxsize=10)
See Also
- DenseEmbeddingFunction - Base class documentation
- QwenDenseEmbedding - Alternative using Qwen API
- DefaultLocalDenseEmbedding - Local model without API calls
- Embedding Functions Overview