在本教學課程中,您會使用 Model Garden 將 Gemma 2 2B 開放式模型部署至採用 TPU 的 Gemini Enterprise Agent Platform 端點。您必須先將模型部署至端點,才能使用模型進行線上預測。部署過程中,系統會將實體資源與模型建立關聯,讓模型以低延遲的方式提供線上預測結果。
部署 Gemma 2 2B 模型後,您可以使用 PredictionServiceClient 推論訓練好的模型,以取得線上預測結果。線上預測是對部署至端點的模型發出的同步要求。
目標
本教學課程說明如何執行下列工作:
- 使用 Model Garden 將 Gemma 2 2B 開放式模型部署至採用 TPU 的端點
- 使用
PredictionServiceClient取得線上預測結果
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
-
A
ct5lp-hightpu-1tmachine type with one TPU_V5 accelerator - Gemini Enterprise Agent Platform prediction and explanation
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除建立的資源,避免繼續計費,詳情請參閱「清除所用資源」。
事前準備
本教學課程需要您:
- 設定 Google Cloud 專案並啟用 Agent Platform API
- 在本機上:
- 安裝、初始化及驗證 Google Cloud CLI
- 安裝適用於您所用語言的 SDK
設定 Google Cloud 專案
設定 Google Cloud 專案並啟用 Agent Platform API。
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Agent Platform API, if it is not already enabled.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.
設定 Google Cloud CLI
在本機電腦上設定 Google Cloud CLI。
-
安裝並初始化 Google Cloud CLI。
-
如果您先前已安裝 gcloud CLI,請執行下列指令,確保
gcloud元件已更新。gcloud components update
-
如要使用 gcloud CLI 進行驗證,請執行下列指令,產生本機應用程式預設憑證 (ADC) 檔案。指令啟動的網頁流程會用於提供使用者憑證。
gcloud auth application-default login
詳情請參閱「gcloud CLI 驗證設定和 ADC 設定」。
為程式設計語言設定 SDK
如要設定本教學課程使用的環境,請為您的語言安裝 Gemini Enterprise Agent Platform SDK 和 Protocol Buffers 程式庫。程式碼範例會使用通訊協定緩衝區程式庫中的函式,將輸入字典轉換為 API 預期的 JSON 格式。
在本機上,按一下下列其中一個分頁標籤,安裝您所用程式語言的 SDK。
Python
在本機上,按一下下列其中一個分頁標籤,安裝程式設計語言的 SDK。
執行下列指令,安裝及更新 Agent Platform SDK for Python。
pip3 install --upgrade "google-cloud-aiplatform>=1.64"
執行下列指令,安裝 Python 適用的 Protocol Buffers 程式庫。
pip3 install --upgrade "protobuf>=5.28"
Node.js
執行下列指令,安裝或更新 Node.js 適用的 aiplatform SDK。
npm install @google-cloud/aiplatform
Java
如要將 google-cloud-aiplatform 新增為依附元件,請為您的環境新增適當的程式碼。
搭配 BOM 使用 Maven
在 pom.xml 中新增下列 HTML:
<dependencyManagement> <dependencies> <dependency> <artifactId>libraries-bom</artifactId> <groupId>com.google.cloud</groupId> <scope>import</scope> <type>pom</type> <version>26.34.0</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-aiplatform</artifactId> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> </dependencies>
不使用 BOM 的 Maven
請將以下內容新增到 pom.xml:
<dependency> <groupId>com.google.cloud</groupId> <artifactId>google-cloud-aiplatform</artifactId> <version>1.1.0</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java-util</artifactId> <version>5.28</version> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.11.0</version> </dependency>
不使用 BOM 的 Gradle
請將以下內容新增到 build.gradle:
implementation 'com.google.cloud:google-cloud-aiplatform:1.1.0'Go
執行下列指令,安裝這些 Go 套件。
go get cloud.google.com/go/aiplatform go get google.golang.org/protobuf go get github.com/googleapis/gax-go/v2
使用 Model Garden 部署 Gemma
您將 Gemma 2 2B 模型部署至ct5lp-hightpu-1t Compute Engine 機器類型,該類型專為小型到中型訓練最佳化。這部機器有一個 TPU v5e 加速器。如要進一步瞭解如何使用 TPU 訓練模型,請參閱 Cloud TPU v5e 訓練。
在本教學課程中,您會使用 Model Garden 中的模型資訊卡,部署經過指令微調的 Gemma 2 2B 開放式模型。具體模型版本為 gemma2-2b-it,其中 -it 代表指令調整。
Gemma 2 2B 模型參數較少,因此資源需求較低,部署彈性也較高。
前往 Google Cloud 控制台的「Model Garden」(模型花園) 頁面。
點按「Gemma 2」Gemma 2模型資訊卡。
按一下「Deploy」(部署),開啟「Deploy model」(部署模型) 窗格。
在「Deploy model」窗格中,指定下列詳細資料。
在「部署環境」部分,點選「Agent Platform」。
在「Deploy model」(部署模型) 部分:
在「Resource ID」(資源 ID) 中選擇
gemma-2b-it。接受「模型名稱」和「端點名稱」的預設值。例如:
- 模型名稱:
gemma2-2b-it-1234567891234 - 端點名稱:
gemma2-2b-it-mg-one-click-deploy
請記下端點名稱,您需要這個 ID,才能找出程式碼範例中使用的端點 ID。
- 模型名稱:
在「部署作業設定」部分:
接受「基本」設定的預設選項。
針對「Region」(區域),請接受預設值,或是從清單中選擇區域。請記下這個區域。程式碼範例會用到。
在「Machine spec」(機器規格) 中,選擇以 TPU 為後端的執行個體:
ct5lp-hightpu-1t (1 TPU_V5_LITEPOD; ct5lp-hightpu-1t)。
按一下「Deploy」(部署)。部署完成後,您會收到一封電子郵件,內含新端點的詳細資料。您也可以依序點選「線上預測」>「端點」,然後選取所在區域,查看端點詳細資料。
使用 PredictionServiceClient 推論 Gemma 2 2B
部署 Gemma 2 2B 後,您可以使用 PredictionServiceClient 針對提示「為什麼天空是藍的?」取得線上預測。
程式碼參數
PredictionServiceClient 程式碼範例需要更新下列項目。
PROJECT_ID:如要找出專案 ID,請按照下列步驟操作。前往 Google Cloud 控制台的「Welcome」(歡迎) 頁面。
在頁面頂端的專案挑選器中選取專案。
專案名稱、專案編號和專案 ID 會顯示在「歡迎」標題下方。
ENDPOINT_REGION:這是部署端點的區域。ENDPOINT_ID:如要找出端點 ID,請在控制台中查看,或執行gcloud ai endpoints list指令。您需要「部署模型」窗格中的端點名稱和區域。控制台
如要查看端點詳細資料,請依序點選「線上預測」>「端點」,然後選取您的區域。請記下「
ID」欄中顯示的數字。gcloud
您可以執行
gcloud ai endpoints list指令,查看端點詳細資料。gcloud ai endpoints list \ --region=ENDPOINT_REGION \ --filter=display_name=ENDPOINT_NAME輸出內容如下所示。
Using endpoint [https://us-central1-aiplatform.googleapis.com/] ENDPOINT_ID: 1234567891234567891 DISPLAY_NAME: gemma2-2b-it-mg-one-click-deploy
程式碼範例
在您所用語言的程式碼範例中,更新 PROJECT_ID、ENDPOINT_REGION 和 ENDPOINT_ID。然後執行程式碼。
Python
如要瞭解如何安裝或更新 Vertex AI SDK for Python,請參閱「安裝 Vertex AI SDK for Python」。 詳情請參閱 Python API 參考文件。
Node.js
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Node.js 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
Java
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Java 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
Go
在試用這個範例之前,請先按照「使用用戶端程式庫的 Agent Platform 快速入門導覽課程」中的 Go 設定說明操作。
如要向 Agent Platform 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證機制」。
清除所用資源
為避免因為本教學課程所用資源,導致系統向 Google Cloud 收取費用,請刪除含有相關資源的專案,或者保留專案但刪除個別資源。
刪除專案
- 前往 Google Cloud 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。
刪除個別資源
如要保留專案,請刪除本教學課程中使用的資源:
- 取消部署模型並刪除端點
- 從 Model Registry 刪除模型
取消部署模型並刪除端點
請使用下列其中一種方法取消部署模型並刪除端點。
控制台
在 Google Cloud 控制台中,依序點選「線上預測」和「端點」。
在「Region」(區域) 下拉式清單中,選擇部署端點的區域。
按一下端點名稱,開啟詳細資料頁面。例如
gemma2-2b-it-mg-one-click-deploy。在
Gemma 2 (Version 1)模型所在列中,依序點選 「動作」和「從端點取消部署模型」。在「取消部署端點中的模型」對話方塊中,按一下「取消部署」。
按一下「Back」(返回) 按鈕,返回「Endpoints」(端點) 頁面。
在
gemma2-2b-it-mg-one-click-deploy列的結尾,按一下 「動作」,然後選取「刪除端點」。在確認提示中,按一下「確認」。
gcloud
如要使用 Google Cloud CLI 取消部署模型並刪除端點,請按照下列步驟操作。
在這些指令中,請按照以下方式替換:
- 將 PROJECT_ID 改成專案名稱
- 將 LOCATION_ID 替換為模型和端點的部署區域
- 將 ENDPOINT_ID 替換成端點 ID
- 將 DEPLOYED_MODEL_NAME 替換為模型顯示名稱
- DEPLOYED_MODEL_ID 替換為模型 ID
執行
gcloud ai endpoints list指令,取得端點 ID。這個指令會列出專案中所有端點的端點 ID。記下本教學課程中使用的端點 ID。gcloud ai endpoints list \ --project=PROJECT_ID \ --region=LOCATION_ID輸出內容如下所示。在輸出內容中,ID 稱為
ENDPOINT_ID。Using endpoint [https://us-central1-aiplatform.googleapis.com/] ENDPOINT_ID: 1234567891234567891 DISPLAY_NAME: gemma2-2b-it-mg-one-click-deploy執行
gcloud ai models describe指令,取得模型 ID。請記下您在本教學課程中部署的模型 ID。gcloud ai models describe DEPLOYED_MODEL_NAME \ --project=PROJECT_ID \ --region=LOCATION_ID縮寫輸出內容如下所示。在輸出內容中,ID 稱為
deployedModelId。Using endpoint [https://us-central1-aiplatform.googleapis.com/] artifactUri: [URI removed] baseModelSource: modelGardenSource: publicModelName: publishers/google/models/gemma2 ... deployedModels: - deployedModelId: '1234567891234567891' endpoint: projects/12345678912/locations/us-central1/endpoints/12345678912345 displayName: gemma2-2b-it-12345678912345 etag: [ETag removed] modelSourceInfo: sourceType: MODEL_GARDEN name: projects/123456789123/locations/us-central1/models/gemma2-2b-it-12345678912345 ...從端點取消部署模型。您需要先前指令中的端點 ID 和模型 ID。
gcloud ai endpoints undeploy-model ENDPOINT_ID \ --project=PROJECT_ID \ --region=LOCATION_ID \ --deployed-model-id=DEPLOYED_MODEL_ID這個指令不會產生任何輸出內容。
執行
gcloud ai endpoints delete指令,刪除端點。gcloud ai endpoints delete ENDPOINT_ID \ --project=PROJECT_ID \ --region=LOCATION_ID出現提示時,請輸入
y確認操作。這個指令不會產生任何輸出內容。
刪除模型
控制台
前往 Google Cloud 控制台的「Agent Platform」部分,然後前往「Model Registry」頁面。
在「Region」(區域) 下拉式清單中,選擇部署模型的區域。
在「
gemma2-2b-it-1234567891234」列的結尾,按一下「動作」。選取「刪除模型」。
刪除模型時,系統會從專案中刪除所有相關聯的模型版本和評估結果。 Google Cloud
在確認提示中,按一下「刪除」。
gcloud
如要使用 Google Cloud CLI 刪除模型,請向 gcloud ai models delete 指令提供模型的顯示名稱和區域。
gcloud ai models delete DEPLOYED_MODEL_NAME \
--project=PROJECT_ID \
--region=LOCATION_ID
將 DEPLOYED_MODEL_NAME 替換為模型的顯示名稱。 將 PROJECT_ID 替換為您的專案名稱。將 LOCATION_ID 替換為您部署模型的區域。
後續步驟
- 進一步瞭解 Gemma 開放模型。
- 詳閱《Gemma 使用條款》。
- 進一步瞭解開放模型。
- 瞭解如何部署微調模型。
- 瞭解如何使用 HuggingFace Textgen Inference (TGI),將 Gemma 2 部署至 Google Kubernetes Engine。
- 如要進一步瞭解
PredictionServiceClient,請參閱下列語言的指南:Python、Node.js、Java 或 Go。