[智能体-178]:为了支持Tools调用,openAI增加了如下的关键项:tools、tool_choice、tool_calls 、ToolMessage,

发布时间:2026/5/31 11:56:41

[智能体-178]:为了支持Tools调用,openAI增加了如下的关键项:tools、tool_choice、tool_calls 、ToolMessage, 结合 OpenAI 接口定义逐一讲解tools、tool_choice、tool_calls、tool角色消息ToolMessage附带完整可运行 Python 代码示例基于官方openai新版 SDK。一、四个核心字段 / 概念说明1.tools请求参数作用客户端向模型声明可用工具列表内部通过 JSON Schema 描述工具名称、功能、入参规则相当于工具说明书。类型数组每个元素为一个工具定义对象。2.tool_choice请求参数作用控制模型调用工具的策略决定是否调用、调用哪个工具。可选值auto模型自主判断是否调用工具默认none禁止使用任何工具指定对象强制调用某一个工具3.tool_calls响应字段作用模型返回给客户端的结构化调用指令包含工具名、调用 IDtool_call_id、入参 JSON 字符串。仅当模型决定调用工具时才会出现对应finish_reason: tool_calls。4.tool角色消息对应 ToolMessage作用客户端把工具执行结果回传给模型的标准消息格式必须携带tool_call_id关联上一轮调用完成会话闭环。OpenAI 原生字段为role: toolLangChain 中封装为ToolMessage。二、完整代码示例原生 OpenAI SDK环境依赖bash运行pip install openai完整可运行代码python运行from openai import OpenAI import json # 初始化客户端 client OpenAI() # 1. 定义本地工具函数 def get_weather(city: str) - str: 模拟查询城市天气 weather_data { 北京: 晴24℃, 上海: 多云26℃, 广州: 小雨28℃ } return f{city}{weather_data.get(city, 暂无天气数据)} # 2. 构造 tools 工具定义JSON Schema tools [ { type: function, function: { name: get_weather, description: 根据城市名称查询当地天气, strict: True, parameters: { type: object, properties: { city: { type: string, description: 需要查询的城市名称 } }, required: [city], additionalProperties: False } } } ] # 3. 初始化对话上下文 messages [ {role: user, content: 帮我查一下北京的天气} ] # 4. 第一轮请求模型生成 tool_calls 调用指令 response client.chat.completions.create( modelgpt-3.5-turbo, messagesmessages, toolstools, # 核心1传入工具列表 tool_choiceauto # 核心2工具调用策略 ) resp_msg response.choices[0].message finish_reason response.choices[0].finish_reason print( 模型原始响应 ) print(ffinish_reason: {finish_reason}) print(ftool_calls: {resp_msg.tool_calls}\n) # 判断是否需要调用工具这是关键表明是否需要执行函数后工具调用 if finish_reason tool_calls and resp_msg.tool_calls: # 遍历解析 tool_calls核心3模型返回的调用指令 for tool_call in resp_msg.tool_calls: call_id tool_call.id func_name tool_call.function.name func_args json.loads(tool_call.function.arguments) # 本地执行工具 if func_name get_weather: tool_result get_weather(**func_args) # 5. 追加 tool 角色消息核心4回传工具结果 messages.append(resp_msg) # 把工具执行的结果添加到上下文中回传给大模型 messages.append({ role: tool, # 表明这是工具调用的输出 tool_call_id: call_id, # 表明这是针对大模型的工具调用上下文id name: func_name, # 表明工具的名称 content: tool_result # 表明工具本次执行的结果 }) # 6. 第二轮请求模型结合工具结果生成最终回答 final_response client.chat.completions.create( modelgpt-3.5-turbo, messagesmessages ) print( 最终回答 ) print(final_response.choices[0].message.content) else: # 无需调用工具直接输出文本 print( 最终回答 ) print(resp_msg.content)三、关键字段逐段解读1.tools字段请求体python运行tools [ { type: function, function: { name: get_weather, description: 根据城市名称查询当地天气, strict: True, parameters: { ... } # JSON Schema 参数规范 } } ]向模型告知可用工具、功能、参数约束。2.tool_choice字段请求体python运行tool_choiceautoauto模型自行决定是否调用天气工具改为tool_choicenone模型直接回答文本拒绝调用工具强制指定工具写法python运行tool_choice{ type: function, function: {name: get_weather} }3.tool_calls字段响应体模型返回结构示例python运行[ { id: call-xxxx, type: function, function: { name: get_weather, arguments: {\city\:\北京\} } } ]id调用唯一 ID回传结果时必须携带name要执行的工具名argumentsJSON 字符串格式的入参。4.tool角色消息结果回传python运行{ role: tool, tool_call_id: call_id, name: func_name, content: tool_result }标准回执消息把工具执行结果送回模型形成完整调用闭环。四、整体执行流程梳理客户端携带messagestoolstool_choice发起请求模型分析后返回tool_calls调用指令客户端解析指令、执行本地工具用role: tool消息回传结果模型基于工具数据生成最终自然语言回答。

相关新闻