langchain.memory.chat_message_histories.momento
.MomentoChatMessageHistory¶
- class langchain.memory.chat_message_histories.momento.MomentoChatMessageHistory(session_id: str, cache_client: momento.CacheClient, cache_name: str, *, key_prefix: str = 'message_store:', ttl: Optional[timedelta] = None, ensure_cache_exists: bool = True)[source]¶
Chat message history cache that uses Momento as a backend.
Instantiate a chat message history cache that uses Momento as a backend.
Note: to instantiate the cache client passed to MomentoChatMessageHistory, you must have a Momento account at https://gomomento.com/.
- Parameters
session_id (str) – The session ID to use for this chat session.
cache_client (CacheClient) – The Momento cache client.
cache_name (str) – The name of the cache to use to store the messages.
key_prefix (str, optional) – The prefix to apply to the cache key. Defaults to “message_store:”.
ttl (Optional[timedelta], optional) – The TTL to use for the messages. Defaults to None, ie the default TTL of the cache will be used.
ensure_cache_exists (bool, optional) – Create the cache if it doesn’t exist. Defaults to True.
- Raises
ImportError – Momento python package is not installed.
TypeError – cache_client is not of type momento.CacheClientObject
Attributes
messages
Retrieve the messages from Momento.
Methods
__init__
(session_id, cache_client, cache_name, *)Instantiate a chat message history cache that uses Momento as a backend.
add_ai_message
(message)Convenience method for adding an AI message string to the store.
add_message
(message)Store a message in the cache.
add_user_message
(message)Convenience method for adding a human message string to the store.
clear
()Remove the session's messages from the cache.
from_client_params
(session_id, cache_name, ...)Construct cache from CacheClient parameters.
- __init__(session_id: str, cache_client: momento.CacheClient, cache_name: str, *, key_prefix: str = 'message_store:', ttl: Optional[timedelta] = None, ensure_cache_exists: bool = True)[source]¶
Instantiate a chat message history cache that uses Momento as a backend.
Note: to instantiate the cache client passed to MomentoChatMessageHistory, you must have a Momento account at https://gomomento.com/.
- Parameters
session_id (str) – The session ID to use for this chat session.
cache_client (CacheClient) – The Momento cache client.
cache_name (str) – The name of the cache to use to store the messages.
key_prefix (str, optional) – The prefix to apply to the cache key. Defaults to “message_store:”.
ttl (Optional[timedelta], optional) – The TTL to use for the messages. Defaults to None, ie the default TTL of the cache will be used.
ensure_cache_exists (bool, optional) – Create the cache if it doesn’t exist. Defaults to True.
- Raises
ImportError – Momento python package is not installed.
TypeError – cache_client is not of type momento.CacheClientObject
- add_ai_message(message: str) None ¶
Convenience method for adding an AI message string to the store.
- Parameters
message – The string contents of an AI message.
- add_message(message: BaseMessage) None [source]¶
Store a message in the cache.
- Parameters
message (BaseMessage) – The message object to store.
- Raises
SdkException – Momento service or network error.
Exception – Unexpected response.
- add_user_message(message: str) None ¶
Convenience method for adding a human message string to the store.
- Parameters
message – The string contents of a human message.
- clear() None [source]¶
Remove the session’s messages from the cache.
- Raises
SdkException – Momento service or network error.
Exception – Unexpected response.
- classmethod from_client_params(session_id: str, cache_name: str, ttl: timedelta, *, configuration: Optional[momento.config.Configuration] = None, api_key: Optional[str] = None, auth_token: Optional[str] = None, **kwargs: Any) MomentoChatMessageHistory [source]¶
Construct cache from CacheClient parameters.