Overview
Embedding functions in Zvec transform multimodal input (text, image, or audio) into vector representations suitable for similarity search and retrieval. Zvec provides two primary types of embeddings:- Dense Embeddings: Fixed-length real-valued vectors (e.g.,
[0.123, -0.456, 0.789, ...]) - Sparse Embeddings: Dictionary-based vectors with only non-zero dimensions (e.g.,
{10: 0.5, 245: 0.8, ...})
Base Classes
Zvec defines two Protocol classes that establish the embedding function interface:DenseEmbeddingFunction
Protocol for dense vector embedding functions that map input to fixed-length vectors.MD: The type of input data (TEXT, IMAGE, or AUDIO)
DenseVectorType: A list of floats, list of ints, or numpy array
SparseEmbeddingFunction
Protocol for sparse vector embedding functions that map input to sparse representations.MD: The type of input data (TEXT, IMAGE, or AUDIO)
SparseVectorType: Dictionary mapping dimension index to non-zero weight
Built-in Implementations
Zvec provides several ready-to-use embedding implementations:Dense Embedding Implementations
OpenAI
Dense embeddings using OpenAI’s API
Qwen
Dense embeddings using Alibaba’s Qwen API
Sentence Transformers
Local dense embeddings with HuggingFace models
Sparse Embedding Implementations
BM25
Lexical search with BM25 algorithm
Qwen Sparse
Sparse embeddings using Qwen API
SPLADE
Local sparse embeddings with SPLADE model
Custom Implementations
You can create custom embedding functions by implementing the requiredembed() method. Since these are Protocol classes, you don’t need to explicitly inherit from them.
Custom Dense Embedding Example
Custom Sparse Embedding Example
Custom Image Embedding Example
Input Types
Embedding functions support multimodal input:- TEXT (
str): Text strings - IMAGE (
str | bytes | np.ndarray): Image file path, raw bytes, or array - AUDIO (
str | bytes | np.ndarray): Audio file path, raw bytes, or array
Best Practices
Choosing Between Dense and Sparse
Choosing Between Dense and Sparse
- Dense embeddings: Better for semantic similarity and cross-modal search
- Sparse embeddings: Better for exact keyword matching and interpretability
- Hybrid approach: Combine both for optimal retrieval performance
API vs Local Models
API vs Local Models
- API-based (OpenAI, Qwen): No setup, always up-to-date, requires network and API key
- Local models (Sentence Transformers, BM25): No API costs, works offline, requires initial download
Normalization
Normalization
For dense embeddings, normalize vectors to unit length when using cosine similarity:
See Also
- Dense Embeddings - Base class documentation
- Sparse Embeddings - Base class documentation
- OpenAI Embeddings - API-based dense embeddings
- Sentence Transformers - Local embeddings