1. 簡介
在本程式碼研究室中,您將瞭解如何部署生成式 AI 資料庫檢索服務,以及如何使用部署環境建立互動式應用程式範例。
如要進一步瞭解生成式 AI 檢索服務和範例應用程式,請參閱這篇文章。
必要條件
- 對 Google Cloud 控制台有基本瞭解
- 指令列介面和 Google Cloud Shell 的基本技能
課程內容
- 如何部署 AlloyDB 叢集
- 如何連線至 AlloyDB
- 如何設定及部署生成式 AI 資料庫檢索服務
- 如何使用已部署的服務部署範例應用程式
軟硬體需求
- Google Cloud 帳戶和 Google Cloud 專案
- 網路瀏覽器,例如 Chrome
2. 設定和需求
自助式環境設定
- 登入 Google Cloud 控制台,然後建立新專案或重複使用現有專案。如果您還沒有 Gmail 或 Google Workspace 帳戶,請務必建立帳戶。
- 「Project name」是這個專案參與者的顯示名稱。這是 Google API 不會使用的字元字串。您隨時可以更新。
- 專案 ID 在所有 Google Cloud 專案中都是不重複的值,且無法變更 (設定後即無法變更)。Cloud 控制台會自動產生唯一字串,您通常不需要特別留意。在大多數程式碼研究室中,您都需要參照專案 ID (通常會標示為
PROJECT_ID
)。如果您不喜歡產生的 ID,可以產生另一個隨機 ID。或者,您也可以自行嘗試,看看是否可用。在這個步驟完成後就無法變更,且會在整個專案期間維持不變。 - 提醒您,有些 API 會使用第三個值「專案編號」。如要進一步瞭解這三個值,請參閱說明文件。
- 接下來,您需要在 Cloud 控制台中啟用帳單功能,才能使用 Cloud 資源/API。執行這個程式碼研究室不會產生任何費用,如要關閉資源,避免在本教學課程結束後繼續產生費用,您可以刪除建立的資源或專案。Google Cloud 新使用者可享有 $300 美元的免費試用期。
啟動 Cloud Shell
雖然 Google Cloud 可透過筆記型電腦遠端操作,但在本程式碼研究室中,您將使用 Google Cloud Shell,這是在雲端運作的指令列環境。
在 Google Cloud 控制台中,按一下右上方工具列的 Cloud Shell 圖示:
佈建並連線至環境的作業需要一些時間才能完成。完成後,畫面應如下所示:
這個虛擬機器會載入您需要的所有開發工具。提供永久的 5 GB 主目錄,而且在 Google Cloud 中運作,可大幅提升網路效能和驗證功能。您可以在瀏覽器中完成本程式碼研究室的所有工作。您不需要安裝任何東西。
3. 事前準備
啟用 API
輸出:
在 Cloud Shell 中,確認專案 ID 已設定完畢:
通常,Cloud Shell 的命令提示會在括號內顯示專案 ID,如下圖所示:
gcloud config set project [YOUR-PROJECT-ID]
接著,將 PROJECT_ID 環境變數設為 Google Cloud 專案 ID:
PROJECT_ID=$(gcloud config get-value project)
啟用所有必要服務:
gcloud services enable alloydb.googleapis.com \
compute.googleapis.com \
cloudresourcemanager.googleapis.com \
servicenetworking.googleapis.com \
vpcaccess.googleapis.com \
aiplatform.googleapis.com \
cloudbuild.googleapis.com \
artifactregistry.googleapis.com \
run.googleapis.com \
iam.googleapis.com
預期的輸出內容:
student@cloudshell:~ (gleb-test-short-004)$ gcloud services enable alloydb.googleapis.com \ compute.googleapis.com \ cloudresourcemanager.googleapis.com \ servicenetworking.googleapis.com \ vpcaccess.googleapis.com \ aiplatform.googleapis.com \ cloudbuild.googleapis.com \ artifactregistry.googleapis.com \ run.googleapis.com \ iam.googleapis.com Operation "operations/acf.p2-404051529011-664c71ad-cb2b-4ab4-86c1-1f3157d70ba1" finished successfully.
4. 部署 AlloyDB 叢集
建立 AlloyDB 叢集和主要執行個體。以下程序說明如何使用 Google Cloud SDK 建立 AlloyDB 叢集和執行個體。如果您偏好使用主控台,請按照這裡的說明文件操作。
在建立 AlloyDB 叢集之前,我們需要在虛擬私有雲端中提供可供日後 AlloyDB 執行個體使用的私人 IP 範圍。如果沒有,我們需要建立,並指派給內部 Google 服務使用,之後才能建立叢集和執行個體。
建立私人 IP 範圍
我們需要在 AlloyDB 的 VPC 中設定私人服務存取權設定。這裡假設我們在專案中使用「預設」虛擬私有雲網路,並且會用於所有動作。
建立私人 IP 範圍:
gcloud compute addresses create psa-range \
--global \
--purpose=VPC_PEERING \
--prefix-length=24 \
--description="VPC private service access" \
--network=default
使用分配的 IP 範圍建立私人連線:
gcloud services vpc-peerings connect \
--service=servicenetworking.googleapis.com \
--ranges=psa-range \
--network=default
預期的控制台輸出內容:
student@cloudshell:~ (test-project-402417)$ gcloud compute addresses create psa-range \ --global \ --purpose=VPC_PEERING \ --prefix-length=24 \ --description="VPC private service access" \ --network=default Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/global/addresses/psa-range]. student@cloudshell:~ (test-project-402417)$ gcloud services vpc-peerings connect \ --service=servicenetworking.googleapis.com \ --ranges=psa-range \ --network=default Operation "operations/pssn.p24-4470404856-595e209f-19b7-4669-8a71-cbd45de8ba66" finished successfully. student@cloudshell:~ (test-project-402417)$
建立 AlloyDB 叢集
在本節中,我們會在 us-central1 區域中建立 AlloyDB 叢集。
定義 postgres 使用者的密碼。您可以自行定義密碼,也可以使用隨機函式產生密碼
export PGPASSWORD=`openssl rand -hex 12`
預期的控制台輸出內容:
student@cloudshell:~ (test-project-402417)$ export PGPASSWORD=`openssl rand -hex 12`
請記下 PostgreSQL 密碼,以供日後使用。
echo $PGPASSWORD
日後您需要該密碼,才能以 postgres 使用者身分連線至執行個體。建議你將其寫下或複製到某處,以便日後使用。
預期的控制台輸出內容:
student@cloudshell:~ (test-project-402417)$ echo $PGPASSWORD bbefbfde7601985b0dee5723
建立免付費試用叢集
如果您之前未曾使用 AlloyDB,可以建立免費的試用叢集:
定義區域和 AlloyDB 叢集名稱。我們將使用 us-central1 區域,並將 alloydb-aip-01 做為叢集名稱:
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
執行指令建立叢集:
gcloud alloydb clusters create $ADBCLUSTER \
--password=$PGPASSWORD \
--network=default \
--region=$REGION \
--subscription-type=TRIAL
預期的控制台輸出內容:
export REGION=us-central1 export ADBCLUSTER=alloydb-aip-01 gcloud alloydb clusters create $ADBCLUSTER \ --password=$PGPASSWORD \ --network=default \ --region=$REGION \ --subscription-type=TRIAL Operation ID: operation-1697655441138-6080235852277-9e7f04f5-2012fce4 Creating cluster...done.
在同一個 Cloud Shell 工作階段中,為叢集建立 AlloyDB 主要執行個體。如果連線中斷,您必須重新定義地區和叢集名稱環境變數。
gcloud alloydb instances create $ADBCLUSTER-pr \
--instance-type=PRIMARY \
--cpu-count=8 \
--region=$REGION \
--cluster=$ADBCLUSTER
預期的控制台輸出內容:
student@cloudshell:~ (test-project-402417)$ gcloud alloydb instances create $ADBCLUSTER-pr \ --instance-type=PRIMARY \ --cpu-count=8 \ --region=$REGION \ --availability-type ZONAL \ --cluster=$ADBCLUSTER Operation ID: operation-1697659203545-6080315c6e8ee-391805db-25852721 Creating instance...done.
建立 AlloyDB Standard 叢集
如果不是專案中的第一個 AlloyDB 叢集,請繼續建立標準叢集。
定義區域和 AlloyDB 叢集名稱。我們將使用 us-central1 區域,並將 alloydb-aip-01 做為叢集名稱:
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
執行指令建立叢集:
gcloud alloydb clusters create $ADBCLUSTER \
--password=$PGPASSWORD \
--network=default \
--region=$REGION
預期的控制台輸出內容:
export REGION=us-central1 export ADBCLUSTER=alloydb-aip-01 gcloud alloydb clusters create $ADBCLUSTER \ --password=$PGPASSWORD \ --network=default \ --region=$REGION Operation ID: operation-1697655441138-6080235852277-9e7f04f5-2012fce4 Creating cluster...done.
在同一個 Cloud Shell 工作階段中,為叢集建立 AlloyDB 主要執行個體。如果連線中斷,您必須重新定義地區和叢集名稱環境變數。
gcloud alloydb instances create $ADBCLUSTER-pr \
--instance-type=PRIMARY \
--cpu-count=2 \
--region=$REGION \
--cluster=$ADBCLUSTER
預期的控制台輸出內容:
student@cloudshell:~ (test-project-402417)$ gcloud alloydb instances create $ADBCLUSTER-pr \ --instance-type=PRIMARY \ --cpu-count=2 \ --region=$REGION \ --availability-type ZONAL \ --cluster=$ADBCLUSTER Operation ID: operation-1697659203545-6080315c6e8ee-391805db-25852721 Creating instance...done.
5. 準備 GCE 虛擬機器
建立服務帳戶
我們會使用 VM 部署生成式 AI 資料庫檢索服務並代管範例應用程式,因此第一步是建立 Google 服務帳戶 (GSA)。GSA 會由 GCE VM 使用,因此我們需要授予必要權限,讓 GSA 與其他服務搭配運作。
在 Cloud Shell 中執行:
PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts create compute-aip --project $PROJECT_ID
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/cloudbuild.builds.editor"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/artifactregistry.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/storage.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/run.admin"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/iam.serviceAccountUser"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/alloydb.viewer"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:compute-aip@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/serviceusage.serviceUsageConsumer"
部署 GCE VM
在與 AlloyDB 叢集相同的區域和虛擬私人雲端中建立 GCE VM。
在 Cloud Shell 中執行:
export ZONE=us-central1-a
gcloud compute instances create instance-1 \
--zone=$ZONE \
--create-disk=auto-delete=yes,boot=yes,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" --format="value(name)") \
--scopes=https://www.googleapis.com/auth/cloud-platform \
--service-account=compute-aip@$PROJECT_ID.iam.gserviceaccount.com
預期的控制台輸出內容:
student@cloudshell:~ (test-project-402417)$ export ZONE=us-central1-a student@cloudshell:~ (test-project-402417)$ export ZONE=us-central1-a gcloud compute instances create instance-1 \ --zone=$ZONE \ --create-disk=auto-delete=yes,boot=yes,image=projects/debian-cloud/global/images/$(gcloud compute images list --filter="family=debian-12 AND family!=debian-12-arm64" --format="value(name)") \ --scopes=https://www.googleapis.com/auth/cloud-platform Created [https://www.googleapis.com/compute/v1/projects/test-project-402417/zones/us-central1-a/instances/instance-1]. NAME: instance-1 ZONE: us-central1-a MACHINE_TYPE: n1-standard-1 PREEMPTIBLE: INTERNAL_IP: 10.128.0.2 EXTERNAL_IP: 34.71.192.233 STATUS: RUNNING
安裝 Postgres 用戶端
在已部署的 VM 上安裝 PostgreSQL 用戶端軟體
連線至 VM:
gcloud compute ssh instance-1 --zone=us-central1-a
預期的控制台輸出內容:
student@cloudshell:~ (test-project-402417)$ gcloud compute ssh instance-1 --zone=us-central1-a Updating project ssh metadata...working..Updated [https://www.googleapis.com/compute/v1/projects/test-project-402417]. Updating project ssh metadata...done. Waiting for SSH key to propagate. Warning: Permanently added 'compute.5110295539541121102' (ECDSA) to the list of known hosts. Linux instance-1 5.10.0-26-cloud-amd64 #1 SMP Debian 5.10.197-1 (2023-09-29) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. student@instance-1:~$
在 VM 內安裝執行軟體的指令:
sudo apt-get update
sudo apt-get install --yes postgresql-client
預期的控制台輸出內容:
student@instance-1:~$ sudo apt-get update sudo apt-get install --yes postgresql-client Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B] Get:4 file:/etc/apt/mirrors/debian-security.list Mirrorlist [39 B] Hit:7 https://packages.cloud.google.com/apt google-compute-engine-bookworm-stable InRelease Get:8 https://packages.cloud.google.com/apt cloud-sdk-bookworm InRelease [1652 B] Get:2 https://deb.debian.org/debian bookworm InRelease [151 kB] Get:3 https://deb.debian.org/debian bookworm-updates InRelease [55.4 kB] ...redacted... update-alternatives: using /usr/share/postgresql/15/man/man1/psql.1.gz to provide /usr/share/man/man1/psql.1.gz (psql.1.gz) in auto mode Setting up postgresql-client (15+248) ... Processing triggers for man-db (2.11.2-2) ... Processing triggers for libc-bin (2.36-9+deb12u7) ...
連線至執行個體
使用 psql 從 VM 連線至主要執行個體。
繼續使用已開啟的 VM SSH 工作階段。如果您已中斷連線,請使用上述相同的指令重新連線。
使用先前記下的 $PGASSWORD 和叢集名稱,從 GCE VM 連線至 AlloyDB:
export PGPASSWORD=<Noted password>
export PROJECT_ID=$(gcloud config get-value project)
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
export INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)")
psql "host=$INSTANCE_IP user=postgres sslmode=require"
預期的控制台輸出內容:
student@instance-1:~$ export PGPASSWORD=P9... student@instance-1:~$ export REGION=us-central1 student@instance-1:~$ export ADBCLUSTER=alloydb-aip-01 student@instance-1:~$ export INSTANCE_IP=export INSTANCE_IP=$(gcloud alloydb instances describe $ADBCLUSTER-pr --cluster=$ADBCLUSTER --region=$REGION --format="value(ipAddress)") student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres sslmode=require" psql (13.11 (Debian 13.11-0+deb11u1), server 14.7) WARNING: psql major version 13, server major version 14. Some psql features might not work. SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off) Type "help" for help. postgres=>
退出 psql 工作階段,並保持 SSH 連線:
exit
預期的控制台輸出內容:
postgres=> exit student@instance-1:~$
6. 初始化資料庫
我們將使用用戶端 VM 做為平台,為資料庫填入資料並代管應用程式。第一步是建立資料庫,並填入資料。
建立資料庫
建立名為「assistantdemo」的資料庫。
在 GCE VM 工作階段中執行:
psql "host=$INSTANCE_IP user=postgres" -c "CREATE DATABASE assistantdemo"
預期的控制台輸出內容:
student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres" -c "CREATE DATABASE assistantdemo" CREATE DATABASE student@instance-1:~$
啟用 pgVector 擴充功能。
psql "host=$INSTANCE_IP user=postgres dbname=assistantdemo" -c "CREATE EXTENSION vector"
預期的控制台輸出內容:
student@instance-1:~$ psql "host=$INSTANCE_IP user=postgres dbname=assistantdemo" -c "CREATE EXTENSION vector" CREATE EXTENSION student@instance-1:~$
準備 Python 環境
接下來,我們將使用 GitHub 存放區中準備好的 Python 指令碼,但在執行這項操作之前,我們需要先安裝必要的軟體。
在 GCE VM 中執行:
sudo apt install -y python3.11-venv git
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
預期的控制台輸出內容:
student@instance-1:~$ sudo apt install -y python3.11-venv git python3 -m venv .venv source .venv/bin/activate pip install --upgrade pip Reading package lists... Done Building dependency tree... Done Reading state information... Done The following additional packages will be installed: git-man liberror-perl patch python3-distutils python3-lib2to3 python3-pip-whl python3-setuptools-whl Suggested packages: git-daemon-run | git-daemon-sysvinit git-doc git-email git-gui gitk gitweb git-cvs git-mediawiki git-svn ed diffutils-doc The following NEW packages will be installed: git git-man liberror-perl patch python3-distutils python3-lib2to3 python3-pip-whl python3-setuptools-whl python3.11-venv 0 upgraded, 9 newly installed, 0 to remove and 2 not upgraded. Need to get 12.4 MB of archives. After this operation, 52.2 MB of additional disk space will be used. Get:1 file:/etc/apt/mirrors/debian.list Mirrorlist [30 B] ...redacted... Installing collected packages: pip Attempting uninstall: pip Found existing installation: pip 23.0.1 Uninstalling pip-23.0.1: Successfully uninstalled pip-23.0.1 Successfully installed pip-24.0 (.venv) student@instance-1:~$
確認 Python 版本。
在 GCE VM 中執行:
python -V
預期的控制台輸出內容:
(.venv) student@instance-1:~$ python -V Python 3.11.2 (.venv) student@instance-1:~$
填充資料庫
複製 GitHub 存放區,其中包含擷取服務和範例應用程式的程式碼。
在 GCE VM 中執行:
git clone https://github.com/GoogleCloudPlatform/genai-databases-retrieval-app.git
預期的控制台輸出內容:
student@instance-1:~$ git clone https://github.com/GoogleCloudPlatform/genai-databases-retrieval-app.git Cloning into 'genai-databases-retrieval-app'... remote: Enumerating objects: 525, done. remote: Counting objects: 100% (336/336), done. remote: Compressing objects: 100% (201/201), done. remote: Total 525 (delta 224), reused 179 (delta 135), pack-reused 189 Receiving objects: 100% (525/525), 46.58 MiB | 16.16 MiB/s, done. Resolving deltas: 100% (289/289), done.
準備設定檔
在 GCE VM 中執行:
cd genai-databases-retrieval-app/retrieval_service
cp example-config.yml config.yml
sed -i s/127.0.0.1/$INSTANCE_IP/g config.yml
sed -i s/my-password/$PGPASSWORD/g config.yml
sed -i s/my_database/assistantdemo/g config.yml
sed -i s/my-user/postgres/g config.yml
cat config.yml
預期的控制台輸出內容:
student@instance-1:~$ cd genai-databases-retrieval-app/retrieval_service cp example-config.yml config.yml sed -i s/127.0.0.1/$INSTANCE_IP/g config.yml sed -i s/my-password/$PGPASSWORD/g config.yml sed -i s/my_database/assistantdemo/g config.yml sed -i s/my-user/postgres/g config.yml cat config.yml host: 0.0.0.0 # port: 8080 datastore: # Example for AlloyDB kind: "postgres" host: 10.65.0.2 # port: 5432 database: "assistantdemo" user: "postgres" password: "P9..."
使用範例資料集填充資料庫。第一個指令會將所有必要套件新增至 Python 虛擬環境,第二個指令則會用資料填充資料庫。
在 GCE VM 中執行:
pip install -r requirements.txt
python run_database_init.py
預期的控制台輸出內容(經過遮蓋):
student@instance-1:~/genai-databases-retrieval-app/retrieval_service$ pip install -r requirements.txt python run_database_init.py Collecting asyncpg==0.28.0 (from -r requirements.txt (line 1)) Obtaining dependency information for asyncpg==0.28.0 from https://files.pythonhosted.org/packages/77/a4/88069f7935b14c58534442a57be3299179eb46aace2d3c8716be199ff6a6/asyncpg-0.28.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata Downloading asyncpg-0.28.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.3 kB) Collecting fastapi==0.101.1 (from -r requirements.txt (line 2)) ... database init done. student@instance-1:~/genai-databases-retrieval-app/retrieval_service$
7. 將檢索服務部署至 Cloud Run
我們現在可以將檢索服務部署至 Cloud Run。這項服務負責與資料庫互動,並根據 AI 應用程式的要求,從資料庫中擷取必要資訊。
建立服務帳戶
為檢索服務建立服務帳戶,並授予必要權限。
使用頂端的「+」符號開啟另一個 Cloud Shell 分頁。
在新的 Cloud Shell 分頁中執行:
export PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts create retrieval-identity
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:retrieval-identity@$PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/aiplatform.user"
預期的控制台輸出內容:
student@cloudshell:~ (gleb-test-short-003)$ gcloud iam service-accounts create retrieval-identity Created service account [retrieval-identity].
在分頁中執行「exit」指令,關閉分頁:
exit
部署檢索服務
在第一個分頁中繼續操作,透過 SSH 連線至 VM,然後部署服務。
在 VM SSH 工作階段中執行以下指令:
cd ~/genai-databases-retrieval-app
gcloud alpha run deploy retrieval-service \
--source=./retrieval_service/\
--no-allow-unauthenticated \
--service-account retrieval-identity \
--region us-central1 \
--network=default \
--quiet
預期的控制台輸出內容:
student@instance-1:~/genai-databases-retrieval-app$ gcloud alpha run deploy retrieval-service \ --source=./retrieval_service/\ --no-allow-unauthenticated \ --service-account retrieval-identity \ --region us-central1 \ --network=default This command is equivalent to running `gcloud builds submit --tag [IMAGE] ./retrieval_service/` and `gcloud run deploy retrieval-service --image [IMAGE]` Building using Dockerfile and deploying container to Cloud Run service [retrieval-service] in project [gleb-test-short-003] region [us-central1] X Building and deploying... Done. ✓ Uploading sources... ✓ Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds/6ebe74bf-3039-4221-b2e9-7ca8fa8dad8e?project=1012713954588]. ✓ Creating Revision... ✓ Routing traffic... Setting IAM Policy... Completed with warnings: Setting IAM policy failed, try "gcloud beta run services remove-iam-policy-binding --region=us-central1 --member=allUsers --role=roles/run.invoker retrieval-service" Service [retrieval-service] revision [retrieval-service-00002-4pl] has been deployed and is serving 100 percent of traffic. Service URL: https://retrieval-service-onme64eorq-uc.a.run.app student@instance-1:~/genai-databases-retrieval-app$
驗證服務
我們現在可以檢查服務是否正常執行,以及 VM 是否可存取端點。我們使用 gcloud 公用程式取得檢索服務端點。或者,您也可以在 Cloud 控制台中查看,並在 curl 指令中將 "$(gcloud run services list –filter="(retrieval-service)" 替換為該值。
在 VM SSH 工作階段中執行以下指令:
curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" $(gcloud run services list --filter="(retrieval-service)" --format="value(URL)")
預期的控制台輸出內容:
student@instance-1:~/genai-databases-retrieval-app$ curl -H "Authorization: Bearer $(gcloud auth print-identity-token)" $(gcloud run services list --filter="(retrieval-service)" --format="value(URL)") {"message":"Hello World"}student@instance-1:~/genai-databases-retrieval-app$
如果看到「Hello World」訊息,表示服務已啟用並能處理要求。
8. 部署範例應用程式
檢索服務啟動並執行後,我們可以部署要使用該服務的範例應用程式。應用程式可部署至 VM 或任何其他服務 (例如 Cloud Run、Kubernetes,甚至是筆電上的本機)。我們將說明如何在 VM 上部署這項服務。
準備環境
我們會繼續使用相同的 SSH 工作階段處理虛擬機器。如要執行應用程式,我們需要新增一些 Python 模組。系統會在相同 Python 虛擬環境中的應用程式目錄中執行這項指令。
在 VM SSH 工作階段中執行以下指令:
cd ~/genai-databases-retrieval-app/llm_demo
pip install -r requirements.txt
預期的輸出內容 (經過遮蓋):
student@instance-1:~$ cd ~/genai-databases-retrieval-app/llm_demo pip install -r requirements.txt Collecting fastapi==0.104.0 (from -r requirements.txt (line 1)) Obtaining dependency information for fastapi==0.104.0 from https://files.pythonhosted.org/packages/db/30/b8d323119c37e15b7fa639e65e0eb7d81eb675ba166ac83e695aad3bd321/fastapi-0.104.0-py3-none-any.whl.metadata Downloading fastapi-0.104.0-py3-none-any.whl.metadata (24 kB) ...
準備用戶端 ID
如要使用應用程式的預訂功能,您必須使用 Cloud 控制台準備 OAuth 2.0 用戶端 ID。否則,我們就無法使用 Google 憑證登入應用程式,進行預訂。我們需要這項權限才能在資料庫中記錄預訂資料。
在 Cloud 控制台中前往「API 和服務」頁面,然後按一下「OAuth 同意畫面」。系統會開啟 Oauth 總覽頁面,我們按一下「開始使用」。
在下一頁中,我們提供應用程式名稱和使用者支援電子郵件,然後按一下「下一步」。
在下一個畫面中,我們為應用程式選擇「內部」,然後再次點選「下一步」。
然後再次提供聯絡電子郵件地址,然後點選「下一步」
接著,我們同意 Google API 服務政策,並按下「建立」按鈕。
這會將我們導向可建立 OAuth 用戶端的頁面。
在畫面上,我們從下拉式選單中選擇「Web Application」,將「Cymbal Air」設為應用程式,然後按下「Add URI」(新增 URI) 按鈕。
URI 代表應用程式的可信來源,取決於您嘗試存取應用程式的來源。我們將「https://localhost:8081」設為已授權 URI,並將「https://localhost:8081/login/google」設為重新導向 URI。如果您在瀏覽器中輸入「https://localhost:8081」做為連線的 URI,這些值就會生效。舉例來說,當您透過電腦的 SSH 通道連線時,我稍後會說明如何操作。
按下「建立」按鈕後,系統會顯示彈出式視窗,其中包含客戶憑證。系統會記錄憑證。您隨時可以複製用於啟動應用程式的用戶端 ID。
稍後您會看到要提供該客戶 ID 的位置。
執行 Google 助理應用程式
啟動應用程式前,我們需要設定一些環境變數。應用程式的基本功能 (例如查詢航班和機場設施) 只需要 BASE_URL,即可將應用程式導向檢索服務。我們可以使用 gcloud 指令取得這個值。
在 VM SSH 工作階段中執行以下指令:
export BASE_URL=$(gcloud run services list --filter="(retrieval-service)" --format="value(URL)")
預期的輸出內容 (經過遮蓋):
student@instance-1:~/genai-databases-retrieval-app/llm_demo$ export BASE_URL=$(gcloud run services list --filter="(retrieval-service)" --format="value(URL)")
如要使用應用程式的進階功能 (例如預訂和變更航班),我們需要使用 Google 帳戶登入應用程式,因此我們需要使用「準備用戶端 ID」一節中的 OAuth 用戶端 ID,提供 CLIENT_ID 環境變數:
export CLIENT_ID=215....apps.googleusercontent.com
預期的輸出內容 (經過遮蓋):
student@instance-1:~/genai-databases-retrieval-app/llm_demo$ export CLIENT_ID=215....apps.googleusercontent.com
我們現在可以執行應用程式:
python run_app.py
預期輸出內容:
student@instance-1:~/genai-databases-retrieval-app/llm_demo$ python main.py INFO: Started server process [28565] INFO: Waiting for application startup. INFO: Application startup complete. INFO: Uvicorn running on http://0.0.0.0:8081 (Press CTRL+C to quit)
連線至應用程式
您可以透過多種方式連線至 VM 上執行的應用程式。舉例來說,您可以在虛擬私有雲使用防火牆規則,開啟 VM 的 8081 通訊埠,或是建立具有公開 IP 的負載平衡器。這裡我們將使用 SSH 通道連線至 VM,將本機通訊埠 8080 轉譯為 VM 通訊埠 8081。
從本機連線
如要從本機電腦連線,就必須執行 SSH 通道。您可以使用 gcloud compute ssh 執行這項操作:
gcloud compute ssh instance-1 --zone=us-central1-a -- -L 8081:localhost:8081
預期輸出內容:
student-macbookpro:~ student$ gcloud compute ssh instance-1 --zone=us-central1-a -- -L 8080:localhost:8081 Warning: Permanently added 'compute.7064281075337367021' (ED25519) to the list of known hosts. Linux instance-1.us-central1-c.c.gleb-test-001.internal 6.1.0-21-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.90-1 (2024-05-03) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. student@instance-1:~$
我們現在可以開啟瀏覽器,並使用 http://localhost:8081 連線至應用程式。我們應該會看到應用程式畫面。
透過 Cloud Shell 連線
或者,我們也可以使用 Google Cloud Shell 進行連線。使用頂端的「+」符號開啟另一個 Cloud Shell 分頁。
在新分頁中,取得執行 gcloud 指令的網頁用戶端來源和重新導向 URI:
echo "origin:"; echo "https://8080-$WEB_HOST"; echo "redirect:"; echo "https://8080-$WEB_HOST/login/google"
預期的輸出內容如下:
student@cloudshell:~ echo "origin:"; echo "https://8080-$WEB_HOST"; echo "redirect:"; echo "https://8080-$WEB_HOST/login/google" origin: https://8080-cs-35704030349-default.cs-us-east1-rtep.cloudshell.dev redirect: https://8080-cs-35704030349-default.cs-us-east1-rtep.cloudshell.dev/login/google
並將來源和 URI 的重新導向做為「已授權的 JavaScript 來源」和「已授權的重新導向 URI」使用,為在「準備用戶端 ID」章節中建立的憑證,取代或新增原先提供的 http://localhost:8080 值。
在 OAuth 2.0 用戶端 ID 頁面中,按一下「Cymbal Air」。
輸入 Cloud Shell 的來源和重新導向 URI,然後按下「Save」(儲存) 按鈕。
在新的 Cloud Shell 分頁中,執行 gcloud 指令,啟動連往 VM 的通道:
gcloud compute ssh instance-1 --zone=us-central1-a -- -L 8080:localhost:8081
如果系統顯示「無法指派要求的地址」錯誤訊息,請忽略該訊息。
預期的輸出內容如下:
student@cloudshell:~ gcloud compute ssh instance-1 --zone=us-central1-a -- -L 8080:localhost:8081 bind [::1]:8081: Cannot assign requested address inux instance-1.us-central1-a.c.gleb-codelive-01.internal 6.1.0-21-cloud-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.90-1 (2024-05-03) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. Last login: Sat May 25 19:15:46 2024 from 35.243.235.73 student@instance-1:~$
這會在 Cloud Shell 上開啟通訊埠 8080,可用於「網頁預覽」。
按一下 Cloud Shell 右上方的「網頁預覽」按鈕,然後從下拉式選單中選擇「透過以下通訊埠預覽:8080」。
這會在網路瀏覽器中開啟新分頁,並顯示應用程式介面。你應該會看到「Cymbal Air 客戶服務助理」頁面。
登入應用程式
設定完成並開啟應用程式後,我們可以使用應用程式畫面右上方的「登入」按鈕提供憑證。這項功能屬於選用,只有在您想試用應用程式的預訂功能時才需要。
系統會開啟彈出式視窗,讓我們選擇憑證。
登入後,應用程式就會準備就緒,您可以開始將要求發布至視窗底部的欄位。
這個示範影片展示 Cymbal Air 客戶服務助理。Cymbal Air 是虛構的航空公司。這款助理是 AI 聊天機器人,可協助旅客管理航班,並查詢 Cymbal Air 在舊金山國際機場 (SFO) 的樞紐資訊。
即使未登入 (沒有 CLIENT_ID),也能協助解答使用者的問題,例如:
下一個飛往丹佛的航班是什麼時候?
C28 登機門附近有哪些精品店?
在 A6 登機門附近哪裡可以買到咖啡?
哪裡可以購買禮物?
請預訂 10:35 離開的丹佛航班
登入應用程式後,你可以試試其他功能,例如預訂航班或查看系統為你分配的座位是靠窗座位還是走道座位。
該應用程式會使用最新 Google 基礎模型生成回覆,並從運作中的 AlloyDB 資料庫檢索航班和設施資訊,再於回覆中提供。如要進一步瞭解這個示範應用程式,請前往專案的 GitHub 頁面。
9. 清理環境
所有工作都完成後,我們可以清理環境
刪除 Cloud Run 服務
在 Cloud Shell 中執行:
gcloud run services delete retrieval-service --region us-central1
預期的控制台輸出內容:
student@cloudshell:~ (gleb-test-short-004)$ gcloud run services delete retrieval-service --region us-central1 Service [retrieval-service] will be deleted. Do you want to continue (Y/n)? Y Deleting [retrieval-service]...done. Deleted service [retrieval-service].
刪除 Cloud Run 服務的服務帳戶
在 Cloud Shell 中執行:
PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts delete retrieval-identity@$PROJECT_ID.iam.gserviceaccount.com --quiet
預期的控制台輸出內容:
student@cloudshell:~ (gleb-test-short-004)$ PROJECT_ID=$(gcloud config get-value project) Your active configuration is: [cloudshell-222] student@cloudshell:~ (gleb-test-short-004)$ gcloud iam service-accounts delete retrieval-identity@$PROJECT_ID.iam.gserviceaccount.com --quiet deleted service account [retrieval-identity@gleb-test-short-004.iam.gserviceaccount.com] student@cloudshell:~ (gleb-test-short-004)$
完成實驗室後,請銷毀 AlloyDB 執行個體和叢集
刪除 AlloyDB 叢集和所有執行個體
叢集會使用強制選項進行銷毀,同時刪除屬於叢集的所有執行個體。
如果您已連線中斷,且所有先前設定都已遺失,請在 Cloud Shell 中定義專案和環境變數:
gcloud config set project <your project id>
export REGION=us-central1
export ADBCLUSTER=alloydb-aip-01
export PROJECT_ID=$(gcloud config get-value project)
刪除叢集:
gcloud alloydb clusters delete $ADBCLUSTER --region=$REGION --force
預期的控制台輸出內容:
student@cloudshell:~ (test-project-001-402417)$ gcloud alloydb clusters delete $ADBCLUSTER --region=$REGION --force All of the cluster data will be lost when the cluster is deleted. Do you want to continue (Y/n)? Y Operation ID: operation-1697820178429-6082890a0b570-4a72f7e4-4c5df36f Deleting cluster...done.
刪除 AlloyDB 備份
刪除叢集的所有 AlloyDB 備份:
for i in $(gcloud alloydb backups list --filter="CLUSTER_NAME: projects/$PROJECT_ID/locations/$REGION/clusters/$ADBCLUSTER" --format="value(name)" --sort-by=~createTime) ; do gcloud alloydb backups delete $(basename $i) --region $REGION --quiet; done
預期的控制台輸出內容:
student@cloudshell:~ (test-project-001-402417)$ for i in $(gcloud alloydb backups list --filter="CLUSTER_NAME: projects/$PROJECT_ID/locations/$REGION/clusters/$ADBCLUSTER" --format="value(name)" --sort-by=~createTime) ; do gcloud alloydb backups delete $(basename $i) --region $REGION --quiet; done Operation ID: operation-1697826266108-60829fb7b5258-7f99dc0b-99f3c35f Deleting backup...done.
我們現在可以刪除 VM
刪除 GCE VM
在 Cloud Shell 中執行:
export GCEVM=instance-1
export ZONE=us-central1-a
gcloud compute instances delete $GCEVM \
--zone=$ZONE \
--quiet
預期的控制台輸出內容:
student@cloudshell:~ (test-project-001-402417)$ export GCEVM=instance-1 export ZONE=us-central1-a gcloud compute instances delete $GCEVM \ --zone=$ZONE \ --quiet Deleted
刪除 GCE VM 和擷取服務的服務帳戶
在 Cloud Shell 中執行:
PROJECT_ID=$(gcloud config get-value project)
gcloud iam service-accounts delete compute-aip@$PROJECT_ID.iam.gserviceaccount.com --quiet
預期的控制台輸出內容:
student@cloudshell:~ (gleb-test-short-004)$ PROJECT_ID=$(gcloud config get-value project) gcloud iam service-accounts delete compute-aip@$PROJECT_ID.iam.gserviceaccount.com --quiet Your active configuration is: [cloudshell-222] deleted service account [compute-aip@gleb-test-short-004.iam.gserviceaccount.com] student@cloudshell:~ (gleb-test-short-004)$
10. 恭喜
恭喜您完成程式碼研究室!
涵蓋內容
- 如何部署 AlloyDB 叢集
- 如何連線至 AlloyDB
- 如何設定及部署生成式 AI 資料庫檢索服務
- 如何使用已部署的服務部署範例應用程式
11. 問卷調查
輸出: