Skip to main content
Firestore 是一种可按需扩展的无服务器面向文档数据库。利用 Firestore 的 LangChain 集成,扩展您的数据库应用,构建 AI 驱动的体验。
本 notebook 介绍如何使用 Firestore 通过 FirestoreLoaderFirestoreSaver保存、加载和删除 LangChain 文档 GitHub 上了解更多关于该软件包的信息。 Open In Colab

开始之前

要运行本 notebook,您需要完成以下步骤: 在本 notebook 的运行环境中确认可以访问数据库后,填写以下值并在运行示例脚本前先运行该单元格。
# @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 项目,以便在本 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()

基本用法

保存文档

FirestoreSaver 可以将文档存储到 Firestore。默认情况下,它会尝试从元数据中提取文档引用。 使用 FirestoreSaver.upsert_documents(<documents>) 保存 LangChain 文档。
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)

从集合或子集合加载

使用 FirestoreLoader.load()Firestore.lazy_load() 加载 LangChain 文档。lazy_load 返回一个生成器,仅在迭代时查询数据库。要初始化 FirestoreLoader 类,您需要提供:
  1. 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)

删除文档

使用 FirestoreSaver.delete_documents(<documents>) 从 Firestore 集合中删除一组 LangChain 文档。 如果提供了文档 ID,则文档本身将被忽略。
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_fieldsmetadata_fields 参数用于指定要写入 LangChain 文档 page_contentmetadata 的 Firestore 文档字段。
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,
)