Jeremy Live
commited on
Commit
·
82420e4
1
Parent(s):
1e24325
v5
Browse files
app.py
CHANGED
@@ -572,7 +572,6 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
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)
|
578 |
if sql_query and looks_like_sql(sql_query):
|
@@ -580,167 +579,173 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
580 |
# Execute the query and update the response
|
581 |
db_connection, _ = setup_database_connection()
|
582 |
if db_connection:
|
583 |
-
query_result = execute_sql_query(sql_query, db_connection)
|
584 |
-
|
585 |
-
# Add the query and its result to the response
|
586 |
-
response_text += f"\n\n### 🔍 Resultado de la consulta:\n```sql\n{sql_query}\n```\n\n{query_result}"
|
587 |
-
|
588 |
-
# Try to generate an interactive chart if the result is tabular
|
589 |
try:
|
590 |
-
|
591 |
-
# Clean up the markdown table
|
592 |
-
lines = [line.strip() for line in query_result.split('\n')
|
593 |
-
if line.strip() and '---' not in line and '|' in line]
|
594 |
-
if len(lines) > 1: # At least header + 1 data row
|
595 |
-
# Get column names from the first line
|
596 |
-
columns = [col.strip() for col in lines[0].split('|')[1:-1]]
|
597 |
-
# Get data rows
|
598 |
-
data = []
|
599 |
-
for line in lines[1:]:
|
600 |
-
values = [val.strip() for val in line.split('|')[1:-1]]
|
601 |
-
if len(values) == len(columns):
|
602 |
-
data.append(dict(zip(columns, values)))
|
603 |
-
|
604 |
-
if data and len(columns) >= 2:
|
605 |
-
# Determine chart type from user's question
|
606 |
-
_, desired_type = detect_chart_preferences(question)
|
607 |
-
|
608 |
-
# Choose x/y columns (assume first is category, second numeric)
|
609 |
-
x_col = columns[0]
|
610 |
-
y_col = columns[1]
|
611 |
|
612 |
-
|
613 |
-
|
614 |
-
try:
|
615 |
-
row[y_col] = float(re.sub(r"[^0-9.\-]", "", str(row[y_col])))
|
616 |
-
except Exception:
|
617 |
-
pass
|
618 |
-
|
619 |
-
chart_fig = generate_chart(
|
620 |
-
data=data,
|
621 |
-
chart_type=desired_type,
|
622 |
-
x=x_col,
|
623 |
-
y=y_col,
|
624 |
-
title=f"{y_col} por {x_col}"
|
625 |
-
)
|
626 |
-
if chart_fig is not None:
|
627 |
-
logger.info(f"Chart generated from SQL table: type={desired_type}, x={x_col}, y={y_col}, rows={len(data)}")
|
628 |
-
except Exception as e:
|
629 |
-
logger.error(f"Error generating chart: {str(e)}", exc_info=True)
|
630 |
-
# Don't fail the whole request if chart generation fails
|
631 |
-
response_text += "\n\n⚠️ No se pudo generar la visualización de los datos."
|
632 |
-
else:
|
633 |
-
response_text += "\n\n⚠️ No se pudo conectar a la base de datos para ejecutar la consulta."
|
634 |
-
elif sql_query and not looks_like_sql(sql_query):
|
635 |
-
logger.info("Detected code block but it does not look like SQL; skipping execution.")
|
636 |
|
637 |
-
|
638 |
-
|
639 |
-
if chart_fig is None:
|
640 |
-
wants_chart, default_type = detect_chart_preferences(question)
|
641 |
-
if wants_chart:
|
642 |
try:
|
643 |
-
|
644 |
-
|
645 |
-
|
646 |
-
|
647 |
-
|
648 |
-
|
649 |
-
|
650 |
-
|
651 |
-
|
652 |
-
|
653 |
-
|
654 |
-
|
655 |
-
|
656 |
-
|
657 |
-
|
658 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
659 |
try:
|
660 |
-
|
661 |
-
df = pd.read_csv(io.StringIO(query_result), sep="|")
|
662 |
-
data = df
|
663 |
except Exception:
|
664 |
pass
|
665 |
-
|
666 |
-
|
667 |
-
|
668 |
-
|
669 |
-
|
670 |
-
y_col
|
671 |
-
|
672 |
-
|
673 |
-
|
674 |
-
|
675 |
-
|
676 |
-
|
677 |
-
continue
|
678 |
-
if y_col:
|
679 |
-
desired_type = default_type
|
680 |
-
chart_fig = generate_chart(
|
681 |
-
data=data,
|
682 |
-
chart_type=desired_type,
|
683 |
-
x=x_col,
|
684 |
-
y=y_col,
|
685 |
-
title=f"{y_col} por {x_col}"
|
686 |
-
)
|
687 |
-
if chart_fig is not None:
|
688 |
-
logger.info("Chart generated from second-pass SQL execution.")
|
689 |
-
else:
|
690 |
-
logger.info("No DB connection on second pass; skipping.")
|
691 |
except Exception as e:
|
692 |
-
logger.error(f"
|
693 |
-
|
694 |
-
|
695 |
-
|
696 |
-
|
697 |
-
|
698 |
-
|
699 |
-
|
700 |
-
|
701 |
-
|
702 |
-
|
703 |
-
|
704 |
-
|
705 |
-
|
706 |
-
|
707 |
-
|
708 |
-
|
709 |
-
|
710 |
-
|
711 |
-
|
712 |
-
|
713 |
-
|
714 |
-
|
715 |
-
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
|
724 |
-
|
725 |
-
|
726 |
-
|
727 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
728 |
try:
|
729 |
-
|
|
|
|
|
730 |
except Exception:
|
731 |
continue
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
736 |
-
|
737 |
-
|
738 |
-
|
739 |
-
|
740 |
-
|
741 |
-
|
742 |
-
|
743 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
744 |
|
745 |
# Update the assistant's message with the response
|
746 |
assistant_message["content"] = response_text
|
|
|
572 |
response_text = str(response)
|
573 |
|
574 |
logger.info(f"Extracted response text: {response_text[:200]}...")
|
|
|
575 |
# Check if the response contains an SQL query and it truly looks like SQL
|
576 |
sql_query = extract_sql_query(response_text)
|
577 |
if sql_query and looks_like_sql(sql_query):
|
|
|
579 |
# Execute the query and update the response
|
580 |
db_connection, _ = setup_database_connection()
|
581 |
if db_connection:
|
|
|
|
|
|
|
|
|
|
|
|
|
582 |
try:
|
583 |
+
query_result = execute_sql_query(sql_query, db_connection)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
584 |
|
585 |
+
# Add the query and its result to the response
|
586 |
+
response_text += f"\n\n### 🔍 Resultado de la consulta:\n```sql\n{sql_query}\n```\n\n{query_result}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
587 |
|
588 |
+
# Try to generate an interactive chart if the result is tabular
|
589 |
+
if isinstance(query_result, str) and '|' in query_result and '---' in query_result:
|
|
|
|
|
|
|
590 |
try:
|
591 |
+
# Clean up the markdown table
|
592 |
+
lines = [line.strip() for line in query_result.split('\n')
|
593 |
+
if line.strip() and '---' not in line and '|' in line]
|
594 |
+
if len(lines) > 1: # At least header + 1 data row
|
595 |
+
# Get column names from the first line
|
596 |
+
columns = [col.strip() for col in lines[0].split('|')[1:-1]]
|
597 |
+
# Get data rows
|
598 |
+
data = []
|
599 |
+
for line in lines[1:]:
|
600 |
+
values = [val.strip() for val in line.split('|')[1:-1]]
|
601 |
+
if len(values) == len(columns):
|
602 |
+
data.append(dict(zip(columns, values)))
|
603 |
+
|
604 |
+
if data and len(columns) >= 2:
|
605 |
+
# Determine chart type from user's question
|
606 |
+
_, desired_type = detect_chart_preferences(question)
|
607 |
+
|
608 |
+
# Choose x/y columns (assume first is category, second numeric)
|
609 |
+
x_col = columns[0]
|
610 |
+
y_col = columns[1]
|
611 |
+
|
612 |
+
# Coerce numeric values for y
|
613 |
+
for row in data:
|
614 |
try:
|
615 |
+
row[y_col] = float(re.sub(r"[^0-9.\-]", "", str(row[y_col])))
|
|
|
|
|
616 |
except Exception:
|
617 |
pass
|
618 |
+
|
619 |
+
chart_fig = generate_chart(
|
620 |
+
data=data,
|
621 |
+
chart_type=desired_type,
|
622 |
+
x=x_col,
|
623 |
+
y=y_col,
|
624 |
+
title=f"{y_col} por {x_col}"
|
625 |
+
)
|
626 |
+
if chart_fig is not None:
|
627 |
+
logger.info(
|
628 |
+
f"Chart generated from SQL table: type={desired_type}, x={x_col}, y={y_col}, rows={len(data)}"
|
629 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
630 |
except Exception as e:
|
631 |
+
logger.error(f"Error generating chart: {str(e)}", exc_info=True)
|
632 |
+
# Don't fail the whole request if chart generation fails
|
633 |
+
response_text += "\n\n⚠️ No se pudo generar la visualización de los datos."
|
634 |
+
except Exception as e:
|
635 |
+
logger.error(f"Error handling SQL result: {e}", exc_info=True)
|
636 |
+
response_text += "\n\n⚠️ Ocurrió un error al procesar la consulta."
|
637 |
+
else:
|
638 |
+
response_text += "\n\n⚠️ No se pudo conectar a la base de datos para ejecutar la consulta."
|
639 |
+
elif sql_query and not looks_like_sql(sql_query):
|
640 |
+
logger.info("Detected code block but it does not look like SQL; skipping execution.")
|
641 |
+
|
642 |
+
# If we still have no chart but the user clearly wants one,
|
643 |
+
# try a second pass to get ONLY a SQL query from the agent and execute it.
|
644 |
+
if chart_fig is None:
|
645 |
+
wants_chart, default_type = detect_chart_preferences(question)
|
646 |
+
if wants_chart:
|
647 |
+
try:
|
648 |
+
logger.info("Second pass: asking agent for ONLY SQL query in fenced block.")
|
649 |
+
sql_only_prompt = (
|
650 |
+
"Devuelve SOLO la consulta SQL en un bloque ```sql``` para responder a: "
|
651 |
+
f"{question}. No incluyas explicación ni texto adicional."
|
652 |
+
)
|
653 |
+
sql_only_resp = await agent.ainvoke({"input": sql_only_prompt})
|
654 |
+
sql_only_text = str(sql_only_resp)
|
655 |
+
sql_query2 = extract_sql_query(sql_only_text)
|
656 |
+
if sql_query2 and looks_like_sql(sql_query2):
|
657 |
+
logger.info(f"Second pass SQL detected: {sql_query2}")
|
658 |
+
db_connection, _ = setup_database_connection()
|
659 |
+
if db_connection:
|
660 |
+
query_result = execute_sql_query(sql_query2, db_connection)
|
661 |
+
# Try to parse table-like text into DataFrame if possible
|
662 |
+
data = None
|
663 |
+
if isinstance(query_result, str):
|
664 |
+
try:
|
665 |
+
import pandas as pd
|
666 |
+
df = pd.read_csv(io.StringIO(query_result), sep="|")
|
667 |
+
data = df
|
668 |
+
except Exception:
|
669 |
+
pass
|
670 |
+
# As a fallback, don't rely on text table; just skip charting here
|
671 |
+
if data is not None and hasattr(data, "empty") and not data.empty:
|
672 |
+
# Heuristics: choose first column as x and second as y if numeric
|
673 |
+
x_col = data.columns[0]
|
674 |
+
# pick first numeric column different to x
|
675 |
+
y_col = None
|
676 |
+
for col in data.columns[1:]:
|
677 |
try:
|
678 |
+
pd.to_numeric(data[col])
|
679 |
+
y_col = col
|
680 |
+
break
|
681 |
except Exception:
|
682 |
continue
|
683 |
+
if y_col:
|
684 |
+
desired_type = default_type
|
685 |
+
chart_fig = generate_chart(
|
686 |
+
data=data,
|
687 |
+
chart_type=desired_type,
|
688 |
+
x=x_col,
|
689 |
+
y=y_col,
|
690 |
+
title=f"{y_col} por {x_col}"
|
691 |
+
)
|
692 |
+
if chart_fig is not None:
|
693 |
+
logger.info("Chart generated from second-pass SQL execution.")
|
694 |
+
else:
|
695 |
+
logger.info("No DB connection on second pass; skipping.")
|
696 |
+
except Exception as e:
|
697 |
+
logger.error(f"Second-pass SQL synthesis failed: {e}")
|
698 |
+
|
699 |
+
# Fallback: if user asked for a chart and we didn't get SQL or chart yet,
|
700 |
+
# parse the most recent assistant text for lines like "LABEL: NUMBER" (bulleted or plain).
|
701 |
+
if chart_fig is None:
|
702 |
+
wants_chart, desired_type = detect_chart_preferences(question)
|
703 |
+
if wants_chart:
|
704 |
+
# Find the most recent assistant message with usable numeric pairs
|
705 |
+
candidate_text = ""
|
706 |
+
if chat_history:
|
707 |
+
for pair in reversed(chat_history):
|
708 |
+
if len(pair) >= 2 and isinstance(pair[1], str) and pair[1].strip():
|
709 |
+
candidate_text = pair[1]
|
710 |
+
break
|
711 |
+
# Also consider current response_text as a data source
|
712 |
+
if not candidate_text and isinstance(response_text, str) and response_text.strip():
|
713 |
+
candidate_text = response_text
|
714 |
+
if candidate_text:
|
715 |
+
raw_lines = candidate_text.split('\n')
|
716 |
+
# Normalize lines: strip bullets and markdown symbols
|
717 |
+
norm_lines = []
|
718 |
+
for l in raw_lines:
|
719 |
+
s = l.strip()
|
720 |
+
if not s:
|
721 |
+
continue
|
722 |
+
s = s.lstrip("•*-\t ")
|
723 |
+
# Remove surrounding markdown emphasis from labels later
|
724 |
+
norm_lines.append(s)
|
725 |
+
data = []
|
726 |
+
for l in norm_lines:
|
727 |
+
# Accept patterns like "**LABEL**: 123" or "LABEL: 1,234"
|
728 |
+
m = re.match(r"^(.+?):\s*([0-9][0-9.,]*)$", l)
|
729 |
+
if m:
|
730 |
+
label = m.group(1).strip()
|
731 |
+
# Strip common markdown emphasis
|
732 |
+
label = re.sub(r"[*_`]+", "", label).strip()
|
733 |
+
try:
|
734 |
+
val = float(m.group(2).replace(',', ''))
|
735 |
+
except Exception:
|
736 |
+
continue
|
737 |
+
data.append({"label": label, "value": val})
|
738 |
+
logger.info(f"Fallback parse from text: extracted {len(data)} items for potential chart")
|
739 |
+
if len(data) >= 2:
|
740 |
+
chart_fig = generate_chart(
|
741 |
+
data=data,
|
742 |
+
chart_type=desired_type,
|
743 |
+
x="label",
|
744 |
+
y="value",
|
745 |
+
title="Distribución"
|
746 |
+
)
|
747 |
+
if chart_fig is not None:
|
748 |
+
logger.info(f"Chart generated from text fallback: type={desired_type}, items={len(data)}")
|
749 |
|
750 |
# Update the assistant's message with the response
|
751 |
assistant_message["content"] = response_text
|