Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
160 changes: 104 additions & 56 deletions docs/chat_message_history.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,123 +6,167 @@
"id": "6-0_o3DxsFGi"
},
"source": [
"Google Database\n",
"# Google Memorystore for Redis\n",
"\n",
"Use [Google Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview) to store chat message history for LangChain."
"> [Google Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview) is a fully-managed service that is powered by the Redis in-memory data store to build application caches that provide sub-millisecond data access. Extend your database application to build AI-powered experiences leveraging Memorystore for Redis's Langchain integrations.\n",
"\n",
"This notebook goes over how to use [Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview) to store chat message history with the `MemorystoreChatMessageHistory` class.\n",
"\n",
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/googleapis/langchain-google-memorystore-redis-python/blob/main/docs/chat_message_history.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "dWakBoPnsFGj"
},
"metadata": {},
"source": [
"## Pre-reqs"
"## Before You Begin\n",
"\n",
"To run this notebook, you will need to do the following:\n",
"* [Create a Google Cloud Project](https://developers.google.com/workspace/guides/create-project)\n",
"* [Create a Memorystore for Redis instance](https://cloud.google.com/memorystore/docs/redis/create-instance-console). Ensure that the version is greater than or equal to 5.0.\n",
"\n",
"After confirmed access to database in the runtime environment of this notebook, filling the following values and run the cell before running example scripts."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EudfLv_UsFGk"
},
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Setting Up a Memorystore for Redis Instance\n",
"\n",
"Before proceeding, an active Memorystore for Redis instance is needed to store chat message history:\n",
"\n",
"* Create a Memorystore for Redis Instance (>= 5.0): If an instance doesn't exist, follow the instructions at https://cloud.google.com/memorystore/docs/redis/create-instance-console to create a new one. Ensure the version is greater than or equal to 5.0.\n",
"* Obtain Endpoint: Note the endpoint associated with the instance."
"# @markdown Please specify an endpoint associated with the instance or demo purpose.\n",
"ENDPOINT = \"redis://127.0.0.1:6379\" # @param {type:\"string\"}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "J5nxjYxHsFGk"
},
"metadata": {},
"source": [
"### Installing the LangChain Memorystore for Redis Module\n",
"### 🦜🔗 Library Installation\n",
"\n",
"Interaction with the Memorystore for Redis instance from LangChain requires installing the necessary module:"
"The integration lives in its own `langchain-google-memorystore-redis` package, so we need to install it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [],
"id": "iLwVMVkYsFGk"
"id": "iLwVMVkYsFGk",
"tags": []
},
"outputs": [],
"source": [
"# Install Memorystore for Redis for LangChain module\n",
"%pip install langchain_google_memorystore_redis"
"%pip install -upgrade --quiet langchain-google-memorystore-redis"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2L7kMu__sFGl"
},
"metadata": {},
"source": [
"## Basic Usage"
"**Colab only:** Uncomment the following cell to restart the kernel or use the button to restart the kernel. For Vertex AI Workbench you can restart the terminal using the button on top."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# # Automatically restart kernel after installs so that your environment can access the new packages\n",
"# import IPython\n",
"\n",
"# app = IPython.Application.instance()\n",
"# app.kernel.do_shutdown(True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "A2fT1iEhsFGl"
},
"metadata": {},
"source": [
"### Initialize a MemorystoreChatMessageHistory\n",
"### ☁ Set Your Google Cloud Project\n",
"Set your Google Cloud project so that you can leverage Google Cloud resources within this notebook.\n",
"\n",
"Each chat message history object must have a unique session ID. If the session ID already has messages stored in Redis, they will can be retrieved."
"If you don't know your project ID, try the following:\n",
"\n",
"* Run `gcloud config list`.\n",
"* Run `gcloud projects list`.\n",
"* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YEDKWR6asFGl"
},
"metadata": {},
"outputs": [],
"source": [
"import redis\n",
"from langchain_google_memorystore_redis import MemorystoreChatMessageHistory\n",
"# @markdown Please fill in the value below with your Google Cloud project ID and then run the cell.\n",
"\n",
"# Connect to a Memorystore for Redis instance\n",
"redis_client = redis.from_url(\"redis://127.0.0.1:6379\")\n",
"PROJECT_ID = \"my-project-id\" # @param {type:\"string\"}\n",
"\n",
"message_history = MemorystoreChatMessageHistory(redis_client, session_id=\"session1\")"
"# Set the project id\n",
"!gcloud config set project {PROJECT_ID}"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "EmoJcTgosFGl"
},
"metadata": {},
"source": [
"### Add Messages"
"### 🔐 Authentication\n",
"Authenticate to Google Cloud as the IAM user logged into this notebook in order to access your Google Cloud Project.\n",
"\n",
"* If you are using Colab to run this notebook, use the cell below and continue.\n",
"* If you are using Vertex AI Workbench, check out the setup instructions [here](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from google.colab import auth\n",
"\n",
"auth.authenticate_user()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "gB1PGe6wsFGm"
"id": "2L7kMu__sFGl"
},
"outputs": [],
"source": [
"message_history.add_ai_message(\"Hey! I am AI!\")\n",
"message_history.add_user_message(\"Hey! I am human!\")"
"## Basic Usage"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "02xxvmzTsFGm"
"id": "A2fT1iEhsFGl"
},
"source": [
"### MemorystoreChatMessageHistory\n",
"\n",
"To initialize the `MemorystoreMessageHistory` class you need to provide only 2 things:\n",
"\n",
"1. `redis_client` - An instance of a Memorystore Redis.\n",
"1. `session_id` - Each chat message history object must have a unique session ID. If the session ID already has messages stored in Redis, they will can be retrieved."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YEDKWR6asFGl"
},
"outputs": [],
"source": [
"### Retrieve All Messages Stored in the Session"
"import redis\n",
"from langchain_google_memorystore_redis import MemorystoreChatMessageHistory\n",
"\n",
"# Connect to a Memorystore for Redis instance\n",
"redis_client = redis.from_url(\"redis://127.0.0.1:6379\")\n",
"\n",
"message_history = MemorystoreChatMessageHistory(redis_client, session_id=\"session1\")"
]
},
{
Expand All @@ -142,7 +186,11 @@
"id": "sFJdt3ubsFGo"
},
"source": [
"### Clear Messages"
"#### Cleaning up\n",
"\n",
"When the history of a specific session is obsolete and can be deleted, it can be done the following way.\n",
"\n",
"**Note:** Once deleted, the data is no longer stored in Memorystore for Redis and is gone forever."
]
},
{
Expand All @@ -158,6 +206,9 @@
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
Expand All @@ -174,9 +225,6 @@
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
},
"colab": {
"provenance": []
}
},
"nbformat": 4,
Expand Down
Loading