Google Firestore (原生模式)
Firestore 是一個無伺服器、面向文件的資料庫,可擴展以滿足任何需求。擴展您的資料庫應用程式,以利用 Firestore 的 Langchain 整合來建構 AI 驅動的體驗。
本筆記本說明如何使用 Firestore 和 FirestoreLoader
和 FirestoreSaver
來儲存、載入和刪除 langchain 文件。
在 GitHub 上了解更多關於此套件的資訊。
開始之前
要執行此筆記本,您需要執行以下操作
在確認可以存取此筆記本的執行環境中的資料庫後,填寫以下值並在執行範例腳本之前執行儲存格。
# @markdown Please specify a source for demo purpose.
SOURCE = "test" # @param {type:"Query"|"CollectionGroup"|"DocumentReference"|"string"}
🦜🔗 函式庫安裝
此整合存在於其自身的 langchain-google-firestore
套件中,因此我們需要安裝它。
%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 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,請查看 此處 的設定說明。
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
類別,您需要提供
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 或 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_fields
和 metadata_fields
的引數將指定要寫入 LangChain Document page_content
和 metadata
的 Firestore Document 欄位。
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,
)