使用 Gemma 開放式模型和 AI.GENERATE_TEXT 函式生成文字
本教學課程說明如何建立以 Gemma 模型為基礎的遠端模型,然後使用 AI.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)。
這些預先定義的角色具備執行本文所述工作所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:
所需權限
- 建立資料集:
bigquery.datasets.create - 建立、委派及使用連線:
bigquery.connections.* - 設定預設連線:
bigquery.config.* - 設定服務帳戶權限:
resourcemanager.projects.getIamPolicy和resourcemanager.projects.setIamPolicy - 部署及取消部署 Vertex AI 模型:
aiplatform.endpoints.deployaiplatform.endpoints.undeploy
- 建立模型並執行推論:
bigquery.jobs.createbigquery.models.createbigquery.models.getDatabigquery.models.updateDatabigquery.models.updateMetadata
費用
在本文件中,您會使用下列 Cloud de Confiance by S3NS的計費元件:
- 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 定價頁面。
事前準備
-
在 Cloud de Confiance 控制台的專案選擇器頁面中,選取或建立 Cloud de Confiance 專案。
選取或建立專案所需的角色
- 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您已獲授角色,即可選取任何專案。
-
建立專案:如要建立專案,您需要「專案建立者」角色 (
roles/resourcemanager.projectCreator),其中包含resourcemanager.projects.create權限。瞭解如何授予角色。
-
啟用 BigQuery、BigQuery Connection 和 Vertex AI API。
啟用 API 時所需的角色
如要啟用 API,您需要服務使用情形管理員 IAM 角色 (
roles/serviceusage.serviceUsageAdmin),其中包含serviceusage.services.enable權限。瞭解如何授予角色。
建立資料集
建立 BigQuery 資料集來儲存機器學習模型。
控制台
前往 Cloud de Confiance 控制台的「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" } }
建立遠端模型
建立代表代管 Vertex AI 模型的遠端模型:
前往 Cloud de Confiance 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式:
CREATE OR REPLACE MODEL `bqml_tutorial.gemma_model` REMOTE WITH CONNECTION DEFAULT OPTIONS ( MODEL_GARDEN_MODEL_NAME = 'publishers/google/models/gemma3@gemma-3-270m-it', MACHINE_TYPE = 'g2-standard-12' );
查詢作業最多需要 20 分鐘才能完成,完成後,gemma_model 模型會顯示在「Explorer」(探索工具) 窗格的 bqml_tutorial 資料集中。由於查詢是使用 CREATE MODEL 陳述式建立模型,因此不會有查詢結果。
執行關鍵字擷取
使用遠端模型和 AI.GENERATE_TEXT 函式,對 IMDB 電影評論執行關鍵字擷取作業:
前往 Cloud de Confiance 控制台的「BigQuery」頁面。
在查詢編輯器中輸入下列陳述式,對 10 則電影評論執行關鍵字擷取作業:
SELECT * FROM AI.GENERATE_TEXT( MODEL `bqml_tutorial.gemma_model`, ( SELECT '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));
輸出結果會與下列內容相似,為求明確起見,我們省略了非產生的資料欄:
+----------------------------------------------+-------------------------+-----------------------------+-----+ | result | 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,"... | | | | +----------------------------------------------+-------------------------+-----------------------------+-----+ | 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 ... | | | line negatively. * **Moronic, sadistic,... | | | | +----------------------------------------------+-------------------------+-----------------------------+-----+結果包含下列資料欄:
result:生成的文字。status:對應資料列的 API 回應狀態。如果作業成功,這個值會留空。prompt:用於情緒分析的提示。bigquery-public-data.imdb.reviews資料表中的所有欄。
執行情緒分析
使用遠端模型和 AI.GENERATE_TEXT 函式,對 IMDB 電影評論執行情緒分析:
前往 Cloud de Confiance 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式,對 10 則電影評論執行情緒分析:
SELECT * FROM AI.GENERATE_TEXT( MODEL `bqml_tutorial.gemma_model`, ( SELECT '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));
輸出結果會與下列內容相似,為求明確起見,我們省略了非產生的資料欄:
+-----------------------------+-------------------------+-----------------------------+-----+ | result | status | prompt | ... | +-----------------------------+-------------------------+-----------------------------+-----+ | **NEGATIVE** | | Analyze the sentiment of | | | | | movie review and classify | | | | | it as either POSITIVE or | | | | | NEGATIVE. Movie Review: | | | | | Although Charlie Chaplin | | | | | made some great short | | | | | comedies in the late... | | +-----------------------------+-------------------------+-----------------------------+-----+ | **NEGATIVE** | | Analyze the sentiment of | | | | | movie review and classify | | | | | it as either POSITIVE or | | | | | NEGATIVE. Movie Review: | | | | | Opulent sets and sumptuous | | | | | costumes well photographed | | | | | by Theodor Sparkuhl, and... | | +-----------------------------+-------------------------+-----------------------------+-----+結果包含與「執行關鍵字擷取作業」相同的資料欄。
取消部署模型
如果選擇不按照建議刪除專案,請務必在 Vertex AI 中取消部署 Gemma 模型,以免持續產生相關費用。在指定閒置時間 (預設為 6.5 小時) 過後,BigQuery 會自動取消部署模型。或者,您也可以使用 ALTER MODEL 陳述式立即取消部署模型,如下列範例所示:
ALTER MODEL `bqml_tutorial.gemma_model` SET OPTIONS (deploy_model = false);
詳情請參閱「自動或立即取消部署開放模型」。
清除所用資源
- 前往 Cloud de Confiance 控制台的「Manage resources」(管理資源) 頁面。
- 在專案清單中選取要刪除的專案,然後點選「Delete」(刪除)。
- 在對話方塊中輸入專案 ID,然後按一下 [Shut down] (關閉) 以刪除專案。