跳到主要內容

Google AlloyDB for PostgreSQL

AlloyDB 是一項完全託管的關聯式資料庫服務,提供高效能、無縫整合和令人印象深刻的可擴展性。 AlloyDB 與 PostgreSQL 100% 相容。 擴展您的資料庫應用程式,利用 AlloyDB 的 Langchain 整合功能來建構 AI 驅動的體驗。

本筆記本說明如何使用 AlloyDB for PostgreSQL,透過 AlloyDBLoader 類別載入文件 (Documents)。

GitHub 上了解更多關於此套件的資訊。

Open In Colab

開始之前 (Before you begin)

要執行此筆記本,您需要執行以下操作:

🦜🔗 程式庫安裝 (Library Installation)

安裝整合程式庫 langchain-google-alloydb-pg

%pip install --upgrade --quiet  langchain-google-alloydb-pg

僅限 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)

🔐 驗證 (Authentication)

以登入此筆記本的 IAM 使用者身分驗證 Google Cloud,以便存取您的 Google Cloud 專案。

  • 如果您使用 Colab 執行此筆記本,請使用以下儲存格並繼續。
  • 如果您使用 Vertex AI Workbench,請查看此處的設定說明。
from google.colab import auth

auth.authenticate_user()

☁ 設定您的 Google Cloud 專案 (Set Your Google Cloud Project)

設定您的 Google Cloud 專案,以便您可以利用此筆記本中的 Google Cloud 資源。

如果您不知道您的專案 ID,請嘗試以下操作:

  • 執行 gcloud config list
  • 執行 gcloud projects list
  • 請參閱支援頁面:尋找專案 ID
# @title Project { display-mode: "form" }
PROJECT_ID = "gcp_project_id" # @param {type:"string"}

# Set the project id
! gcloud config set project {PROJECT_ID}

基本用法 (Basic Usage)

設定 AlloyDB 資料庫變數 (Set AlloyDB database variables)

AlloyDB 執行個體頁面中尋找您的資料庫值。

# @title Set Your Values Here { display-mode: "form" }
REGION = "us-central1" # @param {type: "string"}
CLUSTER = "my-cluster" # @param {type: "string"}
INSTANCE = "my-primary" # @param {type: "string"}
DATABASE = "my-database" # @param {type: "string"}
TABLE_NAME = "vector_store" # @param {type: "string"}

AlloyDBEngine 連線池 (Connection Pool)

將 AlloyDB 建立為向量儲存區的要求和引數之一是 AlloyDBEngine 物件。 AlloyDBEngine 配置到 AlloyDB 資料庫的連線池,從而啟用來自應用程式的成功連線並遵循產業最佳實務。

若要使用 AlloyDBEngine.from_instance() 建立 AlloyDBEngine,您只需要提供 5 件事:

  1. project_id:AlloyDB 執行個體所在的 Google Cloud 專案的專案 ID。
  2. region:AlloyDB 執行個體所在的區域。
  3. cluster:AlloyDB 叢集的名稱。
  4. instance:AlloyDB 執行個體的名稱。
  5. database:要連線到 AlloyDB 執行個體上的資料庫名稱。

預設情況下,IAM 資料庫驗證將用作資料庫驗證的方法。 此程式庫使用屬於從環境來源的應用程式預設憑證 (ADC)的 IAM 主體。

或者,也可以使用使用使用者名稱和密碼存取 AlloyDB 資料庫的內建資料庫驗證。 只需將可選的 userpassword 引數提供給 AlloyDBEngine.from_instance()

  • user:用於內建資料庫驗證和登入的資料庫使用者
  • password:用於內建資料庫驗證和登入的資料庫密碼。

注意:本教學課程示範了非同步介面。 所有非同步方法都有對應的同步方法。

from langchain_google_alloydb_pg import AlloyDBEngine

engine = await AlloyDBEngine.afrom_instance(
project_id=PROJECT_ID,
region=REGION,
cluster=CLUSTER,
instance=INSTANCE,
database=DATABASE,
)

建立 AlloyDBLoader (Create AlloyDBLoader)

from langchain_google_alloydb_pg import AlloyDBLoader

# Creating a basic AlloyDBLoader object
loader = await AlloyDBLoader.create(engine, table_name=TABLE_NAME)

透過預設表格載入文件 (Load Documents via default table)

載入器 (loader) 使用第一欄作為 page_content 並使用所有其他欄作為 metadata 從表格傳回文件清單。 預設表格的第一欄將作為 page_content,第二欄將作為 metadata (JSON)。 每一列都會變成一份文件。

docs = await loader.aload()
print(docs)

透過自訂表格/metadata 或自訂頁面內容欄載入文件 (Load documents via custom table/metadata or custom page content columns)

loader = await AlloyDBLoader.create(
engine,
table_name=TABLE_NAME,
content_columns=["product_name"], # Optional
metadata_columns=["id"], # Optional
)
docs = await loader.aload()
print(docs)

設定頁面內容格式 (Set page content format)

載入器會傳回文件清單,每列一份文件,頁面內容採用指定的字串格式,例如 text(以空格分隔的串連)、JSON、YAML、CSV 等。JSON 和 YAML 格式包含標頭,而 text 和 CSV 不包含欄位標頭。

loader = AlloyDBLoader.create(
engine,
table_name="products",
content_columns=["product_name", "description"],
format="YAML",
)
docs = await loader.aload()
print(docs)

此頁面是否對您有幫助? (Was this page helpful?)