Jeremy Live commited on
Commit
e0bf1a3
·
1 Parent(s): 98aba2a

fix issue with the async/await pattern

Browse files
Files changed (1) hide show
  1. app.py +12 -13
app.py CHANGED
@@ -616,34 +616,33 @@ def create_application():
616
  async def bot_response(chat_history: List[Dict]) -> List[Dict]:
617
  """Get bot response and update chat history."""
618
  if not chat_history or chat_history[-1]["role"] != "assistant":
619
- yield chat_history
620
- return
621
 
622
  try:
623
  # Get the user's question (second to last message)
624
  if len(chat_history) < 2:
625
- yield chat_history
626
- return
627
 
628
  question = chat_history[-2]["content"]
629
  logger.info(f"Processing question: {question}")
630
 
631
- # Call the agent and stream the response
632
- async for response in stream_agent_response(question, chat_history[:-2]):
633
- if isinstance(response, list):
634
- for msg in response:
635
- if msg["role"] == "assistant":
636
- # Update the assistant's response
637
- chat_history[-1] = msg
638
- yield chat_history
639
 
640
  logger.info("Response generation complete")
 
641
 
642
  except Exception as e:
643
  error_msg = f"Error al procesar la solicitud: {str(e)}"
644
  logger.error(error_msg, exc_info=True)
645
  chat_history[-1]["content"] = error_msg
646
- yield chat_history
647
 
648
  # Event handlers
649
  with demo:
 
616
  async def bot_response(chat_history: List[Dict]) -> List[Dict]:
617
  """Get bot response and update chat history."""
618
  if not chat_history or chat_history[-1]["role"] != "assistant":
619
+ return chat_history
 
620
 
621
  try:
622
  # Get the user's question (second to last message)
623
  if len(chat_history) < 2:
624
+ return chat_history
 
625
 
626
  question = chat_history[-2]["content"]
627
  logger.info(f"Processing question: {question}")
628
 
629
+ # Call the agent and get the response
630
+ response = await stream_agent_response(question, chat_history[:-2])
631
+
632
+ if isinstance(response, list):
633
+ for msg in response:
634
+ if msg["role"] == "assistant":
635
+ # Update the assistant's response
636
+ chat_history[-1] = msg
637
 
638
  logger.info("Response generation complete")
639
+ return chat_history
640
 
641
  except Exception as e:
642
  error_msg = f"Error al procesar la solicitud: {str(e)}"
643
  logger.error(error_msg, exc_info=True)
644
  chat_history[-1]["content"] = error_msg
645
+ return chat_history
646
 
647
  # Event handlers
648
  with demo: