使用 Cloud Functions 和 Pub/Sub 啟用模型事件通知

在 Gemini Enterprise Agent Platform Vision 中,模型會接收攝影機等裝置的媒體資料、對資料執行 AI 預測,並持續產生註解。您通常會將處理過的資料傳送至資料目的地 (「資料接收器」),例如媒體倉儲或 BigQuery,以便進一步執行分析工作。不過,您可能需要以不同方式處理某些註解,或註解需求有時效性。與 Cloud Run functions 和 Pub/Sub 整合,可協助您滿足這些需求。

如要啟用模型事件通知,請按照下列步驟操作:

  1. 使用 Cloud Run functions 監聽模型資料,並根據資料產生事件。
  2. 透過 Pub/Sub 事件管道,傳送 Cloud Run functions 產生的事件。

支援的模型

下列模型提供 Cloud Run functions 事件生成和 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 函式:

控制台

  1. 開啟 Gemini Enterprise Agent Platform Vision 資訊主頁的「Applications」(應用程式) 分頁。

    前往「應用程式」分頁

  2. 從清單中選取應用程式名稱旁的「 查看應用程式」。

  3. 按一下支援的模型,開啟模型詳細資料側邊面板。

  4. 在「Event notification」(事件通知) 專區的「post-processing」(後置處理) 清單中,選取現有的 Cloud Run 函式,或建立新的函式。

    在 Cloud 控制台中選取後置處理 Cloud 函式映像檔

啟用 Pub/Sub 模型事件通知

設定 Cloud Run functions 來處理模型輸出內容並產生事件後,您可以使用 Pub/Sub 設定事件通知。如要讀取主題中的訊息,您也必須選擇並建立 Pub/Sub 訂閱項目。

控制台

  1. 開啟 Gemini Enterprise Agent Platform Vision 資訊主頁的「Applications」(應用程式) 分頁。

    前往「應用程式」分頁

  2. 從清單中選取應用程式名稱旁的「 查看應用程式」。

  3. 按一下支援的模型,開啟模型詳細資料側邊面板。

  4. 在「Event notification」(事件通知) 區段中,選取「Set up event notification」(設定事件通知)。

  5. 在開啟的「Set up Pub/Sub for event notifications」(為事件通知設定 Pub/Sub) 選項視窗中,選擇現有的 Pub/Sub 主題,或建立新主題。

  6. 在「Frequency」(頻率) 欄位中,設定傳送相同類型事件通知的頻率值 (以秒為單位,整數值)。

    Cloud 控制台中的「設定事件通知」圖片

  7. 按一下「設定」。

後續步驟