对话接口通用说明

使用前按接入基础设置 SHANHAI_BASE_URL(本站根地址)和 SHANHAI_API_KEY(本站令牌)。下方命令适用于 Bash;素材示例地址须替换为可直接下载的真实地址。

接口格式与选择

格式 请求地址 主要输入 最终文本位置
Chat Completions POST /v1/chat/completions messages choices[].message.content
Messages POST /v1/messages 顶层 system 和 messages content 中 type:text 的 text
Responses POST /v1/responses instructions 和 input output 中 message 的 content[].text

已有应用优先沿用它使用的格式。下文兼容格式示例使用 qwen3.8-flash;各模型可用输入和工具仍以对应页面为准。三种格式的字段不能混在一个请求里,接口兼容不代表自动具有内置浏览、代码执行、文件托管等能力。

请求参数

参数 类型 必填 用法
model string 是 本页准确模型名
messages array 是 按时间顺序传入完整对话,至少一条
messages[].role string 是 system 设置任务,user 提问,assistant 为历史回答,tool 为工具结果
messages[].content string / array / null 视消息而定 文字通常为字符串;工具调用消息可为 null。图片内容格式见下文,不能把普通文件路径当文字自动上传
messages[].tool_calls array 否 原样保留模型返回的工具调用,和对应工具结果一起回传
messages[].tool_call_id string 工具结果必填 与被回应的工具调用 id 相同
max_tokens integer 否 限制输出长度,示例 1024 不是模型上限;上下文还包括输入与历史消息
stream boolean 否 默认 false;true 按事件逐段返回
stream_options.include_usage boolean 否 配合 stream:true 请求末尾用量;处理时允许空 choices 和空 usage
temperature number 否 调整生成随机性;未设置时保留模型默认值,思考模式下可能固定或忽略
top_p number 否 调整候选词范围,通常与 temperature 只调一个
stop string / array 否 遇到指定文本时停止;不要把结束条件写成消息角色
response_format object 否 {"type":"json_object"} 请求 JSON,并在消息中明确要求 JSON;支持情况受模型及思考模式限制
tools array 否 函数工具定义;type 为 function,function 内写 name、description、parameters
tool_choice string / object 否 auto 由模型判断;none 禁用;required 或指定函数需模型支持
parallel_tool_calls boolean 否 是否允许并行工具建议,客户端仍应处理返回的每个调用

消息、历史和思考

每次请求传入本轮需要的历史,保留角色和顺序。工具调用型 assistant 消息可以没有文字,但必须保留 tool_calls。某些模型返回 reasoning_content(思考内容),应与 content(最终答案)分开保存;多轮回传时保持其原字段位置。

不同模型使用不同思考开关,按模型页填写。不要同时传 thinking、enable_thinking、reasoning_effort 来表达矛盾的设置。max_tokens 的含义随模型而异;支持 max_completion_tokens 的模型可用它限制完整输出,但不要同时传两个上限。

请求示例

curl --fail-with-body "$SHANHAI_BASE_URL/v1/chat/completions" \
  -H "Authorization: Bearer $SHANHAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen3.8-flash",
  "messages": [
    {
      "role": "system",
      "content": "你是一位简明的中文助手。"
    },
    {
      "role": "user",
      "content": "用一句话介绍ShangHai Do。"
    }
  ],
  "max_tokens": 1024,
  "stream": false
}'

连续输出和用量

设置 stream:true;可同时请求 stream_options.include_usage:true。服务返回以空行分隔的 data: 事件,不保证一次网络读取恰好对应一个事件。

curl -N --fail-with-body "$SHANHAI_BASE_URL/v1/chat/completions" \
  -H "Authorization: Bearer $SHANHAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen3.8-flash",
  "messages": [
    {
      "role": "user",
      "content": "写一段产品介绍。"
    }
  ],
  "stream": true,
  "stream_options": {
    "include_usage": true
  }
}'

读取 choices[].delta.content 并顺序拼接;reasoning_content 独立保存。末尾可能只含 usage 而 choices 为空,也可能在最后一段内容上附带 usage。收到 data: [DONE] 后结束。网络断开且没有结束事件时,标记回答未完整接收,不要自动重复提交有外部副作用的工具请求。

工具定义、执行和结果回传

第一步,向模型提供函数名称、用途和参数结构。下面的函数由你的应用实现,本站不会替你访问天气服务。

