Commit
·
b71cc3a
1
Parent(s):
9d85333
Update app.py
Browse filesAdd function to clear memory
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import g4f
|
2 |
import gradio as gr
|
|
|
3 |
from g4f.Provider import (
|
4 |
Ails,
|
5 |
You,
|
@@ -92,8 +93,9 @@ def change_prompt(prompt_set_name, prompt_name):
|
|
92 |
def user(user_message, history):
|
93 |
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
94 |
|
95 |
-
def bot(
|
96 |
-
|
|
|
97 |
|
98 |
if len(system_msg)>3000:
|
99 |
system_msg = system_msg[:2000] + system_msg[-1000:]
|
@@ -126,22 +128,36 @@ def bot(message, history, model_name, provider_name, system_msg, agent):
|
|
126 |
prompt = PromptTemplate(
|
127 |
input_variables=["chat_history", "human_input"], template=new_template
|
128 |
)
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
for c in bot_msg:
|
137 |
-
|
138 |
-
|
139 |
|
140 |
-
def
|
141 |
global memory
|
142 |
-
memory = ConversationBufferWindowMemory(k=
|
143 |
return None
|
144 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
145 |
prompt_set_list = {}
|
146 |
for prompt_file in os.listdir("prompt_set"):
|
147 |
key = prompt_file
|
@@ -165,7 +181,7 @@ with gr.Blocks() as demo:
|
|
165 |
The following is a conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
|
166 |
{{chat_history}}
|
167 |
Human: {{human_input}}
|
168 |
-
|
169 |
|
170 |
memory = ConversationBufferWindowMemory(k=6, memory_key="chat_history")
|
171 |
with gr.Row():
|
@@ -173,18 +189,42 @@ with gr.Blocks() as demo:
|
|
173 |
provider = gr.Dropdown(available_dict['gpt-3.5-turbo'], value='AiService', label='提供者', min_width=20)
|
174 |
agent = gr.Dropdown(['系统提示', '维基百科', 'duckduckgo'], value='系统提示', label='Agent')
|
175 |
system_msg = gr.Textbox(value="你是一名助手,可以解答问题。", label='系统提示')
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
with gr.Row():
|
184 |
default_prompt_set = "1 中文提示词.json"
|
185 |
prompt_set_name = gr.Dropdown(prompt_set_list.keys(), value=default_prompt_set, label='提示词集合')
|
186 |
prompt_name = gr.Dropdown(prompt_set_list[default_prompt_set].keys(), label='提示词', min_width=5, container=True)
|
187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
188 |
prompt_set_name.select(change_prompt_set, prompt_set_name, prompt_name)
|
189 |
model_name.select(change_model, model_name, provider)
|
190 |
prompt_name.select(change_prompt, [prompt_set_name, prompt_name], system_msg)
|
|
|
1 |
import g4f
|
2 |
import gradio as gr
|
3 |
+
from gradio import ChatInterface
|
4 |
from g4f.Provider import (
|
5 |
Ails,
|
6 |
You,
|
|
|
93 |
def user(user_message, history):
|
94 |
return gr.update(value="", interactive=False), history + [[user_message, None]]
|
95 |
|
96 |
+
def bot(history, model_name, provider_name, system_msg, agent):
|
97 |
+
history[-1][1] = ''
|
98 |
+
message = history[-1][0]
|
99 |
|
100 |
if len(system_msg)>3000:
|
101 |
system_msg = system_msg[:2000] + system_msg[-1000:]
|
|
|
128 |
prompt = PromptTemplate(
|
129 |
input_variables=["chat_history", "human_input"], template=new_template
|
130 |
)
|
131 |
+
|
132 |
+
prev_memory = ''
|
133 |
+
if len(history)>1 and history[-2][1]!=None:
|
134 |
+
memory.chat_memory.add_user_message(history[-2][0])
|
135 |
+
memory.chat_memory.add_ai_message(history[-2][1])
|
136 |
+
prev_memory = memory.load_memory_variables({})['chat_history']
|
137 |
+
|
138 |
+
prompt = new_template.format(
|
139 |
+
chat_history = prev_memory,
|
140 |
+
human_input = message
|
141 |
+
)
|
142 |
+
|
143 |
+
bot_msg = llm._call(prompt=prompt)
|
144 |
+
|
145 |
for c in bot_msg:
|
146 |
+
history[-1][1] += c
|
147 |
+
yield history
|
148 |
|
149 |
+
def empty_fn():
|
150 |
global memory
|
151 |
+
memory = ConversationBufferWindowMemory(k=10, memory_key="chat_history")
|
152 |
return None
|
153 |
|
154 |
+
def undo_fn(history):
|
155 |
+
return history[:-1]
|
156 |
+
|
157 |
+
def retry_fn(history):
|
158 |
+
history[-1][1] = None
|
159 |
+
return history
|
160 |
+
|
161 |
prompt_set_list = {}
|
162 |
for prompt_file in os.listdir("prompt_set"):
|
163 |
key = prompt_file
|
|
|
181 |
The following is a conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
|
182 |
{{chat_history}}
|
183 |
Human: {{human_input}}
|
184 |
+
AI:"""
|
185 |
|
186 |
memory = ConversationBufferWindowMemory(k=6, memory_key="chat_history")
|
187 |
with gr.Row():
|
|
|
189 |
provider = gr.Dropdown(available_dict['gpt-3.5-turbo'], value='AiService', label='提供者', min_width=20)
|
190 |
agent = gr.Dropdown(['系统提示', '维基百科', 'duckduckgo'], value='系统提示', label='Agent')
|
191 |
system_msg = gr.Textbox(value="你是一名助手,可以解答问题。", label='系统提示')
|
192 |
+
|
193 |
+
chatbot = gr.Chatbot([[None, None]], label='AI')
|
194 |
+
with gr.Group():
|
195 |
+
with gr.Row():
|
196 |
+
textbox = gr.Textbox(
|
197 |
+
container=False,
|
198 |
+
show_label=False,
|
199 |
+
label="请输入:",
|
200 |
+
scale=7,
|
201 |
+
autofocus=True,
|
202 |
+
)
|
203 |
+
submit = gr.Button('发送', scale=1, variant="primary", min_width=150,)
|
204 |
+
with gr.Row():
|
205 |
+
retry = gr.Button('🔄 重试')
|
206 |
+
undo = gr.Button('↩️ 撤销')
|
207 |
+
clear = gr.Button("🗑️ 清空")
|
208 |
+
|
209 |
with gr.Row():
|
210 |
default_prompt_set = "1 中文提示词.json"
|
211 |
prompt_set_name = gr.Dropdown(prompt_set_list.keys(), value=default_prompt_set, label='提示词集合')
|
212 |
prompt_name = gr.Dropdown(prompt_set_list[default_prompt_set].keys(), label='提示词', min_width=5, container=True)
|
213 |
|
214 |
+
textbox.submit(user, [textbox, chatbot], [textbox, chatbot], queue=False).then(
|
215 |
+
bot, [chatbot, model_name, provider, system_msg, agent], chatbot
|
216 |
+
).then(lambda: gr.update(interactive=True), None, [textbox], queue=False)
|
217 |
+
|
218 |
+
response = submit.click(user, [textbox, chatbot], [textbox, chatbot], queue=False).then(
|
219 |
+
bot, [chatbot, model_name, provider, system_msg, agent], chatbot
|
220 |
+
).then(lambda: gr.update(interactive=True), None, [textbox], queue=False)
|
221 |
+
|
222 |
+
retry.click(retry_fn, [chatbot], [chatbot]).then(
|
223 |
+
bot, [chatbot, model_name, provider, system_msg, agent], chatbot
|
224 |
+
)
|
225 |
+
undo.click(undo_fn, [chatbot], [chatbot], queue=False)
|
226 |
+
clear.click(empty_fn, None, [chatbot], queue=False)
|
227 |
+
|
228 |
prompt_set_name.select(change_prompt_set, prompt_set_name, prompt_name)
|
229 |
model_name.select(change_model, model_name, provider)
|
230 |
prompt_name.select(change_prompt, [prompt_set_name, prompt_name], system_msg)
|