使用 Cloud Functions 和 Pub/Sub 启用模型事件通知

在 Vertex AI Vision 中,模型会从摄像头等设备接收媒体数据,对数据运行 AI 预测,并持续生成注释。通常,您会将这些处理后的数据发送到数据目的地(“数据接收器”),例如媒体仓库或 BigQuery,以便执行进一步的分析作业。不过,在某些情况下,您可能必须以不同的方式处理某些注释,或者注释需求具有时效性。与 Cloud Run 函数和 Pub/Sub 的集成可帮助您满足这些需求。

如需启用模型事件通知,您需要执行以下操作:

  1. 监听模型数据,并使用 Cloud Run 函数根据这些数据生成事件。
  2. 通过 Pub/Sub 事件通道发送 Cloud Run 函数生成的事件。

支持的模型

以下模型提供 Cloud Run functions 事件生成和 Pub/Sub 事件通知集成:

准备工作

  • 创建一个应用,其中至少包含一个数据流节点和一个受支持的模型节点。
  • 可选。安装 Vertex AI Vision SDK 并将数据提取到您的应用中。如果您未在设置事件通知之前执行此操作,则必须在设置事件通知后执行此操作。
  • 可选。创建要使用的 Cloud Run 函数。 如果您在配置 Cloud Run 函数来处理模型输出之前未创建 Cloud Run 函数,则必须在该过程中创建该函数。
  • 可选。创建要使用的 Pub/Sub 主题。如果您未在启用使用 Pub/Sub 的模型事件通知之前创建 Pub/Sub 主题,则必须在此过程中创建该主题。
  • 可选。选择并创建 Pub/Sub 订阅。如果您未在启用使用 Pub/Sub 的模型事件通知之前创建 Pub/Sub 订阅,则必须在启用后创建该订阅,才能从主题中读取消息。

配置 Cloud Run 函数以处理模型输出

如需触发基于事件的通知,您必须先设置 Cloud Run 函数来处理模型输出并生成事件。

您的 Cloud Run 函数会连接到模型,并监听其输出作为后处理操作。您应让 Cloud Run 函数返回 AppPlatformCloudFunctionResponse。系统会将事件 (appplatformeventbody) 发送到您在下一步中配置的 Pub/Sub 主题。

Cloud Run 函数示例(入住人数分析模型)

Cloud Run 函数示例

/**
* 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);
}

请按照以下说明将模型输出流发送到 Cloud Run 函数:

控制台

  1. 打开 Vertex AI Vision 信息中心的应用标签页。

    前往“应用”标签页

  2. 从列表中选择应用名称旁边的 查看应用

  3. 点击受支持的模型以打开模型详情侧边栏。

  4. 事件通知部分的后处理列表中,选择现有的 Cloud Run 函数或创建一个新函数。

    在 Cloud 控制台中选择后处理 Cloud Functions 函数图片

使用 Pub/Sub 启用模型事件通知

设置 Cloud Run 函数来处理模型输出并生成事件后,您可以使用 Pub/Sub 设置事件通知。如需读取主题中的消息,您还需要选择并创建 Pub/Sub 订阅

控制台

  1. 打开 Vertex AI Vision 信息中心的应用标签页。

    前往“应用”标签页

  2. 从列表中选择应用名称旁边的 查看应用

  3. 点击受支持的模型以打开模型详情侧边栏。

  4. 活动通知部分,选择设置活动通知

  5. 在随即打开的为事件通知设置 Pub/Sub 选项窗口中,选择现有的 Pub/Sub 主题或创建新主题。

  6. 频率字段中,为频率值设置一个整数值(以秒为单位),表示系统可以发送同一类型事件的通知的间隔时间。

    在 Cloud 控制台中设置事件通知图片

  7. 点击设置

后续步骤