Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix rethink memory non existent block error #2181

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions letta/offline_memory_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def trigger_rethink_memory(agent_state: "AgentState", message: Optional[str]) ->

"""
from letta import create_client

client = create_client()
agents = client.list_agents()
for agent in agents:
Expand All @@ -35,7 +34,6 @@ def trigger_rethink_memory_convo(agent_state: "AgentState", message: Optional[st

"""
from letta import create_client

client = create_client()
recent_convo = "".join([str(message) for message in agent_state.messages])[
-2000:
Expand All @@ -61,10 +59,14 @@ def rethink_memory_convo(agent_state: "AgentState", new_memory: str, target_bloc
Returns:
Optional[str]: None is always returned as this function does not produce a response.
"""
from letta import create_client
client = create_client()
if target_block_label is not None:
if agent_state.memory.get_block(target_block_label) is None:
agent_state.memory.create_block(label=target_block_label, value=new_memory)
agent_state.memory.update_block_value(label=target_block_label, value=new_memory)
if target_block_label not in agent_state.memory.list_block_labels():
new_block = client.create_block(label=target_block_label, value=new_memory)
agent_state.memory.set_block(new_block)
else:
agent_state.memory.update_block_value(label=target_block_label, value=new_memory)
return None


Expand All @@ -81,14 +83,18 @@ def rethink_memory(agent_state: "AgentState", new_memory: str, target_block_labe
Returns:
Optional[str]: None is always returned as this function does not produce a response.
"""

from letta import create_client
client = create_client()
if target_block_label is not None:
if agent_state.memory.get_block(target_block_label) is None:
agent_state.memory.create_block(label=target_block_label, value=new_memory)
agent_state.memory.update_block_value(label=target_block_label, value=new_memory)
if target_block_label not in agent_state.memory.list_block_labels():
new_block = client.create_block(label=target_block_label, value=new_memory)
agent_state.memory.set_block(new_block)
else:
agent_state.memory.update_block_value(label=target_block_label, value=new_memory)
return None



def finish_rethinking_memory(agent_state: "AgentState") -> Optional[str]: # type: ignore
"""
This function is called when the agent is done rethinking the memory.
Expand Down
27 changes: 25 additions & 2 deletions tests/test_offline_memory_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,30 @@ def client():

yield client


@pytest.fixture(autouse=True)
def clear_agents(client):
for agent in client.list_agents():
client.delete_agent(agent.id)

def test_rethink_memory_new_block(client):
"""
Test that when rethink memory is called with a block that does not exist in the agent,
the new block is created.
"""
client.create_agent()
agent = client.create_agent(
agent_type=AgentType.memgpt_agent,
system=gpt_system.get_system_text("memgpt_convo_only"),
llm_config=LLMConfig.default_config("gpt-4"),
embedding_config=EmbeddingConfig.default_config("text-embedding-ada-002"),
include_base_tools=False,
)
assert set(agent.memory.list_block_labels()) == {"persona", "human"}
rethink_memory(
agent_state=agent, new_memory="I am a new memory block content", source_block_label="human", target_block_label="new_memory_block"
)
assert set(agent.memory.list_block_labels()) == {"persona", "human", "new_memory_block"}


def test_ripple_edit(client, mock_e2b_api_key_none):
trigger_rethink_memory_tool = client.create_or_update_tool(trigger_rethink_memory)
Expand Down Expand Up @@ -71,7 +89,12 @@ def test_ripple_edit(client, mock_e2b_api_key_none):
)
assert conversation_agent is not None

assert set(conversation_agent.memory.list_block_labels()) == {"persona", "human", "fact_block", "rethink_memory_block"}
assert set(conversation_agent.memory.list_block_labels()) == {
"persona",
"human",
"fact_block",
"rethink_memory_block",
}

rethink_memory_tool = client.create_tool(rethink_memory)
finish_rethinking_memory_tool = client.create_tool(finish_rethinking_memory)
Expand Down
Loading