在 Kubernetes 使用 AlloyDB Omni 和 EmbeddingGemma,搭配 Gemma 4

1. 簡介

在本程式碼實驗室中,您將瞭解如何在 Google Kubernetes Engine (GKE) 部署 AlloyDB Omni,並搭配使用 EmbeddingGemmaGemma 4 等開放式模型,進行嵌入和預測。在同一個叢集中執行資料庫和模型,可減少網路延遲,並避免第三方服務依附元件。此外,由於資料不會離開您的環境,因此也能協助您滿足法規遵循和資料落地規定。

GKE 上的 AlloyDB Omni 和 EmbeddingGemma 架構圖

必要條件

  • 對 Google Cloud 和 Google Cloud 控制台有基本瞭解
  • Kubernetes 和 GKE 的基本知識
  • 熟悉指令列介面和 Google Cloud Shell

課程內容

  • 如何在 GKE 叢集上部署 AlloyDB Omni
  • 如何連線至 AlloyDB Omni
  • 如何將資料載入 AlloyDB Omni
  • 如何在 GKE 部署 AI 模型 (嵌入和 LLM)
  • 如何在 AlloyDB Omni 註冊 AI 模型
  • 如何生成語意搜尋的嵌入項目
  • 如何在 AlloyDB Omni 中執行語意搜尋查詢
  • 如何在 AlloyDB Omni 中建立及使用向量索引

軟硬體需求

  • Google Cloud 帳戶和 Google Cloud 專案
  • 網路瀏覽器,例如 Chrome

2. 設定和需求條件

專案設定

  1. 登入 Google Cloud 控制台。如果您還沒有 Gmail 或 Google Workspace 帳戶,請建立帳戶。請改用個人帳戶,而非公司或學校帳戶。
  1. 建立新專案或選取現有專案。在 Google Cloud 控制台標題中,按一下「選取專案」,然後按一下「新專案」

在 Google Cloud 控制台中選取專案對話方塊

在「選取專案」視窗中,按一下「新專案」,開啟專案建立對話方塊。

建立新專案的對話方塊

在對話方塊中輸入「專案名稱」,然後選取機構或地點。

專案詳細資料輸入欄位

  • 專案名稱是這個專案參與者的顯示名稱。Google API 不會使用專案名稱,且您隨時可以變更。
  • 專案 ID 在所有 Google Cloud 專案中都是不重複的,而且設定後即無法變更。Google Cloud 控制台會自動產生專屬 ID,您也可以自行提供。在本程式碼研究室中,您會使用 預留位置參照專案 ID。
  • 專案編號是部分 API 使用的第三個 ID。詳情請參閱 Resource Manager 說明文件

啟用計費功能

如果使用 Google Cloud 抵免額設定計費,則可略過此步驟。

如要設定個人帳單帳戶,請在 Google Cloud 控制台中啟用計費功能。

  • 完成本實驗室的 Google Cloud 資源費用不到 $5 美元。
  • 請按照本實驗室結尾的清除步驟刪除資源,以免產生額外費用。
  • 新使用者可獲得價值 $300 美元的免費試用期

啟動 Cloud Shell

在本程式碼實驗室中,您將使用 Google Cloud Shell,這是可在雲端執行的指令列環境。

Google Cloud 控制台中,點選右上角工具列的「啟用 Cloud Shell」圖示:

「啟用 Cloud Shell」按鈕

或者,依序按下 G 和 S 鍵,或直接開啟 Google Cloud Shell

連線後,Cloud Shell 會顯示終端機提示:

Google Cloud Shell 終端機的螢幕截圖

Cloud Shell 包含永久儲存空間和開發工具,您可以在瀏覽器中完成本程式碼研究室的所有步驟。

3. 啟用 API

如要使用 Google Kubernetes Engine (GKE) 部署 AlloyDB Omni 和模型,請在 Google Cloud 雲端專案中啟用 Compute Engine 和 GKE API。

在 Cloud Shell 中,確認專案 ID 已設定完成:

PROJECT_ID=$(gcloud config get-value project)
echo $PROJECT_ID

如果未定義專案 ID,請設定專案 ID:

export PROJECT_ID=<YOUR_PROJECT_ID>
gcloud config set project $PROJECT_ID

啟用必要的 API:

gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com

預期輸出內容:

student@cloudshell:~ (test-project-001-402417)$ PROJECT_ID=test-project-001-402417
student@cloudshell:~ (test-project-001-402417)$ gcloud config set project test-project-001-402417
Updated property [core/project].
student@cloudshell:~ (test-project-001-402417)$ gcloud services enable compute.googleapis.com
gcloud services enable container.googleapis.com
Operation "operations/acat.p2-4470404856-1f44ebd8-894e-4356-bea7-b84165a57442" finished successfully.

如要瞭解各項已啟用的 API,請參閱說明文件

4. 在 GKE 上部署 AlloyDB Omni

如要在 GKE 上部署 AlloyDB Omni,請按照 AlloyDB Omni 運算子需求準備 Kubernetes 叢集。

建立 GKE 叢集

部署標準 GKE 叢集,具備執行 AlloyDB Omni、運算子和監控容器的容量。AlloyDB Omni 至少需要兩個 CPU 和 8 GB 的 RAM。本教學課程使用 n2-standard-4 機型。

為部署作業設定環境變數:

export PROJECT_ID=$(gcloud config get-value project)
export LOCATION=us-central1
export CLUSTER_NAME=alloydb-ai-gke
export MACHINE_TYPE=n2-standard-4

建立標準 GKE 叢集:

gcloud container clusters create ${CLUSTER_NAME} \
  --project=${PROJECT_ID} \
  --region=${LOCATION} \
  --workload-pool=${PROJECT_ID}.svc.id.goog \
  --release-channel=rapid \
  --machine-type=${MACHINE_TYPE} \
  --num-nodes=1

預期的控制台輸出內容:

student@cloudshell:~ (test-project-001-402417)$ export PROJECT_ID=$(gcloud config get project)
export LOCATION=us-central1
export CLUSTER_NAME=alloydb-ai-gke
export MACHINE_TYPE=n2-standard-4
Your active configuration is: [test-project-001-402417]
student@cloudshell:~ (test-project-001-402417)$ gcloud container clusters create ${CLUSTER_NAME} \
  --project=${PROJECT_ID} \
  --region=${LOCATION} \
  --workload-pool=${PROJECT_ID}.svc.id.goog \
  --release-channel=rapid \
  --machine-type=${MACHINE_TYPE} \
  --num-nodes=1
Note: Your Pod address range (`--cluster-ipv4-cidr`) can accommodate at most 1008 node(s).
Creating cluster alloydb-ai-gke in us-central1... Cluster is being health-checked (Kubernetes Control Plane is healthy)...done.                                                                                            
Created [https://container.googleapis.com/v1/projects/test-project-001-402417/zones/us-central1/clusters/alloydb-ai-gke].
To inspect the contents of your cluster, go to: https://console.cloud.google.com/kubernetes/workload_/gcloud/us-central1/alloydb-ai-gke?project=test-project-001-402417
kubeconfig entry generated for alloydb-ai-gke.
NAME: alloydb-ai-gke
LOCATION: us-central1
MASTER_VERSION: 1.36.3-gke.1640000
MASTER_IP: 34.121.243.65
MACHINE_TYPE: n2-standard-4
NODE_VERSION: 1.36.3-gke.1640000
NUM_NODES: 3
STATUS: RUNNING
STACK_TYPE: IPV4

準備叢集

安裝必要元件,例如 Kubernetes 的原生憑證控制器 cert-manager。詳情請參閱 cert-manager 安裝說明文件

Cloud Shell 包含 Kubernetes 指令列工具 kubectl。使用 gcloud 取得叢集憑證:

gcloud container clusters get-credentials ${CLUSTER_NAME} --region=${LOCATION}

使用 kubectl 安裝 cert-manager

kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.21.1/cert-manager.yaml

預期的控制台輸出內容 (已刪除):

student@cloudshell:~$ kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/v1.21.1/cert-manager.yaml
namespace/cert-manager created
customresourcedefinition.apiextensions.k8s.io/certificaterequests.cert-manager.io created
customresourcedefinition.apiextensions.k8s.io/certificates.cert-manager.io created
customresourcedefinition.apiextensions.k8s.io/challenges.acme.cert-manager.io created
customresourcedefinition.apiextensions.k8s.io/clusterissuers.cert-manager.io created
...
validatingwebhookconfiguration.admissionregistration.k8s.io/cert-manager-webhook created

安裝 AlloyDB Omni 運算子

使用 Helm 安裝 AlloyDB Omni operator。

下載並安裝 AlloyDB Omni 運算子圖表:

helm install alloydbomni-operator oci://gcr.io/alloydb-omni/alloydbomni-operator \
--version 1.8.1 \
--create-namespace \
--namespace alloydb-omni-system \
--atomic \
--timeout 5m

預期的控制台輸出內容 (已刪除):

student@cloudshell:~$ helm install alloydbomni-operator oci://gcr.io/alloydb-omni/alloydbomni-operator \
> --version 1.8.0 \
> --create-namespace \
> --namespace alloydb-omni-system \
> --atomic \
> --timeout 5m
Flag --atomic has been deprecated, use --rollback-on-failure instead
Pulled: gcr.io/alloydb-omni/alloydbomni-operator:1.8.0
Digest: sha256:f2d98fa7a3b08dfc1e83b811582718b94e5c017b81aade700c83e917c59f0395
NAME: alloydbomni-operator
LAST DEPLOYED: Thu Aug 27 17:57:30 2026
NAMESPACE: alloydb-omni-system
STATUS: deployed
REVISION: 1
DESCRIPTION: Install complete
TEST SUITE: None

部署資料庫叢集。

下列資訊清單會設定已啟用 googleMLExtension 的資料庫叢集和內部負載平衡器:

cat << 'EOF' > my-omni.yaml
apiVersion: v1
kind: Secret
metadata:
  name: db-pw-my-omni
type: Opaque
data:
  my-omni: "VmVyeVN0cm9uZ1Bhc3N3b3Jk"
---
apiVersion: alloydbomni.dbadmin.goog/v1
kind: DBCluster
metadata:
  name: my-omni
spec:
  databaseVersion: "18.3.0"
  primarySpec:
    adminUser:
      passwordRef:
        name: db-pw-my-omni
    features:
      googleMLExtension:
        enabled: true
    resources:
      cpu: 1
      memory: 8Gi
      disks:
      - name: DataDisk
        size: 20Gi
        storageClass: standard
    dbLoadBalancerOptions:
      annotations:
        networking.gke.io/load-balancer-type: "internal"
  allowExternalIncomingTraffic: true
EOF

密碼密鑰值是 VeryStrongPassword 的 Base64 表示法。在實際工作環境中,請使用 Google Secret Manager 管理密碼。詳情請參閱 Secret Manager 說明文件

資訊清單會儲存為 my-omni.yaml。在 Cloud Shell 中,按一下終端機視窗右上角的「開啟編輯器」,然後讀取檔案。

在 Cloud Shell 中開啟編輯器

閱讀 my-omni.yaml 資訊清單後,按一下「Open Terminal」(開啟終端機) 返回命令提示字元。

在 Cloud Shell 中開啟終端機

套用 my-omni.yaml 資訊清單:

kubectl apply -f my-omni.yaml

預期的控制台輸出內容:

secret/db-pw-my-omni created
dbcluster.alloydbomni.dbadmin.goog/my-omni created

檢查 my-omni 叢集的狀態:

kubectl get dbclusters.alloydbomni.dbadmin.goog my-omni -n default

部署期間,資料庫叢集會經歷各個設定階段,直到達到 DBClusterReady 狀態。

預期的控制台輸出內容:

$ kubectl get dbclusters.alloydbomni.dbadmin.goog my-omni -n default
NAME      PRIMARYENDPOINT   PRIMARYPHASE   DBCLUSTERPHASE   HAREADYSTATUS   HAREADYREASON
my-omni   10.131.0.33        Ready          DBClusterReady

您也可以選擇使用 kubectl log 指令監控叢集部署作業:

kubectl logs -l alloydbomni.internal.dbadmin.goog/dbcluster=my-omni --all-containers -f

連線至 AlloyDB Omni

叢集就緒後,請使用 PostgreSQL 用戶端 (psql) 連線至資料庫 Pod。密碼為 my-omni.yaml 中定義的 VeryStrongPassword

DB_CLUSTER_NAME=my-omni
DB_CLUSTER_NAMESPACE=default
DBPOD=`kubectl get pod --selector=alloydbomni.internal.dbadmin.goog/dbcluster=$DB_CLUSTER_NAME,alloydbomni.internal.dbadmin.goog/task-type=database -n $DB_CLUSTER_NAMESPACE -o jsonpath='{.items[0].metadata.name}'`
kubectl exec -ti $DBPOD -n $DB_CLUSTER_NAMESPACE -c database -- psql -h localhost -U postgres

控制台輸出內容範例:

DB_CLUSTER_NAME=my-omni
DB_CLUSTER_NAMESPACE=default
DBPOD=`kubectl get pod --selector=alloydbomni.internal.dbadmin.goog/dbcluster=$DB_CLUSTER_NAME,alloydbomni.internal.dbadmin.goog/task-type=database -n $DB_CLUSTER_NAMESPACE -o jsonpath='{.items[0].metadata.name}'`
kubectl exec -ti $DBPOD -n $DB_CLUSTER_NAMESPACE -c database -- psql -h localhost -U postgres
Password for user postgres:
psql (18.3)
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_128_GCM_SHA256, compression: off, ALPN: postgresql)
Type "help" for help.

