Embeddings
BaseRagasEmbedding
Bases: ABC
Modern abstract base class for Ragas embedding implementations.
This class provides a consistent interface for embedding text using various providers. Implementations should provide both sync and async methods for embedding single texts, with batch methods automatically provided.
embed_text
abstractmethod
Embed a single text.
Args: text: The text to embed **kwargs: Additional arguments for the embedding call
Returns: List of floats representing the embedding
Source code in src/ragas/embeddings/base.py
aembed_text
abstractmethod
async
Asynchronously embed a single text.
Args: text: The text to embed **kwargs: Additional arguments for the embedding call
Returns: List of floats representing the embedding
Source code in src/ragas/embeddings/base.py
embed_texts
Embed multiple texts.
Default implementation processes texts individually. Override for batch optimization.
Args: texts: List of texts to embed **kwargs: Additional arguments for the embedding calls
Returns: List of embeddings, one for each input text
Source code in src/ragas/embeddings/base.py
aembed_texts
async
Asynchronously embed multiple texts.
Default implementation processes texts concurrently. Override for batch optimization.
Args: texts: List of texts to embed **kwargs: Additional arguments for the embedding calls
Returns: List of embeddings, one for each input text
Source code in src/ragas/embeddings/base.py
BaseRagasEmbeddings
BaseRagasEmbeddings(cache: Optional[CacheInterface] = None)
Bases: Embeddings, ABC
Abstract base class for Ragas embeddings.
This class extends the Embeddings class and provides methods for embedding text and managing run configurations.
Attributes: run_config (RunConfig): Configuration for running the embedding operations.
Source code in src/ragas/embeddings/base.py
embed_text
async
embed_texts
async
Embed multiple texts.
Source code in src/ragas/embeddings/base.py
set_run_config
set_run_config(run_config: RunConfig)
HuggingfaceEmbeddings
HuggingfaceEmbeddings(cache: Optional[CacheInterface] = None)
Bases: BaseRagasEmbeddings
Hugging Face embeddings class for generating embeddings using pre-trained models.
This class provides functionality to load and use Hugging Face models for generating embeddings of text inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_name
|
str
|
Name of the pre-trained model to use, by default DEFAULT_MODEL_NAME. |
required |
cache_folder
|
str
|
Path to store downloaded models. Can also be set by SENTENCE_TRANSFORMERS_HOME environment variable. |
required |
model_kwargs
|
dict
|
Additional keyword arguments to pass to the model. |
required |
encode_kwargs
|
dict
|
Additional keyword arguments to pass to the encoding method. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
model |
Union[SentenceTransformer, CrossEncoder]
|
The loaded Hugging Face model. |
is_cross_encoder |
bool
|
Flag indicating whether the model is a cross-encoder. |
Methods:
| Name | Description |
|---|---|
embed_query |
Embed a single query text. |
embed_documents |
Embed multiple documents. |
predict |
Make predictions using a cross-encoder model. |
Notes
This class requires the sentence_transformers and transformers packages
to be installed.
Examples:
>>> embeddings = HuggingfaceEmbeddings(model_name="bert-base-uncased")
>>> query_embedding = embeddings.embed_query("What is the capital of France?")
>>> doc_embeddings = embeddings.embed_documents(["Paris is the capital of France.", "London is the capital of the UK."])
Source code in src/ragas/embeddings/base.py
embed_query
embed_documents
Embed multiple documents.
Source code in src/ragas/embeddings/base.py
predict
Make predictions using a cross-encoder model.
Source code in src/ragas/embeddings/base.py
GoogleEmbeddings
GoogleEmbeddings(client: Any, model: str = 'text-embedding-004', use_vertex: bool = False, project_id: Optional[str] = None, location: Optional[str] = 'us-central1', **kwargs: Any)
Bases: BaseRagasEmbedding
Google embeddings using Vertex AI or Google AI (Gemini).
Supports both Vertex AI and Google AI (Gemini) embedding models. For Vertex AI, requires google-cloud-aiplatform package. For Google AI, requires google-generativeai package.
Source code in src/ragas/embeddings/google_provider.py
embed_text
Embed a single text using Google's embedding service.
Source code in src/ragas/embeddings/google_provider.py
aembed_text
async
Asynchronously embed a single text using Google's embedding service.
Google's SDK doesn't provide native async support, so we use ThreadPoolExecutor.
Source code in src/ragas/embeddings/google_provider.py
embed_texts
Embed multiple texts using Google's embedding service.
Source code in src/ragas/embeddings/google_provider.py
aembed_texts
async
Asynchronously embed multiple texts using Google's embedding service.
Source code in src/ragas/embeddings/google_provider.py
HaystackEmbeddingsWrapper
HaystackEmbeddingsWrapper(embedder: Any, run_config: Optional[RunConfig] = None, cache: Optional[CacheInterface] = None)
Bases: BaseRagasEmbeddings
A wrapper for using Haystack embedders within the Ragas framework.
This class allows you to use both synchronous and asynchronous methods
(embed_query/embed_documents and aembed_query/aembed_documents)
for generating embeddings through a Haystack embedder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embedder
|
AzureOpenAITextEmbedder | HuggingFaceAPITextEmbedder | OpenAITextEmbedder | SentenceTransformersTextEmbedder
|
An instance of a supported Haystack embedder class. |
required |
run_config
|
RunConfig
|
A configuration object to manage embedding execution settings, by default None. |
None
|
cache
|
CacheInterface
|
A cache instance for storing and retrieving embedding results, by default None. |
None
|
Source code in src/ragas/embeddings/haystack_wrapper.py
HuggingFaceEmbeddings
HuggingFaceEmbeddings(model: str, use_api: bool = False, api_key: Optional[str] = None, device: Optional[str] = None, normalize_embeddings: bool = True, batch_size: int = 32, **model_kwargs: Any)
Bases: BaseRagasEmbedding
HuggingFace embeddings supporting both local and API-based models.
Supports sentence-transformers for local models and HuggingFace API for hosted models. Provides efficient batch processing and caching.
Source code in src/ragas/embeddings/huggingface_provider.py
embed_text
Embed a single text using HuggingFace.
Source code in src/ragas/embeddings/huggingface_provider.py
aembed_text
async
Asynchronously embed a single text using HuggingFace.
Source code in src/ragas/embeddings/huggingface_provider.py
embed_texts
Embed multiple texts using HuggingFace with batching.
Source code in src/ragas/embeddings/huggingface_provider.py
aembed_texts
async
Asynchronously embed multiple texts using HuggingFace.
Source code in src/ragas/embeddings/huggingface_provider.py
LiteLLMEmbeddings
LiteLLMEmbeddings(model: str, api_key: Optional[str] = None, api_base: Optional[str] = None, api_version: Optional[str] = None, timeout: int = 600, max_retries: int = 3, batch_size: Optional[int] = None, **litellm_params: Any)
Bases: BaseRagasEmbedding
Universal embedding interface using LiteLLM.
Supports 100+ models across OpenAI, Azure, Google, Cohere, Anthropic, and more. Provides intelligent batching and provider-specific optimizations.
Source code in src/ragas/embeddings/litellm_provider.py
embed_text
Embed a single text using LiteLLM.
Source code in src/ragas/embeddings/litellm_provider.py
aembed_text
async
Asynchronously embed a single text using LiteLLM.
Source code in src/ragas/embeddings/litellm_provider.py
embed_texts
Embed multiple texts using LiteLLM with intelligent batching.
Source code in src/ragas/embeddings/litellm_provider.py
aembed_texts
async
Asynchronously embed multiple texts using LiteLLM with intelligent batching.
Source code in src/ragas/embeddings/litellm_provider.py
OpenAIEmbeddings
Bases: BaseRagasEmbedding
OpenAI embeddings implementation with batch optimization.
Supports both sync and async OpenAI clients with automatic detection. Provides optimized batch processing for better performance.
Source code in src/ragas/embeddings/openai_provider.py
embed_text
Embed a single text using OpenAI.
For async clients, this will run the async method in the appropriate event loop.
Source code in src/ragas/embeddings/openai_provider.py
aembed_text
async
Asynchronously embed a single text using OpenAI.
Source code in src/ragas/embeddings/openai_provider.py
embed_texts
Embed multiple texts using OpenAI's batch API for optimization.
Source code in src/ragas/embeddings/openai_provider.py
aembed_texts
async
Asynchronously embed multiple texts using OpenAI's batch API.
Source code in src/ragas/embeddings/openai_provider.py
batch_texts
Batch a list of texts into smaller chunks.
Args: texts: List of texts to batch batch_size: Size of each batch
Returns: List of batches, where each batch is a list of texts
Source code in src/ragas/embeddings/utils.py
get_optimal_batch_size
Get optimal batch size for a provider/model combination.
Args: provider: The embedding provider model: The model name
Returns: Optimal batch size for the provider/model
Source code in src/ragas/embeddings/utils.py
validate_texts
Validate and normalize text inputs.
Args: texts: Single text or list of texts
Returns: List of validated texts
Raises: ValueError: If texts are invalid
Source code in src/ragas/embeddings/utils.py
embedding_factory
Deprecated: Use embedding_factory from base module directly.