- 監聽模型資料,並使用 Cloud Run 函式產生事件。
- 透過 Pub/Sub 事件管道傳送 Cloud Run 函式產生的事件。
支援的模型
下列模型提供 Cloud Run 函式事件產生和 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 函式會連結至模型,並監聽其輸出內容,做為後處理動作。您應傳回 AppPlatformCloudFunctionResponse
的 Cloud Run 函式。事件 (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 函式:
控制台
開啟 Vertex AI Vision 資訊主頁的「Applications」分頁。
在清單中選取應用程式名稱旁的「查看應用程式」
。在「事件通知」部分的「後置處理」清單中,選取現有的 Cloud Run 函式,或建立新的函式。
使用 Pub/Sub 啟用模型事件通知
設定 Cloud Run 函式來處理模型輸出內容並產生事件後,您可以使用 Pub/Sub 設定事件通知。如要讀取主題中的訊息,您也必須選擇並建立 Pub/Sub 訂閱項目。
控制台
開啟 Vertex AI Vision 資訊主頁的「Applications」分頁。
在清單中選取應用程式名稱旁的「查看應用程式」
。在「事件通知」部分,選取「設定事件通知」。
在隨即開啟的「Set up Pub/Sub for event notifications」(為事件通知設定 Pub/Sub) 選項視窗中,選擇現有的 Pub/Sub 主題,或建立新的主題。
在「頻率」欄位中,設定以秒為單位的整數值,代表可傳送同類型事件通知的間隔時間。
按一下「設定」。
後續步驟
- 如要瞭解如何部署應用程式來測試模型事件通知,請參閱「部署及取消部署應用程式」一文中的操作說明。
- 完成 Vertex AI Vision 人流分析應用程式與事件管理 程式碼研究室。
- 如要進一步瞭解 Pub/Sub 和 Cloud Run 函式,請參閱「什麼是 Pub/Sub?」和「Cloud Run 函式總覽」。