Skip to content

Commit

Permalink
feat: Add Redis Chat Memory (#3832)
Browse files Browse the repository at this point in the history
* feat: Add Redis Chat Memory

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* Refactor password handling in RedisIndexChatMemory to use a local variable

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Gabriel Luiz Freitas Almeida <[email protected]>
  • Loading branch information
3 people committed Oct 4, 2024
1 parent be39752 commit 0276375
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/backend/base/langflow/components/memories/RedisChatMemory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from urllib import parse

from langchain_community.chat_message_histories.redis import RedisChatMessageHistory

from langflow.base.memory.model import LCChatMemoryComponent
from langflow.field_typing import BaseChatMessageHistory
from langflow.inputs import IntInput, MessageTextInput, SecretStrInput, StrInput


class RedisIndexChatMemory(LCChatMemoryComponent):
display_name = "Redis Chat Memory"
description = "Retrieves and store chat messages from Redis."
name = "RedisChatMemory"
icon = "Redis"

inputs = [
StrInput(
name="host", display_name="hostname", required=True, value="localhost", info="IP address or hostname."
),
IntInput(name="port", display_name="port", required=True, value=6379, info="Redis Port Number."),
StrInput(name="database", display_name="database", required=True, value="0", info="Redis database."),
MessageTextInput(
name="username", display_name="Username", value="", info="The Redis user name.", advanced=True
),
SecretStrInput(
name="password", display_name="Password", value="", info="The password for username.", advanced=True
),
StrInput(name="key_prefix", display_name="Key prefix", info="Key prefix.", advanced=True),
MessageTextInput(
name="session_id", display_name="Session ID", info="Session ID for the message.", advanced=True
),
]

def build_message_history(self) -> BaseChatMessageHistory:
kwargs = {}
password: str | None = self.password
if self.key_prefix:
kwargs["key_prefix"] = self.key_prefix
if password is not None and password != "":
password = parse.quote_plus(password)

url = f"redis://{self.username}:{self.password}@{self.host}:{self.port}/{self.database}"
return RedisChatMessageHistory(session_id=self.session_id, url=url, **kwargs)

0 comments on commit 0276375

Please sign in to comment.