Commit
·
d4a3ee0
1
Parent(s):
6751efe
v3
Browse files
app.py
CHANGED
|
@@ -543,39 +543,35 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
| 543 |
logger.error(f"Error updating agent memory: {str(e)}", exc_info=True)
|
| 544 |
|
| 545 |
# Execute the agent with proper error handling
|
| 546 |
-
|
| 547 |
-
|
| 548 |
-
|
| 549 |
-
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
| 557 |
-
|
| 558 |
-
|
| 559 |
-
"devuelve solo la consulta en un bloque ```sql``` sin explicaciones.\n\n"
|
| 560 |
-
f"Pregunta del usuario: {question}"
|
| 561 |
-
)
|
| 562 |
|
| 563 |
-
|
| 564 |
-
|
| 565 |
-
|
| 566 |
-
|
| 567 |
-
|
| 568 |
-
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
-
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
logger.info(f"Extracted response text: {response_text[:200]}...")
|
| 579 |
|
| 580 |
# Check if the response contains an SQL query and it truly looks like SQL
|
| 581 |
sql_query = extract_sql_query(response_text)
|
|
|
|
| 543 |
logger.error(f"Error updating agent memory: {str(e)}", exc_info=True)
|
| 544 |
|
| 545 |
# Execute the agent with proper error handling
|
| 546 |
+
assistant_message = {"role": "assistant", "content": ""}
|
| 547 |
+
messages.append(assistant_message)
|
| 548 |
+
|
| 549 |
+
wants_chart, _desired = detect_chart_preferences(question)
|
| 550 |
+
input_text = question
|
| 551 |
+
if wants_chart:
|
| 552 |
+
input_text = (
|
| 553 |
+
"Instrucciones: Si el usuario pide una visualización y los datos ya "
|
| 554 |
+
"están en el historial, no pidas más contexto. Responde brevemente "
|
| 555 |
+
"confirmando que generarás/has generado la gráfica y, si necesitas SQL, "
|
| 556 |
+
"devuelve solo la consulta en un bloque ```sql``` sin explicaciones.\n\n"
|
| 557 |
+
f"Pregunta del usuario: {question}"
|
| 558 |
+
)
|
|
|
|
|
|
|
|
|
|
| 559 |
|
| 560 |
+
response = await agent.ainvoke({"input": input_text})
|
| 561 |
+
logger.info(f"Agent response type: {type(response)}")
|
| 562 |
+
logger.info(f"Agent response content: {str(response)[:500]}...")
|
| 563 |
+
|
| 564 |
+
# Handle different response formats
|
| 565 |
+
if hasattr(response, 'output') and response.output:
|
| 566 |
+
response_text = response.output
|
| 567 |
+
elif isinstance(response, str):
|
| 568 |
+
response_text = response
|
| 569 |
+
elif hasattr(response, 'get') and callable(response.get) and 'output' in response:
|
| 570 |
+
response_text = response['output']
|
| 571 |
+
else:
|
| 572 |
+
response_text = str(response)
|
| 573 |
+
|
| 574 |
+
logger.info(f"Extracted response text: {response_text[:200]}...")
|
|
|
|
| 575 |
|
| 576 |
# Check if the response contains an SQL query and it truly looks like SQL
|
| 577 |
sql_query = extract_sql_query(response_text)
|