Embeddings class provides a unified interface for generating vector embeddings across multiple providers. You write your code once, and switch between OpenAI, Cohere, Gemini, Jina, Mistral, or any other supported provider by changing a single preset name.
Understanding Embeddings
Before diving into the API, it helps to understand the core concepts:- Vectors — Embeddings represent text as arrays of floating-point numbers in a high-dimensional space (typically 256 to 3072 dimensions).
- Semantic similarity — Texts with similar meaning produce vectors that are closer together, measurable through cosine similarity, Euclidean distance, or dot product.
- Provider models — Different providers offer models with varying dimension counts, language support, and performance characteristics.
- Semantic search — Find documents similar to a query based on meaning, not just keywords.
- Clustering — Group related documents together automatically.
- Classification — Assign categories to text based on content.
- Recommendations — Suggest related items based on vector proximity.
- RAG (Retrieval-Augmented Generation) — Retrieve relevant context for LLM prompts.
The Embeddings Class
TheEmbeddings class is a facade that combines provider configuration, request building, and result handling into a fluent, immutable API. Every method that modifies state returns a new instance, making the class safe to reuse and compose.
Architecture Overview
The class is built from several focused components:Entry Points
You can create anEmbeddings instance in several ways, depending on how much control you need:
Request Methods
Configure what to embed before executing the request:Execution Methods
Three convenience methods execute the request and return results at different levels of detail:
For advanced use cases,
create() returns a PendingEmbeddings instance that you can inspect or execute manually.
Supported Providers
Polyglot ships with presets for the following providers:Note: Mistral and Ollama use the OpenAI-compatible driver, since their APIs follow the same format.
Custom Driver Registration
You can register your own driver for providers not bundled with Polyglot by creating a customEmbeddingsDriverRegistry:
CanHandleVectorization contract.