对提示和回答进行排错

Model Armor 会针对已配置的筛选置信度级别检查提示和回答。本页面详细介绍了如何清理提示和回答。

在开始之前,请按照创建模板中的说明创建模板。

清理提示

Model Armor 会清理文本和基于文件的格式的提示。

文本提示

REST

使用此命令可在 Model Armor 中清理文本提示。使用我们在创建模板的第 7 步 - 高级 Sensitive Data Protection 中创建的模板 (ma-template-id-1234)。

  curl -X POST 
-d "{user_prompt_data: { text: '[UNSAFE TEXT]' } }"
-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:sanitizeUserPrompt"

替换以下内容:

  • PROJECT_ID 是模板的项目 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

这会返回以下响应。请注意,MATCH_FOUND 属于“危险”类别。

  {
  "sanitizationResult": {
    "filterMatchState": "MATCH_FOUND",
    "invocationResult": "SUCCESS",
    "filterResults": {
      "csam": {
        "csamFilterFilterResult": {
          "executionState": "EXECUTION_SUCCESS",
          "matchState": "NO_MATCH_FOUND"
        }
      },
      "malicious_uris": {
        "maliciousUriFilterResult": {
          "executionState": "EXECUTION_SUCCESS",
          "matchState": "NO_MATCH_FOUND"
        }
      },
      "rai": {
        "raiFilterResult": {
          "executionState": "EXECUTION_SUCCESS",
          "matchState": "MATCH_FOUND",
          "raiFilterTypeResults": {
            "sexually_explicit": {
              "matchState": "NO_MATCH_FOUND"
            },
            "hate_speech": {
              "matchState": "NO_MATCH_FOUND"
            },
            "harassment": {
              "matchState": "NO_MATCH_FOUND"
            },
            "dangerous": {
              "matchState": "MATCH_FOUND"
            }
          }
        }
      },
      "pi_and_jailbreak": {
        "piAndJailbreakFilterResult": {
          "executionState": "EXECUTION_SUCCESS",
          "matchState": "MATCH_FOUND"
        }
      },
      "sdp": {
        "sdpFilterResult": {
          "inspectResult": {
            "executionState": "EXECUTION_SUCCESS",
            "matchState": "NO_MATCH_FOUND"
          }
        }
      }
    }
  }
  }
  

Python

如需运行此命令,请先在 Python 中初始化 Model Armor 客户端

 user_prompt_data = modelarmor_v1.DataItem()
 user_prompt_data.text = "[UNSAFE TEXT]"
 request = modelarmor_v1.SanitizeUserPromptRequest(
    name="projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID",
    user_prompt_data=user_prompt_data,
 )
 response = client.sanitize_user_prompt(request=request)
 

替换以下内容:

  • PROJECT_ID 是模板的项目 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

这会返回以下响应。

  sanitization_result {
    filter_match_state: MATCH_FOUND
    filter_results {
      key: "rai"
      value {
        rai_filter_result {
          execution_state: EXECUTION_SUCCESS
          match_state: MATCH_FOUND
          rai_filter_type_results {
            key: "dangerous"
            value {
              confidence_level: HIGH
              match_state: MATCH_FOUND
            }
          }
        }
      }
    }
    filter_results {
      key: "pi_and_jailbreak"
      value {
        pi_and_jailbreak_filter_result {
          execution_state: EXECUTION_SUCCESS
          match_state: MATCH_FOUND
          confidence_level: HIGH
        }
      }
    }
    filter_results {
      key: "malicious_uris"
      value {
        malicious_uri_filter_result {
          execution_state: EXECUTION_SUCCESS
          match_state: NO_MATCH_FOUND
        }
      }
    }
    filter_results {
      key: "csam"
      value {
        csam_filter_filter_result {
          execution_state: EXECUTION_SUCCESS
          match_state: NO_MATCH_FOUND
        }
      }
    }
    invocation_result: SUCCESS
  }
  

在启用多语言检测的情况下清理文本提示

