thinkall commited on
Commit
8a93ebd
·
1 Parent(s): 5367e22

Fix message out of index

Browse files
Files changed (1) hide show
  1. app.py +32 -11
app.py CHANGED
@@ -15,6 +15,7 @@ from autogen.agentchat.contrib.retrieve_user_proxy_agent import (
15
 
16
  TIMEOUT = 60
17
 
 
18
  def initialize_agents(config_list, docs_path=None):
19
  if isinstance(config_list, gr.State):
20
  _config_list = config_list.value
@@ -54,7 +55,9 @@ def initiate_chat(config_list, problem, queue, n_results=3):
54
  else:
55
  _config_list = config_list
56
  if len(_config_list[0].get("api_key", "")) < 2:
57
- queue.put(["Hi, nice to meet you! Please enter your API keys in below text boxs."])
 
 
58
  return
59
  else:
60
  llm_config = (
@@ -92,7 +95,11 @@ def chatbot_reply(input_text):
92
  # process.join(TIMEOUT+2)
93
  messages = queue.get(timeout=TIMEOUT)
94
  except Exception as e:
95
- messages = [str(e) if len(str(e)) > 0 else "Invalid Request to OpenAI, please check your API keys."]
 
 
 
 
96
  finally:
97
  try:
98
  process.terminate()
@@ -148,6 +155,7 @@ with gr.Blocks() as demo:
148
  )
149
 
150
  with gr.Row():
 
151
  def update_config(config_list):
152
  global assistant, ragproxyagent
153
  config_list = autogen.config_list_from_models(
@@ -222,7 +230,8 @@ with gr.Blocks() as demo:
222
 
223
  clear = gr.ClearButton([txt_input, chatbot])
224
 
225
- with gr.Row():
 
226
  def upload_file(file):
227
  return update_context_url(file.name)
228
 
@@ -256,26 +265,34 @@ with gr.Blocks() as demo:
256
  set_params(model, oai_key, aoai_key, aoai_base)
257
  config_list = update_config(config_list)
258
  messages = chatbot_reply(message)
259
- chat_history.append(
260
- (message, messages[-1] if messages[-1] != "TERMINATE" else messages[-2])
 
 
 
 
261
  )
 
262
  return "", chat_history
263
 
264
  def update_prompt(prompt):
265
  ragproxyagent.customized_prompt = prompt
266
  return prompt
267
-
268
  def update_context_url(context_url):
269
  global assistant, ragproxyagent
270
-
271
  file_extension = Path(context_url).suffix
272
  print("file_extension: ", file_extension)
273
- if file_extension.lower() not in [f'.{i}' for i in TEXT_FORMATS]:
274
  return f"File must be in the format of {TEXT_FORMATS}"
275
-
276
  if is_url(context_url):
277
  try:
278
- file_path = get_file_from_url(context_url, save_path=os.path.join("/tmp", os.path.basename(context_url)))
 
 
 
279
  except Exception as e:
280
  return str(e)
281
  else:
@@ -289,7 +306,11 @@ with gr.Blocks() as demo:
289
  assistant, ragproxyagent = initialize_agents(config_list, docs_path=file_path)
290
  return context_url
291
 
292
- txt_input.submit(respond, [txt_input, chatbot, txt_model, txt_oai_key, txt_aoai_key, txt_aoai_base_url], [txt_input, chatbot])
 
 
 
 
293
  txt_prompt.submit(update_prompt, [txt_prompt], [txt_prompt])
294
  txt_context_url.submit(update_context_url, [txt_context_url], [txt_context_url])
295
  upload_button.upload(upload_file, upload_button, [txt_context_url])
 
15
 
16
  TIMEOUT = 60
17
 
18
+
19
  def initialize_agents(config_list, docs_path=None):
20
  if isinstance(config_list, gr.State):
21
  _config_list = config_list.value
 
55
  else:
56
  _config_list = config_list
57
  if len(_config_list[0].get("api_key", "")) < 2:
58
+ queue.put(
59
+ ["Hi, nice to meet you! Please enter your API keys in below text boxs."]
60
+ )
61
  return
62
  else:
63
  llm_config = (
 
95
  # process.join(TIMEOUT+2)
96
  messages = queue.get(timeout=TIMEOUT)
97
  except Exception as e:
98
+ messages = [
99
+ str(e)
100
+ if len(str(e)) > 0
101
+ else "Invalid Request to OpenAI, please check your API keys."
102
+ ]
103
  finally:
104
  try:
105
  process.terminate()
 
155
  )
156
 
157
  with gr.Row():
158
+
159
  def update_config(config_list):
160
  global assistant, ragproxyagent
161
  config_list = autogen.config_list_from_models(
 
230
 
231
  clear = gr.ClearButton([txt_input, chatbot])
232
 
233
+ with gr.Row():
234
+
235
  def upload_file(file):
236
  return update_context_url(file.name)
237
 
 
265
  set_params(model, oai_key, aoai_key, aoai_base)
266
  config_list = update_config(config_list)
267
  messages = chatbot_reply(message)
268
+ _msg = (
269
+ messages[-1]
270
+ if len(messages) > 0 and messages[-1] != "TERMINATE"
271
+ else messages[-2]
272
+ if len(messages) > 1
273
+ else "Context is not enough for answering the question. Please press `enter` in the context url textbox to make sure the context is activated for the chat."
274
  )
275
+ chat_history.append((message, _msg))
276
  return "", chat_history
277
 
278
  def update_prompt(prompt):
279
  ragproxyagent.customized_prompt = prompt
280
  return prompt
281
+
282
  def update_context_url(context_url):
283
  global assistant, ragproxyagent
284
+
285
  file_extension = Path(context_url).suffix
286
  print("file_extension: ", file_extension)
287
+ if file_extension.lower() not in [f".{i}" for i in TEXT_FORMATS]:
288
  return f"File must be in the format of {TEXT_FORMATS}"
289
+
290
  if is_url(context_url):
291
  try:
292
+ file_path = get_file_from_url(
293
+ context_url,
294
+ save_path=os.path.join("/tmp", os.path.basename(context_url)),
295
+ )
296
  except Exception as e:
297
  return str(e)
298
  else:
 
306
  assistant, ragproxyagent = initialize_agents(config_list, docs_path=file_path)
307
  return context_url
308
 
309
+ txt_input.submit(
310
+ respond,
311
+ [txt_input, chatbot, txt_model, txt_oai_key, txt_aoai_key, txt_aoai_base_url],
312
+ [txt_input, chatbot],
313
+ )
314
  txt_prompt.submit(update_prompt, [txt_prompt], [txt_prompt])
315
  txt_context_url.submit(update_context_url, [txt_context_url], [txt_context_url])
316
  upload_button.upload(upload_file, upload_button, [txt_context_url])