postgres=#

輸入 \q 並按下 Enter 鍵,結束 psql 工作階段:

postgres=# \q

5. 在 GKE 上部署 EmbeddingGemma 模型

如要測試 AlloyDB Omni AI 與本機模型的整合,請將嵌入模型部署至 GKE 叢集。本教學課程使用 Google 的 EmbeddingGemma 模型。

為模型建立節點集區

如要執行模型推論,請準備專屬節點集區。您可以選擇僅使用 CPU 的節點集區,或是使用 GPU 加速的節點集區 (例如g2-standard-8搭配 NVIDIA L4 GPU)。本教學課程使用以 CPU 為基礎的節點集區,並採用 c3-standard-8 機器類型。

建立單一節點 CPU 節點集區:

export PROJECT_ID=$(gcloud config get-value project)
export LOCATION=us-central1
export CLUSTER_NAME=alloydb-ai-gke
gcloud container node-pools create cpupool \
  --project=${PROJECT_ID} \
  --location=${LOCATION} \
  --node-locations=${LOCATION}-a \
  --cluster=${CLUSTER_NAME} \
  --machine-type=c3-standard-8 \
  --num-nodes=1

預期輸出內容:

student@cloudshell$ export PROJECT_ID=$(gcloud config get project)
Your active configuration is: [pant]
export LOCATION=us-central1
export CLUSTER_NAME=alloydb-ai-gke
student@cloudshell$ gcloud container node-pools create cpupool \
>   --project=${PROJECT_ID} \
>   --location=${LOCATION} \
>   --node-locations=${LOCATION}-a \
>   --cluster=${CLUSTER_NAME} \
>   --machine-type=c3-standard-8 \
>   --num-nodes=1
Creating node pool cpupool...done.
Created [https://container.googleapis.com/v1/projects/gleb-test-short-003-483115/zones/us-central1/clusters/alloydb-ai-gke/nodePools/cpupool].
NAME     MACHINE_TYPE    DISK_SIZE_GB  NODE_VERSION
cpupool  c3-standard-8  100           1.34.1-gke.3355002

取得 Hugging Face 權杖

本教學課程會從 Hugging Face 部署 EmbeddingGemma 模型。如要存取模型權重,請產生 Hugging Face 存取權杖:

  1. 登入或建立 Hugging Face 帳戶。
  2. 依序前往「Your Profile」 >「Access Tokens」
  3. 按一下「建立新權杖」
  4. 輸入權杖名稱,然後選取「讀取」角色。
  5. 按一下「建立權杖」,然後複製產生的權杖值。
  6. 如果尚未接受模型條款,請前往 EmbeddingGemma 模型頁面接受。

在 Cloud Shell 中建立包含 Hugging Face 權杖的 Kubernetes 密鑰 (將權杖預留位置替換為您的權杖):

export HF_TOKEN=<YOUR_HUGGING_FACE_TOKEN>
kubectl create secret generic hf-secret \
  --from-literal=hf_api_token=$HF_TOKEN \
  --dry-run=client -o yaml | kubectl apply -f -

準備部署資訊清單

如要部署模型,請使用 Hugging Face 的 Text Embeddings Inference (TEI) 容器套件。詳情請參閱 Hugging Face GKE TEI 說明文件

從 GitHub 複製部署存放區:

git clone https://github.com/huggingface/Google-Cloud-Containers

檢查及修改 CPU 設定資訊清單:

edit Google-Cloud-Containers/examples/gke/tei-deployment/cpu-config/deployment.yaml

CPU 部署作業的更新資訊清單:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tei-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: tei-server
  template:
    metadata:
      labels:
        app: tei-server
        hf.co/model: Google--embeddinggemma-300m
        hf.co/task: text-embeddings
    spec:
      containers:
        - name: tei-container
          image: ghcr.io/huggingface/text-embeddings-inference:cpu-latest
          resources:
            requests:
              cpu: "6"
              memory: "24Gi"
            limits:
              cpu: "6"
              memory: "24Gi"
          env:
            - name: MODEL_ID
              value: google/embeddinggemma-300m
            - name: NUM_SHARD
              value: "1"
            - name: PORT
              value: "8080"
            - name: HF_TOKEN
              valueFrom:
                secretKeyRef:
                  name: hf-secret
                  key: hf_api_token
          volumeMounts:
            - mountPath: /tmp
              name: tmp
      volumes:
        - name: tmp
          emptyDir: {}
      nodeSelector:
        cloud.google.com/machine-family: "c3"

按下 ctrl+s 儲存變更,然後切換回終端機。

部署模型

套用資訊清單來部署 TEI 伺服器:

kubectl apply -f Google-Cloud-Containers/examples/gke/tei-deployment/cpu-config

監控部署作業,直到部署作業處於就緒狀態為止:

printf "Waiting for model to load..."; until kubectl logs -l app=tei-server --tail=50 2>/dev/null | grep -q "Ready"; do printf "."; sleep 3; done; printf '\n\033[1;32m========================================\n[SUCCESS] Model is loaded and ready!\nYou can now proceed to the next step.\n========================================\033[0m\n'

檢查 tei-service Kubernetes 服務:

kubectl get service tei-service

預期輸出內容:

student@cloudshell$ kubectl get service tei-service
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
tei-service   ClusterIP   34.118.233.48   <none>        8080/TCP   10m

服務 CLUSTER-IP 會在內部 http://34.118.233.48:8080/embed 處理要求。

使用 kubectl port-forward 在本機測試模型端點:

kubectl port-forward service/tei-service 8080:8080

點選終端機頂端的「+」,開啟第二個 Cloud Shell 分頁:

新增 Cloud Shell 分頁

在新分頁中,使用 curl 測試嵌入生成功能:

curl http://localhost:8080/embed \
  -X POST \
  -d '{"inputs":"Test"}' \
  -H 'Content-Type: application/json'

預期輸出內容 (向量陣列):

curl http://localhost:8080/embed \
>     -X POST \
>     -d '{"inputs":"Test"}' \
>     -H 'Content-Type: application/json'
[[-0.018975832,0.0071419072,0.06347208,0.022992613,0.014205903
...
-0.03677433,0.01636146,0.06731572]]

在第一個分頁中按下 ctrl+c,停止連接埠轉送。

6. 在 AlloyDB Omni 中註冊嵌入模型

如要從 AlloyDB Omni 使用已部署的模型,請建立資料庫、定義轉換函式,並註冊模型端點。

建立用戶端 VM 和資料庫

在同一個 VPC 中建立 Compute Engine VM 執行個體,做為用戶端跳板機:

網路架構圖,顯示用戶端 VM 和 AlloyDB Omni

在 Cloud Shell 中建立用戶端 VM:

export ZONE=us-central1-a
gcloud compute instances create instance-1 \
  --zone=$ZONE

擷取 AlloyDB Omni 端點 IP:

echo "INSTANCE_IP=$(kubectl get dbclusters.alloydbomni.dbadmin.goog my-omni -n default -o jsonpath='{.status.primary.endpoint}')"

預期輸出內容:

INSTANCE_IP=10.128.0.33

INSTANCE_IP 值是 AlloyDB Omni 叢集的內部負載平衡器 IP。在本範例中為 10.131.0.33

使用 SSH 連線至 VM 執行個體:

gcloud compute ssh instance-1 --zone=$ZONE

instance-1 的 SSH 工作階段中,安裝 PostgreSQL 用戶端:

sudo apt-get update && sudo apt-get install --yes postgresql-client 

匯出 AlloyDB Omni 負載平衡器 IP (請替換為您的 PRIMARYENDPOINT IP):

export INSTANCE_IP=10.131.0.33

使用 psql 連線至 AlloyDB Omni (密碼為 VeryStrongPassword):

psql "host=$INSTANCE_IP user=postgres sslmode=require"

psql 工作階段中,建立 demo 資料庫:

CREATE DATABASE demo;

切換至 demo 資料庫:

\c demo

建立轉換函式

自訂嵌入端點需要輸入和輸出轉換函式,才能在 AlloyDB Omni 和模型 API 之間調整資料格式。

建立輸入轉換函式:

CREATE OR REPLACE FUNCTION tei_text_input_transform(model_id VARCHAR(100), input_text TEXT)
RETURNS JSON
LANGUAGE plpgsql
AS $$
DECLARE
  transformed_input JSON;
BEGIN
  SELECT json_build_object('inputs', input_text, 'truncate', true)::JSON INTO transformed_input;
  RETURN transformed_input;
END;
$$;

預期輸出內容:

demo=# CREATE OR REPLACE FUNCTION tei_text_input_transform(model_id VARCHAR(100), input_text TEXT)
RETURNS JSON
LANGUAGE plpgsql
AS $$
DECLARE
  transformed_input JSON;
BEGIN
  SELECT json_build_object('inputs', input_text, 'truncate', true)::JSON INTO transformed_input;
  RETURN transformed_input;
END;
$$;
CREATE FUNCTION
demo=#

建立輸出轉換函式,剖析向量陣列回應:

CREATE OR REPLACE FUNCTION tei_text_output_transform(model_id VARCHAR(100), response_json JSON)
RETURNS REAL[]
LANGUAGE plpgsql
AS $$
DECLARE
  transformed_output REAL[];
BEGIN
  SELECT ARRAY(SELECT json_array_elements_text(response_json->0)) INTO transformed_output;
  RETURN transformed_output;
END;
$$;

預期輸出內容:

demo=# CREATE OR REPLACE FUNCTION tei_text_output_transform(model_id VARCHAR(100), response_json JSON)
RETURNS REAL[]
LANGUAGE plpgsql
AS $$
DECLARE
  transformed_output REAL[];
BEGIN
  SELECT ARRAY(SELECT json_array_elements_text(response_json->0)) INTO transformed_output;
  RETURN transformed_output;
END;
$$;
CREATE FUNCTION
demo=#

註冊模型

使用 google_ml.create_model 程序在 AlloyDB Omni 註冊模型。將 http://tei-service:8080/embed 指定為 model_request_url,將要求轉送至 Kubernetes 叢集服務:

CALL
  google_ml.create_model(
    model_id => 'embeddinggemma',
    model_request_url => 'http://tei-service:8080/embed',
    model_provider => 'custom',
    model_type => 'text_embedding',
    model_in_transform_fn => 'tei_text_input_transform',
    model_out_transform_fn => 'tei_text_output_transform');

預期輸出內容:

demo=# CALL
  google_ml.create_model(
    model_id => 'embeddinggemma',
    model_request_url => 'http://tei-service:8080/embed',
    model_provider => 'custom',
    model_type => 'text_embedding',
    model_in_transform_fn => 'tei_text_input_transform',
    model_out_transform_fn => 'tei_text_output_transform');
CALL
demo=#

使用 SQL 查詢範例測試已註冊的模型:

SELECT google_ml.embedding('embeddinggemma', 'What is AlloyDB Omni?');

函式會傳回由 GKE 上執行的本機 EmbeddingGemma 模型產生的實數陣列表示法。

按下 q 即可返回 psql 工作階段提示。

退出 psql 工作階段:

\q

7. 使用範例資料測試模型

載入範例資料

本教學課程會使用 Cymbal 零售資料集,示範向量相似度搜尋。您將使用 Google Cloud SDK 和 PostgreSQL 用戶端,將資料匯入 AlloyDB Omni。

instance-1 的 SSH 工作階段中,連線至示範資料庫並啟用 vector 擴充功能:

psql "host=$INSTANCE_IP user=postgres sslmode=require dbname=demo"

在 psql 工作階段中:

CREATE EXTENSION IF NOT EXISTS vector;

退出 psql 工作階段:

\q

下載並套用結構定義,在 demo 資料庫中建立資料表:

gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_demo_schema.sql |psql "host=$INSTANCE_IP user=postgres dbname=demo"

預期輸出內容:

student@cloudshell:~$ gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_demo_schema.sql |psql "host=$INSTANCE_IP user=postgres dbname=demo"
Password for user postgres:
SET
SET
SET
SET
SET
 set_config
------------

(1 row)

SET
SET
SET
SET
SET
SET
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE TABLE
ALTER TABLE
CREATE SEQUENCE
ALTER TABLE
ALTER SEQUENCE
ALTER TABLE
ALTER TABLE
ALTER TABLE
student@cloudshell:~$

確認已建立的資料表:

psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "\dt+"

預期輸出內容:

student@cloudshell:~$ psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "\dt+"
Password for user postgres: 
                                           List of relations
 Schema |       Name       | Type  |  Owner   | Persistence | Access method |    Size    | Description 
--------+------------------+-------+----------+-------------+---------------+------------+-------------
 public | cymbal_embedding | table | postgres | permanent   | heap          | 8192 bytes | 
 public | cymbal_inventory | table | postgres | permanent   | heap          | 8192 bytes | 
 public | cymbal_products  | table | postgres | permanent   | heap          | 8192 bytes | 
 public | cymbal_stores    | table | postgres | permanent   | heap          | 8192 bytes | 
(4 rows)

將資料載入 cymbal_products 資料表:

gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_products.csv |psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "\copy cymbal_products from stdin csv header"

預期輸出內容:

student@cloudshell:~$ gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_products.csv |psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "\copy cymbal_products from stdin csv header"
COPY 941
student@cloudshell:~$ 

以下是 cymbal_products 資料表的部分資料列範例。

psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "SELECT uniq_id,left(product_name,30),left(product_description,50),sale_price FROM cymbal_products limit 3"

預期輸出內容:

student@cloudshell:~$ psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "SELECT uniq_id,left(product_name,30),left(product_description,50),sale_price FROM cymbal_products limit 3"
Password for user postgres: 
             uniq_id              |              left              |                        left                        | sale_price 
----------------------------------+--------------------------------+----------------------------------------------------+------------
 a73d5f754f225ecb9fdc64232a57bc37 | Laundry Tub Strainer Cup       |   Laundry tub strainer cup Chrome For 1-.50, drain |      11.74
 41b8993891aa7d39352f092ace8f3a86 | LED Starry Star Night Light La |  LED Starry Star Night Light Laser Projector 3D Oc |      46.97
 ed4a5c1b02990a1bebec908d416fe801 | Surya Horizon HRZ-1060 Area Ru |  The 100% polypropylene construction of the Surya  |       77.4
(3 rows)
student@cloudshell:~$ 

將資料載入 cymbal_inventory 資料表:

gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_inventory.csv |psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "\copy cymbal_inventory from stdin csv header"

預期輸出內容:

student@cloudshell:~$ gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_inventory.csv |psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "\copy cymbal_inventory from stdin csv header"
Password for user postgres: 
COPY 263861
student@cloudshell:~$ 

以下是 cymbal_inventory 資料表的部分資料列範例。

psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "SELECT * FROM cymbal_inventory LIMIT 3"

輸出內容:

student@cloudshell:~$ psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "SELECT * FROM cymbal_inventory LIMIT 3"
Password for user postgres: 
 store_id |             uniq_id              | inventory 
----------+----------------------------------+-----------
     1583 | adc4964a6138d1148b1d98c557546695 |         5
     1490 | adc4964a6138d1148b1d98c557546695 |         4
     1492 | adc4964a6138d1148b1d98c557546695 |         3
(3 rows)
student@cloudshell:~$ 

將資料載入 cymbal_stores 資料表:

gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_stores.csv |psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "\copy cymbal_stores from stdin csv header"

預期的控制台輸出內容:

student@cloudshell:~$ gcloud storage cat gs://cloud-training/gcc/gcc-tech-004/cymbal_stores.csv |psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "\copy cymbal_stores from stdin csv header"
Password for user postgres: 
COPY 4654
student@cloudshell:~$

以下是 cymbal_stores 資料表的部分資料列範例。

psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "SELECT store_id, name, zip_code FROM cymbal_stores limit 3"

輸出內容:

student@cloudshell:~$ psql "host=$INSTANCE_IP user=postgres dbname=demo" -c "SELECT store_id, name, zip_code FROM cymbal_stores limit 3"
Password for user postgres: 
 store_id |       name        | zip_code 
----------+-------------------+----------
     1990 | Mayaguez Store    |      680
     2267 | Ware Supercenter  |     1082
     4359 | Ponce Supercenter |      780
(3 rows)
student@cloudshell:~$ 

建立嵌入

使用 psql 連線至示範資料庫,並根據產品說明,為 cymbal_products 資料表中的產品建立嵌入。

連線至示範資料庫:

psql "host=$INSTANCE_IP user=postgres sslmode=require dbname=demo"

使用 embedding 類型的 vector 欄,儲存產品說明生成的文字嵌入。

啟用查詢時間:

\timing

為每個產品說明生成嵌入項目,並儲存在 cymbal_embedding 資料表中:

INSERT INTO cymbal_embedding (uniq_id, embedding)
SELECT uniq_id, google_ml.embedding('embeddinggemma', product_description)::vector
FROM cymbal_products;

預期輸出內容:

demo=# INSERT INTO cymbal_embedding(uniq_id,embedding)  SELECT uniq_id, google_ml.embedding('embeddinggemma',product_description)::vector FROM cymbal_products;
INSERT 0 941
Time: 497878.136 ms (08:17.878)
demo=#

執行語意搜尋查詢

psql 工作階段中,使用餘弦距離 (<=>) 找出與問題 "What kind of fruit trees grow well here?" 相符的前五項產品:

SELECT
    cp.product_name,
    left(cp.product_description, 80) AS description,
    cp.sale_price,
    cs.zip_code,
    (ce.embedding <=> google_ml.embedding('embeddinggemma', 'What kind of fruit trees grow well here?')::vector) AS distance
FROM
    cymbal_products cp
JOIN cymbal_embedding ce ON ce.uniq_id = cp.uniq_id
JOIN cymbal_inventory ci ON ci.uniq_id = cp.uniq_id
JOIN cymbal_stores cs ON cs.store_id = ci.store_id
WHERE
    ci.inventory > 0
    AND cs.store_id = 1583
ORDER BY
    distance ASC
LIMIT 5;

預期輸出內容:

demo=# SELECT
        cp.product_name,
        left(cp.product_description,80) as description,
        cp.sale_price,
        cs.zip_code,
        (ce.embedding <=> google_ml.embedding('embeddinggemma','What kind of fruit trees grow well here?')::vector) as distance
FROM
        cymbal_products cp
JOIN cymbal_embedding ce on ce.uniq_id=cp.uniq_id
JOIN cymbal_inventory ci on ci.uniq_id=cp.uniq_id
JOIN cymbal_stores cs on cs.store_id=ci.store_id
WHERE
    ci.inventory > 0
    AND cs.store_id = 1583
ORDER BY
    distance ASC
LIMIT 5;
     product_name      |                                   description                                    | sale_price | zip_code |      distance
-----------------------+----------------------------------------------------------------------------------+------------+----------+--------------------
 Cherry Tree           | This is a beautiful cherry tree that will produce delicious cherries. It is an d |      75.00 |    93230 | 0.5210549378080666
 California Lilac      | This is a beautiful lilac tree that can grow to be over 10 feet tall. It is an d |       5.00 |    93230 | 0.5639421771781971
 Toyon                 | This is a beautiful toyon tree that can grow to be over 20 feet tall. It is an e |      10.00 |    93230 | 0.5670010914504852
 Rose Bush             | This is a beautiful rose bush that will produce fragrant roses. It is a perennia |      50.00 |    93230 | 0.5731542622882957
 California Peppertree | This is a beautiful peppertree that can grow to be over 30 feet tall. It is an e |      25.00 |    93230 | 0.5750934653011995
(5 rows)

Time: 83.610 ms
demo=#

這項查詢執行了 83 毫秒,並從 cymbal_products 資料表傳回符合要求的樹狀結構清單,且商店 1583 號有現貨。

建構 ANN 索引

資料集較小時,系統會掃描所有嵌入內容,因此很容易使用精確搜尋。但隨著資料量增加,載入和回應時間也會變長。如要提升效能,可以在嵌入資料上建立索引。以下範例說明如何使用向量資料的 Google ScaNN 索引執行這項操作。

如果連線中斷,請重新連線至試用資料庫:

psql "host=$INSTANCE_IP user=postgres sslmode=require dbname=demo"

啟用 alloydb_scann 擴充功能:

CREATE EXTENSION IF NOT EXISTS alloydb_scann;

embedding 欄上建立 ScaNN 索引:

CREATE INDEX cymbal_products_embeddings_scann ON cymbal_embedding
  USING scann (embedding cosine)
  WITH (num_leaves=10, max_num_levels = 1);

重新執行語意搜尋查詢,比較執行效能:

SELECT
    cp.product_name,
    left(cp.product_description, 80) AS description,
    cp.sale_price,
    cs.zip_code,
    (ce.embedding <=> google_ml.embedding('embeddinggemma', 'What kind of fruit trees grow well here?')::vector) AS distance
FROM
    cymbal_products cp
JOIN cymbal_embedding ce ON ce.uniq_id = cp.uniq_id
JOIN cymbal_inventory ci ON ci.uniq_id = cp.uniq_id
JOIN cymbal_stores cs ON cs.store_id = ci.store_id
WHERE
    ci.inventory > 0
    AND cs.store_id = 1583
ORDER BY
    distance ASC
LIMIT 5;

預期輸出內容:

demo=# SELECT
    cp.product_name,
    left(cp.product_description,80) as description,
    cp.sale_price,
    cs.zip_code,
    (ce.embedding <=> google_ml.embedding('embeddinggemma', 'What kind of fruit trees grow well here?')::vector) AS distance
FROM
    cymbal_products cp
JOIN cymbal_embedding ce ON ce.uniq_id = cp.uniq_id
JOIN cymbal_inventory ci ON ci.uniq_id = cp.uniq_id
JOIN cymbal_stores cs ON cs.store_id = ci.store_id
WHERE
    ci.inventory > 0
    AND cs.store_id = 1583
ORDER BY
    distance ASC
LIMIT 5;
     product_name      |                                   description                                    | sale_price | zip_code |      distance
-----------------------+----------------------------------------------------------------------------------+------------+----------+--------------------
 Cherry Tree           | This is a beautiful cherry tree that will produce delicious cherries. It is an d |      75.00 |    93230 | 0.5210549378080666
 California Lilac      | This is a beautiful lilac tree that can grow to be over 10 feet tall. It is an d |       5.00 |    93230 | 0.5639421771781971
 Toyon                 | This is a beautiful toyon tree that can grow to be over 20 feet tall. It is an e |      10.00 |    93230 | 0.5670010914504852
 Rose Bush             | This is a beautiful rose bush that will produce fragrant roses. It is a perennia |      50.00 |    93230 | 0.5731542622882957
 California Peppertree | This is a beautiful peppertree that can grow to be over 30 feet tall. It is an e |      25.00 |    93230 | 0.5750934653011995
(5 rows)

Time: 64.783 ms

查詢執行時間略為縮短,資料集越大,增幅就越明顯。傳回的資料應與沒有索引時的資料相同或非常相似。

請嘗試其他查詢,並參閱說明文件,進一步瞭解如何最佳化向量索引。

退出 psql 工作階段:

\q

按下 CTRL+D 鍵或輸入 exit,中斷 instance-1 ssh 工作階段,返回 Google Cloud Shell。

8. 透過 vLLM 部署 Gemma

為 Gemma 新增節點集區

首先,請查看您所在區域可用的節點類型:

export LOCATION=us-central1-a
gcloud compute accelerator-types list --filter="zone:${LOCATION}"

您應該會看到可用加速器類型清單,包括 nvidia-l4 加速器。現在,請建立使用 nvidia-l4 加速器類型的節點集區:

export PROJECT_ID=$(gcloud config get project)
export LOCATION=us-central1
export CLUSTER_NAME=alloydb-ai-gke
gcloud container node-pools create gpupool \
    --accelerator type=nvidia-l4,count=1,gpu-driver-version=latest \
    --project=${PROJECT_ID} \
    --location=${LOCATION} \
    --node-locations=${LOCATION}-a \
    --cluster=${CLUSTER_NAME} \
    --machine-type=g2-standard-8 \
    --num-nodes=1

使用 vLLM 為 Google Gemini 4 12B 模型建立 Deployment 資訊清單:

cat << 'EOF' > gemma-12b-gpu-vllm-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: gemma-12b-gpu-vllm-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: gemma-12b-gpu-vllm
  template:
    metadata:
      labels:
        app: gemma-12b-gpu-vllm
        ai.gke.io/model: gemma-4-12b-it
        ai.gke.io/inference-server: vllm
        examples.ai.gke.io/source: user-guide
    spec:
      containers:
      - name: inference-server
        image: us-docker.pkg.dev/vertex-ai/vertex-vision-model-garden-dockers/pytorch-vllm-serve:latest
        resources:
          requests:
            cpu: "4"
            memory: "16Gi"
            ephemeral-storage: "30Gi"
            nvidia.com/gpu: "1"
          limits:
            cpu: "8"
            memory: "24Gi"
            ephemeral-storage: "30Gi"
            nvidia.com/gpu: "1"
        command: ["python3", "-m", "vllm.entrypoints.api_server"]
        args:
          - --model=$(MODEL_ID)
          - --host=0.0.0.0
          - --port=8000
          - --tensor-parallel-size=1
          - --enable-log-requests
          - --enable-chunked-prefill
          - --enable-prefix-caching
          - --enable-auto-tool-choice
          - --generation-config=auto
          - --tool-call-parser=gemma4
          - --dtype=bfloat16
          - --max-num-seqs=16
          - --max-model-len=32768
          - --gpu-memory-utilization=0.95
          - --reasoning-parser=gemma4
          - --trust-remote-code
          - --quantization=fp8
        env:
        - name: LD_LIBRARY_PATH
          value: ${LD_LIBRARY_PATH}:/usr/local/nvidia/lib64
        - name: MODEL_ID
          value: google/gemma-4-12b-it
        - name: HF_TOKEN
          valueFrom:
            secretKeyRef:
              name: hf-secret
              key: hf_api_token
        volumeMounts:
        - mountPath: /dev/shm
          name: dshm
      volumes:
      - name: dshm
        emptyDir:
            medium: Memory
      nodeSelector:
        cloud.google.com/gke-accelerator: nvidia-l4
        cloud.google.com/gke-gpu-driver-version: latest
---
apiVersion: v1
kind: Service
metadata:
  name: gemma-12b-gpu-vllm-service
spec:
  selector:
    app: gemma-12b-gpu-vllm
  type: ClusterIP
  ports:
    - protocol: TCP
      port: 8000
      targetPort: 8000
EOF

套用已儲存的 gemma-12b-gpu-vllm-deployment.yaml 部署作業:

kubectl apply -f gemma-12b-gpu-vllm-deployment.yaml

預期輸出內容:

$ kubectl apply -f gemma-12b-gpu-vllm-deployment.yaml
deployment.apps/gemma-12b-gpu-vllm-deployment created
service/gemma-12b-gpu-vllm-service created

等待部署作業完成並載入模型。這可能需要幾分鐘的時間。

printf "Waiting for model to load..."; until kubectl logs -l app=gemma-12b-gpu-vllm --tail=50 2>/dev/null | grep -q "Application startup complete"; do printf "."; sleep 3; done; printf '\n\033[1;32m========================================\n[SUCCESS] Model is loaded and ready!\nYou can now proceed to the next step.\n========================================\033[0m\n'

預期輸出內容:

Waiting for model to load...
========================================
[SUCCESS] Model is loaded and ready!
You can now proceed to the next step.
========================================

測試模型。啟用通訊埠轉送功能,即可存取模型:

kubectl port-forward svc/gemma-12b-gpu-vllm-service 8090:8000

在另一個終端機視窗中,使用 curl 將提示傳送至模型:

curl http://localhost:8090/v1/chat/completions \
  -H "Content-Type: application/json" \
  -d '{
    "messages": [
      {"role": "system", "content": "You are a helpful assistant running on GKE."},
      {"role": "user", "content": "What is AlloyDB Omni."}
    ],
    "temperature": 0.7
  }' | jq -r '.choices[0].message.content'

