|
6 | 6 | "id": "6-0_o3DxsFGi" |
7 | 7 | }, |
8 | 8 | "source": [ |
9 | | - "Google Database\n", |
| 9 | + "# Google Memorystore for Redis\n", |
10 | 10 | "\n", |
11 | | - "Use [Google Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview) to store chat message history for LangChain." |
| 11 | + "> [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", |
| 12 | + "\n", |
| 13 | + "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", |
| 14 | + "\n", |
| 15 | + "[](https://colab.research.google.com/github/googleapis/langchain-google-memorystore-redis-python/blob/main/docs/chat_message_history.ipynb)" |
12 | 16 | ] |
13 | 17 | }, |
14 | 18 | { |
15 | 19 | "cell_type": "markdown", |
16 | | - "metadata": { |
17 | | - "id": "dWakBoPnsFGj" |
18 | | - }, |
| 20 | + "metadata": {}, |
19 | 21 | "source": [ |
20 | | - "## Pre-reqs" |
| 22 | + "## Before You Begin\n", |
| 23 | + "\n", |
| 24 | + "To run this notebook, you will need to do the following:\n", |
| 25 | + "* [Create a Google Cloud Project](https://developers.google.com/workspace/guides/create-project)\n", |
| 26 | + "* [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", |
| 27 | + "\n", |
| 28 | + "After confirmed access to database in the runtime environment of this notebook, filling the following values and run the cell before running example scripts." |
21 | 29 | ] |
22 | 30 | }, |
23 | 31 | { |
24 | | - "cell_type": "markdown", |
25 | | - "metadata": { |
26 | | - "id": "EudfLv_UsFGk" |
27 | | - }, |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
28 | 36 | "source": [ |
29 | | - "### Setting Up a Memorystore for Redis Instance\n", |
30 | | - "\n", |
31 | | - "Before proceeding, an active Memorystore for Redis instance is needed to store chat message history:\n", |
32 | | - "\n", |
33 | | - "* 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", |
34 | | - "* Obtain Endpoint: Note the endpoint associated with the instance." |
| 37 | + "# @markdown Please specify an endpoint associated with the instance or demo purpose.\n", |
| 38 | + "ENDPOINT = \"redis://127.0.0.1:6379\" # @param {type:\"string\"}" |
35 | 39 | ] |
36 | 40 | }, |
37 | 41 | { |
38 | 42 | "cell_type": "markdown", |
39 | | - "metadata": { |
40 | | - "id": "J5nxjYxHsFGk" |
41 | | - }, |
| 43 | + "metadata": {}, |
42 | 44 | "source": [ |
43 | | - "### Installing the LangChain Memorystore for Redis Module\n", |
| 45 | + "### 🦜🔗 Library Installation\n", |
44 | 46 | "\n", |
45 | | - "Interaction with the Memorystore for Redis instance from LangChain requires installing the necessary module:" |
| 47 | + "The integration lives in its own `langchain-google-memorystore-redis` package, so we need to install it." |
46 | 48 | ] |
47 | 49 | }, |
48 | 50 | { |
49 | 51 | "cell_type": "code", |
50 | 52 | "execution_count": null, |
51 | 53 | "metadata": { |
52 | | - "tags": [], |
53 | | - "id": "iLwVMVkYsFGk" |
| 54 | + "id": "iLwVMVkYsFGk", |
| 55 | + "tags": [] |
54 | 56 | }, |
55 | 57 | "outputs": [], |
56 | 58 | "source": [ |
57 | | - "# Install Memorystore for Redis for LangChain module\n", |
58 | | - "%pip install langchain_google_memorystore_redis" |
| 59 | + "%pip install -upgrade --quiet langchain-google-memorystore-redis" |
59 | 60 | ] |
60 | 61 | }, |
61 | 62 | { |
62 | 63 | "cell_type": "markdown", |
63 | | - "metadata": { |
64 | | - "id": "2L7kMu__sFGl" |
65 | | - }, |
| 64 | + "metadata": {}, |
66 | 65 | "source": [ |
67 | | - "## Basic Usage" |
| 66 | + "**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." |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "code", |
| 71 | + "execution_count": null, |
| 72 | + "metadata": {}, |
| 73 | + "outputs": [], |
| 74 | + "source": [ |
| 75 | + "# # Automatically restart kernel after installs so that your environment can access the new packages\n", |
| 76 | + "# import IPython\n", |
| 77 | + "\n", |
| 78 | + "# app = IPython.Application.instance()\n", |
| 79 | + "# app.kernel.do_shutdown(True)" |
68 | 80 | ] |
69 | 81 | }, |
70 | 82 | { |
71 | 83 | "cell_type": "markdown", |
72 | | - "metadata": { |
73 | | - "id": "A2fT1iEhsFGl" |
74 | | - }, |
| 84 | + "metadata": {}, |
75 | 85 | "source": [ |
76 | | - "### Initialize a MemorystoreChatMessageHistory\n", |
| 86 | + "### ☁ Set Your Google Cloud Project\n", |
| 87 | + "Set your Google Cloud project so that you can leverage Google Cloud resources within this notebook.\n", |
77 | 88 | "\n", |
78 | | - "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." |
| 89 | + "If you don't know your project ID, try the following:\n", |
| 90 | + "\n", |
| 91 | + "* Run `gcloud config list`.\n", |
| 92 | + "* Run `gcloud projects list`.\n", |
| 93 | + "* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)." |
79 | 94 | ] |
80 | 95 | }, |
81 | 96 | { |
82 | 97 | "cell_type": "code", |
83 | 98 | "execution_count": null, |
84 | | - "metadata": { |
85 | | - "id": "YEDKWR6asFGl" |
86 | | - }, |
| 99 | + "metadata": {}, |
87 | 100 | "outputs": [], |
88 | 101 | "source": [ |
89 | | - "import redis\n", |
90 | | - "from langchain_google_memorystore_redis import MemorystoreChatMessageHistory\n", |
| 102 | + "# @markdown Please fill in the value below with your Google Cloud project ID and then run the cell.\n", |
91 | 103 | "\n", |
92 | | - "# Connect to a Memorystore for Redis instance\n", |
93 | | - "redis_client = redis.from_url(\"redis://127.0.0.1:6379\")\n", |
| 104 | + "PROJECT_ID = \"my-project-id\" # @param {type:\"string\"}\n", |
94 | 105 | "\n", |
95 | | - "message_history = MemorystoreChatMessageHistory(redis_client, session_id=\"session1\")" |
| 106 | + "# Set the project id\n", |
| 107 | + "!gcloud config set project {PROJECT_ID}" |
96 | 108 | ] |
97 | 109 | }, |
98 | 110 | { |
99 | 111 | "cell_type": "markdown", |
100 | | - "metadata": { |
101 | | - "id": "EmoJcTgosFGl" |
102 | | - }, |
| 112 | + "metadata": {}, |
103 | 113 | "source": [ |
104 | | - "### Add Messages" |
| 114 | + "### 🔐 Authentication\n", |
| 115 | + "Authenticate to Google Cloud as the IAM user logged into this notebook in order to access your Google Cloud Project.\n", |
| 116 | + "\n", |
| 117 | + "* If you are using Colab to run this notebook, use the cell below and continue.\n", |
| 118 | + "* If you are using Vertex AI Workbench, check out the setup instructions [here](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env)." |
105 | 119 | ] |
106 | 120 | }, |
107 | 121 | { |
108 | 122 | "cell_type": "code", |
109 | 123 | "execution_count": null, |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [], |
| 126 | + "source": [ |
| 127 | + "from google.colab import auth\n", |
| 128 | + "\n", |
| 129 | + "auth.authenticate_user()" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "markdown", |
110 | 134 | "metadata": { |
111 | | - "id": "gB1PGe6wsFGm" |
| 135 | + "id": "2L7kMu__sFGl" |
112 | 136 | }, |
113 | | - "outputs": [], |
114 | 137 | "source": [ |
115 | | - "message_history.add_ai_message(\"Hey! I am AI!\")\n", |
116 | | - "message_history.add_user_message(\"Hey! I am human!\")" |
| 138 | + "## Basic Usage" |
117 | 139 | ] |
118 | 140 | }, |
119 | 141 | { |
120 | 142 | "cell_type": "markdown", |
121 | 143 | "metadata": { |
122 | | - "id": "02xxvmzTsFGm" |
| 144 | + "id": "A2fT1iEhsFGl" |
| 145 | + }, |
| 146 | + "source": [ |
| 147 | + "### MemorystoreChatMessageHistory\n", |
| 148 | + "\n", |
| 149 | + "To initialize the `MemorystoreMessageHistory` class you need to provide only 2 things:\n", |
| 150 | + "\n", |
| 151 | + "1. `redis_client` - An instance of a Memorystore Redis.\n", |
| 152 | + "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." |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "metadata": { |
| 159 | + "id": "YEDKWR6asFGl" |
123 | 160 | }, |
| 161 | + "outputs": [], |
124 | 162 | "source": [ |
125 | | - "### Retrieve All Messages Stored in the Session" |
| 163 | + "import redis\n", |
| 164 | + "from langchain_google_memorystore_redis import MemorystoreChatMessageHistory\n", |
| 165 | + "\n", |
| 166 | + "# Connect to a Memorystore for Redis instance\n", |
| 167 | + "redis_client = redis.from_url(\"redis://127.0.0.1:6379\")\n", |
| 168 | + "\n", |
| 169 | + "message_history = MemorystoreChatMessageHistory(redis_client, session_id=\"session1\")" |
126 | 170 | ] |
127 | 171 | }, |
128 | 172 | { |
|
142 | 186 | "id": "sFJdt3ubsFGo" |
143 | 187 | }, |
144 | 188 | "source": [ |
145 | | - "### Clear Messages" |
| 189 | + "#### Cleaning up\n", |
| 190 | + "\n", |
| 191 | + "When the history of a specific session is obsolete and can be deleted, it can be done the following way.\n", |
| 192 | + "\n", |
| 193 | + "**Note:** Once deleted, the data is no longer stored in Memorystore for Redis and is gone forever." |
146 | 194 | ] |
147 | 195 | }, |
148 | 196 | { |
|
158 | 206 | } |
159 | 207 | ], |
160 | 208 | "metadata": { |
| 209 | + "colab": { |
| 210 | + "provenance": [] |
| 211 | + }, |
161 | 212 | "kernelspec": { |
162 | 213 | "display_name": "Python 3 (ipykernel)", |
163 | 214 | "language": "python", |
|
174 | 225 | "nbconvert_exporter": "python", |
175 | 226 | "pygments_lexer": "ipython3", |
176 | 227 | "version": "3.11.6" |
177 | | - }, |
178 | | - "colab": { |
179 | | - "provenance": [] |
180 | 228 | } |
181 | 229 | }, |
182 | 230 | "nbformat": 4, |
|
0 commit comments