Skip to content

Commit

Permalink
fix-storage-ag-2662
Browse files Browse the repository at this point in the history
  • Loading branch information
ysolanky committed Feb 6, 2025
1 parent a02d2af commit 2621bcc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 31 deletions.
20 changes: 15 additions & 5 deletions libs/agno/agno/agent/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2124,19 +2124,29 @@ def get_run_messages(

# 3. Add history to run_messages
if self.add_history_to_messages:
from copy import deepcopy

history: List[Message] = self.memory.get_messages_from_last_n_runs(
last_n=self.num_history_responses, skip_role=self.get_system_message_role()
)
if len(history) > 0:
logger.debug(f"Adding {len(history)} messages from history")
# Create a deep copy of the history messages to avoid modifying the original messages
history_copy = [deepcopy(msg) for msg in history]

# Tag each message as coming from history
for _msg in history_copy:
_msg.from_history = True

logger.debug(f"Adding {len(history_copy)} messages from history")

if self.run_response.extra_data is None:
self.run_response.extra_data = RunResponseExtraData(history=history)
self.run_response.extra_data = RunResponseExtraData(history=history_copy)
else:
if self.run_response.extra_data.history is None:
self.run_response.extra_data.history = history
self.run_response.extra_data.history = history_copy
else:
self.run_response.extra_data.history.extend(history)
run_messages.messages += history
self.run_response.extra_data.history.extend(history_copy)
run_messages.messages += history_copy

# 4.Add user message to run_messages
user_message: Optional[Message] = None
Expand Down
50 changes: 24 additions & 26 deletions libs/agno/agno/memory/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,39 +134,37 @@ def get_messages(self) -> List[Dict[str, Any]]:
def get_messages_from_last_n_runs(
self, last_n: Optional[int] = None, skip_role: Optional[str] = None
) -> List[Message]:
"""Returns the messages from the last_n runs
"""Returns the messages from the last_n runs, excluding previously tagged history messages.
Args:
last_n: The number of runs to return from the end of the conversation.
skip_role: Skip messages with this role.
Returns:
A list of Messages in the last_n runs.
A list of Messages from the specified runs, excluding history messages.
"""
if last_n is None:
logger.debug("Getting messages from all previous runs")
messages_from_all_history = []
for prev_run in self.runs:
if prev_run.response and prev_run.response.messages:
if skip_role:
prev_run_messages = [m for m in prev_run.response.messages if m.role != skip_role]
else:
prev_run_messages = prev_run.response.messages
messages_from_all_history.extend(prev_run_messages)
logger.debug(f"Messages from previous runs: {len(messages_from_all_history)}")
return messages_from_all_history

logger.debug(f"Getting messages from last {last_n} runs")
messages_from_last_n_history = []
for prev_run in self.runs[-last_n:]:
if prev_run.response and prev_run.response.messages:
if skip_role:
prev_run_messages = [m for m in prev_run.response.messages if m.role != skip_role]
else:
prev_run_messages = prev_run.response.messages
messages_from_last_n_history.extend(prev_run_messages)
logger.debug(f"Messages from last {last_n} runs: {len(messages_from_last_n_history)}")
return messages_from_last_n_history
if not self.runs:
return []

runs_to_process = self.runs if last_n is None else self.runs[-last_n:]
messages_from_history = []

for run in runs_to_process:
if not (run.response and run.response.messages):
continue

for message in run.response.messages:
# Skip messages with specified role
if skip_role and message.role == skip_role:
continue
# Skip messages that were tagged as history in previous runs
if hasattr(message, "from_history") and message.from_history:
continue

messages_from_history.append(message)

logger.debug(f"Getting messages from previous runs: {len(messages_from_history)}")
return messages_from_history

def get_message_pairs(
self, user_role: str = "user", assistant_role: Optional[List[str]] = None
Expand Down
2 changes: 2 additions & 0 deletions libs/agno/agno/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class Message(BaseModel):
stop_after_tool_call: bool = False
# When True, the message will be added to the agent's memory.
add_to_agent_memory: bool = True
# This flag is enabled when a message is fetched from the agent's memory.
from_history: bool = False
# Metrics for the message.
metrics: Dict[str, Any] = Field(default_factory=dict)
# The references added to the message for RAG
Expand Down

0 comments on commit 2621bcc

Please sign in to comment.