QuantaLogic provides sophisticated memory management through two main components: AgentMemory for conversation history and VariableMemory for variable storage.
defcompact(self,n:int=2)->None:"""Compact the memory to keep only essential messages. This method keeps: - The system message (if present) - First two pairs of user-assistant messages - Last n pairs of user-assistant messages (default: 2) Args: n (int): Number of last message pairs to keep. Defaults to 2. """
fromquantalogicimportAgentMemoryfromquantalogic.typesimportMessage# Create memory instancememory=AgentMemory()# Add messagesmemory.add(Message(role="system",content="You are an AI assistant"))memory.add(Message(role="user",content="Hello!"))memory.add(Message(role="assistant",content="Hi there!"))# Optimize memory when it grows largememory.compact(n=3)# Keep last 3 pairs# Clear memorymemory.reset()
defadd(self,value:str)->str:"""Add a value to the variable memory. Args: value (str): The value to add to memory. Returns: str: The key associated with the added value (e.g., 'var1'). """
defget(self,key:str,default:str|None=None)->str|None:"""Get a value from the variable memory. Args: key (str): The key to retrieve. default (str | None, optional): Default value if key not found. Returns: str | None: The value associated with the key, or default if not found. """
defpop(self,key:str,default:str|None=None)->str|None:"""Remove and return a value for a key. Args: key (str): The key to remove. default (str | None, optional): Default value if key not found. Returns: str | None: The value associated with the key, or default if not found. """
defupdate(self,other:dict[str,str]|None=None,**kwargs)->None:"""Update the memory with key-value pairs. Args: other (dict[str, str] | None, optional): Dictionary to update from. **kwargs: Additional key-value pairs to update. """