如需使用 Gemini on Gemini Enterprise Agent Platform,请使用Google Cloud API 密钥或应用默认凭证进行身份验证。我们建议在测试时使用 API 密钥,在生产环境中使用应用默认凭证。
本页面介绍了如何获取 Google Cloud API 密钥,具体取决于您是新用户还是现有 Google Cloud 用户。
选择您的用户类型:
创建 Google Cloud API 密钥
如果您已经是启用结算功能的标准 Google Cloud 用户,请按照以下说明获取标准 Google Cloud API 密钥。或者,您也可以使用应用默认凭证,而不是使用 API 密钥。
准备工作
选择项目,启用结算功能,启用 Agent Platform API
-
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.
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.
启用服务账号 API 密钥创建
- 依次打开 IAM 和管理 > 组织政策。
- 在政策列表中,过滤出称为 iam.managed.disableServiceAccountApiKeyCreation 的政策。
- 依次点击操作 > 修改政策。
- 在政策来源下,选择覆盖父资源的政策,然后点击添加规则。
- 在强制执行下方,选择关闭。
- 点击完成。
- 点击设置政策。在弹出的对话框中,再次点击设置政策。
创建新服务账号
- 打开 IAM 和管理 > 服务账号。
- 点击创建服务账号。
- 按如下方式配置服务账号:
- 服务账号名称:
vertex-ai-runner - 服务账号 ID:
vertexairunner
- 服务账号名称:
- 点击创建并继续。
- 在权限下,点击选择角色,然后从菜单中选择 Gemini Enterprise Agent Platform Express User(Beta 版)。
- 点击继续。
- 点击完成。
创建 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 GOOGLE_API_KEY=YOUR_API_KEY
-
保存文件,然后运行以下命令以应用更改:
source ~/.bashrc
-
-
如果上一步的输出为
/bin/zsh:-
打开
.zshrc:touch ~/.zshrc open ~/.zshrc -
将以下行添加到
.zshrc中:export GOOGLE_API_KEY=YOUR_API_KEY
-
保存文件,然后运行以下命令以应用更改:
source ~/.zshrc
-
-
Windows
- 在系统设置中搜索“环境变量”
- 修改“用户变量”(针对当前用户)或“系统变量”(针对所有用户 - 请谨慎使用)。
- 创建变量并添加
export GOOGLE_API_KEY=YOUR_API_KEY - 应用更改