Google Memorystore for Redis 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 Memorystore for Redis to save, load and delete langchain documents with
MemorystoreDocumentLoader
and MemorystoreDocumentSaver
.
Learn more about the package on GitHub.
Before You Begin
To run this notebook, you will need to do the following:- Create a Google Cloud Project
- Enable the Memorystore for Redis API
- Create a Memorystore for Redis instance. Ensure that the version is greater than or equal to 5.0.
π¦π Library Installation
The integration lives in its ownlangchain-google-memorystore-redis
package, so we need to install it.
β 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.
π 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.
Basic Usage
Save documents
Save langchain documents withMemorystoreDocumentSaver.add_documents(<documents>)
. To initialize MemorystoreDocumentSaver
class you need to provide 2 things:
client
- Aredis.Redis
client object.key_prefix
- A prefix for the keys to store Documents in Redis.
key_prefix
. Alternatively, you can designate the suffixes of the keys by specifying ids
in the add_documents
method.
Load documents
Initialize a loader that loads all documents stored in the Memorystore for Redis instance with a specific prefix. Load langchain documents withMemorystoreDocumentLoader.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:
client
- Aredis.Redis
client object.key_prefix
- A prefix for the keys to store Documents in Redis.
Delete documents
Delete all of keys with the specified prefix in the Memorystore for Redis instance withMemorystoreDocumentSaver.delete()
. You can also specify the suffixes of the keys if you know.
Advanced Usage
Customize Document Page Content & Metadata
When initializing a loader with more than 1 content field, thepage_content
of the loaded Documents will contain a JSON-encoded string with top level fields equal to the specified fields in content_fields
.
If the metadata_fields
are specified, the metadata
field of the loaded Documents will only have the top level fields equal to the specified metadata_fields
. If any of the values of the metadata fields is stored as a JSON-encoded string, it will be decoded prior to being loaded to the metadata fields.