使用 AI.GENERATE 函数生成文本
本教程介绍了如何使用 AI.GENERATE 函数根据文本或多模态数据生成文本。借助 AI.GENERATE 函数,您可以使用 Cloud 资源连接来连接到托管的 Gemini 模型,这样您就不必创建和维护自己的模型。
本教程介绍如何完成以下任务:
- 总结文本内容,并以函数的默认格式输出结果。
- 总结文本内容并输出结构化结果。
- 转写和翻译视频内容。
- 分析音频文件内容。
费用
在本文档中,您将使用 Google Cloud的以下收费组件:
- BigQuery ML: You incur costs for the data that you process in BigQuery.
- Gemini Enterprise Agent Platform: You incur costs for calls to the Agent Platform model.
如需根据您的预计使用情况来估算费用,请使用价格计算器。
如需详细了解 BigQuery 价格,请参阅 BigQuery 文档中的 BigQuery 价格。
如需详细了解 Agent Platform 生成式 AI 的价格,请参阅 Agent Platform 价格页面。
准备工作
- 登录您的 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.
-
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.
- 新项目会自动启用 BigQuery。如需在现有项目中启用 BigQuery,请前往
启用 BigQuery API。
启用 API 所需的角色
如需启用 API,您需要拥有 Service Usage Admin IAM 角色 (
roles/serviceusage.serviceUsageAdmin),该角色包含serviceusage.services.enable权限。了解如何授予角色。
所需的角色
如需使用 AI.GENERATE 函数,您需要拥有以下 Identity and Access Management (IAM) 角色:
- 创建和使用 BigQuery 数据集和表:项目的 BigQuery Data Editor (
roles/bigquery.dataEditor)。 - 创建、委托和使用 BigQuery 连接:项目的 BigQuery Connections Admin (
roles/bigquery.connectionsAdmin)。 - 为连接的服务账号授予权限:包含 Gemini Enterprise Agent Platform 端点的项目的 Project IAM Admin (
roles/resourcemanager.projectIamAdmin)。 - 创建 BigQuery 作业:项目的 BigQuery Job User (
roles/bigquery.jobUser)。
这些预定义角色包含执行本文档中的任务所需的权限。如需查看所需的确切权限,请展开所需权限部分:
所需权限
- 创建数据集:
bigquery.datasets.create - 创建、委托和使用连接:
bigquery.connections.* - 设置服务账号权限:
resourcemanager.projects.getIamPolicy和resourcemanager.projects.setIamPolicy - 查询表数据:
bigquery.tables.getData
创建数据集
创建 BigQuery 数据集以存储机器学习模型。
控制台
在 Google Cloud 控制台中,前往 BigQuery 页面。
在探索器窗格中,点击您的项目名称。
点击 查看操作 > 创建数据集
在 创建数据集 页面上,执行以下操作:
在数据集 ID 部分,输入
bqml_tutorial。在位置类型部分,选择多区域,然后选择美国。
保持其余默认设置不变,然后点击创建数据集。
bq
如需创建新数据集,请使用 bq mk --dataset 命令。
创建一个名为
bqml_tutorial的数据集,并将数据位置设置为US。bq mk --dataset \ --location=US \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial
确认已创建数据集:
bq ls
API
使用已定义的数据集资源调用 datasets.insert 方法。
{ "datasetReference": { "datasetId": "bqml_tutorial" } }
创建连接
创建 Cloud 资源连接并获取连接的服务账号。 在上一步中创建的数据集所在的位置创建连接。
请按照以下步骤创建连接:
转到 BigQuery 页面。
在探索器窗格中,点击 添加数据:
系统随即会打开添加数据对话框。
在过滤条件窗格中的数据源类型部分,选择企业应用。
或者,在搜索数据源字段中,您可以输入
Vertex AI。在精选数据源部分中,点击 Vertex AI。
点击 Vertex AI 模型:BigQuery 联合解决方案卡片。
在连接类型列表中,选择 Vertex AI 远程模型、远程函数、BigLake 和 Spanner(Cloud 资源)。
在连接 ID 字段中,输入
test_connection。点击创建连接。
点击转到连接。
在连接信息窗格中,复制服务账号 ID 以在下一步中使用。
向服务账号授予访问权限
向连接的服务账号授予 Vertex AI User 角色。
如需授予该角色,请按以下步骤操作:
前往 IAM 和管理页面。
点击 Add。
系统随即会打开添加主账号对话框。
在新的主账号字段中,输入您之前复制的服务账号 ID。
在选择角色字段中,选择 Vertex AI,然后选择 Vertex AI User。
点击添加其他角色。
在选择角色字段中,选择 Cloud Storage,然后选择 Storage Object Viewer。
点击保存。
总结文本并使用默认输出格式
请按照以下步骤操作,使用 AI.GENERATE 函数生成文本,并以 AI.GENERATE 函数的默认格式输出结果:
在 Google Cloud 控制台中,前往 BigQuery 页面。
在查询编辑器中,运行以下查询:
WITH bbc_news AS ( SELECT body FROM `bigquery-public-data.bbc_news.fulltext` LIMIT 5 ) SELECT AI.GENERATE(body) AS news FROM bbc_news;
输出类似于以下内容:
+---------------------------------------------+------------------------------------+---------------+ | news.result | news.full_response | news.status | +---------------------------------------------+------------------------------------+---------------+ | This article presents a debate about the | {"candidates":[{"avg_logprobs": | | | "digital divide" between rich and poor | -0.31465074559841777, content": | | | nations. Here's a breakdown of the key.. | {"parts":[{"text":"This article.. | | +---------------------------------------------+------------------------------------+---------------+ | This article discusses how advanced | {"candidates":[{"avg_logprobs": | | | mapping technology is aiding humanitarian | -0.21313422900091983,"content": | | | efforts in Darfur, Sudan. Here's a... | {"parts":[{"text":"This article.. | | +---------------------------------------------+------------------------------------+---------------+ | ... | ... | ... | +---------------------------------------------+------------------------------------+---------------+
总结文本并输出结构化结果
请按照以下步骤操作,使用 AI.GENERATE 函数生成文本,并使用 AI.GENERATE 函数的 output_schema 参数设置输出格式:
在 Google Cloud 控制台中,前往 BigQuery 页面。
在查询编辑器中,运行以下查询:
WITH bbc_news AS ( SELECT body FROM `bigquery-public-data`.bbc_news.fulltext LIMIT 5 ) SELECT news.good_sentiment, news.summary FROM bbc_news, UNNEST(ARRAY[AI.GENERATE(body, output_schema => 'summary STRING, good_sentiment BOOL')]) AS news;
输出类似于以下内容:
+----------------+--------------------------------------------+ | good_sentiment | summary | +----------------+--------------------------------------------+ | true | A World Bank report suggests the digital | | | divide is rapidly closing due to increased | | | access to technology in developing.. | +----------------+--------------------------------------------+ | true | A review of sports games, focusing on the | | | rivalry between EA Sports and ESPN, and | | | the recent deal where EA acquired the.. | +----------------+--------------------------------------------+ | ... | ... | +----------------+--------------------------------------------+
转写和翻译视频内容
请按照以下步骤操作,基于公开视频内容创建对象表,然后转写和翻译视频:
在 Google Cloud 控制台中,前往 BigQuery 页面。
在查询编辑器中,运行以下查询以创建对象表:
CREATE OR REPLACE EXTERNAL TABLE `bqml_tutorial.video` WITH CONNECTION `us.test_connection` OPTIONS ( object_metadata = 'SIMPLE', uris = ['gs://cloud-samples-data/generative-ai/video/*']);
在查询编辑器中,运行以下查询以转写和翻译
pixel8.mp4文件:SELECT AI.GENERATE( (OBJ.GET_ACCESS_URL(ref, 'r'), 'Transcribe the video in Japanese and then translate to English.'), endpoint => 'gemini-2.5-flash', output_schema => 'japanese_transcript STRING, english_translation STRING' ).* EXCEPT (full_response, status) FROM `bqml_tutorial.video` WHERE REGEXP_CONTAINS(uri, 'pixel8.mp4');
输出类似于以下内容:
+--------------------------------------------+--------------------------------+ | english_translation | japanese_transcript | +--------------------------------------------+--------------------------------+ | My name is Saeka Shimada. I'm a | 島田 さえか です 。 東京 で フ | | photographer in Tokyo. Tokyo has many | ォトグラファー を し て い ま | | faces. The city at night is totally... | す 。 東京 に は いろんな 顔 が | +--------------------------------------------+--------------------------------+
分析音频文件内容
按照以下步骤,基于公开音频内容创建对象表,然后分析音频文件的内容。
在 Google Cloud 控制台中,前往 BigQuery 页面。
在查询编辑器中,运行以下查询以创建对象表:
CREATE OR REPLACE EXTERNAL TABLE `bqml_tutorial.audio` WITH CONNECTION `us.test_connection` OPTIONS ( object_metadata = 'SIMPLE', uris = ['gs://cloud-samples-data/generative-ai/audio/*']);
在查询编辑器中,运行以下查询以分析音频文件:
SELECT AI.GENERATE( (OBJ.GET_ACCESS_URL(ref, 'r'), 'Summarize the content of this audio file.'), endpoint => 'gemini-2.5-flash', output_schema => 'topic ARRAY<STRING>, summary STRING' ).* EXCEPT (full_response, status), uri FROM `bqml_tutorial.audio`;
结果类似于以下内容:
+--------------------------------------------+-----------------------------------------------------------+ | summary | topic | uri | +--------------------------------------------+-----------------------------------------------------------+ | The audio contains a distinctive 'beep' | beep sound | gs://cloud-samples-data/generativ... | | sound, followed by the characteristic | | | | sound of a large vehicle or bus backing.. | | | +--------------------------------------------+--------------------+--------------------------------------+ | | vehicle backing up | | | +--------------------+ | | | bus | | | +--------------------+ | | | alarm | | +--------------------------------------------+--------------------+--------------------------------------+ | The speaker introduces themselves | Introduction | gs://cloud-samples-data/generativ... | | as Gemini and expresses their excitement | | | | and readiness to dive into something.. | | | +--------------------------------------------+--------------------+--------------------------------------+ | | Readiness | | | +--------------------+ | | | Excitement | | | +--------------------+ | | | Collaboration | | +--------------------------------------------+--------------------+--------------------------------------+ | ... | ... | ... | +--------------------------------------------+--------------------+--------------------------------------+
清理
- 在 Google Cloud 控制台中,前往管理资源页面。
- 在项目列表中,选择要删除的项目,然后点击删除。
- 在对话框中输入项目 ID,然后点击关闭以删除项目。