Aurelio Docs
Client Referenceencoders

semantic_router.encoders.huggingface

This module provides the HFEndpointEncoder class to embeddings models using Huggingface's endpoint.

The HFEndpointEncoder class is a subclass of DenseEncoder and utilizes a specified Huggingface endpoint to generate embeddings for given documents. It requires the URL of the Huggingface API endpoint and an API key for authentication.

Example usage:

from semantic_router.encoders.hfendpointencoder import HFEndpointEncoder

encoder = HFEndpointEncoder(
    huggingface_url="https://api-inference.huggingface.co/models/BAAI/bge-large-en-v1.5",
    huggingface_api_key="your-hugging-face-api-key"
)
embeddings = encoder(["document1", "document2"])

Classes: HFEndpointEncoder: A class for generating embeddings using a Huggingface endpoint.

HuggingFaceEncoder Objects

class HuggingFaceEncoder(DenseEncoder)

HuggingFace encoder class for local embedding models. Models can be trained and loaded from private repositories, or from the Huggingface Hub.

Example usage:

from semantic_router.encoders import HuggingFaceEncoder

encoder = HuggingFaceEncoder(
    name="sentence-transformers/all-MiniLM-L6-v2",
    device="cuda"
)
embeddings = encoder(["document1", "document2"])

__call__

def __call__(docs: List[str],
             batch_size: int = 32,
             normalize_embeddings: bool = True,
             pooling_strategy: str = "mean") -> List[List[float]]

Encode a list of documents into embeddings using the local Hugging Face model.

Arguments:

  • docs (List[str]): A list of documents to encode.
  • batch_size: The batch size for encoding.

HFEndpointEncoder Objects

class HFEndpointEncoder(DenseEncoder)

HFEndpointEncoder class to embeddings models using Huggingface's inference endpoints.

The HFEndpointEncoder class is a subclass of DenseEncoder and utilizes a specified Huggingface endpoint to generate embeddings for given documents. It requires the URL of the Huggingface API endpoint and an API key for authentication.

Example usage:

from semantic_router.encoders import HFEndpointEncoder

encoder = HFEndpointEncoder(
    huggingface_url="https://api-inference.huggingface.co/models/BAAI/bge-large-en-v1.5",
    huggingface_api_key="your-hugging-face-api-key"
)
embeddings = encoder(["document1", "document2"])

__init__

def __init__(name: Optional[str] = "hugging_face_custom_endpoint",
             huggingface_url: Optional[str] = None,
             huggingface_api_key: Optional[str] = None)

Initializes the HFEndpointEncoder with the specified parameters.

Arguments:

  • name (str): The name of the encoder.
  • huggingface_url (str): The URL of the Hugging Face API endpoint.
  • huggingface_api_key (str): The API key for the Hugging Face API.

Raises:

  • ValueError: If either huggingface_url or huggingface_api_key is None.

__call__

def __call__(docs: List[str]) -> List[List[float]]

Encodes a list of documents into embeddings using the Hugging Face API.

Arguments:

  • docs (List[str]): A list of documents to encode.

Raises:

  • ValueError: If no embeddings are returned for a document.

Returns:

List[List[float]]: A list of embeddings for the given documents.

query

def query(payload, max_retries=3, retry_interval=5)

Sends a query to the Hugging Face API and returns the response.

Arguments:

  • payload (dict): The payload to send in the request.

Raises:

  • ValueError: If the query fails or the response status is not 200.

Returns:

dict: The response from the Hugging Face API.

On this page