Benachrichtigungen zu Modellereignissen mit Cloud Functions und Pub/Sub aktivieren
Mit Sammlungen den Überblick behalten
Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
In Vertex AI Vision erhalten Modelle Mediendaten von Geräten wie Kameras, führen KI-Vorhersagen auf den Daten aus und erstellen kontinuierlich Anmerkungen.
Häufig werden diese verarbeiteten Daten an ein Datenziel (Data Sink) wie ein Media-Warehouse oder BigQuery gesendet, um sie für weitere Analysejobs zu verwenden. Es kann jedoch vorkommen, dass einige Anmerkungen anders behandelt werden müssen oder dass die Anmerkungen zeitnah benötigt werden. Integrationen mit Cloud Run Functions und Pub/Sub können Ihnen dabei helfen, diese Anforderungen zu erfüllen.
So aktivieren Sie Benachrichtigungen zu Modellereignissen:
Die Modelldaten abhören und mithilfe von Cloud Run-Funktionen Ereignisse daraus generieren.
Senden Sie die von Cloud Run-Funktionen generierten Ereignisse über den Pub/Sub-Ereigniskanal.
Unterstützte Modelle
Die folgenden Modelle bieten die Ereignisgenerierung für Cloud Run-Funktionen und die Einbindung von Pub/Sub-Ereignisbenachrichtigungen:
Optional. Erstellen Sie eine Cloud Run-Funktion, die Sie verwenden möchten.
Wenn Sie Ihre Cloud Run-Funktion nicht erstellen, bevor Sie Cloud Run-Funktionen zum Verarbeiten der Modellausgabe konfigurieren, müssen Sie sie während dieses Vorgangs erstellen.
Optional. Erstellen Sie ein Pub/Sub-Thema, das Sie verwenden möchten.
Wenn Sie Ihr Pub/Sub-Thema nicht erstellen, bevor Sie die Benachrichtigung über Modellereignisse mit Pub/Sub aktivieren, müssen Sie es während dieses Vorgangs erstellen.
Optional. Wählen Sie ein Pub/Sub-Abo aus und erstellen Sie es.
Wenn Sie Ihr Pub/Sub-Abo nicht erstellen, bevor Sie die Benachrichtigung über Modellereignisse mit Pub/Sub aktivieren, müssen Sie es danach erstellen, um Nachrichten von einem Thema zu lesen.
Cloud Run-Funktionen für die Verarbeitung der Modellausgabe konfigurieren
Wenn Sie ereignisbasierte Benachrichtigungen auslösen möchten, müssen Sie zuerst Cloud Run-Funktionen einrichten, um die Modellausgabe zu verarbeiten und Ereignisse zu generieren.
Ihre Cloud Run-Funktion stellt eine Verbindung zum Modell her und überwacht die Ausgabe als Nachbearbeitungsaktion. Die Cloud Run-Funktion sollte einen AppPlatformCloudFunctionResponse zurückgeben. Die Ereignisse (appplatformeventbody) werden an das Pub/Sub-Thema gesendet, das Sie im nächsten Schritt konfigurieren.
Beispiel für eine Cloud Run-Funktion (Modell für Belegung analysieren)
Beispiel für eine Cloud Run-Funktion
/***RespondstoanyHTTPrequest.**@param{!express:Request}reqHTTPrequestcontext.*@param{!express:Response}resHTTPresponsecontext.*/exports.hello_http=(req,res)=>{//Loggingstatementcanbereadwithcmd`gcloudfunctionslogsread{$functionName}`.//Formoreaboutlogging,pleaseseehttps://cloud.google.com/functions/docs/monitoring//Theprocessoroutputwillbestoredinreq.body.constmessageString=constructMessage(req.body);//SendyourmessagetooperatoroutputwithresHTTPresponsecontext.res.status(200).send(messageString);};functionconstructMessage(data){//Typically,yourprocessoroutputshouldcontainsappPlatformMetadata & it's designed output.//Herewewillusetheoccupancyanalyticsmodelasanexample.constappPlatformMetadata=data.appPlatformMetadata;constannotations=data.annotations;constevents=[];for(constannotationofannotations){events.push({"event_message":"Event message goes here","payload":{"attr_key_goes_here":"val_goes_here"},"event_id":"event_id_goes_here"});}//Typically,yourcloudfunctionshouldreturnastringrepresentaJSONwhichhastwofields://"annotations"mustfollowthespecificationofthetargetmodel.//"events"shouldbeoftype"AppPlatformEventBody".constmessageJson={"annotations":annotations,"events":events,};returnJSON.stringify(messageJson);}
So senden Sie den Modellausgabestream an Ihre Cloud Run-Funktion:
Console
Öffnen Sie den Tab Anwendungen des Vertex AI Vision-Dashboards.
Wählen Sie in der Liste neben dem Namen Ihrer Anwendung die Option schemaApp ansehen aus.
Klicken Sie auf das unterstützte Modell, um die Seitenleiste mit den Modelldetails zu öffnen.
Wählen Sie im Bereich Ereignisbenachrichtigung in der Liste Nachbearbeitung eine vorhandene Cloud Run-Funktion aus oder erstellen Sie eine neue.
Benachrichtigungen zu Modellereignissen mit Pub/Sub aktivieren
Nachdem Sie Cloud Run-Funktionen eingerichtet haben, um die Modellausgabe zu verarbeiten und Ereignisse zu generieren, können Sie Ereignisbenachrichtigungen mit Pub/Sub einrichten. Wenn Sie Nachrichten von einem Thema lesen möchten, müssen Sie außerdem ein Pub/Sub-Abo auswählen und erstellen.
Console
Öffnen Sie den Tab Anwendungen des Vertex AI Vision-Dashboards.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-01 (UTC)."],[],[],null,["# Enable model event notification with Cloud Functions and Pub/Sub\n\nIn Vertex AI Vision, models receive media data from devices like cameras, run AI predictions on the data, and produce annotations continuously. Frequently you send that processed data to a data destination (\"data sink\") such as a media warehouse or BigQuery for further analytic jobs. However, you may have a case where some annotations must be handled differently, or the annotation needs are time-sensitive. Integrations with Cloud Run functions and Pub/Sub help you address these needs.\n\n\u003cbr /\u003e\n\nTo be able to enable model event notifications, you need to do the following:\n\n\u003cbr /\u003e\n\n1. Listen to the model data and generate events from it using Cloud Run functions.\n2. Send the Cloud Run functions generated events through the Pub/Sub event channel.\n\n#### Supported models\n\nThe following models offer Cloud Run functions event generation and\nPub/Sub event notification integrations:\n\n- [Occupancy analytics](/vision-ai/docs/occupancy-analytics-model) model\n- Vertex AI [custom-trained models](#add-vertex-custom-model)\n\nBefore you begin\n----------------\n\n- Create an app with, at minimum, a stream node and a [supported model](#event-supported-models) node.\n- Optional. [Install the Vertex AI Vision SDK](/vision-ai/docs/cloud-environment#get-source-code) and [ingest data](/vision-ai/docs/create-manage-streams#ingest-videos) into your app. If you don't do this before you set up event notification you must do it after.\n- Optional. [Create a Cloud Run function](/functions/docs/create-deploy-gcloud-1st-gen) to use. If you don't create your Cloud Run function before you configure Cloud Run functions to process model output, you must create it during that process.\n- Optional. [Create a Pub/Sub topic](/pubsub/docs/create-topic) to use. If you don't create your Pub/Sub topic before you enable model event notification with Pub/Sub, you must create it during that process.\n- Optional. [Choose and create a Pub/Sub subscription](/pubsub/docs/create-subscription). If you don't create your Pub/Sub subscription before you enable model event notification with Pub/Sub, you must create it after to read messages from a topic.\n\n### Configure Cloud Run functions to process model output\n\nTo trigger event-based notifications, you must first set up Cloud Run functions\nto process model output and generate events.\n\nYour Cloud Run function connects to the model and listens to its output\nas its post-processing action. The Cloud Run function you should return\nan [`AppPlatformCloudFunctionResponse`](/vision-ai/docs/reference/rpc/google.cloud.visionai.v1#appplatformcloudfunctionresponse). The events\n([`appplatformeventbody`](/vision-ai/docs/reference/rpc/google.cloud.visionai.v1#appplatformeventbody)) are\nsent to the Pub/Sub topic you configure in the next step.\n\n\u003cbr /\u003e\n\n#### Sample Cloud Run function (occupancy analytics model)\n\n### Sample Cloud Run function\n\n /**\n * Responds to any HTTP request.\n *\n * @param {!express:Request} req HTTP request context.\n * @param {!express:Response} res HTTP response context.\n */\n exports.hello_http = (req, res) =\u003e {\n // Logging statement can be read with cmd `gcloud functions logs read {$functionName}`.\n // For more about logging, please see https://cloud.google.com/functions/docs/monitoring\n\n // The processor output will be stored in req.body.\n const messageString = constructMessage(req.body);\n\n // Send your message to operator output with res HTTP response context.\n res.status(200).send(messageString);\n };\n\n function constructMessage(data) {\n // Typically, your processor output should contains appPlatformMetadata & it's designed output.\n // Here we will use the occupancy analytics model as an example.\n const appPlatformMetadata = data.appPlatformMetadata;\n const annotations = data.annotations;\n const events = [];\n for(const annotation of annotations) {\n events.push({\n \"event_message\": \"Event message goes here\",\n \"payload\" : {\n \"attr_key_goes_here\" : \"val_goes_here\"\n },\n \"event_id\" : \"event_id_goes_here\"\n });\n }\n\n // Typically, your cloud function should return a string represent a JSON which has two fields:\n // \"annotations\" must follow the specification of the target model.\n // \"events\" should be of type \"AppPlatformEventBody\".\n const messageJson = {\n \"annotations\": annotations,\n \"events\": events,\n };\n return JSON.stringify(messageJson);\n }\n\nUse the following instructions to\nto send the model output stream to your Cloud Run function: \n\n### Console\n\n1. Open the **Applications** tab of the Vertex AI Vision dashboard.\n\n [Go to the Applications tab](https://console.cloud.google.com/ai/vision-ai/applications)\n2. Select schema**View app** next to the\n name of your application from the list.\n\n3. Click on the [supported model](#event-supported-model) to open the model\n details side panel.\n\n4. In the **post-processing** list of the **Event notification** section,\n select your existing Cloud Run function, or create a new one.\n\n### Enable model event notification with Pub/Sub\n\nAfter you have set up Cloud Run functions to process model output and generate\nevents, you can set up event notification with Pub/Sub. To read messages\nfrom a topic, you also need to\n[Choose and create a Pub/Sub subscription](/pubsub/docs/create-subscription). \n\n### Console\n\n1. Open the **Applications** tab of the Vertex AI Vision dashboard.\n\n [Go to the Applications tab](https://console.cloud.google.com/ai/vision-ai/applications)\n2. Select schema**View app** next to the\n name of your application from the list.\n\n3. Click on the [supported model](#event-supported-model) to open the model\n details side panel.\n\n4. In the **Event notification** section, select\n **Set up event notification**.\n\n5. In the **Set up Pub/Sub for event notifications** option window that\n opens, choose your existing Pub/Sub topic, or create a new one.\n\n6. In the **Frequency** field, set an integer value for the frequency value\n in seconds a notification for the same type of event can be sent.\n\n7. Click **Set up**.\n\nWhat's next\n-----------\n\n- Read instructions about how to deploy your app to test model event notification in [Deploy and undeploy an application](/vision-ai/docs/deploy-app).\n- Work through the [Vertex AI Vision Occupancy Analytics app with event management](https://codelabs.developers.google.com/vertex-ai-event-management#0) codelab.\n- Learn more about Pub/Sub and Cloud Run functions in [What is Pub/Sub?](/pubsub/docs/overview) and the [Cloud Run functions overview](/functions/docs/concepts/overview)."]]