如需使用 Vertex AI 上的 Gemini,您需要使用 Google Cloud API 密钥或应用默认凭证进行身份验证。我们建议使用 API 密钥进行测试,并使用应用默认凭证进行生产。本页面介绍了如何获取 Google Cloud API 密钥,具体取决于您是新用户还是现有 Google Cloud 用户。
选择您是否拥有包含现有 Google Cloud 项目的 Google 账号:
创建 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 User。
- 点击继续。
- 点击完成。
创建 API 密钥
- 依次打开 API 和服务 > 凭证。
- 点击创建凭据 > API 密钥。
- 按如下方式配置 API 密钥:
- 名称:
vertexaiapikey
- 通过服务账号对 API 调用进行身份验证:已选择。
- 名称:
- 点击选择服务账号。
- 选择您在上一步中创建的服务账号,然后点击选择。
- 点击创建。
发出您的第一个 API 请求
获取 API 密钥后,请参阅 API 快速入门,了解如何使用 API 密钥发出您的第一个请求。
可选:在本地设置 API 密钥
对于初始测试,您可以对 API 密钥进行硬编码,但这只应是临时做法,因为这种做法并不安全。本部分的其余内容将介绍如何在不同操作系统中将 API 密钥在本地设置为环境变量。
点击即可展开说明
Linux/macOS
-
运行以下命令,查看您正在使用的命令行 shell:
echo $SHELL
输出类似于以下内容:
/bin/bash
-
通过执行以下操作之一,为您的 API 密钥添加 shell 导出变量:
-
如果上一步的输出为
/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
- 应用更改