content/develop/ai/redisvl/0.9.1/api/message_history.md
<a id="semantic-message-history-api"></a>
class SemanticMessageHistory(name, session_tag=None, prefix=None, vectorizer=None, distance_threshold=0.3, redis_client=None, redis_url='redis://localhost:6379', connection_kwargs={}, overwrite=False, **kwargs)Bases: BaseMessageHistory
Initialize message history with index
Semantic Message History stores the current and previous user text prompts and LLM responses to allow for enriching future prompts with session context. Message history is stored in individual user or LLM prompts and responses.
The proposed schema will support a single vector embedding constructed from either the prompt or response in a single string.
add_message(message, session_tag=None)Insert a single prompt or response into the message history. A timestamp is associated with it so that it can be later sorted in sequential ordering after retrieval.
add_messages(messages, session_tag=None)Insert a list of prompts and responses into the session memory. A timestamp is associated with each so that they can be later sorted in sequential ordering after retrieval.
clear()Clears the message history.
delete()Clear all message keys and remove the search index.
drop(id=None)Remove a specific exchange from the message history.
get_recent(top_k=5, as_text=False, raw=False, session_tag=None, role=None)Retrieve the recent message history in sequential order.
get_relevant(prompt, as_text=False, top_k=5, fall_back=False, session_tag=None, raw=False, distance_threshold=None, role=None)Searches the message history for information semantically related to the specified prompt.
This method uses vector similarity search with a text prompt as input. It checks for semantically similar prompts and responses and gets the top k most relevant previous prompts or responses to include as context to the next LLM call.
Raises ValueError: if top_k is not an integer greater or equal to 0, : or if role contains invalid values.
store(prompt, response, session_tag=None)Insert a prompt:response pair into the message history. A timestamp is associated with each message so that they can be later sorted in sequential ordering after retrieval.
property messages: List[str] | List[Dict[str, str]]Returns the full message history.
<a id="message-history-api"></a>
class MessageHistory(name, session_tag=None, prefix=None, redis_client=None, redis_url='redis://localhost:6379', connection_kwargs={}, **kwargs)Bases: BaseMessageHistory
Initialize message history
Message History stores the current and previous user text prompts and LLM responses to allow for enriching future prompts with session context. Message history is stored in individual user or LLM prompts and responses.
add_message(message, session_tag=None)Insert a single prompt or response into the message history. A timestamp is associated with it so that it can be later sorted in sequential ordering after retrieval.
add_messages(messages, session_tag=None)Insert a list of prompts and responses into the message history. A timestamp is associated with each so that they can be later sorted in sequential ordering after retrieval.
clear()Clears the conversation message history.
delete()Clear all conversation keys and remove the search index.
drop(id=None)Remove a specific exchange from the conversation history.
get_recent(top_k=5, as_text=False, raw=False, session_tag=None, role=None)Retrieve the recent message history in sequential order.
store(prompt, response, session_tag=None)Insert a prompt:response pair into the message history. A timestamp is associated with each exchange so that they can be later sorted in sequential ordering after retrieval.
property messages: List[str] | List[Dict[str, str]]Returns the full message history.