forked from futz12/SDU_DeepSeek
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_chat.py
More file actions
461 lines (402 loc) · 15 KB
/
cli_chat.py
File metadata and controls
461 lines (402 loc) · 15 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import subprocess
import sys
import time
import signal
import json
import uuid
import requests
import sduwrap
import sdu_aiassist_login as login
import getpass
import warnings
try:
import readline
except ImportError:
readline = None
warnings.filterwarnings("ignore")
HISTORY_FILE = ".cli_history"
API_BASE = "http://127.0.0.1:8000"
def load_or_login():
try:
with open("./cookies.json", "r") as f:
cookies = json.load(f)
if not cookies:
raise FileNotFoundError
sduwrap.cookies = cookies
print("已加载登录状态")
except FileNotFoundError:
print("未找到cookies.json,正在登录...")
sdu_id = input("请输入您的SDU学号: ")
password = getpass.getpass("请输入您的密码: ")
try:
with open("./fingerprint.txt", "r") as f:
fingerprint = f.read().strip()
except FileNotFoundError:
fingerprint = input("请输入设备指纹(留空自动生成): ")
if not fingerprint:
fingerprint = str(uuid.uuid4())
with open("./fingerprint.txt", "w") as f:
f.write(fingerprint)
fingerprint = str(uuid.uuid5(uuid.NAMESPACE_URL, fingerprint))
login_result = login.login(sdu_id, password, fingerprint)
cookies = login_result["cookies"]
if not cookies:
raise Exception("登录失败")
sduwrap.cookies = cookies
with open("./cookies.json", "w") as f:
json.dump(cookies, f)
print("登录成功,已保存cookies")
def start_server():
print("[Server] 正在启动本地 API 服务...")
log_file = open("server.log", "w")
proc = subprocess.Popen(
[sys.executable, "-m", "uvicorn", "main:app", "--host", "127.0.0.1", "--port", "8000", "--no-access-log"],
stdout=subprocess.DEVNULL,
stderr=log_file,
)
for _ in range(30):
try:
r = requests.get(f"{API_BASE}/v1/models", timeout=1)
if r.status_code == 200:
print("[Server] 服务已就绪 (日志: server.log)")
return proc
except Exception:
pass
time.sleep(0.5)
print("[Server] 启动超时,请检查 server.log 查看错误日志")
proc.terminate()
sys.exit(1)
def setup_readline():
if readline is None:
return
try:
readline.read_history_file(HISTORY_FILE)
readline.set_history_length(1000)
except FileNotFoundError:
pass
def save_readline_history():
if readline is None:
return
try:
readline.write_history_file(HISTORY_FILE)
except Exception:
pass
def select_model(model_ids):
print("可用模型:")
for i, m in enumerate(model_ids, 1):
print(f" {i}. {m}")
while True:
choice = input("请选择模型编号: ").strip()
if not choice:
print("无效选择,请重新输入")
continue
try:
idx = int(choice) - 1
if 0 <= idx < len(model_ids):
return model_ids[idx]
except ValueError:
pass
print("无效选择,请重新输入")
def print_stream(resp):
full_content = ""
full_reasoning = ""
got_data = False
for line in resp.iter_lines():
if line:
text = line.decode("utf-8") if isinstance(line, bytes) else line
if text.startswith("data: "):
data = text[6:]
if data == "[DONE]":
break
try:
chunk = json.loads(data)
got_data = True
if chunk.get("error"):
print(f"\n[API错误]: {chunk['error']}", end="", flush=True)
continue
delta = chunk["choices"][0].get("delta", {})
if delta.get("reasoning_content"):
print(f"\n[思考]: {delta['reasoning_content']}", end="", flush=True)
full_reasoning += delta["reasoning_content"]
if delta.get("content"):
print(delta["content"], end="", flush=True)
full_content += delta["content"]
except (json.JSONDecodeError, KeyError) as e:
print(f"\n[解析错误]: {e} | 原始数据: {data[:200]}", end="", flush=True)
if not got_data:
print("\n[警告] 服务端未返回任何数据", end="", flush=True)
elif not full_content and not full_reasoning:
print("\n[警告] 服务端返回了空内容", end="", flush=True)
print()
return full_content, full_reasoning
# ========== 客户端工具定义 ==========
def execute_tool(name: str, arguments: dict) -> str:
if name == "get_random_number":
import random
min_val = arguments.get("min", 1)
max_val = arguments.get("max", 100)
return str(random.randint(min_val, max_val))
elif name == "get_system_time":
from datetime import datetime
return datetime.now().strftime("%Y年%m月%d日 %H时%M分%S秒 %A")
elif name == "calculator":
import ast
import operator
expression = arguments.get("expression", "")
allowed_ops = {
ast.Add: operator.add,
ast.Sub: operator.sub,
ast.Mult: operator.mul,
ast.Div: operator.truediv,
ast.USub: operator.neg,
ast.Pow: operator.pow,
}
def eval_node(node):
if isinstance(node, ast.Constant):
return node.value
elif isinstance(node, ast.BinOp):
op = allowed_ops.get(type(node.op))
if op is None:
raise ValueError(f"不支持的运算符: {type(node.op)}")
return op(eval_node(node.left), eval_node(node.right))
elif isinstance(node, ast.UnaryOp):
op = allowed_ops.get(type(node.op))
if op is None:
raise ValueError(f"不支持的一元运算符: {type(node.op)}")
return op(eval_node(node.operand))
elif isinstance(node, ast.Expression):
return eval_node(node.body)
else:
raise ValueError(f"不支持的表达式类型: {type(node)}")
try:
tree = ast.parse(expression, mode="eval")
result = eval_node(tree)
return str(result)
except Exception as e:
return f"计算错误: {str(e)}"
else:
return f"错误: 未知工具 '{name}'"
TOOLS = [
{
"type": "function",
"function": {
"name": "get_random_number",
"description": "获取一个指定范围内的随机整数",
"parameters": {
"type": "object",
"properties": {
"min": {"type": "integer", "description": "最小值,默认为 1"},
"max": {"type": "integer", "description": "最大值,默认为 100"},
},
"required": [],
},
},
},
{
"type": "function",
"function": {
"name": "get_system_time",
"description": "获取当前系统时间和日期信息",
"parameters": {"type": "object", "properties": {}, "required": []},
},
},
{
"type": "function",
"function": {
"name": "calculator",
"description": "计算器,执行简单的数学表达式(支持 + - * / 和括号)",
"parameters": {
"type": "object",
"properties": {
"expression": {"type": "string", "description": "数学表达式,例如 1+2*3"}
},
"required": ["expression"],
},
},
},
]
def parse_tool_calls_from_content(content: str) -> List[dict]:
"""客户端 fallback 解析 content 中的 <tool_call> 标签"""
import re
pattern = r"<tool_call>\s*(.*?)\s*</tool_call>"
matches = re.findall(pattern, content, re.DOTALL)
tool_calls = []
for match in matches:
data = None
try:
data = json.loads(match)
except json.JSONDecodeError:
try:
import ast
data = ast.literal_eval(match.strip())
except Exception:
pass
if isinstance(data, dict) and "name" in data:
tool_calls.append({
"id": f"call_{uuid.uuid4().hex[:24]}",
"type": "function",
"function": {
"name": data["name"],
"arguments": json.dumps(data.get("arguments", {}), ensure_ascii=False)
}
})
return tool_calls
def _stream_chat(model_name: str, history: list) -> str:
"""发送流式请求并逐字打印"""
payload = {
"model": model_name,
"messages": history,
"stream": True,
}
resp = requests.post(
f"{API_BASE}/v1/chat/completions",
json=payload,
stream=True,
timeout=300,
)
resp.raise_for_status()
full_content, _ = print_stream(resp)
return full_content
def chat_with_tools(model_name: str, history: list, tools: list) -> str:
"""客户端 ReAct 循环:发送请求 → 执行工具 → 获取最终答案
策略:
- 第一轮必须非流式,以便可靠解析 tool_calls
- 若不需要工具,重发流式请求给用户看打字机效果
- 执行工具后,第二轮直接走流式输出最终答案
"""
max_rounds = 5
for round_idx in range(max_rounds):
# 如果已经执行过工具(round_idx > 0),直接走流式输出最终答案
if round_idx > 0:
return _stream_chat(model_name, history)
# 第一轮:非流式判断是否需要工具
payload = {
"model": model_name,
"messages": history,
"tools": tools,
"stream": False,
}
resp = requests.post(
f"{API_BASE}/v1/chat/completions",
json=payload,
timeout=300,
)
resp.raise_for_status()
data = resp.json()
choice = data["choices"][0]
message = choice["message"]
tool_calls = message.get("tool_calls", [])
# Fallback:如果服务端没解析到 tool_calls,但 content 里有 <tool_call>,客户端自己解析
if not tool_calls and message.get("content"):
tool_calls = parse_tool_calls_from_content(message["content"])
if choice["finish_reason"] == "tool_calls" or tool_calls:
if not tool_calls:
# 服务端标记了 tool_calls 但解析不到,直接输出 content
content = message.get("content", "")
reasoning = message.get("reasoning_content", "")
if reasoning:
print(f"\n[思考]: {reasoning}")
print(content)
return content
# 添加 assistant 的 tool_calls 到历史
history.append({
"role": "assistant",
"content": message.get("content", ""),
"tool_calls": tool_calls,
})
# 执行工具
for tc in tool_calls:
fn = tc["function"]
name = fn["name"]
arguments = json.loads(fn["arguments"])
result = execute_tool(name, arguments)
print(f"[调用工具] {name}({json.dumps(arguments, ensure_ascii=False)}) → {result}")
history.append({
"role": "tool",
"tool_call_id": tc["id"],
"content": f"[{name}] 执行结果:{result}",
})
# 继续下一轮(会走 _stream_chat 流式输出最终答案)
else:
# 不需要工具,这是普通对话。重发流式请求给用户看打字机效果
return _stream_chat(model_name, history)
return "[错误] 工具调用轮数超过上限"
def main():
load_or_login()
server_proc = start_server()
def cleanup(signum=None, frame=None):
print("\n[Server] 正在关闭...")
server_proc.terminate()
try:
server_proc.wait(timeout=5)
except Exception:
server_proc.kill()
save_readline_history()
sys.exit(0)
signal.signal(signal.SIGINT, cleanup)
signal.signal(signal.SIGTERM, cleanup)
setup_readline()
models_resp = requests.get(f"{API_BASE}/v1/models").json()
model_ids = [m["id"] for m in models_resp.get("data", [])]
if not model_ids:
print("[Error] 无法获取模型列表")
cleanup()
model_name = select_model(model_ids)
use_tools = True
history = []
print("\n=== 山东大学AI助手CLI对话工具 ===")
print("输入 'quit' 或 'exit' 退出")
print("输入 'clear' 清空对话历史")
print("输入 'model' 切换模型")
print("输入 'tools' 开启/关闭工具调用")
print("支持方向键移动光标、上下键查看历史输入、退格删除中文\n")
print(f"当前模型: {model_name}")
print(f"工具调用: {'开启' if use_tools else '关闭'}\n")
while True:
try:
user_input = input("\n你: ").strip()
except (KeyboardInterrupt, EOFError):
cleanup()
if user_input.lower() in ["quit", "exit"]:
cleanup()
if user_input.lower() == "clear":
history = []
print("对话历史已清空")
continue
if user_input.lower() == "model":
model_name = select_model(model_ids)
print(f"已切换模型: {model_name}")
continue
if user_input.lower() == "tools":
use_tools = not use_tools
print(f"工具调用: {'开启' if use_tools else '关闭'}")
continue
if not user_input:
continue
history.append({"role": "user", "content": user_input})
try:
if use_tools:
print(f"{model_name}: ", end="", flush=True)
content = chat_with_tools(model_name, history, TOOLS)
history.append({"role": "assistant", "content": content})
else:
payload = {
"model": model_name,
"messages": history,
"stream": True,
}
print(f"{model_name}: ", end="", flush=True)
resp = requests.post(
f"{API_BASE}/v1/chat/completions",
json=payload,
stream=True,
timeout=300,
)
resp.raise_for_status()
full_content, _ = print_stream(resp)
history.append({"role": "assistant", "content": full_content})
except Exception as e:
print(f"\n错误: {e}")
cleanup()
if __name__ == "__main__":
main()