Skip to main content
Open In ColabOpen on GitHub

Google Firestore(原生模式)

Firestore 是一种无服务器的文档导向型数据库,能够根据需求进行扩展。利用 Firestore 的 LangChain 集成,您可以将数据库应用扩展到构建基于 AI 的体验。

这个笔记本介绍了如何使用Firestore通过FirestoreLoaderFirestoreSaver来保存、加载和删除langchain文档。

Learn more about the package on GitHub.

Open In Colab

开始之前

要运行此笔记本,您需要执行以下操作:

在确认此笔记本运行环境中的数据库访问后,请填写以下值并运行该单元格,然后运行示例脚本。

# @markdown Please specify a source for demo purpose.
SOURCE = "test" # @param {type:"Query"|"CollectionGroup"|"DocumentReference"|"string"}

🦜🔗 库安装

The integration lives in its own langchain-google-firestore package, so we need to install it.

%pip install -upgrade --quiet 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云项目

设置您的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()

基本用法

保存文档

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)
API 参考:文档

保存文档而不引用

如果指定了集合,文档将使用自动生成的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)

Load from Collection or SubCollection

使用 FirestoreLoader.load()Firestore.lazy_load() 加载 langchain 文档。lazy_load 返回一个生成器,在迭代过程中仅在查询数据库时进行查询。要初始化 FirestoreLoader 类,您需要提供:

  1. source - 一个Query、CollectionGroup、DocumentReference的实例,或者是通过1分隔符连接的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 或 Query 加载

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 的参数将指定 Firestore 文档字段被写入 LangChain 文档 page_contentmetadata

loader = FirestoreLoader(
source="foo/bar/subcol",
page_content_fields=["data_field"],
metadata_fields=["metadata_field"],
)

data = loader.load()

自定义页面内容格式

当`0`只包含一个字段时,信息将是该字段的值。否则,`1`将以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,
)