Momento Cache
Momento Cache 是世界上第一個真正的無伺服器快取服務。它提供即時彈性、擴展到零的能力和極快的效能。
本筆記本介紹如何使用 Momento Cache,透過 MomentoChatMessageHistory
類別儲存聊天訊息歷史記錄。有關如何設定 Momento 的更多詳細資訊,請參閱 Momento 文檔。
請注意,預設情況下,如果具有給定名稱的快取尚不存在,我們將建立一個快取。
您需要取得 Momento API 金鑰才能使用此類別。如果您想直接實例化 momento.CacheClient,可以將其傳遞到 momento.CacheClient 中,作為 MomentoChatMessageHistory.from_client_params
的命名參數 api_key
,或者可以僅將其設定為環境變數 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?")
API 參考:MomentoChatMessageHistory
history.messages
[HumanMessage(content='hi!', additional_kwargs={}, example=False),
AIMessage(content='whats up?', additional_kwargs={}, example=False)]