mirror of
http://47.106.207.27:3000/Jeremy_liu/AI0814_jiaoan_public.git
synced 2026-09-27 08:24:14 +08:00
101 lines
3.1 KiB
Python
101 lines
3.1 KiB
Python
from openai import OpenAI
|
|||
|
|
import os
|
||
|
|
import json
|
||
|
|
from dotenv import load_dotenv
|
||
|
|
|
||
|
|
load_dotenv()
|
||
|
|
|
||
|
|
client = OpenAI(
|
||
|
|
api_key=os.environ.get("DEEPSEEK_API_KEY"),
|
||
|
|
base_url="https://api.deepseek.com",
|
||
|
|
)
|
||
|
|
|
||
|
|
# 1. 定义一个获取天气的工具
|
||
|
|
tools = [
|
||
|
|
{
|
||
|
|
"type": "function",
|
||
|
|
"function": {
|
||
|
|
"name": "get_weather",
|
||
|
|
"description": "获取指定城市的天气",
|
||
|
|
"parameters": {
|
||
|
|
"type": "object",
|
||
|
|
"properties": {
|
||
|
|
"location": {"type": "string", "description": "城市名,如杭州"},
|
||
|
|
},
|
||
|
|
"required": ["location"],
|
||
|
|
},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
]
|
||
|
|
|
||
|
|
# 工具实现
|
||
|
|
def get_weather(location: str) -> str:
|
||
|
|
return f"{location}的天气为 8℃"
|
||
|
|
|
||
|
|
tools_map = {'get_weather': get_weather}
|
||
|
|
|
||
|
|
# ====================== 【新增:工具执行函数】======================
|
||
|
|
def execute_tool_calls(tool_calls, tools_map: dict) -> list:
|
||
|
|
"""
|
||
|
|
执行模型返回的tool_calls,返回tool角色消息列表,用于追加到messages
|
||
|
|
:param tool_calls: response.choices[0].message.tool_calls
|
||
|
|
:param tools_map: 工具名字 -> 函数 的映射字典
|
||
|
|
:return: list[dict] tool消息数组
|
||
|
|
"""
|
||
|
|
tool_messages = []
|
||
|
|
for tool_call in tool_calls:
|
||
|
|
func_name = tool_call.function.name
|
||
|
|
print(f"调用工具:{func_name}")
|
||
|
|
# 解析参数
|
||
|
|
args = json.loads(tool_call.function.arguments)
|
||
|
|
print(f"参数:{args}")
|
||
|
|
# 获取工具函数并执行
|
||
|
|
func = tools_map[func_name]
|
||
|
|
result = func(**args)
|
||
|
|
# 组装tool消息
|
||
|
|
tool_msg = {
|
||
|
|
"role": "tool",
|
||
|
|
"tool_call_id": tool_call.id,
|
||
|
|
"content": result
|
||
|
|
}
|
||
|
|
tool_messages.append(tool_msg)
|
||
|
|
return tool_messages
|
||
|
|
# ==================================================================
|
||
|
|
|
||
|
|
# 2. 用户提问
|
||
|
|
messages = [{"role": "user", "content": "你好,杭州和深圳和北京的天气怎么样"}]
|
||
|
|
|
||
|
|
# 第一次调用模型:模型会返回 tool_calls
|
||
|
|
response = client.chat.completions.create(
|
||
|
|
model="deepseek-v4-pro",
|
||
|
|
messages=messages,
|
||
|
|
tools=tools,
|
||
|
|
)
|
||
|
|
|
||
|
|
# 获取模型的回复
|
||
|
|
assistant_msg = response.choices[0].message
|
||
|
|
print(assistant_msg)
|
||
|
|
print(assistant_msg.model_dump_json(indent=2))
|
||
|
|
print(assistant_msg.content)
|
||
|
|
print("模型第一次回复:", assistant_msg.content)
|
||
|
|
|
||
|
|
# 把assistant消息放进对话上下文
|
||
|
|
messages.append(assistant_msg)
|
||
|
|
|
||
|
|
# 如果存在工具调用,执行工具
|
||
|
|
if assistant_msg.tool_calls:
|
||
|
|
# 调用封装好的函数,拿到tool消息列表
|
||
|
|
tool_msg_list = execute_tool_calls(assistant_msg.tool_calls, tools_map)
|
||
|
|
# 批量追加tool结果到对话
|
||
|
|
messages.extend(tool_msg_list)
|
||
|
|
|
||
|
|
# 第二次请求模型,传入工具返回结果
|
||
|
|
response = client.chat.completions.create(
|
||
|
|
model="deepseek-v4-pro",
|
||
|
|
messages=messages,
|
||
|
|
tools=tools,
|
||
|
|
)
|
||
|
|
print("模型第二次回复:", response.choices[0].message.content)
|
||
|
|
|
||
|
|
# 思考题:如何让你的程序 实现自己判断是否还需要再次请求大模型的调用
|
||
|
|
# 参考思路:当大模型不再需要工具调用的时候就结束大模型请求
|