Platform for stateful agents: AI with advanced memory that can learn and self-improve over time.
feat: create an abstract agent type + cleanup typing errors in `memory.py` (#1720)
Charles Packerf4168d688759
diff --git a/memgpt/agent.py b/memgpt/agent.pyindex c7995e127..78a9d11fe 100644--- a/memgpt/agent.py+++ b/memgpt/agent.py@@ -1,6 +1,7 @@import datetimeimport inspectimport traceback+from abc import ABC, abstractmethodfrom typing import List, Literal, Optional, Tuple, Unionfrom tqdm import tqdm@@ -68,8 +69,8 @@ def compile_memory_metadata_block(memory_metadata_block = "\n".join([f"### Memory [last modified: {timestamp_str}]",- f"{len(recall_memory) if recall_memory else 0} previous messages between you and the user are stored in recall memory (use functions to access them)",- f"{len(archival_memory) if archival_memory else 0} total memories you created are stored in archival memory (use functions to access them)",+ f"{recall_memory.count() if recall_memory else 0} previous messages between you and the user are stored in recall memory (use functions to access them)",+ f"{archival_memory.count() if archival_memory else 0} total memories you created are stored in archival memory (use functions to access them)","\nCore memory shown below (limited in size, additional information stored in archival / recall memory):",])@@ -188,6 +189,18 @@ def initialize_message_sequence(return messages+class BaseAgent(ABC):+ """Base class for all agents. Only two interfaces are required: step and update_state."""++ @abstractmethod+ def step(self, message: Message) -> List[Message]:+ raise NotImplementedError++ @abstractmethod+ def update_state(self) -> AgentState:+ raise NotImplementedError++class Agent(object):def __init__(self,diff --git a/memgpt/autogen/memgpt_agent.py b/memgpt/autogen/memgpt_agent.pyindex 04d95dfbe..3e83117cf 100644--- a/memgpt/autogen/memgpt_agent.py+++ b/memgpt/autogen/memgpt_agent.py@@ -13,7 +13,7 @@ from autogen.agentchat import (import memgpt.constants as constantsimport memgpt.system as systemimport memgpt.utils as utils-from memgpt.agent import Agent as MemGPTAgent+from memgpt.agent import Agent as Agentfrom memgpt.agent import save_agentfrom memgpt.agent_store.storage import StorageConnector, TableTypefrom memgpt.autogen.interface import AutoGenInterface@@ -29,7 +29,7 @@ class MemGPTConversableAgent(ConversableAgent):def __init__(self,name: str,- agent: MemGPTAgent,+ agent: Agent,skip_verify: bool = False,auto_save: bool = False,concat_other_agent_messages: bool = False,@@ -271,7 +271,7 @@ def load_autogen_memgpt_agent(# Create the agent object directly from the loaded state (not via preset creation)try:- memgpt_agent = MemGPTAgent(agent_state=agent_state, interface=interface)+ memgpt_agent = Agent(agent_state=agent_state, interface=interface)except Exception:print(f"Failed to create an agent object from agent state =\n{agent_state}")raise@@ -359,7 +359,7 @@ def create_autogen_memgpt_agent(preset_obj.human = agent_config["human"] if "human" in agent_config else get_human_text(config.human)preset_obj.persona = agent_config["persona"] if "persona" in agent_config else get_persona_text(config.persona)- memgpt_agent = MemGPTAgent(+ memgpt_agent = Agent(interface=interface,name=agent_config["name"] if "name" in agent_config else None,created_by=user.id,diff --git a/memgpt/memory.py b/memgpt/memory.pyindex f03bafb7f..2e88b47d3 100644--- a/memgpt/memory.py+++ b/memgpt/memory.py@@ -1,7 +1,6 @@import datetime-import uuidfrom abc import ABC, abstractmethod-from typing import List, Optional, Tuple, Union+from typing import Callable, Dict, List, Tuple, Unionfrom memgpt.constants import MESSAGE_SUMMARY_REQUEST_ACK, MESSAGE_SUMMARY_WARNING_FRACfrom memgpt.embeddings import embedding_model, parse_and_chunk_text, query_embedding@@ -20,7 +19,7 @@ from memgpt.utils import ()-def get_memory_functions(cls: Memory) -> List[callable]:+def get_memory_functions(cls: Memory) -> Dict[str, Callable]:"""Get memory functions for a memory class"""functions = {}@@ -113,7 +112,11 @@ class ArchivalMemory(ABC):@abstractmethoddef compile(self) -> str:- pass+ """Convert archival memory into a string representation for a prompt"""++ @abstractmethod+ def count(self) -> int:+ """Count the number of memories in the archival memory"""class RecallMemory(ABC):@@ -127,7 +130,11 @@ class RecallMemory(ABC):@abstractmethoddef compile(self) -> str:- pass+ """Convert recall memory into a string representation for a prompt"""++ @abstractmethod+ def count(self) -> int:+ """Count the number of memories in the recall memory"""@abstractmethoddef insert(self, message: Message):@@ -154,6 +161,9 @@ class DummyRecallMemory(RecallMemory):def __len__(self):return len(self._message_logs)+ def count(self) -> int:+ return len(self)+def compile(self) -> str:# don't dump all the conversations, just statisticssystem_count = user_count = assistant_count = function_count = other_count = 0@@ -186,6 +196,8 @@ class DummyRecallMemory(RecallMemory):def text_search(self, query_string, count=None, start=None):# in the dummy version, run an (inefficient) case-insensitive match searchmessage_pool = [d for d in self._message_logs if d["message"]["role"] not in ["system", "function"]]+ start = 0 if start is None else int(start)+ count = 0 if count is None else int(count)printd(f"recall_memory.text_search: searching for {query_string} (c={count}, s={start}) in {len(self._message_logs)} total messages"@@ -224,8 +236,8 @@ class DummyRecallMemory(RecallMemory):]# start/count support paging through results- start = int(start) if start is None else start- count = int(count) if count is None else count+ start = 0 if start is None else int(start)+ count = 0 if count is None else int(count)if start is not None and count is not None:return matches[start : start + count], len(matches)elif start is None and count is not None:@@ -257,16 +269,22 @@ class BaseRecallMemory(RecallMemory):self.cache = {}def get_all(self, start=0, count=None):+ start = 0 if start is None else int(start)+ count = 0 if count is None else int(count)results = self.storage.get_all(start, count)results_json = [message.to_openai_dict() for message in results]return results_json, len(results)def text_search(self, query_string, count=None, start=None):+ start = 0 if start is None else int(start)+ count = 0 if count is None else int(count)results = self.storage.query_text(query_string, count, start)results_json = [message.to_openai_dict_search_results() for message in results]return results_json, len(results)def date_search(self, start_date, end_date, count=None, start=None):+ start = 0 if start is None else int(start)+ count = 0 if count is None else int(count)results = self.storage.query_date(start_date, end_date, count, start)results_json = [message.to_openai_dict_search_results() for message in results]return results_json, len(results)@@ -302,11 +320,14 @@ class BaseRecallMemory(RecallMemory):def __len__(self):return self.storage.size()+ def count(self) -> int:+ return len(self)+class EmbeddingArchivalMemory(ArchivalMemory):"""Archival memory with embedding based search"""- def __init__(self, agent_state: AgentState, top_k: Optional[int] = 100):+ def __init__(self, agent_state: AgentState, top_k: int = 100):"""Init function for archival memory:param archival_memory_database: name of dataset to pre-fill archival with@@ -319,8 +340,10 @@ class EmbeddingArchivalMemory(ArchivalMemory):# create embedding modelself.embed_model = embedding_model(agent_state.embedding_config)- self.embedding_chunk_size = agent_state.embedding_config.embedding_chunk_size- assert self.embedding_chunk_size, f"Must set {agent_state.embedding_config.embedding_chunk_size}"+ if agent_state.embedding_config.embedding_chunk_size is None:+ raise ValueError(f"Must set {agent_state.embedding_config.embedding_chunk_size}")+ else:+ self.embedding_chunk_size = agent_state.embedding_config.embedding_chunk_size# create storage backendself.storage = StorageConnector.get_archival_storage_connector(user_id=agent_state.user_id, agent_id=agent_state.id)@@ -340,7 +363,7 @@ class EmbeddingArchivalMemory(ArchivalMemory):"""Save the index to disk"""self.storage.save()- def insert(self, memory_string, return_ids=False) -> Union[bool, List[uuid.UUID]]:+ def insert(self, memory_string, return_ids=False) -> Union[bool, List[str]]:"""Embed and save memory string"""if not isinstance(memory_string, str):@@ -381,6 +404,9 @@ class EmbeddingArchivalMemory(ArchivalMemory):def search(self, query_string, count=None, start=None):"""Search query string"""+ start = 0 if start is None else int(start)+ count = self.top_k if count is None else int(count)+if not isinstance(query_string, str):return TypeError("query must be a string")@@ -390,8 +416,6 @@ class EmbeddingArchivalMemory(ArchivalMemory):query_vec = query_embedding(self.embed_model, query_string)self.cache[query_string] = self.storage.query(query_string, query_vec, top_k=self.top_k)- start = int(start if start else 0)- count = int(count if count else self.top_k)end = min(count + start, len(self.cache[query_string]))results = self.cache[query_string][start:end]@@ -411,3 +435,6 @@ class EmbeddingArchivalMemory(ArchivalMemory):def __len__(self):return self.storage.size()++ def count(self) -> int:+ return len(self)