通过为各个请求将 enable_multi_language_detection 标志设置为 true,按请求启用多语言检测。您可以选择性地指定源语言,以获得更准确的结果。 如果未指定源语言,系统会自动检测源语言,以提供多语言支持。

使用以下命令在请求级别启用多语言检测时,清理 Model Armor 中的文本提示。

curl -X POST \
-d  "{user_prompt_data: { text: '[UNSAFE TEXT]' }, multi_language_detection_metadata: { enable_multi_language_detection: true , source_language: 'jp'} }" \
-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:sanitizeUserPrompt"

替换以下内容:

  • PROJECT_ID 是模板的项目 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

基本 Sensitive Data Protection 配置

创建一个启用了基本 Sensitive Data Protection 设置的模板。基本 Sensitive Data Protection 可帮助您筛选一组固定的 Sensitive Data Protection infoType。

所有区域的提示中都会扫描以下 Sensitive Data Protection infoType:

  • CREDIT_CARD_NUMBER:信用卡号由 12 至 19 位数字组成,可用于全球付款交易。
  • FINANCIAL_ACCOUNT_NUMBER:指向特定金融账户的编号。例如,银行账号或退休账号。
  • GCP_CREDENTIALS: Google Cloud 服务账号凭证。这些凭证可用于通过 Google API 客户端库和服务账号进行身份验证。
  • GCP_API_KEY: Google Cloud API密钥。在调用不需要访问私有用户数据的 Google Cloud API 时使用的加密字符串。
  • PASSWORD:配置、代码和其他文本中的明文密码。

美国区域的提示中都会扫描以下额外的 Sensitive Data Protection infoType:

  • US_SOCIAL_SECURITY_NUMBER:美国社会保障号 (SSN) 是核发给美国公民、永久居民和临时居民的一个 9 位数号码。此检测器不会与任何数字群组中全为零的数字(即 000-###-####、###-00-#### 或 ###-####-0000)、第一个数字群组中为 666 的数字或第一个数字为 9 的数字进行比对。
  • US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER:美国个人纳税人识别号码 (ITIN) 是由美国国税局 (IRS) 核发的一种税号 (TIN)。ITIN 是一种税务处理编号,仅适用于无法获得社会保障号 (SSN) 的某些居民外籍人士和非居民外籍人士,以及他们的配偶和家属。

以下是基本 Sensitive Data Protection 配置示例:

gcloud

gcloud model-armor templates create template-name \
  --location=location \
  --basic-config-filter-enforcement=enabled

REST

export FILTER_CONFIG_SDP_BASIC='{
  "filterConfig": {
    "sdpSettings": {
      "basicConfig": {
        "filterEnforcement": "ENABLED"
      }
    }
  }
}'

curl -X POST \
  -d  $FILTER_CONFIG_SDP_BASIC \
  -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=sdp_basic"

替换以下内容:

  • 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": {
        "sdp_settings": {
            "basic_config": {
                "filter_enforcement": "ENABLED"
            }
        }
      },
    }
)
response = client.create_template(request=request)

替换以下内容:

  • PROJECT_ID 是模板所属项目的 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

使用创建的模板来筛选提示。示例如下:

curl -X POST \
  -d  "{ user_prompt_data: { 'text': 'can you remember my ITIN : ###-##-####'} }" \
  -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/sdp_basic:sanitizeUserPrompt"

此示例会返回以下响应:

{
  "sanitizationResult": {
      "filterMatchState": "MATCH_FOUND",
      "invocationResult": "SUCCESS",
      "filterResults": [
        {
          "csamFilterFilterResult": {
            "executionState": "EXECUTION_SUCCESS",
            "matchState": "NO_MATCH_FOUND"
          }
        },
        {
      "sdpFilterResult": {
        "inspectResult": {
          "executionState": "EXECUTION_SUCCESS",
          "matchState": "MATCH_FOUND",
          "findings": [
            {
              "infoType": "US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER",
              "likelihood": "LIKELY",
              "location": {
                "byteRange": {
                  "start": "26",
                  "end": "37"
                },
                "codepointRange": {
                  "start": "26",
                  "end": "37"
                }
              }
            }
          ]
        }
       }
      }
    ]
  }
}

