反覆呼叫 ML.GENERATE_TEXT,處理配額錯誤
本教學課程說明如何使用 BigQuery bqutil.procedure.bqml_generate_text 公開預存程序,逐一呼叫 ML.GENERATE_TEXT 函式。反覆呼叫函式可解決因超出函式配額和限制而發生的任何可重試錯誤。
如要查看 GitHub 中儲存程序的原始碼,請參閱 bqml_generate_text.sqlx。bqutil.procedure.bqml_generate_text如要進一步瞭解預存程序參數和用法,請參閱 README 檔案。
本教學課程會逐步引導您完成下列工作:
- 建立
gemini-2.0-flash模型的遠端模型。 - 使用遠端模型和
bigquery-public-data.bbc_news.fulltext公開資料表,透過bqutil.procedure.bqml_generate_text預存程序,反覆呼叫ML.GENERATE_TEXT函式。
所需權限
如要執行本教學課程,您需要下列 Identity and Access Management (IAM) 角色:
- 建立及使用 BigQuery 資料集、連線和模型:
BigQuery 管理員 (
roles/bigquery.admin)。 - 將權限授予連線的服務帳戶:專案 IAM 管理員 (
roles/resourcemanager.projectIamAdmin)。
這些預先定義的角色具備執行本文中工作所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:
所需權限
- 建立資料集:
bigquery.datasets.create - 建立、委派及使用連線:
bigquery.connections.* - 設定預設連線:
bigquery.config.* - 設定服務帳戶權限:
resourcemanager.projects.getIamPolicy和resourcemanager.projects.setIamPolicy - 建立模型並執行推論:
bigquery.jobs.createbigquery.models.createbigquery.models.getDatabigquery.models.updateDatabigquery.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.
如要根據預測用量估算費用,請使用 Pricing Calculator。
如要進一步瞭解 BigQuery 定價,請參閱 BigQuery 定價。
如要進一步瞭解 Vertex AI 定價,請參閱 Vertex AI 定價。
事前準備
-
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
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the BigQuery, BigQuery Connection, and Vertex AI APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
建立資料集
建立 BigQuery 資料集來儲存模型和範例資料:
前往 Google Cloud 控制台的「BigQuery」頁面。
在「Explorer」窗格中,按一下專案名稱。
依序點按 「查看動作」>「建立資料集」。
在「建立資料集」頁面中,執行下列操作:
在「Dataset ID」(資料集 ID) 中輸入
sample。針對「Location type」(位置類型) 選取「Multi-region」(多區域),然後選取「US (multiple regions in United States)」(us (多個美國區域))。
其餘設定保留預設值,然後點選「建立資料集」。
建立文字生成模型
建立代表代管 Vertex AI gemini-2.0-flash 模型的遠端模型:
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式:
CREATE OR REPLACE MODEL `sample.generate_text` REMOTE WITH CONNECTION DEFAULT OPTIONS (ENDPOINT = 'gemini-2.0-flash');
查詢作業會在幾秒內完成,完成後,
generate_text模型會顯示在「Explorer」(探索工具) 窗格的sample資料集中。由於查詢使用CREATE MODEL陳述式建立模型,因此沒有查詢結果。
執行預存程序
執行 bqutil.procedure.bqml_generate_text 預存程序,該程序會使用 sample.generate_text 模型和 bigquery-public-data.bbc_news.fulltext 公開資料表,反覆呼叫 ML.GENERATE_TEXT 函式:
前往 Google Cloud 控制台的「BigQuery」頁面。
在查詢編輯器中執行下列陳述式:
CALL `bqutil.procedure.bqml_generate_text`( "bigquery-public-data.bbc_news.fulltext", -- source table "PROJECT_ID.sample.news_generated_text", -- destination table "PROJECT_ID.sample.generate_text", -- model "body", -- content column ["filename"], -- key columns '{}' -- optional arguments );
將
PROJECT_ID替換為您在本教學課程中使用的專案 ID。預存程序會建立
sample.news_generated_text資料表,用來存放ML.GENERATE_TEXT函式的輸出內容。查詢執行完畢後,請確認
sample.news_generated_text資料表中沒有包含可重試錯誤的資料列。在查詢編輯器中執行下列陳述式:SELECT * FROM `sample.news_generated_text` WHERE ml_generate_text_status LIKE '%A retryable error occurred%';
查詢會傳回訊息
No data to display。
清除所用資源
- 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.