.ipynb
.pdf
Dynamodb Chat Message History
 Contents 
DynamoDBChatMessageHistory
DynamoDBChatMessageHistory with Custom Endpoint URL
Agent with DynamoDB Memory
Dynamodb Chat Message History#
This notebook goes over how to use Dynamodb to store chat message history.
First make sure you have correctly configured the AWS CLI. Then make sure you have installed boto3.
Next, create the DynamoDB Table where we will be storing messages:
import boto3
# Get the service resource.
dynamodb = boto3.resource('dynamodb')
# Create the DynamoDB table.
table = dynamodb.create_table(
    TableName='SessionTable',
    KeySchema=[
        {
            'AttributeName': 'SessionId',
            'KeyType': 'HASH'
        }
    ],
    AttributeDefinitions=[
        {
            'AttributeName': 'SessionId',
            'AttributeType': 'S'
        }
    ],
    BillingMode='PAY_PER_REQUEST',
)
# Wait until the table exists.
table.meta.client.get_waiter('table_exists').wait(TableName='SessionTable')
# Print out some data about the table.
print(table.item_count)
0
DynamoDBChatMessageHistory#
from langchain.memory.chat_message_histories import DynamoDBChatMessageHistory
history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="0")
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
 AIMessage(content='whats up?', additional_kwargs={}, example=False)]
DynamoDBChatMessageHistory with Custom Endpoint URL#
rtdocs_stable/api.python.langchain.com/en/stable/modules/memory/examples/dynamodb_chat_message_history.html