高级 Sensitive Data Protection 配置

借助 Model Armor,您可以使用高级 Sensitive Data Protection 配置设置,通过 Sensitive Data Protection 模板来筛选 LLM 提示和回答。这样,您就可以使用基本 Sensitive Data Protection 设置中提供的 infoType 之外的 Sensitive Data Protection 功能。

如需在 Model Armor 中使用 Sensitive Data Protection 高级过滤器,Sensitive Data Protection 模板应位于与 Model Armor 模板相同的云位置,例如本例中的 us-central1

gcloud

gcloud model-armor templates create template-name
--location=LOCATION \
--advanced-config-inspect-template="path/to/template" \

REST

  export FILTER_CONFIG_SDP_ADV='{
    "filterConfig": {
      "sdpSettings": {
        "advancedConfig": {
          "deidentifyTemplate": "projects/PROJECT_ID/locations/LOCATION/deidentifyTemplates/deidentify-ip-address",
          "inspectTemplate": "projects/PROJECT_ID/locations/LOCATION/inspectTemplates/inspect-ip-address"
        }
      }
    }
  }'

 curl -X POST \
 -d  $FILTER_CONFIG_SDP_ADV \
 -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=sdp_advanced"

# Result of CreateTemplate
{
  "name": "projects/PROJECT_ID/locations/LOCATION/templates/all-filters-test",
  "createTime": "2024-12-16T17:08:19.626693819Z",
  "updateTime": "2024-12-16T17:08:19.626693819Z",
   "filterConfig": {
      "sdpSettings": {
        "advancedConfig": {
          "deidentifyTemplate":  "projects/PROJECT_ID/locations/LOCATION/deidentifyTemplates/deidentify-ip-address",
          "inspectTemplate": "projects/PROJECT_ID/locations/LOCATION/inspectTemplates/inspect-ip-address"
        }
      }
    }
},
service_agent_email: "service-PROJECT_NUMBER@gcp-sa-modelarmor.iam.gserviceaccount.com"

替换以下内容:

  • PROJECT_ID 是模板所属项目的 ID。
  • LOCATION 是模板的位置。
  • PROJECT_NUMBER 是服务账号的名称。

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": {
          "sdp_settings": {
            "advanced_config": {
                "inspect_template": "projects/PROJECT_ID/locations/LOCATION/inspectTemplates/inspect-ip-address",
                "deidentify_template": "projects/PROJECT_ID/locations/LOCATION/deidentifyTemplates/deidentify-ip-address"
            }
          }
        },
      }
)
response = client.create_template(request=request)

替换以下内容:

  • PROJECT_ID 是模板所属项目的 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

在包含 Sensitive Data Protection 模板的项目中,将 DLP User 角色 (roles/dlp.user) 和 DLP Reader 角色 (roles/dlp.reader) 授予作为创建模板的第 7 步 - 高级 Sensitive Data Protection 的一部分创建的服务代理。如果 Sensitive Data Protection 模板与 Model Armor 模板位于同一项目中,您可以跳过此步骤。

gcloud projects add-iam-policy-binding PROJECT_ID \
  --member serviceAccount:service-PROJECT_NUMBER@gcp-sa-modelarmor.iam.gserviceaccount.com --role roles/dlp.user

gcloud projects add-iam-policy-binding PROJECT_ID \
  --member serviceAccount:service-PROJECT_NUMBER@gcp-sa-modelarmor.iam.gserviceaccount.com --role roles/dlp.reader

替换以下内容:

  • PROJECT_ID 替换为模板所属项目的 ID。
  • PROJECT_NUMBER 是服务账号的名称。

使用创建的模板来筛选提示。示例如下:

curl -X POST \
  -d  "{ user_prompt_data: { 'text': 'is there anything malicious running on 1.1.1.1?'} }" \
  -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:sanitizeUserPrompt"

替换以下内容:

  • PROJECT_ID 是模板所属项目的 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

