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

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

如要啟用模型事件通知,請完成下列步驟:

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

支援的模型

下列模型提供 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. 在「事件通知」部分的「後續處理」清單中,選取現有的 Cloud Run 函式,或建立新的函式。

    在 Cloud 控制台中選取後續處理 Cloud Functions 映像檔

透過 Pub/Sub 啟用模型事件通知

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

控制台

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

    前往「Applications」(應用程式) 分頁

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

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

  4. 在「事件通知」部分,選取「設定事件通知」

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

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

    在 Cloud 控制台中設定事件通知圖片

  7. 按一下「設定」

後續步驟