Function Calling工作机制与阿里云百炼模型支持版本汇总
时间:2026-07-19 | 作者:318050 | 阅读:0聊大模型,绕不开一个话题:Function Calling。
大模型本身没有"手"和"眼",无法访问实时数据,也无法直接操控外部系统。但有了Function Calling,情况就大不相同了——它允许模型调用外部工具,比如API、数据库、自定义函数,从而获取信息或执行操作,真正突破模型自身能力的限制。
简单来说,Function Calling就是给大模型配上了"外设",让它能与外部世界进行交互,完成更复杂的任务。
工作原理
Function Calling 的实现,本质上是一次应用程序与大模型之间的多步骤"对话":
- 发起第一次模型调用:应用程序将用户问题以及一份"可用工具清单"一起发给大模型。
- 接收模型的工具调用指令:如果模型判断需要调用某个外部工具,会返回一个JSON格式的指令,明确告诉你要调用哪个函数,以及传入什么参数。
如果模型觉得不需要调用工具,它会直接返回一段自然语言回复。
- 在应用端运行工具:应用程序根据指令执行对应的工具函数,获取输出结果。
- 发起第二次模型调用:把工具的输出结果塞回消息数组(messages),再次请求模型。
- 接收来自模型的最终响应:模型结合工具的输出和用户的问题,生成一段自然语言回复。
工作流程示意图
哪些模型支持Function Calling
目前,主流的模型几乎都支持了Function Calling能力。以下是阿里云百炼、DeepSeek、GLM、Kimi和MiniMax各自支持Function Calling的模型版本清单。更多模型信息可以在阿里云百炼平台找到。
千问
文本生成模型:
- 千问Max:Qwen3.7-Max系列、Qwen3.6-Max系列、Qwen3-Max系列、Qwen-Max系列
- 千问Plus:Qwen3.7-Plus系列、Qwen3.6-Plus系列、Qwen3.5-Plus系列、Qwen-Plus系列
- 千问Flash:Qwen3.6-Flash系列、Qwen3.5-Flash系列、Qwen-Flash系列
- 千问Coder:Qwen3-Coder系列、Qwen2.5-Coder系列、Qwen-Coder系列
- 千问Turbo:Qwen-Turbo系列
- Qwen3.6开源系列
- Qwen3.5开源系列
- Qwen3开源系列
- Qwen2.5开源系列
多模态模型:
- 千问VL: Qwen3-VL-Plus系列、 Qwen3-VL-Flash系列
- 千问Omni:Qwen3.5-Omni-Plus系列、Qwen3.5-Omni-Flash系列、Qwen3-Omni-Flash系列
- 千问Omni-Realtime:Qwen3.5-Omni-Plus-Realtime系列、Qwen3.5-Omni-Flash-Realtime系列
- Qwen3-VL 开源系列
DeepSeek
- deepseek-v4-pro
- deepseek-v4-flash
- deepseek-v3.2
- deepseek-v3.2-exp(非思考模式)
- deepseek-v3.1(非思考模式)
- deepseek-r1
- deepseek-r1-0528
- deepseek-v3
GLM
- glm-5.2
- glm-5.1
- glm-5
- glm-4.7
- glm-4.6
- glm-4.5
- glm-4.5-air
Kimi
- kimi-k2.6
- kimi-k2.5
- kimi-k2-thinking
- Moonshot-Kimi-K2-Instruct
MiniMax
- MiniMax-M2.5
- MiniMax-M2.1
Function Calling使用教程
理论讲完了,来看一个实际的例子。下面通过一个"天气查询"场景,完整演示Function Calling的流程。
OpenAI 兼容接口示例
先看Python版本的实现:
from openai import OpenAI from datetime import datetime import json import os import random # 初始化客户端 client = OpenAI( api_key=os.getenv("DASHSCOPE_API_KEY"), base_url="https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1", ) # 模拟用户问题 USER_QUESTION = "北京天气咋样" # 定义工具列表 tools = [ { "type": "function", "function": { "name": "get_current_weather", "description": "当你想查询指定城市的天气时非常有用。", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "城市或县区,比如北京市、杭州市、余杭区等。", } }, "required": ["location"], }, }, }, ] # 模拟天气查询工具 def get_current_weather(arguments): weather_conditions = ["晴天", "多云", "雨天"] random_weather = random.choice(weather_conditions) location = arguments["location"] return f"{location}今天是{random_weather}。" # 封装模型响应函数 def get_response(messages): completion = client.chat.completions.create( model="qwen3.6-plus", extra_body={"enable_thinking": False}, messages=messages, tools=tools, ) return completion messages = [{"role": "user", "content": USER_QUESTION}] response = get_response(messages) assistant_output = response.choices[0].message if assistant_output.content is None: assistant_output.content = "" messages.append(assistant_output) # 如果不需要调用工具,直接输出内容 if assistant_output.tool_calls is None: print(f"无需调用天气查询工具,直接回复:{assistant_output.content}") else: # 进入工具调用循环 while assistant_output.tool_calls is not None: tool_call = assistant_output.tool_calls[0] tool_call_id = tool_call.id func_name = tool_call.function.name arguments = json.loads(tool_call.function.arguments) print(f"正在调用工具 [{func_name}],参数:{arguments}") # 执行工具 tool_result = get_current_weather(arguments) # 构造工具返回信息 tool_message = { "role": "tool", "tool_call_id": tool_call_id, "content": tool_result, # 保持原始工具输出 } print(f"工具返回:{tool_message['content']}") messages.append(tool_message) # 再次调用模型,获取总结后的自然语言回复 response = get_response(messages) assistant_output = response.choices[0].message if assistant_output.content is None: assistant_output.content = "" messages.append(assistant_output) print(f"助手最终回复:{assistant_output.content}")
Node.js版本的实现也很类似:
import OpenAI from 'openai'; // 初始化客户端 const openai = new OpenAI({ apiKey: process.env.DASHSCOPE_API_KEY, baseURL: "https://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/compatible-mode/v1", }); // 定义工具列表 const tools = [ { type: "function", function: { name: "get_current_weather", description: "当你想查询指定城市的天气时非常有用。", parameters: { type: "object", properties: { location: { type: "string", description: "城市或县区,比如北京市、杭州市、余杭区等。", }, }, required: ["location"], }, }, }, ]; // 模拟天气查询工具 const getCurrentWeather = (args) => { const weatherConditions = ["晴天", "多云", "雨天"]; const randomWeather = weatherConditions[Math.floor(Math.random() * weatherConditions.length)]; const location = args.location; return `${location}今天是${randomWeather}。`; }; // 封装模型响应函数 const getResponse = async (messages) => { const response = await openai.chat.completions.create({ model: "qwen3.6-plus", enable_thinking: false, messages: messages, tools: tools, }); return response; }; const main = async () => { const input = "北京天气咋样"; let messages = [ { role: "user", content: input, } ]; let response = await getResponse(messages); let assistantOutput = response.choices[0].message; // 确保 content 不是 null if (!assistantOutput.content) assistantOutput.content = ""; messages.push(assistantOutput); // 判断是否需要调用工具 if (!assistantOutput.tool_calls) { console.log(`无需调用天气查询工具,直接回复:${assistantOutput.content}`); } else { // 进入工具调用循环 while (assistantOutput.tool_calls) { const toolCall = assistantOutput.tool_calls[0]; const toolCallId = toolCall.id; const funcName = toolCall.function.name; const funcArgs = JSON.parse(toolCall.function.arguments); console.log(`正在调用工具 [${funcName}],参数:`, funcArgs); // 执行工具 const toolResult = getCurrentWeather(funcArgs); // 构造工具返回信息 const toolMessage = { role: "tool", tool_call_id: toolCallId, content: toolResult, }; console.log(`工具返回:${toolMessage.content}`); messages.push(toolMessage); // 再次调用模型获取自然语言总结 response = await getResponse(messages); assistantOutput = response.choices[0].message; if (!assistantOutput.content) assistantOutput.content = ""; messages.push(assistantOutput); } console.log(`助手最终回复:${assistantOutput.content}`); } }; // 启动程序 main().catch(console.error);
执行后,控制台会输出类似这样的结果:
正在调用工具 [get_current_weather],参数:{'location': '北京'} 工具返回:北京今天是多云。 助手最终回复:北京今天是多云的天气。
如何使用Function Calling?
Function Calling支持两种方式传入工具信息:
- 方式一:通过 tools 参数传入(推荐)参见如何使用,按照定义工具、创建 messages 数组、发起 Function Calling、运行工具函数、大模型总结工具函数输出的步骤调用。
- 方式二:通过 System Message 传入通过 tools 参数传入效果最佳,服务端会自动适配最优 prompt 模板。如使用 Qwen 模型且不期望使用 tools 参数,参见通过 System Message 传入工具信息。
以下以 OpenAI 兼容接口为例,通过 tools 参数分步骤介绍 Function Calling 的详细用法。
假设业务场景会收到天气查询与时间查询两类问题。
1. 定义工具
工具连接大模型与外部服务,首先需定义工具。
1.1. 创建工具函数
创建两个工具函数:天气查询工具与时间查询工具。
- 天气查询工具接收
arguments参数,arguments格式为{"location": "查询的地点"}。工具的输出为字符串,格式为:“{位置}今天是{天气}”。
为了便于演示,此处定义的天气查询工具并不真正查询天气,会从晴天、多云、雨天随机选择。在实际业务中可使用如 高德天气查询 等工具进行替换。
- 时间查询工具时间查询工具不需要输入参数。工具的输出为字符串,格式为:
“当前时间:{查询到的时间}。”。
如果使用 Node.js,请运行 npm install date-fns 安装获取时间的工具包 date-fns。
Python 实现
## 步骤1:定义工具函数 # 添加导入random模块 import random from datetime import datetime # 模拟天气查询工具。返回结果示例:“北京今天是雨天。” def get_current_weather(arguments): # 定义备选的天气条件列表 weather_conditions = ["晴天", "多云", "雨天"] # 随机选择一个天气条件 random_weather = random.choice(weather_conditions) # 从 JSON 中提取位置信息 location = arguments["location"] # 返回格式化的天气信息 return f"{location}今天是{random_weather}。" # 查询当前时间的工具。返回结果示例:“当前时间:2024-04-15 17:15:18。“ def get_current_time(): # 获取当前日期和时间 current_datetime = datetime.now() # 格式化当前日期和时间 formatted_time = current_datetime.strftime('%Y-%m-%d %H:%M:%S') # 返回格式化后的当前时间 return f"当前时间:{formatted_time}。" # 测试工具函数并输出结果,运行后续步骤时可以去掉以下四句测试代码 print("测试工具输出:") print(get_current_weather({"location": "上海"})) print(get_current_time()) print("n")
Node.js 实现
// 步骤1:定义工具函数 // 导入时间查询工具 import { format } from 'date-fns'; function getCurrentWeather(args) { // 定义备选的天气条件列表 const weatherConditions = ["晴天", "多云", "雨天"]; // 随机选择一个天气条件 const randomWeather = weatherConditions[Math.floor(Math.random() * weatherConditions.length)]; // 从 JSON 中提取位置信息 const location = args.location; // 返回格式化的天气信息 return `${location}今天是${randomWeather}。`; } function getCurrentTime() { // 获取当前日期和时间 const currentDatetime = new Date(); // 格式化当前日期和时间 const formattedTime = format(currentDatetime, 'yyyy-MM-dd HH:mm:ss'); // 返回格式化后的当前时间 return `当前时间:${formattedTime}。`; } // 测试工具函数并输出结果,运行后续步骤时可以去掉以下四句测试代码 console.log("测试工具输出:") console.log(getCurrentWeather({location:"上海"})); console.log(getCurrentTime()); console.log("n")
运行工具后,得到输出:
测试工具输出: 上海今天是多云。 当前时间:2025-01-08 20:21:45。
更多详细的使用教程,请移步到官方文档。
来源:整理自互联网
免责声明:文中图文均来自网络,如有侵权请联系删除,心愿游戏发布此文仅为传递信息,不代表心愿游戏认同其观点或证实其描述。
相关文章
更多-
- Runway Media Router 自动切换AI模型 创意验证到成片省Token
- 时间:2026-07-25
-
- 美国封禁中国AI模型 英伟达黄仁勋AMD苏姿丰支持开源
- 时间:2026-07-24
-
- AI模型攻克16%远程项目,设计自由职业者危机
- 时间:2026-07-24
-
- Runway发布生成式AI模型路由平台 开放开发者架构
- 时间:2026-07-24
-
- 美国推动封禁中国AI模型!英伟达黄仁勋、AMD苏姿丰相继表态 支持开源
- 时间:2026-07-24
-
- 苹果、谷歌、英伟达宣布联合开发新一代AI模型
- 时间:2026-07-23
-
- 阿里云Qoder灵码社区版免费AI模型与credits计费指南
- 时间:2026-07-23
-
- 阿里云Token Plan详解:收费、Credits计费与模型支持指南
- 时间:2026-07-23
精选合集
更多大家都在玩
热门话题
大家都在看
更多-
- iOS 13.5.1电池续航差是电池耗电问题吗
- 时间:2026-07-25
-
- 苹果教育优惠开启 附购买攻略
- 时间:2026-07-25
-
- 苹果iOS 14 beta 2 测试版主要更新内容:除细节变化外修复多项Bug
- 时间:2026-07-25
-
- iOS 14 beta 2 是否解决内存占用过多问题?
- 时间:2026-07-25
-
- 受欢迎的奥特曼游戏有哪些
- 时间:2026-07-25
-
- iOS 14信息应用5大更新变化
- 时间:2026-07-25
-
- iOS 14正式版上线时间公布 官方全新介绍
- 时间:2026-07-25
-
- 最新苹果iOS 14 Beta 2版本更新内容全解析与升级教程
- 时间:2026-07-25

