Jeremy Live
commited on
Commit
·
fa719ce
1
Parent(s):
22e000d
memory chatbot upgrade
Browse files
app.py
CHANGED
@@ -468,19 +468,23 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
468 |
f"3. El modelo de lenguaje esté disponible\n\n"
|
469 |
f"Error: {agent_error}"
|
470 |
)
|
471 |
-
return error_msg, None
|
472 |
|
473 |
# Update the agent's memory with the full conversation history
|
474 |
try:
|
475 |
-
#
|
476 |
if hasattr(agent, 'memory') and agent.memory is not None:
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
|
|
|
|
|
|
|
|
484 |
except Exception as e:
|
485 |
logger.error(f"Error updating agent memory: {str(e)}", exc_info=True)
|
486 |
|
@@ -726,7 +730,7 @@ async def stream_agent_response(question: str, chat_history: List[List[str]]) ->
|
|
726 |
error_msg = f"## ❌ Error\n\nOcurrió un error al procesar tu solicitud:\n\n```\n{str(e)}\n```"
|
727 |
logger.error(f"Error in stream_agent_response: {str(e)}", exc_info=True)
|
728 |
# Return error message and no chart
|
729 |
-
return error_msg, None
|
730 |
|
731 |
# Custom CSS for the app
|
732 |
custom_css = """
|
|
|
468 |
f"3. El modelo de lenguaje esté disponible\n\n"
|
469 |
f"Error: {agent_error}"
|
470 |
)
|
471 |
+
return error_msg, None, None
|
472 |
|
473 |
# Update the agent's memory with the full conversation history
|
474 |
try:
|
475 |
+
# Hydrate agent memory only if it's empty; do NOT clear existing memory.
|
476 |
if hasattr(agent, 'memory') and agent.memory is not None:
|
477 |
+
try:
|
478 |
+
existing_messages = getattr(getattr(agent.memory, 'chat_memory', None), 'messages', []) or []
|
479 |
+
except Exception:
|
480 |
+
existing_messages = []
|
481 |
+
if not existing_messages and chat_history:
|
482 |
+
for pair in chat_history:
|
483 |
+
try:
|
484 |
+
user_prev, bot_prev = pair[0], pair[1] if len(pair) > 1 else ""
|
485 |
+
agent.memory.save_context({"input": user_prev}, {"output": bot_prev})
|
486 |
+
except Exception:
|
487 |
+
continue
|
488 |
except Exception as e:
|
489 |
logger.error(f"Error updating agent memory: {str(e)}", exc_info=True)
|
490 |
|
|
|
730 |
error_msg = f"## ❌ Error\n\nOcurrió un error al procesar tu solicitud:\n\n```\n{str(e)}\n```"
|
731 |
logger.error(f"Error in stream_agent_response: {str(e)}", exc_info=True)
|
732 |
# Return error message and no chart
|
733 |
+
return error_msg, None, None
|
734 |
|
735 |
# Custom CSS for the app
|
736 |
custom_css = """
|