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[源代码]

基类:ABC

Base class for memory.

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

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[源代码]

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[源代码]

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[源代码]

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.

参数:

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[源代码]

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[源代码]

Clean memory, depending on how the memory are stored

abstract size() int[源代码]

Returns the number of memory segments in memory.