> ## Documentation Index
> Fetch the complete documentation index at: https://docs.aurelio.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 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](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

```python theme={null}
class HuggingFaceEncoder(DenseEncoder)
```

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:

```python theme={null}
from semantic_router.encoders import HuggingFaceEncoder

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

#### \_\_call\_\_

```python theme={null}
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

```python theme={null}
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. The class supports
customization of the score threshold for filtering or processing the embeddings.

Example usage:

```python theme={null}
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\_\_

```python theme={null}
def __init__(name: Optional[str] = "hugging_face_custom_endpoint",
             huggingface_url: Optional[str] = None,
             huggingface_api_key: Optional[str] = None,
             score_threshold: float = 0.8)
```

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 either `huggingface_url` or `huggingface_api_key` is None.

#### \_\_call\_\_

```python theme={null}
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

```python theme={null}
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.
