Tool

百科 · 工具与行动 · 模型只点名

上一篇说,问「今天星期几」,Chatbot 只能认怂或瞎编。Agent 要补的第一样是工具:窗口外的事,交给一个有名字、有参数、有返回值的行动。模型只点名,运行时才去执行。

这一篇停在点名。时钟还没被看。

清单

工具写在请求的 tools 里,不写在 messages 里。每一项至少有三样:

  • name:模型用来点名的标识
  • description:什么时候该用它
  • parameters:参数的 JSON Schema。模型按这个形状填,不是随便写一句人话
curl https://api.deepseek.com/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $DEEPSEEK_API_KEY" \
  -d '{
    "model": "deepseek-chat",
    "messages": [
      {"role": "user", "content": "今天星期几"}
    ],
    "tools": [
      {
        "type": "function",
        "function": {
          "name": "get_weekday",
          "description": "按给定时区返回当前日期和星期几",
          "parameters": {
            "type": "object",
            "properties": {
              "timezone": {
                "type": "string",
                "description": "IANA 时区,例如 Asia/Shanghai"
              }
            },
            "required": ["timezone"]
          }
        }
      }
    ],
    "stream": false
  }'

没有这份清单时,模型只能用字回答。有了它,模型知道可以伸一次手。

它点了名

回来的不再是一段自我介绍式的话:

{
  "choices": [
    {
      "message": {
        "role": "assistant",
        "content": null,
        "tool_calls": [
          {
            "id": "call_8f2a",
            "type": "function",
            "function": {
              "name": "get_weekday",
              "arguments": "{\"timezone\":\"Asia/Shanghai\"}"
            }
          }
        ]
      },
      "finish_reason": "tool_calls"
    }
  ]
}

翻译成人话:

  • 你问:今天星期几
  • 它没有回答星期几。content 是空的
  • 它点名 get_weekday,并填了 timezone
  • arguments 是一段 JSON 字符串,还不是已经解析好的对象
  • finish_reason 为 tool_calls:这轮停在伸手,不是 stop

响应里没有日期。接口不会替你跑 get_weekday。谁来读时钟、读完把结果写回 messages,见 Agent loop。

总结

  • 工具 = 名字 + 说明 + 参数 Schema + 一次返回值
  • 清单放在 tools。模型用 tool_calls 点名并填参
  • 点名不是执行。finish_reason 为 tool_calls 时,星期几还不知道

参见