プロンプトとレスポンスをサニタイズする

Model Armor は、構成されたスクリーニングの信頼レベルに基づいてプロンプトとレスポンスをチェックします。このページでは、プロンプトとレスポンスをサニタイズする方法について詳しく説明します。

始める前に、テンプレートを作成するの手順に沿ってテンプレートを作成します。

プロンプトをサニタイズする

Model Armor は、テキスト形式とファイルベースの形式でプロンプトをサニタイズします。

テキスト プロンプト

このコマンドを使用して、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"
         }
       }
     }
   }
 }
}

機密データ保護の基本的な構成

基本的な 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

以下に、基本的な Sensitive Data Protection 構成の例を示します。

gcloud

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

REST API

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。

作成したテンプレートを使用して、プロンプトをスクリーニングします。次に例を示します。

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

機密データ保護の詳細な構成

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 API

  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: テンプレートのロケーション。

Sensitive Data Protection テンプレートを含むプロジェクトで、テンプレートを作成するのステップ 7 - 高度な Sensitive Data Protection の一部として作成されたサービス エージェントに、DLP ユーザーのロールroles/dlp.user)と DLP 読み取り者のロールroles/dlp.reader)を付与します。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 に置き換えます。

作成したテンプレートを使用して、プロンプトをスクリーニングします。次に例を示します。

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 でモデル レスポンスをサニタイズするコマンドの例を次に示します。

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

次のステップ