此示例会返回以下响应:

{
  "sanitizationResult": {
    "filterMatchState": "MATCH_FOUND",
    "invocationResult": "SUCCESS",
      "filterResults": [
      {
        "csamFilterFilterResult": {
          "executionState": "EXECUTION_SUCCESS",
          "matchState": "NO_MATCH_FOUND"
        }
      },
      {
      "sdpFilterResult": {
        "deidentifyResult": {
          "executionState": "EXECUTION_SUCCESS",
          "matchState": "MATCH_FOUND",
          "data": {
            "text": "is there anything malicious running on [IP_ADDRESS]?"
          },
            "transformedBytes": "7",
            "infoTypes": ["IP_ADDRESS"]
        }
      }
      }
      ]
  }
}

基于文件的提示

使用此命令可通过 Model Armor 清理文件格式的用户提示。文件需要以 Base64 编码格式传递。

curl -X POST \
  -d "$(jq -n \
    --arg data "$(base64 -w 0 -i sample.pdf)" \
  '{userPromptData: {byteItem: {byteDataType: "PDF", byteData: $data}}}')" \
  -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:sanitizeUserPrompt"

替换以下内容:

  • PROJECT_ID 是模板所属项目的 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

清理模型响应

以下是用于在 Model Armor 中清理模型回答的命令示例。

REST

 curl -X POST 
-d "{model_response_data: { text: 'It might hurt and cause pain' } }"
-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:sanitizeModelResponse"

此示例会返回以下响应:

 {
 "sanitizationResult": {
   "filterMatchState": "MATCH_FOUND",
    "invocationResult": "SUCCESS",
      "filterResults": {
        "rai": {
          "raiFilterResult": {
            "executionState": "EXECUTION_SUCCESS",
            "matchState": "MATCH_FOUND",
            "raiFilterTypeResults": {
        "dangerous": {
          "confidenceLevel": "MEDIUM_AND_ABOVE",
          "matchState": "MATCH_FOUND"
        },
        "sexually_explicit": {
          "matchState": "NO_MATCH_FOUND"
        },
        "hate_speech": {
          "matchState": "NO_MATCH_FOUND"
        },
        "harassment": {
          "matchState": "NO_MATCH_FOUND"
        }
      }
    }
  },
  "pi_and_jailbreak": {
    "piAndJailbreakFilterResult": {
      "executionState": "EXECUTION_SUCCESS",
      "matchState": "NO_MATCH_FOUND"
      }
  },
 "csam": {
    "csamFilterFilterResult": {
      "executionState": "EXECUTION_SUCCESS",
      "matchState": "NO_MATCH_FOUND"
    }
  },
  "malicious_uris": {
    "maliciousUriFilterResult": {
      "executionState": "EXECUTION_SUCCESS",
      "matchState": "NO_MATCH_FOUND"
    }
  },
  }
 }
 }
 

Python

如需运行此命令,请先在 Python 中初始化 Model Armor 客户端

 model_response_data = modelarmor_v1.DataItem()
 model_response_data.text = "It might hurt and cause pain"
 request = modelarmor_v1.SanitizeModelResponseRequest(
    name="projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID",
    model_response_data=model_response_data,
 )
 response = client.sanitize_model_response(request=request)
 

替换以下内容:

  • PROJECT_ID 是模板的项目 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

在启用多语言检测的情况下清理模型回答

通过为各个回应将 enable_multi_language_detection 标志设置为 true,按请求启用多语言检测。 您可以选择性地指定源语言,以获得更准确的结果。 如果未指定源语言,系统会自动检测源语言,以提供多语言支持。

curl -X POST \
-d  "{model_response_data: { text: '[UNSAFE TEXT]' }, multi_language_detection_metadata: { enable_multi_language_detection: true , source_language: 'jp' } }" \
-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:sanitizeModelResponse"

替换以下内容:

  • PROJECT_ID 是模板所属项目的 ID。
  • LOCATION 是模板的位置。
  • TEMPLATE_ID 是模板的 ID。

后续步骤