通过以迭代方式调用 ML.GENERATE_EMBEDDING 来处理配额错误

本教程介绍如何使用 BigQuery bqutil.procedure.bqml_generate_embeddings 公共存储过程来迭代对 ML.GENERATE_EMBEDDING 函数的调用。通过以迭代方式调用函数,您可以解决因超出适用于函数的配额和限制而发生的任何可重试错误。

如需查看 GitHub 中 bqutil.procedure.bqml_generate_embeddings 存储过程的源代码,请参阅 bqml_generate_embeddings.sqlx。如需详细了解存储过程参数和用法,请参阅自述文件

本教程将指导您完成以下任务:

所需权限

如需运行本教程,您需要拥有以下 Identity and Access Management (IAM) 角色:

  • 创建和使用 BigQuery 数据集、连接和模型:BigQuery Admin (roles/bigquery.admin)。
  • 为连接的服务账号授予权限:Project IAM Admin (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.

您可使用价格计算器根据您的预计使用情况来估算费用。

新 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 页面

  2. 探索器窗格中,点击您的项目名称。

  3. 点击 查看操作 > 创建数据集

  4. 创建数据集 页面上,执行以下操作:

    1. 数据集 ID 部分,输入 target_dataset

    2. 位置类型部分,选择多区域,然后选择 US (multiple regions in United States)(美国[美国的多个区域])。

    3. 保持其余默认设置不变,然后点击创建数据集

创建文本嵌入生成模型

创建一个代表托管式 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 模型会显示在探索器窗格的 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.