Cloud Functions と Pub/Sub を使用してモデル イベント通知を有効にする

Gemini Enterprise Agent Platform Vision では、モデルはカメラなどのデバイスからメディアデータを受信し、データに対して AI 予測を実行して、アノテーションを継続的に生成します。多くの場合、処理されたデータは、メディア ウェアハウスや BigQuery などのデータ宛先(「データシンク」)に送信され、さらに分析ジョブが実行されます。ただし、一部のアノテーションを異なる方法で処理する必要がある場合や、アノテーションのニーズが時間的に制約される場合があります。Cloud Run functions と Pub/Sub との統合は、これらのニーズに対応するのに役立ちます。

モデルイベント通知を有効にするには、次の操作を行う必要があります。

  1. Cloud Run functions を使用してモデルデータをリッスンし、そこからイベントを生成する。
  2. Cloud Run functions で生成されたイベントを Pub/Sub イベント チャネル経由で送信します。

サポートされているモデル

次のモデルは、Cloud Run functions のイベント生成と Pub/Sub イベント通知の統合を提供します。

始める前に

モデル出力を処理するように Cloud Run functions を構成する

イベントベースの通知をトリガーするには、まず、モデル出力を処理してイベントを生成するように Cloud Run functions を設定する必要があります。

Cloud Run functions はモデルに接続し、後処理アクションとしてその出力をリッスンします。Cloud Run functions の関数は 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 functions の関数に送信するには、次の操作を行います。

コンソール

  1. Gemini Enterprise Agent Platform Vision ダッシュボードの [Applications] タブを開きます。

    [アプリケーション] タブに移動

  2. リストからアプリケーションの名前の横にある [アプリを表示] を選択します。

  3. サポートされているモデルをクリックして、モデルの詳細サイドパネルを開きます。

  4. [アクティビティ通知] セクションの [後処理] リストで、既存の Cloud Run 関数を選択するか、新しい関数を作成します。

    Cloud コンソールで後処理 Cloud Functions のイメージを選択する

Pub/Sub でモデルイベント通知を有効にする

モデル出力を処理してイベントを生成するように Cloud Run functions を設定したら、Pub/Sub を使用してアクティビティ通知を設定できます。トピックからメッセージを読み取るには、Pub/Sub サブスクリプションを選択して作成する必要もあります。

コンソール

  1. Gemini Enterprise Agent Platform Vision ダッシュボードの [Applications] タブを開きます。

    [アプリケーション] タブに移動

  2. リストからアプリケーションの名前の横にある [アプリを表示] を選択します。

  3. サポートされているモデルをクリックして、モデルの詳細サイドパネルを開きます。

  4. [アクティビティ通知] セクションで、[アクティビティ通知を設定] を選択します。

  5. [アクティビティ通知用の Pub/Sub を設定する] オプション ウィンドウが開いたら、既存の Pub/Sub トピックを選択するか、新しいトピックを作成します。

  6. [Frequency] フィールドに、同じタイプのイベントの通知を送信できる頻度値を秒単位で整数値で設定します。

    Cloud コンソールでアクティビティ通知の画像を設定する

  7. [設定] をクリックします。

次のステップ