Commit
路
938934c
1
Parent(s):
1fe11cb
memory upgrade
Browse files
app.py
CHANGED
|
@@ -411,6 +411,8 @@ def convert_to_messages_format(chat_history):
|
|
| 411 |
|
| 412 |
async def stream_agent_response(question: str, chat_history: List) -> List[Dict]:
|
| 413 |
"""Procesa la pregunta del usuario y devuelve la respuesta del agente con memoria de conversaci贸n."""
|
|
|
|
|
|
|
| 414 |
# Initialize response
|
| 415 |
response_text = ""
|
| 416 |
messages = []
|
|
@@ -439,6 +441,22 @@ async def stream_agent_response(question: str, chat_history: List) -> List[Dict]
|
|
| 439 |
)
|
| 440 |
assistant_message = {"role": "assistant", "content": error_msg}
|
| 441 |
return [assistant_message]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 442 |
|
| 443 |
try:
|
| 444 |
# Add empty assistant message that will be updated
|
|
|
|
| 411 |
|
| 412 |
async def stream_agent_response(question: str, chat_history: List) -> List[Dict]:
|
| 413 |
"""Procesa la pregunta del usuario y devuelve la respuesta del agente con memoria de conversaci贸n."""
|
| 414 |
+
global agent # Make sure we can modify the agent's memory
|
| 415 |
+
|
| 416 |
# Initialize response
|
| 417 |
response_text = ""
|
| 418 |
messages = []
|
|
|
|
| 441 |
)
|
| 442 |
assistant_message = {"role": "assistant", "content": error_msg}
|
| 443 |
return [assistant_message]
|
| 444 |
+
|
| 445 |
+
# Update the agent's memory with the full conversation history
|
| 446 |
+
try:
|
| 447 |
+
# Clear existing memory
|
| 448 |
+
if hasattr(agent, 'memory') and agent.memory is not None:
|
| 449 |
+
agent.memory.clear()
|
| 450 |
+
|
| 451 |
+
# Add all messages to memory
|
| 452 |
+
for i in range(0, len(messages)-1, 2): # Process in pairs (user, assistant)
|
| 453 |
+
if i+1 < len(messages):
|
| 454 |
+
agent.memory.save_context(
|
| 455 |
+
{"input": messages[i].content},
|
| 456 |
+
{"output": messages[i+1].content}
|
| 457 |
+
)
|
| 458 |
+
except Exception as e:
|
| 459 |
+
logger.error(f"Error updating agent memory: {str(e)}", exc_info=True)
|
| 460 |
|
| 461 |
try:
|
| 462 |
# Add empty assistant message that will be updated
|