工具调用
在 Chat Completions、Responses 和 Anthropic Messages 中声明工具、处理模型返回的调用并回传结果,以及 GPT-6 工具调用的路径规则。
工作方式
工具不是单独的接口,而是挂在对话接口上的参数:
- 请求时声明
tools; - 模型返回要调用的工具和参数;
- 你在本地执行工具,把结果追加到对话中再次请求;
- 模型根据工具结果给出最终回复。
不同接口的字段不同,请按所用接口查看对应小节。
Chat Completions
适用于 GPT 对话模型、GLM、Kimi、DeepSeek,以及通过 OpenAI 格式调用的 Claude。
第一次请求:携带 tools 和 tool_choice。
curl https://run.fluxlane.ai/v1/chat/completions \
-H "Authorization: Bearer $FLUXLANE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "模型名称",
"messages": [{"role": "user", "content": "北京今天天气如何?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}],
"tool_choice": "auto"
}'
模型返回:调用信息在 message.tool_calls 中。
{
"role": "assistant",
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"北京\"}"
}
}]
}
回传结果:把上一步的 assistant 消息原样追加,再追加一条 role 为 tool、带对应 tool_call_id 的消息。示例中的 call_abc123 需换成上一次响应返回的 id。
curl https://run.fluxlane.ai/v1/chat/completions \
-H "Authorization: Bearer $FLUXLANE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "模型名称",
"messages": [
{"role": "user", "content": "北京今天天气如何?"},
{
"role": "assistant",
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {"name": "get_weather", "arguments": "{\"city\":\"北京\"}"}
}]
},
{"role": "tool", "tool_call_id": "call_abc123", "content": "晴,26°C"}
],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}],
"tool_choice": "auto"
}'
Kimi 需要原样回传 assistant 消息,并逐条回传每个工具调用的结果。
GPT-6 的工具调用路径
GPT-6 在 Chat Completions 上默认开启推理,此时携带 tools 会被拒绝。按模型选择路径:
| 模型 | 纯文本对话 | 工具调用 |
|---|---|---|
gpt-6-astra | /v1/chat/completions | 只能用 /v1/responses |
gpt-6-luna | /v1/chat/completions | 同一路径,并带 reasoning_effort: none |
gpt-6-sol | /v1/chat/completions | /v1/responses,或同一路径并带 reasoning_effort: none |
gpt-5-codex、gpt-5.1-codex、gpt-5.1-codex-mini、gpt-5.6-luna | 只用 /v1/responses | 同一路径 |
在 Chat Completions 上为 gpt-6-luna、gpt-6-sol 使用工具时,须带 "reasoning_effort": "none",输出长度用 max_completion_tokens,回传方式与上面相同:
curl https://run.fluxlane.ai/v1/chat/completions \
-H "Authorization: Bearer $FLUXLANE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6-luna",
"max_completion_tokens": 1024,
"reasoning_effort": "none",
"messages": [{"role": "user", "content": "北京今天天气如何?"}],
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}
}],
"tool_choice": "auto"
}'
Responses
工具声明为扁平结构,输出长度用 max_output_tokens。以 gpt-6-sol 为例(gpt-6-astra 写法相同):
curl https://run.fluxlane.ai/v1/responses \
-H "Authorization: Bearer $FLUXLANE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6-sol",
"max_output_tokens": 1024,
"input": "北京今天天气如何?",
"tools": [{
"type": "function",
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}]
}'
模型在 output 中返回 type 为 function_call 的条目:
{
"type": "function_call",
"call_id": "call_abc123",
"name": "get_weather",
"arguments": "{\"city\":\"北京\"}"
}
把调用条目和 function_call_output 追加到 input 后再次请求。call_abc123 需换成上一次响应返回的 call_id:
curl https://run.fluxlane.ai/v1/responses \
-H "Authorization: Bearer $FLUXLANE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6-sol",
"max_output_tokens": 1024,
"input": [
{"role": "user", "content": "北京今天天气如何?"},
{"type": "function_call", "call_id": "call_abc123", "name": "get_weather", "arguments": "{\"city\":\"北京\"}"},
{"type": "function_call_output", "call_id": "call_abc123", "output": "晴,26°C"}
],
"tools": [{
"type": "function",
"name": "get_weather",
"description": "查询指定城市的天气",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}]
}'
DeepSeek 在本路径上同样使用 {"type": "function", ...} 声明工具,会忽略 web_search、file_search 等内置工具;工具返回的图片放在 output 的 input_image 里。
Anthropic Messages
tools 使用 input_schema。模型返回 tool_use 内容块后,在下一条 user 消息里用 tool_result 回传。toolu_abc123 需换成上一次响应返回的 id。
curl https://run.fluxlane.ai/v1/messages \
-H "x-api-key: $FLUXLANE_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-haiku-4-5-20251001",
"max_tokens": 1024,
"tools": [{
"name": "get_weather",
"description": "查询指定城市的天气",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"]
}
}],
"messages": [
{"role": "user", "content": "北京今天天气如何?"},
{"role": "assistant", "content": [
{"type": "tool_use", "id": "toolu_abc123", "name": "get_weather", "input": {"city": "北京"}}
]},
{"role": "user", "content": [
{"type": "tool_result", "tool_use_id": "toolu_abc123", "content": "晴,26°C"}
]}
]
}'
第一次请求只需要 tools 和第一条 user 消息;上例展示的是回传结果时的完整消息结构。