使用 Cloud Run 函式新增處理邏輯

您可以使用 Cloud Run 函式,進一步處理 Vertex AI 自訂訓練模型和 BigQuery 應用程式節點的輸出資料。您可以透過下列方式,將這些整合功能與應用程式節點搭配使用:

  • Vertex AI 自訂模型節點:使用 Cloud Run 函式,對原始 Vertex AI 自訂模型的預測結果進行後處理。
  • BigQuery 節點:使用 Cloud Run 函式,透過原始註解產生自訂 BigQuery 資料列。

您在 App Platform 中使用的所有 Cloud Run 函式都必須符合下列規定:

API 定義:AppPlatformMetadataAppPlatformCloudFunctionRequestAppPlatformCloudFunctionResponse

// Message of essential metadata of App Platform.
// This message is usually attached to a certain model output annotation for
// customer to identify the source of the data.
message AppPlatformMetadata {
  // The application resource name.
  string application = 1;
  // The instance resource id. Instance is the nested resource of application
  // under collection 'instances'.
  string instance_id = 2;
  // The node name of the application graph.
  string node = 3;
  // The referred model resource name of the application node.
  string processor = 4;
}

// For any Cloud Run function based customer processing logic, customer's cloud
// function is expected to receive AppPlatformCloudFunctionRequest as request
// and send back AppPlatformCloudFunctionResponse as response.
// Message of request from AppPlatform to Cloud Run functions.
message AppPlatformCloudFunctionRequest {
  // The metadata of the AppPlatform for customer to identify the source of the
  // payload.
  AppPlatformMetadata app_platform_metadata = 1;
  // A general annotation message that uses struct format to represent different
  // concrete annotation protobufs.
  message StructedInputAnnotation {
    // The ingestion time of the current annotation.
    int64 ingestion_time_micros = 1;
    // The struct format of the actual annotation.
    protobuf.Struct annotation = 2;
  }
  // The actual annotations to be processed by the customized Cloud Run function.
  repeated StructedInputAnnotation annotations = 2;
}

// Message of the response from customer's Cloud Run function to AppPlatform.
message AppPlatformCloudFunctionResponse {
  // A general annotation message that uses struct format to represent different
  // concrete annotation protobufs.
  message StructedOutputAnnotation {
    // The struct format of the actual annotation.
    protobuf.Struct annotation = 1;
  }

  // The modified annotations that is returned back to AppPlatform.
  // If the annotations fields are empty, then those annotations will be dropped
  // by AppPlatform.
  repeated StructedOutputAnnotation annotations = 2;
}

用法示範

請使用以下程式碼,對 Vertex AI 自訂訓練模型的註解進行後置處理,並將註解替換為常數鍵/值組合。

Python

import functions_framework
from flask import jsonify

@functions_framework.http
def hello_http(request):
   request_json = request.get_json(silent=True)
   request_args = request.args

   if request_json and 'annotations' in request_json:
       annotations = []
       for ele in request_json['annotations']:
           for k, v in ele.items():
               if k == "annotation":
                if "predictions" in v:
                    # Replace the annotation.
                    v["predictions"][0] = {"user": "googler"}
                    annotations.append({"annotation" : v})
   else:
       annotations = 'Failure'
   return jsonify(annotations=annotations)