agentscope.memory.memory module

Base class for memory

TODO: a abstract class for a piece of memory TODO: data structure to organize multiple memory pieces in memory class

class agentscope.memory.memory.MemoryBase[source]

Bases: ABC

Base class for memory.

abstract get_memory(recent_n: int | None = None, filter_func: Callable[[int, dict], bool] | None = None) list[source]

Return a certain range (recent_n or all) of memory, filtered by filter_func :param recent_n: indicate the most recent N memory pieces to be returned. :type recent_n: int, optional :param filter_func: filter function to decide which pieces of memory should

be returned, taking the index and a piece of memory as input and return True (return this memory) or False (does not return)

abstract add(memories: Sequence[Msg] | Msg | None) None[source]

Adding new memory fragment, depending on how the memory are stored :param memories: Memories to be added. :type memories: Union[Sequence[Msg], Msg, None]

abstract delete(index: Iterable | int) None[source]

Delete memory fragment, depending on how the memory are stored and matched :param index: indices of the memory fragments to delete :type index: Union[Iterable, int]

abstract load(memories: str | list[Msg] | Msg, overwrite: bool = False) None[source]

Load memory, depending on how the memory are passed, design to load from both file or dict :param memories: memories to be loaded.

If it is in str type, it will be first checked if it is a file; otherwise it will be deserialized as messages. Otherwise, memories must be either in message type or list

of messages.

Parameters:

overwrite (bool) – if True, clear the current memory before loading the new ones; if False, memories will be appended to the old one at the end.

abstract export(file_path: str | None = None, to_mem: bool = False) list | None[source]

Export memory, depending on how the memory are stored :param file_path: file path to save the memory to. :type file_path: Optional[str] :param to_mem: if True, just return the list of messages in memory :type to_mem: Optional[str]

Notice: this method prevents file_path is None when to_mem is False.

abstract clear() None[source]

Clean memory, depending on how the memory are stored

abstract size() int[source]

Returns the number of memory segments in memory.