Jeremy Live
commited on
Commit
·
2de8ed1
1
Parent(s):
e0bf1a3
better handle agent responses and improve error logging
Browse files
app.py
CHANGED
@@ -358,16 +358,28 @@ async def stream_agent_response(question: str, chat_history: List) -> List[Dict]
|
|
358 |
assistant_message = {"role": "assistant", "content": ""}
|
359 |
messages.append(assistant_message)
|
360 |
|
361 |
-
# Execute the agent
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
367 |
|
368 |
# Check if the response contains an SQL query
|
369 |
sql_query = extract_sql_query(response_text)
|
370 |
if sql_query:
|
|
|
371 |
# Execute the query and update the response
|
372 |
db_connection, _ = setup_database_connection()
|
373 |
if db_connection:
|
@@ -379,6 +391,11 @@ async def stream_agent_response(question: str, chat_history: List) -> List[Dict]
|
|
379 |
# Update the assistant's message with the response
|
380 |
assistant_message["content"] = response_text
|
381 |
|
|
|
|
|
|
|
|
|
|
|
382 |
else:
|
383 |
assistant_message["content"] = "Error: No se recibió respuesta del agente."
|
384 |
|
|
|
358 |
assistant_message = {"role": "assistant", "content": ""}
|
359 |
messages.append(assistant_message)
|
360 |
|
361 |
+
# Execute the agent with proper error handling
|
362 |
+
try:
|
363 |
+
response = await agent.ainvoke({"input": question, "chat_history": chat_history})
|
364 |
+
logger.info(f"Agent response type: {type(response)}")
|
365 |
+
logger.info(f"Agent response content: {str(response)[:500]}...")
|
366 |
+
|
367 |
+
# Handle different response formats
|
368 |
+
if hasattr(response, 'output') and response.output:
|
369 |
+
response_text = response.output
|
370 |
+
elif isinstance(response, str):
|
371 |
+
response_text = response
|
372 |
+
elif hasattr(response, 'get') and callable(response.get) and 'output' in response:
|
373 |
+
response_text = response['output']
|
374 |
+
else:
|
375 |
+
response_text = str(response)
|
376 |
+
|
377 |
+
logger.info(f"Extracted response text: {response_text[:200]}...")
|
378 |
|
379 |
# Check if the response contains an SQL query
|
380 |
sql_query = extract_sql_query(response_text)
|
381 |
if sql_query:
|
382 |
+
logger.info(f"Detected SQL query: {sql_query}")
|
383 |
# Execute the query and update the response
|
384 |
db_connection, _ = setup_database_connection()
|
385 |
if db_connection:
|
|
|
391 |
# Update the assistant's message with the response
|
392 |
assistant_message["content"] = response_text
|
393 |
|
394 |
+
except Exception as e:
|
395 |
+
error_msg = f"Error al ejecutar el agente: {str(e)}"
|
396 |
+
logger.error(error_msg, exc_info=True)
|
397 |
+
assistant_message["content"] = f"## ❌ Error\n\n{error_msg}"
|
398 |
+
|
399 |
else:
|
400 |
assistant_message["content"] = "Error: No se recibió respuesta del agente."
|
401 |
|