RedisChatMessageHistory(
self,
session_id: str,
redis_url: str = 'redis://localhost:6379',
key_prefix: str| Name | Type | Description |
|---|---|---|
session_id* | str | A unique identifier for the chat session. |
redis_url | str | Default: 'redis://localhost:6379'URL of the Redis instance. |
key_prefix | str | Default: 'chat:'Prefix for Redis keys. |
ttl | Optional[int] | Default: None |
index_name | str | Default: 'idx:chat_history' |
redis_client | Optional[Redis] | Default: None |
overwrite_index | bool | Default: False |
**kwargs | Any | Default: {} |
Redis-based implementation of chat message history using RedisVL.
This class provides a way to store and retrieve chat message history using Redis with RedisVL for efficient indexing, querying, and document management.
Example:
from langchain_redis import RedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
history = RedisChatMessageHistory(
session_id="user123",
redis_url="redis://localhost:6379",
ttl=3600 # Expire chat history after 1 hour
)
# Add messages to the history
history.add_message(HumanMessage(content="Hello, AI!"))
history.add_message(
AIMessage(content="Hello, human! How can I assist you today?")
)
# Retrieve all messages
messages = history.messages
for message in messages:
print(f"{message.type}: {message.content}")
# Clear the history for the session
history.clear()
Note:
session_id is used to group messages belonging to the same conversation
or user session.Time-to-live for entries in seconds.
Name of the Redis search index.
Existing Redis client instance.
If provided, redis_url is ignored.
Whether to overwrite an existing index if it already exists.
If False and an index exists with a different key_prefix, a warning will
be logged.
Additional keyword arguments to pass to the Redis client.