Mem0LongTermMemory
public class Mem0LongTermMemory implements LongTermMemory
io/agentscope/core/memory/mem0/Mem0LongTermMemory.java:113–439Mem 0 Long Term 记忆,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.core.memory.mem0 包,实现 LongTermMemory。核心成员包括 record、retrieve、builder。
Long-term memory implementation using Mem0 as the store.
This implementation integrates with Mem0, a memory layer for AI applications that provides persistent, searchable memory storage using vector embeddings and LLM-powered memory extraction.
Key Features:
-
Semantic memory search using vector embeddings
-
LLM-powered memory extraction and inference
-
Multi-tenant memory isolation (agent, user, run)
-
Custom metadata support for tagging and filtering memories
-
Automatic fallback mechanisms to ensure reliable memory storage
-
Reactive, non-blocking operations
Memory Isolation: Memories are organized using three levels of metadata:
-
agentId: Identifies the agent (optional)
-
userId: Identifies the user/workspace (optional)
-
runId: Identifies the session/run (optional)
-
metadata: Custom key-value pairs for additional filtering (optional)
At least one identifier is required. During retrieval, only memories with matching metadata are returned.
Usage Example:
`// Create memory instance with authentication
Mem0LongTermMemory memory = Mem0LongTermMemory.builder()
.agentName("Assistant")
.userId("user_123")
.apiBaseUrl("http://localhost:8000")
.apiKey(System.getenv("MEM0_API_KEY"))
.build();
// For local deployments without authentication
Mem0LongTermMemory localMemory = Mem0LongTermMemory.builder()
.agentName("Assistant")
.userId("user_123")
.apiBaseUrl("http://localhost:8000")
.build();
// For self-hosted Mem0
Mem0LongTermMemory selfHostedMemory = Mem0LongTermMemory.builder()
.agentName("Assistant")
.userId("user_123")
.apiBaseUrl("http://localhost:8000")
.apiType(Mem0ApiType.SELF_HOSTED) // Specify self-hosted API type
.build();
// With custom metadata for filtering
Map<String, Object> metadata = new HashMap<>();
metadata.put("category", "travel");
metadata.put("project_id", "proj_001");
Mem0LongTermMemory memoryWithMetadata = Mem0LongTermMemory.builder()
.agentName("Assistant")
.userId("user_123")
.apiBaseUrl("http://localhost:8000")
.metadata(metadata) // Custom metadata for storage and filtering
.build();
// Record a message (metadata will be stored with the memory)
Msg msg = Msg.builder()
.role(MsgRole.USER)
.content("I prefer homestays when traveling")
.build();
memory.record(List.of(msg)).block();
// Retrieve relevant memories (metadata will be used as filter)
Msg query = Msg.builder()
.role(MsgRole.USER)
.content("What are my travel preferences?")
.build();
String memories = memory.retrieve(query).block();
// Result: "User prefers homestays when traveling"
`
嵌套类摘要Nested Class Summary
| 类Class | 类型Kind | 说明Description |
|---|---|---|
Builder | 类class | Mem0LongTermMemory的构建器。 Builder for Mem0LongTermMemory. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public Mono<Void> record(List<Msg> msgs) | 设置记录。 Records messages to long-term memory. |
public Mono<String> retrieve(Msg msg) | 按查询检索相关文档。 Retrieves relevant memories based on the input message. |
public static Builder builder()static | 创建新的构建器用于 Mem0LongTermMemory。 Creates a new builder for Mem0LongTermMemory. |
方法详情Method Detail
record
public Mono<Void> record(List<Msg> msgs)
设置记录。
Records messages to long-term memory.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msgs | List<Msg> | 列出的消息到。List of messages to record |
Mono<Void>io/agentscope/core/memory/mem0/Mem0LongTermMemory.java:169–204retrieve
public Mono<String> retrieve(Msg msg)
按查询检索相关文档。
Retrieves relevant memories based on the input message.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
msg | Msg | 消息到使用 as 搜索查询。The message to use as a search query |
Mono<String>io/agentscope/core/memory/mem0/Mem0LongTermMemory.java:268–292builder
public static Builder builder()
创建新的构建器用于 Mem0LongTermMemory。
Creates a new builder for Mem0LongTermMemory.
Builderio/agentscope/core/memory/mem0/Mem0LongTermMemory.java:299–301嵌套类详情Nested Class Detail
Builder
public static class BuilderMem0LongTermMemory的构建器。位于 io.agentscope.core.memory.mem0 包。核心成员包括 agentName、userId、runName、apiBaseUrl、apiKey、timeout。
Builder for Mem0LongTermMemory.