批量获取响应是高效发送大量非延迟时间敏感类嵌入请求的方法。与获取在线响应(一次只能有一个输入请求)不同,您可以在单个批量请求中发送大量 LLM 请求。与对 Vertex AI 中的表格数据执行批量预测的方式类似,您可以确定输出位置、添加输入,然后响应会异步填充到输出位置。
支持批量预测的文本嵌入模型
除 Gemini 嵌入模型 (gemini-embedding-001) 外,所有稳定版文本嵌入模型都支持批量预测。稳定版在生产环境中完全受支持。如需查看嵌入模型的完整列表,请参阅嵌入模型和版本。
准备输入
批量请求的输入是可以存储在 BigQuery 表中或作为 JSON 行 (JSONL) 文件存储在 Cloud Storage 中的提示列表。每个请求最多可包含 30,000 项提示。
JSONL 示例
本部分介绍如何设置 JSONL 输入和输出的格式。
JSONL 输入示例
{"content":"Give a short description of a machine learning model:"}
{"content":"Best recipe for banana bread:"}
JSONL 输出示例
{"instance":{"content":"Give..."},"predictions": [{"embeddings":{"statistics":{"token_count":8,"truncated":false},"values":[0.2,....]}}],"status":""}
{"instance":{"content":"Best..."},"predictions": [{"embeddings":{"statistics":{"token_count":3,"truncated":false},"values":[0.1,....]}}],"status":""}
BigQuery 示例
本部分介绍如何设置 BigQuery 输入和输出的格式。
BigQuery 输入示例
此示例展示了一个单列 BigQuery 表。
| 内容 |
|---|
| “提供机器学习模型的简短说明:” |
| “最适合香蕉面包的食谱:” |
BigQuery 输出示例
| 内容 | 预测 | status |
|---|---|---|
| “提供机器学习模型的简短说明:” |
'[{"embeddings": { "statistics":{"token_count":8,"truncated":false}, "Values":[0.1,....] } } ]' |
|
| “最适合香蕉面包的食谱:” |
'[{"embeddings": { "statistics":{"token_count":3,"truncated":false}, "Values":[0.2,....] } } ]' |
请求批量响应
批量生成任务可能需要一些时间才能完成,具体取决于提交的输入数据项数量。
REST
如需使用 Vertex AI API 测试文本提示,请向发布方模型端点发送 POST 请求。
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目的 ID。
- BP_JOB_NAME:作业名称。
- INPUT_URI:输入源 URI。这是 BigQuery 表 URI 或 Cloud Storage 中的 JSONL 文件 URI。
- OUTPUT_URI:输出目标 URI。
HTTP 方法和网址:
POST https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/batchPredictionJobs
请求 JSON 正文:
{
"name": "BP_JOB_NAME",
"displayName": "BP_JOB_NAME",
"model": "publishers/google/models/textembedding-gecko",
"inputConfig": {
"instancesFormat":"bigquery",
"bigquerySource":{
"inputUri" : "INPUT_URI"
}
},
"outputConfig": {
"predictionsFormat":"bigquery",
"bigqueryDestination":{
"outputUri": "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://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/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://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/batchPredictionJobs" | Select-Object -Expand Content
您应该收到类似以下内容的 JSON 响应:
{
"name": "projects/123456789012/locations/us-central1/batchPredictionJobs/1234567890123456789",
"displayName": "BP_sample_publisher_BQ_20230712_134650",
"model": "projects/{PROJECT_ID}/locations/us-central1/models/textembedding-gecko",
"inputConfig": {
"instancesFormat": "bigquery",
"bigquerySource": {
"inputUri": "bq://project_name.dataset_name.text_input"
}
},
"modelParameters": {},
"outputConfig": {
"predictionsFormat": "bigquery",
"bigqueryDestination": {
"outputUri": "bq://project_name.llm_dataset.embedding_out_BP_sample_publisher_BQ_20230712_134650"
}
},
"state": "JOB_STATE_PENDING",
"createTime": "2023-07-12T20:46:52.148717Z",
"updateTime": "2023-07-12T20:46:52.148717Z",
"labels": {
"owner": "sample_owner",
"product": "llm"
},
"modelVersionId": "1",
"modelMonitoringStatus": {}
}
响应包含批量作业的唯一标识符。您可以使用 BATCH_JOB_ID 轮询批量作业的状态,直到作业 state 为 JOB_STATE_SUCCEEDED。 例如:
curl \ -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ https://us-central1-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/us-central1/batchPredictionJobs/BATCH_JOB_ID
Python
安装
pip install --upgrade google-genai
如需了解详情,请参阅 SDK 参考文档。
设置环境变量以将 Gen AI SDK 与 Vertex AI 搭配使用:
# Replace the `GOOGLE_CLOUD_PROJECT` and `GOOGLE_CLOUD_LOCATION` values # with appropriate values for your project. export GOOGLE_CLOUD_PROJECT=GOOGLE_CLOUD_PROJECT export GOOGLE_CLOUD_LOCATION=us-central1 export GOOGLE_GENAI_USE_VERTEXAI=True
检索批量输出
批量预测任务完成后,输出存储在您在请求中指定的 Cloud Storage 存储桶或 BigQuery 表中。
后续步骤
- 了解如何获取文本嵌入。