Jeremy Live
commited on
Commit
·
70c3587
1
Parent(s):
390ac6e
memory Heurísticamente
Browse files
app.py
CHANGED
@@ -466,6 +466,22 @@ def detect_chart_preferences(question: str) -> Tuple[bool, str]:
|
|
466 |
except Exception:
|
467 |
return False, "bar"
|
468 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
469 |
def generate_plot(data, x_col, y_col, title, x_label, y_label):
|
470 |
"""Generate a plot from data and return the file path."""
|
471 |
plt.figure(figsize=(10, 6))
|
@@ -537,6 +553,20 @@ async def stream_agent_response(question: str, chat_history: List[List[str]], se
|
|
537 |
# Add current user's question
|
538 |
user_message = HumanMessage(content=question)
|
539 |
messages.append(user_message)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
540 |
|
541 |
active_agent = session_agent if session_agent is not None else agent
|
542 |
if not active_agent:
|
|
|
466 |
except Exception:
|
467 |
return False, "bar"
|
468 |
|
469 |
+
def is_db_intent(question: str) -> bool:
|
470 |
+
"""Heurísticamente detecta si la intención del usuario requiere la BD/SQL."""
|
471 |
+
try:
|
472 |
+
q = (question or "").lower()
|
473 |
+
keywords = [
|
474 |
+
"sql", "consulta", "query", "tabla", "tablas", "columna",
|
475 |
+
"columnas", "registro", "registros", "bd", "base de datos",
|
476 |
+
"database", "select", "insert", "update", "delete", "from",
|
477 |
+
"where", "join", "group by", "order by", "count(", "avg(",
|
478 |
+
"sum(", "min(", "max(", "gráfico" , "grafico", "gráfica",
|
479 |
+
"grafica", "visualización", "visualizacion"
|
480 |
+
]
|
481 |
+
return any(k in q for k in keywords)
|
482 |
+
except Exception:
|
483 |
+
return False
|
484 |
+
|
485 |
def generate_plot(data, x_col, y_col, title, x_label, y_label):
|
486 |
"""Generate a plot from data and return the file path."""
|
487 |
plt.figure(figsize=(10, 6))
|
|
|
553 |
# Add current user's question
|
554 |
user_message = HumanMessage(content=question)
|
555 |
messages.append(user_message)
|
556 |
+
|
557 |
+
# If the user is NOT asking about DB/SQL and not requesting a chart,
|
558 |
+
# answer conversationally using the raw LLM with the accumulated history.
|
559 |
+
wants_chart, _ = detect_chart_preferences(question)
|
560 |
+
if not is_db_intent(question) and not wants_chart:
|
561 |
+
try:
|
562 |
+
llm_messages = prev_messages + [user_message]
|
563 |
+
llm_resp = await llm.ainvoke(llm_messages)
|
564 |
+
if isinstance(llm_resp, (AIMessage,)) and getattr(llm_resp, "content", None):
|
565 |
+
return llm_resp.content, None
|
566 |
+
return str(llm_resp), None
|
567 |
+
except Exception as e:
|
568 |
+
logger.error(f"Conversational LLM failed: {e}")
|
569 |
+
# Fallback to agent path if LLM direct fails
|
570 |
|
571 |
active_agent = session_agent if session_agent is not None else agent
|
572 |
if not active_agent:
|