-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathserver.py
131 lines (115 loc) · 4.92 KB
/
server.py
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
import chat
import re
from prompt import User, SCENARIOS, CHAT_SAMPLER, INSTRUCT_SAMPLER
from chat import GenerateMode, model
try:
with open("qq.txt", 'r') as file:
QQ = file.read()
except:
print("Please provide your QQ number in `qq.txt`")
QQ = ""
CHAT_HELP_COMMAND = "-c, -chat"
PRIVATE_HELP_COMMAND = ""
with open("./help.md", 'r') as file:
model_name = model.args.MODEL_NAME.split('/')[-1].replace('.pth', '')
HELP_MESSAGE = file.read()
HELP_MESSAGE = HELP_MESSAGE.replace('<model>', model_name)
HELP_MESSAGE = HELP_MESSAGE.replace('<scenarios>', str(SCENARIOS))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<chat_nucleus>', 'Yes' if CHAT_SAMPLER.sample.__name__ == "sample_nucleus" else '')
HELP_MESSAGE = HELP_MESSAGE.replace(
'<chat_typical>', 'Yes' if CHAT_SAMPLER.sample.__name__ == "sample_typical" else '')
HELP_MESSAGE = HELP_MESSAGE.replace(
'<chat_temp>', str(CHAT_SAMPLER.temp))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<chat_top_p>', str(CHAT_SAMPLER.top_p))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<chat_tau>', str(CHAT_SAMPLER.tau))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<chat_af>', str(CHAT_SAMPLER.count_penalty))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<chat_ap>', str(CHAT_SAMPLER.presence_penalty))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<chat_ar>', str(CHAT_SAMPLER.penalty_range))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<inst_nucleus>', 'Yes' if INSTRUCT_SAMPLER.sample.__name__ == "sample_nucleus" else '')
HELP_MESSAGE = HELP_MESSAGE.replace(
'<inst_typical>', 'Yes' if INSTRUCT_SAMPLER.sample.__name__ == "sample_typical" else '')
HELP_MESSAGE = HELP_MESSAGE.replace(
'<inst_temp>', str(INSTRUCT_SAMPLER.temp))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<inst_top_p>', str(INSTRUCT_SAMPLER.top_p))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<inst_tau>', str(INSTRUCT_SAMPLER.tau))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<inst_af>', str(INSTRUCT_SAMPLER.count_penalty))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<inst_ap>', str(INSTRUCT_SAMPLER.presence_penalty))
HELP_MESSAGE = HELP_MESSAGE.replace(
'<inst_ar>', str(INSTRUCT_SAMPLER.penalty_range))
def commands(user: User, message, enable_chat=False, is_private=False):
help_match = re.match("\-h(elp)?", message)
params_match = re.match("\-p(arams)?", message)
prompts_match = re.match("\-pr(ompts)?", message)
translate_match = re.match("\-tr", message)
retry_match = re.match("\-(retry|e)", message)
more_match = re.match("\-m(ore)?", message)
gen_match = re.match("\-g(en)?\s+", message)
inst_match = re.match("\-i(nst)?\s+", message)
reset_match = re.match("\-(reset|s)\s*", message)
list_match = re.match("\-l(ist)?", message)
alt_match = re.match("\-a(lt)?", message)
chat_match = re.match("\-c(hat)?\s+", message)
at_match = re.match(f"\[CQ:at,qq={QQ}\]", message)
help = HELP_MESSAGE
if is_private:
help = help.replace('<chat>', PRIVATE_HELP_COMMAND)
else:
help = help.replace('<chat>', CHAT_HELP_COMMAND)
prompt = message
reply = ""
matched = True
if help_match:
reply = help
elif prompts_match:
prompt = message[prompts_match.end():]
reply = chat.on_show_params(user, prompt, prompts=True)
elif params_match:
prompt = message[params_match.end():]
reply = chat.on_show_params(user, prompt)
elif translate_match:
prompt = message[translate_match.end():]
reply = chat.on_translate(user, prompt)
elif retry_match:
prompt = message[retry_match.end():]
reply = chat.on_generate(user, prompt, mode=GenerateMode.RETRY)
elif more_match:
prompt = message[more_match.end():]
reply = chat.on_generate(user, prompt, mode=GenerateMode.MORE)
elif gen_match:
prompt = message[gen_match.end():]
reply = chat.on_generate(user, prompt, mode=GenerateMode.GENERATE)
elif enable_chat and inst_match:
prompt = message[inst_match.end():]
reply = chat.on_generate(user, prompt, mode=GenerateMode.INSTRUCT)
elif enable_chat and reset_match:
prompt = message[reset_match.end():]
reply = chat.on_reset(user, prompt)
elif enable_chat and list_match:
reply = str(SCENARIOS)
elif enable_chat and alt_match:
prompt = message[alt_match.end():]
reply = chat.on_message(user, prompt, alt=True)
elif enable_chat and is_private:
reply = chat.on_message(user, prompt)
elif enable_chat and not is_private and chat_match:
prompt = message[chat_match.end():]
reply = chat.on_message(user, prompt)
elif QQ and enable_chat and not is_private and at_match:
prompt = message[at_match.end():]
reply = chat.on_message(user, prompt)
else:
matched = False
return matched, prompt, reply
def init():
chat.init_run()