Hugging Face
Hugging Face
LiteLLM은 Hugging Face Hub에 호스팅된 모델에 대해 여러 서비스를 통해 추론 실행을 지원해요.
- Serverless Inference Providers - Hugging Face는 Together AI, Sambanova 등 여러 추론 제공자를 통해 서버리스 AI 추론에 쉽고 통합된 접근을 제공해요. 유지보수 없이 확장 가능한 이 솔루션은 제품에 AI를 통합하는 가장 빠른 방법이에요.
- Dedicated Inference Endpoints - 모델을 프로덕션에 쉽게 배포하는 제품. 추론은 선택한 클라우드 제공자의 전용 완전 관리 인프라에서 Hugging Face가 실행해요.
출처: 문서
본문
지원 모델 (Supported Models)
Serverless Inference Providers
inference provider의 사용 가능한 모델은 huggingface.co/models에서 "Other" 필터 탭을 클릭해 원하는 provider를 선택해 확인할 수 있어요. 예를 들어 Fireworks 지원 모델 전체는 여기에서 찾을 수 있어요.
Dedicated Inference Endpoints
사용 가능한 모델 목록은 Inference Endpoints 카탈로그를 참고하세요.
인증 (Authentication)
단일 Hugging Face 토큰으로 여러 provider를 통해 추론에 접근할 수 있어요. 호출은 Hugging Face를 통해 라우팅되고 사용량은 표준 provider API 요율로 Hugging Face 계정에 직접 청구돼요. HF_TOKEN 환경 변수를 Hugging Face 토큰으로 설정하기만 하면 됩니다 (https://huggingface.co/settings/tokens 에서 생성). 또는 파라미터로 토큰을 전달할 수 있어요.
export HF_TOKEN="hf_xxxxxx"
completion(..., api_key="hf_xxxxxx")
시작하기 (Getting Started)
Hugging Face 모델을 사용하려면 다음 형식으로 provider와 모델을 모두 지정해요:
huggingface/<provider>/<hf_org_or_user>/<hf_model>
<hf_org_or_user>/<hf_model>은 Hugging Face 모델 ID이고 <provider>는 inference provider예요. 기본적으로 provider를 지정하지 않으면 LiteLLM이 HF Inference API를 사용해요.
예시:
completion(model="huggingface/together/deepseek-ai/DeepSeek-R1", messages=messages)- Together AI로 DeepSeek-R1 실행completion(model="huggingface/sambanova/Qwen/Qwen2.5-72B-Instruct", messages=messages)- Sambanova로 Qwen2.5-72B-Instruct 실행completion(model="huggingface/meta-llama/Llama-3.3-70B-Instruct", messages=messages)- HF Inference API로 Llama-3.3-70B-Instruct 실행
기본 Completion
import os
from litellm import completion
os.environ["HF_TOKEN"] = "hf_xxxxxx"
response = completion(
model="huggingface/together/deepseek-ai/DeepSeek-R1",
messages=[
{
"role": "user",
"content": "How many r's are in the word 'strawberry'?",
}
],
)
print(response)
스트리밍 (Streaming)
import os
from litellm import completion
os.environ["HF_TOKEN"] = "hf_xxxxxx"
response = completion(
model="huggingface/together/deepseek-ai/DeepSeek-R1",
messages=[
{
"role": "user",
"content": "How many r's are in the word `strawberry`?",
}
],
stream=True,
)
for chunk in response:
print(chunk)
이미지 입력
모델이 지원하면 이미지를 전달할 수도 있어요. Sambanova를 통한 Llama-3.2-11B-Vision-Instruct 모델 예시:
from litellm import completion
# Set your Hugging Face Token
os.environ["HF_TOKEN"] = "hf_xxxxxx"
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://awsmp-logos.s3.amazonaws.com/seller-xw5kijmvmzasy/c233c9ade2ccb5491072ae232c814942.png",
}
},
],
}
]
response = completion(
model="huggingface/sambanova/meta-llama/Llama-3.2-11B-Vision-Instruct",
messages=messages,
)
print(response.choices[0])
Function Calling
Sambanova를 통한 Qwen2.5-72B-Instruct 모델로 function calling 예시:
import os
from litellm import completion
# Set your Hugging Face Token
os.environ["HF_TOKEN"] = "hf_xxxxxx"
tools = [
{
"type": "function",
"function": {
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"unit": {"type": "string", "enum": ["celsius", "fahrenheit"]},
},
"required": ["location"],
},
}
}
]
messages = [
{
"role": "user",
"content": "What's the weather like in Boston today?",
}
]
response = completion(
model="huggingface/sambanova/meta-llama/Llama-3.3-70B-Instruct",
messages=messages,
tools=tools,
tool_choice="auto"
)
print(response)
Dedicated Inference Endpoints
전용 인프라에 Hugging Face Inference Endpoint를 배포한 후, api_base에 엔드포인트 base URL을 제공하고 모델 이름으로 huggingface/tgi를 지정해 추론을 실행할 수 있어요.
기본 Completion
import os
from litellm import completion
os.environ["HF_TOKEN"] = "hf_xxxxxx"
response = completion(
model="huggingface/tgi",
messages=[{"content": "Hello, how are you?", "role": "user"}],
api_base="https://my-endpoint.endpoints.huggingface.cloud/v1/"
)
print(response)
스트리밍 (Streaming)
import os
from litellm import completion
os.environ["HF_TOKEN"] = "hf_xxxxxx"
response = completion(
model="huggingface/tgi",
messages=[{"content": "Hello, how are you?", "role": "user"}],
api_base="https://my-endpoint.endpoints.huggingface.cloud/v1/",
stream=True
)
for chunk in response:
print(chunk)
이미지 입력
import os
from litellm import completion
os.environ["HF_TOKEN"] = "hf_xxxxxx"
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": "https://awsmp-logos.s3.amazonaws.com/seller-xw5kijmvmzasy/c233c9ade2ccb5491072ae232c814942.png",
}
},
],
}
]
response = completion(
model="huggingface/tgi",
messages=messages,
api_base="https://my-endpoint.endpoints.huggingface.cloud/v1/"
)
print(response.choices[0])
Function Calling
import os
from litellm import completion
os.environ["HF_TOKEN"] = "hf_xxxxxx"
functions = [{
"name": "get_weather",
"description": "Get the weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The location to get weather for"
}
},
"required": ["location"]
}
}]
response = completion(
model="huggingface/tgi",
messages=[{"content": "What's the weather like in San Francisco?", "role": "user"}],
api_base="https://my-endpoint.endpoints.huggingface.cloud/v1/",
functions=functions
)
print(response)
LiteLLM Proxy Server with Hugging Face 모델
지원되는 Inference Providers를 통해 Hugging Face 모델을 서빙하는 LiteLLM Proxy Server를 설정할 수 있어요.
Step 1. config 파일 설정
이 경우 Together AI를 백엔드 Inference Provider로 사용해 Hugging Face에서 DeepSeek R1을 서빙하는 proxy를 구성하고 있어요.
model_list:
- model_name: my-r1-model
litellm_params:
model: huggingface/together/deepseek-ai/DeepSeek-R1
api_key: os.environ/HF_TOKEN # ensure you have `HF_TOKEN` in your .env
Step 2. 서버 시작
litellm --config /path/to/config.yaml
Step 3. 서버에 요청
curl:
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data '{
"model": "my-r1-model",
"messages": [
{
"role": "user",
"content": "Hello, how are you?"
}
]
}'
python:
# uv add openai
from openai import OpenAI
client = OpenAI(
base_url="http://0.0.0.0:4000",
api_key="anything",
)
response = client.chat.completions.create(
model="my-r1-model",
messages=[
{"role": "user", "content": "Hello, how are you?"}
]
)
print(response)
Embedding
LiteLLM은 Hugging Face의 text-embedding-inference 모델도 지원해요.
from litellm import embedding
import os
os.environ['HF_TOKEN'] = "hf_xxxxxx"
response = embedding(
model='huggingface/microsoft/codebert-base',
input=["good morning from litellm"]
)