跳到主要内容
Firestore(Datastore 模式)是一个 NoSQL 文档数据库,专为自动扩缩、高性能和易于应用开发而构建。利用 Datastore 的 LangChain 集成,扩展您的数据库应用程序以构建 AI 驱动的体验。
本笔记本介绍了如何使用Firestore(Datastore 模式)DatastoreLoaderDatastoreSaver保存、加载和删除 LangChain 文档 GitHub上了解有关该软件包的更多信息。 在 Colab 中打开

开始之前

要运行此 notebook,您需要执行以下操作 在本 notebook 的运行时环境中确认数据库访问权限后,填写以下值并运行该单元格,然后再运行示例脚本。

🦜🔗 库安装

该集成位于其自己的langchain-google-datastore包中,因此我们需要安装它。
pip install -upgrade --quiet langchain-google-datastore
仅限 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 Cloud 项目

设置您的 Google Cloud 项目,以便您可以在此 notebook 中利用 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}

🔐 身份验证

以登录到此 notebook 的 IAM 用户身份向 Google Cloud 进行身份验证,以访问您的 Google Cloud 项目。
  • 如果您正在使用 Colab 运行此 notebook,请使用下面的单元格并继续。
  • 如果您正在使用 Vertex AI Workbench,请查看此处的设置说明。
from google.colab import auth

auth.authenticate_user()

基本用法

保存文档

使用DatastoreSaver.upsert_documents(<documents>)保存 LangChain 文档。默认情况下,它会尝试从文档元数据中的key中提取实体键。
from langchain_core.documents import Document
from langchain_google_datastore import DatastoreSaver

saver = DatastoreSaver()

data = [Document(page_content="Hello, World!")]
saver.upsert_documents(data)

保存没有键的文档

如果指定了kind,文档将以自动生成的 ID 存储。
saver = DatastoreSaver("MyKind")

saver.upsert_documents(data)

通过种类加载文档

使用DatastoreLoader.load()DatastoreLoader.lazy_load()加载 LangChain 文档。lazy_load返回一个生成器,该生成器仅在迭代期间查询数据库。要初始化DatastoreLoader类,您需要提供
  1. source - 加载文档的来源。它可以是 Query 实例或要读取的 Datastore kind 的名称。
from langchain_google_datastore import DatastoreLoader

loader = DatastoreLoader("MyKind")
data = loader.load()

通过查询加载文档

除了从 kind 加载文档外,我们还可以选择从查询中加载文档。例如
from google.cloud import datastore

client = datastore.Client(database="non-default-db", namespace="custom_namespace")
query_load = client.query(kind="MyKind")
query_load.add_filter("region", "=", "west_coast")

loader_document = DatastoreLoader(query_load)

data = loader_document.load()

删除文档

使用DatastoreSaver.delete_documents(<documents>)从 Datastore 中删除 LangChain 文档列表。
saver = DatastoreSaver()

saver.delete_documents(data)

keys_to_delete = [
    ["Kind1", "identifier"],
    ["Kind2", 123],
    ["Kind3", "identifier", "NestedKind", 456],
]
# The Documents will be ignored and only the document ids will be used.
saver.delete_documents(data, keys_to_delete)

高级用法

加载具有自定义文档页面内容和元数据的文档

page_content_propertiesmetadata_properties参数将指定要写入 LangChain Document page_contentmetadata的实体属性。
loader = DatastoreLoader(
    source="MyKind",
    page_content_fields=["data_field"],
    metadata_fields=["metadata_field"],
)

data = loader.load()

自定义页面内容格式

page_content只包含一个字段时,信息将仅为字段值。否则,page_content将采用 JSON 格式。

自定义连接和认证

from google.auth import compute_engine
from google.cloud.firestore import Client

client = Client(database="non-default-db", creds=compute_engine.Credentials())
loader = DatastoreLoader(
    source="foo",
    client=client,
)

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.