- Cloud Run 関数を使用してモデルデータをリッスンし、イベントを生成します。
- Cloud Run functions によって生成されたイベントを Pub/Sub イベント チャネル経由で送信します。
サポートされているモデル
次のモデルでは、Cloud Run functions イベント生成と Pub/Sub イベント通知の統合が提供されます。
- 利用人数の分析モデル
- Vertex AI のカスタム トレーニング モデル
始める前に
- 少なくともストリームノードとサポートされているモデルノードを含むアプリを作成します。
- 省略可。Vertex AI Vision SDK をインストールし、アプリにデータを取り込む。イベント通知を設定する前にこれを行わない場合は、後で行う必要があります。
- 省略可。使用する Cloud Run 関数を作成します。モデル出力を処理するように Cloud Run 関数を構成する前に Cloud Run 関数を作成しない場合は、そのプロセス中に作成する必要があります。
- 省略可。使用する Pub/Sub トピックを作成します。Pub/Sub でモデルイベント通知を有効にする前に Pub/Sub トピックを作成していない場合は、そのプロセス中に作成する必要があります。
- 省略可。Pub/Sub サブスクリプションを選択して作成します。Pub/Sub でモデルイベント通知を有効にする前に Pub/Sub サブスクリプションを作成しなかった場合は、トピックからメッセージを読み取るために、後で作成する必要があります。
モデル出力を処理するように Cloud Run functions を構成する
イベントベースの通知をトリガーするには、まず、モデル出力を処理してイベントを生成する Cloud Run functions 関数を設定する必要があります。
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 関数に送信します。
Console
Vertex AI Vision ダッシュボードの [アプリケーション] タブを開きます。
リストからアプリケーションの名前の横にある
[アプリを表示] を選択します。サポートされているモデルをクリックして、モデルの詳細サイドパネルを開きます。
[イベント通知] セクションの [後処理] リストで、既存の Cloud Run 関数を選択するか、新しい関数を作成します。
Pub/Sub でモデルイベント通知を有効にする
モデル出力を処理してイベントを生成する Cloud Run 関数を設定したら、Pub/Sub でイベント通知を設定できます。トピックからメッセージを読み取るには、Pub/Sub サブスクリプションを選択して作成することも必要です。
Console
Vertex AI Vision ダッシュボードの [アプリケーション] タブを開きます。
リストからアプリケーション名の横にある
[アプリを表示] を選択します。サポートされているモデルをクリックして、モデルの詳細サイドパネルを開きます。
[イベント通知] セクションで、[イベント通知を設定] を選択します。
開いた [イベント通知用に Pub/Sub を設定する] オプション ウィンドウで、既存の Pub/Sub トピックを選択するか、新しいトピックを作成します。
[Frequency] フィールドに、同じタイプのイベントの通知を送信できる頻度(秒単位)の整数値を設定します。
[設定] をクリックします。
次のステップ
- アプリをデプロイしてモデルイベント通知をテストする手順については、アプリケーションのデプロイとデプロイ解除をご覧ください。
- Vertex AI Vision の占有状況分析アプリとイベント管理の Codelab を実施する。
- Pub/Sub と Cloud Run 関数の詳細については、Pub/Sub とはと Cloud Run 関数の概要をご覧ください。