Memory
Memory
module is responsible for holding the short memory of the agent. The short memory is a list of messages that are exchanged between the agent and the language model. The short memory is used to keep track of the conversation history and to provide context of the dialog for the language model.
Memory
object has a simple interface of
class Memory(ABC):
"""
Interface for the memory object.
"""
def __init__(self) -> None:
"""
Initialize the memory object.
"""
@abstractmethod
def add(self, item: Message | str) -> None:
"""
Add item to memory.
:param item: Message or string to add to memory
"""
@abstractmethod
def get(self, number: int) -> List[Message | str]:
"""
Get last number of items from memory.
:param number: Number of the item to get
:return: List[Message] or List[str] object
"""
ShortMemory
object is provided as an argument to the run
function. The short memory holds information about messages send to the language model in the current session.
Short memory is a special instance of the Memory
class. In our implementation it contains a simple list of messages as a container, together with method to easily add and retrieve them.
class ShortMemory(Memory):
def __init__(self, capacity: int = -1) -> None:
...
def add(self, item: Message | str) -> None:
...
def get(self, number: int) -> List[Message | str]:
...
def reset(self) -> None:
...
def get_all(self) -> List[Message]:
...
def get_last(self) -> Message:
...
LongMemory
is implemented with the same Memory
inheritance. However, it retrieves the messages trough a semantic-search and uses a vector database to store message from previous sessions.