Class BaseMessageHistory

java.lang.Object
com.redis.vl.extensions.messagehistory.BaseMessageHistory
Direct Known Subclasses:
MessageHistory, SemanticMessageHistory

public abstract class BaseMessageHistory extends Object
Base class for message history implementations.

Matches the Python BaseMessageHistory from redisvl.extensions.message_history.base_history

  • Field Details

    • name

      protected final String name
    • sessionTag

      protected final String sessionTag
  • Constructor Details

    • BaseMessageHistory

      protected BaseMessageHistory(String name, String sessionTag)
      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.
  • Method Details

    • clear

      public abstract void clear()
      Clears the chat message history.
    • delete

      public abstract void delete()
      Clear all conversation history and remove any search indices.
    • drop

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

      public abstract List<Map<String,Object>> getMessages()
      Returns the full chat history.
    • getRecent

      public abstract <T> List<T> getRecent(int topK, boolean asText, boolean raw, String sessionTag, Object role)
      Retrieve the recent conversation history in sequential order.
      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)
      Throws:
      IllegalArgumentException - if topK is not an integer greater than or equal to 0, or if role contains invalid values
    • store

      public abstract void store(String prompt, String response, String sessionTag)
      Insert a prompt:response pair into the message history.
      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.
    • addMessages

      public abstract void addMessages(List<Map<String,String>> messages, String sessionTag)
      Insert a list of prompts and responses into the message history.
      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 abstract void addMessage(Map<String,String> message, String sessionTag)
      Insert a single prompt or response into the message history.
      Parameters:
      message - The user prompt or LLM response
      sessionTag - The tag to mark the message with. Defaults to instance session tag.
    • formatContext

      protected <T> List<T> formatContext(List<Map<String,Object>> messages, boolean asText)
      Formats messages from Redis into either text strings or structured maps.
      Parameters:
      messages - The messages from the message history index
      asText - Whether to return as text strings or maps
      Returns:
      Formatted messages
    • validateRoles

      protected List<String> validateRoles(Object role)
      Validate and normalize role parameter for filtering messages.

      Matches Python _validate_roles from base_history.py (lines 90-128)

      Parameters:
      role - A single role string, List of roles, or null
      Returns:
      List of valid role strings if role is provided, null otherwise
      Throws:
      IllegalArgumentException - if role contains invalid values or is the wrong type
    • getName

      public String getName()
    • getSessionTag

      public String getSessionTag()