- 使用 Cloud Run functions 監聽模型資料,並根據資料產生事件。
- 透過 Pub/Sub 事件管道,傳送 Cloud Run functions 產生的事件。
支援的模型
下列模型提供 Cloud Run functions 事件生成和 Pub/Sub 事件通知整合功能:
事前準備
- 建立應用程式,至少包含一個串流節點和一個支援的模型節點。
- 選用。安裝 Gemini Enterprise Agent Platform Vision SDK,並將資料擷取至應用程式。如果未在設定事件通知前完成這項操作,請務必在設定後完成。
- 選用。建立要使用的 Cloud Run 函式。如果您在設定 Cloud Run functions 處理模型輸出內容之前,尚未建立 Cloud Run 函式,則必須在該程序中建立。
- 選用。建立要使用的 Pub/Sub 主題。如果您在透過 Pub/Sub 啟用模型事件通知前,尚未建立 Pub/Sub 主題,則必須在該程序中建立。
- 選用。選擇並建立 Pub/Sub 訂閱項目。如果您未在啟用 Pub/Sub 模型事件通知前建立 Pub/Sub 訂閱項目,則必須在啟用後建立,才能讀取主題中的訊息。
設定 Cloud Run 函式來處理模型輸出內容
如要觸發事件通知,您必須先設定 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 函式:
控制台
開啟 Gemini Enterprise Agent Platform Vision 資訊主頁的「Applications」(應用程式) 分頁。
從清單中選取應用程式名稱旁的「 查看應用程式」。
按一下支援的模型,開啟模型詳細資料側邊面板。
在「Event notification」(事件通知) 專區的「post-processing」(後置處理) 清單中,選取現有的 Cloud Run 函式,或建立新的函式。

啟用 Pub/Sub 模型事件通知
設定 Cloud Run functions 來處理模型輸出內容並產生事件後,您可以使用 Pub/Sub 設定事件通知。如要讀取主題中的訊息,您也必須選擇並建立 Pub/Sub 訂閱項目。
控制台
開啟 Gemini Enterprise Agent Platform Vision 資訊主頁的「Applications」(應用程式) 分頁。
從清單中選取應用程式名稱旁的「 查看應用程式」。
按一下支援的模型,開啟模型詳細資料側邊面板。
在「Event notification」(事件通知) 區段中,選取「Set up event notification」(設定事件通知)。
在開啟的「Set up Pub/Sub for event notifications」(為事件通知設定 Pub/Sub) 選項視窗中,選擇現有的 Pub/Sub 主題,或建立新主題。
在「Frequency」(頻率) 欄位中,設定傳送相同類型事件通知的頻率值 (以秒為單位,整數值)。

按一下「設定」。
後續步驟
- 如要瞭解如何部署應用程式,以便測試模型事件通知,請參閱「部署及取消部署應用程式」的操作說明。
- 完成 Gemini Enterprise Agent Platform Vision Occupancy Analytics 應用程式 (含事件管理功能) 程式碼實驗室。
- 如要進一步瞭解 Pub/Sub 和 Cloud Run functions,請參閱「什麼是 Pub/Sub?」和「Cloud Run functions 總覽」。