- 使用 Cloud Run 函式監聽模型資料,並從中產生事件。
- 透過 Pub/Sub 事件管道傳送 Cloud Run 函式產生的事件。
支援的模型
下列模型提供 Cloud Run functions 事件產生和 Pub/Sub 事件通知整合功能:
事前準備
- 建立應用程式,其中至少包含串流節點和支援的模型節點。
- 選用。安裝 Gemini Enterprise Agent Platform Vision SDK,然後將資料匯入應用程式。如果未在設定事件通知前執行這項操作,請務必在設定後執行。
- 選用。建立要使用的 Cloud Run 函式。 如果您在設定 Cloud Run 函式來處理模型輸出內容之前,未建立 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 資訊主頁的「應用程式」分頁。
從清單中選取應用程式名稱旁的「查看應用程式」。
按一下支援的模型,開啟模型詳細資料側邊面板。
在「事件通知」部分的「後續處理」清單中,選取現有的 Cloud Run 函式,或建立新的函式。

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

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