AI.GENERATE 関数を使用してテキストを生成する
このチュートリアルでは、AI.GENERATE 関数を使用して、テキストまたはマルチモーダル データからテキストを生成する方法について説明します。AI.GENERATE 関数を使用すると、ホストされている Gemini モデルに Cloud リソース接続を使用して接続できるため、独自のモデルを作成して維持する必要がなくなります。
このチュートリアルでは、次のタスクを行う方法を説明します。
- テキスト コンテンツを要約して関数のデフォルトの形式で結果を出力する。
- テキスト コンテンツを要約して構造化された結果を出力する。
- 動画コンテンツの文字起こしと翻訳を行う。
- 音声ファイルの内容を分析する。
費用
このドキュメントでは、課金対象である次の 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.
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
BigQuery の料金の詳細については、BigQuery ドキュメントの BigQuery の料金をご覧ください。
Vertex AI の生成 AI の料金の詳細については、Vertex AI の料金のページをご覧ください。
始める前に
- Google Cloud アカウントにログインします。 Google Cloudを初めて使用する場合は、 アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $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 を有効にするには、
serviceusage.services.enable権限を含む Service Usage 管理者 IAM ロール(roles/serviceusage.serviceUsageAdmin)が必要です。詳しくは、ロールを付与する方法をご覧ください。
必要なロール
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)。
これらの事前定義ロールには、このドキュメントのタスクを実行するために必要な権限が含まれています。必要とされる正確な権限については、「必要な権限」セクションを開いてご確認ください。
必要な権限
- データセットを作成する:
bigquery.datasets.create - 接続を作成、委任、使用する:
bigquery.connections.* - サービス アカウントの権限を設定する:
resourcemanager.projects.getIamPolicyとresourcemanager.projects.setIamPolicy - テーブルデータをクエリする:
bigquery.tables.getData
カスタムロールや他の事前定義ロールを使用して、これらの権限を取得することもできます。
データセットを作成する
ML モデルを保存する BigQuery データセットを作成します。
コンソール
Google Cloud コンソールで、[BigQuery] ページに移動します。
[エクスプローラ] ペインで、プロジェクト名をクリックします。
「アクションを表示」> [データセットを作成] をクリックします。
[データセットを作成する] ページで、次の操作を行います。
[データセット ID] に「
bqml_tutorial」と入力します。[ロケーション タイプ] で [マルチリージョン] を選択してから、[US] を選択します。
残りのデフォルトの設定は変更せず、[データセットを作成] をクリックします。
bq
新しいデータセットを作成するには、bq mk --dataset コマンドを使用します。
データの場所が
USに設定された、bqml_tutorialという名前のデータセットを作成します。bq mk --dataset \ --location=US \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial
データセットが作成されたことを確認します。
bq ls
API
定義済みのデータセット リソースを使用して datasets.insert メソッドを呼び出します。
{ "datasetReference": { "datasetId": "bqml_tutorial" } }
接続を作成する
クラウド リソース接続を作成し、接続のサービス アカウントを取得します。前の手順で作成したデータセットと同じロケーションに接続を作成します。
接続を作成する手順は次のとおりです。
[BigQuery] ページに移動します。
[エクスプローラ] ペインで、[ データを追加] をクリックします。
[データを追加] ダイアログが開きます。
[フィルタ条件] ペインの [データソースのタイプ] セクションで、[ビジネス アプリケーション] を選択します。
または、[データソースを検索] フィールドに「
Vertex AI」と入力します。[特徴量データソース] セクションで、[Vertex AI] をクリックします。
[Vertex AI モデル: BigQuery フェデレーション] ソリューション カードをクリックします。
[接続タイプ] リストで、[Vertex AI リモートモデル、リモート関数、BigLake、Spanner(Cloud リソース)] を選択します。
[接続 ID] フィールドに「
test_connection」と入力します。[接続を作成] をクリックします。
[接続に移動] をクリックします。
[接続情報] ペインで、次の手順で使用するためにサービス アカウント ID をコピーします。
サービス アカウントにアクセス権を付与する
接続のサービス アカウントに Vertex AI ユーザーロールを付与します。
ロールを付与する手順は次のとおりです。
[IAM と管理] ページに移動します。
[追加] をクリックします。
[プリンシパルを追加] ダイアログが開きます。
[新しいプリンシパル] フィールドに、前の手順でコピーしたサービス アカウント ID を入力します。
[ロールを選択] フィールドで、[Vertex AI] を選択し、[Vertex AI ユーザー] を選択します。
[別のロールを追加] をクリックします。
[ロールを選択] フィールドで、[Cloud Storage] を選択し、続いて [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 コンソールで [リソースの管理] ページに移動します。
- プロジェクト リストで、削除するプロジェクトを選択し、[削除] をクリックします。
- ダイアログでプロジェクト ID を入力し、[シャットダウン] をクリックしてプロジェクトを削除します。