黑马天启 · 多模型 Token 平台

工具调用与结构化输出

平台按上游支持程度透传 tools / tool_choice / response_format,不做改写。

工具调用(Function Calling)

curl https://api.token.heimatianqi.com/v1/chat/completions \
  -H "Authorization: Bearer sk-heima-xxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "deepseek-v4-flash",
    "messages": [{"role": "user", "content": "北京现在天气怎么样?"}],
    "tools": [{
      "type": "function",
      "function": {
        "name": "get_weather",
        "description": "查询城市天气",
        "parameters": {
          "type": "object",
          "properties": {"city": {"type": "string"}},
          "required": ["city"]
        }
      }
    }]
  }'

模型返回 tool_calls 时,由你的代码执行函数并把结果作为 role: "tool" 的消息回传, 再发起下一轮请求。

结构化输出

{"response_format": {"type": "json_object"}}

部分上游还支持 json_schema 形态。是否生效取决于模型与上游:

  • 模型详情里会标注是否支持结构化输出;
  • 上游不支持时可能忽略该字段(返回自由文本)或直接报错,请以实际返回为准。

注意

  • 工具调用会让请求体变大、并可能触发多轮调用,成本随轮次线性增加。
  • 需要强约束格式时,除 response_format 外,建议在提示词里也写明输出格式,并做一次校验重试。

常见问题

  • 报错 "tools is not supported":换用支持工具调用的模型,或去掉 tools。
  • 结构化输出偶尔不合法:这是模型行为而非平台问题;应用侧要有兜底解析与重试。