批量预测

通过批量预测,您可以高效地向模型发送对延迟不敏感的多个纯文本提示。与在线预测(每次请求发送一个输入提示)相比,您可以在单个请求中批量发送大量输入提示。

支持的模型

Gemini Enterprise Agent Platform 支持对以下模型进行批量预测。

Llama:

OpenAI gpt-oss:

Qwen:

DeepSeek:

嵌入模型:

准备输入

在开始之前,请在 BigQuery 表中或以 Cloud Storage 中的 JSONL 文件形式准备输入内容。这两种来源的输入都必须遵循 OpenAI API 架构 JSONL 格式,如以下示例所示。

大语言模型:

{"custom_id": "test-request-0", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "MODEL_ID", "messages": [{"role": "user", "content": "Give me a recipe for banana bread"}], "max_tokens": 1000}}

嵌入模型:

{"custom_id": "test-request-0", "method": "POST", "url": "/v1/embeddings", "body": {"model": "MODEL_ID", "input": "Hello World"}}

BigQuery

BigQuery 输入表必须遵循以下架构:

列名 说明
custom_id 每个请求的 ID,用于将输入与输出进行匹配。
方法 请求方法。
网址 请求端点。
body(JSON) 您的输入提示。
  • 输入表可以有其他列,这些列会被批量作业忽略,并直接传递到输出表。
  • 批量预测作业为批量预测输出预留了两个列名:response(JSON) 和 id。请勿在输入表中使用这些列。
  • 系统会舍弃 method 和 url 列,不会将其包含在输出表中。

Cloud Storage

对于 Cloud Storage,输入文件必须是位于 Cloud Storage 存储桶中的 JSONL 文件。

请求批量预测

使用来自 BigQuery 或 Cloud Storage 的输入,对模型进行批量预测。您可以独立选择将预测结果输出到 BigQuery 表或 Cloud Storage 存储桶中的 JSONL 文件。

BigQuery

指定 BigQuery 输入表、模型和输出位置。批量预测作业和您的表必须位于同一区域。

REST

在使用任何请求数据之前, 请进行以下替换:

  • LOCATION:支持相应模型的区域。
  • PROJECT_ID: 您的 项目 ID。 。
  • MODEL:要调优的 模型 的名称。
  • INPUT_URI:批量预测输入所在的 BigQuery 表,例如 myproject.mydataset.input_table。
  • OUTPUT_FORMAT:如需输出到 BigQuery 表,请指定 bigquery。如需输出到 Cloud Storage 存储桶,请指定 jsonl。
  • DESTINATION:对于 BigQuery,请指定 bigqueryDestination。对于 Cloud Storage,请指定 gcsDestination。
  • OUTPUT_URI_FIELD_NAME:对于 BigQuery,请指定 outputUri。对于 Cloud Storage,请指定 outputUriPrefix。
  • OUTPUT_URI:对于 BigQuery,请指定表位置,例如 myproject.mydataset.output_result。对于 Cloud Storage,请指定存储桶和文件夹位置,例如 gs://mybucket/path/to/outputfile。

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs

请求 JSON 正文:

'{
  "displayName": "JOB_NAME",
  "model": "publishers/PUBLISHER/models/MODEL_ID",
  "inputConfig": {
    "instancesFormat":"bigquery",
    "bigquerySource":{
      "inputUri" : "INPUT_URI"
    }
  },
  "outputConfig": {
    "predictionsFormat":"OUTPUT_FORMAT",
    "DESTINATION":{
      "OUTPUT_URI_FIELD_NAME": "OUTPUT_URI"
    }
  }
}'

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs" | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应。

Cloud Storage

指定 JSONL 文件的 Cloud Storage 位置、模型和输出位置。

REST

在使用任何请求数据之前, 请进行以下替换:

  • LOCATION:支持相应模型的区域。
  • PROJECT_ID: 您的 项目 ID。 。
  • MODEL:要调优的 模型 的名称。
  • INPUT_URI:JSONL 批量预测输入的 Cloud Storage 位置,例如 gs://bucketname/path/to/jsonl。
  • OUTPUT_FORMAT:如需输出到 BigQuery 表,请指定 bigquery。如需输出到 Cloud Storage 存储桶,请指定 jsonl。
  • DESTINATION:对于 BigQuery,请指定 bigqueryDestination。对于 Cloud Storage,请指定 gcsDestination。
  • OUTPUT_URI_FIELD_NAME:对于 BigQuery,请指定 outputUri。对于 Cloud Storage,请指定 outputUriPrefix。
  • OUTPUT_URI:对于 BigQuery,请指定表位置,例如 myproject.mydataset.output_result。对于 Cloud Storage,请指定存储桶和文件夹位置,例如 gs://mybucket/path/to/outputfile。

HTTP 方法和网址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs

请求 JSON 正文:

'{
  "displayName": "JOB_NAME",
  "model": "publishers/PUBLISHER/models/MODEL_ID",
  "inputConfig": {
    "instancesFormat":"jsonl",
    "gcsSource":{
      "uris" : "INPUT_URI"
    }
  },
  "outputConfig": {
    "predictionsFormat":"OUTPUT_FORMAT",
    "DESTINATION":{
      "OUTPUT_URI_FIELD_NAME": "OUTPUT_URI"
    }
  }
}'

如需发送请求,请选择以下方式之一:

curl

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs"

PowerShell

将请求正文保存在名为 request.json 的文件中,然后执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs" | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应。

获取批量预测作业的状态

获取批量预测作业的状态,以检查其是否已成功完成。作业时长取决于您提交的输入数据项的数量。

REST

在使用任何请求数据之前, 请进行以下替换:

  • PROJECT_ID: 您的 项目 ID。 。
  • LOCATION:批量作业所在的区域。
  • JOB_ID:您创建作业时返回的批量作业 ID。

HTTP 方法和网址:

GET https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs/JOB_ID

如需发送请求,请选择以下方式之一:

curl

执行以下命令:

curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs/JOB_ID"

PowerShell

执行以下命令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/batchPredictionJobs/JOB_ID" | Select-Object -Expand Content

您应该收到类似以下内容的 JSON 响应。

检索输出

批量预测作业完成后,可从您指定的位置检索输出。对于 BigQuery,输出位于目标 BigQuery 表的 response(JSON) 列中。对于 Cloud Storage,输出会以 JSONL 文件的形式保存在输出 Cloud Storage 位置中。

后续步骤

  • 了解如何针对开放模型调用 MaaS API,以用于流式和非流式用例。