手把手教学:利用Qwen3-14B的Function Calling,打造自动查天气、订票的AI助手
手把手教学利用Qwen3-14B的Function Calling打造自动查天气、订票的AI助手1. 引言为什么选择Qwen3-14B想象一下你正在开发一个智能助手用户问北京明天天气怎么样它能自动调用天气API获取数据用户说帮我订一张下周去上海的机票它能理解需求并触发订票流程。这种能说会做的AI能力正是Qwen3-14B的强项。Qwen3-14B是通义千问系列中的140亿参数大模型在保持合理资源消耗的同时提供了出色的指令理解、逻辑推理和工具调用能力。相比纯文本生成模型它原生支持Function Calling功能可以理解用户意图并触发外部工具执行是实现智能代理(Agent)的理想选择。本文将带你从零开始使用Qwen3-14B构建一个能自动查天气、订票的AI助手。我们会涵盖快速部署Qwen3-14B设计Function Calling流程实现天气查询和订票功能优化交互体验的实用技巧2. 快速部署Qwen3-14B2.1 通过CSDN星图镜像部署最简单的方式是使用CSDN星图镜像广场提供的预置镜像登录CSDN星图平台搜索Qwen3-14B镜像点击一键部署按钮等待部署完成后即可通过Web界面或API访问模型2.2 本地部署方案如需本地部署可以使用Docker方式docker pull registry.cn-beijing.aliyuncs.com/qwen/qwen3-14b:latest docker run -p 8000:8000 -v /path/to/models:/models qwen3-14b这将启动一个兼容OpenAI API的服务监听8000端口。3. 理解Function Calling机制3.1 什么是Function CallingFunction Calling允许大模型在对话过程中识别需要调用外部工具的时机并生成结构化请求。例如用户问北京明天天气如何 模型可能返回{ tool_calls: [ { name: get_weather, arguments: {location: 北京, date: 2024-06-20} } ] }3.2 Qwen3-14B的Function Calling特点Qwen3-14B的Function Calling有几个优势识别准确能准确判断何时需要调用工具参数提取精准能从自然语言中提取结构化参数多工具支持可同时处理多个工具调用请求上下文感知能结合对话历史决定是否调用工具4. 构建天气查询功能4.1 定义天气查询工具首先我们需要定义一个天气查询工具的规范tools [ { type: function, function: { name: get_weather, description: 获取指定城市和日期的天气信息, parameters: { type: object, properties: { location: {type: string, description: 城市名称}, date: {type: string, description: 日期格式YYYY-MM-DD} }, required: [location] } } } ]4.2 实现天气API调用假设我们有一个天气API服务可以这样实现调用逻辑import requests def get_weather(location, dateNone): base_url https://api.weather.com/v3/wx/forecast params { location: location, format: json, apiKey: YOUR_API_KEY } if date: params[date] date response requests.get(base_url, paramsparams) return response.json()4.3 集成到对话流程将天气查询集成到对话中from openai import OpenAI client OpenAI(base_urlhttp://localhost:8000/v1, api_keynone) def chat_with_weather(query): response client.chat.completions.create( modelqwen3-14b, messages[{role: user, content: query}], toolstools, tool_choiceauto ) message response.choices[0].message if message.tool_calls: for tool_call in message.tool_calls: if tool_call.function.name get_weather: args json.loads(tool_call.function.arguments) weather_data get_weather(**args) return f{args[location]}的天气是{weather_data[condition]}, 温度{weather_data[temp]}℃ return message.content5. 实现订票功能5.1 定义订票工具订票功能需要更复杂的参数tools.append({ type: function, function: { name: book_ticket, description: 预订机票, parameters: { type: object, properties: { origin: {type: string, description: 出发城市}, destination: {type: string, description: 目的城市}, date: {type: string, description: 出发日期}, passenger: {type: string, description: 乘客姓名}, seat_class: {type: string, enum: [economy, business, first], description: 舱位等级} }, required: [origin, destination, date] } } })5.2 实现订票逻辑订票功能可能涉及支付等敏感操作需要更严谨def book_ticket(origin, destination, date, passengerNone, seat_classeconomy): # 验证参数 if not validate_cities(origin, destination): return {status: error, message: 无效的城市} # 查询航班 flights search_flights(origin, destination, date) if not flights: return {status: error, message: 没有可用航班} # 选择航班简化版 selected flights[0] # 创建订单 order create_order( flight_idselected[id], passengerpassenger or 默认乘客, seat_classseat_class ) return { status: success, order_id: order[id], flight: selected[number], departure: selected[departure_time], arrival: selected[arrival_time] }5.3 处理订票对话订票通常需要多轮对话确认def handle_booking_conversation(messages): # 初始查询 response client.chat.completions.create( modelqwen3-14b, messagesmessages, toolstools, tool_choiceauto ) message response.choices[0].message if message.tool_calls: for tool_call in message.tool_calls: if tool_call.function.name book_ticket: args json.loads(tool_call.function.arguments) # 检查必要参数 if not all(k in args for k in [origin, destination, date]): return 请提供出发地、目的地和日期 # 获取乘客信息可能需要追问 if passenger not in args: return 请问乘客姓名是什么 # 执行订票 result book_ticket(**args) if result[status] success: return f已为您预订{args[origin]}到{args[destination]}的航班{result[flight]} else: return result[message] return message.content6. 优化交互体验的技巧6.1 设计清晰的系统提示好的系统提示能显著提升Function Calling的准确性你是一个智能旅行助手可以帮助用户查询天气和预订机票。当用户询问天气或订票时请调用相应的工具函数。如果信息不完整请礼貌地询问缺失的细节。回答应简洁专业。6.2 处理模糊请求当用户说我想去上海模型需要追问# 在handle_booking_conversation中添加 if destination in args and origin not in args: return 请问您要从哪个城市出发 if date not in args: return 请问您计划哪天出发6.3 多工具调用处理用户可能同时询问天气和订票# 修改对话处理逻辑 for tool_call in message.tool_calls: if tool_call.function.name get_weather: # 处理天气查询 elif tool_call.function.name book_ticket: # 处理订票 # 最后合并所有工具调用的结果6.4 错误处理和重试为工具调用添加重试机制max_retries 3 retry_delay 1 for attempt in range(max_retries): try: result call_external_api(params) break except Exception as e: if attempt max_retries - 1: return 服务暂时不可用请稍后再试 time.sleep(retry_delay)7. 总结与扩展建议通过本教程我们实现了一个能自动查天气和订票的AI助手核心功能。Qwen3-14B的Function Calling能力让模型不仅能理解用户意图还能触发实际业务操作大大提升了AI应用的实用性。7.1 关键要点回顾Qwen3-14B原生支持Function Calling适合构建能执行实际任务的AI助手定义清晰的工具规范有助于模型准确识别调用时机和参数多轮对话处理和信息确认能提升复杂任务的成功率良好的错误处理机制保证用户体验的流畅性7.2 进一步扩展方向集成更多服务酒店预订、景点推荐、交通查询等用户偏好记忆记住用户常去的城市、喜欢的舱位等多模态交互结合图片识别确认机票信息离线优化使用量化技术提升本地部署效率7.3 生产环境建议性能监控记录工具调用耗时和成功率限流保护防止API被过度调用敏感操作确认如支付前要求二次确认日志审计保留完整的对话和操作记录现在你已经掌握了使用Qwen3-14B构建实用AI助手的关键技能。试着扩展更多功能打造属于你的智能代理吧获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。