函数调用(工具)
如何通过 GetVibe API 使用函数调用(工具使用):tools 和 tool_choice 参数,以及多轮工具对话。提供 Python 和 curl 编写的完整智能体循环。
函数调用(工具使用)允许模型请求调用你的函数,并获取返回结果。所有 OpenAI 兼容模型均支持此功能。
参数
| 参数 | 类型 | 描述 |
|---|---|---|
tools | arrayGPT · Claude | 工具列表。每个对象:{ type: "function", function: { name, description, parameters } }。 |
tool_choice | string | objectGPT · Claude | "auto"(由模型决定)、"none"(不调用)、"required"(必须调用)或 { type: "function", function: { name } }:指定工具。 |
parallel_tool_calls | booleanGPT | true — 模型会同时请求多个工具。默认为 true。Claude 不支持此标志,但会自行执行并行调用。 |
示例:单次调用
curl https://getvibeapi.com/v1/chat/completions \
-H "Authorization: Bearer $GETVIBE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-6.1-sol",
"tools": [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Returns the current weather in a city",
"parameters": {
"type": "object",
"properties": {
"city": { "type": "string", "description": "City name" }
},
"required": ["city"]
}
}
}],
"tool_choice": "auto",
"messages": [{"role": "user", "content": "What is the weather in London?"}]
}'多轮:传回结果
工具调用后,添加一条包含结果的 tool 角色消息,然后发送下一次请求。
# messages after the first response with tool_call:
messages = [
{"role": "user", "content": "What is the weather in London?"},
# model response with the tool call
{"role": "assistant", "tool_calls": msg.tool_calls, "content": None},
# the result of your function
{
"role": "tool",
"tool_call_id": msg.tool_calls[0].id,
"content": json.dumps({"city": "London", "temp": 18, "desc": "Cloudy"}),
},
]
resp2 = client.chat.completions.create(model="gpt-6.1-sol", tools=tools, messages=messages)
print(resp2.choices[0].message.content)
# → "It is currently 18°C and cloudy in London."响应示例(tool_call)
{
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": "call_abc123",
"type": "function",
"function": {
"name": "get_weather",
"arguments": "{\"city\":\"London\"}"
}
}]
},
"finish_reason": "tool_calls"
}]
}OpenAI 兼容格式接受
tools 和 tool_choice 字段;实际支持情况取决于所选模型。不要依赖逐字节透传或上游特有的响应字段。