反覆呼叫 ML.GENERATE_EMBEDDING,處理配額錯誤

本教學課程說明如何使用 BigQuery bqutil.procedure.bqml_generate_embeddings 公開預存程序,逐一呼叫 ML.GENERATE_EMBEDDING 函式。反覆呼叫函式可解決因超出函式適用的配額和限制而發生的任何可重試錯誤。

如要查看 GitHub 中儲存程序的原始碼,請參閱 bqml_generate_embeddings.sqlxbqutil.procedure.bqml_generate_embeddings如要進一步瞭解預存程序參數和用法,請參閱 README 檔案

本教學課程會逐步引導您完成下列工作:

  • 建立text-embedding-005 模型的遠端模型
  • 使用遠端模型和 bigquery-public-data.bbc_news.fulltext 公開資料表,透過 bqutil.procedure.bqml_generate_embeddings 預存程序,反覆呼叫 ML.GENERATE_EMBEDDING 函式。

所需權限

如要執行本教學課程,您需要下列 Identity and Access Management (IAM) 角色:

  • 建立及使用 BigQuery 資料集、連線和模型: BigQuery 管理員 (roles/bigquery.admin)。
  • 將權限授予連線的服務帳戶:專案 IAM 管理員 (roles/resourcemanager.projectIamAdmin)。

這些預先定義的角色具備執行本文中工作所需的權限。如要查看確切的必要權限,請展開「必要權限」部分:

所需權限

  • 建立資料集:bigquery.datasets.create
  • 建立、委派及使用連線: bigquery.connections.*
  • 設定預設連線:bigquery.config.*
  • 設定服務帳戶權限: resourcemanager.projects.getIamPolicyresourcemanager.projects.setIamPolicy
  • 建立模型並執行推論:
    • 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.

如要根據預測用量估算費用,請使用 Pricing Calculator

初次使用 Google Cloud 的使用者可能符合免費試用資格。

如要進一步瞭解 BigQuery 定價,請參閱 BigQuery 定價

如要進一步瞭解 Vertex AI 定價,請參閱 Vertex AI 定價

事前準備

  1. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  2. Verify that billing is enabled for your Google Cloud project.

  3. Enable the BigQuery, BigQuery Connection, and Vertex AI APIs.

    Enable the APIs

建立資料集

建立 BigQuery 資料集來儲存模型和範例資料:

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往「BigQuery」BigQuery頁面

  2. 在「Explorer」窗格中,按一下專案名稱。

  3. 依序點按 「查看動作」>「建立資料集」

  4. 在「建立資料集」頁面中,執行下列操作:

    1. 在「Dataset ID」(資料集 ID) 中輸入 target_dataset

    2. 針對「Location type」(位置類型) 選取「Multi-region」(多區域),然後選取「US (multiple regions in United States)」(us (多個美國區域))

    3. 其餘設定請保留預設狀態,然後按一下「Create dataset」(建立資料集)

建立文字嵌入生成模型

建立代表代管 Vertex AI text-embedding-005 模型的遠端模型:

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在查詢編輯器中執行下列陳述式:

    CREATE OR REPLACE MODEL `target_dataset.embedding_model`
      REMOTE WITH CONNECTION DEFAULT
      OPTIONS (ENDPOINT = 'text-embedding-005');

    查詢作業會在幾秒內完成,完成後,embedding 模型會顯示在「Explorer」(探索工具) 窗格的 sample 資料集中。由於查詢是使用 CREATE MODEL 陳述式建立模型,因此不會有查詢結果。

執行預存程序

執行 bqutil.procedure.bqml_generate_embeddings 預存程序,該程序會使用 target_dataset.embedding_model 模型和 bigquery-public-data.bbc_news.fulltext 公開資料表,反覆呼叫 ML.GENERATE_EMBEDDING 函式:

  1. 前往 Google Cloud 控制台的「BigQuery」頁面。

    前往 BigQuery

  2. 在查詢編輯器中執行下列陳述式:

    CALL `bqutil.procedure.bqml_generate_embeddings`(
        "bigquery-public-data.bbc_news.fulltext",            -- source table
        "PROJECT_ID.target_dataset.news_body_embeddings",  -- destination table
        "PROJECT_ID.target_dataset.embedding_model",       -- model
        "body",                                              -- content column
        ["filename"],                                        -- key columns
        '{}'                                                 -- optional arguments encoded as a JSON string
    );

    PROJECT_ID 替換為您在本教學課程中使用的專案 ID。

    預存程序會建立 target_dataset.news_body_embeddings 資料表,用來存放 ML.GENERATE_EMBEDDING 函式的輸出內容。

  3. 查詢執行完畢後,請確認 target_dataset.news_body_embeddings 資料表中沒有包含可重試錯誤的資料列。在查詢編輯器中執行下列陳述式:

    SELECT *
    FROM `target_dataset.news_body_embeddings`
    WHERE ml_generate_embedding_status LIKE '%A retryable error occurred%';

    查詢會傳回訊息 No data to display

清除所用資源

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.