본문 바로가기
C.W.K.
Stream
Lesson 01 of 05 · published

InferenceClient: 객체 하나, 백엔드 여러 개

~26 min · inference, client

Level 0스카우트
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

클라이언트 하나가 여러 추론 백엔드를 감싸

huggingface_hub.InferenceClient는 HF Inference API, HF가 연결한 외부 provider, 직접 운영하는 TGI 서버, OpenAI 호환 끝점과 대화하는 공통 클라이언트야. providermodel을 정한 뒤 chat_completion, text_generation, image_generation 같은 작업 메서드를 호출해.

호출부와 운영 위치를 분리해

개발에서는 관리형 Hub 끝점을 쓰고 운영에서는 자체 TGI로 옮겨도 애플리케이션 호출 구조를 유지할 수 있어. 백엔드 주소와 provider 설정만 바꾸면 되므로 특정 서비스 SDK가 코드 전체에 퍼지는 일을 막아 줘.

Code

HF-호스트 인퍼런스에 InferenceClient·python
from huggingface_hub import InferenceClient

client = InferenceClient(
    model="meta-llama/Llama-3.1-8B-Instruct",
    provider="hf-inference",   # HF 자체 routing
)

resp = client.chat_completion(
    messages=[{"role": "user", "content": "Explain Hugging Face in one sentence."}],
    max_tokens=120,
)
print(resp.choices[0].message.content)
같은 코드, 다른 백엔드 (Together AI)·python
from huggingface_hub import InferenceClient

client = InferenceClient(
    model="meta-llama/Meta-Llama-3.1-70B-Instruct-Turbo",
    provider="together",
)

resp = client.chat_completion(
    messages=[{"role": "user", "content": "Same prompt, larger model."}],
    max_tokens=120,
)
print(resp.choices[0].message.content)

External links

Exercise

같은 chat_completion 콜을 같은 모델 id (또는 가장 가까운 매치) 로 프로바이더 셋 (예: hf-inference, together, 접근 가능한 다른 거) 에 쳐. 각각 시간 측정. 어느 프로바이더가 빠른지, region 관련 변동 있는지 메모.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.