Vertex AI 生成式 AI (也稱為生成式 AI 或生成式 AI) 可讓您存取 Google 的生成式 AI 模型,支援多種模態 (文字、程式碼、圖片、語音)。您可以測試及調整這些大型語言模型 (LLM),然後部署這些模型,在 AI 輔助應用程式中使用。詳情請參閱 Vertex AI 生成式 AI 總覽。
Vertex AI 提供各種生成式 AI 基礎模型,可透過 API 存取,包括本指南中使用的模型。如要進一步瞭解如何選擇模型,請參閱「Google 模型」。
每個模型都是由您Google Cloud 專案特定的發布端點公開,除非您需要依據特定用途調整模型,否則不必部署基礎模型。您可以將提示傳送至發布商端點。提示是傳送至大型語言模型的自然語言要求,用來引導模型提供回覆。
本教學課程將示範工作流程,說明如何使用 Workflows 連接器或 HTTP POST
要求,將文字提示傳送至發布者端點,藉此從 Vertex AI 模型生成回覆。詳情請參閱「Vertex AI API 連接器總覽」和「提出 HTTP 要求」。
請注意,您可以獨立部署及執行每個工作流程。
目標
在本教學課程中,您將完成下列學習內容:
- 啟用 Vertex AI 和 Workflows API,並將 Vertex AI 使用者 (
roles/aiplatform.user
) 角色授予服務帳戶。這個角色可存取大部分的 Vertex AI 功能。如要進一步瞭解如何設定 Vertex AI,請參閱「設定專案和開發環境」。 - 部署及執行工作流程,提示 Vertex AI 模型描述透過 Cloud Storage 公開提供的圖片。詳情請參閱公開資料。
- 部署及執行工作流程,平行處理國家/地區清單,並提示 Vertex AI 模型生成及傳回這些國家/地區的歷史記錄。使用平行分支可同時啟動對 LLM 的呼叫,並等待所有呼叫完成後再合併結果,藉此縮短總執行時間。詳情請參閱「平行執行工作流程步驟」。
- 部署可摘要大型文件的流程。由於脈絡窗口設有上限,會限制模型在訓練期間 (以及預測時) 的回溯範圍,因此工作流程會將文件分割成較小的部分,然後提示 Vertex AI 模型平行摘要每個部分。詳情請參閱「提示策略總覽」和「預測範圍、背景資訊視窗和預測視窗」。
費用
在本文件中,您會使用 Google Cloud的下列計費元件:
如要根據預測用量估算費用,請使用 Pricing Calculator。
完成本文所述工作後,您可以刪除已建立的資源,避免繼續計費。詳情請參閱清除所用資源一節。
事前準備
在嘗試本教學課程中的範例之前,請務必完成下列步驟。
控制台
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Vertex AI > Vertex AI User role to the service account.
To grant the role, find the Select a role list, then select Vertex AI > Vertex AI User.
- Click Continue.
-
Click Done to finish creating the service account.
-
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Workflows APIs.
-
Create a service account:
-
In the Google Cloud console, go to the Create service account page.
Go to Create service account - Select your project.
-
In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.
In the Service account description field, enter a description. For example,
Service account for quickstart
. - Click Create and continue.
-
Grant the Vertex AI > Vertex AI User role to the service account.
To grant the role, find the Select a role list, then select Vertex AI > Vertex AI User.
- Click Continue.
-
Click Done to finish creating the service account.
-
gcloud
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Workflows APIs:
gcloud services enable aiplatform.googleapis.com
workflows.googleapis.com -
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/aiplatform.user
IAM role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/aiplatform.user
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
-
Install the Google Cloud CLI.
-
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create or select a Google Cloud project.
-
Create a Google Cloud project:
gcloud projects create PROJECT_ID
Replace
PROJECT_ID
with a name for the Google Cloud project you are creating. -
Select the Google Cloud project that you created:
gcloud config set project PROJECT_ID
Replace
PROJECT_ID
with your Google Cloud project name.
-
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Workflows APIs:
gcloud services enable aiplatform.googleapis.com
workflows.googleapis.com -
Set up authentication:
-
Create the service account:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
Replace
SERVICE_ACCOUNT_NAME
with a name for the service account. -
Grant the
roles/aiplatform.user
IAM role to the service account:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/aiplatform.user
Replace the following:
SERVICE_ACCOUNT_NAME
: the name of the service accountPROJECT_ID
: the project ID where you created the service account
-
部署描述圖片的工作流程
部署使用連接器方法 (generateContent
) 的工作流程,向模型發布者端點發出要求。這項方法支援使用多模態輸入內容生成內容。
工作流程會提供文字提示,以及 Cloud Storage bucket 中可公開存取的圖片 URI。您可以查看圖片,並在 Google Cloud 控制台中查看物件詳細資料。
工作流程會從模型生成的結果中,傳回圖片說明。
如要進一步瞭解提示 LLM 時使用的 HTTP 要求主體參數,以及回應主體元素,請參閱 Gemini API 參考資料。
控制台
前往 Google Cloud 控制台的「Workflows」頁面。
按一下「建立」
。輸入新工作流程的名稱:
describe-image
。在「Region」(區域) 清單中,選取「us-central1 (Iowa)」(us-central1 (愛荷華州))。
在「Service account」(服務帳戶) 部分,選取您先前建立的服務帳戶。
點選「下一步」。
在工作流程編輯器中,輸入下列工作流程定義:
按一下 [Deploy] (部署)。
gcloud
為工作流程建立原始碼檔案:
touch describe-image.yaml
在文字編輯器中,將下列工作流程複製到原始碼檔案:
輸入下列指令來部署工作流程:
gcloud workflows deploy describe-image \ --source=describe-image.yaml \ --location=us-central1 \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
執行工作流程
執行工作流程時,系統會執行與該工作流程相關聯的目前工作流程定義。
控制台
前往 Google Cloud 控制台的「Workflows」頁面。
在「Workflows」頁面中,選取「describe-image」工作流程,前往詳細資料頁面。
在「工作流程詳細資料」頁面中,按一下 play_arrow「執行」。
在「Input」(輸入) 中輸入以下內容:
{"image_url":"gs://generativeai-downloads/images/scones.jpg"}
再次點按「執行」。
在「輸出」窗格中查看工作流程結果。
畫面會顯示如下的輸出內容:
{ "image_description": "There are three pink peony flowers on the right side of the picture[]...]There is a white napkin on the table.", "image_url": "gs://generativeai-downloads/images/scones.jpg" }
gcloud
開啟終端機。
執行工作流程:
gcloud workflows run describe-image \ --data='{"image_url":"gs://generativeai-downloads/images/scones.jpg"}'
執行結果應與下列內容類似:
Waiting for execution [258b530e-a093-46d7-a4ff-cbf5392273c0] to complete...done. argument: '{"image_url":"gs://generativeai-downloads/images/scones.jpg"}' createTime: '2024-02-09T13:59:32.166409938Z' duration: 4.174708484s endTime: '2024-02-09T13:59:36.341118422Z' name: projects/1051295516635/locations/us-central1/workflows/describe-image/executions/258b530e-a093-46d7-a4ff-cbf5392273c0 result: "{\"image_description\":\"The picture shows a rustic table with a white surface,\ \ on which there are several scones with blueberries, as well as two cups of coffee\ [...] \ on the table. The background of the table is a dark blue color.\",\"image_url\"\ :\"gs://generativeai-downloads/images/scones.jpg\"}" startTime: '2024-02-09T13:59:32.166409938Z' state: SUCCEEDED
部署可產生國家/地區記錄的工作流程
部署工作流程,以平行方式疊代國家/地區的輸入清單,並使用連接器方法 (generateContent
) 向模型發布者端點提出要求。這項方法支援使用多模態輸入內容生成內容。
工作流程會傳回模型產生的國家/地區記錄,並將這些記錄合併到 map 中。
如要進一步瞭解提示 LLM 時使用的 HTTP 要求主體參數,以及回應主體元素,請參閱 Gemini API 參考資料。
控制台
前往 Google Cloud 控制台的「Workflows」頁面。
按一下「建立」
。輸入新工作流程的名稱:
gemini-pro-country-histories
。在「Region」(區域) 清單中,選取「us-central1 (Iowa)」(us-central1 (愛荷華州))。
在「Service account」(服務帳戶) 部分,選取您先前建立的服務帳戶。
點選「下一步」。
在工作流程編輯器中,輸入下列工作流程定義:
按一下 [Deploy] (部署)。
gcloud
為工作流程建立原始碼檔案:
touch gemini-pro-country-histories.yaml
在文字編輯器中,將下列工作流程複製到原始碼檔案:
輸入下列指令來部署工作流程:
gcloud workflows deploy gemini-pro-country-histories \ --source=gemini-pro-country-histories.yaml \ --location=us-central1 \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
執行工作流程
執行工作流程時,系統會執行與該工作流程相關聯的目前工作流程定義。
控制台
前往 Google Cloud 控制台的「Workflows」頁面。
在「Workflows」頁面中,選取「gemini-pro-country-histories」工作流程,前往詳細資料頁面。
在「工作流程詳細資料」頁面中,按一下 play_arrow「執行」。
在「Input」(輸入) 中輸入以下內容:
{"countries":["Argentina", "Bhutan", "Cyprus", "Denmark", "Ethiopia"]}
再次點按「執行」。
在「輸出」窗格中查看工作流程結果。
畫面會顯示如下的輸出內容:
{ "Argentina": "The history of Argentina is a complex and fascinating one, marked by periods of prosperity and decline, political [...] "Bhutan": "The history of Bhutan is a rich and fascinating one, dating back to the 7th century AD. Here is a brief overview: [...] "Cyprus": "The history of Cyprus is a long and complex one, spanning over 10,000 years. The island has been ruled by a succession [...] "Denmark": "1. **Prehistory and Early History (c. 12,000 BC - 800 AD)**\\n - The earliest evidence of human habitation in Denmark [...] "Ethiopia": "The history of Ethiopia is a long and complex one, stretching back to the earliest human civilizations. The country is [...] }
gcloud
開啟終端機。
執行工作流程:
gcloud workflows run gemini-pro-country-histories \ --data='{"countries":["Argentina", "Bhutan", "Cyprus", "Denmark", "Ethiopia"]}' \ --location=us-central1
執行結果應與下列內容類似:
Waiting for execution [7ae1ccf1-29b7-4c2c-99ec-7a12ae289391] to complete...done. argument: '{"countries":["Argentina","Bhutan","Cyprus","Denmark","Ethiopia"]}' createTime: '2024-02-09T16:25:16.742349156Z' duration: 12.075968673s endTime: '2024-02-09T16:25:28.818317829Z' name: projects/1051295516635/locations/us-central1/workflows/gemini-pro-country-histories/executions/7ae1ccf1-29b7-4c2c-99ec-7a12ae289391 result: "{\"Argentina\":\"The history of Argentina can be traced back to the arrival\ [...] n* 2015: Argentina elects Mauricio Macri as president.\",\"Bhutan\":\"The history\ [...] \ natural beauty, ancient monasteries, and friendly people.\",\"Cyprus\":\"The history\ [...] ,\"Denmark\":\"The history of Denmark can be traced back to the Stone Age, with\ [...] \ a high standard of living.\",\"Ethiopia\":\"The history of Ethiopia is long and\ [...] startTime: '2024-02-09T16:25:16.742349156Z' state: SUCCEEDED
部署可摘要大型文件的工作流程
部署工作流程,將大型文件分割成較小的部分,並http.post
平行向模型發布者端點提出要求,讓模型同時摘要各部分內容。工作流程最後會將所有部分摘要合併為完整摘要。
如要進一步瞭解提示 LLM 時使用的 HTTP 要求主體參數,以及回應主體元素,請參閱 Gemini API 參考資料。
工作流程定義會假設您已建立 Cloud Storage 值區,並可將文字檔案上傳至該值區。如要進一步瞭解用於從 Cloud Storage 值區擷取物件的 Workflows 連接器 (googleapis.storage.v1.objects.get
),請參閱連接器參考資料。
部署工作流程後,您可以建立適當的 Eventarc 觸發條件,然後將檔案上傳至 bucket,即可執行工作流程。詳情請參閱「將 Cloud Storage 事件轉送至 Workflows」。請注意,您必須啟用其他 API,並授予其他角色,包括將 Storage 物件使用者 (roles/storage.objectUser
) 角色授予服務帳戶,以支援使用 Cloud Storage 物件。詳情請參閱「準備建立觸發條件」一節。
控制台
前往 Google Cloud 控制台的「Workflows」頁面。
按一下「建立」
。輸入新工作流程的名稱:
gemini-pro-summaries
。在「Region」(區域) 清單中,選取「us-central1 (Iowa)」(us-central1 (愛荷華州))。
在「Service account」(服務帳戶) 部分,選取您先前建立的服務帳戶。
點選「下一步」。
在工作流程編輯器中,輸入下列工作流程定義:
按一下 [Deploy] (部署)。
gcloud
為工作流程建立原始碼檔案:
touch gemini-pro-summaries.yaml
在文字編輯器中,將下列工作流程複製到原始碼檔案:
輸入下列指令來部署工作流程:
gcloud workflows deploy gemini-pro-summaries \ --source=gemini-pro-summaries.yaml \ --location=us-central1 \ --service-account=SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com
清除所用資源
如要避免系統向您的 Google Cloud 帳戶收取本教學課程中所用資源的相關費用,請刪除含有該項資源的專案,或者保留專案但刪除個別資源。
刪除專案
控制台
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.
gcloud
Delete a Google Cloud project:
gcloud projects delete PROJECT_ID
刪除個別資源
後續步驟
- 進一步瞭解 Workflows 連接器。
- 進一步瞭解 Vertex AI
generateContent
方法。