Understanding Embeddings
Before diving into code, it’s helpful to understand what embeddings are and how they work:- Embeddings represent words, phrases, or documents as vectors of floating-point numbers in a high-dimensional space
- Similar items (semantically related) have vectors that are closer together in this space
- The “distance” between vectors can be measured using metrics like cosine similarity or Euclidean distance
- Modern embedding models are trained on massive corpora of text to capture nuanced relationships
- Semantic search: Finding documents similar to a query based on meaning, not just keywords
- Clustering: Grouping similar documents together
- Classification: Assigning categories to documents based on their content
- Recommendations: Suggesting related items
- Information retrieval: Finding relevant information in large datasets
Embeddings class
The Embeddings class is a facade that provides access to embeddings APIs across multiple providers.
It combines functionality through traits for provider configuration, request building, and result handling.
Architecture Overview
TheEmbeddings class combines functionality through traits:
- HandlesInitMethods: Provider configuration and setup
- HandlesFluentMethods: Request parameter configuration
- HandlesInvocation: Request execution and PendingEmbeddings creation
- HandlesShortcuts: Convenient methods for common result formats
Supported providers
Embeddings class supports the following embeddings providers:
- Azure OpenAI: Azure-hosted OpenAI embedding models
- Cohere: Cohere’s embedding models
- Gemini: Google’s Gemini embedding models
- Jina: Jina AI’s embedding models
- OpenAI: OpenAI’s embedding models (text-embedding-ada-002, text-embedding-3-small, text-embedding-3-large)