Embeddings
BaseRagasEmbedding
BaseRagasEmbedding(cache: Optional[CacheInterface] = None)
Bases: CachedCopyable, 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.
Args: cache: Optional cache backend for caching embeddings. Use DiskCacheBackend() for persistent caching.
Source code in src/ragas/embeddings/base.py
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
OpenAIEmbeddings
OpenAIEmbeddings(
client: Any,
model: str = "text-embedding-3-small",
cache: Optional[CacheInterface] = None,
)
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
GoogleEmbeddings
GoogleEmbeddings(
client: Optional[Any] = None,
model: str = "gemini-embedding-001",
use_vertex: bool = False,
project_id: Optional[str] = None,
location: Optional[str] = "us-central1",
cache: Optional[CacheInterface] = None,
**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, supports both: - New SDK (google-genai): Recommended, uses genai.Client() - Old SDK (google-generativeai): Deprecated (support ends Aug 2025)
The client parameter is flexible:
- For new SDK: genai.Client(api_key="...") instance
- For old SDK: None (auto-imports), the genai module, or a GenerativeModel instance
- For Vertex (required): a google-genai genai.Client(vertexai=True, ...),
a vertexai.language_models.TextEmbeddingModel instance, or any other
object once vertexai.init() has been called (the model named by
model is then loaded once, on first use, and reused)
Note: Unlike LLM generation, embeddings work correctly with both SDKs. The known instructor safety settings issue (github.com/567-labs/instructor/issues/1658) only affects LLM generation, not embeddings.
Examples: # New SDK (google-genai) - recommended from google import genai client = genai.Client(api_key="...") embeddings = GoogleEmbeddings(client=client, model="gemini-embedding-001")
# Old SDK (google-generativeai) - deprecated
import google.generativeai as genai
genai.configure(api_key="...")
embeddings = GoogleEmbeddings(client=genai, model="gemini-embedding-2")
# Auto-import (tries new SDK first, falls back to old)
embeddings = GoogleEmbeddings(model="gemini-embedding-2")
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
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,
cache: Optional[CacheInterface] = None,
rate_limit: Optional[RateLimit] = 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.
rate_limit (a ragas.rate_limit.RateLimit) paces every embedding
request: one per call, or one per batch (#171).
Source code in src/ragas/embeddings/litellm_provider.py
embed_text
aembed_text
async
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
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,
cache: Optional[CacheInterface] = None,
**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
embedding_factory
Deprecated: Use embedding_factory from base module directly.