Langchain DynamoDBChatMessageHistory add sortkey and customer attributes

2 min read 04-10-2024
Langchain DynamoDBChatMessageHistory add sortkey and customer attributes


Enhancing LangChain's Chat History with DynamoDB: Sort Key and Customer Attributes

Introduction

LangChain's DynamoDBChatMessageHistory provides a robust way to store and retrieve conversational history using Amazon DynamoDB. This allows for persistent memory and improved context awareness in your chat applications. However, there are scenarios where you might need to go beyond the basic structure and incorporate additional data like sort keys and customer attributes for enhanced flexibility and searchability.

The Challenge

Imagine you're building a customer support chatbot. You want to store the conversation history for each customer, and be able to easily retrieve all conversations related to a specific topic or query. The default DynamoDBChatMessageHistory implementation only uses the conversation ID as the primary key, making it challenging to filter based on other criteria.

Solution: Adding Sort Key and Customer Attributes

To overcome this limitation, we can extend the DynamoDBChatMessageHistory class to include a sort key and customer-specific attributes. Here's a code example:

from langchain.memory.chat_history import DynamoDBChatMessageHistory
from langchain.schema import ChatMessage
from langchain.utils.yaml import yaml
import boto3

class EnhancedDynamoDBChatMessageHistory(DynamoDBChatMessageHistory):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.table_name = 'your_table_name'

    def add_message(self, message: ChatMessage):
        # Create a unique ID for the message within the conversation
        message_id = str(uuid.uuid4())
        item = {
            'conversation_id': self.conversation_id,
            'sort_key': message.created_at,  # Using timestamp as sort key
            'customer_id': self.customer_id, # Assuming customer ID is accessible 
            'message_id': message_id,
            'message': yaml.dump(message.content),
            'role': message.role,
        }
        self.table.put_item(Item=item)
        return message_id

    def load_chat_history(self) -> list[ChatMessage]:
        response = self.table.query(
            KeyConditionExpression='conversation_id = :conversation_id',
            ScanIndexForward=False  # Sort by timestamp in descending order
        )
        messages = [
            ChatMessage(
                content=yaml.load(item['message']),
                role=item['role']
            )
            for item in response['Items']
        ]
        return messages

# Usage example:
history = EnhancedDynamoDBChatMessageHistory(
    region_name='your_region', 
    customer_id='customer_123'  # Provide customer ID
)
history.add_message(ChatMessage(content='Hello!', role='user'))
history.add_message(ChatMessage(content='Hi there!', role='assistant'))
messages = history.load_chat_history()
print(messages)

This modified class allows you to:

  • Add a sort key: We use the message creation timestamp as the sort key, which enables efficient retrieval of messages in chronological order.
  • Include customer attributes: You can store customer-specific information like customer_id to easily filter conversations based on individual users.
  • Retrieve messages based on multiple criteria: Using DynamoDB's query functionality, you can fetch messages filtered by conversation ID, sort key (timestamp), and customer ID.

Benefits of Using Sort Keys and Customer Attributes

  • Improved searchability: Retrieve specific messages based on customer ID, timestamp, or other relevant criteria.
  • Enhanced organization: Structure the conversation history logically, allowing for better analysis and understanding.
  • Personalized experiences: Tailor responses based on individual customer data and previous interactions.

Conclusion

By extending LangChain's DynamoDBChatMessageHistory with a sort key and customer attributes, you can gain significant advantages in managing and utilizing conversational history. This approach provides a flexible and efficient way to store and retrieve information for diverse chat-based applications, enabling richer and more personalized interactions.