預期輸出內容:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  3957  100  3761  100   196     85      4  0:00:49  0:00:43  0:00:06   830
**AlloyDB Omni** is a fully managed, PostgreSQL-compatible database engine from Google Cloud that can be run **on-premises, in other clouds, or in your own data centers.**

To understand it simply: It allows you to run the high-performance, enterprise-grade capabilities of Google's **AlloyDB** (a cloud-native database) on your own infrastructure.

Here is a breakdown of what makes it significant:

### 1. The "Best of Both Worlds" Architecture
Normally, you have to choose between:
*   **Managed Cloud Databases:** Easy to scale and manage, but you are locked into the cloud provider's infrastructure.
*   **Self-Managed Databases:** You have full control over the hardware/location, but you are responsible for scaling, patching, and high availability.

**AlloyDB Omni** bridges this gap. It provides the advanced features of a cloud-native database (like intelligent indexing, high availability, and massive scalability) while allowing you to run it anywhere.

在第一個終端機中,按下 Ctrl+C 停止通訊埠轉送 (如果仍在執行)。

9. 在 AlloyDB Omni 註冊 Gemma 4 模型

使用 google_ml.create_model 程序在 AlloyDB Omni 註冊 Gemma 12B 模型。將 http://gemma-12b-gpu-vllm-service:8000/v1/chat/completions 指定為 model_request_url,將要求轉送至 Kubernetes 叢集服務:

