Chat Completions
Send multiple rounds of conversations and parse replies, end reasons and usage.
Minimum request
POST /v1/chat/completions:
{
"model": "MODEL_ID",
"messages": [
{
"role": "system",
"content": "用简洁中文回答。"
},
{
"role": "user",
"content": "解释什么是向量检索。"
}
]
}
Please bring the API key of this site. When the model does not support the system role or certain parameters, it should be adjusted according to its protocol description to avoid appending invalid fields.
Multi-turn context
Chat interface usually requires the caller to submit necessary historical messages. Append the previous round of assistant replies and the new round of user messages to the history; the longer the history, the greater the input cost and context usage. Before deleting history, make sure you will not lose tool calls and result pairs.
Read response
Normal text reads choices[0].message.content. Also check finish_reason: normal ending, length truncation, tool calls, etc. require different handling. The tool results cannot be executed as ordinary text splicing. The details of usage are subject to the actual return from the model and upstream.
Parameter usage
Keep requests to a minimum first, then add temperatures, output caps, or structured formats based on model capabilities. Some inference models do not accept traditional sampling parameters. The semantics of field defaults and explicit 0 and false are different. Do not use true or false judgment to discard user settings.
See Streaming for real-time output.
Directly executable requests
First set DUNDUN_API_KEY and replace the model in the request with the real ID:
curl --fail-with-body https://dunduntoken.com/v1/chat/completions -H "Authorization: Bearer $DUNDUN_API_KEY" -H "Content-Type: application/json" -d '{"model":"MODEL_ID","messages":[{"role":"user","content":"只回复 OK"}]}'
Response field diagram
{
"id": "EXAMPLE_COMPLETION_ID",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "OK"
},
"finish_reason": "stop"
}
]
}
The application first checks the HTTP status and then checks whether choices exist. stop usually indicates that the current round ends normally; length indicates possible truncation; tool_calls indicates that a tool process is required. Different models may have additional termination reasons, save unknown values for diagnostics, and do not silently treat them as successes.
Second round of requests
{
"model": "MODEL_ID",
"messages": [
{
"role": "user",
"content": "只回复 OK"
},
{
"role": "assistant",
"content": "OK"
},
{
"role": "user",
"content": "将刚才的回答翻译成中文"
}
]
}
The history is maintained by the application. Preserve system constraints, current problem, and full tool pairing when cropping. Don’t assume that the response ID will automatically be inherited by the next Chat.
Common mistakes
400 Check roles, content blocks, and unsupported parameters; 401 Check Key; 404 Check path or model; 429 Reduce concurrency. Log the response header X-Oneapi-Request-Id, do not log Authorization. For complete SDK error handling, see SDK Tutorial.