Class Mem0LongTermMemory
- All Implemented Interfaces:
io.agentscope.core.memory.LongTermMemory
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)
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"
- See Also:
-
LongTermMemoryMem0Client
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for Mem0LongTermMemory. -
Method Summary
Modifier and TypeMethodDescriptionstatic Mem0LongTermMemory.Builderbuilder()Creates a new builder for Mem0LongTermMemory.reactor.core.publisher.Mono<Void>Records messages to long-term memory.reactor.core.publisher.Mono<String>retrieve(io.agentscope.core.message.Msg msg) Retrieves relevant memories based on the input message.
-
Method Details
-
record
Records messages to long-term memory.This method converts each message to a Mem0Message object, preserving the conversation structure (role and content). The messages are sent to Mem0 API which uses LLM inference to extract memorable information.
Null messages and messages with empty text content are filtered out before processing. Empty message lists are handled gracefully without error.
- Specified by:
recordin interfaceio.agentscope.core.memory.LongTermMemory- Parameters:
msgs- List of messages to record- Returns:
- A Mono that completes when recording is finished
-
retrieve
Retrieves relevant memories based on the input message.Uses semantic search to find memories relevant to the message content. Returns memory text as a newline-separated string, or empty string if no relevant memories are found.
Only memories with matching metadata (agentId, userId, runId) are returned.
- Specified by:
retrievein interfaceio.agentscope.core.memory.LongTermMemory- Parameters:
msg- The message to use as a search query- Returns:
- A Mono emitting the retrieved memory text (may be empty)
-
builder
Creates a new builder for Mem0LongTermMemory.- Returns:
- A new builder instance
-