Class BaseMessageHistory
java.lang.Object
com.redis.vl.extensions.messagehistory.BaseMessageHistory
- Direct Known Subclasses:
MessageHistory,SemanticMessageHistory
Base class for message history implementations.
Matches the Python BaseMessageHistory from redisvl.extensions.message_history.base_history
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedBaseMessageHistory(String name, String sessionTag) Initialize message history. -
Method Summary
Modifier and TypeMethodDescriptionabstract voidaddMessage(Map<String, String> message, String sessionTag) Insert a single prompt or response into the message history.abstract voidInsert a list of prompts and responses into the message history.abstract voidclear()Clears the chat message history.abstract voiddelete()Clear all conversation history and remove any search indices.abstract voidRemove a specific exchange from the conversation history.protected <T> List<T> formatContext(List<Map<String, Object>> messages, boolean asText) Formats messages from Redis into either text strings or structured maps.Returns the full chat history.getName()abstract <T> List<T> Retrieve the recent conversation history in sequential order.abstract voidInsert a prompt:response pair into the message history.validateRoles(Object role) Validate and normalize role parameter for filtering messages.
-
Field Details
-
name
-
sessionTag
-
-
Constructor Details
-
BaseMessageHistory
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.
-
-
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
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
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
Insert a prompt:response pair into the message history.- Parameters:
prompt- The user prompt to the LLMresponse- The corresponding LLM responsesessionTag- The tag to mark the messages with. Defaults to instance session tag.
-
addMessages
Insert a list of prompts and responses into the message history.- Parameters:
messages- The list of user prompts and LLM responsessessionTag- The tag to mark the messages with. Defaults to instance session tag.
-
addMessage
Insert a single prompt or response into the message history.- Parameters:
message- The user prompt or LLM responsesessionTag- The tag to mark the message with. Defaults to instance session tag.
-
formatContext
Formats messages from Redis into either text strings or structured maps.- Parameters:
messages- The messages from the message history indexasText- Whether to return as text strings or maps- Returns:
- Formatted messages
-
validateRoles
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
-
getSessionTag
-