Skip to content

Conversation Memory Drivers

Overview

You can persist and load memory by using Conversation Memory Drivers. You can build drivers for your own data stores by extending BaseConversationMemoryDriver.

Conversation Memory Drivers

Griptape Cloud

The GriptapeCloudConversationMemoryDriver allows you to persist Conversation Memory in Griptape Cloud. It provides seamless integration with Griptape's cloud-based Threads and Messages resources.

import os
import uuid

from griptape.drivers import GriptapeCloudConversationMemoryDriver
from griptape.memory.structure import ConversationMemory
from griptape.structures import Agent

conversation_id = uuid.uuid4().hex
cloud_conversation_driver = GriptapeCloudConversationMemoryDriver(
    api_key=os.environ["GT_CLOUD_API_KEY"],
)
agent = Agent(conversation_memory=ConversationMemory(conversation_memory_driver=cloud_conversation_driver))

agent.run("My name is Jeff.")
agent.run("What is my name?")

Local

The LocalConversationMemoryDriver allows you to persist Conversation Memory in a local JSON file.

from griptape.drivers import LocalConversationMemoryDriver
from griptape.memory.structure import ConversationMemory
from griptape.structures import Agent

local_driver = LocalConversationMemoryDriver(persist_file="memory.json")
agent = Agent(conversation_memory=ConversationMemory(conversation_memory_driver=local_driver))

agent.run("Surfing is my favorite sport.")
agent.run("What is my favorite sport?")

Amazon DynamoDb

Info

This driver requires the drivers-memory-conversation-amazon-dynamodb extra.

The AmazonDynamoDbConversationMemoryDriver allows you to persist Conversation Memory in Amazon DynamoDb.

import os
import uuid

from griptape.drivers import AmazonDynamoDbConversationMemoryDriver
from griptape.memory.structure import ConversationMemory
from griptape.structures import Agent

conversation_id = uuid.uuid4().hex
dynamodb_driver = AmazonDynamoDbConversationMemoryDriver(
    table_name=os.environ["DYNAMODB_TABLE_NAME"],
    partition_key="id",
    value_attribute_key="memory",
    partition_key_value=conversation_id,
)

agent = Agent(conversation_memory=ConversationMemory(conversation_memory_driver=dynamodb_driver))

agent.run("My name is Jeff.")
agent.run("What is my name?")
Optional parameters sort_key and sort_key_value can be supplied for tables with a composite primary key.

Redis

Info

This driver requires the drivers-memory-conversation-redis extra.

The RedisConversationMemoryDriver allows you to persist Conversation Memory in Redis.

import os
import uuid

from griptape.drivers import RedisConversationMemoryDriver
from griptape.memory.structure import ConversationMemory
from griptape.structures import Agent

conversation_id = uuid.uuid4().hex
redis_conversation_driver = RedisConversationMemoryDriver(
    host=os.environ["REDIS_HOST"],
    port=int(os.environ["REDIS_PORT"]),
    password=os.environ["REDIS_PASSWORD"],
    index=os.environ["REDIS_INDEX"],
    conversation_id=conversation_id,
)

agent = Agent(conversation_memory=ConversationMemory(conversation_memory_driver=redis_conversation_driver))

agent.run("My name is Jeff.")
agent.run("What is my name?")