Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
refactor: clean up `agent.step()` (#1756)
Charles Packer930ab946f2d0
diff --git a/memgpt/agent.py b/memgpt/agent.pyindex c97daa219..9be5e198f 100644--- a/memgpt/agent.py+++ b/memgpt/agent.py@@ -22,7 +22,7 @@ from memgpt.llm_api.llm_api_tools import create, is_context_overflow_errorfrom memgpt.memory import ArchivalMemory, RecallMemory, summarize_messagesfrom memgpt.metadata import MetadataStorefrom memgpt.persistence_manager import LocalStateManager-from memgpt.schemas.agent import AgentState+from memgpt.schemas.agent import AgentState, AgentStepResponsefrom memgpt.schemas.block import Blockfrom memgpt.schemas.embedding_config import EmbeddingConfigfrom memgpt.schemas.enums import MessageRole, OptionState@@ -196,15 +196,10 @@ class BaseAgent(ABC):Only two interfaces are required: step and update_state."""- # @abstractmethod- # def step(self, message: Message) -> List[Message]:- # raise NotImplementedError-- # TODO cleanup@abstractmethoddef step(self,- user_message: Union[Message, str], # NOTE: should be json.dump(dict)+ messages: Union[Message, List[Message], str], # TODO deprecate str inputsfirst_message: bool = False,first_message_retry_limit: int = FIRST_MESSAGE_ATTEMPTS,skip_verify: bool = False,@@ -214,10 +209,11 @@ class BaseAgent(ABC):timestamp: Optional[datetime.datetime] = None,inner_thoughts_in_kwargs: OptionState = OptionState.DEFAULT,ms: Optional[MetadataStore] = None,- ) -> Tuple[List[Union[dict, Message]], bool, bool, bool]:+ ) -> AgentStepResponse:"""Top-level event message handler for the agent."""+ raise NotImplementedError@abstractmethoddef update_state(self) -> AgentState:@@ -611,7 +607,7 @@ class Agent(BaseAgent):# (Still parsing function args)# Handle requests for immediate heartbeatheartbeat_request = function_args.pop("request_heartbeat", None)- if not (isinstance(heartbeat_request, bool) or heartbeat_request is None):+ if not isinstance(heartbeat_request, bool) or heartbeat_request is None:printd(f"{CLI_WARNING_PREFIX}'request_heartbeat' arg parsed was not a bool or None, type={type(heartbeat_request)}, value={heartbeat_request}")@@ -708,45 +704,21 @@ class Agent(BaseAgent):def step(self,- user_message: Union[Message, str], # NOTE: should be json.dump(dict)+ user_message: Union[Message, None, str], # NOTE: should be json.dump(dict)first_message: bool = False,first_message_retry_limit: int = FIRST_MESSAGE_ATTEMPTS,skip_verify: bool = False,- return_dicts: bool = True, # if True, return dicts, if False, return Message objects+ return_dicts: bool = True,recreate_message_timestamp: bool = True, # if True, when input is a Message type, recreated the 'created_at' fieldstream: bool = False, # TODO move to config?timestamp: Optional[datetime.datetime] = None,inner_thoughts_in_kwargs: OptionState = OptionState.DEFAULT,ms: Optional[MetadataStore] = None,- ) -> Tuple[List[Union[dict, Message]], bool, bool, bool]:+ ) -> AgentStepResponse:"""Top-level event message handler for the MemGPT agent"""- def strip_name_field_from_user_message(user_message_text: str) -> Tuple[str, Optional[str]]:- """If 'name' exists in the JSON string, remove it and return the cleaned text + name value"""- try:- user_message_json = dict(json_loads(user_message_text))- # Special handling for AutoGen messages with 'name' field- # Treat 'name' as a special field- # If it exists in the input message, elevate it to the 'message' level- name = user_message_json.pop("name", None)- clean_message = json_dumps(user_message_json)-- except Exception as e:- print(f"{CLI_WARNING_PREFIX}handling of 'name' field failed with: {e}")-- return clean_message, name-- def validate_json(user_message_text: str, raise_on_error: bool) -> str:- try:- user_message_json = dict(json_loads(user_message_text))- user_message_json_val = json_dumps(user_message_json)- return user_message_json_val- except Exception as e:- print(f"{CLI_WARNING_PREFIX}couldn't parse user input message as JSON: {e}")- if raise_on_error:- raise e-try:+# Step 0: update core memory# only pulling latest block data if shared memory is being used# TODO: ensure we're passing in metadata store from all surfaces@@ -760,11 +732,14 @@ class Agent(BaseAgent):# once we ensure we're correctly comparing whether in-memory core# data is different than persisted core data.self.rebuild_memory(force=True, ms=ms)+# Step 1: add user messageif user_message is not None:if isinstance(user_message, Message):+ assert user_message.text is not None+# Validate JSON via save/load- user_message_text = validate_json(user_message.text, False)+ user_message_text = validate_json(user_message.text)cleaned_user_message_text, name = strip_name_field_from_user_message(user_message_text)if name is not None:@@ -778,7 +753,7 @@ class Agent(BaseAgent):elif isinstance(user_message, str):# Validate JSON via save/load- user_message = validate_json(user_message, False)+ user_message = validate_json(user_message)cleaned_user_message_text, name = strip_name_field_from_user_message(user_message)# If user_message['name'] is not None, it will be handled properly by dict_to_message@@ -799,6 +774,7 @@ class Agent(BaseAgent):self.interface.user_message(user_message.text, msg_obj=user_message)input_message_sequence = self._messages + [user_message]+# Alternatively, the requestor can send an empty user messageelse:input_message_sequence = self._messages@@ -806,7 +782,7 @@ class Agent(BaseAgent):if len(input_message_sequence) > 1 and input_message_sequence[-1].role != "user":printd(f"{CLI_WARNING_PREFIX}Attempting to run ChatCompletion without user as the last message in the queue")- # Step 2: send the conversation and available functions to GPT+ # Step 2: send the conversation and available functions to the LLMif not skip_verify and (first_message or self.messages_total == self.messages_total_init):printd(f"This is the first message. Running extra verifier on AI response.")counter = 0@@ -843,17 +819,6 @@ class Agent(BaseAgent):response_message_id=response.id if stream else None,)- # Add the extra metadata to the assistant response- # (e.g. enough metadata to enable recreating the API call)- # assert "api_response" not in all_response_messages[0]- # all_response_messages[0]["api_response"] = response_message_copy- # assert "api_args" not in all_response_messages[0]- # all_response_messages[0]["api_args"] = {- # "model": self.model,- # "messages": input_message_sequence,- # "functions": self.functions,- # }-# Step 6: extend the message historyif user_message is not None:if isinstance(user_message, Message):@@ -866,6 +831,7 @@ class Agent(BaseAgent):# Check the memory pressure and potentially issue a memory pressure warningcurrent_total_tokens = response.usage.total_tokensactive_memory_warning = False+# We can't do summarize logic properly if context_window is undefinedif self.agent_state.llm_config.context_window is None:# Fallback if for some reason context_window is missing, just set to the default@@ -874,14 +840,17 @@ class Agent(BaseAgent):self.agent_state.llm_config.context_window = (LLM_MAX_TOKENS[self.model] if (self.model is not None and self.model in LLM_MAX_TOKENS) else LLM_MAX_TOKENS["DEFAULT"])+if current_total_tokens > MESSAGE_SUMMARY_WARNING_FRAC * int(self.agent_state.llm_config.context_window):printd(f"{CLI_WARNING_PREFIX}last response total_tokens ({current_total_tokens}) > {MESSAGE_SUMMARY_WARNING_FRAC * int(self.agent_state.llm_config.context_window)}")+# Only deliver the alert if we haven't already (this period)if not self.agent_alerted_about_memory_pressure:active_memory_warning = Trueself.agent_alerted_about_memory_pressure = True # it's up to the outer loop to handle this+else:printd(f"last response total_tokens ({current_total_tokens}) < {MESSAGE_SUMMARY_WARNING_FRAC * int(self.agent_state.llm_config.context_window)}"@@ -893,7 +862,13 @@ class Agent(BaseAgent):# update state after each stepself.update_state()- return messages_to_return, heartbeat_request, function_failed, active_memory_warning, response.usage+ return AgentStepResponse(+ messages=messages_to_return,+ heartbeat_request=heartbeat_request,+ function_failed=function_failed,+ in_context_memory_warning=active_memory_warning,+ usage=response.usage,+ )except Exception as e:printd(f"step() failed\nuser_message = {user_message}\nerror = {e}")@@ -937,15 +912,6 @@ class Agent(BaseAgent):candidate_messages_to_summarize = candidate_messages_to_summarize[:-MESSAGE_SUMMARY_TRUNC_KEEP_N_LAST]token_counts = token_counts[:-MESSAGE_SUMMARY_TRUNC_KEEP_N_LAST]- # if disallow_tool_as_first:- # # We have to make sure that a "tool" call is not sitting at the front (after system message),- # # otherwise we'll get an error from OpenAI (if using the OpenAI API)- # while len(candidate_messages_to_summarize) > 0:- # if candidate_messages_to_summarize[0]["role"] in ["tool", "function"]:- # candidate_messages_to_summarize.pop(0)- # else:- # break-printd(f"MESSAGE_SUMMARY_TRUNC_TOKEN_FRAC={MESSAGE_SUMMARY_TRUNC_TOKEN_FRAC}")printd(f"MESSAGE_SUMMARY_TRUNC_KEEP_N_LAST={MESSAGE_SUMMARY_TRUNC_KEEP_N_LAST}")printd(f"token_counts={token_counts}")@@ -1386,9 +1352,11 @@ class Agent(BaseAgent):self.pop_until_user()user_message = self.pop_message(count=1)[0]- messages, _, _, _, _ = self.step(user_message=user_message.text, return_dicts=False)+ step_response = self.step(user_message=user_message.text, return_dicts=False)+ messages = step_response.messages- assert messages is not None and all(isinstance(msg, Message) for msg in messages), "step() returned non-Message objects"+ assert messages is not None+ assert all(isinstance(msg, Message) for msg in messages), "step() returned non-Message objects"return messages@@ -1430,3 +1398,30 @@ def save_agent_memory(agent: Agent, ms: MetadataStore):if block.value is None:block.value = ""ms.update_or_create_block(block)+++def strip_name_field_from_user_message(user_message_text: str) -> Tuple[str, Optional[str]]:+ """If 'name' exists in the JSON string, remove it and return the cleaned text + name value"""+ try:+ user_message_json = dict(json_loads(user_message_text))+ # Special handling for AutoGen messages with 'name' field+ # Treat 'name' as a special field+ # If it exists in the input message, elevate it to the 'message' level+ name = user_message_json.pop("name", None)+ clean_message = json_dumps(user_message_json)+ return clean_message, name++ except Exception as e:+ print(f"{CLI_WARNING_PREFIX}handling of 'name' field failed with: {e}")+ raise e+++def validate_json(user_message_text: str) -> str:+ """Make sure that the user input message is valid JSON"""+ try:+ user_message_json = dict(json_loads(user_message_text))+ user_message_json_val = json_dumps(user_message_json)+ return user_message_json_val+ except Exception as e:+ print(f"{CLI_WARNING_PREFIX}couldn't parse user input message as JSON: {e}")+ raise ediff --git a/memgpt/main.py b/memgpt/main.pyindex fceaff6b2..d13839372 100644--- a/memgpt/main.py+++ b/memgpt/main.py@@ -366,7 +366,7 @@ def run_agent_loop(skip_next_user_input = Falsedef process_agent_step(user_message, no_verify):- new_messages, heartbeat_request, function_failed, token_warning, tokens_accumulated = memgpt_agent.step(+ step_response = memgpt_agent.step(user_message,first_message=False,skip_verify=no_verify,@@ -374,6 +374,11 @@ def run_agent_loop(inner_thoughts_in_kwargs=inner_thoughts_in_kwargs,ms=ms,)+ new_messages = step_response.messages+ heartbeat_request = step_response.heartbeat_request+ function_failed = step_response.function_failed+ token_warning = step_response.in_context_memory_warning+ step_response.usageagent.save_agent(memgpt_agent, ms)skip_next_user_input = Falsediff --git a/memgpt/schemas/agent.py b/memgpt/schemas/agent.pyindex 54a9aec14..1826c7848 100644--- a/memgpt/schemas/agent.py+++ b/memgpt/schemas/agent.py@@ -1,13 +1,15 @@import uuidfrom datetime import datetime-from typing import Dict, List, Optional+from typing import Dict, List, Optional, Union-from pydantic import Field, field_validator+from pydantic import BaseModel, Field, field_validatorfrom memgpt.schemas.embedding_config import EmbeddingConfigfrom memgpt.schemas.llm_config import LLMConfigfrom memgpt.schemas.memgpt_base import MemGPTBasefrom memgpt.schemas.memory import Memory+from memgpt.schemas.message import Message+from memgpt.schemas.openai.chat_completion_response import UsageStatisticsclass BaseAgent(MemGPTBase, validate_assignment=True):@@ -102,3 +104,14 @@ class UpdateAgentState(BaseAgent):# TODO: determine if these should be editable via this schema?message_ids: Optional[List[str]] = Field(None, description="The ids of the messages in the agent's in-context memory.")memory: Optional[Memory] = Field(None, description="The in-context memory of the agent.")+++class AgentStepResponse(BaseModel):+ # TODO remove support for list of dicts+ messages: Union[List[Message], List[dict]] = Field(..., description="The messages generated during the agent's step.")+ heartbeat_request: bool = Field(..., description="Whether the agent requested a heartbeat (i.e. follow-up execution).")+ function_failed: bool = Field(..., description="Whether the agent step ended because a function call failed.")+ in_context_memory_warning: bool = Field(+ ..., description="Whether the agent step ended because the in-context memory is near its limit."+ )+ usage: UsageStatistics = Field(..., description="Usage statistics of the LLM call during the agent's step.")diff --git a/memgpt/server/server.py b/memgpt/server/server.pyindex 834117d04..3a25de0fa 100644--- a/memgpt/server/server.py+++ b/memgpt/server/server.py@@ -320,7 +320,7 @@ class SyncServer(Server):total_usage = UsageStatistics()step_count = 0while True:- new_messages, heartbeat_request, function_failed, token_warning, usage = memgpt_agent.step(+ step_response = memgpt_agent.step(next_input_message,first_message=False,skip_verify=no_verify,@@ -329,6 +329,12 @@ class SyncServer(Server):timestamp=timestamp,ms=self.ms,)+ step_response.messages+ heartbeat_request = step_response.heartbeat_request+ function_failed = step_response.function_failed+ token_warning = step_response.in_context_memory_warning+ usage = step_response.usage+step_count += 1total_usage += usagecounter += 1