1、概述大模型根据语言理解调用外部工具比如我们自己定义的函数或工具把参数传给函数。【作用】1、扩展模型的能力模型本身无法直接操作外部系统比如数据库等。通过调用预设函数(Function call)就可以完成发送邮件、获取天气等功能就打通了与外部系统的交互突破模型本身的能力边界。2、结构话输出模型将用户自然语言格式化成结构话参数传递给函数比如用户的自然语言请求如 “明天北京天气如何”解析并格式化为函数可直接使用的结构化参数如 location北京, date2025-05-06让非结构化的语言变成机器能执行的指令。3、模型可根据上下文决定是否/何时调用函数甚至可以链式调用多个函数【流程步骤】步骤 1定义工具 / 函数 schema明确函数能做什么、参数要什么步骤 2大模型解析用户自然语言匹配对应函数步骤 3生成结构化的函数调用参数如 get_weather(北京, 2026-03-30)步骤 4外部程序执行函数获取返回结果步骤 5大模型结合结果生成自然语言回答2、细节点学习1、qwen-agent(Function-call)工具类定义步骤1、使用 register_tool(工具名) 注册工具2、定义类继承BaseTool自定义工具类必须继承这个基类基类提供了call方法规范、参数解析、异常捕获能力3、description定义工具的描述这是给大模型看的说明书模型根据描述判断什么时候调用这个工具4、def call 是模型真正调用的函数工具的执行入口params模型传过来的参数(字典或json字符串)返回值必须是str示例代码:register_tool(exc_sql) class ExcSQLTool(BaseTool): SQL查询工具执行传入的SQL语句并返回结果。 description 对于生成的SQL进行SQL查询 parameters [{ name: sql_input, type: string, description: 生成的SQL语句, required: True }] def call(self, params: str, **kwargs) - str: import json args json.loads(params) sql_input args[sql_input] database args.get(database, ubr) # 创建数据库连接 engine create_engine( fmysqlmysqlconnector://xxxxxxxxxxxxxxxxxxxxxxxxxxx/{database}?charsetutf8mb4, connect_args{connect_timeout: 10}, pool_size10, max_overflow20 ) try: df pd.read_sql(sql_input, engine) # 返回前10行防止数据过多 return df.head(10).to_markdown(indexFalse) except Exception as e: return fSQL执行出错: {str(e)}2、使用function call 构建agent demo需要安装依赖pip install qwen-agentfrom asyncio import streams import os from urllib import response import dashscope import json from qwen_agent.agents import Assistant from qwen_agent.tools.base import BaseTool, register_tool # 配置密钥 dashscope.api_key os.getenv(DASHSCOPE_API_KEY, ) # # Step1: 注册工具名 # register_tool(calculator) # # Step2: 继承 BaseTool # class CalculatorTool(BaseTool): # # Step3: 描述 参数定义 # name calculator description 用于进行加减乘除数学计算 parameters [ { name: a, type: number, description: 数字a, required: True }, { name: b, type: number, description: 数字b, required: True }, { name: op, type: string, description: 运算符 - * /, required: True } ] # # Step4: call 执行入口 # def call(self, params: dict, **kwargs) - str: # 关键修复如果 params 是字符串先解析为字典 if isinstance(params, str): params json.loads(params) a params[a] b params[b] op params[op] try: if op : res a b elif op -: res a - b elif op *: res a * b elif op /: if b 0: return 错误除数不能为0 res a / b else: return 不支持的运算符 return f计算结果{a} {op} {b} {res} except Exception as e: return f计算错误{str(e)} # # 初始化 Agent # def init_agent(): llm_cfg { model: qwen-max, stream: False } bot Assistant( llmllm_cfg, system_message你是一个擅长计算的助手遇到数学问题请调用 calculator 工具求解, function_list[calculator], # 必须和注册名一致 ) return bot # # 测试对话 # if __name__ __main__: # 初始化助手 bot init_agent() messages [{role: user, content: 3 加 5 等于多少}] response list(bot.run(messages)) for res in response: # 判断是否为字典类型 if isinstance(res, dict): if res.get(role) assistant and res.get(content): print(\nBot 返回, res[content]) break # 找到第一个有效回答就停止 elif isinstance(res, list): # 如果是列表遍历其中的每个字典项 for item in res: if isinstance(item, dict) and item.get(role) assistant and item.get(content): print(\nBot 返回, item[content]) break