Skip to main content

Momento聊天消息历史记录 (Momento Chat Message History)

本笔记本介绍如何使用Momento Cache来存储聊天消息历史记录,使用MomentoChatMessageHistory类。有关如何设置Momento的详细信息,请参阅Momento的文档

请注意,默认情况下,如果给定名称的缓存不存在,我们将创建一个缓存。

您需要获取Momento的身份验证令牌才能使用此类。这可以直接传递给MomentoChatMessageHistory.from_client_params作为命名参数auth_token,也可以将其设置为环境变量MOMENTO_AUTH_TOKEN

from datetime import timedelta

from langchain.memory import MomentoChatMessageHistory

session_id = "foo"
cache_name = "langchain"
ttl = timedelta(days=1)
history = MomentoChatMessageHistory.from_client_params(
session_id,
cache_name,
ttl,
)

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)]