-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
RedisConversationMemoryDriver
(#787)
Co-authored-by: torabshaikh <[email protected]>
- Loading branch information
Showing
7 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
56 changes: 56 additions & 0 deletions
56
griptape/drivers/memory/conversation/redis_conversation_memory_driver.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,56 @@ | ||
from __future__ import annotations | ||
import uuid | ||
from attr import define, field, Factory | ||
from typing import Optional, TYPE_CHECKING | ||
from griptape.drivers import BaseConversationMemoryDriver | ||
from griptape.memory.structure import BaseConversationMemory | ||
from griptape.utils.import_utils import import_optional_dependency | ||
|
||
if TYPE_CHECKING: | ||
from redis import Redis | ||
|
||
|
||
@define | ||
class RedisConversationMemoryDriver(BaseConversationMemoryDriver): | ||
"""A Conversation Memory Driver for Redis. | ||
This driver interfaces with a Redis instance and utilizes the Redis hashes and RediSearch module to store, | ||
retrieve, and query conversations in a structured manner. | ||
Proper setup of the Redis instance and RediSearch is necessary for the driver to function correctly. | ||
Attributes: | ||
host: The host of the Redis instance. | ||
port: The port of the Redis instance. | ||
db: The database of the Redis instance. | ||
password: The password of the Redis instance. | ||
index: The name of the index to use. | ||
conversation_id: The id of the conversation. | ||
""" | ||
|
||
host: str = field(kw_only=True, metadata={"serializable": True}) | ||
port: int = field(kw_only=True, metadata={"serializable": True}) | ||
db: int = field(kw_only=True, default=0, metadata={"serializable": True}) | ||
password: Optional[str] = field(default=None, kw_only=True, metadata={"serializable": False}) | ||
index: str = field(kw_only=True, metadata={"serializable": True}) | ||
conversation_id: str = field(kw_only=True, default=uuid.uuid4().hex) | ||
|
||
client: Redis = field( | ||
default=Factory( | ||
lambda self: import_optional_dependency("redis").Redis( | ||
host=self.host, port=self.port, db=self.db, password=self.password, decode_responses=False | ||
), | ||
takes_self=True, | ||
) | ||
) | ||
|
||
def store(self, memory: BaseConversationMemory) -> None: | ||
self.client.hset(self.index, self.conversation_id, memory.to_json()) | ||
|
||
def load(self) -> Optional[BaseConversationMemory]: | ||
key = self.index | ||
memory_json = self.client.hget(key, self.conversation_id) | ||
if memory_json: | ||
memory = BaseConversationMemory.from_json(memory_json) | ||
memory.driver = self | ||
return memory | ||
return None |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
40 changes: 40 additions & 0 deletions
40
tests/unit/drivers/memory/conversation/test_redis_conversation_memory_driver.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,40 @@ | ||
import pytest | ||
import redis | ||
from griptape.memory.structure.base_conversation_memory import BaseConversationMemory | ||
from griptape.drivers.memory.conversation.redis_conversation_memory_driver import RedisConversationMemoryDriver | ||
|
||
TEST_CONVERSATION = '{"type": "ConversationMemory", "runs": [{"type": "Run", "id": "729ca6be5d79433d9762eb06dfd677e2", "input": "Hi There, Hello", "output": "Hello! How can I assist you today?"}], "max_runs": 2}' | ||
CONVERSATION_ID = "117151897f344ff684b553d0655d8f39" | ||
INDEX = "griptape_converstaion" | ||
HOST = "127.0.0.1" | ||
PORT = 6379 | ||
PASSWORD = "" | ||
|
||
|
||
class TestRedisConversationMemoryDriver: | ||
@pytest.fixture(autouse=True) | ||
def mock_redis(self, mocker): | ||
mocker.patch.object(redis.StrictRedis, "hset", return_value=None) | ||
mocker.patch.object(redis.StrictRedis, "keys", return_value=[b"test"]) | ||
mocker.patch.object(redis.StrictRedis, "hget", return_value=TEST_CONVERSATION) | ||
|
||
fake_redisearch = mocker.MagicMock() | ||
fake_redisearch.search = mocker.MagicMock(return_value=mocker.MagicMock(docs=[])) | ||
fake_redisearch.info = mocker.MagicMock(side_effect=Exception("Index not found")) | ||
fake_redisearch.create_index = mocker.MagicMock(return_value=None) | ||
|
||
mocker.patch.object(redis.StrictRedis, "ft", return_value=fake_redisearch) | ||
|
||
@pytest.fixture | ||
def driver(self): | ||
return RedisConversationMemoryDriver(host=HOST, port=PORT, db=0, index=INDEX, conversation_id=CONVERSATION_ID) | ||
|
||
def test_store(self, driver): | ||
memory = BaseConversationMemory.from_json(TEST_CONVERSATION) | ||
assert driver.store(memory) == None | ||
|
||
def test_load(self, driver): | ||
memory = driver.load() | ||
assert memory.type == "ConversationMemory" | ||
assert memory.max_runs == 2 | ||
assert memory.runs == BaseConversationMemory.from_json(TEST_CONVERSATION).runs |