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.

fix: enable json support in system message (#1757)

cthomasbf2c37a18e1c
diff --git a/letta/services/helpers/agent_manager_helper.py b/letta/services/helpers/agent_manager_helper.py
index 33f52dc7e..1667fb08e 100644
--- a/letta/services/helpers/agent_manager_helper.py
+++ b/letta/services/helpers/agent_manager_helper.py
@@ -194,10 +194,9 @@ def compile_system_message(
variables[IN_CONTEXT_MEMORY_KEYWORD] = full_memory_string
if template_format == "f-string":
-
+ memory_variable_string = "{" + IN_CONTEXT_MEMORY_KEYWORD + "}"
# Catch the special case where the system prompt is unformatted
if append_icm_if_missing:
- memory_variable_string = "{" + IN_CONTEXT_MEMORY_KEYWORD + "}"
if memory_variable_string not in system_prompt:
# In this case, append it to the end to make sure memory is still injected
# warnings.warn(f"{IN_CONTEXT_MEMORY_KEYWORD} variable was missing from system prompt, appending instead")
@@ -205,7 +204,10 @@ def compile_system_message(
# render the variables using the built-in templater
try:
- formatted_prompt = safe_format(system_prompt, variables)
+ if user_defined_variables:
+ formatted_prompt = safe_format(system_prompt, variables)
+ else:
+ formatted_prompt = system_prompt.replace(memory_variable_string, full_memory_string)
except Exception as e:
raise ValueError(f"Failed to format system prompt - {str(e)}. System prompt value:\n{system_prompt}")
diff --git a/tests/test_managers.py b/tests/test_managers.py
index 2d868cd37..45fafe5d0 100644
--- a/tests/test_managers.py
+++ b/tests/test_managers.py
@@ -692,6 +692,35 @@ def test_create_agent_default_initial_message(server: SyncServer, default_user,
assert create_agent_request.memory_blocks[0].value in init_messages[0].content[0].text
+def test_create_agent_with_json_in_system_message(server: SyncServer, default_user, default_block):
+ system_prompt = (
+ "You are an expert teaching agent with encyclopedic knowledge. "
+ "When you receive a topic, query the external database for more "
+ "information. Format the queries as a JSON list of queries making "
+ "sure to include your reasoning for that query, e.g. "
+ "{'query1' : 'reason1', 'query2' : 'reason2'}"
+ )
+ create_agent_request = CreateAgent(
+ system=system_prompt,
+ llm_config=LLMConfig.default_config("gpt-4"),
+ embedding_config=EmbeddingConfig.default_config(provider="openai"),
+ block_ids=[default_block.id],
+ tags=["a", "b"],
+ description="test_description",
+ include_base_tools=False,
+ )
+ agent_state = server.agent_manager.create_agent(
+ create_agent_request,
+ actor=default_user,
+ )
+ assert agent_state is not None
+ system_message_id = agent_state.message_ids[0]
+ system_message = server.message_manager.get_message_by_id(message_id=system_message_id, actor=default_user)
+ assert system_prompt in system_message.content[0].text
+ assert default_block.value in system_message.content[0].text
+ server.agent_manager.delete_agent(agent_id=agent_state.id, actor=default_user)
+
+
def test_update_agent(server: SyncServer, comprehensive_test_agent_fixture, other_tool, other_source, other_block, default_user):
agent, _ = comprehensive_test_agent_fixture
update_agent_request = UpdateAgent(