[docs]classRedisChatMessageHistory(BaseChatMessageHistory):"""Chat message history stored in a Redis database. Setup: Install ``redis`` python package. .. code-block:: bash pip install redis Instantiate: .. code-block:: python from langchain_community.chat_message_histories import RedisChatMessageHistory history = RedisChatMessageHistory( session_id = "your-session-id", url="redis://your-host:your-port:your-database", # redis://localhost:6379/0 ) Add and retrieve messages: .. code-block:: python # Add single message history.add_message(message) # Add batch messages history.add_messages([message1, message2, message3, ...]) # Add human message history.add_user_message(human_message) # Add ai message history.add_ai_message(ai_message) # Retrieve messages messages = history.messages """# noqa: E501
[docs]def__init__(self,session_id:str,url:str="redis://localhost:6379/0",key_prefix:str="message_store:",ttl:Optional[int]=None,):"""Initialize with a RedisChatMessageHistory instance. Args: session_id: str The ID for single chat session. Used to form keys with `key_prefix`. url: Optional[str] String parameter configuration for connecting to the redis. key_prefix: Optional[str] The prefix of the key, combined with `session id` to form the key. ttl: Optional[int] Set the expiration time of `key`, the unit is seconds. """try:importredisexceptImportError:raiseImportError("Could not import redis python package. ""Please install it with `pip install redis`.")try:self.redis_client=get_client(redis_url=url)exceptredis.exceptions.ConnectionErroraserror:logger.error(error)self.session_id=session_idself.key_prefix=key_prefixself.ttl=ttl
@propertydefkey(self)->str:"""Construct the record key to use"""returnself.key_prefix+self.session_id@propertydefmessages(self)->List[BaseMessage]:"""Retrieve the messages from Redis"""_items=self.redis_client.lrange(self.key,0,-1)items=[json.loads(m.decode("utf-8"))formin_items[::-1]]messages=messages_from_dict(items)returnmessages@messages.setterdefmessages(self,messages:List[BaseMessage])->None:raiseNotImplementedError("Direct assignment to 'messages' is not allowed."" Use the 'add_messages' instead.")
[docs]defadd_message(self,message:BaseMessage)->None:"""Append the message to the record in Redis"""self.redis_client.lpush(self.key,json.dumps(message_to_dict(message)))ifself.ttl:self.redis_client.expire(self.key,self.ttl)
[docs]defclear(self)->None:"""Clear session memory from Redis"""self.redis_client.delete(self.key)