使用內容篩選條件封鎖有害影像
Amazon Bedrock 防護機制可協助在防護機制內設定內容篩選條件時,封鎖不適當或有害的影像。
先決條件和限制
-
此功能支援純影像,但不支援具有內嵌視訊內容的影像。
-
此功能在美國東部 (維吉尼亞北部)、美國西部 (奧勒岡)、歐洲 (法蘭克福) 和亞太地區 (東京) AWS 區域 全面推出,其支援內容篩選條件中的仇恨、侮辱、性、暴力、不法行為和提示攻擊類別。
-
此功能在美國東部 (俄亥俄)、亞太地區 (孟買、首爾、新加坡、東京)、歐洲 (愛爾蘭、倫敦) 和美國 GovCloud (美國西部) AWS 區域 提供預覽,內容篩選條件中支援仇恨、侮辱、性別和暴力類別。
-
此功能允許的影像維度上限為 8000x8000 (適用於 JPEG 和 PNG 檔案)。
-
使用者可以上傳大小上限為 4 MB 的影像,單一請求最多 20 個影像。
-
預設限制為每秒 25 張影像。此值不可設定。
-
影像內容僅支援 PNG 和 JPEG 格式。
概觀:
純影像或包含文字的影像才支援偵測和封鎖有害影像。建立防護機制時,使用者可以自行或搭配文字選項選取影像選項,並將個別的篩選強度設定為 NONE、LOW、MEDIUM 或 HIGH。如果同時選取這兩種模態,則對於文字和影像內容這些都是常見的閾值。防護機制將評估以使用者輸入傳送的影像,或從模型回應產生為輸出的影像。
偵測有害影像內容支援的類別如下所述:
-
仇恨 – 描述因身份 (例如種族、民族、性別、宗教、性取向、能力和國籍) 而歧視、批評、侮辱、譴責或做出非人性化的內容。它還包括顯示仇恨群組符號的圖形和真實視覺內容、仇恨符號,以及與各種鼓吹歧視、種族主義和不容忍的組織相關聯的影像。
-
侮辱 – 描述包括貶低、羞辱、嘲笑、侮辱或貶低言語的內容。這種類型的語言也被標記為霸凌。它還包含各種形式的粗魯、不尊重或令人反感的手勢,旨在表達藐視、憤怒或不認同。
-
性暗示 – 描述使用直接或間接引用身體部位、身體特徵或性別,表現出性興趣、活動或激起性欲的內容。它還包括顯示私有部分的影像,以及涉及性交的性活動。此類別也包含卡通、動畫、繪圖、草圖和其他具有性主題的說明內容。
-
暴力 – 描述包括對個人、群體或實物造成身體痛苦、傷害或損害的美化或威脅的內容。它還包含與具有傷害意圖的武器相關的影像。
-
不法行為 – 描述輸入提示和模型回應,這些提示和模型回應會尋找或提供有關參與犯罪活動的資訊,或是傷害、詐騙或利用個人、群體或機構。
-
提示攻擊 – 描述旨在繞過基礎模型的安全和管制功能的使用者提示,以產生有害的內容 (也稱為越獄),並忽略及覆寫開發人員指定的指示 (稱為提示注入)。需要使用輸入標記,才能套用提示攻擊。提示攻擊偵測需要使用輸入標籤。
使用影像內容篩選條件
使用影像的內容篩選條件建立或更新防護機制
建立新的防護機制或更新現有的防護機制時,除了現有的文字選項之外,使用者現在還會看到選取影像的選項。
注意
預設會啟用文字選項,且需要明確啟用影像選項。使用者可以根據使用案例選擇文字和/或影像。
篩選分類和封鎖層級
根據使用者輸入和 FM 回應的信賴度分類進行篩選。所有使用者輸入和模型回應都分為四個強度等級:無、低、中和高。篩選強度決定篩選有害內容的敏感度。隨著篩選強度的增加,篩選有害內容的可能性也會增加,而在應用程式中查看有害內容的機率會降低。選取影像和文字選項時,相同的篩選強度會套用至特定類別的兩種模態。
若要設定有害類別的影像和文字篩選條件,請選取設定有害類別篩選條件。
-
選取文字和/或影像,從提示或進出模型的回應篩選文字或影像內容。
-
針對您要套用至每個類別的篩選層級,選取無、低、中或高。高的設定有助於封鎖套用至篩選條件該類別的最多文字或影像。
-
選取針對回應使用相同的有害類別篩選條件,以使用您用於提示的相同篩選條件設定。您也可以選擇不選取此選項,為提示或回應設定不同的篩選條件層級。選取重設閾值,以重設提示或回應的所有篩選條件層級。
-
選取檢閱並建立或下一步,以建立防護機制。
使用 API 設定影像的內容篩選條件
您可以使用防護機制 API 在 Amazon Bedrock 防護機制中設定影像內容篩選條件。以下範例顯示套用了不同有害內容類別和篩選條件強度的 Amazon Bedrock防護機制篩選條件。您可以使用此範本作為您自己使用案例的範例。
使用 contentPolicyConfig 操作時,filtersConfig 是一個物件,如下列範例所示。
使用影像內容篩選條件建立防護機制的範例 Python Boto3 程式碼
import boto3 import botocore import json def main(): bedrock = boto3.client('bedrock', region_name='us-east-1') try: create_guardrail_response = bedrock.create_guardrail( name='my-image-guardrail', contentPolicyConfig={ 'filtersConfig': [ { 'type': 'SEXUAL', 'inputStrength': 'HIGH', 'outputStrength': 'HIGH', 'inputModalities': ['TEXT', 'IMAGE'], 'outputModalities': ['TEXT', 'IMAGE'] }, { 'type': 'VIOLENCE', 'inputStrength': 'HIGH', 'outputStrength': 'HIGH', 'inputModalities': ['TEXT', 'IMAGE'], 'outputModalities': ['TEXT', 'IMAGE'] }, { 'type': 'HATE', 'inputStrength': 'HIGH', 'outputStrength': 'HIGH', 'inputModalities': ['TEXT', 'IMAGE'], 'outputModalities': ['TEXT', 'IMAGE'] }, { 'type': 'INSULTS', 'inputStrength': 'HIGH', 'outputStrength': 'HIGH', 'inputModalities': ['TEXT', 'IMAGE'], 'outputModalities': ['TEXT', 'IMAGE'] }, { 'type': 'MISCONDUCT', 'inputStrength': 'HIGH', 'outputStrength': 'HIGH', 'inputModalities': ['TEXT'], 'outputModalities': ['TEXT'] }, { 'type': 'PROMPT_ATTACK', 'inputStrength': 'HIGH', 'outputStrength': 'NONE', 'inputModalities': ['TEXT'], 'outputModalities': ['TEXT'] } ] }, blockedInputMessaging='Sorry, the model cannot answer this question.', blockedOutputsMessaging='Sorry, the model cannot answer this question.', ) create_guardrail_response['createdAt'] = create_guardrail_response['createdAt'].strftime('%Y-%m-%d %H:%M:%S') print("Successfully created guardrail with details:") print(json.dumps(create_guardrail_response, indent=2)) except botocore.exceptions.ClientError as err: print("Failed while calling CreateGuardrail API with RequestId = " + err.response['ResponseMetadata']['RequestId']) raise err if __name__ == "__main__": main()
設定影像篩選條件以使用 ApplyGuardrail API
您可以使用 ApplyGuardrail API 對影像和文字內容使用內容篩選條件。此選項可讓您使用內容篩選條件設定,而無需調用 Amazon Bedrock 模型。您可以依照 Amazon Bedrock 防護機制支援的每個底圖基礎模型的推論參數文件,藉此更新下列指令碼中各種模型的請求承載。
您可以依照 Amazon Bedrock 防護機制支援的每個底圖基礎模型的推論參數文件,藉此更新下列指令碼中各種模型的請求承載。
import boto3 import botocore import json guardrail_id = 'guardrail-id' guardrail_version = 'DRAFT' content_source = 'INPUT' image_path = '/path/to/image.jpg' with open(image_path, 'rb') as image: image_bytes = image.read() content = [ { "text": { "text": "Hi, can you explain this image art to me." } }, { "image": { "format": "jpeg", "source": { "bytes": image_bytes } } } ] def main(): bedrock_runtime_client = boto3.client("bedrock-runtime", region_name="us-east-1") try: print("Making a call to ApplyGuardrail API now") response = bedrock_runtime_client.apply_guardrail( guardrailIdentifier=guardrail_id, guardrailVersion=guardrail_version, source=content_source, content=content ) print("Received response from ApplyGuardrail API:") print(json.dumps(response, indent=2)) except botocore.exceptions.ClientError as err: print("Failed while calling ApplyGuardrail API with RequestId = " + err.response['ResponseMetadata']['RequestId']) raise err if __name__ == "__main__": main()
設定影像篩選條件以使用影像產生模型
您也可以將 Amazon Bedrock 防護機制影像篩選條件與 Titan Image Generator 和穩定影像或擴散模型等影像產生模型搭配使用。目前透過 InvokeModel API 支援這些模型,可利用防護機制調用。您可以依照防護機制支援的各種 Amazon Bedrock 基礎模型的推論參數文件,藉此更新下列指令碼中各種模型的請求承載。
import base64 import boto3 import botocore import json import os import random import string guardrail_id = 'guardrail-id' guardrail_version = 'DRAFT' model_id = 'stability.sd3-large-v1:0' output_images_folder = '/path/to/folder/' body = json.dumps( { "prompt": "Create an image of a beautiful flower", # Prompt for image generation ("A gun" should get blocked by violence) "output_format": "jpeg" } ) def main(): bedrock_runtime_client = boto3.client("bedrock-runtime", region_name="us-west-2") try: print("Making a call to InvokeModel API for model: {}".format(model_id)) response = bedrock_runtime_client.invoke_model( body=body, modelId=model_id, trace='ENABLED', guardrailIdentifier=guardrail_id, guardrailVersion=guardrail_version ) response_body = json.loads(response.get('body').read()) print("Received response from InvokeModel API (Request Id: {})".format(response['ResponseMetadata']['RequestId'])) if 'images' in response_body and len(response_body['images']) > 0: os.makedirs(output_images_folder, exist_ok=True) images = response_body["images"] for image in images: image_id = ''.join(random.choices(string.ascii_lowercase + string.digits, k=6)) image_file = os.path.join(output_images_folder, "generated-image-{}.jpg".format(image_id)) print("Saving generated image {} at {}".format(image_id, image_file)) with open(image_file, 'wb') as image_file_descriptor: image_file_descriptor.write(base64.b64decode(image.encode('utf-8'))) else: print("No images generated from model") guardrail_trace = response_body['amazon-bedrock-trace']['guardrail'] guardrail_trace['modelOutput'] = ['<REDACTED>'] print("Guardrail Trace: {}".format(json.dumps(guardrail_trace, indent=2))) except botocore.exceptions.ClientError as err: print("Failed while calling InvokeModel API with RequestId = {}".format(err.response['ResponseMetadata']['RequestId'])) raise err if __name__ == "__main__": main()