-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
be39752
commit 0276375
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/backend/base/langflow/components/memories/RedisChatMemory.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |