Add a message to the chat history using RedisVL.
This method adds a new message to the Redis store for the current session using RedisVL's document loading capabilities.
add_message(
self,
message: BaseMessage,
) -> NoneExample:
from langchain_redis import RedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
history = RedisChatMessageHistory(
session_id="user123",
redis_url="redis://localhost:6379",
ttl=3600 # optional: set TTL to 1 hour
)
# Add a human message
history.add_message(HumanMessage(content="Hello, AI!"))
# Add an AI message
history.add_message(
AIMessage(content="Hello! How can I assist you today?")
)
# Verify messages were added
print(f"Number of messages: {len(history.messages)}")
Note:
session_id.| Name | Type | Description |
|---|---|---|
message* | BaseMessage | The message to add to the history. This should be an instance of a class derived from |