在本教學課程中,您將使用 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 (適用於您的語言) 和通訊協定緩衝區程式庫。程式碼範例會使用通訊協定緩衝區程式庫中的函式,將輸入字典轉換為 API 預期的 JSON 格式。
在本機電腦上,按一下下列其中一個分頁標籤,安裝所需程式設計語言的 SDK。
Python
在本機電腦上,按一下下列其中一個分頁標籤,安裝所需程式設計語言的 SDK。
執行下列指令,安裝及更新 Agent Platform SDK for Python。
pip3 install --upgrade "google-cloud-aiplatform>=1.64"
執行下列指令,安裝 Python 的通訊協定緩衝區程式庫。
pip3 install --upgrade "protobuf>=5.28"
Node.js
執行下列指令,安裝或更新 Node.js 適用的 aiplatform SDK。
npm install @google-cloud/aiplatform
Java
如要將 google-cloud-aiplatform 新增為依附元件,請依據您的環境新增適當的程式碼。
搭配使用 Maven 與 BOM
在 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」(部署模型) 窗格中,指定下列詳細資料。
在「Deployment environment」部分,點選「Agent Platform」。
在「Deploy model」(部署模型) 部分:
在「Resource ID」(資源 ID) 部分,選擇
gemma-2b-it。「Model name」(模型名稱) 和「Endpoint name」(端點名稱) 接受預設值。例如:
- 模型名稱:
gemma2-2b-it-1234567891234 - 端點名稱:
gemma2-2b-it-mg-one-click-deploy
請記下端點名稱,您需要使用這個 ID,才能找到程式碼範例中使用的端點 ID。
- 模型名稱:
在「Deployment settings」(部署作業設定) 部分:
接受「基本」設定的預設選項。
在「Region」(區域) 部分,接受預設值或從清單中選擇區域。請記下區域。程式碼範例會用到。
在「Machine spec」(機器規格) 部分,選擇 TPU 支援的執行個體:
ct5lp-hightpu-1t (1 TPU_V5_LITEPOD; ct5lp-hightpu-1t)。
點按「Deploy」。部署完成後,您會收到一封電子郵件,內含新端點的詳細資料。您也可以依序點選「Online prediction」(線上預測) >「Endpoints」(端點),然後選取區域,查看端點詳細資料。
使用 PredictionServiceClient 推論 Gemma 2 2B
部署 Gemma 2 2B 後,您可以使用 PredictionServiceClient,針對「Why is the sky blue?」(為什麼天空是藍色的?) 提示詞取得線上預測。
程式碼參數
PredictionServiceClient 程式碼範例需要您更新下列項目。
PROJECT_ID:如要找出專案 ID,請按照下列步驟操作。前往 Google Cloud 控制台的「Welcome」(歡迎) 頁面。
從頁面頂端的專案挑選器選取專案。
專案名稱、專案編號和專案 ID 會顯示在「Welcome」(歡迎) 標題下方。
ENDPOINT_REGION:這是您部署端點的區域。ENDPOINT_ID:如要查看端點 ID,請在控制台中查看,或執行gcloud ai endpoints list指令。您需要「Deploy model」(部署模型) 窗格中的端點名稱和區域 。控制台
如要查看端點詳細資料,請依序點選「Online prediction」(線上預測) >「Endpoints」(端點),然後選取區域。請記下「
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 控制台中,依序點選「Online prediction」(線上預測) 和「Endpoints」(端點)。
在「Region」(區域) 下拉式選單中,選擇您部署端點的區域。
按一下端點名稱,開啟詳細資料頁面。例如
gemma2-2b-it-mg-one-click-deploy。在
Gemma 2 (Version 1)模型所在的資料列中,依序點選 「Actions」(動作) 和「Undeploy model from endpoint」(從端點取消部署模型)。在「Undeploy model from endpoint」(取消部署端點的模型) 對話方塊中,按一下「Undeploy」(取消部署)。
點選「返回」按鈕,回到「端點」頁面。
按一下
gemma2-2b-it-mg-one-click-deploy列尾端的 「Actions」(動作),然後選取「Delete endpoint」(刪除端點)。在確認提示中,按一下「確認」。
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列結尾的 「Actions」(動作)。選取「刪除模型」。
刪除模型後,所有相關聯的模型版本和評估結果都會從 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。