Gemma オープンモデルと ML.GENERATE_TEXT 関数を使用してテキストを生成する
このチュートリアルでは、Gemma モデルに基づいてリモートモデルを作成する方法について説明します。また、そのモデルを ML.GENERATE_TEXT
関数とともに使用して、bigquery-public-data.imdb.reviews
公開テーブルの映画レビューからキーワードを抽出し、感情分析を行う方法についても説明します。
必要な権限
このチュートリアルを実行するには、次の Identity and Access Management(IAM)ロールが必要です。
- BigQuery データセット、接続、モデルを作成して使用する: BigQuery 管理者(
roles/bigquery.admin
)。 - 接続のサービス アカウントに権限を付与する: プロジェクト IAM 管理者(
roles/resourcemanager.projectIamAdmin
)。 - Vertex AI でモデルのデプロイとデプロイ解除を行う: Vertex AI 管理者(
roles/aiplatform.admin
)。
これらの事前定義ロールには、このドキュメントのタスクを実行するために必要な権限が含まれています。必要とされる正確な権限については、「必要な権限」セクションを開いてご確認ください。
必要な権限
- データセットを作成する:
bigquery.datasets.create
- 接続を作成、委任、使用する:
bigquery.connections.*
- デフォルトの接続を設定する:
bigquery.config.*
- サービス アカウントの権限を設定する:
resourcemanager.projects.getIamPolicy
とresourcemanager.projects.setIamPolicy
- Vertex AI モデルのデプロイとデプロイ解除を行う
aiplatform.endpoints.deploy
aiplatform.endpoints.undeploy
- モデルを作成して推論を実行する
bigquery.jobs.create
bigquery.models.create
bigquery.models.getData
bigquery.models.updateData
bigquery.models.updateMetadata
カスタムロールや他の事前定義ロールを使用して、これらの権限を取得することもできます。
費用
このドキュメントでは、課金対象である次の 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 that's represented by the remote model.
料金計算ツールを使うと、予想使用量に基づいて費用の見積もりを生成できます。
BigQuery の料金の詳細については、BigQuery ドキュメントの BigQuery の料金をご覧ください。
Vertex AI にデプロイするオープンモデルは、マシン時間単位で課金されます。つまり、エンドポイントの設定が完了するとすぐに課金が開始され、そのデプロイを解除するまで課金が継続されます。Vertex AI の料金の詳細については、Vertex AI の料金のページをご覧ください。
始める前に
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Google Cloud project.
-
Enable the BigQuery, BigQuery Connection, and Vertex AI APIs.
Vertex AI に Gemma モデルをデプロイする
Model Garden のモデルをデプロイするの手順に沿って、gemma-2-27b-it
モデルを Vertex AI にデプロイします。デプロイ時に、デプロイ ワークフローの [エンドポイント アクセス] フィールドの値として [パブリック(共有エンドポイント)] を選択する必要があります。
データセットを作成する
ML モデルを保存する BigQuery データセットを作成します。
コンソール
Google Cloud コンソールで、[BigQuery] ページに移動します。
[エクスプローラ] ペインで、プロジェクト名をクリックします。
[アクションを表示] > [データセットを作成] をクリックします。
[データセットを作成] ページで、次の操作を行います。
[データセット ID] に「
bqml_tutorial
」と入力します。[ロケーション タイプ] で [マルチリージョン] を選択してから、[US(米国の複数のリージョン)] を選択します。
残りのデフォルトの設定は変更せず、[データセットを作成] をクリックします。
bq
新しいデータセットを作成するには、--location
フラグを指定した bq mk
コマンドを使用します。使用可能なパラメータの一覧については、bq mk --dataset
コマンドのリファレンスをご覧ください。
データの場所が
US
に設定され、BigQuery ML tutorial dataset
という説明の付いた、bqml_tutorial
という名前のデータセットを作成します。bq --location=US mk -d \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial
このコマンドでは、
--dataset
フラグの代わりに-d
ショートカットを使用しています。-d
と--dataset
を省略した場合、このコマンドはデフォルトでデータセットを作成します。データセットが作成されたことを確認します。
bq ls
API
定義済みのデータセット リソースを使用して、datasets.insert
メソッドを呼び出します。
{ "datasetReference": { "datasetId": "bqml_tutorial" } }
BigQuery DataFrames
このサンプルを試す前に、BigQuery DataFrames を使用した BigQuery クイックスタートの手順に沿って BigQuery DataFrames を設定してください。詳細については、BigQuery DataFrames のリファレンス ドキュメントをご覧ください。
BigQuery に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の ADC の設定をご覧ください。
リモートモデルを作成する
Vertex AI モデルを表すリモートモデルを作成します。
Google Cloud コンソールで、[BigQuery] ページに移動します。
クエリエディタで、次のステートメントを実行します。
CREATE OR REPLACE MODEL `bqml_tutorial.gemma_model` REMOTE WITH CONNECTION DEFAULT OPTIONS (ENDPOINT = 'https://ENDPOINT_REGION-aiplatform.googleapis.com/v1/projects/ENDPOINT_PROJECT_ID/locations/ENDPOINT_REGION/endpoints/ENDPOINT_ID');
次のように置き換えます。
ENDPOINT_REGION
: オープンモデルがデプロイされているリージョン。ENDPOINT_PROJECT_ID
: オープンモデルがデプロイされているプロジェクト。ENDPOINT_ID
: オープンモデルで使用される HTTPS エンドポイントの ID。エンドポイント ID は、[オンライン予測] ページでオープンモデルを探して、その [ID] フィールドの値をコピーすることで取得できます。
次の例は、有効な HTTP エンドポイントの形式を示しています。
https://us-central1-aiplatform.googleapis.com/v1/projects/myproject/locations/us-central1/endpoints/1234
。
クエリが完了するまでに数秒かかります。完了後、gemma_model
モデルが [エクスプローラ] ペインの bqml_tutorial
データセットに表示されます。クエリは CREATE MODEL
ステートメントを使用してモデルを作成するため、クエリの結果はありません。
キーワード抽出を行う
リモートモデルと ML.GENERATE_TEXT
関数を使用して、IMDB 映画レビューに対してキーワード抽出を行います。
Google Cloud コンソールで、[BigQuery] ページに移動します。
クエリエディタで次のステートメントを入力して、5 つの映画レビューでキーワード抽出を行います。
SELECT * FROM ML.GENERATE_TEXT( MODEL `bqml_tutorial.gemma_model`, ( SELECT CONCAT('Extract the key words from the movie review below: ', review) AS prompt, * FROM `bigquery-public-data.imdb.reviews` LIMIT 10 ), STRUCT( 0.2 AS temperature, 100 AS max_output_tokens, TRUE AS flatten_json_output));
出力は次のようになります(わかりやすくするために、生成されていない列は省略しています)。
+----------------------------------------------+-------------------------+-----------------------------+-----+ | generated_text | ml_generate_text_status | prompt | ... | +----------------------------------------------+-------------------------------------------------------+-----+ | Here are some key words from the | | Extract the key words from | | | movie review: * **Romance:** | | the movie review below: | | | "romantic tryst," "elope" * **Comedy:** | | Linda Arvidson (as Jennie) | | | "Contrived Comedy" * **Burglary:** | | and Harry Solter (as Frank) | | | "burglar," "rob," "booty" * **Chase:** | | are enjoying a romantic | | | "chases," "escape" * **Director:** "D.W. | | tryst, when in walks her | | | Griffith" * **Actors:** "Linda Arvidson,"... | | father Charles Inslee;... | | +----------------------------------------------+-------------------------+-----------------------------+-----+ | Here are some key words from the | | Extract the key words from | | | movie review: * **Elderbush Gilch:** The | | the movie review below: | | | name of the movie being reviewed. * | | This is the second addition | | | **Disappointment:** The reviewer's | | to Frank Baum's personally | | | overall feeling about the film. * | | produced trilogy of Oz | | | **Dim-witted:** Describes the story | | films. It's essentially the | | | line negatively. * **Moronic, sadistic,... | | same childishness as the... | | +----------------------------------------------+-------------------------+-----------------------------+-----+
結果には次の列が含まれます。
generated_text
: 生成されたテキスト。ml_generate_text_status
: 対応する行の API レスポンス ステータス。オペレーションが成功した場合、この値は空になります。prompt
: 感情分析に使用されるプロンプト。bigquery-public-data.imdb.reviews
テーブルのすべての列。
感情分析を行う
リモートモデルと ML.GENERATE_TEXT
関数を使用して、IMDB 映画レビューに対して感情分析を行います。
Google Cloud コンソールで、[BigQuery] ページに移動します。
クエリエディタで次のステートメントを実行して、5 つの映画レビューの感情分析を行います。
SELECT * FROM ML.GENERATE_TEXT( MODEL `bqml_tutorial.gemma_model`, ( SELECT CONCAT('Analyze the sentiment of the following movie review and classify it as either POSITIVE or NEGATIVE. \nMovie Review: ', review) AS prompt, * FROM `bigquery-public-data.imdb.reviews` LIMIT 10 ), STRUCT( 0.2 AS temperature, 128 AS max_output_tokens, TRUE AS flatten_json_output));
出力は次のようになります(わかりやすくするために、生成されていない列は省略しています)。
+----------------------------------------------+-------------------------+-----------------------------+-----+ | generated_text | ml_generate_text_status | prompt | ... | +----------------------------------------------+-------------------------------------------------------+-----+ | **Sentiment:** NEGATIVE **Justification:** | | Analyze the sentiment of | | | * **Negative Language:** The reviewer uses | | movie review and classify | | | phrases like "don't quite make it," "come to | | it as either POSITIVE or | | | mind," "quite disappointing," and "not many | | NEGATIVE. Movie Review: | | | laughs." * **Specific Criticisms:** The | | Although Charlie Chaplin | | | reviewer points out specific flaws in the | | made some great short | | | plot and humor, stating that the manager... | | comedies in the late... | | +----------------------------------------------+-------------------------+-----------------------------+-----+ | **Sentiment:** NEGATIVE **Reasoning:** | | Analyze the sentiment of | | | * **Negative Language:** The reviewer uses | | movie review and classify | | | phrases like "poor writing," "static camera- | | it as either POSITIVE or | | | work," "chews the scenery," "all surface and | | NEGATIVE. Movie Review: | | | no depth," "sterile spectacles," which all | | Opulent sets and sumptuous | | | carry negative connotations. * **Comparison | | costumes well photographed | | | to a More Successful Film:**... | | by Theodor Sparkuhl, and... | | +----------------------------------------------+-------------------------+-----------------------------+-----+
結果には、キーワード抽出を行うで説明したものと同じ列が含まれます。
モデルのデプロイ解除
推奨どおりにプロジェクトを削除しない場合は、請求が継続しないように、Vertex AI で Gemma モデルのデプロイを解除するようにしてください。
クリーンアップする
- In the Google Cloud console, go to the Manage resources page.
- In the project list, select the project that you want to delete, and then click Delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.