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. The class supports customization of the score threshold for filtering or processing the embeddings.
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
HuggingFace encoder class for local embedding models. Models can be trained and loaded from private repositories, or from the Huggingface Hub. The class supports customization of the score threshold for filtering or processing the embeddings.
Example usage:
__call__
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
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. The class supports customization of the score threshold for filtering or processing the embeddings.
Example usage:
__init__
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.score_threshold
(float
): A threshold for processing the embeddings.
Raises:
ValueError
: If eitherhuggingface_url
orhuggingface_api_key
is None.
__call__
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
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.