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

# Hugging Face Inference Providers

> Use Hugging Face Inference Providers through the OpenAI-compatible backend.

[Hugging Face Inference Providers](https://huggingface.co/docs/inference-providers) give you one API for models hosted by many inference partners. The API is compatible with the OpenAI API, so you can use it with the `openai` backend in Curator. This guide generates vegan recipes.

## Setup

1. [Create a Hugging Face account](https://huggingface.co/join) if you do not have one.
2. [Create an access token](https://huggingface.co/settings/tokens). You use it to authenticate your requests.
3. Turn on at least one provider on the [Inference Providers settings page](https://huggingface.co/settings/inference-providers).

Install the packages.

```bash theme={null}
pip install bespokelabs-curator huggingface_hub
```

<Steps>
  <Step title="Create an LLM subclass">
    ```python theme={null}
    from datasets import Dataset

    from bespokelabs import curator


    class RecipeGenerator(curator.LLM):
        """Generate vegan recipes for different cuisines."""

        def prompt(self, input: dict) -> str:
            return f"Generate a random {input['cuisine']} recipe. Be creative but keep it realistic and make the recipe vegan."

        def parse(self, input: dict, response: str) -> dict:
            return {"recipe": response, "cuisine": input["cuisine"]}
    ```
  </Step>

  <Step title="Create the input dataset">
    ```python theme={null}
    cuisines = Dataset.from_list([
        {"cuisine": cuisine}
        for cuisine in [
            "Chinese", "Italian", "Mexican", "French", "Japanese",
            "Indian", "Thai", "Korean", "Vietnamese", "Brazilian",
        ]
    ])
    ```
  </Step>

  <Step title="Point the OpenAI backend at Hugging Face">
    Set these three values.

    * `base_url` is the Hugging Face router, `https://router.huggingface.co/v1`.
    * `api_key` is your Hugging Face token.
    * `model_name` is the model ID on the Hub. Add `:<provider>` to the end to choose a provider, e.g., `:together`. Without it, Hugging Face picks a provider for you.

    ```python theme={null}
    import os

    HF_TOKEN = os.environ["HF_TOKEN"]

    recipe_generator = RecipeGenerator(
        model_name="meta-llama/Llama-3.3-70B-Instruct:together",
        backend="openai",
        backend_params={
            "base_url": "https://router.huggingface.co/v1",
            "api_key": HF_TOKEN,
        },
    )

    results = recipe_generator(cuisines).dataset
    print(results.to_pandas())
    ```

    The model page on the Hub shows which providers serve a model.
  </Step>

  <Step title="Share the results">
    To look through the results in the [Curator Viewer](/curator/viewer), upload them with `push_to_viewer`.

    ```python theme={null}
    url = curator.push_to_viewer(results)
    ```

    The results are a Hugging Face `Dataset`, so you can also push them to the Hub.

    ```python theme={null}
    results.push_to_hub("<hf-username>/vegan-recipes", private=False, token=HF_TOKEN)
    ```

    [This dataset](https://huggingface.co/datasets/davanstrien/llama-recipes) on the Hub was made in the same way.
  </Step>
</Steps>

## Switch models

You can change the model or the provider by changing `model_name`. To find the models that one provider serves, filter the Hub by that provider, e.g., the [models served by Together](https://huggingface.co/models?inference_provider=together).
