Using HuggingFace inference providers with Curator
This guide demonstrates how to use Hugging Face Inference Providers with curator to generate synthetic data using various LLM providers available as Inference Providers on Hugging Face. We’ll walk through an example of generating synthetic recipes, but this approach can be adapted for any synthetic data generation task.
What is Inference Providers
Hugging Face’s Inference Providers give developers streamlined, unified access to hundreds of machine learning models, powered by Hugging Face’s serverless inference partners. Using Inference Providers gives you access to a wide range of state of the art models, with newly released models regularly added by providers.
Since Inference Providers are available using OpenAI compatible APIs, you can use them as a drop in replacement for OpenAI in your project. This is the approach we’ll take in this guide.
Setup
Ensure you have a Hugging Face account, you can sign up here.
Create a Hugging Face API key, you can create one here. This will be used to authenticate your requests to the Inference Providers.
Ensure you have at least one Inference Provider enabled in your Hugging Face account. You can configure this in the Inference Providers page.
First, install the necessary packages:
pip install bespokelabs-curator
pip install huggingface_hub openai
Steps
1. Create a curator.LLM Subclass
First, create a class that inherits from curator.LLM. You’ll need to implement two key methods:
prompt()
: Generates the prompt for the LLMparse()
: Processes the LLM’s response into your desired format
"""Generate synthetic recipes for different cuisines using curator."""
from datasets import Dataset
from bespokelabs import curator
class RecipeGenerator(curator.LLM):
"""A recipe generator that generates recipes for different cuisines."""
def prompt(self, input: dict) -> str:
"""Generate a prompt using the template and cuisine."""
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:
"""Parse the model response along with the input to the model into the desired output format."""
return {
"recipe": response,
"cuisine": input["cuisine"],
}
2. Set Up Your Seed Dataset
Create a dataset of inputs using the HuggingFace Dataset class:
# List of cuisines to generate recipes for
cuisines = [
{"cuisine": cuisine}
for cuisine in [
"Chinese",
"Italian",
"Mexican",
"French",
"Japanese",
"Indian",
"Thai",
"Korean",
"Vietnamese",
"Brazilian",
]
]
cuisines = Dataset.from_list(cuisines)
3. Configure the OpenAI Backend to use an Inference Provider
Since Inference Providers are available using OpenAI compatible APIs, we can use them as a drop in replacement for OpenAI in our project. We just need to configure a few things:
the
base_url
the
api_key
the
model_name
You can find this infromation on the model page of the Inference Provider. For example, here’s the information for the Together Inference Provider: https://huggingface.co/meta-llama/Llama-4-Scout-17B-16E-Instruct?inference_provider=together&language=python&inference_api=true
recipe_generator = RecipeGenerator(
model_name="meta-llama/Llama-4-Scout-17B-16E-Instruct",
backend="openai",
backend_params={
"base_url": "https://router.huggingface.co/together/v1",
"api_key": HF_TOKEN, # your Hugging Face API key
},
)
results = recipe_generator(cuisines).dataset
print(results.to_pandas())
4. Push to Hub
You can view the dataset in Curator Viewer or HuggingFace hub
Curator Viewer
# We can visualize data using Curator viewer easily.
import os
os.environ['CURATOR_VIEWER']='1'
from bespokelabs.curator.utils import push_to_viewer
url = push_to_viewer(results)
OR
since curator is using 🤗 datasets, you can push the results to the Hub just like any other dataset!
results.push_to_hub(
"hf_username/llama-recipes",
private=False,
token=HF_TOKEN,
)
You can see what the dataset looks like on the Hub here!
Tips
Since Inference Providers are available via a standard protocol, it’s easy to swap out models on providers in your pipeline depending on the needs of your project.
You can find a list of models that are available via a specific Inference Provider by using a Inference Provider filter on the Hub. For example, here’s the list of models that are available via the Together Inference Provider.
Last updated