对话补全

为对话生成模型响应。

端点

POST
$ POST https://api.alltoken.ai/v1/chat/completions

请求体

JSON
1{
2 "model": "deepseek-chat",
3 "messages": [
4 { "role": "system", "content": "你是一个有用的助手。" },
5 { "role": "user", "content": "你好!" }
6 ],
7 "stream": false,
8 "temperature": 0.7,
9 "max_tokens": 1024
10}

参数

  • model (必填) — 模型 ID(如 "deepseek-chat"
  • messages (必填) — 消息对象数组,包含 rolecontent
  • streamtrue 启用 SSE 流式响应,false 返回完整响应(默认:false)
  • temperature — 采样温度,0-2(默认:1)
  • top_p — 核采样,0-1(默认:1)
  • max_tokens — 最大生成 token 数
  • frequency_penalty — 频率惩罚,-2 到 2(默认:0)
  • presence_penalty — 存在惩罚,-2 到 2(默认:0)
  • tools — 工具/函数定义数组
  • response_format{"type": "json_object"} 保证输出 JSON
  • web_searchtrue 启用联网搜索(取决于模型)

消息角色

  • system — 设置模型的行为和上下文
  • user — 用户消息
  • assistant — 模型之前的回复(用于多轮对话)
  • tool — 函数调用的结果(带 tool_call_id

响应

JSON
1{
2 "id": "chatcmpl-abc123",
3 "object": "chat.completion",
4 "created": 1700000000,
5 "model": "deepseek-chat",
6 "choices": [
7 {
8 "index": 0,
9 "message": {
10 "role": "assistant",
11 "content": "你好!有什么可以帮你的?"
12 },
13 "finish_reason": "stop"
14 }
15 ],
16 "usage": {
17 "prompt_tokens": 12,
18 "completion_tokens": 10,
19 "total_tokens": 22
20 }
21}