Skip to main content
Open In Colab在 GitHub 上打开

Momento 缓存

Momento Cache 是世界上第一个真正的无服务器缓存服务。它提供即时弹性,缩放到零 功能,以及超快的性能。

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

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

您需要获取 Momento API 密钥才能使用此类。这可以传递给 momento。CacheClient(如果您想直接实例化它)作为命名参数api_keyMomentoChatMessageHistory.from_client_params,或者可以仅设置为环境变量MOMENTO_API_KEY.

from datetime import timedelta

from langchain_community.chat_message_histories 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)]