hiwei commited on
Commit
79c8493
·
verified ·
1 Parent(s): 42c41b0

add retry last chat message button

Browse files
Files changed (1) hide show
  1. app.py +19 -1
app.py CHANGED
@@ -77,6 +77,7 @@ class RAGDemo(object):
77
  documents=docs, embedding=self.embedding
78
  )
79
  gr.Info("Vector database built successfully.")
 
80
 
81
  def _init_settings(self, model_name, api_key, embedding_model, embedding_api_key, data_file):
82
  self._init_chat_model(model_name, api_key)
@@ -94,6 +95,8 @@ class RAGDemo(object):
94
  return resp['result']
95
 
96
  def _chat_qa(self, message, chat_history):
 
 
97
  memory = ConversationBufferMemory(
98
  chat_memory=ChatMessageHistory(
99
  messages=convert_chat_history_to_messages(chat_history)
@@ -108,9 +111,16 @@ class RAGDemo(object):
108
  verbose=True,
109
  )
110
  resp = qa.invoke(message)
 
111
  chat_history.append((message, resp['answer']))
112
  return "", chat_history
113
 
 
 
 
 
 
 
114
  def __call__(self):
115
  with gr.Blocks(title="🔥 RAG Demo") as demo:
116
  gr.Markdown("# RAG Demo\n\nbase on the [RAG learning note](https://www.jianshu.com/p/9792f1e6c3f9) and "
@@ -144,7 +154,9 @@ class RAGDemo(object):
144
  with gr.Tab("Chat RAG"):
145
  chatbot = gr.Chatbot(label="chat with pdf")
146
  input_msg = gr.Textbox(placeholder="input your question...", label="input")
147
- clear_btn = gr.ClearButton([chatbot, input_msg])
 
 
148
  initial_btn.click(
149
  self._init_settings,
150
  inputs=[model_name, api_key, embedding_model, embedding_api_key, data_file]
@@ -161,6 +173,12 @@ class RAGDemo(object):
161
  inputs=[input_msg, chatbot],
162
  outputs=[input_msg, chatbot]
163
  )
 
 
 
 
 
 
164
  return demo
165
 
166
 
 
77
  documents=docs, embedding=self.embedding
78
  )
79
  gr.Info("Vector database built successfully.")
80
+ print("Vector database built successfully.")
81
 
82
  def _init_settings(self, model_name, api_key, embedding_model, embedding_api_key, data_file):
83
  self._init_chat_model(model_name, api_key)
 
95
  return resp['result']
96
 
97
  def _chat_qa(self, message, chat_history):
98
+ if not message:
99
+ return "", chat_history
100
  memory = ConversationBufferMemory(
101
  chat_memory=ChatMessageHistory(
102
  messages=convert_chat_history_to_messages(chat_history)
 
111
  verbose=True,
112
  )
113
  resp = qa.invoke(message)
114
+ print(f">>> {resp}")
115
  chat_history.append((message, resp['answer']))
116
  return "", chat_history
117
 
118
+ def _retry_chat_qa(self, chat_history):
119
+ message = ""
120
+ if chat_history:
121
+ message, _ = chat_history.pop()
122
+ return self._chat_qa(message, chat_history)
123
+
124
  def __call__(self):
125
  with gr.Blocks(title="🔥 RAG Demo") as demo:
126
  gr.Markdown("# RAG Demo\n\nbase on the [RAG learning note](https://www.jianshu.com/p/9792f1e6c3f9) and "
 
154
  with gr.Tab("Chat RAG"):
155
  chatbot = gr.Chatbot(label="chat with pdf")
156
  input_msg = gr.Textbox(placeholder="input your question...", label="input")
157
+ with gr.Row():
158
+ clear_btn = gr.ClearButton([chatbot, input_msg], value="🧹 Clear")
159
+ retry_btn = gr.Button("♻️ Retry")
160
  initial_btn.click(
161
  self._init_settings,
162
  inputs=[model_name, api_key, embedding_model, embedding_api_key, data_file]
 
173
  inputs=[input_msg, chatbot],
174
  outputs=[input_msg, chatbot]
175
  )
176
+
177
+ retry_btn.click(
178
+ self._retry_chat_qa,
179
+ inputs=chatbot,
180
+ outputs=[input_msg, chatbot]
181
+ )
182
  return demo
183
 
184