Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
fix: added proper newlines to the rules (#2940)
Charles Packer25fd99ff7965
diff --git a/letta/constants.py b/letta/constants.pyindex 5f6e73615..958548089 100644--- a/letta/constants.py+++ b/letta/constants.py@@ -130,6 +130,11 @@ BUILTIN_TOOLS = ["run_code", "web_search"]# Built in toolsFILES_TOOLS = ["open_file", "close_file", "grep", "search_files"]+FILE_MEMORY_EXISTS_MESSAGE = "The following files are currently accessible in memory:"+FILE_MEMORY_EMPTY_MESSAGE = (+ "There are no files currently available in memory. Files will appear here once they are uploaded directly to your system."+)+# Set of all built-in Letta toolsLETTA_TOOL_SET = set(BASE_TOOLSdiff --git a/letta/helpers/tool_rule_solver.py b/letta/helpers/tool_rule_solver.pyindex ec32c1135..cdaaebc1b 100644--- a/letta/helpers/tool_rule_solver.py+++ b/letta/helpers/tool_rule_solver.py@@ -197,7 +197,7 @@ class ToolRulesSolver(BaseModel):return Block(label="tool_usage_rules",value="\n".join(compiled_prompts),- description="The following constraints define rules for tool usage and guide desired behavior. These rules must be followed to ensure proper tool execution and workflow.",+ description="The following constraints define rules for tool usage and guide desired behavior. These rules must be followed to ensure proper tool execution and workflow. A single response may contain multiple tool calls.",)return Nonediff --git a/letta/schemas/agent.py b/letta/schemas/agent.pyindex e53a88b88..939df9813 100644--- a/letta/schemas/agent.py+++ b/letta/schemas/agent.py@@ -4,7 +4,12 @@ from typing import Dict, List, Optionalfrom pydantic import BaseModel, Field, field_validator, model_validator-from letta.constants import CORE_MEMORY_LINE_NUMBER_WARNING, DEFAULT_EMBEDDING_CHUNK_SIZE+from letta.constants import (+ CORE_MEMORY_LINE_NUMBER_WARNING,+ DEFAULT_EMBEDDING_CHUNK_SIZE,+ FILE_MEMORY_EMPTY_MESSAGE,+ FILE_MEMORY_EXISTS_MESSAGE,+)from letta.schemas.block import CreateBlockfrom letta.schemas.embedding_config import EmbeddingConfigfrom letta.schemas.environment_variables import AgentEnvironmentVariable@@ -332,7 +337,13 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{% if not loop.last %}\n{% endif %}""{% endfor %}""\n</memory_blocks>"- "<files>\nThe following memory files are currently accessible:\n\n"+ "\n\n{% if tool_usage_rules %}"+ "<tool_usage_rules>\n"+ "{{ tool_usage_rules.description }}\n\n"+ "{{ tool_usage_rules.value }}\n"+ "</tool_usage_rules>"+ "{% endif %}"+ f"\n\n<files>\n{{% if file_blocks %}}{FILE_MEMORY_EXISTS_MESSAGE}\n{{% else %}}{FILE_MEMORY_EMPTY_MESSAGE}{{% endif %}}""{% for block in file_blocks %}"f"<file status=\"{{{{ '{FileStatus.open.value}' if block.value else '{FileStatus.closed.value}' }}}}\">\n""<{{ block.label }}>\n"@@ -378,7 +389,13 @@ def get_prompt_template_for_agent_type(agent_type: Optional[AgentType] = None):"{% if not loop.last %}\n{% endif %}""{% endfor %}""\n</memory_blocks>"- "<files>\nThe following memory files are currently accessible:\n\n"+ "\n\n{% if tool_usage_rules %}"+ "<tool_usage_rules>\n"+ "{{ tool_usage_rules.description }}\n\n"+ "{{ tool_usage_rules.value }}\n"+ "</tool_usage_rules>"+ "{% endif %}"+ f"\n\n<files>\n{{% if file_blocks %}}{FILE_MEMORY_EXISTS_MESSAGE}\n{{% else %}}{FILE_MEMORY_EMPTY_MESSAGE}{{% endif %}}""{% for block in file_blocks %}"f"<file status=\"{{{{ '{FileStatus.open.value}' if block.value else '{FileStatus.closed.value}' }}}}\">\n""<{{ block.label }}>\n"diff --git a/letta/schemas/memory.py b/letta/schemas/memory.pyindex ac5ce6b0e..f18fdd4e1 100644--- a/letta/schemas/memory.py+++ b/letta/schemas/memory.py@@ -108,10 +108,10 @@ class Memory(BaseModel, validate_assignment=True):except Exception as e:raise ValueError(f"Prompt template is not compatible with current memory structure: {str(e)}")- def compile(self) -> str:+ def compile(self, tool_usage_rules=None) -> str:"""Generate a string representation of the memory in-context using the Jinja2 template"""template = Template(self.prompt_template)- return template.render(blocks=self.blocks, file_blocks=self.file_blocks)+ return template.render(blocks=self.blocks, file_blocks=self.file_blocks, tool_usage_rules=tool_usage_rules)def list_block_labels(self) -> List[str]:"""Return a list of the block names held inside the memory object"""diff --git a/letta/schemas/tool_rule.py b/letta/schemas/tool_rule.pyindex fef643238..6d78c8105 100644--- a/letta/schemas/tool_rule.py+++ b/letta/schemas/tool_rule.py@@ -52,7 +52,7 @@ class ChildToolRule(BaseToolRule):type: Literal[ToolRuleType.constrain_child_tools] = ToolRuleType.constrain_child_toolschildren: List[str] = Field(..., description="The children tools that can be invoked.")prompt_template: Optional[str] = Field(- default="<tool_constraint>After using {{ tool_name }}, you can only use these tools: {{ children | join(', ') }}</tool_constraint>",+ default="<tool_rule>\nAfter using {{ tool_name }}, you can only use these tools: {{ children | join(', ') }}\n</tool_rule>",description="Optional Jinja2 template for generating agent prompt about this tool rule.",)@@ -61,7 +61,7 @@ class ChildToolRule(BaseToolRule):return set(self.children) if last_tool == self.tool_name else available_toolsdef _get_default_template(self) -> Optional[str]:- return "<tool_constraint>After using {{ tool_name }}, you can only use these tools: {{ children | join(', ') }}</tool_constraint>"+ return "<tool_rule>\nAfter using {{ tool_name }}, you can only use these tools: {{ children | join(', ') }}\n</tool_rule>"class ParentToolRule(BaseToolRule):@@ -72,7 +72,7 @@ class ParentToolRule(BaseToolRule):type: Literal[ToolRuleType.parent_last_tool] = ToolRuleType.parent_last_toolchildren: List[str] = Field(..., description="The children tools that can be invoked.")prompt_template: Optional[str] = Field(- default="<tool_constraint>{{ children | join(', ') }} can only be used after {{ tool_name }}</tool_constraint>",+ default="<tool_rule>\n{{ children | join(', ') }} can only be used after {{ tool_name }}\n</tool_rule>",description="Optional Jinja2 template for generating agent prompt about this tool rule.",)@@ -81,7 +81,7 @@ class ParentToolRule(BaseToolRule):return set(self.children) if last_tool == self.tool_name else available_tools - set(self.children)def _get_default_template(self) -> Optional[str]:- return "<tool_constraint>{{ children | join(', ') }} can only be used after {{ tool_name }}</tool_constraint>"+ return "<tool_rule>\n{{ children | join(', ') }} can only be used after {{ tool_name }}\n</tool_rule>"class ConditionalToolRule(BaseToolRule):@@ -94,7 +94,7 @@ class ConditionalToolRule(BaseToolRule):child_output_mapping: Dict[Any, str] = Field(..., description="The output case to check for mapping")require_output_mapping: bool = Field(default=False, description="Whether to throw an error when output doesn't match any case")prompt_template: Optional[str] = Field(- default="<tool_constraint>{{ tool_name }} will determine which tool to use next based on its output</tool_constraint>",+ default="<tool_rule>\n{{ tool_name }} will determine which tool to use next based on its output\n</tool_rule>",description="Optional Jinja2 template for generating agent prompt about this tool rule.",)@@ -143,7 +143,7 @@ class ConditionalToolRule(BaseToolRule):return str(function_output) == str(key)def _get_default_template(self) -> Optional[str]:- return "<tool_constraint>{{ tool_name }} will determine which tool to use next based on its output</tool_constraint>"+ return "<tool_rule>\n{{ tool_name }} will determine which tool to use next based on its output\n</tool_rule>"class InitToolRule(BaseToolRule):@@ -161,12 +161,12 @@ class TerminalToolRule(BaseToolRule):type: Literal[ToolRuleType.exit_loop] = ToolRuleType.exit_loopprompt_template: Optional[str] = Field(- default="<tool_constraint>{{ tool_name }} ends the conversation when called</tool_constraint>",+ default="<tool_rule>\n{{ tool_name }} ends your response (yields control) when called\n</tool_rule>",description="Optional Jinja2 template for generating agent prompt about this tool rule.",)def _get_default_template(self) -> Optional[str]:- return "<tool_constraint>{{ tool_name }} ends the conversation when called</tool_constraint>"+ return "<tool_rule>\n{{ tool_name }} ends your response (yields control) when called\n</tool_rule>"class ContinueToolRule(BaseToolRule):@@ -176,7 +176,7 @@ class ContinueToolRule(BaseToolRule):type: Literal[ToolRuleType.continue_loop] = ToolRuleType.continue_loopprompt_template: Optional[str] = Field(- default="<tool_constraint>{{ tool_name }} requires continuing the conversation when called</tool_constraint>",+ default="<tool_rule>\n{{ tool_name }} requires continuing your response when called\n</tool_rule>",description="Optional Jinja2 template for generating agent prompt about this tool rule.",)@@ -188,7 +188,7 @@ class RequiredBeforeExitToolRule(BaseToolRule):type: Literal[ToolRuleType.required_before_exit] = ToolRuleType.required_before_exitprompt_template: Optional[str] = Field(- default="<tool_constraint>{{ tool_name }} must be called before ending the conversation</tool_constraint>",+ default="<tool_rule>{{ tool_name }} must be called before ending the conversation</tool_rule>",description="Optional Jinja2 template for generating agent prompt about this tool rule.",)@@ -197,7 +197,7 @@ class RequiredBeforeExitToolRule(BaseToolRule):return available_toolsdef _get_default_template(self) -> Optional[str]:- return "<tool_constraint>{{ tool_name }} must be called before ending the conversation</tool_constraint>"+ return "<tool_rule>{{ tool_name }} must be called before ending the conversation</tool_rule>"class MaxCountPerStepToolRule(BaseToolRule):@@ -208,7 +208,7 @@ class MaxCountPerStepToolRule(BaseToolRule):type: Literal[ToolRuleType.max_count_per_step] = ToolRuleType.max_count_per_stepmax_count_limit: int = Field(..., description="The max limit for the total number of times this tool can be invoked in a single step.")prompt_template: Optional[str] = Field(- default="<tool_constraint>{{ tool_name }}: max {{ max_count_limit }} use(s) per turn</tool_constraint>",+ default="<tool_rule>\n{{ tool_name }}: max {{ max_count_limit }} use(s) per response\n</tool_rule>",description="Optional Jinja2 template for generating agent prompt about this tool rule.",)@@ -223,7 +223,7 @@ class MaxCountPerStepToolRule(BaseToolRule):return available_toolsdef _get_default_template(self) -> Optional[str]:- return "<tool_constraint>{{ tool_name }}: max {{ max_count_limit }} use(s) per turn</tool_constraint>"+ return "<tool_rule>\n{{ tool_name }}: max {{ max_count_limit }} use(s) per response\n</tool_rule>"ToolRule = Annotated[diff --git a/letta/services/helpers/agent_manager_helper.py b/letta/services/helpers/agent_manager_helper.pyindex 0f875ae83..43ff09e3c 100644--- a/letta/services/helpers/agent_manager_helper.py+++ b/letta/services/helpers/agent_manager_helper.py@@ -239,10 +239,9 @@ def compile_system_message(- CORE_MEMORY: the in-context memory of the LLM"""# Add tool rule constraints if available+ tool_constraint_block = Noneif tool_rules_solver is not None:tool_constraint_block = tool_rules_solver.compile_tool_rule_prompts()- if tool_constraint_block: # There may not be any depending on if there are tool rules attached- in_context_memory.blocks.append(tool_constraint_block)if user_defined_variables is not None:# TODO eventually support the user defining their own variables to inject@@ -260,7 +259,7 @@ def compile_system_message(previous_message_count=previous_message_count,archival_memory_size=archival_memory_size,)- full_memory_string = in_context_memory.compile() + "\n\n" + memory_metadata_string+ full_memory_string = in_context_memory.compile(tool_usage_rules=tool_constraint_block) + "\n\n" + memory_metadata_string# Add to the variables list to injectvariables[IN_CONTEXT_MEMORY_KEYWORD] = full_memory_string