擷取 AlloyDB Omni 端點 IP:

echo "INSTANCE_IP=$(kubectl get dbclusters.alloydbomni.dbadmin.goog my-omni -n default -o jsonpath='{.status.primary.endpoint}')"

使用 SSH 連線至 VM 執行個體:

export ZONE=us-central1-a
gcloud compute ssh instance-1 --zone=$ZONE

連線至 VM 後,請匯出上一個步驟中的 INSTANCE_IP 變數 (10.128.0.33 值僅為範例,請改為您的 IP):

export INSTANCE_IP=10.128.0.33

匯出 AlloyDB 密碼:

export PGPASSWORD=VeryStrongPassword

連線到 demo 資料庫:

psql "host=$INSTANCE_IP user=postgres sslmode=require dbname=demo"

在 psql 工作階段中註冊模型:

CALL
  google_ml.create_model(
    model_id => 'gemma-12b-gpu',
    model_request_url => 'http://gemma-12b-gpu-vllm-service:8000/v1/chat/completions',
    model_provider => 'custom',
    model_type => 'llm');

使用 SQL 查詢範例測試模型:

SELECT google_ml.predict_row(
  model_id => 'gemma-12b-gpu',
  request_body => json_build_object(
    'messages', json_build_array(
      json_build_object('role', 'user', 'content', 'What is AlloyDB Omni?'))))->'choices'->0->'message'->'content';

