函数调用FunctionCall
Function Call 可以让模型根据用户问题,自动判断是否需要调用外部函数,并生成对应的函数名和参数。适合天气查询、订单查询、库存检索、数据库查询、企业内部系统对接等场景。
接口地址
https://api.maomaotoken.com/v1/responses功能特性
- 智能调用模型可以根据用户问题自动选择合适的工具函数。
- 参数解析模型会从自然语言中提取结构化参数,例如城市、单位、订单号等。
- 灵活控制通过 tool_choice 控制自动调用、强制调用或禁用函数调用。
Python 调用示例
下面示例定义了一个 get_current_weather 工具,让模型根据用户问题判断是否需要查询天气。
"""
MaomaoToken Function Call 示例
功能:
通过定义工具函数,让模型能够生成外部函数调用参数。
示例场景:
用户询问天气时,模型会生成 get_current_weather 的函数调用。
"""
import json
import requests
# ==================== API 配置 ====================
url = "https://api.maomaotoken.com/v1/responses"
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer sk-*********************************",
}
# ==================== 工具定义 ====================
tools = [
{
"type": "function",
"name": "get_current_weather",
"description": "获取指定位置的当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市和国家或地区,例如 Beijing, China",
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
},
},
"required": ["location", "unit"],
},
}
]
# ==================== 请求数据 ====================
data = {
"model": "gpt-5-mini",
"input": "今天北京的天气热吗?",
"tools": tools,
"tool_choice": "auto",
}
# ==================== 发送请求 ====================
print("正在发送请求到 MaomaoToken...")
print(f"模型: {data['model']}")
print(f"问题: {data['input']}")
response = requests.post(
url=url,
headers=headers,
data=json.dumps(data),
)
if response.status_code == 200:
result = response.json()
print("响应成功:")
print("=" * 50)
print(json.dumps(result, indent=2, ensure_ascii=False))
print("=" * 50)
else:
print(f"请求失败,状态码: {response.status_code}")
print(response.text)响应示例
当模型判断需要调用函数时,output 数组中会出现 function_call 类型的结果:
{
"id": "resp_example",
"object": "response",
"created_at": 1762522554,
"status": "completed",
"model": "gpt-5-mini",
"output": [
{
"id": "rs_example",
"type": "reasoning",
"summary": []
},
{
"id": "fc_example",
"type": "function_call",
"status": "completed",
"arguments": "{\"location\":\"Beijing, China\",\"unit\":\"celsius\"}",
"call_id": "call_example",
"name": "get_current_weather"
}
],
"tool_choice": "auto",
"usage": {
"input_tokens": 73,
"output_tokens": 157,
"total_tokens": 230
}
}解析函数调用
你可以从 output 中找到 function_call,读取函数名和参数:
result = response.json()
for item in result.get("output", []):
if item.get("type") == "function_call":
function_name = item.get("name")
arguments = json.loads(item.get("arguments", "{}"))
print("函数名:", function_name)
print("参数:", arguments)实际业务中,下一步通常是:
- 根据
name找到你本地或服务端的真实函数。 - 使用
arguments执行函数。 - 将函数执行结果再提交给模型,让模型生成最终回答。
字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
type | string | 输出类型,函数调用时通常为 function_call。 |
name | string | 模型选择调用的函数名称。 |
arguments | string | JSON 字符串格式的函数参数。 |
call_id | string | 本次函数调用的唯一标识。 |
status | string | 函数调用生成状态。 |
tool_choice 参数
| 值 | 说明 | 使用场景 |
|---|---|---|
auto | 模型自动判断是否调用函数。 | 默认推荐,灵活性最高。 |
required | 强制模型调用函数。 | 必须获取实时数据或业务系统数据时使用。 |
none | 禁用函数调用。 | 只需要普通文本回答时使用。 |
工具定义规范
每个工具通常需要包含:
type:固定为function。name:函数名称,建议使用下划线命名。description:清晰描述函数用途,帮助模型判断调用时机。parameters:符合 JSON Schema 的参数定义。
注意事项
- 请将示例中的
sk-*********************************替换为你的真实 API Key。 - 函数参数需要符合 JSON Schema 规范。
- 生产环境建议添加错误处理、超时控制和重试机制。
- 对订单、账户、支付等敏感操作,建议加入权限校验和人工确认流程。
- Function Call 返回的是「要调用哪个函数以及参数」,真实函数执行仍然需要由你的业务系统完成。