Spaces:
Sleeping
Sleeping
lewiswu1209
commited on
Commit
·
c1e6869
1
Parent(s):
f3c6b77
add qa skills
Browse files- app.py +3 -1
- bot/skills/couplet.py +3 -0
- bot/skills/delete_memory.py +3 -0
- bot/skills/give_role.py +3 -0
- bot/skills/poem.py +3 -0
- bot/skills/qa.py +46 -0
- templates/chat_template.html +47 -15
- web.py +3 -2
app.py
CHANGED
@@ -39,6 +39,7 @@ def general(input_txt:str, state:dict = {}):
|
|
39 |
plugin = __import__("bot.skills."+skill_name, fromlist=[skill_name])
|
40 |
plugin_class = getattr(plugin, "Skill")
|
41 |
p = plugin_class()
|
|
|
42 |
output_txt, history_list, role_card = p.process(input_txt, history_list, role_card)
|
43 |
|
44 |
if output_txt is None:
|
@@ -64,7 +65,8 @@ def main() -> None:
|
|
64 |
description += "可以修改的角色信息有:<br>"
|
65 |
description += "<NAME>, <GENDER>, <YEAROFBIRTH>, <MONTHOFBIRTH>, <DAYOFBIRTH>, <ZODIAC>, <AGE><br>"
|
66 |
description += "输入“上联:XXXXXXX”,Winnie会和你对对联<br>"
|
67 |
-
description += "输入“写诗:XXXXXXX”,Winnie会以XXXXXXX
|
|
|
68 |
|
69 |
gr.Interface(
|
70 |
fn = general,
|
|
|
39 |
plugin = __import__("bot.skills."+skill_name, fromlist=[skill_name])
|
40 |
plugin_class = getattr(plugin, "Skill")
|
41 |
p = plugin_class()
|
42 |
+
p.set_tokenizer( bot.tokenizer )
|
43 |
output_txt, history_list, role_card = p.process(input_txt, history_list, role_card)
|
44 |
|
45 |
if output_txt is None:
|
|
|
65 |
description += "可以修改的角色信息有:<br>"
|
66 |
description += "<NAME>, <GENDER>, <YEAROFBIRTH>, <MONTHOFBIRTH>, <DAYOFBIRTH>, <ZODIAC>, <AGE><br>"
|
67 |
description += "输入“上联:XXXXXXX”,Winnie会和你对对联<br>"
|
68 |
+
description += "输入“写诗:XXXXXXX”,Winnie会以XXXXXXX为开头写诗<br>"
|
69 |
+
description += "以\"请问\"开头并以问号结尾,Winnie会回答该问题"
|
70 |
|
71 |
gr.Interface(
|
72 |
fn = general,
|
bot/skills/couplet.py
CHANGED
@@ -5,6 +5,9 @@ class Skill:
|
|
5 |
def __init__(self:object) -> None:
|
6 |
pass
|
7 |
|
|
|
|
|
|
|
8 |
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
9 |
output_text:str = None
|
10 |
if input_txt.startswith("上联:") or input_txt.startswith("上联:"):
|
|
|
5 |
def __init__(self:object) -> None:
|
6 |
pass
|
7 |
|
8 |
+
def set_tokenizer(self:object, tokenizer:object):
|
9 |
+
self.tokenizer = tokenizer
|
10 |
+
|
11 |
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
12 |
output_text:str = None
|
13 |
if input_txt.startswith("上联:") or input_txt.startswith("上联:"):
|
bot/skills/delete_memory.py
CHANGED
@@ -3,6 +3,9 @@ class Skill:
|
|
3 |
def __init__(self:object) -> None:
|
4 |
pass
|
5 |
|
|
|
|
|
|
|
6 |
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
7 |
output_txt:str = None
|
8 |
if input_txt.upper()=="ERASE MEMORY":
|
|
|
3 |
def __init__(self:object) -> None:
|
4 |
pass
|
5 |
|
6 |
+
def set_tokenizer(self:object, tokenizer:object):
|
7 |
+
self.tokenizer = tokenizer
|
8 |
+
|
9 |
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
10 |
output_txt:str = None
|
11 |
if input_txt.upper()=="ERASE MEMORY":
|
bot/skills/give_role.py
CHANGED
@@ -3,6 +3,9 @@ class Skill:
|
|
3 |
def __init__(self:object) -> None:
|
4 |
pass
|
5 |
|
|
|
|
|
|
|
6 |
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
7 |
output_txt:str = None
|
8 |
for tag in role_card.keys():
|
|
|
3 |
def __init__(self:object) -> None:
|
4 |
pass
|
5 |
|
6 |
+
def set_tokenizer(self:object, tokenizer:object):
|
7 |
+
self.tokenizer = tokenizer
|
8 |
+
|
9 |
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
10 |
output_txt:str = None
|
11 |
for tag in role_card.keys():
|
bot/skills/poem.py
CHANGED
@@ -5,6 +5,9 @@ class Skill:
|
|
5 |
def __init__(self:object) -> None:
|
6 |
pass
|
7 |
|
|
|
|
|
|
|
8 |
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
9 |
output_text:str = None
|
10 |
if input_txt.startswith("写诗:") or input_txt.startswith("写诗:"):
|
|
|
5 |
def __init__(self:object) -> None:
|
6 |
pass
|
7 |
|
8 |
+
def set_tokenizer(self:object, tokenizer:object):
|
9 |
+
self.tokenizer = tokenizer
|
10 |
+
|
11 |
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
12 |
output_text:str = None
|
13 |
if input_txt.startswith("写诗:") or input_txt.startswith("写诗:"):
|
bot/skills/qa.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import os
|
3 |
+
import re
|
4 |
+
import json
|
5 |
+
import requests
|
6 |
+
|
7 |
+
class Skill:
|
8 |
+
def __init__(self:object) -> None:
|
9 |
+
pass
|
10 |
+
|
11 |
+
def set_tokenizer(self:object, tokenizer:object):
|
12 |
+
self.tokenizer = tokenizer
|
13 |
+
|
14 |
+
def predict(self, question:str, seed=1234, out_seq_length=256, min_gen_length=1, sampling_strategy="BaseStrategy", num_beams=2, length_penalty=1, no_repeat_ngram_size=3, temperature=0.7, topk=8, topp=0.7):
|
15 |
+
url = 'https://pretrain.aminer.cn/api/v2/completions'
|
16 |
+
prompt = "{}\n答:[gMASK]".format(question)
|
17 |
+
payload = json.dumps({
|
18 |
+
"apikey": os.environ.get("WINNIE_APIKEY"),
|
19 |
+
"apisecret": os.environ.get("WINNIE_APISECRET"),
|
20 |
+
"model": "glm",
|
21 |
+
"language": "zh-CN",
|
22 |
+
"prompt": prompt,
|
23 |
+
"temperature": temperature,
|
24 |
+
"top_k": topk,
|
25 |
+
"top_p": topp,
|
26 |
+
"max_tokens": out_seq_length,
|
27 |
+
"stop": ["\n"],
|
28 |
+
"presence_penalty": 2,
|
29 |
+
"frequency_penalty": 2
|
30 |
+
})
|
31 |
+
|
32 |
+
headers = {
|
33 |
+
'Content-Type': 'application/json'
|
34 |
+
}
|
35 |
+
response = requests.request("POST", url, headers=headers, data=payload).json()
|
36 |
+
if "output" in response["result"]:
|
37 |
+
answer = response["result"]["output"]["raw"].split("<|startofpiece|>")[-1]
|
38 |
+
return answer
|
39 |
+
|
40 |
+
def process(self:object, input_txt:str, history_list:list, role_card:dict):
|
41 |
+
output_text:str = None
|
42 |
+
if re.match(r'^请问.+[??.。!!]$', input_txt):
|
43 |
+
history_list.append( self.tokenizer.encode(input_txt, add_special_tokens=False) )
|
44 |
+
output_text = self.predict(input_txt[2:])
|
45 |
+
history_list.append( self.tokenizer.encode(output_text, add_special_tokens=False) )
|
46 |
+
return output_text, history_list, role_card
|
templates/chat_template.html
CHANGED
@@ -200,22 +200,54 @@
|
|
200 |
chat_thread.scrollTop = chat_thread.scrollHeight;
|
201 |
chat_window_message.value = "";
|
202 |
document.title = "聊天机器人 ~ 对方正在输入…"
|
203 |
-
$.
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
var
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
|
|
|
|
|
|
215 |
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
219 |
}
|
220 |
|
221 |
function loadhistory() {
|
|
|
200 |
chat_thread.scrollTop = chat_thread.scrollHeight;
|
201 |
chat_window_message.value = "";
|
202 |
document.title = "聊天机器人 ~ 对方正在输入…"
|
203 |
+
$.ajax({
|
204 |
+
url: "/chitchat/chat?text="+text,
|
205 |
+
dataType: 'json',
|
206 |
+
success: function(data){
|
207 |
+
var new_li_label = document.createElement("li");
|
208 |
+
data.forEach(function(item){
|
209 |
+
var new_text = document.createTextNode(item);
|
210 |
+
var new_span = document.createElement("span");
|
211 |
+
new_span.appendChild(new_text);
|
212 |
+
var new_br = document.createElement("br");
|
213 |
+
new_li_label.appendChild(new_span);
|
214 |
+
new_li_label.appendChild(new_br);
|
215 |
+
});
|
216 |
+
chat_thread.appendChild(new_li_label);
|
217 |
+
chat_thread.scrollTop = chat_thread.scrollHeight;
|
218 |
|
219 |
+
document.title = "聊天机器人"
|
220 |
+
chat_window_message.disabled = false;
|
221 |
+
},
|
222 |
+
timeout: 60000,
|
223 |
+
error: function(jqXHR, status, errorThrown){
|
224 |
+
var new_li_label = document.createElement("li");
|
225 |
+
var new_text = document.createTextNode("网络超时1分钟,请重试");
|
226 |
+
|
227 |
+
new_li_label.appendChild(new_text);
|
228 |
+
chat_thread.appendChild(new_li_label);
|
229 |
+
chat_thread.scrollTop = chat_thread.scrollHeight;
|
230 |
+
|
231 |
+
document.title = "聊天机器人"
|
232 |
+
chat_window_message.disabled = false;
|
233 |
+
}
|
234 |
+
});
|
235 |
+
// $.getJSON("/chitchat/chat?text="+text, function(data){
|
236 |
+
// var new_li_label = document.createElement("li");
|
237 |
+
// data.forEach(function(item){
|
238 |
+
// var new_text = document.createTextNode(item);
|
239 |
+
// var new_span = document.createElement("span");
|
240 |
+
// new_span.appendChild(new_text);
|
241 |
+
// var new_br = document.createElement("br");
|
242 |
+
// new_li_label.appendChild(new_span);
|
243 |
+
// new_li_label.appendChild(new_br);
|
244 |
+
// });
|
245 |
+
// chat_thread.appendChild(new_li_label);
|
246 |
+
// chat_thread.scrollTop = chat_thread.scrollHeight;
|
247 |
+
|
248 |
+
// document.title = "聊天机器人"
|
249 |
+
// chat_window_message.disabled = false;
|
250 |
+
// });
|
251 |
}
|
252 |
|
253 |
function loadhistory() {
|
web.py
CHANGED
@@ -80,14 +80,15 @@ def talk() -> str:
|
|
80 |
history_list = session.get("history")
|
81 |
|
82 |
if input_text.upper()=="HELP":
|
83 |
-
help_info_list = ["输入任意文字,Winnie
|
84 |
"输入ERASE MEMORY,Winnie会清空记忆",
|
85 |
"输入\"<TAG>=<VALUE>\",Winnie会记录你的角色信息",
|
86 |
"例如:<NAME>=Vicky,Winnie会修改自己的名字",
|
87 |
"可以修改的角色信息有:",
|
88 |
"<NAME>, <GENDER>, <YEAROFBIRTH>, <MONTHOFBIRTH>, <DAYOFBIRTH>, <ZODIAC>, <AGE>",
|
89 |
"输入“上联:XXXXXXX”,Winnie会和你对对联",
|
90 |
-
"输入“写诗:XXXXXXX”,Winnie会以XXXXXXX为开头写诗"
|
|
|
91 |
]
|
92 |
return jsonify(help_info_list)
|
93 |
|
|
|
80 |
history_list = session.get("history")
|
81 |
|
82 |
if input_text.upper()=="HELP":
|
83 |
+
help_info_list = ["输入任意文字,Winnie会和你对话",
|
84 |
"输入ERASE MEMORY,Winnie会清空记忆",
|
85 |
"输入\"<TAG>=<VALUE>\",Winnie会记录你的角色信息",
|
86 |
"例如:<NAME>=Vicky,Winnie会修改自己的名字",
|
87 |
"可以修改的角色信息有:",
|
88 |
"<NAME>, <GENDER>, <YEAROFBIRTH>, <MONTHOFBIRTH>, <DAYOFBIRTH>, <ZODIAC>, <AGE>",
|
89 |
"输入“上联:XXXXXXX”,Winnie会和你对对联",
|
90 |
+
"输入“写诗:XXXXXXX”,Winnie会以XXXXXXX为开头写诗",
|
91 |
+
"以\"请问\"开头并以问号结尾,Winnie会回答该问题"
|
92 |
]
|
93 |
return jsonify(help_info_list)
|
94 |
|