建立 Model Armor 範本
主控台
前往 Google Cloud 控制台的「Model Armor」頁面。
確認您正在查看已啟用 Model Armor 的專案。
在「Model Armor」頁面中,按一下「Create Template」(建立範本)。 系統會顯示「建立範本」頁面。
指定範本 ID。範本 ID 可以使用英文字母、數字或連字號。長度不得超過 63 個字元,且不得包含空格或以連字號開頭。
選取要執行 Model Armor 範本的區域,地區一經設定即無法變更。
選用:新增標籤。標籤是鍵/值組合,可用於將相關範本分組。
在「偵測」部分,設定下列偵測設定:
惡意網址偵測:找出用於危害使用者或系統的網址。這些網址可能會通往網路釣魚網站、誘導下載惡意軟體或進行其他網路攻擊。詳情請參閱「惡意網址偵測」一文。
提示詞注入和越獄偵測:偵測提示中的惡意內容和越獄嘗試。如要強制執行更嚴格的設定,請將信心水準設為「低與以上」**,這樣就能偵測到多數可能屬於提示詞注入和越獄嘗試的內容。詳情請參閱「提示詞注入和越獄偵測」。
機密資料保護:偵測私密/機密資料,防止意外曝光或遭到提示詞注入等攻擊。詳情請參閱「Sensitive Data Protection」。
您可以設定基本或進階的 Sensitive Data Protection。
基本:使用預先定義的 infoType 偵測機密/私密資料類型。 如要進一步瞭解預先定義的 infoType,請參閱基本 Sensitive Data Protection 設定。
進階 - 使用 Sensitive Data Protection 服務中定義的檢查範本,做為機密/私密資料 infoType 的單一來源。
如果選取「進階敏感資料保護」,您必須指定下列參數:
檢查範本: 用於儲存檢查掃描工作設定資訊的範本,包括要使用的預先定義或自訂偵測工具。 請按照下列格式輸入範本名稱:
projects/projectName/locations/locationID/inspectTemplates/templateName
選用:去識別化範本:用於儲存去識別化工作設定資訊的範本,包括 infoType 和結構化資料集轉換。請按照下列格式輸入去識別化範本的 ID:
projects/projectName/locations/locationID/deidentifyTemplates/templateName
確認 Sensitive Data Protection 中存在檢查範本和去識別化範本。如果範本位於其他專案,則必須將該專案的 DLP 使用者角色 (
roles/dlp.user
) 和 DLP 讀取者角色 (roles/dlp.reader
) 授予 Model Armor 服務代理程式。
在「Responsible AI」部分,你可以為每個內容篩選器設定信心水準。信心水準代表發現項目與某個內容篩選器類型相符的可能性。可能的值包括:
- 無:不偵測任何內容類型。
- 低與以上:偵測信心水準為低、中或高的內容。
- 中等以上:偵測信心水準為中或高的內容。
- 高:偵測信心水準為高的內容。
如要強制執行更嚴格的設定,請將信心水準設為「低與以上」,這樣就能偵測到多數屬於該內容篩選器類型的內容。您也可以一次選取所有內容類型的信心水準。
點選「建立」。
gcloud
執行下列指令:
gcloud model-armor templates create TEMPLATE_ID --location LOCATION
更改下列內容:
LOCATION
是範本的位置。TEMPLATE_ID
是範本的 ID。
以下是所有篩選條件的範例。
gcloud model-armor templates create --location LOCATION TEMPLATE_ID
--rai-settings-filters='[{ "filterType": "HATE_SPEECH", "confidenceLevel": "MEDIUM_AND_ABOVE" },{ "filterType": "HARASSMENT", "confidenceLevel": "MEDIUM_AND_ABOVE" },{ "filterType": "SEXUALLY_EXPLICIT", "confidenceLevel": "MEDIUM_AND_ABOVE" }]'
--basic-config-filter-enforcement=enabled
--pi-and-jailbreak-filter-settings-enforcement=enabled
--pi-and-jailbreak-filter-settings-confidence-level=LOW_AND_ABOVE
--malicious-uri-filter-settings-enforcement=enabled
--template-metadata-custom-llm-response-safety-error-code=798
--template-metadata-custom-llm-response-safety-error-message="test template llm response evaluation failed"
--template-metadata-custom-prompt-safety-error-code=799
--template-metadata-custom-prompt-safety-error-message="test template prompt evaluation failed"
--template-metadata-ignore-partial-invocation-failures
--template-metadata-log-operations
--template-metadata-log-sanitize-operations
REST
使用這項指令建立新的 Model Armor 範本。
curl -X POST \
-d "{'FILTER_CONFIG': {} }" \
-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"
更改下列內容:
FILTER_CONFIG
是範本的篩選器設定。PROJECT_ID
是範本所屬的專案 ID。TEMPLATE_ID
是要建立的範本 ID。LOCATION
是範本的位置。
請按照這些步驟取得這個權杖。
以下範例說明 Model Armor 範本設定。
- 建立設定 (在本例中為
FILTER_CONFIG
),指定模型裝甲範本要篩選的篩選器,以及適用的信賴度。 - 使用您建立的設定,建立 Model Armor 範本 (在本例中為
ma-template-id-1234
)。
請注意,LOW_AND_ABOVE
代表系統會標記任何可能為低、中或高風險的內容。
export FILTER_CONFIG='{ "filterConfig": { "raiSettings": { "raiFilters": [{ "filterType": "HATE_SPEECH", "confidenceLevel": "MEDIUM_AND_ABOVE" }, { "filterType": "HARASSMENT", "confidenceLevel": "HIGH" }, { "filterType": "DANGEROUS", "confidenceLevel": "MEDIUM_AND_ABOVE" },{ "filterType": "SEXUALLY_EXPLICIT", "confidenceLevel": "MEDIUM_AND_ABOVE" }] }, "piAndJailbreakFilterSettings": { "filterEnforcement": "ENABLED", "confidenceLevel": "LOW_AND_ABOVE" }, "maliciousUriFilterSettings": { "filterEnforcement": "ENABLED" } } }' curl -X POST \ -d $FILTER_CONFIG \ -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_ID/templates?template_id=TEMPLATE_ID"
如要建立啟用多語言偵測功能的 Model Armor 範本,請在指令中傳遞 TEMPLATE_CONFIG
參數。這個參數會定義語言偵測設定的設定。
使用這項指令建立新的 Model Armor 範本,並啟用多語言偵測功能。
curl -X POST \
-d "{'TEMPLATE_CONFIG': {} }" \
-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"
以下範例說明啟用多語言偵測功能的 Model Armor 範本設定。
export TEMPLATE_CONFIG='{ "filterConfig": { "raiSettings": { "raiFilters": [{ "filterType": "HATE_SPEECH", "confidenceLevel": "MEDIUM_AND_ABOVE" }, { "filterType": "HARASSMENT", "confidenceLevel": "HIGH" }, { "filterType": "DANGEROUS", "confidenceLevel": "MEDIUM_AND_ABOVE" },{ "filterType": "SEXUALLY_EXPLICIT", "confidenceLevel": "MEDIUM_AND_ABOVE" }] }, "piAndJailbreakFilterSettings": { "filterEnforcement": "ENABLED", "confidenceLevel": "LOW_AND_ABOVE" }, "maliciousUriFilterSettings": { "filterEnforcement": "ENABLED" } }, "templateMetadata": { "multiLanguageDetection": { "enableMultiLanguageDetection": true } } }' curl -X POST \ -d $TEMPLATE_CONFIG \ -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_ID/templates?template_id=TEMPLATE_ID"
Python
如要執行這項指令,請先在 Python 中初始化 Model Armor 用戶端。
request = modelarmor_v1.CreateTemplateRequest( parent="projects/PROJECT_ID/locations/LOCATION", template_id="TEMPLATE_ID", { "name": "projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID", "filter_config": FILTER_CONFIG } ) response = client.create_template(request=request)
更改下列內容:
FILTER_CONFIG
是範本的篩選器設定。PROJECT_ID
是範本所屬的專案 ID。TEMPLATE_ID
是要建立的範本 ID。LOCATION
是範本的位置。
如要在範本上啟用完整記錄功能,請參閱「Model Armor 稽核和平台記錄」。
查看 Model Armor 範本
主控台
前往 Google Cloud 控制台的「Model Armor」頁面。
確認您正在查看已啟用 Model Armor 的專案。系統會顯示「Model Armor」頁面,列出為專案建立的範本。
按一下清單中的任一範本,即可查看詳細資料。
gcloud
執行下列指令:
gcloud model-armor templates describe TEMPLATE_ID --location LOCATION
更改下列內容:
LOCATION
是範本的位置。TEMPLATE_ID
是範本的 ID。
REST
執行下列指令:
curl -X GET \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://modelarmor.LOCATION_ID.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID"
更改下列內容:
PROJECT_ID
是範本所屬的專案 ID。TEMPLATE_ID
是要查看的範本 ID。LOCATION
是範本的位置。
Python
如要執行這項指令,請先在 Python 中初始化 Model Armor 用戶端。
request = modelarmor_v1.GetTemplateRequest( name="projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID", ) response = client.get_template(request=request)
更改下列內容:
PROJECT_ID
是範本所屬的專案 ID。TEMPLATE_ID
是要查看的範本 ID。LOCATION
是範本的位置。
更新 Model Armor 範本
主控台
前往 Google Cloud 控制台的「Model Armor」頁面。
確認您正在查看已啟用 Model Armor 的專案。系統會顯示「Model Armor」(模型裝甲) 頁面,列出為貴機構建立的範本。
在清單中按一下要更新的範本。系統隨即會顯示「範本詳細資料」頁面。
按一下 [編輯]。
更新必要參數,然後按一下「儲存」。
gcloud
執行下列指令:
gcloud model-armor templates update TEMPLATE_ID --location LOCATION
更改下列內容:
LOCATION
是範本的位置。TEMPLATE_ID
是範本的 ID。
REST
執行下列指令:
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \ -d $FILTER_CONFIG \ "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID?update_mask=FILTER_CONFIG"
更改下列內容:
PROJECT_ID
是範本所屬的專案 ID。LOCATION
是範本的位置。TEMPLATE_ID
是範本的 ID。FILTER_CONFIG
是篩選器設定的 JSON 表示法。
其他欄位都無法變更。嘗試更新其他欄位 (例如範本名稱) 會導致錯誤。
Python
如要執行這項指令,請先在 Python 中初始化 Model Armor 用戶端。
request = modelarmor_v1.UpdateTemplateRequest( template={ "name": "projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID", "filter_config": FILTER_CONFIG } ) response = client.update_template(request=request)
更改下列內容:
FILTER_CONFIG
是範本的篩選器設定。PROJECT_ID
是範本所屬的專案 ID。TEMPLATE_ID
是要更新的範本 ID。LOCATION
是範本的位置。
刪除 Model Armor 範本
主控台
前往 Google Cloud 控制台的「Model Armor」頁面。
確認您正在查看已啟用 Model Armor 的專案。系統會顯示「Model Armor」(模型裝甲) 頁面,列出為貴機構建立的範本。
在清單中按一下要刪除的範本。系統隨即會顯示「範本詳細資料」頁面。
按一下 [Delete] (刪除),系統隨即會顯示確認對話方塊。
輸入範本名稱來確認刪除,然後按一下「刪除」。
gcloud
執行下列指令:
gcloud model-armor templates delete TEMPLATE_ID --location LOCATION
更改下列內容:
LOCATION
是範本的位置。TEMPLATE_ID
是範本的 ID。
REST
執行下列指令:
curl -X DELETE \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ "https://modelarmor.LOCATION_ID.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID"
更改下列內容:
PROJECT_ID
是範本所屬的專案 ID。LOCATION
是範本的位置。TEMPLATE_ID
是範本的 ID。
Python
如要執行這項指令,請先在 Python 中初始化 Model Armor 用戶端。
request = modelarmor_v1.DeleteTemplateRequest( name="projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID", ) response = client.delete_template(request=request)
更改下列內容:
PROJECT_ID
是範本所屬的專案 ID。TEMPLATE_ID
是要刪除的範本 ID。LOCATION
是範本的位置。
範本中繼資料
Model Armor 範本中繼資料可協助您設定 Model Armor 的行為,包括安全性和安全性檢查處理、錯誤處理和記錄行為。
Model Armor 的範本中繼資料包含下列欄位:
中繼資料 | 類型 | 說明 |
---|---|---|
multi_language_detection | 布林值 | 啟用多語言偵測功能。 |
enforcement_type | 列舉 |
定義強制執行類型。請使用下列其中一個值:
|
log_template_operations | 布林值 | 啟用範本作業的記錄功能。 |
log_sanitize_operations | 布林值 | 啟用記錄清除作業。 |