Avec les fonctions Cloud Run, vous pouvez traiter davantage les données de sortie à partir du modèle entraîné personnalisé Vertex AI et des nœuds d'application BigQuery. Vous pouvez utiliser ces intégrations avec des nœuds d'application de différentes manières :
- Nœud Modèle personnalisé Vertex AI : utilisez Cloud Run Functions pour post-traiter les résultats de prédiction à partir du modèle personnalisé Vertex AI d'origine.
- Nœud BigQuery : utilisez Cloud Run Functions pour générer des lignes BigQuery personnalisées avec les annotations d'origine.
Toutes les fonctions Cloud Run que vous utilisez avec App Platform doivent répondre aux exigences suivantes :
- Les fonctions Cloud Run doivent fournir le déclencheur HTTP.
- Les fonctions Cloud Run doivent recevoir une
AppPlatformCloudFunctionRequestchaîne JSON et renvoyer uneAppPlatformCloudFunctionResponsechaîne JSON. - Le schéma de charge utile d'annotation stocké dans la requête et la réponse doit suivre la spécification du modèle cible.
Définitions d'API : AppPlatformMetadata, AppPlatformCloudFunctionRequest, AppPlatformCloudFunctionResponse
// 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; }
Exemple d'utilisation
Utilisez le code suivant pour post-traiter les annotations du modèle entraîné personnalisé Vertex AI et remplacer les annotations par une paire clé-valeur constante.
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)