Google Firestore in Datastore Mode
Firestore in Datastore 模式是一個 NoSQL 文件資料庫,專為自動擴展、高效能和易於應用程式開發而建構。 擴展您的資料庫應用程式,以利用 Datastore 的 Langchain 整合來建構 AI 驅動的體驗。
本筆記本將介紹如何使用 Firestore in Datastore 模式,透過 DatastoreLoader
和 DatastoreSaver
儲存、載入和刪除 langchain 文件。
在 GitHub 上了解更多關於此套件的資訊。
開始之前
要執行此筆記本,您需要執行以下操作
確認可在此筆記本的執行環境中存取資料庫後,填寫以下值並執行儲存格,然後再執行範例指令碼。
🦜🔗 函式庫安裝
此整合位於其自身的 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 專案,以便您可以在此筆記本中利用 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()
基本用法
儲存文件
使用 DatastoreSaver.upsert_documents(<documents>)
儲存 langchain 文件。 預設情況下,它會嘗試從 Document metadata 中的 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)
透過 Kind 載入文件
使用 DatastoreLoader.load()
或 DatastoreLoader.lazy_load()
載入 langchain 文件。 lazy_load
傳回一個產生器,該產生器僅在疊代期間查詢資料庫。 要初始化 DatastoreLoader
類別,您需要提供
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)
進階用法
載入具有自訂文件頁面內容 & metadata 的文件
page_content_properties
和 metadata_properties
的引數將指定要寫入 LangChain Document page_content
和 metadata
的 Entity 屬性。
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,
)