forked from LeoYoung-code/Aigc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
63 lines (51 loc) · 1.82 KB
/
common.py
File metadata and controls
63 lines (51 loc) · 1.82 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
from rich.console import Console
from rich.markdown import Markdown
_CONSOLE = Console()
def print_stream(stream):
resp_think = []
resp_conclusion = []
for chunk in stream:
if not chunk.choices:
continue
delta = chunk.choices[0].delta
if not delta:
continue
if reason := delta.reasoning_content:
print(reason, end="")
resp_think.append(reason)
if res := delta.content:
print(res, end="")
resp_conclusion.append(res)
if resp_think:
print_think_md(''.join(resp_think))
if resp_conclusion:
print_conclusion_md(''.join(resp_conclusion))
def print_think_md(res):
print("\n" * 3, end="")
title = "# 🤔思考内容输出: \n"
md = Markdown(title + res)
_CONSOLE.print(md)
def print_conclusion_md(res):
print("\n" * 3, end="")
title = "# 📒结论输出: "
md = Markdown(title + res)
_CONSOLE.print(md)
def get_input():
print("\n" * 3 + "请输入您的问题👩⚕️(空行结束):")
lines = []
while True:
line = input()
if not line: # 检测到空行时终止
break
lines.append(line)
print("\n" * 3 + "输入结束, ⌛️请等待回答...")
return '\n'.join(lines)
def handle(request):
try:
while True:
if callable(request):
request()
else:
print("Not a function!")
except KeyboardInterrupt:
print("\n检测到用户终止操作,Bye😊!")