接口调用
创建对话
POST /v1/chat/completions 的请求字段、完整响应示例与工具调用流程。
请求地址
POST https://modelrouter.club/v1/chat/completions
请求头按 快速开始 设置。
常用字段
| 字段 | 类型 | 说明 |
|---|---|---|
model | string | 必填,精确模型 ID |
messages | array | 必填,对话消息列表 |
messages[].role | string | system、user、assistant;回传工具结果时用 tool |
messages[].content | string | 消息内容 |
max_tokens | integer | 可选,最多生成的 token 数,包含思考过程。设得太小会导致正文为空,见下方 finish_reason |
stream | boolean | 可选,设为 true 请求流式输出,见 流式输出 |
tools | array | 可选,提供给模型调用的函数列表,见下方「工具调用」一节 |
请求示例
curl https://modelrouter.club/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "deepseek-v4-pro",
"messages": [
{ "role": "system", "content": "用一句话回答" },
{ "role": "user", "content": "天空为什么是蓝色的?" }
],
"max_tokens": 1024
}'响应示例
以下为 deepseek-v4-pro 的真实返回:
{
"id": "e85dc74d-c71e-4e95-a112-5baefd539b69",
"object": "chat.completion",
"created": 1790524433,
"model": "deepseek-v4-pro",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "因为大气分子对太阳光中波长较短的蓝光散射更强,所以天空呈现蓝色。",
"reasoning_content": "我们需要回答用户中文问题“天空为什么是蓝色的?”要求用一句话回答……"
},
"logprobs": null,
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 91,
"completion_tokens": 80,
"total_tokens": 171,
"prompt_tokens_details": { "cached_tokens": 0 },
"completion_tokens_details": { "reasoning_tokens": 59 }
}
}不同模型会多返回一些各自的字段,例如 DeepSeek 的 system_fingerprint、GLM 的 request_id。读取时按需取用下表中的字段即可,不要依赖字段顺序。
响应字段
| 字段 | 说明 |
|---|---|
choices[0].message.content | 模型的回答正文 |
choices[0].message.reasoning_content | 推理模型的思考过程。只用于展示或调试,下一轮对话不需要回传 |
choices[0].message.tool_calls | 模型要求调用的函数,只在工具调用时出现 |
choices[0].finish_reason | 结束原因,见下表 |
usage.prompt_tokens | 输入 token 数 |
usage.completion_tokens | 输出 token 数,包含思考过程 |
usage.prompt_tokens_details.cached_tokens | 命中缓存的输入 token 数(若模型支持缓存) |
usage.completion_tokens_details.reasoning_tokens | 输出中用于思考过程的 token 数 |
实际扣费以控制台 使用日志 为准。
finish_reason
| 值 | 含义 | 处理 |
|---|---|---|
stop | 正常结束 | 读取 content |
length | 达到 max_tokens 上限被截断 | 调大 max_tokens。推理模型在上限太小时,额度可能全部用于思考,content 为空字符串 |
tool_calls | 模型要求调用函数 | 执行函数并回传结果,见下方「工具调用」一节 |
工具调用
DeepSeek、GLM、Kimi 系列都支持工具调用,流程分三步。
1. 在请求中提供 tools:
{
"model": "deepseek-v4-pro",
"messages": [{ "role": "user", "content": "北京今天天气怎么样?" }],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询城市天气",
"parameters": {
"type": "object",
"properties": { "city": { "type": "string" } },
"required": ["city"]
}
}
}
]
}2. 模型返回要调用的函数,finish_reason 为 tool_calls:
{
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "",
"tool_calls": [
{
"index": 0,
"id": "call_00_ELhHebn204OKGIUlJnQy5964",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\": \"北京\"}"
}
}
]
},
"finish_reason": "tool_calls"
}
]
}arguments 是 JSON 字符串,需要自己解析后再调用函数。
**3. 执行函数,把结果回传给模型:**在 messages 末尾依次追加上一步的 assistant 消息,以及一条 role 为 tool 的结果消息,tool_call_id 对应上一步的 id。tools 保持不变:
{
"model": "deepseek-v4-pro",
"messages": [
{ "role": "user", "content": "北京今天天气怎么样?" },
{
"role": "assistant",
"content": "",
"tool_calls": [
{
"id": "call_00_ELhHebn204OKGIUlJnQy5964",
"type": "function",
"function": { "name": "get_weather", "arguments": "{\"city\": \"北京\"}" }
}
]
},
{
"role": "tool",
"tool_call_id": "call_00_ELhHebn204OKGIUlJnQy5964",
"content": "{\"weather\": \"晴\", \"temp_c\": 22}"
}
],
"tools": [ ... ]
}模型据此给出最终回答,例如 北京今天天气晴朗,气温 22°C。,finish_reason 为 stop。
连续对话
在后续请求的 messages 中附上需要保留的历史消息。接口不会因为复用同一密钥而自动记住上一次对话。历史消息会占用上下文,并可能增加输入用量。
说明
温度、推理强度等其他参数的支持和限制因模型而异。先用最小请求跑通,再按对应模型能力逐项添加。