Vertex AI で Gemini を使用するには、Google Cloud API キーまたはアプリケーションのデフォルト認証情報を使用して認証します。テストには API キーを使用し、本番環境にはアプリケーションのデフォルト認証情報を使用することをおすすめします。
このページでは、 Google Cloud の新規ユーザーか既存ユーザーかに応じて Google Cloud API キーを取得する方法について説明します。
ユーザータイプを選択します。
Google Cloud API キーを作成する
請求が有効になっている標準の Google Cloud ユーザーである場合は、次の手順で標準の Google Cloud API キーを取得してください。API キーの代わりに、アプリケーションのデフォルト認証情報を使用することもできます。
始める前に
プロジェクトを選択して課金を有効にし、Vertex AI API を有効にする
-
Sign in to your Google Account.
If you don't already have one, sign up for a new account.
-
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 API.
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. -
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 API.
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.
サービス アカウントの API キーの作成を有効にする
- [IAM と管理] > [組織のポリシー] を開きます。
- ポリシーのリストで、iam.managed.disableServiceAccountApiKeyCreation というポリシーをフィルタします。
- [操作] > [ポリシーを編集] をクリックします。
- [ポリシーのソース] で、[親のポリシーをオーバーライドする] を選択し、[ルールを追加] をクリックします。
- [適用] で [オフ] を選択します。
- [完了] をクリックします。
- [ポリシーを設定] をクリックします。表示されたダイアログで、もう一度 [ポリシーを設定] をクリックします。
サービス アカウントを新規作成
- [IAM と管理] > [サービス アカウント] を開きます。
- [サービス アカウントを作成] をクリックします。
- サービス アカウントを次のように構成します。
- サービス アカウント名:
vertex-ai-runner
- サービス アカウント ID:
vertexairunner
- サービス アカウント名:
- [作成して続行] をクリックします。
- [権限] で、[ロールを選択] をクリックし、メニューから [Vertex AI Platform Express ユーザー] を選択します。
- [続行] をクリックします。
- [完了] をクリックします。
API キーを作成する
- [API とサービス] > [認証情報] を開きます。
- [認証情報を作成] > [API キー] をクリックします。
- API キーを次のように構成します。
- 名前:
vertexaiapikey
- サービス アカウントを介して API 呼び出しを認証する: 選択します。
- 名前:
- [サービス アカウントを選択] をクリックします。
- 前の手順で作成したサービス アカウントを選択し、[選択] をクリックします。
- [作成] をクリックします。
最初のリクエストを送信する
API キーを取得したら、API クイックスタートで API キーを使用して最初のリクエストを行う方法を確認してください。
省略可: API キーをローカルで設定する
初期テストでは API キーをハードコードできますが、これは安全ではなく、一時的なものにすぎません。このセクションの残りの部分では、さまざまなオペレーティング システムで API キーをローカルで環境変数として設定する方法について説明します。
クリックして手順を開く
Linux / macOS
-
次のコマンドを実行して、使用しているコマンドライン シェルを確認します。
echo $SHELL
出力は次のようになります。
/bin/bash
-
次のいずれかの方法で、API キーのシェル エクスポート変数を追加します。
-
前の手順の出力が
/bin/bash
の場合:-
.bashrc
を開きます。touch ~/.bashrc open ~/.bashrc
-
.bashrc
に次の行を追加します。export GEMINI_API_KEY=YOUR_API_KEY
-
ファイルを保存し、次のコマンドを実行して変更を適用します。
source ~/.bashrc
-
-
前の手順の出力が
/bin/zsh
の場合:-
.zshrc
を開きます。touch ~/.zshrc open ~/.zshrc
-
.zshrc
に次の行を追加します。export GEMINI_API_KEY= YOUR_API_KEY
-
ファイルを保存し、次のコマンドを実行して変更を適用します。
source ~/.zshrc
-
-
Windows
- システム設定で「環境変数」を検索します。
- [ユーザー変数](現在のユーザーの場合)または [システム変数](すべてのユーザーの場合 - 注意して使用)のいずれかを編集します。
- 変数を作成して
export GEMINI_API_KEY=YOUR_API_KEY
を追加する - 変更を適用する