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. Defaults to "redis://localhost:6379". |
key_prefix | str | Default: 'chat:'Prefix for Redis keys. Defaults to "chat:". |
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:
.. code-block:: python
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 )
history.add_message(HumanMessage(content="Hello, AI!")) history.add_message( AIMessage(content="Hello, human! How can I assist you today?") )
messages = history.messages for message in messages: print(f"{message.type}: {message.content}")
history.clear()
Note:
Time-to-live for entries in seconds. Defaults to None (no expiration).
Name of the Redis search index. Defaults to "idx:chat_history".
Existing Redis client instance. If provided, redis_url is ignored.
Whether to overwrite an existing index if it already exists. Defaults to False. 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.