5. 对话补全 Chat Completions#
POST /v1/chat/completions — 最核心的接口,兼容 OpenAI Chat Completions。
请求参数#
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名,如 gpt-4o、claude-3-5-sonnet-20241022 |
messages | array | 是 | 消息数组,每项含 role(system/user/assistant)与 content |
stream | boolean | 否 | 是否流式返回,默认 false |
temperature | number | 否 | 采样温度,越高越随机 |
max_tokens | integer | 否 | 生成的最大 token 数 |
其他 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": "它的导演是谁?" }
]
}