Skip to main content

MemoryBlock

An abstract class serves as the fundamental component within the agent memory system. This class is equipped with “write” and “clear” functions. However, it intentionally does not define a retrieval interface, as the structure of the data to be retrieved may vary in different types of memory blocks.

write_records

Writes records to the memory, appending them to existing ones. Parameters:
  • records (List[MemoryRecord]): Records to be added to the memory.

write_record

Writes a record to the memory, appending it to existing ones. Parameters:
  • record (MemoryRecord): Record to be added to the memory.

pop_records

Removes records from the memory and returns the removed records. Parameters:
  • count (int): Number of records to remove.
Returns: List[MemoryRecord]: The records that were removed from the memory in their original order.

remove_records_by_indices

Removes records at specified indices from the memory. Parameters:
  • indices (List[int]): List of indices to remove. Indices should be valid positions in the current record list.
Returns: List[MemoryRecord]: The removed records in their original order.

clear

Clears all messages from the memory.

BaseContextCreator

An abstract base class defining the interface for context creation strategies. This class provides a foundational structure for different strategies to generate conversational context from a list of context records. The primary goal is to create a context that is aligned with a specified token count limit, allowing subclasses to define their specific approach. Subclasses should implement the :obj:token_counter,:obj: token_limit, and :obj:create_context methods to provide specific context creation logic. Parameters:
  • token_counter (BaseTokenCounter): A token counter instance responsible for counting tokens in a message.
  • token_limit (int): The maximum number of tokens allowed in the generated context.

token_counter

token_limit

create_context

An abstract method to create conversational context from the chat history. Constructs the context from provided records. The specifics of how this is done and how the token count is managed should be provided by subclasses implementing this method. The output messages order should keep same as the input order. Parameters:
  • records (List[ContextRecord]): A list of context records from which to generate the context.
Returns: Tuple[List[OpenAIMessage], int]: A tuple containing the constructed context in OpenAIMessage format and the total token count.

AgentMemory

Represents a specialized form of MemoryBlock, uniquely designed for direct integration with an agent. Two key abstract functions, “retrieve” and “get_context_creator”, are used for generating model context based on the memory records stored within the AgentMemory.

agent_id

agent_id

retrieve

Returns: List[ContextRecord]: A record list for creating model context.

get_context_creator

Returns: BaseContextCreator: A model context creator.

get_context

Returns: (List[OpenAIMessage], int): A tuple containing the constructed context in OpenAIMessage format and the total token count.

clean_tool_calls

Removes tool call messages from memory. This is an optional method that can be overridden by subclasses to implement cleaning of tool-related messages. By default, it does nothing, maintaining backward compatibility.

repr

Returns: str: A string in the format ‘ClassName(agent_id=<id>)’ if agent_id exists, otherwise just ‘ClassName()’.