使用 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.
如要根據預測用量估算費用,請使用 Pricing Calculator。
如要進一步瞭解 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.
-
Verify 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。在部署期間,您必須在部署工作流程中,選取「Endpoint access」欄位的值為「Public (shared endpoint)」。
建立資料集
建立 BigQuery 資料集來儲存機器學習模型。
控制台
前往 Google Cloud 控制台的「BigQuery」頁面。
在「Explorer」窗格中,按一下專案名稱。
依序點按
「View actions」(查看動作) >「Create dataset」(建立資料集)。在「建立資料集」頁面中,執行下列操作:
在「Dataset ID」(資料集 ID) 中輸入
bqml_tutorial
。針對「Location type」(位置類型) 選取「Multi-region」(多區域),然後選取「US (multiple regions in United States)」(us (多個美國區域))。
其餘設定請保留預設狀態,然後按一下「Create dataset」(建立資料集)。
bq
如要建立新的資料集,請使用 bq mk
指令搭配 --location
旗標。如需可能的完整參數清單,請參閱 bq mk --dataset
指令參考資料。
建立名為「
bqml_tutorial
」的資料集,並將資料位置設為「US
」,以及說明設為「BigQuery ML tutorial dataset
」:bq --location=US mk -d \ --description "BigQuery ML tutorial dataset." \ bqml_tutorial
這個指令採用
-d
捷徑,而不是使用--dataset
旗標。如果您省略-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
模型會顯示在「Explorer」(探索工具) 窗格的 bqml_tutorial
資料集中。由於查詢是使用 CREATE MODEL
陳述式建立模型,因此不會有查詢結果。
執行關鍵字擷取作業
使用遠端模型和 ML.GENERATE_TEXT
函式,對 IMDB 電影評論執行關鍵字擷取作業:
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中輸入下列陳述式,對五則電影評論執行關鍵字擷取作業:
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」頁面。
在查詢編輯器中執行下列陳述式,對五則電影評論進行情緒分析:
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.