|
12 | 12 | "\n", |
13 | 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", |
14 | 14 | "\n", |
| 15 | + "Learn more about the package on [GitHub](https://github.com/googleapis/langchain-google-memorystore-redis-python/).\n", |
| 16 | + "\n", |
15 | 17 | "[](https://colab.research.google.com/github/googleapis/langchain-google-memorystore-redis-python/blob/main/docs/document_loader.ipynb)" |
16 | 18 | ] |
17 | 19 | }, |
|
22 | 24 | "## Before You Begin\n", |
23 | 25 | "\n", |
24 | 26 | "To run this notebook, you will need to do the following:\n", |
| 27 | + "\n", |
25 | 28 | "* [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", |
26 | 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", |
27 | 31 | "\n", |
28 | 32 | "After confirmed access to database in the runtime environment of this notebook, filling the following values and run the cell before running example scripts." |
|
144 | 148 | "### Save documents\n", |
145 | 149 | "\n", |
146 | 150 | "Save langchain documents with `MemorystoreDocumentSaver.add_documents(<documents>)`. To initialize `MemorystoreDocumentSaver` class you need to provide 2 things:\n", |
| 151 | + "\n", |
147 | 152 | "1. `client` - A `redis.Redis` client object.\n", |
148 | 153 | "1. `key_prefix` - A prefix for the keys to store Documents in Redis.\n", |
149 | 154 | "\n", |
|
156 | 161 | "metadata": {}, |
157 | 162 | "outputs": [], |
158 | 163 | "source": [ |
| 164 | + "import redis\n", |
| 165 | + "from langchain_core.documents import Document\n", |
159 | 166 | "from langchain_google_memorystore_redis import MemorystoreDocumentSaver\n", |
160 | 167 | "\n", |
161 | 168 | "test_docs = [\n", |
|
180 | 187 | " key_prefix=KEY_PREFIX,\n", |
181 | 188 | " content_field=\"page_content\",\n", |
182 | 189 | ")\n", |
183 | | - "saver.add_documents(test_docs, ids=docs_ids)" |
| 190 | + "saver.add_documents(test_docs, ids=doc_ids)" |
184 | 191 | ] |
185 | 192 | }, |
186 | 193 | { |
|
194 | 201 | "Initialize a loader that loads all documents stored in the Memorystore for Redis instance with a specific prefix.\n", |
195 | 202 | "\n", |
196 | 203 | "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 | + "\n", |
197 | 205 | "1. `client` - A `redis.Redis` client object.\n", |
198 | 206 | "1. `key_prefix` - A prefix for the keys to store Documents in Redis." |
199 | 207 | ] |
|
274 | 282 | "source": [ |
275 | 283 | "loader = MemorystoreDocumentLoader(\n", |
276 | 284 | " client=redis_client,\n", |
277 | | - " key_prefix=prefix,\n", |
| 285 | + " key_prefix=KEY_PREFIX,\n", |
278 | 286 | " content_fields=set([\"content_field_1\", \"content_field_2\"]),\n", |
279 | 287 | " metadata_fields=set([\"title\", \"author\"]),\n", |
280 | 288 | ")" |
|
0 commit comments