5分钟打造智能天气助手GPT-3.5函数调用实战指南上周帮朋友调试一个天气查询功能时我惊讶地发现他还在用传统API对接方式——手动解析地址参数、处理错误响应、拼接返回结果。这让我想起三年前自己写的第一行天气查询代码当时花了两天时间才搞定基础功能。而现在借助GPT-3.5-turbo的函数调用特性同样的事情只需要5分钟就能完成。这个功能本质上让AI模型成为了你的API翻译官。你只需要用自然语言描述函数功能模型就能自动将用户提问转化为结构化API请求。想象一下当用户问上海明天会下雨吗系统会自动转换成类似get_weather(location上海, date2023-07-20)的调用这比传统开发流程节省了90%的代码量。1. 环境准备与基础配置在开始前确保你已准备好以下三样东西OpenAI API密钥从平台后台获取建议设置合理的用量限制天气API服务推荐OpenWeatherMap的免费套餐每天1000次调用开发环境本文示例使用Python 3.8但核心逻辑适配任何语言安装必要依赖pip install openai requests python-dotenv创建.env文件存储敏感信息OPENAI_API_KEY你的API密钥 WEATHER_API_KEY你的天气API密钥安全提示永远不要将API密钥直接写入代码。使用环境变量或专业密钥管理服务2. 定义天气查询函数函数调用的核心在于明确定义你的外部工具能力。以下是一个完整的天气函数定义示例import json import requests from typing import Literal def get_current_weather( location: str, unit: Literal[celsius, fahrenheit] celsius ) - str: 获取指定城市的当前天气情况 Args: location: 城市名称如北京或New York unit: 温度单位默认为摄氏度 Returns: JSON格式的天气数据字符串 base_url https://api.openweathermap.org/data/2.5/weather params { q: location, appid: os.getenv(WEATHER_API_KEY), units: metric if unit celsius else imperial } try: response requests.get(base_url, paramsparams, timeout5) data { temperature: response.json()[main][temp], unit: unit, description: response.json()[weather][0][description] } return json.dumps(data) except Exception as e: return json.dumps({error: str(e)})关键点说明类型提示使用Python的类型注解明确参数类型文档字符串详细描述函数用途这将成为AI理解功能的基础错误处理确保异常情况下仍返回合法JSON3. 构建AI交互管道现在我们需要让GPT理解这个函数能力。以下是配置函数描述的完整代码from openai import OpenAI client OpenAI() def ask_weather(query: str) - str: # 定义函数描述会被发送给AI理解 functions [ { name: get_current_weather, description: 获取指定城市的当前天气信息, parameters: { type: object, properties: { location: { type: string, description: 城市名称如北京或New York, }, unit: { type: string, enum: [celsius, fahrenheit], }, }, required: [location], }, } ] # 第一步发送用户查询给AI response client.chat.completions.create( modelgpt-3.5-turbo-0613, messages[{role: user, content: query}], functionsfunctions, function_callauto, ) # 第二步处理AI返回的函数调用请求 if response.choices[0].message.function_call: function_name response.choices[0].message.function_call.name if function_name get_current_weather: arguments json.loads(response.choices[0].message.function_call.arguments) weather_data get_current_weather(**arguments) # 第三步将API结果返回给AI生成最终回复 second_response client.chat.completions.create( modelgpt-3.5-turbo-0613, messages[ {role: user, content: query}, response.choices[0].message, { role: function, name: function_name, content: weather_data, }, ], ) return second_response.choices[0].message.content return response.choices[0].message.content4. 实战测试与优化技巧让我们测试几个典型场景案例1基础查询print(ask_weather(北京现在多少度)) # 输出北京当前气温为28摄氏度天气晴朗。案例2单位转换print(ask_weather(Whats the temperature in New York in Fahrenheit?)) # 输出The current temperature in New York is 75°F with clear skies.案例3模糊查询print(ask_weather(明天上海天气怎么样)) # 输出我目前只能查询实时天气信息。上海现在的天气是...通过测试我们发现几个常见问题及解决方案问题类型表现解决方案时间范围用户询问未来天气在函数描述中明确说明仅支持实时查询地点模糊我家附近等表述让AI先询问具体位置API限制免费套餐调用超限添加速率限制和缓存机制高级优化建议批量处理当检测到多个函数调用需求时可以并行执行API请求本地缓存对相同参数的查询结果缓存5-10分钟减少API调用备用数据源当主天气API不可用时自动切换备用服务# 缓存装饰器示例 from functools import lru_cache import time lru_cache(maxsize100) def cached_weather(location: str, unit: str): 带缓存的天气查询有效期10分钟 return get_current_weather(location, unit) # 在get_current_weather实现中添加时间戳验证5. 扩展应用场景这个模式可以轻松扩展到其他领域股票查询机器人functions [ { name: get_stock_price, description: 获取指定股票的实时价格, parameters: { type: object, properties: { symbol: {type: string}, currency: {type: string, enum: [USD, CNY]} } } } ]酒店预订系统functions [ { name: search_hotels, description: 搜索符合条件的酒店, parameters: { type: object, properties: { location: {type: string}, check_in: {type: string, format: date}, check_out: {type: string, format: date}, max_price: {type: number} } } } ]实际项目中我更喜欢将函数描述存储在JSON文件中方便团队协作和维护// functions.json { weather: { name: get_current_weather, description: ..., parameters: {...} }, stock: { name: get_stock_price, description: ..., parameters: {...} } }加载方式import json with open(functions.json) as f: function_registry json.load(f) def get_function_config(name): return function_registry.get(name)这种架构下新增功能只需更新JSON文件无需修改核心代码。上周刚用这种方式为客户的电商平台添加了物流查询功能开发时间从预估的3人日压缩到2小时。