Google Memorystore for Redis
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 store vector embeddings with the MemorystoreVectorStore
class.
Learn more about the package on GitHub.
Pre-reqs
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 7.2.
🦜🔗 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 langchain
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 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.
# @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
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.
from google.colab import auth
auth.authenticate_user()
Basic Usage
Initialize a Vector Index
import redis
from langchain_google_memorystore_redis import (
DistanceStrategy,
HNSWConfig,
RedisVectorStore,
)
# Connect to a Memorystore for Redis instance
redis_client = redis.from_url("redis://127.0.0.1:6379")
# Configure HNSW index with descriptive parameters
index_config = HNSWConfig(
name="my_vector_index", distance_strategy=DistanceStrategy.COSINE, vector_size=128
)
# Initialize/create the vector store index
RedisVectorStore.init_index(client=redis_client, index_config=index_config)
Prepare Documents
Text needs processing and numerical representation before interacting with a vector store. This involves:
- Loading Text: The TextLoader obtains text data from a file (e.g., "state_of_the_union.txt").
- Text Splitting: The CharacterTextSplitter breaks the text into smaller chunks for embedding models.
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
loader = TextLoader("./state_of_the_union.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
Add Documents to the Vector Store
After text preparation and embedding generation, the following methods insert them into the Redis vector store.
Method 1: Classmethod for Direct Insertion
This approach combines embedding creation and insertion into a single step using the from_documents classmethod:
from langchain_community.embeddings.fake import FakeEmbeddings
embeddings = FakeEmbeddings(size=128)
redis_client = redis.from_url("redis://127.0.0.1:6379")
rvs = RedisVectorStore.from_documents(
docs, embedding=embeddings, client=redis_client, index_name="my_vector_index"
)
Method 2: Instance-Based Insertion
This approach offers flexibility when working with a new or existing RedisVectorStore:
- [Optional] Create a RedisVectorStore Instance: Instantiate a RedisVectorStore object for customization. If you already have an instance, proceed to the next step.
- Add Text with Metadata: Provide raw text and metadata to the instance. Embedding generation and insertion into the vector store are handled automatically.
rvs = RedisVectorStore(
client=redis_client, index_name="my_vector_index", embeddings=embeddings
)
ids = rvs.add_texts(
texts=[d.page_content for d in docs], metadatas=[d.metadata for d in docs]
)