From e6bb8b81b17a73fcacc6df68acd047a4fb4de507 Mon Sep 17 00:00:00 2001 From: Trisha Mandal <97489901+trish11953@users.noreply.github.com> Date: Tue, 29 Aug 2023 14:58:32 -0700 Subject: [PATCH] updated dynamodb example --- .../store-conversation-memory-in-dynamodb.md | 62 ++++++++----------- 1 file changed, 26 insertions(+), 36 deletions(-) diff --git a/docs/examples/store-conversation-memory-in-dynamodb.md b/docs/examples/store-conversation-memory-in-dynamodb.md index dac73c3f..4654d79c 100644 --- a/docs/examples/store-conversation-memory-in-dynamodb.md +++ b/docs/examples/store-conversation-memory-in-dynamodb.md @@ -1,47 +1,37 @@ To store your conversation on DynamoDB you can use DynamoDbConversationMemoryDriver. ```python -from griptape.memory.structure import ConversationMemory -from griptape.memory.structure import ConversationMemoryElement, Turn, Message from griptape.drivers import DynamoDbConversationMemoryDriver +from griptape.memory.structure import ConversationMemory -# Instantiate DynamoDbConversationMemoryDriver -dynamo_driver = DynamoDbConversationMemoryDriver( - aws_region="us-east-1", - table_name="conversations", - partition_key="convo_id", - value_attribute_key="convo_data", - partition_key_value="convo1" +# Initialize the DynamoDB driver +driver = DynamoDbConversationMemoryDriver( + table_name='conversation_memory', + partition_key='user_id', + value_attribute_key='conversation_data', + partition_key_value='12345' # This could be a user_id or session_id ) -# Create a ConversationMemory structure -conv_mem = ConversationMemory( - turns=[ - Turn( - turn_index=0, - system=Message("Hello"), - user=Message("Hi") - ), - Turn( - turn_index=1, - system=Message("How can I assist you today?"), - user=Message("I need some information") - ) - ], - latest_turn=Turn( - turn_index=2, - system=Message("Sure, what information do you need?"), - user=None # user has not yet responded - ), - driver=dynamo_driver # set the driver +# Create a sample conversation memory object +conversation_memory = ConversationMemory( + id="12345", + history=[ + { + "user": "Hello, how are you?", + "bot": "I'm good, thanks for asking!" + } + ] ) -# Store the conversation in DynamoDB -dynamo_driver.store(conv_mem) - -# Load the conversation from DynamoDB -loaded_conv_mem = dynamo_driver.load() +# Store the memory +driver.store(conversation_memory) -# Display the loaded conversation -print(loaded_conv_mem.to_json()) +# Load the memory back +loaded_memory = driver.load() +if loaded_memory: + for entry in loaded_memory.history: + print(f"User: {entry['user']}") + print(f"Bot: {entry['bot']}") +else: + print("No conversation found for the given partition_key_value.") ``` \ No newline at end of file