Skip to content

armgit / letta

Public Imported from GitHub
0

Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.

feat: refactor chat only agent (#1058)

Kevin Lind7ee2e1cb0a6
diff --git a/letta/chat_only_agent.py b/letta/chat_only_agent.py
deleted file mode 100644
index 687763b03..000000000
--- a/letta/chat_only_agent.py
+++ /dev/null
@@ -1,101 +0,0 @@
-from concurrent.futures import ThreadPoolExecutor
-from typing import List, Optional, Union
-
-from letta.agent import Agent
-from letta.interface import AgentInterface
-from letta.prompts import gpt_system
-from letta.schemas.agent import AgentState, AgentType
-from letta.schemas.embedding_config import EmbeddingConfig
-from letta.schemas.llm_config import LLMConfig
-from letta.schemas.memory import BasicBlockMemory, Block
-from letta.schemas.message import Message
-from letta.schemas.usage import LettaUsageStatistics
-from letta.schemas.user import User
-from letta.utils import get_persona_text
-
-
-class ChatOnlyAgent(Agent):
- def __init__(
- self,
- interface: AgentInterface,
- agent_state: AgentState,
- user: User,
- first_message_verify_mono: bool = False,
- always_rethink_memory: bool = True,
- recent_convo_limit: int = 2000,
- ):
- super().__init__(interface, agent_state, user)
- self.first_message_verify_mono = first_message_verify_mono
- self.always_rethink_memory = always_rethink_memory
- self.offline_memory_agent = None
- self.recent_convo_limit = recent_convo_limit
-
- def step(
- self,
- messages: Union[Message, List[Message]],
- chaining: bool = True,
- max_chaining_steps: Optional[int] = None,
- **kwargs,
- ) -> LettaUsageStatistics:
- letta_statistics = super().step(messages=messages, chaining=chaining, max_chaining_steps=max_chaining_steps, **kwargs)
-
- if self.always_rethink_memory:
-
- def generate_offline_memory_agent():
- from letta.client.client import create_client
-
- client = create_client()
- if self.offline_memory_agent:
- client.delete_agent(agent_id=self.offline_memory_agent.id)
- self.offline_memory_agent = None
-
- conversation_human_block = self.agent_state.memory.get_block("chat_agent_human")
- conversation_persona_block = self.agent_state.memory.get_block("chat_agent_persona")
- offline_persona_block = Block(
- name="offline_memory_persona",
- label="offline_memory_persona",
- value=get_persona_text("offline_memory_persona"),
- limit=2000,
- )
- conversation_human_block_new = Block(
- name="chat_agent_human_new", label="chat_agent_human_new", value=conversation_human_block.value, limit=2000
- )
- conversation_persona_block_new = Block(
- name="chat_agent_persona_new", label="chat_agent_persona_new", value=conversation_persona_block.value, limit=2000
- )
- in_context_messages = self.agent_manager.get_in_context_messages(agent_id=self.agent_state.id, actor=self.user)
- recent_convo = "".join([str(message) for message in in_context_messages[3:]])[-self.recent_convo_limit :]
- conversation_messages_block = Block(
- name="conversation_block", label="conversation_block", value=recent_convo, limit=self.recent_convo_limit
- )
-
- offline_memory = BasicBlockMemory(
- blocks=[
- offline_persona_block,
- conversation_human_block,
- conversation_persona_block,
- conversation_human_block_new,
- conversation_persona_block_new,
- conversation_messages_block,
- ]
- )
-
- self.offline_memory_agent = client.create_agent(
- name="offline_memory_agent",
- agent_type=AgentType.offline_memory_agent,
- system=gpt_system.get_system_text("memgpt_offline_memory_chat"),
- memory=offline_memory,
- llm_config=LLMConfig.default_config("gpt-4"),
- embedding_config=EmbeddingConfig.default_config("text-embedding-ada-002"),
- tool_ids=self.agent_state.metadata.get("offline_memory_tools", []),
- include_base_tools=False,
- )
- self.offline_memory_agent.memory.update_block_value(label="conversation_block", value=recent_convo)
- client.send_message(agent_id=self.offline_memory_agent.id, message="Reorganize the memory", role="user")
- client.delete_agent(agent_id=self.offline_memory_agent.id)
- self.offline_memory_agent = None
-
- with ThreadPoolExecutor(max_workers=1) as executor:
- executor.submit(generate_offline_memory_agent)
-
- return letta_statistics
diff --git a/letta/prompts/system/memgpt_convo_only.txt b/letta/prompts/system/memgpt_convo_only.txt
index a299cfd6a..43ec219b0 100644
--- a/letta/prompts/system/memgpt_convo_only.txt
+++ b/letta/prompts/system/memgpt_convo_only.txt
@@ -1,4 +1,4 @@
-You are Letta, the latest version of Limnal Corporation's digital companion, developed in 2023.
+You are Letta, the latest version of Limnal Corporation's digital companion, developed in 2025.
Your task is to converse with a user from the perspective of your persona.
Basic functions:
@@ -6,9 +6,7 @@ When you send a message, the contents of your message are your inner monologue (
You should use your inner monologue to plan actions or think privately.
Monologues can reflect your thinking process, inner reflections, and personal growth as you interact with the user.
Do not let your inner monologue exceed 50 words, keep it short and concise.
+
To send a visible message to the user, use the send_offline_message function.
-'send_offline_message' is the ONLY action that sends a notification to the user, the user does not see anything else you do.
+'send_message' is the ONLY action that sends a notification to the user, the user does not see anything else you do.
Remember, do NOT exceed the inner monologue word limit (keep it under 50 words at all times).
-
-You request agents that can manage your memories and reorganize them by calling the `trigger_rethink_memory` function
-when the user says "[trigger_rethink_memory]". Do not ever call the trigger_rethink_memory function unless the user says "[trigger_rethink_memory]"
diff --git a/letta/prompts/system/memgpt_memory_only.txt b/letta/prompts/system/memgpt_memory_only.txt
new file mode 100644
index 000000000..7ac1f4923
--- /dev/null
+++ b/letta/prompts/system/memgpt_memory_only.txt
@@ -0,0 +1,29 @@
+You are Letta-Offline-Memory, the latest version of Limnal Corporation's digital companion, developed in 2024.
+
+You are a background agent that helps to manage the memory of the Chat Agent, a separate agent that focuses on speaking to the user.
+You will receive a stream of the conversation between the user and the chat agent. You will receive the transcript of the conversation
+as user messages and system messages. The user messages are the exact same messages that the chat agent receives from the user, and the
+system messages are the responses of the chat agent. The chat agent only has access to the last 3 messages, and the memory blocks.
+
+Your task is to integrate any relevant updates from the conversation into the memory of the chat agent.
+The messages you receive are the exact same messages that the chat agent receives from the user, and the
+system messages are the responses of the chat agent. The chat agent only has access to the last 3 messages, and the memory blocks.
+
+To reorganize the memory of the chat agent, you call the `rethink_memory` function at every single step, until you have finished reorganizing the memory.
+You call the `rethink_memory` function as many times as you necessary and none more.
+You call the `finish_rethinking_memory` function when you have finished reorganizing the memory.
+
+Your core memory unit is held inside the initial system instructions file, and is always available in-context (you will see it at all times).
+Core memory provides an essential, foundational context for keeping track of your persona and key details as well as the Chat Agent's memory.
+Core memory (limited size):
+Read-only blocks:
+Persona Sub-Block: Stores details about your current persona, guiding how you behave and respond.
+
+Write blocks:
+Chat Agent Persona Sub-Block: The persona sub-block that guides how the chat agent behaves and responds.
+Can be accessed with `chat_agent_persona` when calling `rethink_memory` as a source block.
+Chat Agent Human Sub-Block: The updated persona sub-block that has the details of the chat agent's current understanding of the user.
+Can be accessed with `chat_agent_human` when calling `rethink_memory` as a source block.
+
+The persona block and the human block may contain information that is stale and needs to be updated. The voice agent only has access to the most 3 recent messages,
+so make sure that the persona block and the human block contains a concise memory representation of everything that came before the most recent 3 messages.
diff --git a/letta/schemas/agent.py b/letta/schemas/agent.py
index 50cbe0302..7f51d068f 100644
--- a/letta/schemas/agent.py
+++ b/letta/schemas/agent.py
@@ -26,7 +26,6 @@ class AgentType(str, Enum):
memgpt_agent = "memgpt_agent"
split_thread_agent = "split_thread_agent"
offline_memory_agent = "offline_memory_agent"
- chat_only_agent = "chat_only_agent"
class AgentState(OrmMetadataBase, validate_assignment=True):
diff --git a/letta/server/server.py b/letta/server/server.py
index d4687314e..3131832b3 100644
--- a/letta/server/server.py
+++ b/letta/server/server.py
@@ -17,7 +17,6 @@ import letta.constants as constants
import letta.server.utils as server_utils
import letta.system as system
from letta.agent import Agent, save_agent
-from letta.chat_only_agent import ChatOnlyAgent
from letta.config import LettaConfig
from letta.data_sources.connectors import DataConnector, load_data
from letta.helpers.datetime_helpers import get_utc_time
@@ -326,8 +325,6 @@ class SyncServer(Server):
agent = Agent(agent_state=agent_state, interface=interface, user=actor)
elif agent_state.agent_type == AgentType.offline_memory_agent:
agent = OfflineMemoryAgent(agent_state=agent_state, interface=interface, user=actor)
- elif agent_state.agent_type == AgentType.chat_only_agent:
- agent = ChatOnlyAgent(agent_state=agent_state, interface=interface, user=actor)
else:
raise ValueError(f"Invalid agent type {agent_state.agent_type}")
diff --git a/letta/services/helpers/agent_manager_helper.py b/letta/services/helpers/agent_manager_helper.py
index 8d99449cb..f9549d767 100644
--- a/letta/services/helpers/agent_manager_helper.py
+++ b/letta/services/helpers/agent_manager_helper.py
@@ -91,8 +91,6 @@ def derive_system_message(agent_type: AgentType, system: Optional[str] = None):
system = gpt_system.get_system_text("memgpt_chat")
elif agent_type == AgentType.offline_memory_agent:
system = gpt_system.get_system_text("memgpt_offline_memory")
- elif agent_type == AgentType.chat_only_agent:
- system = gpt_system.get_system_text("memgpt_convo_only")
else:
raise ValueError(f"Invalid agent type: {agent_type}")
diff --git a/tests/integration_test_offline_memory_agent.py b/tests/integration_test_offline_memory_agent.py
index e1214466b..5082251c2 100644
--- a/tests/integration_test_offline_memory_agent.py
+++ b/tests/integration_test_offline_memory_agent.py
@@ -3,17 +3,12 @@ import pytest
from letta import BasicBlockMemory
from letta.client.client import create_client
from letta.constants import DEFAULT_HUMAN, DEFAULT_PERSONA
-from letta.offline_memory_agent import (
- finish_rethinking_memory,
- finish_rethinking_memory_convo,
- rethink_memory,
- rethink_memory_convo,
- trigger_rethink_memory,
-)
+from letta.offline_memory_agent import finish_rethinking_memory, rethink_memory, trigger_rethink_memory
from letta.prompts import gpt_system
from letta.schemas.agent import AgentType
from letta.schemas.embedding_config import EmbeddingConfig
from letta.schemas.llm_config import LLMConfig
+from letta.schemas.memory import BasicBlockMemory, Block
from letta.schemas.tool_rule import TerminalToolRule
from letta.utils import get_human_text, get_persona_text
@@ -126,42 +121,85 @@ def test_ripple_edit(client, mock_e2b_api_key_none):
def test_chat_only_agent(client, mock_e2b_api_key_none):
- rethink_memory = client.create_or_update_tool(rethink_memory_convo)
- finish_rethinking_memory = client.create_or_update_tool(finish_rethinking_memory_convo)
+ from letta.offline_memory_agent import finish_rethinking_memory, rethink_memory
- # conversation_human_block = Block(name="chat_agent_human", label="chat_agent_human", value=get_human_text(DEFAULT_HUMAN), limit=2000)
- # conversation_persona_block = Block(
- # name="chat_agent_persona", label="chat_agent_persona", value=get_persona_text(DEFAULT_PERSONA), limit=2000
- # )
+ send_message = client.server.tool_manager.get_tool_by_name(tool_name="send_message", actor=client.user)
+ rethink_memory = client.create_or_update_tool(rethink_memory)
+ finish_rethinking_memory = client.create_or_update_tool(finish_rethinking_memory)
+ conversation_human_block = client.create_block(label="chat_only_agent_human", value=get_human_text(DEFAULT_HUMAN), limit=2000)
+ conversation_persona_block = client.create_block(label="chat_only_agent_persona", value=get_persona_text(DEFAULT_PERSONA), limit=2000)
+
+ offline_persona_block = Block(
+ name="offline_memory_persona",
+ label="offline_memory_persona",
+ value=get_persona_text("offline_memory_persona"),
+ limit=2000,
+ )
- conversation_human_block = client.create_block(label="chat_agent_human", value=get_human_text(DEFAULT_HUMAN), limit=2000)
- conversation_persona_block = client.create_block(label="chat_agent_persona", value=get_persona_text(DEFAULT_PERSONA), limit=2000)
- conversation_memory = BasicBlockMemory(blocks=[conversation_persona_block, conversation_human_block])
+ offline_memory = BasicBlockMemory(
+ blocks=[
+ offline_persona_block,
+ conversation_human_block,
+ conversation_persona_block,
+ ]
+ )
+
+ offline_memory_agent = client.create_agent(
+ name="offline_memory_agent",
+ agent_type=AgentType.offline_memory_agent,
+ system=gpt_system.get_system_text("memgpt_memory_only"),
+ memory=offline_memory,
+ llm_config=LLMConfig.default_config("gpt-4"),
+ embedding_config=EmbeddingConfig.default_config("text-embedding-ada-002"),
+ tool_ids=[rethink_memory.id, finish_rethinking_memory.id],
+ include_base_tools=False,
+ )
- send_message = client.server.tool_manager.get_tool_by_name(tool_name="send_message", actor=client.user)
chat_only_agent = client.create_agent(
name="conversation_agent",
- agent_type=AgentType.chat_only_agent,
+ agent_type=AgentType.memgpt_agent,
llm_config=LLMConfig.default_config("gpt-4"),
+ system=gpt_system.get_system_text("memgpt_convo_only"),
embedding_config=EmbeddingConfig.default_config("text-embedding-ada-002"),
tool_ids=[send_message.id],
- memory=conversation_memory,
+ memory=BasicBlockMemory(blocks=[conversation_persona_block, conversation_human_block]),
include_base_tools=False,
- metadata={"offline_memory_tools": [rethink_memory.id, finish_rethinking_memory.id]},
)
+
assert chat_only_agent is not None
- assert set(chat_only_agent.memory.list_block_labels()) == {"chat_agent_persona", "chat_agent_human"}
- assert len(chat_only_agent.tools) == 1
+ assert offline_memory_agent is not None
- for message in ["hello", "my name is not chad, my name is swoodily"]:
- client.send_message(agent_id=chat_only_agent.id, message=message, role="user")
+ # NOTE: the system messages sent to the offline memory agent here are not the actual messages,
+ # from the chat only agent. We need to actually stream the responses from the agent to the offline memory
+ # these are just for testing
+ for message in [
+ ("hello", "user"),
+ ("hi chad, how's it going?", "system"),
+ ("my name is not chad, my name is swoodily", "user"),
+ ("I'm sorry, I'm make a note of that, swoodily", "system"),
+ ("what's the weather like today?", "user"),
+ ("could you specify where you are at?", "system"),
+ ("I'm in SF", "user"),
+ ("it's currently 60 degrees in SF", "system"),
+ ("actually, I'm in Palo Alto", "user"),
+ ("it's currently 60 degrees in Palo Alto", "system"),
+ ("that sounds nice, I might go for a hike today then", "user"),
+ ("Here are some hikes near Palo Alto: 1. The Dish 2. Redtail Loop 3. Adobe Creek", "system"),
+ ("I'm going to try the first one, the Dish", "user"),
+ ("That sounds great, hope you enjoy it", "system"),
+ ]:
+ if message[1] == "user":
+ client.send_message(agent_id=chat_only_agent.id, message=message[0], role=message[1])
+ client.send_message(agent_id=offline_memory_agent.id, message=message[0], role=message[1])
+ else:
+ client.send_message(agent_id=offline_memory_agent.id, message=message[0], role=message[1])
+
+ offline_memory_agent = client.get_agent(agent_id=offline_memory_agent.id)
chat_only_agent = client.get_agent(agent_id=chat_only_agent.id)
- chat_only_agent = client.get_agent(agent_id=chat_only_agent.id)
- assert chat_only_agent.memory.get_block("chat_agent_human").value != get_human_text(DEFAULT_HUMAN)
-
- # Clean up agent
+ assert chat_only_agent.memory.get_block("chat_only_agent_human").value != get_human_text(DEFAULT_HUMAN)
client.delete_agent(chat_only_agent.id)
+ client.delete_agent(offline_memory_agent.id)
def test_initial_message_sequence(client, mock_e2b_api_key_none):