제5장 / 총 21장약 1분

5. 채팅 완성 Chat Completions#

POST /v1/chat/completions — 가장 핵심적인 엔드포인트로, OpenAI Chat Completions와 호환됩니다.

요청 파라미터#

파라미터타입필수설명
modelstring예모델명, 예: gpt-4o, claude-3-5-sonnet-20241022
messagesarray예메시지 배열, 각 항목은 role(system/user/assistant)과 content 포함
streamboolean아니오스트리밍 반환 여부, 기본값 false
temperaturenumber아니오샘플링 온도, 높을수록 무작위성 증가
max_tokensinteger아니오생성할 최대 토큰 수

그 외 OpenAI 표준 파라미터(top_p, presence_penalty 등)는 업스트림 모델로 그대로 전달됩니다.

비스트리밍 요청#

cURL
curl http://183.240.79.21:52099/v1/chat/completions \
  -H "Authorization: Bearer sk-你的YiTuoToken-Key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      { "role": "system", "content": "你是一个乐于助人的助手。" },
      { "role": "user", "content": "你好,请介绍一下你自己" }
    ],
    "temperature": 0.7
  }'

스트리밍 요청(SSE)#

요청 본문에 "stream": true를 추가하면 응답이 Server-Sent Events로 청크 단위로 반환됩니다. 각 청크는 chat.completion.chunk이며, 마지막에 data: [DONE]으로 끝납니다.

cURL
curl http://183.240.79.21:52099/v1/chat/completions \
  -H "Authorization: Bearer sk-你的YiTuoToken-Key" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4o",
    "messages": [{ "role": "user", "content": "写一首关于秋天的短诗" }],
    "stream": true
  }'

멀티턴 대화#

과거 메시지를 순서대로 messages 배열에 넣으면 컨텍스트가 유지됩니다:

JSON
{
  "model": "gpt-4o",
  "messages": [
    { "role": "user", "content": "推荐一部科幻电影" },
    { "role": "assistant", "content": "推荐《星际穿越》。" },
    { "role": "user", "content": "它的导演是谁?" }
  ]
}