按下 q 即可從結果視窗返回 psql 提示

在 AlloyDB Omni 中,將向量搜尋與 LLM RAG 結合使用

搭配使用向量搜尋與 LLM 要求,向 LLM 示範 RAG (檢索增強生成)。

在 plsql 中執行 SQL 查詢:

WITH trees AS (
SELECT
        cp.product_name,
        cp.product_description AS description,
        cp.sale_price,
        cs.zip_code,
        cp.uniq_id AS product_id
FROM
    cymbal_products cp
JOIN cymbal_embedding ce ON ce.uniq_id = cp.uniq_id
JOIN cymbal_inventory ci ON ci.uniq_id = cp.uniq_id
JOIN cymbal_stores cs ON cs.store_id = ci.store_id
WHERE
        ci.inventory>0
        AND cs.store_id = 1583
ORDER BY
        (ce.embedding <=> embedding('embeddinggemma',
        'What kind of fruit trees grow well here?')::vector) ASC
LIMIT 1),
prompt AS (
SELECT
        'You are a friendly advisor helping to find a product based on the customer''s needs.
Based on the client request we have loaded a list of products closely related to search.
The list in JSON format with list of values like {"product_name":"name","product_description":"some description","sale_price":10}
Here is the list of products:' || json_agg(trees) || 'The customer asked "What kind of fruit trees grow well here?"
You should give information about the product, price and some supplemental information' AS prompt_text
FROM
        trees),
