微信扫码
添加专属顾问
我要投稿
大模型如何突破自身限制,实现实时数据获取与复杂操作?揭秘Function Calling的神奇能力。 核心内容: 1. Function Calling的定义与核心原理 2. 大模型如何通过预设函数完成实时数据获取与复杂操作 3. 实际应用案例:自定义天气查询函数的实现过程
现如今我们已经习惯了大模型能回答问题、写代码、生成文案,但大模型本身其实“手无缚鸡之力”,它无法直接访问数据库、不能实时获取天气。
但现如今的大模型又是怎么做到这些的呢?答案就是——Function Calling(函数调用)。
Function Calling 是一种让大模型在理解用户自然语言后,主动调用外部工具或函数的能力。
大模型本身无法直接操作外部系统(如数据库、计算工具),但通过调用预设函数,可以完成:实时数据获取(天气、股价、新闻)、复杂计算(数学运算、代码执行)、操作外部系统(发送邮件、控制智能设备)
模型可将用户自然语言请求转化为结构化参数,传递给函数。例如:用户说“明天北京天气如何?” → 模型调用 get_weather(location="北京", date="2025-05-06")
模型可根据上下文决定是否/何时调用函数,甚至链式调用多个函数(如先查天气,再推荐穿搭)。
举个例子🌰
我将询问千问大模型北京的天气怎么样,并让千文使用我们自定义的函数查询天气信息并返回个用户天气信息。
首先我们定义一个工具函数,json格式的,是为了让大模型了解这个函数时干什么的,函数参数是什么。
import requestsfrom http import HTTPStatusimport dashscopeimport os# 设置 DashScope API Keydashscope.api_key = os.getenv("DASHSCOPE_API_KEY")# 高德天气 API 的 天气工具定义(JSON 格式)weather_tool = {"type": "function","function": {"name": "get_current_weather","description": "Get the current weather in a given location","parameters": {"type": "object","properties": {"location": {"type": "string","description": "The city name, e.g. 北京",},"adcode": {"type": "string","description": "The city code, e.g. 110000 (北京)",}},"required": ["location"],},},}
接着编写天气条用的函数
def get_current_weather(location: str, adcode: str = None):"""调用高德地图API查询天气"""gaode_api_key = os.getenv("GAOGE_API_KEY") # 替换成你的高德API Keybase_url = "https://restapi.amap.com/v3/weather/weatherInfo"params = {"key": gaode_api_key,"city": adcode if adcode else location,"extensions": "base", # 可改为 "all" 获取预报}response = requests.get(base_url, params=params)if response.status_code == 200:return response.json()else:return {"error": f"Failed to fetch weather: {response.status_code}"}
通过dashscope调用大模型回答问题,大模型会根据之前的函数定义信息构造出函数需要的参数值和参数格式,再调用函数。
"""使用 Qwen3 + 查询天气"""messages = [{"role": "system", "content": "你是一个智能助手,可以查询天气信息。"},{"role": "user", "content": "北京现在天气怎么样?"}]response = dashscope.Generation.call(model="qwen-turbo-latest", # 可使用 Qwen3 最新版本messages=messages,tools=[weather_tool], # 传入工具定义tool_choice="auto", # 让模型决定是否调用工具)if response.status_code == HTTPStatus.OK:# 检查是否需要调用工具if "tool_calls" in response.output.choices[0].message:print('response=', response.output.choices[0])tool_call = response.output.choices[0].message.tool_calls[0]print('tool_call=', tool_call)if tool_call["function"]["name"] == "get_current_weather":# 解析参数并调用高德APIimport jsonargs = json.loads(tool_call["function"]["arguments"])location = args.get("location", "北京")adcode = args.get("adcode", None)weather_data = get_current_weather(location, adcode)print(f"查询结果:{weather_data}")else:print(response.output.choices[0].message.content)else:print(f"请求失败: {response.code} - {response.message}")
summary
怎么样,使用funcation calling是不是很简单,我们只需要将函数的具体功能实现并告诉大模型,这样大模型就会自动调用函数获取关键信息。
53AI,企业落地大模型首选服务商
产品:场景落地咨询+大模型应用平台+行业解决方案
承诺:免费POC验证,效果达标后再合作。零风险落地应用大模型,已交付160+中大型企业
2025-10-29
沃尔沃RAG实战:企业级知识库,早就该放弃小分块策略
2025-10-29
Claude发布新功能Agent Skills,让你的Agent更专业
2025-10-29
星环科技AI Infra平台:重构企业AI基础设施
2025-10-29
SOFA AI 网关基于 Higress 的落地实践
2025-10-29
Claude Skills 可能比 MCP更重要!
2025-10-29
初创公司的增长之道:如何让AI主动推荐你的产品?(上)
2025-10-29
VS Code Copilot Plan Agent:让 AI 编程更系统化的任务规划工具
2025-10-29
大模型音频水印技术:用AI守护音频数据的“身份指纹”
2025-08-21
2025-08-21
2025-08-19
2025-09-16
2025-09-08
2025-10-02
2025-09-17
2025-08-19
2025-09-29
2025-08-20
2025-10-29
2025-10-29
2025-10-28
2025-10-28
2025-10-27
2025-10-26
2025-10-25
2025-10-23