JavaWeb在线旅游网站课程设计:完整项目导入与核心功能实现
2026/10/8 13:54:25
以OpenAI的Function Calling为例,调用流程如下:
import openai # 1. 定义可用的工具 tools = [{ "type": "function", "function": { "name": "get_weather", "description": "获取指定城市的天气信息", "parameters": { "type": "object", "properties": { "city": {"type": "string", "description": "城市名称"} }, "required": ["city"] } } }] # 2. 发送请求,模型判断是否需要调用工具 response = openai.chat.completions.create( model="gpt-4-turbo", messages=[{"role": "user", "content": "北京今天天气怎么样?"}], tools=tools ) # 3. 如果模型返回 tool_calls,解析并执行真实 API if response.choices[0].message.tool_calls: tool_call = response.choices[0].message.tool_calls[0] # 解析参数,调用真实的天气 API,拿到结果后继续对话