Class BailianLongTermMemory

java.lang.Object
io.agentscope.core.memory.bailian.BailianLongTermMemory
All Implemented Interfaces:
io.agentscope.core.memory.LongTermMemory, AutoCloseable

public class BailianLongTermMemory extends Object implements io.agentscope.core.memory.LongTermMemory, AutoCloseable
Long-term memory implementation using Bailian Long Term Memory service.

This implementation integrates with Alibaba Cloud Bailian's memory service, which provides persistent, searchable memory storage using semantic similarity and LLM-powered memory extraction.

Key Features:

  • Semantic memory search using vector embeddings
  • LLM-powered memory extraction and inference
  • Multi-tenant memory isolation (user_id, memory_library_id)
  • Reactive, non-blocking operations

Usage Example:


 // Using try-with-resources (recommended)
 try (BailianLongTermMemory memory = BailianLongTermMemory.builder()
         .apiKey(System.getenv("DASHSCOPE_API_KEY"))
         .userId("user_001")
         .memoryLibraryId("memory_library_123")
         .projectId(project_id_123)
         .profileSchema(profile_schema_123)
         .build()) {

     // Record messages
     Msg msg = Msg.builder()
         .role(MsgRole.USER)
         .content("Remind me to drink water at 9 a.m. every day")
         .build();
     memory.record(List.of(msg)).block();

     // Retrieve memories
     Msg query = Msg.builder()
         .role(MsgRole.USER)
         .content("What reminder do I have?")
         .build();
     String memories = memory.retrieve(query).block();
 }
 
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 BailianMessage object, preserving the conversation structure (role and content). The messages are sent to Bailian API which uses LLM inference to extract memorable information.

      Only USER and ASSISTANT messages are recorded. For ASSISTANT messages, only those without ToolUseBlock (pure assistant replies) are kept, filtering out tool call requests. TOOL and SYSTEM messages are also filtered out to keep the conversation history clean and focused on user-assistant interactions.

      Messages containing compressed history markers (<compressed_history>) are filtered out to avoid storing redundant compressed 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.

      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 (maybe empty)
    • builder

      public static BailianLongTermMemory.Builder builder()
      Creates a new builder for BailianLongTermMemory.
      Returns:
      a new builder instance
    • close

      public void close()
      Closes the BailianMemoryClient and releases resources.
      Specified by:
      close in interface AutoCloseable