Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: include most recent archival passages in metadata message (#1211)
Kevin Lin35186880eea5
diff --git a/letta/agent.py b/letta/agent.pyindex b8c67cc2c..023157d0a 100644--- a/letta/agent.py+++ b/letta/agent.py@@ -1195,6 +1195,7 @@ class Agent(BaseAgent):memory_edit_timestamp=get_utc_time(),previous_message_count=self.message_manager.size(actor=self.user, agent_id=self.agent_state.id),archival_memory_size=self.agent_manager.passage_size(actor=self.user, agent_id=self.agent_state.id),+ recent_passages=self.agent_manager.list_passages(actor=self.user, agent_id=self.agent_state.id, ascending=False, limit=10),)num_tokens_external_memory_summary = count_tokens(external_memory_summary)diff --git a/letta/services/agent_manager.py b/letta/services/agent_manager.pyindex 2a6d8aaa2..12ef7790f 100644--- a/letta/services/agent_manager.py+++ b/letta/services/agent_manager.py@@ -598,6 +598,7 @@ class AgentManager:system_prompt=agent_state.system,in_context_memory=agent_state.memory,in_context_memory_last_edit=memory_edit_timestamp,+ recent_passages=self.list_passages(actor=actor, agent_id=agent_id, ascending=False, limit=10),)diff = united_diff(curr_system_message_openai["content"], new_system_message_str)@@ -768,7 +769,9 @@ class AgentManager:# Commit the changesagent.update(session, actor=actor)- # Add system messsage alert to agent+ # Force rebuild of system prompt so that the agent is updated with passage count+ # and recent passages and add system message alert to agent+ self.rebuild_system_prompt(agent_id=agent_id, actor=actor, force=True)self.append_system_message(agent_id=agent_id,content=DATA_SOURCE_ATTACH_ALERT,diff --git a/letta/services/helpers/agent_manager_helper.py b/letta/services/helpers/agent_manager_helper.pyindex f9549d767..e1d3c91a9 100644--- a/letta/services/helpers/agent_manager_helper.py+++ b/letta/services/helpers/agent_manager_helper.py@@ -13,6 +13,7 @@ from letta.schemas.agent import AgentState, AgentTypefrom letta.schemas.enums import MessageRolefrom letta.schemas.memory import Memoryfrom letta.schemas.message import Message, MessageCreate, TextContent+from letta.schemas.passage import Passage as PydanticPassagefrom letta.schemas.tool_rule import ToolRulefrom letta.schemas.user import Userfrom letta.system import get_initial_boot_messages, get_login_event@@ -99,7 +100,10 @@ def derive_system_message(agent_type: AgentType, system: Optional[str] = None):# TODO: This code is kind of wonky and deserves a rewritedef compile_memory_metadata_block(- memory_edit_timestamp: datetime.datetime, previous_message_count: int = 0, archival_memory_size: int = 0+ memory_edit_timestamp: datetime.datetime,+ previous_message_count: int = 0,+ archival_memory_size: int = 0,+ recent_passages: List[PydanticPassage] = None,) -> str:# Put the timestamp in the local timezone (mimicking get_local_time())timestamp_str = memory_edit_timestamp.astimezone().strftime("%Y-%m-%d %I:%M:%S %p %Z%z").strip()@@ -110,6 +114,11 @@ def compile_memory_metadata_block(f"### Memory [last modified: {timestamp_str}]",f"{previous_message_count} previous messages between you and the user are stored in recall memory (use functions to access them)",f"{archival_memory_size} total memories you created are stored in archival memory (use functions to access them)",+ (+ f"Most recent archival passages {len(recent_passages)} recent passages: {[passage.text for passage in recent_passages]}"+ if recent_passages is not None+ else ""+ ),"\nCore memory shown below (limited in size, additional information stored in archival / recall memory):",])@@ -146,6 +155,7 @@ def compile_system_message(template_format: Literal["f-string", "mustache", "jinja2"] = "f-string",previous_message_count: int = 0,archival_memory_size: int = 0,+ recent_passages: Optional[List[PydanticPassage]] = None,) -> str:"""Prepare the final/full system message that will be fed into the LLM API@@ -170,6 +180,7 @@ def compile_system_message(memory_edit_timestamp=in_context_memory_last_edit,previous_message_count=previous_message_count,archival_memory_size=archival_memory_size,+ recent_passages=recent_passages,)full_memory_string = memory_metadata_string + "\n" + in_context_memory.compile()