設定 Model Armor 的記錄功能

本文說明如何設定 Model Armor,記錄下列作業:

  • 建立、更新或刪除範本的操作
  • 清除使用者提示或模型回覆的作業

Model Armor 會使用稽核記錄,記錄管理和資源管理活動。詳情請參閱 Model Armor 稽核記錄總覽

事前準備

請先完成這些工作,再完成本頁面上的其餘工作。

取得必要權限

如要取得設定 Model Armor 記錄功能所需的權限,請要求管理員為您授予 Model Armor 範本的Model Armor 管理員 (roles/modelarmor.admin) 身分與存取權管理角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

您或許還可透過自訂角色或其他預先定義的角色取得必要權限。

啟用 API

您必須先啟用 Model Armor API,才能使用 Model Armor。

主控台

  1. Enable the Model Armor API.

    Enable the API

  2. 選取要啟用 Model Armor 的專案。

gcloud

開始前,請使用 Google Cloud CLI 搭配 Model Armor API 執行下列步驟:

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. 執行下列指令,為 Model Armor 服務設定 API 端點。

    gcloud config set api_endpoint_overrides/modelarmor "https://modelarmor.LOCATION.rep.googleapis.com/"

    LOCATION 改為要使用 Model Armor 的區域。

  3. 執行下列指令,啟用 Model Armor。

      gcloud services enable modelarmor.googleapis.com --project=PROJECT_ID
       

    PROJECT_ID 替換為專案 ID。

    在範本中設定記錄功能

    範本會定義不同安全和安全性類別的篩選器和門檻。建立或更新 Model Armor 範本時,您可以指定 Model Armor 是否要記錄特定作業。在範本中繼資料中使用下列標記:

    • log_template_operations:布林值,可啟用範本建立、更新、讀取及刪除作業的記錄。
    • log_sanitize_operations:布林值,可啟用記錄清除作業。記錄內容包括提示和回覆、Model Armor 的評估結果,以及其他中繼資料欄位。

    主控台

    1. 前往 Google Cloud 控制台的「Model Armor」頁面。

      前往 Model Armor

    2. 確認您正在查看已啟用 Model Armor 的專案。

    3. 在「Model Armor」頁面中,按一下「Create Template」(建立範本)。 如要進一步瞭解如何建立範本,請參閱「建立 Model Armor 範本」。

    4. 在「設定記錄」部分中,選取要設定記錄的作業。

    5. 點選「建立」

    REST

      curl -X POST \
          -d '{ "filterConfig": {}, "templateMetadata": { "logTemplateOperations": true, "logSanitizeOperations": true } }' \
          -H "Content-Type: application/json" \
          -H "Authorization: Bearer $(gcloud auth print-access-token)" \
          "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates?template_id=TEMPLATE_ID"
    

    更改下列內容:

    • PROJECT_ID:範本所屬專案的 ID。
    • LOCATION:範本的位置。
    • TEMPLATE_ID:範本的 ID。

    Python

       request = modelarmor_v1.CreateTemplateRequest(
         parent="projects/PROJECT_ID/locations/LOCATION",
         template_id="TEMPLATE_ID",
         template={
            "name": "projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID",
            "filter_config": {},
            "template_metadata": {
               "log_template_operations": True,
               "log_sanitize_operations": True
            }
         }
       )
       response = client.create_template(request=request)
       

    更改下列內容:

    • PROJECT_ID:範本所屬專案的 ID。
    • LOCATION:範本的位置。
    • TEMPLATE_ID:範本的 ID。

    查看記錄

    使用 Cloud Logging 中的記錄檔探索工具存取 Model Armor 記錄。 詳情請參閱「使用記錄檔探索工具查看記錄檔」。依服務名稱 modelarmor.googleapis.com 篩選。找出與範本中啟用作業相關的項目。如需所有服務名稱和受監控資源類型的清單,請參閱受監控資源和服務

    篩選 Model Armor 記錄

    使用記錄標籤篩選 Model Armor 記錄,找出清除作業和範本記錄。

    在記錄檔探索工具中執行下列查詢,篩選出清除作業記錄。

    jsonPayload.@type="type.googleapis.com/google.cloud.modelarmor.logging.v1.SanitizeOperationLogEntry"
    

    如要進一步篩選清除作業記錄,可以在查詢中指定專案 ID、用戶端名稱或關聯 ID。

    • 使用專案 ID:

      jsonPayload.@type="type.googleapis.com%2Fgoogle.cloud.modelarmor.logging.v1.SanitizeOperationLogEntry";project=PROJECT_ID
      
    • 使用客戶名稱:

      jsonPayload.@type="type.googleapis.com/google.cloud.modelarmor.logging.v1.SanitizeOperationLogEntry"
      labels."modelarmor.googleapis.com/client_name"="CLIENT_NAME"
      
    • 使用關聯性 ID:

      labels."modelarmor.googleapis.com/client_correlation_id"="CORRELATION_ID"
      

    更改下列內容:

    • PROJECT_ID:專案 ID。 Google Cloud
    • CLIENT_NAME:用戶端名稱。
    • CORRELATION_ID:您為特定要求產生的專屬 ID。

    關聯記錄和相關事件

    如要將記錄和事件與特定互動建立關聯,您需要用戶端關聯性 ID。這是您產生的專屬 ID (例如 UUID),用於追蹤系統中的特定要求。如要在 curl 標頭中設定用戶端關聯性 ID,請使用 -H 選項,在要求中加入自訂標頭。範例格式如下:

    curl -X POST -d  '{"userPromptData": { "text": 'USER_PROMPT' } }' \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        -H "MA-Client-Correlation-Id: $uuid" \
        "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeUserPrompt"
    
    curl -X POST \
        -d  '{"modelResponseData": { "text": 'MODEL_RESPONSE' }, "userPrompt": 'USER_PROMPT' }' \
        -H "Content-Type: application/json" \
        -H "MA-Client-Correlation-Id: $uuid" \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeModelResponse"
    

    更改下列內容:

    • PROJECT_ID:範本所屬專案的 ID。
    • LOCATION:範本的位置。
    • TEMPLATE_ID:範本的 ID。
    • USER_PROMPT:提供給模型的提示。
    • MODEL_RESPONSE:模型傳回的回覆。