{
  "model": "qwen3.8-flash",
  "messages": [
    {
      "role": "user",
      "content": "杭州现在天气如何?"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "查询指定城市天气",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string"
            }
          },
          "required": ["city"],
          "additionalProperties": false
        }
      }
    }
  ],
  "tool_choice": "auto",
  "max_tokens": 1024
}

第二步,模型可能返回这样的 assistant 消息:

{
  "role": "assistant",
  "content": null,
  "tool_calls": [
    {
      "id": "call_EXAMPLE",
      "type": "function",
      "function": {
        "name": "get_weather",
        "arguments": "{\"city\":\"杭州\"}"
      }
    }
  ]
}

第三步,由应用检查函数名是否在允许列表内、解析并校验 arguments,然后执行函数。只执行应用允许的操作,涉及发送、付款、删除等操作时按应用权限确认。把真实结果和原 assistant 消息一起追加回历史。下例的天气是假数据,用于展示结构,须替换为实际结果:

{
  "model": "qwen3.8-flash",
  "messages": [
    {
      "role": "user",
      "content": "杭州现在天气如何?"
    },
    {
      "role": "assistant",
      "content": null,
      "tool_calls": [
        {
          "id": "call_EXAMPLE",
          "type": "function",
          "function": {
            "name": "get_weather",
            "arguments": "{\"city\":\"杭州\"}"
          }
        }
      ]
    },
    {
      "role": "tool",
      "tool_call_id": "call_EXAMPLE",
      "content": "{\"city\":\"杭州\",\"weather\":\"示例:晴\",\"temperature_c\":25}"
    }
  ],
  "tools": [
    {
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "查询指定城市天气",
        "parameters": {
          "type": "object",
          "properties": {
            "city": {
              "type": "string"
            }
          },
          "required": ["city"],
          "additionalProperties": false
        }
      }
    }
  ],
  "tool_choice": "auto",
  "max_tokens": 1024
}

随后模型才能根据工具结果回答。一轮返回多个 tool_calls 时,分别回传每个 id 的结果。连续输出的 function.arguments 是片段,按 tool_calls[].index 合并后才可解析。不要把函数调用当最终文本,也不要遗漏原 assistant 消息。

JSON 输出

同时设置 response_format 和文字指令;接收后仍需做 JSON 解析与业务字段校验。输出因 length 截断时不能作为完整 JSON 使用。json_schema 不是所有模型都支持,不能仅凭接口兼容就启用。

{
  "model": "qwen3.7-plus",
  "enable_thinking": false,
  "response_format": {
    "type": "json_object"
  },
  "messages": [
    {
      "role": "user",
      "content": "只输出 JSON 对象,含 title 字符串和 steps 字符串数组,主题为产品发布。"
    }
  ],
  "max_tokens": 1024
}

图片理解

图片理解不同于生成图片。支持视觉输入的模型可使用以下内容块;示例使用 qwen3.7-plus。地址必须能直接下载,不能填写本机路径。不要把“接口接收了图片字段”当作所有模型均支持视觉的证明。

{
  "model": "qwen3.7-plus",
  "messages": [
    {
      "role": "user",
      "content": [
        {
          "type": "text",
          "text": "描述图中的主体和颜色。"
        },
        {
          "type": "image_url",
          "image_url": {
            "url": "https://example.com/photo.jpg"
          }
        }
      ]
    }
  ],
  "max_tokens": 1024
}

Messages 格式

system 放在顶层;max_tokens 必填。请求仍使用本站令牌,建议带 anthropic-version:2023-06-01。

curl --fail-with-body "$SHANHAI_BASE_URL/v1/messages" \
  -H "Authorization: Bearer $SHANHAI_API_KEY" \
  -H "Content-Type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
  "model": "qwen3.8-flash",
  "system": "用简明中文回答。",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "介绍一下工具调用。"
    }
  ]
}'

返回 content 是内容块数组,文本块 type 为 text,思考块和工具块应分别处理。工具定义使用 name、description、input_schema;模型返回 tool_use(id、name、input)后,在下一条 user 的 content 中加入 tool_result,tool_use_id 指向原 id,content 放工具结果。stop_reason 为 tool_use 时执行工具,为 max_tokens 时表示截断。

