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 는 단일 Python 클래스, 다 얘기해: HF 호스트 Inference API, HF 통해 routing 되는 third-party 프로바이더 (Together AI, Fireworks, Replicate 등), 직접 띄운 text-generation-inference (TGI) 서버, OpenAI 호환 엔드포인트 무엇이든. provider= + model= 로 construct, chat_completion, text_generation, image_generation 같은 메서드 콜.

왜 중요한가

같은 클라이언트 코드가 dev 에선 managed Hub 엔드포인트, prod 에선 TGI 박스에 동작. 마이그레이션이 인자 두 개 바꾸기. hard-coded OpenAI 클라이언트와 비교: 콜 사이트 다시 짜야 할 거.

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

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

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