Vertex AI の生成 AI(別名: genAI)で Google の生成 AI モデルにアクセスすることが可能で、これを複数のモダリティ(テキスト、コード、画像、スピーチ)向けに活用することができます。これらの大規模言語モデル(LLM)をテストして調整し、AI を活用したアプリケーションで使用するためにデプロイできます。詳細については、Vertex AI での生成 AI の概要をご覧ください。
Vertex AI には、このガイドで使用されているモデルを含む、API を通じてアクセス可能なさまざまな生成 AI 基盤モデルがあります。モデルを選択する方法については、Google モデルをご覧ください。
各モデルは、Google Cloud プロジェクト専用のパブリッシャー エンドポイントを通じて公開されます。そのため、特定のユースケース向けにチューニングする場合を除いて、基盤モデルをデプロイする必要はありません。パブリッシャー エンドポイントにプロンプトを送信できます。プロンプトとは、レスポンスを引き出すために LLM に送信される自然言語によるリクエストのことです。
このチュートリアルでは、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コンポーネントを使用します。
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
このドキュメントに記載されているタスクの完了後、作成したリソースを削除すると、それ以上の請求は発生しません。詳細については、クリーンアップをご覧ください。
始める前に
このチュートリアルの例を試す前に、次のことが完了していることを確認してください。
コンソール
- 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.
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Workflows APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator
). Learn how to grant roles. -
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.
-
Ensure that you have the Create Service Accounts IAM role
(
-
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Workflows APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator
). Learn how to grant roles. -
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.
-
Ensure that you have the Create Service Accounts IAM role
(
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.
-
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Workflows APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator
). Learn how to grant roles. -
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 roles/aiplatform.user role to the service account.
To grant the role, find the Select a role list, then select roles/aiplatform.user.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
-
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Vertex AI and Workflows APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator
). Learn how to grant roles. -
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 roles/aiplatform.user role to the service account.
To grant the role, find the Select a role list, then select roles/aiplatform.user.
- Click Continue.
-
Click Done to finish creating the service account.
-
Ensure that you have the Create Service Accounts IAM role
(
画像を説明するワークフローをデプロイする
コネクタ メソッド(generateContent
)を使用してモデル パブリッシャー エンドポイントにリクエストを送信するワークフローをデプロイします。このメソッドは、マルチモーダル入力によるコンテンツ生成をサポートします。
このワークフローは、テキスト プロンプトと、Cloud Storage バケットで一般公開されている画像の URI を提供します。画像を表示したり、 Google Cloud コンソールでオブジェクトの詳細を確認したりすることができます。
ワークフローは、モデルで生成されたレスポンスから画像の説明を返します。
LLM のプロンプトで使用される HTTP リクエストの本文パラメータと、レスポンスの本文要素の詳細については、Gemini API リファレンスをご覧ください。
コンソール
Google Cloud コンソールで [ワークフロー] ページに移動します。
[
作成] をクリックします。新しいフィールドの名前を入力します:
describe-image
。[リージョン] リストで [us-central1 (Iowa)] を選択します。
[サービス アカウント] で、先ほど作成したサービス アカウントを選択します。
[次へ] をクリックします。
ワークフロー エディタで、次のワークフローの定義を入力します。
[デプロイ] をクリックします。
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 コンソールで [ワークフロー] ページに移動します。
[ワークフロー] ページで、[describe-image] ワークフローを選択して詳細ページに移動します。
[ワークフローの詳細] ページで [play_arrow 実行] を選択します。
[Input] に、次のように入力します。
{"image_url":"gs://generativeai-downloads/images/scones.jpg"}
もう一度 [Execute] をクリックします。
ワークフローの結果が [出力] ペインに表示されます。
出力例を以下に示します。
{ "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
)を使用してモデル パブリッシャー エンドポイントに対してリクエストを行います。このメソッドは、マルチモーダル入力によるコンテンツ生成をサポートします。
ワークフローが、モデルによって生成された国の歴史を返し、マップにまとめます。
LLM のプロンプトで使用される HTTP リクエストの本文パラメータと、レスポンスの本文要素の詳細については、Gemini API リファレンスをご覧ください。
コンソール
Google Cloud コンソールで [ワークフロー] ページに移動します。
[
作成] をクリックします。新しいフィールドの名前を入力します:
gemini-pro-country-histories
。[リージョン] リストで [us-central1 (Iowa)] を選択します。
[サービス アカウント] で、先ほど作成したサービス アカウントを選択します。
[次へ] をクリックします。
ワークフロー エディタで、次のワークフローの定義を入力します。
[デプロイ] をクリックします。
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 コンソールで [ワークフロー] ページに移動します。
[ワークフロー] ページで、[gemini-pro-country-histories] ワークフローを選択して詳細ページに移動します。
[ワークフローの詳細] ページで [play_arrow 実行] を選択します。
[Input] に、次のように入力します。
{"countries":["Argentina", "Bhutan", "Cyprus", "Denmark", "Ethiopia"]}
もう一度 [Execute] をクリックします。
ワークフローの結果が [出力] ペインに表示されます。
出力例を以下に示します。
{ "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
リクエストを並列で実行して、モデルが各部分を同時に要約できるようにします。ワークフローが最終的に、部分的なサマリーをすべて 1 つのサマリーにまとめます。
LLM のプロンプトで使用される HTTP リクエストの本文パラメータと、レスポンスの本文要素の詳細については、Gemini API リファレンスをご覧ください。
このワークフロー定義は、テキスト ファイルをアップロードできる Cloud Storage バケットを作成していることを前提としています。Cloud Storage バケットからオブジェクトを取得するために使用される Workflows コネクタ(googleapis.storage.v1.objects.get
)の詳細については、コネクタ リファレンスをご覧ください。
ワークフローをデプロイしたら、適切な Eventarc トリガーを作成し、ファイルをバケットにアップロードして実行できます。詳細については、Cloud Storage イベントを Workflows に転送するをご覧ください。追加の API を有効にする必要があります。また、サービス アカウントに Cloud Storage オブジェクトの使用をサポートするストレージ オブジェクト ユーザー(roles/storage.objectUser
)のロールを付与するなど、追加のロールを付与する必要があります。詳細については、トリガーの作成を準備するセクションをご覧ください。
コンソール
Google Cloud コンソールで [ワークフロー] ページに移動します。
[
作成] をクリックします。新しいフィールドの名前を入力します:
gemini-pro-summaries
。[リージョン] リストで [us-central1 (Iowa)] を選択します。
[サービス アカウント] で、先ほど作成したサービス アカウントを選択します。
[次へ] をクリックします。
ワークフロー エディタで、次のワークフローの定義を入力します。
[デプロイ] をクリックします。
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
リソースを個別に削除する
このチュートリアルで作成したワークフローを削除します。
次のステップ
- Workflows コネクタの詳細を確認する。
- Vertex AI
generateContent
メソッドについて学習する。