> Firestore 是一种无服务器文档数据库,可扩展以满足任何需求。通过 Firestore 的 LangChain 集成扩展您的数据库应用程序,以构建由 AI 驱动的体验。
本笔记本介绍如何使用 Firestore to 保存、加载和删除 LangChain 文档 与 FirestoreLoader 和 FirestoreSaver.
了解更多关于该包的信息请访问 GitHub.

准备工作
要运行本笔记本,您需要执行以下操作:
确认可以访问笔记本运行时的数据库后,填写以下值并运行单元格,然后再运行示例脚本。
# @markdown Please specify a source for demo purpose.
SOURCE = "test" # @param {type:"Query"|"CollectionGroup"|"DocumentReference"|"string"}
🦜🔗 库安装
该集成位于其自有的 langchain-google-firestore 包中,因此我们需要安装它。
pip install -qU langchain-google-firestore
仅限 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 项目,以便您可以在本笔记本中利用 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 Workbench,请查看 Vertex AI Workbench 设置说明.
from google.colab import auth
auth.authenticate_user()
基本用法
保存文档
FirestoreSaver 可以将文档存储到 Firestore。默认情况下,它会尝试从元数据中提取文档引用
使用以下方式保存 LangChain 文档 FirestoreSaver.upsert_documents(<documents>).
from langchain_core.documents import Document
from langchain_google_firestore import FirestoreSaver
saver = FirestoreSaver()
data = [Document(page_content="Hello, World!")]
saver.upsert_documents(data)
保存不带引用的文档
如果指定了集合,文档将以自动生成的 ID 进行存储。
saver = FirestoreSaver("Collection")
saver.upsert_documents(data)
使用其他引用保存文档
doc_ids = ["AnotherCollection/doc_id", "foo/bar"]
saver = FirestoreSaver()
saver.upsert_documents(documents=data, document_ids=doc_ids)
从集合或子集合加载
使用以下方式加载 LangChain 文档 FirestoreLoader.load() or Firestore.lazy_load(). lazy_load 返回一个生成器,仅在迭代期间查询数据库。要初始化 FirestoreLoader 类,您需要提供:
source- Query、CollectionGroup、DocumentReference 或单一\到 Firestore 集合的 - 分隔路径。
from langchain_google_firestore import FirestoreLoader
loader_collection = FirestoreLoader("Collection")
loader_subcollection = FirestoreLoader("Collection/doc/SubCollection")
data_collection = loader_collection.load()
data_subcollection = loader_subcollection.load()
加载单个文档
from google.cloud import firestore
client = firestore.Client()
doc_ref = client.collection("foo").document("bar")
loader_document = FirestoreLoader(doc_ref)
data = loader_document.load()
从 CollectionGroup 或查询加载
from google.cloud.firestore import CollectionGroup, FieldFilter, Query
col_ref = client.collection("col_group")
collection_group = CollectionGroup(col_ref)
loader_group = FirestoreLoader(collection_group)
col_ref = client.collection("collection")
query = col_ref.where(filter=FieldFilter("region", "==", "west_coast"))
loader_query = FirestoreLoader(query)
删除文档
从 Firestore 集合中删除 langchain 文档列表,使用 FirestoreSaver.delete_documents(<documents>).
如果提供了文档 ids,Documents 将被忽略。
saver = FirestoreSaver()
saver.delete_documents(data)
# The Documents will be ignored and only the document ids will be used.
saver.delete_documents(data, doc_ids)
高级用法
加载带有自定义文档页面内容和元数据的文档
参数 page_content_fields 和 metadata_fields 将指定要写入 LangChain Document 的 Firestore Document 字段 page_content 和 metadata.
loader = FirestoreLoader(
source="foo/bar/subcol",
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 = FirestoreLoader(
source="foo",
client=client,
)