Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,7 @@ import random
|
|
4 |
|
5 |
openai.api_key = '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'
|
6 |
|
7 |
-
|
8 |
-
{"role": "system", "content": "你是一个聪明的AI助手。"},
|
9 |
-
{"role": "user", "content": "你会说中文吗?"},
|
10 |
-
{"role": "assistant", "content": "是的,我可以说中文。"}
|
11 |
-
]
|
12 |
|
13 |
def get_assistant_response(user_question, context):
|
14 |
context.append({"role": "user", "content": user_question})
|
@@ -21,14 +17,24 @@ def get_assistant_response(user_question, context):
|
|
21 |
context.append({"role": "assistant", "content": assistant_response})
|
22 |
return assistant_response
|
23 |
|
24 |
-
def greet(user_question, clear_history):
|
25 |
-
global
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
if clear_history:
|
27 |
context = [
|
28 |
{"role": "system", "content": "你是一个聪明的AI助手。"},
|
29 |
{"role": "user", "content": "你会说中文吗?"},
|
30 |
{"role": "assistant", "content": "是的,我可以说中文。"}
|
31 |
]
|
|
|
32 |
return '清空成功', '保持聊天记录'
|
33 |
else:
|
34 |
get_assistant_response(user_question, context)
|
@@ -41,16 +47,17 @@ def greet(user_question, clear_history):
|
|
41 |
demo = gr.Interface(
|
42 |
fn=greet,
|
43 |
inputs=[
|
44 |
-
gr.Textbox(lines=
|
|
|
45 |
gr.Checkbox(label='清空聊天记录', default=False)
|
46 |
],
|
47 |
outputs=[
|
48 |
gr.Textbox(lines=1, label='聊天记录状态', placeholder='等待清空聊天记录'),
|
49 |
-
gr.Textbox(lines=
|
50 |
],
|
51 |
title="AI助手",
|
52 |
description="请输入您的问题,AI助手会给出回答。支持连续对话,可以记录对话历史。重新开始对话勾选清空聊天记录,输出清空成功表示重新开启对话。"
|
53 |
)
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
-
demo.launch()
|
|
|
4 |
|
5 |
openai.api_key = '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'
|
6 |
|
7 |
+
user_contexts = {}
|
|
|
|
|
|
|
|
|
8 |
|
9 |
def get_assistant_response(user_question, context):
|
10 |
context.append({"role": "user", "content": user_question})
|
|
|
17 |
context.append({"role": "assistant", "content": assistant_response})
|
18 |
return assistant_response
|
19 |
|
20 |
+
def greet(user_id, user_question, clear_history):
|
21 |
+
global user_contexts
|
22 |
+
if user_id not in user_contexts:
|
23 |
+
user_contexts[user_id] = [
|
24 |
+
{"role": "system", "content": "你是一个聪明的AI助手。"},
|
25 |
+
{"role": "user", "content": "你会说中文吗?"},
|
26 |
+
{"role": "assistant", "content": "是的,我可以说中文。"}
|
27 |
+
]
|
28 |
+
|
29 |
+
context = user_contexts[user_id]
|
30 |
+
|
31 |
if clear_history:
|
32 |
context = [
|
33 |
{"role": "system", "content": "你是一个聪明的AI助手。"},
|
34 |
{"role": "user", "content": "你会说中文吗?"},
|
35 |
{"role": "assistant", "content": "是的,我可以说中文。"}
|
36 |
]
|
37 |
+
user_contexts[user_id] = context
|
38 |
return '清空成功', '保持聊天记录'
|
39 |
else:
|
40 |
get_assistant_response(user_question, context)
|
|
|
47 |
demo = gr.Interface(
|
48 |
fn=greet,
|
49 |
inputs=[
|
50 |
+
gr.Textbox(lines=1, label='请输入用户ID', placeholder='请输入用户ID'),
|
51 |
+
gr.Textbox(lines=15, label='请输入问题', placeholder='请输入您的问题'),
|
52 |
gr.Checkbox(label='清空聊天记录', default=False)
|
53 |
],
|
54 |
outputs=[
|
55 |
gr.Textbox(lines=1, label='聊天记录状态', placeholder='等待清空聊天记录'),
|
56 |
+
gr.Textbox(lines=20, label='AI回答', placeholder='等待AI回答')
|
57 |
],
|
58 |
title="AI助手",
|
59 |
description="请输入您的问题,AI助手会给出回答。支持连续对话,可以记录对话历史。重新开始对话勾选清空聊天记录,输出清空成功表示重新开启对话。"
|
60 |
)
|
61 |
|
62 |
if __name__ == "__main__":
|
63 |
+
demo.launch()
|