agentscope.memory¶
The memory module.
- class MemoryBase[源代码]¶
基类:
StateModule
The base class for memory in agentscope.
- abstract async add(*args, **kwargs)[源代码]¶
Add items to the memory.
- 参数:
args (Any)
kwargs (Any)
- 返回类型:
None
- abstract async delete(*args, **kwargs)[源代码]¶
Delete items from the memory.
- 参数:
args (Any)
kwargs (Any)
- 返回类型:
None
- abstract async retrieve(*args, **kwargs)[源代码]¶
Retrieve items from the memory.
- 参数:
args (Any)
kwargs (Any)
- 返回类型:
None
- class InMemoryMemory[源代码]¶
基类:
MemoryBase
The in-memory memory class for storing messages.
- load_state_dict(state_dict, strict=True)[源代码]¶
Load the memory from JSON data.
- 参数:
state_dict (dict) -- The state dictionary to load, which should have a "content" field.
strict (bool, defaults to True) -- If True, raises an error if any key in the module is not found in the state_dict. If False, skips missing keys.
- 返回类型:
None
- async retrieve(*args, **kwargs)[源代码]¶
Retrieve items from the memory.
- 参数:
args (Any)
kwargs (Any)
- 返回类型:
None
- async delete(index)[源代码]¶
Delete the specified item by index(es).
- 参数:
index (Union[Iterable, int]) -- The index to delete.
- 返回类型:
None
- class LongTermMemoryBase[源代码]¶
基类:
StateModule
The long-term memory base class, which should be a time-series memory management system.
The record_to_memory and retrieve_from_memory methods are two tool functions for agent to manage the long-term memory voluntarily. You can choose not to implement these two functions.
The record and retrieve methods are for developers to use. For example, retrieving/recording memory at the beginning of each reply, and adding the retrieved memory to the system prompt.
- async record(msgs, **kwargs)[源代码]¶
A developer-designed method to record information from the given input message(s) to the long-term memory.
- 参数:
msgs (list[Msg | None])
kwargs (Any)
- 返回类型:
None
- async retrieve(msg, **kwargs)[源代码]¶
A developer-designed method to retrieve information from the long-term memory based on the given input message(s). The retrieved information will be added to the system prompt of the agent.
- async record_to_memory(thinking, content, **kwargs)[源代码]¶
Use this function to record important information that you may need later. The target content should be specific and concise, e.g. who, when, where, do what, why, how, etc.
- 参数:
thinking (str) -- Your thinking and reasoning about what to record
content (list[str]) -- The content to remember, which is a list of strings.
kwargs (Any)
- 返回类型:
- async retrieve_from_memory(keywords, **kwargs)[源代码]¶
Retrieve the memory based on the given keywords.
- 参数:
keywords (list[str]) -- The keywords to search for in the memory, which should be specific and concise, e.g. the person's name, the date, the location, etc.
kwargs (Any)
- 返回:
A list of messages that match the keywords.
- 返回类型:
list[Msg]
- class Mem0LongTermMemory[源代码]¶
-
A class that implements the LongTermMemoryBase interface using mem0.
- __init__(agent_name=None, user_name=None, run_name=None, model=None, embedding_model=None, vector_store_config=None, mem0_config=None, default_memory_type=None, **kwargs)[源代码]¶
Initialize the Mem0LongTermMemory instance
- 参数:
agent_name (str | None, optional) -- The name of the agent. Default is None.
user_name (str | None, optional) -- The name of the user. Default is None.
run_name (str | None, optional) -- The name of the run/session. Default is None.
model (ChatModelBase | None)
embedding_model (EmbeddingModelBase | None)
vector_store_config (Any | None)
mem0_config (Any | None)
default_memory_type (str | None)
kwargs (Any)
- 返回类型:
None
备注
At least one of agent_name, user_name, or run_name is required.
During memory recording, these parameters become metadata for the stored memories.
During memory retrieval, only memories with matching metadata values will be returned.
- model (ChatModelBase | None, optional):
The chat model to use for the long-term memory. If mem0_config is provided, this will override the LLM configuration. If mem0_config is None, this is required.
- embedding_model (EmbeddingModelBase | None, optional):
The embedding model to use for the long-term memory. If mem0_config is provided, this will override the embedder configuration. If mem0_config is None, this is required.
- vector_store_config (VectorStoreConfig | None, optional):
The vector store config to use for the long-term memory. If mem0_config is provided, this will override the vector store configuration. If mem0_config is None and this is not provided, defaults to Qdrant with on_disk=True.
- mem0_config (MemoryConfig | None, optional):
The mem0 config to use for the long-term memory. If provided, individual model/embedding_model/vector_store_config parameters will override the corresponding configurations in mem0_config. If None, a new MemoryConfig will be created using the provided parameters.
- default_memory_type (str | None, optional):
The type of memory to use. Default is None, to create a semantic memory.
- 抛出:
ValueError -- If mem0_config is None and either model or embedding_model is None.
- 参数:
agent_name (str | None)
user_name (str | None)
run_name (str | None)
model (ChatModelBase | None)
embedding_model (EmbeddingModelBase | None)
vector_store_config (Any | None)
mem0_config (Any | None)
default_memory_type (str | None)
kwargs (Any)
- 返回类型:
None
- async record_to_memory(thinking, content, **kwargs)[源代码]¶
Use this function to record important information that you may need later. The target content should be specific and concise, e.g. who, when, where, do what, why, how, etc.
- 参数:
thinking (str) -- Your thinking and reasoning about what to record.
content (list[str]) -- The content to remember, which is a list of strings.
kwargs (Any)
- 返回类型:
- async retrieve_from_memory(keywords, limit=5, **kwargs)[源代码]¶
Retrieve the memory based on the given keywords.
- 参数:
keywords (list[str]) -- The keywords to search for in the memory, which should be specific and concise, e.g. the person's name, the date, the location, etc.
limit (int, optional) -- The maximum number of memories to retrieve per search.
kwargs (Any)
- 返回:
A ToolResponse containing the retrieved memories as JSON text.
- 返回类型:
ToolResponse
- async record(msgs, memory_type=None, infer=True, **kwargs)[源代码]¶
Record the content to the long-term memory.
- 参数:
msgs (list[Msg | None]) -- The messages to record to memory.
memory_type (str | None, optional) -- The type of memory to use. Default is None, to create a semantic memory. "procedural_memory" is explicitly used for procedural memories.
infer (bool, optional) -- Whether to infer memory from the content. Default is True.
**kwargs (Any) -- Additional keyword arguments for the mem0 recording.
- 返回类型:
None
- async retrieve(msg, limit=5, **kwargs)[源代码]¶
Retrieve the content from the long-term memory.
- 参数:
msg (Msg | list[Msg] | None) -- The message to search for in the memory, which should be specific and concise, e.g. the person's name, the date, the location, etc.
limit (int, optional) -- The maximum number of memories to retrieve per search.
**kwargs (Any) -- Additional keyword arguments.
- 返回:
The retrieved memory
- 返回类型:
str