Overview of Embeddings
Embeddings are a way to represent data (text, images, audio) in a continuous vector space.
Embeddings are a key component of many LLM-based solutions and are used to represent text (or multimodal data) with numbers capturing their meaning and relationships.
Embeddings are numerical representations of text or other data that capture semantic meaning in a way that computers can process efficiently. They enable powerful applications like semantic search, document clustering, recommendation systems, and more. This chapter explores how to use Polyglot’s Embeddings API to work with vector embeddings across multiple providers.
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
Common use cases for embeddings include:
- 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
Embeddings
class offers access to embeddings APIs and convenient methods to find top K vectors
or documents most similar to provided query.
Supported providers
Embeddings
class supports following embeddings providers:
- Azure
- Cohere
- Gemini
- Jina
- Mistral
- OpenAI
Embeddings providers access details can be found and modified via
/config/embed.php
.
Working with embeddings
Polyglot also makes it easy to generate embeddings:
Selecting a provider
In this example, we use OpenAI embeddings provider to generate embeddings for a given list of documents (only one in this case).
The result is a list of embeddings vectors (one per document).
Specifying Models for Embeddings
Creating Custom Configuration
You can create a custom configuration for your embeddings provider, allowing you to specify different models, dimensions, and other parameters: