借助 Vertex AI 上的生成式 AI(也称为 genAI 或 gen AI),您可以访问 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 User (
roles/aiplatform.user) 角色。此角色允许访问大多数 Vertex AI 功能。如需详细了解如何设置 Vertex AI,请参阅设置项目和开发环境。 - 部署并运行一个工作流,该工作流会提示 Vertex AI 模型描述通过 Cloud Storage 公开提供的图片。如需了解详情,请参阅公开数据。
- 部署并运行一个工作流,该工作流会并行遍历国家/地区列表,并提示 Vertex AI 模型生成并返回这些国家/地区的历史记录。使用并行分支可同时开始对 LLM 的调用,并在所有调用完成之后再合并结果,从而缩短总执行时间。如需了解详情,请参阅并行执行工作流步骤。
- 部署可总结大型文档的工作流。由于上下文窗口(用于设置模型在训练期间的回溯时间,也用于预测)存在限制,因此工作流会将文档划分为较小的部分,然后提示 Vertex AI 模型并行总结每个部分。如需了解详情,请参阅提示策略概览和预测范围、上下文窗口和预测窗口。
费用
在本文档中,您将使用 Google Cloud的以下收费组件:
如需根据您的预计使用情况来估算费用,请使用价格计算器。
完成本文档中描述的任务后,您可以通过删除所创建的资源来避免继续计费。如需了解详情,请参阅清理。
准备工作
在试用本教程中的示例之前,请确保您已完成以下操作。
控制台
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
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 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.enablepermission. Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). 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 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 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.enablepermission. Learn how to grant roles.-
Create a service account:
-
Ensure that you have the Create Service Accounts IAM role
(
roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). 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
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
选择或创建项目所需的角色
- 选择项目:选择项目不需要特定的 IAM 角色,您可以选择已获授角色的任何项目。
-
创建项目:如需创建项目,您需要拥有 Project Creator 角色 (
roles/resourcemanager.projectCreator),该角色包含resourcemanager.projects.create权限。了解如何授予角色。
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID替换为您要创建的 Google Cloud 项目的名称。 -
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID替换为您的 Google Cloud 项目名称。
启用 Vertex AI 和 Workflows API:
启用 API 所需的角色
如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (
roles/serviceusage.serviceUsageAdmin),该角色包含serviceusage.services.enable权限。了解如何授予角色。gcloud services enable aiplatform.googleapis.com
workflows.googleapis.com -
设置身份验证:
-
确保您拥有 Create Service Accounts IAM 角色 (
roles/iam.serviceAccountCreator) 和 Project IAM Admin 角色 (roles/resourcemanager.projectIamAdmin)。了解如何授予角色。 -
创建服务账号:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
将
SERVICE_ACCOUNT_NAME替换为服务账号的名称。 -
向服务账号授予
roles/aiplatform.userIAM 角色:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/aiplatform.user
请替换以下内容:
SERVICE_ACCOUNT_NAME:服务账号的名称PROJECT_ID:您在其中创建服务账号的项目的 ID
-
确保您拥有 Create Service Accounts IAM 角色 (
-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
选择或创建项目所需的角色
- 选择项目:选择项目不需要特定的 IAM 角色,您可以选择已获授角色的任何项目。
-
创建项目:如需创建项目,您需要拥有 Project Creator 角色 (
roles/resourcemanager.projectCreator),该角色包含resourcemanager.projects.create权限。了解如何授予角色。
-
创建 Google Cloud 项目:
gcloud projects create PROJECT_ID
将
PROJECT_ID替换为您要创建的 Google Cloud 项目的名称。 -
选择您创建的 Google Cloud 项目:
gcloud config set project PROJECT_ID
将
PROJECT_ID替换为您的 Google Cloud 项目名称。
启用 Vertex AI 和 Workflows API:
启用 API 所需的角色
如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (
roles/serviceusage.serviceUsageAdmin),该角色包含serviceusage.services.enable权限。了解如何授予角色。gcloud services enable aiplatform.googleapis.com
workflows.googleapis.com -
设置身份验证:
-
确保您拥有 Create Service Accounts IAM 角色 (
roles/iam.serviceAccountCreator) 和 Project IAM Admin 角色 (roles/resourcemanager.projectIamAdmin)。了解如何授予角色。 -
创建服务账号:
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME
将
SERVICE_ACCOUNT_NAME替换为服务账号的名称。 -
向服务账号授予
roles/aiplatform.userIAM 角色:gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=roles/aiplatform.user
请替换以下内容:
SERVICE_ACCOUNT_NAME:服务账号的名称PROJECT_ID:您在其中创建服务账号的项目的 ID
-
确保您拥有 Create Service Accounts IAM 角色 (
部署用于描述图片的工作流
部署使用连接器方法 (generateContent) 向模型发布方端点发出请求的工作流。该方法支持使用多模态输入生成内容。
工作流提供文本提示和 Cloud Storage 存储桶中公开提供的图片的 URI。您可以查看图片,并且在 Google Cloud 控制台中,您可以查看对象详情。
工作流会返回模型生成的响应中的图片说明。
如需详细了解在提示 LLM 时使用的 HTTP 请求正文参数和响应正文元素,请参阅使用 Vertex AI 中的 Gemini API 生成内容。
控制台
在 Google Cloud 控制台中,前往 Workflows 页面。
点击 创建。
输入新工作流的名称:
describe-image。在区域列表中,选择 us-central1(爱荷华)。
对于服务账号,选择您之前创建的服务账号。
点击下一步。
在工作流编辑器中,输入工作流的定义:
点击部署。
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 页面。
在工作流页面上,选择 describe-image 工作流以转到其详情页面。
在工作流详情页面上,点击 play_arrow 执行。
对于输入,输入以下内容:
{"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 请求正文参数和响应正文元素,请参阅使用 Vertex AI 中的 Gemini API 生成内容。
控制台
在 Google Cloud 控制台中,前往 Workflows 页面。
点击 创建。
输入新工作流的名称:
gemini-pro-country-histories。在区域列表中,选择 us-central1(爱荷华)。
对于服务账号,选择您之前创建的服务账号。
点击下一步。
在工作流编辑器中,输入工作流的定义:
点击部署。
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 页面。
在工作流页面上,选择 gemini-pro-country-histories 工作流以转到其详情页面。
在工作流详情页面上,点击 play_arrow 执行。
对于输入,输入以下内容:
{"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 请求正文参数和响应正文元素,请参阅使用 Vertex AI 中的 Gemini API 生成内容。
工作流定义假定您已创建一个 Cloud Storage 存储桶,您可以将文本文件上传到该存储桶。如需详细了解用于从 Cloud Storage 存储桶检索对象的 Workflows 连接器 (googleapis.storage.v1.objects.get),请参阅连接器参考。
部署工作流后,您可以通过创建相应的 Eventarc 触发器,然后将文件上传到存储桶来执行该工作流。如需了解详情,请参阅将 Cloud Storage 事件路由到 Workflows。
请注意,您必须启用其他 API 并授予其他角色,包括向您的服务账号授予支持使用 Cloud Storage 对象的 Storage Object User (roles/storage.objectUser) 角色。如需了解详情,请参阅准备创建触发器部分。
控制台
在 Google Cloud 控制台中,前往 Workflows 页面。
点击 创建。
输入新工作流的名称:
gemini-pro-summaries。在区域列表中,选择 us-central1(爱荷华)。
对于服务账号,选择您之前创建的服务账号。
点击下一步。
在工作流编辑器中,输入工作流的定义:
点击部署。
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 账号产生费用,请删除包含这些资源的项目,或者保留项目但删除各个资源。
删除项目
控制台
- 在 Google Cloud 控制台中,前往管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。
gcloud
删除 Google Cloud 项目:
gcloud projects delete PROJECT_ID
删除各个资源
删除您在本教程中创建的工作流。
后续步骤
- 详细了解 Workflows 连接器。
- 详细了解如何使用 Vertex AI 中的 Gemini API 生成内容。