|
8 | 8 | "source": [ |
9 | 9 | "# Google Memorystore for Redis\n", |
10 | 10 | "\n", |
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", |
| 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.\n", |
12 | 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 [save, load and delete langchain documents](https://python.langchain.com/docs/modules/data_connection/document_loaders/) with `MemorystoreDocumentLoader` and `MemorystoreDocumentSaver`.\n", |
| 13 | + "> [Google Memorystore for Redis Cluster](https://cloud.google.com/memorystore/docs/cluster/memorystore-for-redis-cluster-overview) is a fully-managed service that is powered by the Redis in-memory data store. Applications running on Google Cloud can achieve extreme performance by leveraging the highly scalable, available, secure Redis service without the burden of managing complex Redis deployments.\n", |
| 14 | + "\n", |
| 15 | + "> [Google Memorystore for Valkey](https://cloud.google.com/memorystore/docs/valkey/product-overview) is a fully-managed Valkey Cluster service for Google Cloud, and is 100% compatible with the Google Memorystore for Redis Cluster LangChain integration.\n", |
| 16 | + "\n", |
| 17 | + "Extend your database application to build AI-powered experiences leveraging Memorystore's Langchain integrations.\n", |
| 18 | + "\n", |
| 19 | + "This notebook goes over how to use [Memorystore for Redis](https://cloud.google.com/memorystore/docs/redis/memorystore-for-redis-overview), [Memorystore for Redis Cluster](https://cloud.google.com/memorystore/docs/cluster/memorystore-for-redis-cluster-overview) or [Memorystore for Valkey](https://cloud.google.com/memorystore/docs/valkey/product-overview) to [save, load and delete langchain documents](https://python.langchain.com/docs/modules/data_connection/document_loaders/) with `MemorystoreDocumentLoader` and `MemorystoreDocumentSaver`.\n", |
14 | 20 | "\n", |
15 | 21 | "Learn more about the package on [GitHub](https://github.com/googleapis/langchain-google-memorystore-redis-python/).\n", |
16 | 22 | "\n", |
|
26 | 32 | "To run this notebook, you will need to do the following:\n", |
27 | 33 | "\n", |
28 | 34 | "* [Create a Google Cloud Project](https://developers.google.com/workspace/guides/create-project)\n", |
29 | | - "* [Enable the Memorystore for Redis API](https://console.cloud.google.com/flows/enableapi?apiid=redis.googleapis.com)\n", |
30 | | - "* [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", |
| 35 | + "* Enable relevant APIs for your use case\n", |
| 36 | + " * [Enable the Memorystore for Redis API](https://console.cloud.google.com/flows/enableapi?apiid=redis.googleapis.com)\n", |
| 37 | + " * [Enable the Memorystore for Valkey API](https://console.cloud.google.com/flows/enableapi?apiid=memorystore.googleapis.com)\n", |
| 38 | + "* Create Memorystore resources for your use case. You may do any of the following:\n", |
| 39 | + " * [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 7.2.\n", |
| 40 | + " * [Create a Memorystore for Redis cluster](https://cloud.google.com/memorystore/docs/cluster/instance-provisioning-vpc)\n", |
| 41 | + " * [Create a Memorystore for Valkey instance](https://cloud.google.com/memorystore/docs/valkey/instance-provisioning-vpc)\n", |
31 | 42 | "\n", |
32 | 43 | "After confirmed access to database in the runtime environment of this notebook, filling the following values and run the cell before running example scripts." |
33 | 44 | ] |
|
141 | 152 | "## Basic Usage" |
142 | 153 | ] |
143 | 154 | }, |
| 155 | + { |
| 156 | + "cell_type": "markdown", |
| 157 | + "metadata": {}, |
| 158 | + "source": [ |
| 159 | + "### Setup a Redis connection" |
| 160 | + ] |
| 161 | + }, |
| 162 | + { |
| 163 | + "cell_type": "markdown", |
| 164 | + "metadata": {}, |
| 165 | + "source": [ |
| 166 | + "#### Method 1: Standalone Redis Client" |
| 167 | + ] |
| 168 | + }, |
| 169 | + { |
| 170 | + "cell_type": "code", |
| 171 | + "execution_count": null, |
| 172 | + "metadata": {}, |
| 173 | + "outputs": [], |
| 174 | + "source": [ |
| 175 | + "import redis\n", |
| 176 | + "\n", |
| 177 | + "redis_client = redis.from_url(ENDPOINT)" |
| 178 | + ] |
| 179 | + }, |
| 180 | + { |
| 181 | + "cell_type": "markdown", |
| 182 | + "metadata": {}, |
| 183 | + "source": [ |
| 184 | + "#### Method 2: Clustered Redis Client" |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "code", |
| 189 | + "execution_count": null, |
| 190 | + "metadata": {}, |
| 191 | + "outputs": [], |
| 192 | + "source": [ |
| 193 | + "import redis\n", |
| 194 | + "\n", |
| 195 | + "redis_client = redis.cluster.RedisCluster.from_url(ENDPOINT)" |
| 196 | + ] |
| 197 | + }, |
144 | 198 | { |
145 | 199 | "cell_type": "markdown", |
146 | 200 | "metadata": {}, |
|
149 | 203 | "\n", |
150 | 204 | "Save langchain documents with `MemorystoreDocumentSaver.add_documents(<documents>)`. To initialize `MemorystoreDocumentSaver` class you need to provide 2 things:\n", |
151 | 205 | "\n", |
152 | | - "1. `client` - A `redis.Redis` client object.\n", |
| 206 | + "1. `client` - A `redis.Redis` or `redis.cluster.RedisCluster` client object.\n", |
153 | 207 | "1. `key_prefix` - A prefix for the keys to store Documents in Redis.\n", |
154 | 208 | "\n", |
155 | 209 | "The Documents will be stored into randomly generated keys with the specified prefix of `key_prefix`. Alternatively, you can designate the suffixes of the keys by specifying `ids` in the `add_documents` method." |
|
161 | 215 | "metadata": {}, |
162 | 216 | "outputs": [], |
163 | 217 | "source": [ |
164 | | - "import redis\n", |
165 | 218 | "from langchain_core.documents import Document\n", |
166 | 219 | "from langchain_google_memorystore_redis import MemorystoreDocumentSaver\n", |
167 | 220 | "\n", |
|
181 | 234 | "]\n", |
182 | 235 | "doc_ids = [f\"{i}\" for i in range(len(test_docs))]\n", |
183 | 236 | "\n", |
184 | | - "redis_client = redis.from_url(ENDPOINT)\n", |
185 | 237 | "saver = MemorystoreDocumentSaver(\n", |
186 | 238 | " client=redis_client,\n", |
187 | 239 | " key_prefix=KEY_PREFIX,\n", |
|
198 | 250 | "source": [ |
199 | 251 | "### Load documents\n", |
200 | 252 | "\n", |
201 | | - "Initialize a loader that loads all documents stored in the Memorystore for Redis instance with a specific prefix.\n", |
| 253 | + "Initialize a loader that loads all documents stored in the Memorystore instance with a specific prefix.\n", |
202 | 254 | "\n", |
203 | 255 | "Load langchain documents with `MemorystoreDocumentLoader.load()` or `MemorystoreDocumentLoader.lazy_load()`. `lazy_load` returns a generator that only queries database during the iteration. To initialize `MemorystoreDocumentLoader` class you need to provide:\n", |
204 | 256 | "\n", |
205 | | - "1. `client` - A `redis.Redis` client object.\n", |
| 257 | + "1. `client` - A `redis.Redis` ro `redis.cluster.RedisCluster` client object.\n", |
206 | 258 | "1. `key_prefix` - A prefix for the keys to store Documents in Redis." |
207 | 259 | ] |
208 | 260 | }, |
|
214 | 266 | }, |
215 | 267 | "outputs": [], |
216 | 268 | "source": [ |
217 | | - "import redis\n", |
218 | 269 | "from langchain_google_memorystore_redis import MemorystoreDocumentLoader\n", |
219 | 270 | "\n", |
220 | | - "redis_client = redis.from_url(ENDPOINT)\n", |
221 | 271 | "loader = MemorystoreDocumentLoader(\n", |
222 | 272 | " client=redis_client,\n", |
223 | 273 | " key_prefix=KEY_PREFIX,\n", |
|
0 commit comments