对于某些 API 调用,Vertex AI 会返回操作名称。这些 API 调用会启动需要一些时间才能完成的操作,称为“长时间运行的操作”。例如,创建数据集、删除端点或导出模型都是长时间运行的操作。您可以使用辅助方法和操作名称来获取长时间运行的操作的状态或取消长时间运行的操作,如以下部分所述。
获取操作状态
要获取操作状态,请使用请求长时间运行的操作时响应中的操作名称。例如,您创建数据集时,Vertex AI 会返回操作名称,例如:
projects/PROJECT_NUMBER/locations/LOCATION/datasets/DATASET_ID/operations/OPERATION_ID
您可以定期轮询操作,以便了解操作何时完成。
REST
在使用任何请求数据之前,请先进行以下替换:
- OPERATION_NAME:启动长时间运行的操作时返回的操作名称,例如
projects/PROJECT_NUMBER/locations/LOCATION/datasets/DATASET_ID/operations/OPERATION_ID
HTTP 方法和网址:
GET https://LOCATION-aiplatform.googleapis.com/v1/OPERATION_NAME
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X GET \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
"https://LOCATION-aiplatform.googleapis.com/v1/OPERATION_NAME"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/OPERATION_NAME" | Select-Object -Expand Content
在输出中,metadata 对象包含特定于请求类型的信息。done 字段指示操作是否完成。如果操作完成,response 对象将包含操作的结果。
{
"name": "projects/123456789012/locations/us-central1/datasets/1234567890123456789/operations/1223344556677889900",
"metadata": {
"@type": "type.googleapis.com/google.cloud.aiplatform.v1.CreateDatasetOperationMetadata",
"genericMetadata": {
"createTime": "2020-10-12T16:00:44.686500Z",
"updateTime": "2020-10-12T16:01:06.115081Z"
}
},
"done": true,
"response": {
"@type": "type.googleapis.com/google.cloud.aiplatform.v1.Dataset",
"name": "projects/123456789012/locations/us-central1/datasets/1234567890123456789",
"displayName": "image_dataset",
"metadataSchemaUri": "gs://google-cloud-aiplatform/schema/dataset/metadata/image_1.0.0.yaml",
"labels": {
"aiplatform.googleapis.com/dataset_metadata_schema": "IMAGE"
},
"metadata": {
"dataItemSchemaUri": "gs://google-cloud-aiplatform/schema/dataset/dataitem/image_1.0.0.yaml"
}
}
}
取消操作
您可以取消长时间运行的操作,以便在操作完成之前停止该操作。当您成功取消操作后,操作不会被删除。相反,操作会停止并显示错误代码 1 和 CANCELLED 消息。请注意,无法保证取消会成功。
REST
在使用任何请求数据之前,请先进行以下替换:
- OPERATION_NAME:启动长时间运行的操作时返回的操作名称,例如
projects/PROJECT_NUMBER/locations/LOCATION/datasets/DATASET_ID/operations/OPERATION_ID
HTTP 方法和网址:
POST https://LOCATION-aiplatform.googleapis.com/v1/OPERATION_NAME:cancel
如需发送请求,请选择以下方式之一:
curl
执行以下命令:
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d "" \
"https://LOCATION-aiplatform.googleapis.com/v1/OPERATION_NAME:cancel"
PowerShell
执行以下命令:
$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }
Invoke-WebRequest `
-Method POST `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/OPERATION_NAME:cancel" | Select-Object -Expand Content
您应该会收到一个成功的状态代码 (2xx) 和一个空响应。