Class MessageHistory

java.lang.Object
com.redis.vl.extensions.messagehistory.BaseMessageHistory
com.redis.vl.extensions.messagehistory.MessageHistory

public class MessageHistory extends BaseMessageHistory
Message History for storing and retrieving LLM conversation history.

Stores user prompts and LLM responses to allow for enriching future prompts with session context. Messages are tagged by session to support multiple concurrent conversations.

Matches the Python MessageHistory from redisvl.extensions.message_history.message_history

  • Constructor Details

    • MessageHistory

      public MessageHistory(String name, redis.clients.jedis.UnifiedJedis redisClient)
      Initialize message history.
      Parameters:
      name - The name of the message history index
      redisClient - A Jedis client instance
    • MessageHistory

      public MessageHistory(String name, String sessionTag, String prefix, redis.clients.jedis.UnifiedJedis redisClient)
      Initialize message history.
      Parameters:
      name - The name of the message history index
      sessionTag - Tag to be added to entries to link to a specific conversation session. Defaults to instance ULID.
      prefix - Prefix for the keys for this conversation data. Defaults to the index name.
      redisClient - A Jedis client instance
  • Method Details

    • clear

      public void clear()
      Description copied from class: BaseMessageHistory
      Clears the chat message history.
      Specified by:
      clear in class BaseMessageHistory
    • delete

      public void delete()
      Description copied from class: BaseMessageHistory
      Clear all conversation history and remove any search indices.
      Specified by:
      delete in class BaseMessageHistory
    • drop

      public void drop(String id)
      Description copied from class: BaseMessageHistory
      Remove a specific exchange from the conversation history.
      Specified by:
      drop in class BaseMessageHistory
      Parameters:
      id - The id of the entry to delete. If null then the last entry is deleted.
    • getMessages

      public List<Map<String,Object>> getMessages()
      Description copied from class: BaseMessageHistory
      Returns the full chat history.
      Specified by:
      getMessages in class BaseMessageHistory
    • getRecent

      public <T> List<T> getRecent(int topK, boolean asText, boolean raw, String sessionTag)
      Retrieve the recent conversation history (backward-compatible overload without role filter).
      Parameters:
      topK - The number of previous messages to return
      asText - Whether to return as text strings or maps
      raw - Whether to return full Redis hash entries
      sessionTag - Session tag to filter by
      Returns:
      List of messages
    • getRecent

      public <T> List<T> getRecent(int topK, boolean asText, boolean raw, String sessionTag, Object role)
      Description copied from class: BaseMessageHistory
      Retrieve the recent conversation history in sequential order.
      Specified by:
      getRecent in class BaseMessageHistory
      Parameters:
      topK - The number of previous messages to return. Default is 5.
      asText - Whether to return the conversation as a list of content strings, or list of message maps.
      raw - Whether to return the full Redis hash entry or just the role/content/tool_call_id.
      sessionTag - Tag of the entries linked to a specific conversation session. Defaults to instance ULID.
      role - Filter messages by role(s). Can be a single role string ("system", "user", "llm", "tool"), a List of role strings, or null for no filtering.
      Returns:
      List of messages (either as text strings or maps depending on asText parameter)
    • store

      public void store(String prompt, String response, String sessionTag)
      Description copied from class: BaseMessageHistory
      Insert a prompt:response pair into the message history.
      Specified by:
      store in class BaseMessageHistory
      Parameters:
      prompt - The user prompt to the LLM
      response - The corresponding LLM response
      sessionTag - The tag to mark the messages with. Defaults to instance session tag.
    • store

      public void store(String prompt, String response)
      Insert a prompt:response pair into the message history using the default session tag.
      Parameters:
      prompt - The user prompt to the LLM
      response - The corresponding LLM response
    • addMessages

      public void addMessages(List<Map<String,String>> messages, String sessionTag)
      Description copied from class: BaseMessageHistory
      Insert a list of prompts and responses into the message history.
      Specified by:
      addMessages in class BaseMessageHistory
      Parameters:
      messages - The list of user prompts and LLM responses
      sessionTag - The tag to mark the messages with. Defaults to instance session tag.
    • addMessage

      public void addMessage(Map<String,String> message, String sessionTag)
      Description copied from class: BaseMessageHistory
      Insert a single prompt or response into the message history.
      Specified by:
      addMessage in class BaseMessageHistory
      Parameters:
      message - The user prompt or LLM response
      sessionTag - The tag to mark the message with. Defaults to instance session tag.
    • addMessage

      public void addMessage(Map<String,String> message)
      Insert a single prompt or response into the message history using the default session tag.
      Parameters:
      message - The user prompt or LLM response
    • addMessages

      public void addMessages(List<Map<String,String>> messages)
      Insert a list of prompts and responses into the message history using the default session tag.
      Parameters:
      messages - The list of user prompts and LLM responses
    • getIndex

      public SearchIndex getIndex()