1、工具调用
调用完结果看里面tool_calls 是否为空,不为空就调用工具函数处理,
如果为空就中断循环。大模型返回的message结果智能判断是否继续调用
输入输出如下:
请输入:深圳西安天气
------------------------------------------------------------第1轮大模型输出信息:{'choices': [{'message': {'content': '', 'tool_calls': [{'id': 'call_edbdb1743a724c9c9662a5', 'type': 'function', 'function':
{'name': 'get_current_weather', 'arguments': '{"location": "深圳"}'}}]}}]}
需要调用工具,工具调用信息:[{'id': 'call_edbdb1743a724c9c9662a5', 'type': 'function', 'function': {'name': 'get_current_weather', 'arguments': '{"location": "深圳"}'}}]
工具输出信息:深圳今天是雨天。------------------------------------------------------------
深圳今天的天气是雨天。第2轮大模型输出信息:{'content': '深圳今天的天气是雨天。', 'tool_calls': [{'id': 'call_79341955209940628a5a98', 'type': 'function', 'function': {'name': 'get_current_weather', 'arguments': '{"location": "西安"}'}}]}工具输出信息:西安今天是雨天。------------------------------------------------------------
西安今天的天气也是雨天。请注意携带雨具。第3轮大模型输出信息:{'content': '西安今天的天气也是雨天。请注意携带雨具。', 'tool_calls': None}最终答案:西安今天的天气也是雨天。请注意携带雨具。
PS D:\swarm> & d:/swarm/.conda/python.exe d:/swarm/test_auzrapi/test_qwen.py请输入:深圳西安天气
------------------------------------------------------------第1轮大模型输出信息:{'choices': [{'message': {'content': '', 'tool_calls': [{'id': 'call_71fa625dc1a543319dfe9d', 'type': 'function', 'function':
{'name': 'get_current_weather', 'arguments': '{"location": "深圳"}'}}]}}]}location": "深圳"}'}}]
工具输出信息:深圳今天是雨天。------------------------------------------------------------
第2轮大模型输出信息:{'content': '', 'tool_calls': [{'id': 'call_188d490f167e4b109c681f', 'type': 'function', 'function': {'name': 'get_current_weather', 'arguments': '{"location": "西安"}'}}]}工具输出信息:西安今天是雨天。------------------------------------------------------------
深圳今天的天气是雨天,西安今天的天气也是雨天。请记得带伞哦!第3轮大模型输出信息:{'content': '深圳今天的天气是雨天,西安今天的天气也是雨天。请记得
带伞哦!', 'tool_calls': None}最终答案:深圳今天的天气是雨天,西安今天的天气也是雨天。请记得带伞哦!
import os
from openai import OpenAIfrom openai import OpenAI
from datetime import datetime
import json
import osclient = OpenAI(# 若没有配置环境变量,请用百炼API Key将下行替换为:api_key="sk-xxx",# api_key=os.getenv("DASHSCOPE_API_KEY"),api_key="sk-112222", # 修改成你的阿里的qwen的api keybase_url="https://dashscope.aliyuncs.com/compatible-mode/v1", # 填写DashScope SDK的base_url
)# 定义工具列表,模型在选择使用哪个工具时会参考工具的name和description
tools = [# 工具1 获取当前时刻的时间{"type": "function","function": {"name": "get_current_time","description": "当你想知道现在的时间时非常有用。",# 因为获取当前时间无需输入参数,因此parameters为空字典"parameters": {}}}, # 工具2 获取指定城市的天气{"type": "function","function": {"name": "get_current_weather","description": "当你想查询指定城市的天气时非常有用。","parameters": { "type": "object","properties": {# 查询天气时需要提供位置,因此参数设置为location"location": {"type": "string","description": "城市或县区,比如北京市、杭州市、余杭区等。"}}},"required": ["location"]}}
]# 模拟天气查询工具。返回结果示例:"北京今天是雨天。"
def get_current_weather(location):return f"{location}今天是雨天。 "# 查询当前时间的工具。返回结果示例:"当前时间:2024-04-15 17:15:18。"
def get_current_time():# 获取当前日期和时间current_datetime = datetime.now()# 格式化当前日期和时间formatted_time = current_datetime.strftime('%Y-%m-%d %H:%M:%S')# 返回格式化后的当前时间return f"当前时间:{formatted_time}。"# 封装模型响应函数
def get_response(messages):# 添加系统消息if not any(msg.get('role') == 'system' for msg in messages):messages.insert(0, {"role": "system","content": "你是一个有用的助手,可以帮助用户查询天气和时间。"})# 确保所有消息都有正确的 rolefor msg in messages:if 'role' not in msg or msg['role'] not in ['system', 'assistant', 'user', 'tool', 'function']:if 'name' in msg and msg['name'] in ['get_current_weather', 'get_current_time']:msg['role'] = 'function'else:msg['role'] = 'user'completion = client.chat.completions.create(model="qwen-plus",messages=messages,stream=True,tools=tools)# 处理流式响应content_buffer = []tool_call_buffer = Nonefor chunk in completion:try:# 处理内容if chunk.choices[0].delta.content:content_buffer.append(chunk.choices[0].delta.content)print(chunk.choices[0].delta.content, end='', flush=True)# 处理工具调用if hasattr(chunk.choices[0].delta, 'tool_calls') and chunk.choices[0].delta.tool_calls:tool_call = chunk.choices[0].delta.tool_calls[0]if not tool_call_buffer:tool_call_buffer = {'id': tool_call.id,'type': tool_call.type,'function': {'name': tool_call.function.name,'arguments': ''}}if hasattr(tool_call.function, 'arguments') and tool_call.function.arguments:tool_call_buffer['function']['arguments'] += tool_call.function.arguments# 检查参数是否完整try:if tool_call_buffer['function']['arguments']:json.loads(tool_call_buffer['function']['arguments'])return {'choices': [{'message': {'content': ''.join(content_buffer),'tool_calls': [tool_call_buffer]}}]}except json.JSONDecodeError:continueexcept Exception as e:print(f"Error processing chunk: {e}")continue# 如果没有工具调用,返回普通消息return {'choices': [{'message': {'content': ''.join(content_buffer),'tool_calls': None}}]}def call_with_messages():print('\n')messages = [{"role": "system","content": "你是一个有用的助手,可以帮助用户查询天气和时间。"},{"role": "user","content": input('请输入:'), # 提问示例:"现在几点了?" "一个小时后几点" "北京天气如何?"}]print("-"*60)# 模型的第一轮调用i = 1first_response = get_response(messages)assistant_output = first_response['choices'][0]['message']print(f"\n第{i}轮大模型输出信息:{first_response}\n")if assistant_output['content'] is None:assistant_output['content'] = ""messages.append({"role": "assistant","content": assistant_output['content'],"tool_calls": assistant_output.get('tool_calls')})# 如果不需要调用工具,则直接返回最终答案if not assistant_output['tool_calls']:print(f"无需调用工具,我可以直接回复:{assistant_output['content']}")returnelse:print(f"\n需要调用工具,工具调用信息:{assistant_output['tool_calls']}")while assistant_output['tool_calls']:tool_call = assistant_output['tool_calls'][0]func_name = tool_call['function']['name']# 根据工具名称调用相应的函数if func_name == 'get_current_weather':tool_result = get_current_weather(json.loads(tool_call['function']['arguments'])['location'])messages.append({"role": "tool", # 使用 tool 而不是 function"tool_call_id": tool_call['id'], # 添加 tool_call_id"name": "get_current_weather","content": tool_result})elif func_name == 'get_current_time':tool_result = get_current_time()messages.append({"role": "tool", # 使用 tool 而不是 function"tool_call_id": tool_call['id'], # 添加 tool_call_id"name": "get_current_time","content": tool_result})print(f"工具输出信息:{tool_result}\n")print("-"*60)# 获取助手的响应response = get_response(messages)assistant_output = response['choices'][0]['message']if assistant_output['content'] is None:assistant_output['content'] = ""messages.append({"role": "assistant","content": assistant_output['content'],"tool_calls": assistant_output.get('tool_calls')})i += 1print(f"第{i}轮大模型输出信息:{assistant_output}\n")# 如果助手的回复中没有工具调用,就结束循环if not assistant_output.get('tool_calls'):breakprint(f"最终答案:{assistant_output['content']}")if __name__ == '__main__':call_with_messages()