Model Armor 會根據設定的篩選可信度等級檢查提示和回覆。 本頁面詳細說明如何清除提示和回應。
開始之前,請按照「建立範本」一文中的操作說明建立範本。
清除提示
Model Armor 會清除文字和檔案格式的提示。
文字提示
REST
使用這項指令,在 Model Armor 中清除文字提示。使用我們在步驟 7 中建立的範本 (ma-template-id-1234) -「建立範本」的「進階 Sensitive Data Protection」部分。
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 位數的號碼,核發對象是美國公民、永久居民和臨時居民。這項偵測工具不會比對出任何均為 0 的數字群組 (即 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 設定
您可以使用進階 Sensitive Data Protection 設定,透過 Sensitive Data Protection 範本篩選 LLM 提示和回覆。這樣一來,您就能使用 Sensitive Data Protection 功能,而不僅限於基本 Sensitive Data Protection 設定提供的 infoType。
如要在 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 使用者角色 (roles/dlp.user
) 和 DLP 讀取者角色 (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。
後續步驟
- 瞭解 Model Armor 總覽。
- 瞭解 Model Armor 的重要概念。
- 瞭解 Model Armor 底限設定。
- 瞭解 Model Armor 範本。
- 排解 Model Armor 問題。