工具调用(Tool / Function Calling)
- 接口:
POST https://api.olddog.shop/v1/chat/completions - 用途:让模型以结构化方式“调用工具”,由你的程序执行后再把结果回传给模型。
说明:本页按 OpenAI 工具调用规范编写。不同模型对工具调用支持程度可能不同,请以控制台可用模型为准。
请求参数(新增部分)
| 参数名 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| tools | array | 否 | 工具列表。每个工具通常为 {"type":"function","function":{...}}。 |
| tool_choice | string / object | 否 | 工具选择策略:"auto" / "none" 或指定某个工具。 |
tools.function 字段
| 字段 | 类型 | 是否必填 | 说明 |
|---|---|---|---|
| name | string | 是 | 函数名(建议使用英文、下划线)。 |
| description | string | 否 | 函数用途描述。 |
| parameters | object | 是 | JSON Schema 参数定义(type, properties, required 等)。 |
cURL 示例(声明工具)
bash
curl --request POST "https://api.olddog.shop/v1/chat/completions" \
--header "Authorization: Bearer $OLD_DOG_API_KEY" \
--header "Content-Type: application/json" \
--data '{
"model": "gpt-4o-mini",
"messages": [
{ "role": "user", "content": "北京今天的天气怎么样?如果不知道就调用查询工具。" }
],
"tools": [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的实时天气",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "城市名称,如 北京" }
},
"required": ["city"]
}
}
}
],
"tool_choice": "auto"
}'典型调用流程(推荐)
- 第一次请求:携带
tools,让模型产出tool_calls - 你的程序执行工具:根据
tool_calls[].function.name和arguments调用真实能力 - 第二次请求:把工具结果作为
role: "tool"的消息回传,让模型继续生成最终回答
