Class MessageHistory
java.lang.Object
com.redis.vl.extensions.messagehistory.BaseMessageHistory
com.redis.vl.extensions.messagehistory.MessageHistory
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
-
Field Summary
Fields inherited from class com.redis.vl.extensions.messagehistory.BaseMessageHistory
name, sessionTag -
Constructor Summary
ConstructorsConstructorDescriptionMessageHistory(String name, String sessionTag, String prefix, redis.clients.jedis.UnifiedJedis redisClient) Initialize message history.MessageHistory(String name, redis.clients.jedis.UnifiedJedis redisClient) Initialize message history. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddMessage(Map<String, String> message) Insert a single prompt or response into the message history using the default session tag.voidaddMessage(Map<String, String> message, String sessionTag) Insert a single prompt or response into the message history.voidaddMessages(List<Map<String, String>> messages) Insert a list of prompts and responses into the message history using the default session tag.voidInsert a list of prompts and responses into the message history.voidclear()Clears the chat message history.voiddelete()Clear all conversation history and remove any search indices.voidRemove a specific exchange from the conversation history.getIndex()Returns the full chat history.<T> List<T> Retrieve the recent conversation history (backward-compatible overload without role filter).<T> List<T> Retrieve the recent conversation history in sequential order.voidInsert a prompt:response pair into the message history using the default session tag.voidInsert a prompt:response pair into the message history.Methods inherited from class com.redis.vl.extensions.messagehistory.BaseMessageHistory
formatContext, getName, getSessionTag, validateRoles
-
Constructor Details
-
MessageHistory
Initialize message history.- Parameters:
name- The name of the message history indexredisClient- 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 indexsessionTag- 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:BaseMessageHistoryClears the chat message history.- Specified by:
clearin classBaseMessageHistory
-
delete
public void delete()Description copied from class:BaseMessageHistoryClear all conversation history and remove any search indices.- Specified by:
deletein classBaseMessageHistory
-
drop
Description copied from class:BaseMessageHistoryRemove a specific exchange from the conversation history.- Specified by:
dropin classBaseMessageHistory- Parameters:
id- The id of the entry to delete. If null then the last entry is deleted.
-
getMessages
Description copied from class:BaseMessageHistoryReturns the full chat history.- Specified by:
getMessagesin classBaseMessageHistory
-
getRecent
Retrieve the recent conversation history (backward-compatible overload without role filter).- Parameters:
topK- The number of previous messages to returnasText- Whether to return as text strings or mapsraw- Whether to return full Redis hash entriessessionTag- Session tag to filter by- Returns:
- List of messages
-
getRecent
Description copied from class:BaseMessageHistoryRetrieve the recent conversation history in sequential order.- Specified by:
getRecentin classBaseMessageHistory- 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
Description copied from class:BaseMessageHistoryInsert a prompt:response pair into the message history.- Specified by:
storein classBaseMessageHistory- Parameters:
prompt- The user prompt to the LLMresponse- The corresponding LLM responsesessionTag- The tag to mark the messages with. Defaults to instance session tag.
-
store
Insert a prompt:response pair into the message history using the default session tag.- Parameters:
prompt- The user prompt to the LLMresponse- The corresponding LLM response
-
addMessages
Description copied from class:BaseMessageHistoryInsert a list of prompts and responses into the message history.- Specified by:
addMessagesin classBaseMessageHistory- Parameters:
messages- The list of user prompts and LLM responsessessionTag- The tag to mark the messages with. Defaults to instance session tag.
-
addMessage
Description copied from class:BaseMessageHistoryInsert a single prompt or response into the message history.- Specified by:
addMessagein classBaseMessageHistory- Parameters:
message- The user prompt or LLM responsesessionTag- The tag to mark the message with. Defaults to instance session tag.
-
addMessage
Insert a single prompt or response into the message history using the default session tag.- Parameters:
message- The user prompt or LLM response
-
addMessages
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
-