Jeremy Live commited on
Commit
ccbfee5
1 Parent(s): f2961b7

UX de respuesta text segunda grafica

Browse files
Files changed (1) hide show
  1. app.py +36 -3
app.py CHANGED
@@ -542,15 +542,27 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
542
  except Exception as e:
543
  logger.error(f"Error updating agent memory: {str(e)}", exc_info=True)
544
 
545
- try:
546
  # Add empty assistant message that will be updated
547
  assistant_message = {"role": "assistant", "content": ""}
548
  messages.append(assistant_message)
549
 
550
  # Execute the agent with proper error handling
551
  try:
552
- # Let the agent use its memory; don't pass raw chat_history
553
- response = await agent.ainvoke({"input": question})
 
 
 
 
 
 
 
 
 
 
 
 
554
  logger.info(f"Agent response type: {type(response)}")
555
  logger.info(f"Agent response content: {str(response)[:500]}...")
556
 
@@ -757,6 +769,27 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
757
  else:
758
  message_content = str(assistant_message)
759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
760
  # Return the assistant's response and an optional interactive chart figure
761
  if chart_fig is None:
762
  logger.info("No chart generated for this turn.")
 
542
  except Exception as e:
543
  logger.error(f"Error updating agent memory: {str(e)}", exc_info=True)
544
 
545
+ try:
546
  # Add empty assistant message that will be updated
547
  assistant_message = {"role": "assistant", "content": ""}
548
  messages.append(assistant_message)
549
 
550
  # Execute the agent with proper error handling
551
  try:
552
+ # If the user is asking for a chart, steer the agent to be concise
553
+ wants_chart, _desired = detect_chart_preferences(question)
554
+ input_text = question
555
+ if wants_chart:
556
+ input_text = (
557
+ "Instrucciones: Si el usuario pide una visualizaci贸n y los datos ya "
558
+ "est谩n en el historial, no pidas m谩s contexto. Responde brevemente "
559
+ "confirmando que generar谩s/has generado la gr谩fica y, si necesitas SQL, "
560
+ "devuelve solo la consulta en un bloque ```sql``` sin explicaciones.\n\n"
561
+ f"Pregunta del usuario: {question}"
562
+ )
563
+
564
+ # Let the agent use its memory; don't pass raw chat_history
565
+ response = await agent.ainvoke({"input": input_text})
566
  logger.info(f"Agent response type: {type(response)}")
567
  logger.info(f"Agent response content: {str(response)[:500]}...")
568
 
 
769
  else:
770
  message_content = str(assistant_message)
771
 
772
+ # If we generated a chart but the model said it couldn't, replace with a friendly note
773
+ def looks_unhelpful(msg: str) -> bool:
774
+ if not isinstance(msg, str):
775
+ return False
776
+ patterns = [
777
+ r"no t(engo|iene) suficiente informaci贸n",
778
+ r"i don'?t have enough information",
779
+ r"need to know what data",
780
+ r"can you provide more context",
781
+ r"not enough info",
782
+ ]
783
+ text = msg.lower()
784
+ return any(re.search(p, text) for p in patterns)
785
+
786
+ if chart_fig is not None and looks_unhelpful(message_content):
787
+ message_content = (
788
+ "He generado la gr谩fica solicitada utilizando los datos m谩s "
789
+ "recientes de la conversaci贸n. La visualizaci贸n interactiva se "
790
+ "muestra abajo."
791
+ )
792
+
793
  # Return the assistant's response and an optional interactive chart figure
794
  if chart_fig is None:
795
  logger.info("No chart generated for this turn.")