Commit
·
fd506d6
1
Parent(s):
7987763
增加对话记忆按键
Browse files
app.py
CHANGED
@@ -40,16 +40,20 @@ provider_dict = {
|
|
40 |
with gr.Blocks() as demo:
|
41 |
chatbot = gr.Chatbot([[None, None]], label='AI')
|
42 |
msg = gr.Textbox(value="", label='')
|
43 |
-
|
|
|
|
|
44 |
system_msg = gr.Textbox(value="你是一名助手,可以解答问题。", label='系统提示')
|
45 |
with gr.Row():
|
46 |
model_name = gr.Dropdown(['gpt-3.5-turbo', 'gpt-4'], value='gpt-3.5-turbo', label='模型')
|
47 |
provider_name = gr.Dropdown(provider_dict.keys(), value='GetGpt', label='提供者')
|
48 |
|
|
|
|
|
49 |
def user(user_message, history):
|
50 |
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
51 |
|
52 |
-
def bot(history, model_name, provider_name, system_msg):
|
53 |
history[-1][1] = ''
|
54 |
if len(system_msg)>3000:
|
55 |
system_msg = system_msg[:1500] + system_msg[-1500:]
|
@@ -59,6 +63,10 @@ with gr.Blocks() as demo:
|
|
59 |
```
|
60 |
{}
|
61 |
```
|
|
|
|
|
|
|
|
|
62 |
用户最新的话:
|
63 |
```
|
64 |
{}
|
@@ -69,17 +77,52 @@ with gr.Blocks() as demo:
|
|
69 |
provider=provider_dict[provider_name],
|
70 |
messages=[{"role": "user",
|
71 |
"content": prompt.format(system_msg,
|
|
|
72 |
history[-1][0])}],
|
73 |
stream=True)
|
74 |
for c in bot_msg:
|
75 |
history[-1][1] += c
|
76 |
yield history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
79 |
-
bot, [chatbot, model_name, provider_name, system_msg], chatbot
|
80 |
)
|
81 |
response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
|
82 |
-
|
|
|
|
|
|
|
83 |
|
84 |
demo.title = "AI Chat"
|
85 |
demo.queue()
|
|
|
40 |
with gr.Blocks() as demo:
|
41 |
chatbot = gr.Chatbot([[None, None]], label='AI')
|
42 |
msg = gr.Textbox(value="", label='')
|
43 |
+
with gr.Row():
|
44 |
+
clear = gr.Button("清空对话")
|
45 |
+
remember = gr.Button("记住最近一次对话")
|
46 |
system_msg = gr.Textbox(value="你是一名助手,可以解答问题。", label='系统提示')
|
47 |
with gr.Row():
|
48 |
model_name = gr.Dropdown(['gpt-3.5-turbo', 'gpt-4'], value='gpt-3.5-turbo', label='模型')
|
49 |
provider_name = gr.Dropdown(provider_dict.keys(), value='GetGpt', label='提供者')
|
50 |
|
51 |
+
memory_msg = gr.Textbox(value="", label='记忆')
|
52 |
+
|
53 |
def user(user_message, history):
|
54 |
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
55 |
|
56 |
+
def bot(history, model_name, provider_name, system_msg, memory_msg):
|
57 |
history[-1][1] = ''
|
58 |
if len(system_msg)>3000:
|
59 |
system_msg = system_msg[:1500] + system_msg[-1500:]
|
|
|
63 |
```
|
64 |
{}
|
65 |
```
|
66 |
+
历史对话中的关键信息:
|
67 |
+
```
|
68 |
+
{}
|
69 |
+
```
|
70 |
用户最新的话:
|
71 |
```
|
72 |
{}
|
|
|
77 |
provider=provider_dict[provider_name],
|
78 |
messages=[{"role": "user",
|
79 |
"content": prompt.format(system_msg,
|
80 |
+
memory_msg,
|
81 |
history[-1][0])}],
|
82 |
stream=True)
|
83 |
for c in bot_msg:
|
84 |
history[-1][1] += c
|
85 |
yield history
|
86 |
+
|
87 |
+
def update_memory(memory_msg, history, model_name, provider_name):
|
88 |
+
if history==None:
|
89 |
+
return gr.update(value=memory_msg, interactive=False)
|
90 |
+
prompt = """
|
91 |
+
请你记录对话内容,尤其是用户提供的各种关键信息。已知的对话内容总结如下:
|
92 |
+
```
|
93 |
+
{}
|
94 |
+
```
|
95 |
+
最新的对话内容如下:
|
96 |
+
```
|
97 |
+
用户:{}
|
98 |
+
AI:{}
|
99 |
+
```
|
100 |
+
总结格式如下:
|
101 |
+
```
|
102 |
+
关键信息总结:XXX
|
103 |
+
```
|
104 |
+
请对已知对话和最新对话的关键信息进行总结,以应对后续的聊天:
|
105 |
+
"""
|
106 |
+
memory = g4f.ChatCompletion.create(model=model_name,
|
107 |
+
provider=provider_dict[provider_name],
|
108 |
+
messages=[
|
109 |
+
{
|
110 |
+
'role':'user',
|
111 |
+
'content': prompt.format(memory_msg,
|
112 |
+
history[-1][0],
|
113 |
+
history[-1][1])
|
114 |
+
}
|
115 |
+
])
|
116 |
+
return gr.update(value=memory, interactive=False)
|
117 |
|
118 |
response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
119 |
+
bot, [chatbot, model_name, provider_name, system_msg, memory_msg], chatbot
|
120 |
)
|
121 |
response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
|
122 |
+
remember.click(
|
123 |
+
update_memory, [memory_msg, chatbot, model_name, provider_name], memory_msg
|
124 |
+
)
|
125 |
+
clear.click(lambda: (None, None), None, [chatbot, memory_msg], queue=False)
|
126 |
|
127 |
demo.title = "AI Chat"
|
128 |
demo.queue()
|