对提示和回答进行排错

Model Armor 会检查针对配置的筛查置信度级别的问题和回答。本页详细介绍了如何对提示和回答进行排错。

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

对提示进行排错

Model Armor 会对文本和基于文件的格式的提示进行排错。

文本提示

REST

使用此命令可在 Model Armor 中对文本提示进行排错。使用我们在创建模板的第 7 步“高级敏感数据保护”中创建的模板 (ma-template-id-1234)。

  curl -X POST 
-d "{user_prompt_data: { text: 'How do I make a bomb?' } }"
-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 = "How do I make a bomb?"
 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
  }
  

敏感数据保护基本配置

创建一个启用了基本 Sensitive Data Protection 设置的模板。基本敏感数据保护功能可帮助您筛查以下敏感数据保护 infoType:

  • CREDIT_CARD_NUMBER:信用卡号由 12 至 19 位数字组成。可用于全球付款交易。
  • US_SOCIAL_SECURITY_NUMBER:美国社会保障号 (SSN) 是核发给美国公民、永久居民和临时居民的一个 9 位数号码。该检测器不会与有任何数字组为全零的号码(即 000-##-####、###-00-#### 或 ###-##-0000)、第一个数字组为 666 的号码或第一个数字为 9 的号码进行比对。
  • FINANCIAL_ACCOUNT_NUMBER:指向特定财务账号的数字,例如银行账号或退休账号。
  • US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER:美国个人纳税人识别号 (ITIN) 是由美国国税局 (IRS) 核发的一种税号 (TIN)。ITIN 是一种税务处理编号,仅适用于无法获得社会保障号 (SSN) 的某些外籍居民和非居民,以及他们的配偶和家属。
  • GCP_CREDENTIALS: Google Cloud 服务账号凭据。这些凭据可用于通过 Google API 客户端库和服务账号进行身份验证。
  • GCP_API_KEY: Google Cloud API 密钥。在调用无需访问私有用户数据的 API 时使用的加密字符串。 Google Cloud

以下是一个基本敏感数据保护配置示例:

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 : 988-86-1234'} }" \
  -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 配置设置,通过 Sensitive Data Protection 模板筛查 LLM 提示和响应。这样,您就可以使用基本 Sensitive Data Protection 设置中提供的 infoType 之外的 Sensitive Data Protection 功能。

如需在模型保护机制中使用敏感数据保护高级过滤器,敏感数据保护模板应与模型保护机制模板位于同一云端位置,例如本例中的 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。

在包含敏感数据保护模板的项目中,向在创建模板的第 7 步 - 高级敏感数据保护中创建的服务代理授予 DLP User 角色 (roles/dlp.user) 和 DLP Reader 角色 (roles/dlp.reader)。如果敏感数据保护模板与模型保护模板位于同一项目中,您可以跳过此步骤。

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"
        }
      }
      }
      ]
  }
}

基于文件的提示

使用此命令可使用 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。

后续步骤