接口 LongTermMemory


public interface LongTermMemory
Abstract base class for long-term memory implementations.

This class provides a time-series memory management system that persists information beyond individual conversation sessions. Long-term memory enables agents to:

  • Remember user preferences, habits, and personal information across sessions
  • Learn from past interactions and improve over time
  • Maintain context for long-running tasks or projects
  • Build personalized experiences based on historical data

This class defines the core memory API for framework-level integration:

  • record(List) - Record messages to memory (called by framework)
  • retrieve(Msg) - Retrieve relevant memories (called by framework)

For agent-controlled memory operations (AGENT_CONTROL mode), use LongTermMemoryTools which provides tool functions that adapt these core methods for agent use.

All methods are asynchronous and return Reactor Mono types for non-blocking integration with the agent framework.

Usage Example:


 // Create long-term memory instance
 LongTermMemoryBase longTermMemory = Mem0LongTermMemory.builder()
     .agentName("Assistant")
     .userName("user_123")
     .apiBaseUrl("http://localhost:8000")
     .build();

 // Use in ReActAgent
 ReActAgent agent = ReActAgent.builder()
     .name("Assistant")
     .model(model)
     .longTermMemory(longTermMemory)
     .longTermMemoryMode(LongTermMemoryMode.BOTH)
     .build();
 
另请参阅:
  • 方法概要

    修饰符和类型
    方法
    说明
    reactor.core.publisher.Mono<Void>
    record(List<Msg> msgs)
    Records messages to long-term memory.
    reactor.core.publisher.Mono<String>
    Retrieves relevant information from long-term memory based on the input message.
  • 方法详细资料

    • record

      reactor.core.publisher.Mono<Void> record(List<Msg> msgs)
      Records messages to long-term memory.

      This is a developer-facing method designed to be called by the framework (e.g., automatically at the end of each agent reply). Implementations should extract meaningful information from the messages and persist it to the underlying memory store.

      The method filters out null messages before processing. Empty lists are handled gracefully without error.

      Framework Integration: When LongTermMemoryMode.STATIC_CONTROL or LongTermMemoryMode.BOTH is configured, this method is called automatically after each agent reply to record the conversation.

      参数:
      msgs - List of messages to record (null entries are filtered out)
      返回:
      A Mono that completes when recording is finished
    • retrieve

      reactor.core.publisher.Mono<String> retrieve(Msg msg)
      Retrieves relevant information from long-term memory based on the input message.

      This is a developer-facing method designed to be called by the framework (e.g., automatically at the beginning of each agent reply). Implementations should use the message content to search for relevant memories and return them as text.

      The returned text is typically added to the agent's system prompt to provide context from previous interactions.

      Framework Integration: When LongTermMemoryMode.STATIC_CONTROL or LongTermMemoryMode.BOTH is configured, this method is called automatically before each agent reasoning step to inject relevant context.

      参数:
      msg - The message to use as a query for memory retrieval
      返回:
      A Mono emitting the retrieved memory text (may be empty)