【AI+运营商生态新突破】:Open-AutoGLM如何重构话费充值流程?
2025/12/21 9:12:52
Function Call是大模型与真实世界交互的“桥梁”,从语言理解 => 具体行动
Function Calling与MCP的区别?
Function Calling的优势:
MCP 可能成为主流,但 Function Calling 作为底层能力仍将存在
importrequestsfromhttpimportHTTPStatusimportdashscope# 设置 DashScope API Keydashscope.api_key="sk-**********"# 高德天气 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"],},},}defget_weather_from_gaode(location:str,adcode:str=None):"""调用高德地图API查询天气"""gaode_api_key="你的API KEY"# 替换成你的高德API Keybase_url="https://restapi.amap.com/v3/weather/weatherInfo"params={"key":gaode_api_key,"city":adcodeifadcodeelselocation,"extensions":"base",# 可改为 "all" 获取预报}response=requests.get(base_url,params=params)ifresponse.status_code==200:returnresponse.json()else:return{"error":f"Failed to fetch weather:{response.status_code}"}defrun_weather_query():"""使用 Qwen3 + 查询天气"""messages=[{"role":"system","content":"你是一个智能助手,可以查询天气信息。"},{"role":"user","content":"北京现在天气怎么样?"}]response=dashscope.Generation.call(model="qwen-turbo",# 可使用 Qwen3 最新版本messages=messages,tools=[weather_tool],# 传入工具定义tool_choice="auto",# 让模型决定是否调用工具)ifresponse.status_code==HTTPStatus.OK:# 检查是否需要调用工具if"tool_calls"inresponse.output.choices[0].message:tool_call=response.output.choices[0].message.tool_calls[0]iftool_call["function"]["name"]=="get_current_weather":# 解析参数并调用高德APIimportjson args=json.loads(tool_call["function"]["arguments"])location=args.get("location","北京")adcode=args.get("adcode",None)weather_data=get_weather_from_gaode(location,adcode)print(f"查询结果:{weather_data}")else:print(response.output.choices[0].message.content)else:print(f"请求失败:{response.code}-{response.message}")if__name__=="__main__":run_weather_query()