Class RedisVLChatMemoryStore

java.lang.Object
com.redis.vl.langchain4j.RedisVLChatMemoryStore
All Implemented Interfaces:
dev.langchain4j.store.memory.chat.ChatMemoryStore

public class RedisVLChatMemoryStore extends Object implements dev.langchain4j.store.memory.chat.ChatMemoryStore
LangChain4J ChatMemoryStore implementation using Redis as the backend.

This store enables persistent conversation history storage in Redis, allowing chat sessions to be resumed across application restarts and supporting multi-user scenarios.

Messages are stored as JSON in Redis Lists, with each session having its own list key. This provides efficient append and retrieval operations.

Example usage:


 // Create chat memory store
 UnifiedJedis jedis = RedisClient.create("localhost", 6379);
 ChatMemoryStore memoryStore = new RedisVLChatMemoryStore(jedis);

 // Use with LangChain4J chat memory
 ChatMemory chatMemory = MessageWindowChatMemory.builder()
     .id("user-123")
     .maxMessages(10)
     .chatMemoryStore(memoryStore)
     .build();

 // Or with more control
 ChatMemoryStore store = RedisVLChatMemoryStore.builder()
     .jedis(jedis)
     .keyPrefix("chat:history:")
     .ttlSeconds(3600)
     .build();
 
  • Constructor Details

    • RedisVLChatMemoryStore

      public RedisVLChatMemoryStore(UnifiedJedis jedis)
      Creates a new RedisVLChatMemoryStore with default prefix.
      Parameters:
      jedis - The Redis client
    • RedisVLChatMemoryStore

      public RedisVLChatMemoryStore(UnifiedJedis jedis, String keyPrefix)
      Creates a new RedisVLChatMemoryStore with custom prefix.
      Parameters:
      jedis - The Redis client
      keyPrefix - The key prefix for Redis keys
    • RedisVLChatMemoryStore

      public RedisVLChatMemoryStore(UnifiedJedis jedis, String keyPrefix, Integer ttlSeconds)
      Creates a new RedisVLChatMemoryStore with custom prefix and TTL.
      Parameters:
      jedis - The Redis client
      keyPrefix - The key prefix for Redis keys
      ttlSeconds - Optional TTL for conversation history (null for no expiration)
  • Method Details

    • getMessages

      public List<dev.langchain4j.data.message.ChatMessage> getMessages(Object memoryId)
      Specified by:
      getMessages in interface dev.langchain4j.store.memory.chat.ChatMemoryStore
    • updateMessages

      public void updateMessages(Object memoryId, List<dev.langchain4j.data.message.ChatMessage> messages)
      Specified by:
      updateMessages in interface dev.langchain4j.store.memory.chat.ChatMemoryStore
    • deleteMessages

      public void deleteMessages(Object memoryId)
      Specified by:
      deleteMessages in interface dev.langchain4j.store.memory.chat.ChatMemoryStore
    • getJedis

      public UnifiedJedis getJedis()
      Gets the underlying Jedis client.
      Returns:
      The Redis client
    • getKeyPrefix

      public String getKeyPrefix()
      Gets the key prefix.
      Returns:
      The key prefix
    • builder

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