response AS (
SELECT
        google_ml.predict_row(
          model_id =>'gemma-12b-gpu',
          request_body => json_build_object(
            'messages', json_build_array(
              json_build_object('role', 'user', 'content',prompt_text)
            )))->'choices'->0->'message'->'content' AS resp
FROM
        prompt)
SELECT
REPLACE(resp::text, '\n', CHR(10))
FROM
        response;

預期輸出內容:

----------------------------------------------------------------------------------------------------------------------------------------------
 "Hello there! I'd be happy to help you find the perfect tree for your garden.                                                               +
                                                                                                                                             +
 Based on your location, we have a wonderful option that would grow beautifully in your area:                                                +
                                                                                                                                             +
 **Cherry Tree**                                                                                                                             +
 *   **Price:** $75.00                                                                                                                       +
 *   **Description:** This is a stunning deciduous tree that not only provides a beautiful landscape but also produces delicious cherries.   +
 *   **Supplemental Information:**                                                                                                           +
     *   **Growth:** It grows to about 15 feet tall.                                                                                         +
     *   **Appearance:** You can look forward to dark green leaves in the summer that transform into a vibrant red in the fall.              +
     *   **Benefits:** It's a great choice if you're looking for both fruit and extra shade or privacy in your yard.                         +
     *   **Care Tips:** It performs best in a cool, moist climate with sandy soil. Since you are in a suitable zone, it should thrive nicely!+
                                                                                                                                             +
 Would you like more details on how to plant this, or would you like to proceed with an order?"
