Spaces:
Sleeping
Sleeping
Fishead_East
commited on
Commit
•
48157a2
1
Parent(s):
fa03a65
重构代码
Browse files- app.py +1 -1
- chat_poets/chat.py +12 -3
- chat_poets/prompts/allow_chat.json +5 -0
- chat_poets/prompts/example1.json +0 -5
- chat_poets/prompts/example2.json +0 -5
- gradio_ui/gr_chat.py +2 -2
app.py
CHANGED
@@ -28,7 +28,7 @@ with gr.Blocks() as demo:
|
|
28 |
).style(container=False)
|
29 |
|
30 |
image_button = gr.Button("点击生图")
|
31 |
-
image_button.click(None, inputs=txt_img, outputs=image_out) # 生图函数待填充
|
32 |
|
33 |
|
34 |
demo.queue()
|
|
|
28 |
).style(container=False)
|
29 |
|
30 |
image_button = gr.Button("点击生图")
|
31 |
+
image_button.click(None, inputs=txt_img, outputs=image_out) # todo 生图函数待填充
|
32 |
|
33 |
|
34 |
demo.queue()
|
chat_poets/chat.py
CHANGED
@@ -12,9 +12,19 @@ class ChatPoet:
|
|
12 |
@classmethod
|
13 |
def allow_chat(cls, user_message: str) -> bool:
|
14 |
"""
|
15 |
-
对话开始时判断用户输入是否合规:提及诗人或古诗
|
16 |
"""
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
@classmethod
|
20 |
def gen_response(cls, pattern: str, history: list[list]) -> str:
|
@@ -24,4 +34,3 @@ class ChatPoet:
|
|
24 |
注:对话历史的最后一项是需要填充的内容,即history[-1] = [Question, None];Question为用户刚提出的问题,None即尚未回答
|
25 |
"""
|
26 |
return "哇哇哇!"
|
27 |
-
|
|
|
12 |
@classmethod
|
13 |
def allow_chat(cls, user_message: str) -> bool:
|
14 |
"""
|
15 |
+
对话开始时判断用户输入是否合规:提及诗人或古诗 todo 需要进一步优化提示词
|
16 |
"""
|
17 |
+
prompt_allow = load_prompt("chat_poets/prompts/allow_chat.json")
|
18 |
+
chain_allow = LLMChain(llm=cls.llm, prompt=prompt_allow)
|
19 |
+
response = chain_allow.run(user_message)
|
20 |
+
print(f"response: {response}")
|
21 |
+
|
22 |
+
while True:
|
23 |
+
try:
|
24 |
+
allow = int(response)
|
25 |
+
return allow == 1
|
26 |
+
except ValueError:
|
27 |
+
response = chain_allow.run(user_message)
|
28 |
|
29 |
@classmethod
|
30 |
def gen_response(cls, pattern: str, history: list[list]) -> str:
|
|
|
34 |
注:对话历史的最后一项是需要填充的内容,即history[-1] = [Question, None];Question为用户刚提出的问题,None即尚未回答
|
35 |
"""
|
36 |
return "哇哇哇!"
|
|
chat_poets/prompts/allow_chat.json
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_type": "prompt",
|
3 |
+
"input_variables": ["user_message"],
|
4 |
+
"template": "请分析问题中是否提到了诗人或古诗。如果是则输出1,否则输出0;不输出除结果外的其他内容。问题如下->{user_message}"
|
5 |
+
}
|
chat_poets/prompts/example1.json
DELETED
@@ -1,5 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"_type": "prompt",
|
3 |
-
"input_variables": ["text"],
|
4 |
-
"template": "阿巴{text}"
|
5 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
chat_poets/prompts/example2.json
DELETED
@@ -1,5 +0,0 @@
|
|
1 |
-
{
|
2 |
-
"_type": "prompt",
|
3 |
-
"input_variables": ["text"],
|
4 |
-
"template": "阿巴阿巴{text}"
|
5 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
gradio_ui/gr_chat.py
CHANGED
@@ -15,7 +15,7 @@ def chat_poetry(tab: gr.Tab):
|
|
15 |
pattern = gr.Markdown(f"{tab.id}")
|
16 |
action = gr.State(value=False) # 指示有效对话是否开始(开始时用户未提及古诗诗人则未进入)
|
17 |
with gr.Row():
|
18 |
-
# 历史记录
|
19 |
with gr.Column(scale=2):
|
20 |
gr.Markdown("## 历史记录")
|
21 |
chat_history = gr.State([[]]) # 存储单次聊天记录的组件
|
@@ -54,7 +54,7 @@ def newchat(chat_history: list[list]):
|
|
54 |
print("#########")
|
55 |
for chat_list in chat_history:
|
56 |
print(f"##用户:{chat_list[0]} ## Bot:{chat_list[1]}")
|
57 |
-
# 将已有聊天记录存入历史记录
|
58 |
|
59 |
# 刷新聊天记录
|
60 |
return [None, None, None]
|
|
|
15 |
pattern = gr.Markdown(f"{tab.id}")
|
16 |
action = gr.State(value=False) # 指示有效对话是否开始(开始时用户未提及古诗诗人则未进入)
|
17 |
with gr.Row():
|
18 |
+
# todo 历史记录
|
19 |
with gr.Column(scale=2):
|
20 |
gr.Markdown("## 历史记录")
|
21 |
chat_history = gr.State([[]]) # 存储单次聊天记录的组件
|
|
|
54 |
print("#########")
|
55 |
for chat_list in chat_history:
|
56 |
print(f"##用户:{chat_list[0]} ## Bot:{chat_list[1]}")
|
57 |
+
# todo 将已有聊天记录存入历史记录
|
58 |
|
59 |
# 刷新聊天记录
|
60 |
return [None, None, None]
|