forked from Archives/langchain
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.3 KiB
Plaintext
87 lines
2.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "91c6a7ef",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Momento\n",
|
|
"\n",
|
|
"This notebook goes over how to use [Momento Cache](https://gomomento.com) to store chat message history using the `MomentoChatMessageHistory` class. See the Momento [docs](https://docs.momentohq.com/getting-started) for more detail on how to get set up with Momento.\n",
|
|
"\n",
|
|
"Note that, by default we will create a cache if one with the given name doesn't already exist.\n",
|
|
"\n",
|
|
"You'll need to get a Momento auth token to use this class. This can either be passed in to a momento.CacheClient if you'd like to instantiate that directly, as a named parameter `auth_token` to `MomentoChatMessageHistory.from_client_params`, or can just be set as an environment variable `MOMENTO_AUTH_TOKEN`."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"id": "d15e3302",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from datetime import timedelta\n",
|
|
"\n",
|
|
"from langchain.memory import MomentoChatMessageHistory\n",
|
|
"\n",
|
|
"session_id = \"foo\"\n",
|
|
"cache_name = \"langchain\"\n",
|
|
"ttl = timedelta(days=1),\n",
|
|
"history = MomentoChatMessageHistory.from_client_params(\n",
|
|
" session_id, \n",
|
|
" cache_name,\n",
|
|
" ttl,\n",
|
|
")\n",
|
|
"\n",
|
|
"history.add_user_message(\"hi!\")\n",
|
|
"\n",
|
|
"history.add_ai_message(\"whats up?\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"id": "64fc465e",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"[HumanMessage(content='hi!', additional_kwargs={}, example=False),\n",
|
|
" AIMessage(content='whats up?', additional_kwargs={}, example=False)]"
|
|
]
|
|
},
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"history.messages"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.3"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|