Jeremy Live commited on
Commit
5bd02e3
·
1 Parent(s): deeeba1

handle the async generator

Browse files
Files changed (1) hide show
  1. app.py +6 -2
app.py CHANGED
@@ -495,7 +495,7 @@ def create_application():
495
  updated_history = chat_history + [{"role": "user", "content": user_input}]
496
  return "", updated_history
497
 
498
- def bot_response(chat_history: List[Dict]) -> Tuple[List[Dict], Dict]:
499
  """Get bot response and update chat history."""
500
  if not chat_history or not chat_history[-1].get("role") == "user":
501
  return chat_history, gr.update(visible=False)
@@ -513,7 +513,11 @@ def create_application():
513
  old_format[-1][1] = msg["content"]
514
 
515
  # Call the agent and get the response
516
- return stream_agent_response(question, old_format[:-1])
 
 
 
 
517
 
518
  # Event handlers
519
  with demo:
 
495
  updated_history = chat_history + [{"role": "user", "content": user_input}]
496
  return "", updated_history
497
 
498
+ async def bot_response(chat_history: List[Dict]) -> Tuple[List[Dict], Dict]:
499
  """Get bot response and update chat history."""
500
  if not chat_history or not chat_history[-1].get("role") == "user":
501
  return chat_history, gr.update(visible=False)
 
513
  old_format[-1][1] = msg["content"]
514
 
515
  # Call the agent and get the response
516
+ # We need to consume the async generator and return the last value
517
+ last_response = None
518
+ async for response in stream_agent_response(question, old_format[:-1]):
519
+ last_response = response
520
+ return last_response
521
 
522
  # Event handlers
523
  with demo: