使用 Gemini 模型分析圖片
本教學課程將說明如何建立以 gemini-2.0-flash
模型為基礎的 BigQuery ML 遠端模型,然後搭配 ML.GENERATE_TEXT
函式函式使用該模型,分析一組電影海報圖片。
本教學課程涵蓋下列工作:
- 在 Cloud Storage 儲存桶中建立圖片資料的 BigQuery 物件資料表。
- 建立以 Vertex AI
gemini-2.0-flash
模型為目標的 BigQuery ML 遠端模型。 - 使用遠端模型搭配
ML.GENERATE_TEXT
函式,找出與一組電影海報相關聯的電影。
電影海報資料可從公開的 Cloud Storage bucket gs://cloud-samples-data/vertex-ai/dataset-management/datasets/classic-movie-posters
取得。
必要的角色
如要建立連線,您必須具備 BigQuery 連線管理員 (
roles/bigquery.connectionAdmin
) 角色。如要將權限授予連線的服務帳戶,您必須具備「Project IAM Admin」(
roles/resourcemanager.projectIamAdmin
) 角色。如要建立資料集、模型和資料表,您必須具備 BigQuery 資料編輯者 (
roles/bigquery.dataEditor
) 角色。如要執行 BigQuery 工作,您必須具備 BigQuery 使用者 (
roles/bigquery.user
) 角色。
費用
在本文件中,您會使用 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 is represented by the BigQuery remote model.
您可以使用 Pricing Calculator 根據預測用量產生預估費用。
如要進一步瞭解 BigQuery 定價,請參閱 BigQuery 說明文件中的「BigQuery 定價」一節。
如要進一步瞭解 Vertex AI 生成式 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.
建立資料集
建立 BigQuery 資料集來儲存機器學習模型。
控制台
前往 Google Cloud 控制台的「BigQuery」頁面。
在「Explorer」窗格中,按一下專案名稱。
依序點選
「View actions」(查看動作) >「Create dataset」(建立資料集)。在「Create dataset」頁面上執行下列操作:
在「Dataset ID」(資料集 ID) 中輸入
bqml_tutorial
。在「位置類型」中選取「多區域」,然後選取「美國 (多個美國區域)」。
保留其餘預設設定,然後點選「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 快速入門:使用 BigQuery DataFrames,按照 BigQuery DataFrames 設定說明進行操作。詳情請參閱 BigQuery DataFrames 參考資料說明文件。
如要向 BigQuery 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定 ADC」。
建立連線
建立 Cloud 資源連線,並取得連線的服務帳戶。
主控台
前往「BigQuery」頁面
在「Explorer」窗格中,按一下
「Add data」。「Add data」對話方塊隨即開啟。
在「Filter By」窗格中的「Data Source Type」部分,選取「Databases」。
或者,您也可以在「Search for data sources」欄位中輸入
Vertex AI
。在「精選資料來源」部分,按一下「Vertex AI」。
按一下「Vertex AI 模型:BigQuery 聯盟」解決方案資訊卡。
在「連線類型」清單中,選取「Vertex AI 遠端模型、遠端函式和 BigLake (Cloud 資源)」。
在「連線 ID」欄位中輸入
tutorial
。點選「建立連線」。
按一下「前往連線」。
從「連線資訊」窗格複製服務帳戶 ID,以便在後續步驟中使用。
bq
在指令列環境中建立連線:
bq mk --connection --location=us --project_id=PROJECT_ID \ --connection_type=CLOUD_RESOURCE tutorial
--project_id
參數會覆寫預設專案。將
PROJECT_ID
替換為您的Google Cloud 專案 ID。建立連線資源時,BigQuery 會建立專屬的系統服務帳戶,並將該帳戶與連線建立關聯。
疑難排解:如果您收到下列連線錯誤,請更新 Google Cloud SDK:
Flags parsing error: flag --connection_type=CLOUD_RESOURCE: value should be one of...
擷取並複製服務帳戶 ID,以便在後續步驟中使用:
bq show --connection PROJECT_ID.us.tutorial
輸出結果會與下列內容相似:
name properties 1234.us.tutorial {"serviceAccountId": "connection-1234-9u56h9@gcp-sa-bigquery-condel.iam.gserviceaccount.com"}
Terraform
將下列部分附加至 main.tf
檔案。
## This creates a Cloud Resource connection. ## Note: The cloud resource nested object has only one output only field - serviceAccountId. resource "google_bigquery_connection" "connection" { connection_id = "tutorial" project = "PROJECT_ID" location = "us" cloud_resource {} }
請將 PROJECT_ID
替換為您的 Google Cloud 專案 ID。
將權限授予連線的服務帳戶
授予連線服務帳戶適當的角色,以便存取 Cloud Storage 和 Vertex AI 服務。您必須在開始前一節中建立或選取的專案中授予這些角色。在其他專案中授予角色會導致 bqcx-1234567890-xxxx@gcp-sa-bigquery-condel.iam.gserviceaccount.com does not have the permission to access resource
錯誤。
如要授予適當的角色,請按照下列步驟操作:
前往「IAM & Admin」(IAM 與管理) 頁面。
按一下
「授予存取權」。在「新增主體」欄位,輸入先前複製的服務帳戶 ID。
在「請選擇角色」欄位中,依序選擇「Vertex AI」和「Vertex AI 使用者」。
按一下 [Add another role] (新增其他角色)。
在「請選擇角色」欄位中,依序選取「Cloud Storage」和「Storage 物件檢視者」。
按一下 [儲存]。
建立物件資料表
在公開的 Cloud Storage 值區中,針對電影海報圖片建立物件資料表。物件表可讓您分析圖片,而無須將圖片從 Cloud Storage 移出。
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列查詢,建立物件資料表:
CREATE OR REPLACE EXTERNAL TABLE `bqml_tutorial.movie_posters` WITH CONNECTION `us.tutorial` OPTIONS ( object_metadata = 'SIMPLE', uris = ['gs://cloud-samples-data/vertex-ai/dataset-management/datasets/classic-movie-posters/*']);
建立遠端模型
建立代表 Vertex AI gemini-2.0-flash
模型的遠端模型:
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列查詢,建立遠端模型:
CREATE OR REPLACE MODEL `bqml_tutorial.gemini-vision` REMOTE WITH CONNECTION `us.tutorial` OPTIONS (ENDPOINT = 'gemini-2.0-flash');
查詢需要幾秒鐘的時間才能完成,完成後,
gemini-vision
模型就會顯示在「Explorer」窗格中的bqml_tutorial
資料集中。由於查詢是使用CREATE MODEL
陳述式建立模型,因此沒有查詢結果。
分析電影海報
使用遠端模型分析電影海報,並判斷每張海報代表哪部電影,然後將這些資料寫入資料表。
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列查詢,分析電影海報圖片:
CREATE OR REPLACE TABLE `bqml_tutorial.movie_posters_results` AS ( SELECT uri, ml_generate_text_llm_result FROM ML.GENERATE_TEXT( MODEL `bqml_tutorial.gemini-vision`, TABLE `bqml_tutorial.movie_posters`, STRUCT( 0.2 AS temperature, 'For the movie represented by this poster, what is the movie title and year of release? Answer in JSON format with two keys: title, year. title should be string, year should be integer.' AS PROMPT, TRUE AS FLATTEN_JSON_OUTPUT)));
在查詢編輯器中執行下列陳述式,即可查看表格資料:
SELECT * FROM `bqml_tutorial.movie_posters_results`;
輸出結果會與下列內容相似:
+--------------------------------------------+----------------------------------+ | uri | ml_generate_text_llm_result | +--------------------------------------------+----------------------------------+ | gs://cloud-samples-data/vertex-ai/dataset- | ```json | | management/datasets/classic-movie- | { | | posters/little_annie_rooney.jpg | "title": "Little Annie Rooney", | | | "year": 1912 | | | } | | | ``` | +--------------------------------------------+----------------------------------+ | gs://cloud-samples-data/vertex-ai/dataset- | ```json | | management/datasets/classic-movie- | { | | posters/mighty_like_a_mouse.jpg | "title": "Mighty Like a Moose", | | | "year": 1926 | | | } | | | ``` | +--------------------------------------------+----------------------------------+ | gs://cloud-samples-data/vertex-ai/dataset- | ```json | | management/datasets/classic-movie- | { | | posters/brown_of_harvard.jpeg | "title": "Brown of Harvard", | | | "year": 1926 | | | } | | | ``` | +--------------------------------------------+----------------------------------+
設定模型輸出格式
將模型傳回的電影分析資料轉換成格式,讓電影名稱和年份資料更易讀。
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列查詢,以格式化資料:
CREATE OR REPLACE TABLE `bqml_tutorial.movie_posters_results_formatted` AS ( SELECT uri, JSON_QUERY(RTRIM(LTRIM(results.ml_generate_text_llm_result, " ```json"), "```"), "$.title") AS title, JSON_QUERY(RTRIM(LTRIM(results.ml_generate_text_llm_result, " ```json"), "```"), "$.year") AS year FROM `bqml_tutorial.movie_posters_results` results );
在查詢編輯器中執行下列陳述式,即可查看表格資料:
SELECT * FROM `bqml_tutorial.movie_posters_results_formatted`;
輸出結果會與下列內容相似:
+--------------------------------------------+----------------------------+------+ | uri | title | year | +--------------------------------------------+----------------------------+------+ | gs://cloud-samples-data/vertex-ai/dataset- | "Barque sortant du port" | 1895 | | management/datasets/classic-movie- | | | | posters/barque_sortant_du_port.jpeg | | | +--------------------------------------------+----------------------------+------+ | gs://cloud-samples-data/vertex-ai/dataset- | "The Great Train Robbery" | 1903 | | management/datasets/classic-movie- | | | | posters/the_great_train_robbery.jpg | | | +--------------------------------------------+----------------------------+------+ | gs://cloud-samples-data/vertex-ai/dataset- | "Little Annie Rooney" | 1912 | | management/datasets/classic-movie- | | | | posters/little_annie_rooney.jpg | | | +--------------------------------------------+----------------------------+------+
清除所用資源
- 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.