(1 row)

查詢會根據向量搜尋結果,補充 LLM 的提示。

請嘗試其他查詢,並試用 RAG 模式。所呈現架構的優點在於完全自給自足。資料不會傳送至叢集外部,且可在完全隔離的環境中執行。

結束 psql 工作階段:

\q

中斷與 VM 的 SSH 工作階段連線:

exit

別忘了,AlloyDB Omni 還有更多功能和實驗室。

10. 清理環境

如要避免系統持續向您的 Google Cloud 帳戶收費,請刪除本程式碼研究室建立的資源。

刪除 GKE 叢集

在 Cloud Shell 中刪除 GKE 叢集:

export PROJECT_ID=$(gcloud config get-value project)
export LOCATION=us-central1
export CLUSTER_NAME=alloydb-ai-gke
gcloud container clusters delete ${CLUSTER_NAME} \
  --project=${PROJECT_ID} \
  --region=${LOCATION}

預期輸出內容:

student@cloudshell:~$ gcloud container clusters delete ${CLUSTER_NAME} \
>   --project=${PROJECT_ID} \
>   --region=${LOCATION}
The following clusters will be deleted.
 - [alloydb-ai-gke] in [us-central1]

Do you want to continue (Y/n)?  Y

Deleting cluster alloydb-ai-gke...done.
Deleted

刪除用戶端 VM

在 Cloud Shell 中刪除 Compute Engine 執行個體:

export PROJECT_ID=$(gcloud config get-value project)
export ZONE=us-central1-a
gcloud compute instances delete instance-1 \
  --project=${PROJECT_ID} \
  --zone=${ZONE}

預期輸出內容:

student@cloudshell:~$ export PROJECT_ID=$(gcloud config get project)
export ZONE=us-central1-a
gcloud compute instances delete instance-1 \
  --project=${PROJECT_ID} \
  --zone=${ZONE}
Your active configuration is: [cloudshell-5399]
The following instances will be deleted. Any attached disks configured to be auto-deleted will be deleted unless they are attached to any other instances or the `--keep-disks` flag is given and specifies them for keeping. Deleting a disk 
is irreversible and any data on the disk will be lost.
 - [instance-1] in [us-central1-a]

Do you want to continue (Y/n)?  Y

Deleted

如果您為本程式碼研究室建立了新專案,可以選擇在 Google Cloud Resource Manager 中刪除整個專案。

11. 恭喜

恭喜您完成本程式碼研究室!

涵蓋範圍

  • 如何在 GKE 叢集上部署 AlloyDB Omni
  • 如何連線至 AlloyDB Omni
  • 如何將資料載入 AlloyDB Omni
  • 如何在 GKE 部署 AI 模型 (嵌入和 LLM)
  • 如何在 AlloyDB Omni 註冊 AI 模型
  • 如何生成語意搜尋的嵌入項目
  • 如何在 AlloyDB Omni 中執行語意搜尋查詢
  • 如何在 AlloyDB Omni 中建立及使用向量索引

如要進一步瞭解如何在 AlloyDB Omni 中使用 AI,請參閱說明文件

問卷調查

輸出內容:

您會如何使用本教學課程?

僅閱讀內容 閱讀內容並完成練習