{
  "model": "qwen3.8-flash",
  "max_tokens": 1024,
  "messages": [
    {
      "role": "user",
      "content": "杭州天气如何?"
    },
    {
      "role": "assistant",
      "content": [
        {
          "type": "tool_use",
          "id": "toolu_EXAMPLE",
          "name": "get_weather",
          "input": {
            "city": "杭州"
          }
        }
      ]
    },
    {
      "role": "user",
      "content": [
        {
          "type": "tool_result",
          "tool_use_id": "toolu_EXAMPLE",
          "content": "{\"weather\":\"示例:晴\"}"
        }
      ]
    }
  ],
  "tools": [
    {
      "name": "get_weather",
      "description": "查询天气",
      "input_schema": {
        "type": "object",
        "properties": {
          "city": {
            "type": "string"
          }
        },
        "required": ["city"],
        "additionalProperties": false
      }
    }
  ]
}

流式时解析 message_start、content_block_start、content_block_delta、content_block_stop、message_delta、message_stop;文字来自 text_delta.text,工具参数来自 input_json_delta.partial_json。不能用 Chat 的 choices 读取 Messages 结果。

Responses 格式

简单请求可把 input 写成字符串;instructions 用于设置任务。示例使用完整响应,客户端从 output 的 message 内容块读取文本,不要假定原始 JSON 一定有 output_text 快捷字段。

curl --fail-with-body "$SHANHAI_BASE_URL/v1/responses" \
  -H "Authorization: Bearer $SHANHAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "qwen3.8-flash",
  "instructions": "用简明中文回答。",
  "input": "介绍一下工具调用。",
  "max_output_tokens": 1024,
  "stream": false
}'

函数定义在 tools 中直接放 type:function、name、description、parameters,不再包 function 对象。返回 output 中的 function_call 含 call_id、name、arguments;解析参数并执行后,把原输出项和 function_call_output 加入下一轮 input,call_id 必须匹配。

{
  "model": "qwen3.8-flash",
  "input": [
    {
      "role": "user",
      "content": "杭州天气如何?"
    },
    {
      "type": "function_call",
      "call_id": "call_EXAMPLE",
      "name": "get_weather",
      "arguments": "{\"city\":\"杭州\"}"
    },
    {
      "type": "function_call_output",
      "call_id": "call_EXAMPLE",
      "output": "{\"weather\":\"示例:晴\"}"
    }
  ],
  "tools": [
    {
      "type": "function",
      "name": "get_weather",
      "description": "查询天气",
      "parameters": {
        "type": "object",
        "properties": {
          "city": {
            "type": "string"
          }
        },
        "required": ["city"],
        "additionalProperties": false
      }
    }
  ],
  "max_output_tokens": 1024
}

这是显式传历史的用法。若返回思考输出项,也一并保留。previous_response_id、后台任务、内置工具等功能需要当前模型服务另行支持;本文不把它们当作所有对话模型的共同能力。流式读取 response.output_text.delta,工具参数读取 response.function_call_arguments.delta;以 response.completed、response.failed 或 response.incomplete 判断最终状态。

返回与错误处理

情况 处理
400 参数错误 检查模型名、字段位置、组合约束及模型专属参数;修改后再请求
401 / 403 检查令牌、权限、可见模型;不要在日志中记录完整令牌
429 按返回的重试提示等待,并降低请求频率
5xx 或连接中断 保存请求编号及错误;已经返回部分内容或执行过工具时,先确认状态再重试
finish_reason:length 输出截断,调整上限或缩短历史;JSON 可能不完整
工具调用返回 应用执行后继续下一轮;只显示工具参数不会得到最终答案

usage 记录输入、输出和总 Token 数,缓存明细可能出现在 prompt_tokens_details.cached_tokens。不同协议字段名称不同,金额以本站消费记录为准。处理长任务时设置合理超时,并保存请求编号用于排查。

模型明细

deepseek-v4-flash-0817、deepseek-v4-pro-0817、deepseek-v4.1-flash、deepseek-v4.1-flash-1017、glm-5.2、glm-5.3、glm-5.3-flash、kimi-k3、qwen3.7-plus、qwen3.7-max、qwen3.8-flash、qwen3.8-max

遇到问题

调用或配置遇到问题,请联系站内客服,提供模型名、任务编号、出错时间及错误信息,不要提供完整令牌。