配置日志记录

本文档介绍了如何配置 Model Armor 以记录以下操作:

  • 创建、更新或删除模板的操作
  • 用于清理用户提示或模型回答的操作

Model Armor 使用审核日志来记录管理和资源管理活动。如需了解详情,请参阅 Model Armor 审核日志记录

如需了解日志价格,请参阅 Cloud Logging 价格页面。您可能还需要支付 Model Armor 使用费,具体取决于处理的数据量;详情请参阅 Model Armor 定价

准备工作

请先完成以下任务,然后再完成本页面上的其余任务。

获取所需的权限

如需获得为 Model Armor 配置日志记录所需的权限,请让您的管理员为您授予 Model Armor 模板的 Model Armor Admin (roles/modelarmor.admin) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

启用 API

您必须先启用 Model Armor API,然后才能使用 Model Armor。

控制台

  1. Enable the Model Armor API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    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. 设置流量清理

    对于 Google 管理的 Model Context Protocol (MCP) 服务器,请通过下限设置来设置流量清理。如需了解详情,请参阅为 Google 和 Google Cloud 远程 MCP 服务器配置保护。 (预览版

    在模板中配置日志记录

    模板用于定义不同安全与防护类别的过滤条件和阈值。创建或更新 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 页面上,点击创建模板。 如需详细了解如何创建模板,请参阅创建 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

    如需运行此代码,请先设置 Python 开发环境,然后安装 Model Armor Python SDK

       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。

    在下限设置中配置日志记录

    下限设置可在您项目中的所有 Gemini 模型和 Google 管理的 Model Context Protocol (MCP) 服务器(预览版)中建立基准安全和安全过滤条件。更新 Model Armor 下限设置时,您可以指定是否记录 Model Armor 清理操作。

    您可以单独为 Vertex AI 和 Google 管理的 MCP 服务器启用清理操作的日志记录。启用此功能后,日志会包含提示和回答(对于 Vertex AI)或工具调用和工具回答(对于 MCP 服务器)、Model Armor 的评估结果以及其他元数据字段。

    控制台

    1. 在 Google Cloud 控制台中,前往 Model Armor 页面。

      前往 Model Armor

    2. 确认您正在查看的是已启用 Model Armor 的项目。

    3. 前往下限设置标签页。

    4. 日志部分,选择 Google 管理的 MCP

    5. 点击保存

    gcloud

    您可以使用以下标志之一来管理楼层设置中的清理操作日志记录。

    如需启用日志记录,请使用以下标志之一:

    • 对于 Vertex AI,请使用标志 --enable-vertex-ai-cloud-logging
    • 对于 Google 管理的 MCP 服务器,请使用标志 --enable-google-mcp-server-cloud-logging

    如需停用日志记录,请使用以下标志之一:

    • 对于 Vertex AI,请使用标志 --no-enable-vertex-ai-cloud-logging
    • 对于 Google 管理的 MCP 服务器,请使用标志 --no-enable-google-mcp-server-cloud-logging

    以下示例命令会为 Vertex AI 和 Google 管理的 MCP 服务器启用清理操作日志记录:

    gcloud model-armor floorsettings update \
    --full-uri='projects/PROJECT_ID/locations/global/floorSetting' \
    --enable-vertex-ai-cloud-logging \
    --enable-google-mcp-server-cloud-logging
    

    PROJECT_ID 替换为您的项目 ID。

    REST

    您可以使用 UpdateFloorSetting 方法更新楼层设置,以启用清理操作日志记录。使用此方法时,请务必将相应参数设置为 true 以启用日志记录:

    • 对于 Vertex AI,请将 aiPlatformFloorSetting.enableCloudLogging 设置为 true

    • 对于 Google 管理的 MCP 服务器,请将 googleMcpServerFloorSetting.enableCloudLogging 设置为 true

    以下示例命令会为 Vertex AI 和 Google 管理的 MCP 服务器启用清理操作日志记录:

    curl -X PATCH \
     -d '{ "aiPlatformFloorSetting":{ "enableCloudLogging": true}, "googleMcpServerFloorSetting":{ "enableCloudLogging": true}}' \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        "https://modelarmor.googleapis.com/v1/projects/PROJECT_ID/locations/global/floorSetting?updateMask=aiPlatformFloorSetting.enableCloudLogging,googleMcpServerFloorSetting.enableCloudLogging"
    

    PROJECT_ID 替换为您的项目 ID。

    Python

    如需运行此代码,请先设置 Python 开发环境安装 Model Armor Python SDK

    from google.cloud.modelarmor import v1 as modelarmor_v1
    from google.protobuf import field_mask_pb2
    
    # TODO: Initialize the ModelArmorClient, "client"
    # client = modelarmor_v1.ModelArmorClient()
    
    project_id = "PROJECT_ID"
    location = "global"
    
    floor_setting_name = f"projects/{project_id}/locations/{location}/floorSetting"
    
    request = modelarmor_v1.UpdateFloorSettingRequest(
        floor_setting=modelarmor_v1.FloorSetting(
            name=floor_setting_name,
            ai_platform_floor_setting=modelarmor_v1.FloorSetting.AiPlatformFloorSetting(
                enable_cloud_logging=True
            ),
            google_mcp_server_floor_setting=modelarmor_v1.FloorSetting.GoogleMcpServerFloorSetting(
                enable_cloud_logging=True
            ),
        ),
        update_mask=field_mask_pb2.FieldMask(
            paths=["ai_platform_floor_setting.enable_cloud_logging", "google_mcp_server_floor_setting.enable_cloud_logging"]
        )
    )
    
    try:
        response = client.update_floor_setting(request=request)
        print("Successfully updated floor settings logging.")
        print(response)
    except Exception as e:
        print(f"An error occurred: {e}")
    
    

    PROJECT_ID 替换为您的项目 ID。

    查看日志

    如需查看 Model Armor 日志,您可以使用 Logging 中的 Logs Explorer,请按以下步骤操作:

    1. 在 Google Cloud 控制台中前往 Logs Explorer。如需了解详情,请参阅使用日志浏览器查看日志
    2. 按服务名称 modelarmor.googleapis.com 过滤日志。
    3. 查找与您在模板中启用的操作相关的条目。如需查看所有服务名称和受监控的资源类型的列表,请参阅受监控的资源和服务

    过滤 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: Google Cloud 项目 ID。
    • CLIENT_NAME:您的客户端的名称。
    • CORRELATION_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:从模型收到的回答。