Google Memorystore for Redis
Google Memorystore for Redis 是一个由 Redis 内存数据存储驱动的完全托管服务,用于构建提供亚毫秒级数据访问的应用程序缓存。利用 Memorystore for Redis 的 Langchain 集成扩展您的数据库应用以构建 AI 动力化的体验。
这个笔记本介绍了如何使用Memorystore for Redis通过MemorystoreDocumentLoader和MemorystoreDocumentSaver来保存、加载和删除langchain文档。更多详细信息请参阅此处。
Learn more about the package on GitHub.
开始之前
要运行此笔记本,您需要执行以下操作:
在确认此笔记本运行环境中的数据库访问后,请填写以下值并运行该单元格,然后运行示例脚本。
# @markdown Please specify an endpoint associated with the instance and a key prefix for demo purpose.
ENDPOINT = "redis://127.0.0.1:6379" # @param {type:"string"}
KEY_PREFIX = "doc:" # @param {type:"string"}
🦜🔗 库安装
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
仅限 Colab:取消以下单元格的注释以重启内核,或使用按钮重启内核。对于 Vertex AI Workbench,您可以使用顶部的按钮重启终端。
# # 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)
☁ 设置您的Google云项目
设置您的Google Cloud项目,以便在此笔记本中利用Google Cloud资源。
如果您不知道您的项目ID,请尝试以下方法:
- 运行
gcloud config list。 - 运行
gcloud projects list。 - 见支持页面:查找项目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}
🔐 认证
请以笔记本中已登录的IAM用户身份向Google Cloud进行认证,以便访问您的Google Cloud项目。
- 如果您在Colab中运行此笔记本,请使用下方单元格继续。
- 如果您正在使用Vertex AI工作区,请参阅设置说明这里。
from google.colab import auth
auth.authenticate_user()
基本用法
保存文档
使用MemorystoreDocumentSaver.add_documents(<documents>)保存langchain文档。为了初始化MemorystoreDocumentSaver类,您需要提供2个东西:
client- 一个redis.Redis客户端对象。key_prefix- Redis中存储文档的键前缀。
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.
import redis
from langchain_core.documents import Document
from langchain_google_memorystore_redis import MemorystoreDocumentSaver
test_docs = [
Document(
page_content="Apple Granny Smith 150 0.99 1",
metadata={"fruit_id": 1},
),
Document(
page_content="Banana Cavendish 200 0.59 0",
metadata={"fruit_id": 2},
),
Document(
page_content="Orange Navel 80 1.29 1",
metadata={"fruit_id": 3},
),
]
doc_ids = [f"{i}" for i in range(len(test_docs))]
redis_client = redis.from_url(ENDPOINT)
saver = MemorystoreDocumentSaver(
client=redis_client,
key_prefix=KEY_PREFIX,
content_field="page_content",
)
saver.add_documents(test_docs, ids=doc_ids)
加载文档
初始化一个加载器,用于从具有特定前缀的 Memorystore for Redis 实例中加载所有文档。
使用 MemorystoreDocumentLoader.load() 或 MemorystoreDocumentLoader.lazy_load() 加载 langchain 文档。lazy_load 返回一个生成器,在迭代过程中仅在查询数据库时进行查询。要初始化 MemorystoreDocumentLoader 类,您需要提供:
client- 一个redis.Redis客户端对象。key_prefix- Redis中存储文档的键前缀。
import redis
from langchain_google_memorystore_redis import MemorystoreDocumentLoader
redis_client = redis.from_url(ENDPOINT)
loader = MemorystoreDocumentLoader(
client=redis_client,
key_prefix=KEY_PREFIX,
content_fields=set(["page_content"]),
)
for doc in loader.lazy_load():
print("Loaded documents:", doc)
删除文档
使用MemorystoreDocumentSaver.delete()的Memorystore for Redis实例,删除具有指定前缀的所有键。如果您知道键的后缀,也可以指定后缀。
docs = loader.load()
print("Documents before delete:", docs)
saver.delete(ids=[0])
print("Documents after delete:", loader.load())
saver.delete()
print("Documents after delete all:", loader.load())
高级用法
自定义文档页面内容及元数据
当使用带有超过一个内容字段的加载器初始化时,加载的Documents中的page_content将包含一个JSON编码的字符串,其顶级字段等于在content_fields中指定的字段。
如果指定了metadata_fields,加载的Documents的metadata字段将只有顶级字段等于指定的metadata_fields。如果元数据字段中的任何值被存储为JSON编码的字符串,则在加载到元数据字段之前会进行解码。
loader = MemorystoreDocumentLoader(
client=redis_client,
key_prefix=KEY_PREFIX,
content_fields=set(["content_field_1", "content_field_2"]),
metadata_fields=set(["title", "author"]),
)