Repository navigation
Expand file tree
/
Copy pathtest_completions.py
More file actions
118 lines (100 loc) · 3.8 KB
/
Copy pathtest_completions.py
File metadata and controls
118 lines (100 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import requests
import json
import subprocess
# --- 配置与工具函数 ---
url = "http://localhost:8787/v1/chat/completions"
def execute_js_code(code):
"""本地执行 JS 的函数"""
try:
result = subprocess.run(["node", "-e", code], capture_output=True, text=True, timeout=5)
if result.returncode != 0:
return f"Execution Error: {result.stderr}"
return result.stdout.strip()
except Exception as e:
return f"System Error: {str(e)}"
messages = [
# {"role": "system", "content": "You are a helpful assistant. Use exec_js for calculations."},
{
"role": "user",
"content": [
{ "type": "text",
"text": "任务1. 请计算斐波那契数列的第 11 项是多少?\n任务2. 文件名称是什么?内容是什么?\n任务3. 图片中有什么?"
}, {
"type": "file",
"file": {
"file_data": "5oiR5pivZGVlcHNlZWs=",
"filename": "who am i.txt"
}
}, {
"type": "image_url",
"image_url": {
"url": "READMEsrc/deepseekWebCodex.png"
}
}
]
}
]
tools = [
{
"type": "function",
"function": {
"name": "exec_js",
"description": "Execute JavaScript code. Ensure the result is printed.",
"parameters": {
"type": "object",
"properties": {
"javascript_code": {"type": "string", "description": "The JS code to execute."}
},
"required": ["javascript_code"]
}
}
}
]
print(f"{'='*20} 开始对话 {'='*20}")
while True:
# 1. 发送请求
payload = {
"model": "deepseek",
"messages": messages,
"tools": tools,
"tool_choice": "auto"
}
response = requests.post(url, json=payload)
if response.status_code != 200:
print("❌ API 请求失败:", response.text)
break
body = response.json()
message = body["choices"][0]["message"]
print(f"\n🤖 模型回复:\n{message.get('content', '[No content]')}")
# 2. 把模型的回复(无论是文字还是工具调用)存入历史
messages.append(message)
# 3. 判断是否有工具调用
tool_calls = message.get("tool_calls", [])
if tool_calls:
print("\n🤖 模型正在调用工具...")
# 遍历所有工具调用(防止模型一次调多个)
for tool_call in tool_calls:
if tool_call['function']['name'] == 'exec_js':
# A. 提取代码
args = json.loads(tool_call['function']['arguments'])
code_to_run = args['javascript_code']
# B. 执行代码
execution_result = execute_js_code(code_to_run)
print(f" ✅ 结果: {execution_result}")
# C. 构造工具响应消息
tool_message = {
"role": "tool",
"tool_call_id": tool_call['id'], # 本库中ID为原始调用代码
"content": str(execution_result)
}
# D. 将结果加入历史,准备下一轮循环
messages.append(tool_message)
# 从message中删除tool_calls,因为content中有包括工具调用的所有的信息
del message['tool_calls']
# 继续下一次 while 循环(把结果发给模型)
continue
else:
# 4. 如果没有工具调用,说明任务完成,输出最终回答
final_content = message.get("content")
print(f"\n🏁 最终回答:\n{final_content}")
break