TGI의 OpenAI 호환 Messages API 개념

TGI의 OpenAI 호환 Messages API 개념

Text Generation Inference는 OpenAI API와 호환되는 Messages API를 제공해서 기존 OpenAI 방식의 클라이언트 코드를 그대로 TGI 서버에 재사용할 수 있어요. 이 문서는 이 API의 동작 개념을 설명합니다.

출처: https://huggingface.co/docs/text-generation-inference/conceptual/messages_api

Messages API란

TGI는 POST /v1/chat/completions 형태의 엔드포인트를 노출해요. 요청 본문은 OpenAI의 ChatCompletionRequest 스키마를 따르므로, openai Python 라이브러리를 그대로 사용할 수 있습니다.

curl http://localhost:3000/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "model": "tgi",
    "messages": [
      {"role": "user", "content": "What is Deep Learning?"}
    ]
  }'
  • messages: role(user/system/assistant)과 content를 담은 대화 목록.
  • model: 서버가 서빙 중인 모델 식별자(사용자 정의 문자열이어도 동작).
  • stream: true로 설정하면 SSE(Server-Sent Events)로 스트리밍 응답을 받을 수 있어요.

응답 형식

응답도 OpenAI ChatCompletionResponse 스키마를 따릅니다. choices[0].message.content에 생성된 텍스트가 들어가요.

{
  "id": "chatcmpl-...",
  "object": "chat.completion",
  "choices": [
    {
      "index": 0,
      "message": {"role": "assistant", "content": "Deep learning is ..."}
    }
  ]
}

기능

  • Function calling / tool calling: 모델이 도구를 호출할 수 있게 tools 파라미터를 지원해요.
  • Streaming: stream=True로 토큰 단위 응답을 실시간 수신 가능.
  • JSON mode: OpenAI의 response_format(json_object 등)을 지원합니다.

더 알아보기