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 HuggingFaceEmbeddings Provider

From Leeroopedia


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

Overview

Modern HuggingFace embeddings provider supporting both local sentence-transformer models and API-based embedding services.

Description

HuggingFaceEmbeddings extends BaseRagasEmbedding to provide text embeddings using HuggingFace models. It supports local inference via sentence-transformers and remote inference via the HuggingFace Inference API. Batch processing and caching are built in.

Usage

Use this provider for local or API-based HuggingFace embeddings in evaluation pipelines.

Code Reference

Source Location

Signature

class HuggingFaceEmbeddings(BaseRagasEmbedding):
    PROVIDER_NAME = "huggingface"

    def __init__(
        self,
        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,
    ) -> None:
        ...

Import

from ragas.embeddings.huggingface_provider import HuggingFaceEmbeddings

I/O Contract

Inputs

Name Type Required Description
model str Yes HuggingFace model name or path
use_api bool No Use HuggingFace Inference API (default False)
device Optional[str] No Device for local inference (e.g., "cuda")
batch_size int No Batch size for embedding (default 32)

Outputs

Name Type Description
embed_text returns List[float] Single text embedding vector
embed_texts returns List[List[float]] Batch embedding vectors

Usage Examples

from ragas.embeddings.huggingface_provider import HuggingFaceEmbeddings

# Local model
embeddings = HuggingFaceEmbeddings(model="BAAI/bge-small-en-v1.5")
vector = embeddings.embed_text("What is AI?")

# API-based
embeddings_api = HuggingFaceEmbeddings(
    model="BAAI/bge-small-en-v1.5",
    use_api=True,
    api_key="hf_xxx",
)

Related Pages

Page Connections

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