Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Explodinggradients Ragas BaseRagasEmbedding Class

From Leeroopedia


Knowledge Sources
Domains Embeddings, NLP
Last Updated 2026-02-10 00:00 GMT

Overview

Abstract base class hierarchy and factory functions for embedding providers, supporting both modern and legacy (LangChain-compatible) interfaces.

Description

This module provides BaseRagasEmbedding (modern ABC for embedding providers with sync/async support), BaseRagasEmbeddings (legacy LangChain-compatible ABC), LangchainEmbeddingsWrapper (deprecated wrapper), HuggingfaceEmbeddings (sentence-transformer based), LlamaIndexEmbeddingsWrapper, and the embedding_factory function for unified creation across providers.

Usage

Use embedding_factory to create embedding instances by provider name, or extend BaseRagasEmbedding to implement custom embedding providers.

Code Reference

Source Location

Signature

class BaseRagasEmbedding(ABC):
    @abstractmethod
    def embed_text(self, text: str, **kwargs) -> List[float]:
        ...
    @abstractmethod
    async def aembed_text(self, text: str, **kwargs) -> List[float]:
        ...
    def embed_texts(self, texts: List[str], **kwargs) -> List[List[float]]:
        ...
    async def aembed_texts(self, texts: List[str], **kwargs) -> List[List[float]]:
        ...

def embedding_factory(
    provider: str = "openai",
    model: Optional[str] = None,
    run_config: Optional[RunConfig] = None,
    client: Optional[Any] = None,
    interface: str = "auto",
    cache: Optional[CacheInterface] = None,
    **kwargs,
) -> Union[BaseRagasEmbeddings, BaseRagasEmbedding]:
    ...

Import

from ragas.embeddings.base import BaseRagasEmbedding, embedding_factory
from ragas.embeddings import LangchainEmbeddingsWrapper, HuggingfaceEmbeddings

I/O Contract

Inputs

Name Type Required Description
text str Yes Single text to embed
texts List[str] Yes (for batch) Multiple texts to embed
provider str No Provider name (default "openai")
model Optional[str] No Model name override
client Optional[Any] No Pre-configured client

Outputs

Name Type Description
embed_text returns List[float] Embedding vector for single text
embed_texts returns List[List[float]] Embedding vectors for batch
embedding_factory returns BaseRagasEmbedding Configured embedding instance

Usage Examples

from ragas.embeddings.base import embedding_factory

# Create OpenAI embeddings
embeddings = embedding_factory(provider="openai", model="text-embedding-3-small")

# Embed a single text
vector = embeddings.embed_text("What is machine learning?")
print(len(vector))  # embedding dimension

# Batch embed
vectors = embeddings.embed_texts(["Hello", "World"])
print(len(vectors))  # 2

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment