Skip to content

Commit cb567ef

Browse files
authored
feat: add MemorystoreChatMessageHistory (#10)
1 parent 3e428b2 commit cb567ef

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

‎pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ license = {file = "LICENSE"}
77
requires-python = ">=3.8"
88
dependencies = [
99
"langchain==0.1.1",
10+
"redis>=5.0.0",
1011
]
1112

1213
[project.urls]
@@ -37,4 +38,4 @@ profile = "black"
3738

3839
[tool.mypy]
3940
python_version = 3.8
40-
warn_unused_configs = true
41+
warn_unused_configs = true

‎src/langchain_google_memorystore_redis/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
from langchain_google_memorystore_redis.chat_message_history import MemorystoreChatMessageHistory
16+
17+
__all__ = ["MemorystoreChatMessageHistory"]
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Copyright 2024 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import json
16+
import redis
17+
from typing import List, Optional
18+
19+
from langchain_core.chat_history import BaseChatMessageHistory
20+
from langchain_core.messages import (
21+
BaseMessage,
22+
message_to_dict,
23+
messages_from_dict,
24+
)
25+
26+
27+
class MemorystoreChatMessageHistory(BaseChatMessageHistory):
28+
"""Chat message history stored in a Cloud Memorystore for Redis database."""
29+
30+
def __init__(
31+
self,
32+
client: redis.Redis,
33+
session_id: str,
34+
ttl: Optional[int] = None,
35+
):
36+
"""Initializes the chat message history for Memorystore for Redis.
37+
38+
Args:
39+
client: A redis.Redis client object.
40+
session_id: A string that uniquely identifies the chat history.
41+
ttl: Specifies the time in seconds after which the session will
42+
expire and be eliminated from the Redis instance since the most
43+
recent message is added.
44+
"""
45+
46+
self._redis = client
47+
self._key = session_id
48+
self._ttl = ttl
49+
50+
@property
51+
def messages(self) -> List[BaseMessage]:
52+
"""Retrieve all messages chronologically stored in this session."""
53+
all_elements = self._redis.lrange(self._key, 0, -1)
54+
messages = messages_from_dict(
55+
[json.loads(e.decode("utf-8")) for e in all_elements]
56+
)
57+
return messages
58+
59+
def add_message(self, message: BaseMessage) -> None:
60+
"""Append one message to this session."""
61+
self._redis.rpush(self._key, json.dumps(message_to_dict(message)))
62+
if self._ttl:
63+
self._redis.expire(self._key, self._ttl)
64+
65+
def clear(self) -> None:
66+
"""Clear all messages in this session."""
67+
self._redis.delete(self._key)

0 commit comments

Comments
 (0)