跳到主要內容
Open In ColabOpen on GitHub

Google AlloyDB for PostgreSQL

Google Cloud AlloyDB for PostgreSQL 是一項完全受管理的 PostgreSQL 相容資料庫服務,可滿足您最嚴苛的企業工作負載需求。AlloyDB 結合了 Google CloudPostgreSQL 的優勢,提供卓越的效能、規模和可用性。擴展您的資料庫應用程式,以建構利用 AlloyDB Langchain 整合技術的 AI 驅動體驗。

本筆記本說明如何使用 Google Cloud AlloyDB for PostgreSQL,透過 AlloyDBChatMessageHistory 類別儲存聊天訊息歷史記錄。

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

Open In Colab

開始之前

若要執行本筆記本,您需要執行下列操作

🦜🔗 程式庫安裝

此整合位於其自身的 langchain-google-alloydb-pg 套件中,因此我們需要安裝它。

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

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

🔐 驗證

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

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

auth.authenticate_user()

☁ 設定您的 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}

💡 API 啟用

langchain-google-alloydb-pg 套件要求您在您的 Google Cloud 專案中啟用 AlloyDB Admin API

# enable AlloyDB API
!gcloud services enable alloydb.googleapis.com

基本用法

設定 AlloyDB 資料庫值

AlloyDB 叢集頁面中尋找您的資料庫值。

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

AlloyDBEngine 連線集區

將 AlloyDB 建立為 ChatMessageHistory 記憶體儲存區的需求和引數之一是 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 = AlloyDBEngine.from_instance(
project_id=PROJECT_ID,
region=REGION,
cluster=CLUSTER,
instance=INSTANCE,
database=DATABASE,
)

初始化資料表

AlloyDBChatMessageHistory 類別需要具有特定結構描述的資料庫表,才能儲存聊天訊息歷史記錄。

AlloyDBEngine 引擎具有輔助方法 init_chat_history_table(),可用於為您建立具有正確結構描述的資料表。

engine.init_chat_history_table(table_name=TABLE_NAME)

AlloyDBChatMessageHistory

若要初始化 AlloyDBChatMessageHistory 類別,您只需要提供 3 個項目

  1. engine - AlloyDBEngine 引擎的執行個體。
  2. session_id - 指定工作階段 ID 的唯一識別字串。
  3. table_name:AlloyDB 資料庫中用於儲存聊天訊息歷史記錄的資料表名稱。
from langchain_google_alloydb_pg import AlloyDBChatMessageHistory

history = AlloyDBChatMessageHistory.create_sync(
engine, session_id="test_session", table_name=TABLE_NAME
)
history.add_user_message("hi!")
history.add_ai_message("whats up?")
history.messages

清除

當特定工作階段的歷史記錄過時且可以刪除時,可以透過以下方式完成。

注意: 刪除後,資料將不再儲存在 AlloyDB 中,且將永久遺失。

history.clear()

🔗 鏈結

我們可以輕鬆地將此訊息歷史記錄類別與 LCEL Runnables 結合使用

為此,我們將使用 Google 的 Vertex AI 聊天模型 之一,這需要您在您的 Google Cloud 專案中啟用 Vertex AI API

# enable Vertex AI API
!gcloud services enable aiplatform.googleapis.com
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_google_vertexai import ChatVertexAI
prompt = ChatPromptTemplate.from_messages(
[
("system", "You are a helpful assistant."),
MessagesPlaceholder(variable_name="history"),
("human", "{question}"),
]
)

chain = prompt | ChatVertexAI(project=PROJECT_ID)
chain_with_history = RunnableWithMessageHistory(
chain,
lambda session_id: AlloyDBChatMessageHistory.create_sync(
engine,
session_id=session_id,
table_name=TABLE_NAME,
),
input_messages_key="question",
history_messages_key="history",
)
# This is where we configure the session id
config = {"configurable": {"session_id": "test_session"}}
chain_with_history.invoke({"question": "Hi! I'm bob"}, config=config)
chain_with_history.invoke({"question": "Whats my name"}, config=config)

此頁面是否對您有幫助?