Chapter 5 of 211 min

5. Chat Completions#

POST /v1/chat/completions — the core endpoint, compatible with OpenAI Chat Completions.

Request parameters#

ParameterTypeRequiredDescription
modelstringYesModel name, e.g. gpt-4o, claude-3-5-sonnet-20241022
messagesarrayYesMessage array; each item contains role (system/user/assistant) and content
streambooleanNoWhether to stream the response; defaults to false
temperaturenumberNoSampling temperature; higher means more random
max_tokensintegerNoMaximum number of tokens to generate

Other standard OpenAI parameters (top_p, presence_penalty, etc.) are passed through to the upstream model.

Non-streaming request#

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
  }'

Streaming request (SSE)#

Add "stream": true to the request body. The response arrives incrementally as Server-Sent Events; each event is a chat.completion.chunk, and the stream ends with 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
  }'

Multi-turn conversations#

Append prior messages, in order, to the messages array to keep context:

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