使用 AI.GENERATE 函式生成文字
本教學課程說明如何使用 AI.GENERATE 函式,從文字或多模態資料生成文字。您可以使用 AI.GENERATE 函式,透過雲端資源連線連線至代管的 Gemini 模型,不必自行建立及維護模型。
本教學課程將說明如何完成下列工作:
- 總結文字內容,並以函式的預設格式輸出結果。
- 摘要文字內容並輸出結構化結果。
- 轉錄及翻譯影片內容。
- 分析音訊檔案內容。
費用
在本文件中,您會使用下列 Google Cloud的計費元件:
- BigQuery ML: You incur costs for the data that you process in BigQuery.
- Vertex AI: You incur costs for calls to the Vertex AI model.
如要根據預測用量估算費用,請使用 Pricing Calculator。
如要進一步瞭解 BigQuery 定價,請參閱 BigQuery 說明文件中的「BigQuery 定價」一文。
如要進一步瞭解 Vertex AI 生成式 AI 定價,請參閱 Vertex AI 定價頁面。
事前準備
- 登入 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,您需要服務使用情形管理員 IAM 角色 (
roles/serviceusage.serviceUsageAdmin),其中包含serviceusage.services.enable權限。瞭解如何授予角色。
必要的角色
如要使用 AI.GENERATE 函式,您需要下列 Identity and Access Management (IAM) 角色:
- 建立及使用 BigQuery 資料集和資料表:專案的 BigQuery 資料編輯者 (
roles/bigquery.dataEditor)。 - 建立、委派及使用 BigQuery 連線:專案的 BigQuery 連線管理員 (
roles/bigquery.connectionsAdmin)。 - 將權限授予連線的服務帳戶:在包含 Vertex AI 端點的專案中,授予「專案 IAM 管理員」(
roles/resourcemanager.projectIamAdmin) 角色。 - 建立 BigQuery 工作:專案中的 BigQuery 工作使用者 (
roles/bigquery.jobUser)。
這些預先定義的角色具備執行本文所述工作所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:
所需權限
- 建立資料集:
bigquery.datasets.create - 建立、委派及使用連線:
bigquery.connections.* - 設定服務帳戶權限:
resourcemanager.projects.getIamPolicy和resourcemanager.projects.setIamPolicy - 查詢資料表資料:
bigquery.tables.getData
建立資料集
建立 BigQuery 資料集來儲存機器學習模型。
控制台
前往 Google Cloud 控制台的「BigQuery」頁面。
在「Explorer」窗格中,按一下專案名稱。
依序點按 「View actions」(查看動作) >「Create dataset」(建立資料集)
在「建立資料集」頁面中,執行下列操作:
在「Dataset ID」(資料集 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」頁面
在「Explorer」窗格中,按一下「新增資料」:
「新增資料」對話方塊隨即開啟。
在「依條件篩選」窗格的「資料來源類型」部分,選取「商用應用程式」。
或者,您也可以在「Search for data sources」(搜尋資料來源) 欄位中輸入
Vertex AI。在「精選資料來源」部分,點選「Vertex AI」。
按一下「Vertex AI Models: BigQuery Federation」解決方案資訊卡。
在「連線類型」清單中,選取「Vertex AI 遠端模型、遠端函式、BigLake 和 Spanner (Cloud 資源)」。
在「連線 ID」欄位中輸入
test_connection。點選「建立連線」。
點選「前往連線」。
在「連線資訊」窗格中,複製服務帳戶 ID,以便在下一步中使用。
授予服務帳戶存取權
為連線的服務帳戶授予 Vertex AI 使用者角色。
如要授予角色,請按照下列步驟操作:
前往「IAM & Admin」(IAM 與管理) 頁面。
按一下「新增」。
「新增主體」對話方塊隨即開啟。
在「新增主體」欄位,輸入先前複製的服務帳戶 ID。
在「選取角色」欄位中,依序選取「Vertex AI」和「Vertex AI 使用者」。
按一下 [Add another role] (新增其他角色)。
在「Select a role」(請選擇角色) 欄位中,依序選取「Cloud Storage」和「Storage Object Viewer」(Storage 物件檢視者)。
按一下 [儲存]。
摘要文字並使用預設輸出格式
請按照下列步驟使用 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, connection_id => 'us.test_connection') 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, connection_id => 'us.test', 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.'), connection_id => 'us.test_connection', 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.'), connection_id => 'us.test_connection', 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 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。