Enable model event notification with Cloud Functions and Pub/Sub

In 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 Functions and Pub/Sub help you address these needs.

To be able to enable model event notifications, you need to do the following:

  1. Listen to the model data and generate events from it using Cloud Functions.
  2. Send the Cloud Functions generated events through the Pub/Sub event channel.

Supported models

The following models offer Cloud Functions event generation and Pub/Sub event notification integrations:

Before you begin

  • Create an app with, at minimum, a stream node and a supported model node.
  • Optional. Install the Vertex AI Vision SDK and ingest data into your app. If you don't do this before you set up event notification you must do it after.
  • Optional. Create a Cloud Function to use. If you don't create your Cloud Function before you configure Cloud Functions to process model output, you must create it during that process.
  • Optional. Create a Pub/Sub 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.
  • Optional. Choose and create a Pub/Sub 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.

Configure Cloud Functions to process model output

To trigger event-based notifications, you must first set up Cloud Functions to process model output and generate events.

Your Cloud Function connects to the model and listens to its output as its post-processing action. The Cloud Function you should return an AppPlatformCloudFunctionResponse. The events (appplatformeventbody) are sent to the Pub/Sub topic you configure in the next step.

Sample Cloud Function (occupancy analytics model)

Sample Cloud Function

/**
* Responds to any HTTP request.
*
* @param {!express:Request} req HTTP request context.
* @param {!express:Response} res HTTP response context.
*/
exports.hello_http = (req, res) => {
// Logging statement can be read with cmd `gcloud functions logs read {$functionName}`.
// For more about logging, please see https://cloud.google.com/functions/docs/monitoring

// The processor output will be stored in req.body.
const messageString = constructMessage(req.body);

// Send your message to operator output with res HTTP response context.
res.status(200).send(messageString);
};

function constructMessage(data) {
// Typically, your processor output should contains appPlatformMetadata & it's designed output.
// Here we will use the occupancy analytics model as an example.
const appPlatformMetadata = data.appPlatformMetadata;
const annotations = data.annotations;
const events = [];
for(const annotation of annotations) {
   events.push({
      "event_message": "Event message goes here",
      "payload" : {
         "attr_key_goes_here" : "val_goes_here"
      },
      "event_id" : "event_id_goes_here"
   });
}

// Typically, your cloud function should return a string represent a JSON which has two fields:
// "annotations" must follow the specification of the target model.
// "events" should be of type "AppPlatformEventBody".
const messageJson = {
   "annotations": annotations,
   "events": events,
};
return JSON.stringify(messageJson);
}

Use the following instructions to to send the model output stream to your Cloud Function:

Console

  1. Open the Applications tab of the Vertex AI Vision dashboard.

    Go to the Applications tab

  2. Select View app next to the name of your application from the list.

  3. Click on the supported model to open the model details side panel.

  4. In the post-processing list of the Event notification section, select your existing Cloud Function, or create a new one.

    Select postprocessing Cloud Function image in Cloud Console

Enable model event notification with Pub/Sub

After you have set up Cloud Functions to process model output and generate events, you can set up event notification with Pub/Sub. To read messages from a topic, you also need to Choose and create a Pub/Sub subscription.

Console

  1. Open the Applications tab of the Vertex AI Vision dashboard.

    Go to the Applications tab

  2. Select View app next to the name of your application from the list.

  3. Click on the supported model to open the model details side panel.

  4. In the Event notification section, select Set up event notification.

  5. In the Set up Pub/Sub for event notifications option window that opens, choose your existing Pub/Sub topic, or create a new one.

  6. In the Frequency field, set an integer value for the frequency value in seconds a notification for the same type of event can be sent.

    Set up event notification image in Cloud Console

  7. Click Set up.

What's next