## Google Memorystore for Redis

> [Google Cloud 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.

This notebook goes over how to use [Google Cloud Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview) to store chat message history with the `MemorystoreChatMessageHistory` class.

Learn more about the package on [GitHub](https://github.com/googleapis/langchain-google-memorystore-redis-python/).

[![](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)

Open In Colab

## Before You Begin[​](#before-you-begin "Direct link to Before You Begin")

To run this notebook, you will need to do the following:

*   [Create a Google Cloud Project](https://developers.google.com/workspace/guides/create-project)
*   [Enable the Memorystore for Redis API](https://console.cloud.google.com/flows/enableapi?apiid=redis.googleapis.com)
*   [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.

After confirmed access to database in the runtime environment of this notebook, filling the following values and run the cell before running example scripts.

```
# @markdown Please specify an endpoint associated with the instance or demo purpose.ENDPOINT = "redis://127.0.0.1:6379"  # @param {type:"string"}
```

### 🦜🔗 Library Installation[​](#library-installation "Direct link to 🦜🔗 Library Installation")

The integration lives in its own `langchain-google-memorystore-redis` package, so we need to install it.

```
%pip install -upgrade --quiet langchain-google-memorystore-redis
```

**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.

```
# # Automatically restart kernel after installs so that your environment can access the new packages# import IPython# app = IPython.Application.instance()# app.kernel.do_shutdown(True)
```

### ☁ Set Your Google Cloud Project[​](#set-your-google-cloud-project "Direct link to ☁ Set Your Google Cloud Project")

Set your Google Cloud project so that you can leverage Google Cloud resources within this notebook.

If you don’t know your project ID, try the following:

*   Run `gcloud config list`.
*   Run `gcloud projects list`.
*   See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113).

```
# @markdown Please fill in the value below with your Google Cloud project ID and then run the cell.PROJECT_ID = "my-project-id"  # @param {type:"string"}# Set the project id!gcloud config set project {PROJECT_ID}
```

### 🔐 Authentication[​](#authentication "Direct link to 🔐 Authentication")

Authenticate to Google Cloud as the IAM user logged into this notebook in order to access your Google Cloud Project.

*   If you are using Colab to run this notebook, use the cell below and continue.
*   If you are using Vertex AI Workbench, check out the setup instructions [here](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env).

```
from google.colab import authauth.authenticate_user()
```

## Basic Usage[​](#basic-usage "Direct link to Basic Usage")

### MemorystoreChatMessageHistory[​](#memorystorechatmessagehistory "Direct link to MemorystoreChatMessageHistory")

To initialize the `MemorystoreMessageHistory` class you need to provide only 2 things:

1.  `redis_client` - An instance of a Memorystore Redis.
2.  `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.

```
import redisfrom langchain_google_memorystore_redis import MemorystoreChatMessageHistory# Connect to a Memorystore for Redis instanceredis_client = redis.from_url("redis://127.0.0.1:6379")message_history = MemorystoreChatMessageHistory(redis_client, session_id="session1")
```

#### Cleaning up[​](#cleaning-up "Direct link to Cleaning up")

When the history of a specific session is obsolete and can be deleted, it can be done the following way.

**Note:** Once deleted, the data is no longer stored in Memorystore for Redis and is gone forever.