Skip to content

Commit

Permalink
refactor: move prompt string generation in Memory to a non-overload…
Browse files Browse the repository at this point in the history
…ed function (letta-ai#1683)
  • Loading branch information
sarahwooders authored Aug 27, 2024
2 parents 6528130 + 37466da commit 3f15b62
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
7 changes: 3 additions & 4 deletions memgpt/agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime
import inspect
import json
import traceback
from typing import List, Literal, Optional, Tuple, Union

Expand Down Expand Up @@ -108,7 +107,7 @@ def compile_system_message(
archival_memory=archival_memory,
recall_memory=recall_memory,
)
full_memory_string = memory_metadata_string + "\n" + str(in_context_memory)
full_memory_string = memory_metadata_string + "\n" + in_context_memory.compile()

# Add to the variables list to inject
variables[IN_CONTEXT_MEMORY_KEYWORD] = full_memory_string
Expand Down Expand Up @@ -217,7 +216,7 @@ def __init__(
# Initialize the memory object
self.memory = self.agent_state.memory
assert isinstance(self.memory, Memory), f"Memory object is not of type Memory: {type(self.memory)}"
printd("Initialized memory object", self.memory)
printd("Initialized memory object", self.memory.compile())

# Interface must implement:
# - internal_monologue
Expand Down Expand Up @@ -993,7 +992,7 @@ def rebuild_memory(self, force=False, update_timestamp=True, ms: Optional[Metada
curr_system_message = self.messages[0] # this is the system + memory bank, not just the system prompt

# NOTE: This is a hacky way to check if the memory has changed
memory_repr = str(self.memory)
memory_repr = self.memory.compile()
if not force and memory_repr == curr_system_message["content"][-(len(memory_repr)) :]:
printd(f"Memory has not changed, not rebuilding system")
return
Expand Down
4 changes: 2 additions & 2 deletions memgpt/schemas/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def load(cls, state: dict):
obj.memory[key] = Block(**value)
return obj

def __str__(self) -> str:
"""Representation of the memory in-context"""
def compile(self) -> str:
"""Generate a string representation of the memory in-context"""
section_strs = []
for section, module in self.memory.items():
section_strs.append(f'<{section} characters="{len(module)}/{module.limit}">\n{module.value}\n</{section}>')
Expand Down
2 changes: 1 addition & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def test_memory(client, agent):
# _reset_config()

memory_response = client.get_in_context_memory(agent_id=agent.id)
print("MEMORY", memory_response)
print("MEMORY", memory_response.compile())

updated_memory = {"human": "Updated human memory", "persona": "Updated persona memory"}
client.update_in_context_memory(agent_id=agent.id, section="human", value=updated_memory["human"])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def core_memory_clear(self: Agent):

# initial memory
initial_memory = client.get_in_context_memory(agent.id)
print("initial memory", initial_memory)
print("initial memory", initial_memory.compile())
human = initial_memory.get_block("human")
persona = initial_memory.get_block("persona")
print("Initial memory:", human, persona)
Expand Down

0 comments on commit 3f15b62

Please sign in to comment.