Skip to main content

Redis Chat Message History

This notebook demonstrates how to use the RedisChatMessageHistory class from the langchain-redis package to store and manage chat message history using Redis.

Setup​

First, we need to install the required dependencies and ensure we have a Redis instance running.

%pip install -U langchain-redis langchain-openai redis
Requirement already satisfied: langchain-redis in /home/jupyter/venv/lib/python3.11/site-packages (0.0.1)
Requirement already satisfied: langchain-openai in /home/jupyter/venv/lib/python3.11/site-packages (0.1.14)
Requirement already satisfied: redis in /home/jupyter/venv/lib/python3.11/site-packages (5.0.7)
Requirement already satisfied: langchain-core<0.3,>=0.1.52 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-redis) (0.2.11)
Requirement already satisfied: numpy<2,>=1 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-redis) (1.26.4)
Requirement already satisfied: python-ulid<3.0.0,>=2.7.0 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-redis) (2.7.0)
Requirement already satisfied: redisvl<0.3.0,>=0.2.3 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-redis) (0.2.3)
Requirement already satisfied: sentence-transformers<4.0.0,>=3.0.1 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-redis) (3.0.1)
Requirement already satisfied: openai<2.0.0,>=1.32.0 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-openai) (1.35.9)
Requirement already satisfied: tiktoken<1,>=0.7 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-openai) (0.7.0)
Requirement already satisfied: PyYAML>=5.3 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-core<0.3,>=0.1.52->langchain-redis) (6.0.1)
Requirement already satisfied: jsonpatch<2.0,>=1.33 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-core<0.3,>=0.1.52->langchain-redis) (1.33)
Requirement already satisfied: langsmith<0.2.0,>=0.1.75 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-core<0.3,>=0.1.52->langchain-redis) (0.1.83)
Requirement already satisfied: packaging<25,>=23.2 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-core<0.3,>=0.1.52->langchain-redis) (24.1)
Requirement already satisfied: pydantic<3,>=1 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-core<0.3,>=0.1.52->langchain-redis) (2.8.0)
Requirement already satisfied: tenacity!=8.4.0,<9.0.0,>=8.1.0 in /home/jupyter/venv/lib/python3.11/site-packages (from langchain-core<0.3,>=0.1.52->langchain-redis) (8.4.2)
Requirement already satisfied: anyio<5,>=3.5.0 in /home/jupyter/venv/lib/python3.11/site-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (4.4.0)
Requirement already satisfied: distro<2,>=1.7.0 in /home/jupyter/venv/lib/python3.11/site-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (1.9.0)
Requirement already satisfied: httpx<1,>=0.23.0 in /home/jupyter/venv/lib/python3.11/site-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (0.27.0)
Requirement already satisfied: sniffio in /home/jupyter/venv/lib/python3.11/site-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (1.3.1)
Requirement already satisfied: tqdm>4 in /home/jupyter/venv/lib/python3.11/site-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (4.66.4)
Requirement already satisfied: typing-extensions<5,>=4.7 in /home/jupyter/venv/lib/python3.11/site-packages (from openai<2.0.0,>=1.32.0->langchain-openai) (4.12.2)
Requirement already satisfied: coloredlogs in /home/jupyter/venv/lib/python3.11/site-packages (from redisvl<0.3.0,>=0.2.3->langchain-redis) (15.0.1)
Requirement already satisfied: tabulate<1,>=0.9.0 in /home/jupyter/venv/lib/python3.11/site-packages (from redisvl<0.3.0,>=0.2.3->langchain-redis) (0.9.0)
Requirement already satisfied: transformers<5.0.0,>=4.34.0 in /home/jupyter/venv/lib/python3.11/site-packages (from sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (4.42.3)
Requirement already satisfied: torch>=1.11.0 in /home/jupyter/venv/lib/python3.11/site-packages (from sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (2.3.1)
Requirement already satisfied: scikit-learn in /home/jupyter/venv/lib/python3.11/site-packages (from sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (1.5.1)
Requirement already satisfied: scipy in /home/jupyter/venv/lib/python3.11/site-packages (from sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (1.14.0)
Requirement already satisfied: huggingface-hub>=0.15.1 in /home/jupyter/venv/lib/python3.11/site-packages (from sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (0.23.4)
Requirement already satisfied: Pillow in /home/jupyter/venv/lib/python3.11/site-packages (from sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (10.4.0)
Requirement already satisfied: regex>=2022.1.18 in /home/jupyter/venv/lib/python3.11/site-packages (from tiktoken<1,>=0.7->langchain-openai) (2024.5.15)
Requirement already satisfied: requests>=2.26.0 in /home/jupyter/venv/lib/python3.11/site-packages (from tiktoken<1,>=0.7->langchain-openai) (2.32.3)
Requirement already satisfied: idna>=2.8 in /home/jupyter/venv/lib/python3.11/site-packages (from anyio<5,>=3.5.0->openai<2.0.0,>=1.32.0->langchain-openai) (3.7)
Requirement already satisfied: certifi in /home/jupyter/venv/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.32.0->langchain-openai) (2024.6.2)
Requirement already satisfied: httpcore==1.* in /home/jupyter/venv/lib/python3.11/site-packages (from httpx<1,>=0.23.0->openai<2.0.0,>=1.32.0->langchain-openai) (1.0.5)
Requirement already satisfied: h11<0.15,>=0.13 in /home/jupyter/venv/lib/python3.11/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai<2.0.0,>=1.32.0->langchain-openai) (0.14.0)
Requirement already satisfied: filelock in /home/jupyter/venv/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (3.15.4)
Requirement already satisfied: fsspec>=2023.5.0 in /home/jupyter/venv/lib/python3.11/site-packages (from huggingface-hub>=0.15.1->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (2024.6.1)
Requirement already satisfied: jsonpointer>=1.9 in /home/jupyter/venv/lib/python3.11/site-packages (from jsonpatch<2.0,>=1.33->langchain-core<0.3,>=0.1.52->langchain-redis) (3.0.0)
Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /home/jupyter/venv/lib/python3.11/site-packages (from langsmith<0.2.0,>=0.1.75->langchain-core<0.3,>=0.1.52->langchain-redis) (3.10.6)
Requirement already satisfied: annotated-types>=0.4.0 in /home/jupyter/venv/lib/python3.11/site-packages (from pydantic<3,>=1->langchain-core<0.3,>=0.1.52->langchain-redis) (0.7.0)
Requirement already satisfied: pydantic-core==2.20.0 in /home/jupyter/venv/lib/python3.11/site-packages (from pydantic<3,>=1->langchain-core<0.3,>=0.1.52->langchain-redis) (2.20.0)
Requirement already satisfied: charset-normalizer<4,>=2 in /home/jupyter/venv/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain-openai) (3.3.2)
Requirement already satisfied: urllib3<3,>=1.21.1 in /home/jupyter/venv/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken<1,>=0.7->langchain-openai) (2.2.2)
Requirement already satisfied: sympy in /home/jupyter/venv/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (1.12.1)
Requirement already satisfied: networkx in /home/jupyter/venv/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (3.3)
Requirement already satisfied: jinja2 in /home/jupyter/venv/lib/python3.11/site-packages (from torch>=1.11.0->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (3.1.4)
Requirement already satisfied: safetensors>=0.4.1 in /home/jupyter/venv/lib/python3.11/site-packages (from transformers<5.0.0,>=4.34.0->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (0.4.3)
Requirement already satisfied: tokenizers<0.20,>=0.19 in /home/jupyter/venv/lib/python3.11/site-packages (from transformers<5.0.0,>=4.34.0->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (0.19.1)
Requirement already satisfied: humanfriendly>=9.1 in /home/jupyter/venv/lib/python3.11/site-packages (from coloredlogs->redisvl<0.3.0,>=0.2.3->langchain-redis) (10.0)
Requirement already satisfied: joblib>=1.2.0 in /home/jupyter/venv/lib/python3.11/site-packages (from scikit-learn->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (1.4.2)
Requirement already satisfied: threadpoolctl>=3.1.0 in /home/jupyter/venv/lib/python3.11/site-packages (from scikit-learn->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (3.5.0)
Requirement already satisfied: MarkupSafe>=2.0 in /home/jupyter/venv/lib/python3.11/site-packages (from jinja2->torch>=1.11.0->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (2.1.5)
Requirement already satisfied: mpmath<1.4.0,>=1.1.0 in /home/jupyter/venv/lib/python3.11/site-packages (from sympy->torch>=1.11.0->sentence-transformers<4.0.0,>=3.0.1->langchain-redis) (1.3.0)
Note: you may need to restart the kernel to use updated packages.

Make sure you have a Redis server running. You can start one using Docker with the following command:

docker run -d -p 6379:6379 redis:latest

Or install and run Redis locally according to the instructions for your operating system.

import os

# Use the environment variable if set, otherwise default to localhost
REDIS_URL = os.getenv("REDIS_URL", "redis://localhost:6379")
print(f"Connecting to Redis at: {REDIS_URL}")
Connecting to Redis at: redis://redis:6379

Importing Required Libraries​

from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.messages import AIMessage, HumanMessage
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI
from langchain_redis import RedisChatMessageHistory

Basic Usage of RedisChatMessageHistory​

# Initialize RedisChatMessageHistory
history = RedisChatMessageHistory(session_id="user_123", redis_url=REDIS_URL)

# Add messages to the history
history.add_user_message("Hello, AI assistant!")
history.add_ai_message("Hello! How can I assist you today?")

# Retrieve messages
print("Chat History:")
for message in history.messages:
print(f"{type(message).__name__}: {message.content}")
Chat History:
HumanMessage: Hello, AI assistant!
AIMessage: Hello! How can I assist you today?

Using RedisChatMessageHistory with Language Models​

Set OpenAI API key​

from getpass import getpass

# Check if OPENAI_API_KEY is already set in the environment
openai_api_key = os.getenv("OPENAI_API_KEY")

if not openai_api_key:
print("OpenAI API key not found in environment variables.")
openai_api_key = getpass("Please enter your OpenAI API key: ")

# Set the API key for the current session
os.environ["OPENAI_API_KEY"] = openai_api_key
print("OpenAI API key has been set for this session.")
else:
print("OpenAI API key found in environment variables.")
OpenAI API key not found in environment variables.
``````output
Please enter your OpenAI API key: Β·Β·Β·Β·Β·Β·Β·Β·
``````output
OpenAI API key has been set for this session.
# Create a prompt template
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful AI assistant."),
MessagesPlaceholder(variable_name="history"),
("human", "{input}"),
]
)

# Initialize the language model
llm = ChatOpenAI()

# Create the conversational chain
chain = prompt | llm


# Function to get or create a RedisChatMessageHistory instance
def get_redis_history(session_id: str) -> BaseChatMessageHistory:
return RedisChatMessageHistory(session_id, redis_url=REDIS_URL)


# Create a runnable with message history
chain_with_history = RunnableWithMessageHistory(
chain, get_redis_history, input_messages_key="input", history_messages_key="history"
)

# Use the chain in a conversation
response1 = chain_with_history.invoke(
{"input": "Hi, my name is Alice."},
config={"configurable": {"session_id": "alice_123"}},
)
print("AI Response 1:", response1.content)

response2 = chain_with_history.invoke(
{"input": "What's my name?"}, config={"configurable": {"session_id": "alice_123"}}
)
print("AI Response 2:", response2.content)
AI Response 1: Hello Alice! How can I assist you today?
AI Response 2: Your name is Alice.

Advanced Features​

Custom Redis Configuration​

# Initialize with custom Redis configuration
custom_history = RedisChatMessageHistory(
"user_456",
redis_url=REDIS_URL,
key_prefix="custom_prefix:",
ttl=3600, # Set TTL to 1 hour
index_name="custom_index",
)

custom_history.add_user_message("This is a message with custom configuration.")
print("Custom History:", custom_history.messages)
Custom History: [HumanMessage(content='This is a message with custom configuration.')]

Searching Messages​

# Add more messages
history.add_user_message("Tell me about artificial intelligence.")
history.add_ai_message(
"Artificial Intelligence (AI) is a branch of computer science..."
)

# Search for messages containing a specific term
search_results = history.search_messages("artificial intelligence")
print("Search Results:")
for result in search_results:
print(f"{result['type']}: {result['content'][:50]}...")
Search Results:
human: Tell me about artificial intelligence....
ai: Artificial Intelligence (AI) is a branch of comput...

Clearing History​

# Clear the chat history
history.clear()
print("Messages after clearing:", history.messages)
Messages after clearing: []

Conclusion​

This notebook demonstrated the key features of RedisChatMessageHistory from the langchain-redis package. It showed how to initialize and use the chat history, integrate it with language models, and utilize advanced features like custom configurations and message searching. Redis provides a fast and scalable solution for managing chat history in AI applications.


Was this page helpful?


You can also leave detailed feedback on GitHub.