Class Mem0LongTermMemory

java.lang.Object
io.agentscope.core.memory.mem0.Mem0LongTermMemory
All Implemented Interfaces:
io.agentscope.core.memory.LongTermMemory

public class Mem0LongTermMemory extends Object implements io.agentscope.core.memory.LongTermMemory
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"
 
See Also:
  • Method Details

    • record

      public reactor.core.publisher.Mono<Void> record(List<io.agentscope.core.message.Msg> msgs)
      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:
      record in interface io.agentscope.core.memory.LongTermMemory
      Parameters:
      msgs - List of messages to record
      Returns:
      A Mono that completes when recording is finished
    • retrieve

      public reactor.core.publisher.Mono<String> retrieve(io.agentscope.core.message.Msg msg)
      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:
      retrieve in interface io.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

      public static Mem0LongTermMemory.Builder builder()
      Creates a new builder for Mem0LongTermMemory.
      Returns:
      A new builder instance