本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用內容篩選條件封鎖有害映像
Amazon Bedrock 護欄可協助在護欄內設定內容篩選條件時封鎖不適當或有害的映像。
先決條件和限制
-
此功能僅支援映像,不支援具有內嵌視訊內容的映像。
-
此功能通常在美國東部 (維吉尼亞北部)、美國西部 (奧勒岡)、歐洲 (法蘭克福) 和亞太區域 (東京) 提供 AWS 區域,其支援內容篩選條件中的仇恨、侮辱、性、暴力、行為不當和提示攻擊類別。
-
此功能在美國東部 (俄亥俄)、亞太區域 (孟買、首爾、新加坡、東京)、歐洲 (愛爾蘭、倫敦) 和美國 GovCloud (美國西部) 提供預覽 AWS 區域,其中內容篩選條件中支援仇恨、侮辱、性別和暴力類別。
-
此功能允許的映像維度上限為 8000x8000 (適用於 JPEG 和 PNG 檔案)。
-
使用者可以上傳大小上限為 4 MB 的影像,單一請求最多可以上傳 20 張影像。
-
預設限制為每秒 25 張影像。此值不可設定。
-
影像內容僅支援 PNG 和 JPEG 格式。
概觀
只有具有文字的映像或映像才支援偵測和封鎖有害映像。建立護欄時,使用者可以自行或與文字選項一起選取影像選項,並將個別的篩選強度設定為 NONE、LOW、MEDIUM 或 HIGH。如果同時選取這兩種模態,這些閾值對於文字和影像內容都是常見的。護欄將評估以使用者輸入傳送的影像,或從模型回應產生為輸出的影像。
偵測有害映像內容支援的類別如下所述:
-
仇恨 – 描述根據身分 (例如種族、族裔、性別、宗教、性傾向、能力和原國籍) 來歧視、批評、侮辱、譴責或取消人格的內容。它還包括顯示仇恨群組符號的圖形和真實視覺內容、仇恨符號,以及與各種組織促進歧視、種族主義和不容忍相關的影像。
-
侮辱 – 描述內容,包括侮辱、羞辱、模擬、侮辱或輕視語言。種類型的語言也被標記為霸凌。它還包括各種形式的粗魯、不尊重或令人反感的手勢,旨在表達藐視、憤怒或不核准。
-
性 – 描述使用直接或間接參考身體部分、身體特徵或性別來表示性興趣、活動或覺察的內容。它還包括顯示私有部分的影像,以及涉及性交的性活動。此類別也包含卡通、動畫、繪圖、草圖和其他具有性主題的說明內容。
-
暴力 – 描述內容,包括對人員、群組或物件造成身體痛苦、傷害或傷害的美化或威脅。它還包含與意圖傷害的武器相關的影像。
-
不法行為 – 描述輸入提示和模型回應,以尋求或提供有關參與犯罪活動的資訊,或傷害、詐騙或利用人員、群組或機構。
-
提示攻擊 – 描述使用者提示,旨在繞過基礎模型的安全和管制功能,以產生有害內容 (也稱為 jailbreak),並忽略和覆寫開發人員指定的指示 (稱為提示注入)。需要使用輸入標記,才能套用提示攻擊。提示攻擊偵測需要使用輸入標籤。
使用映像內容篩選條件
使用影像的內容篩選條件建立或更新護欄
建立新的護欄或更新現有的護欄時,除了現有的文字選項之外,使用者現在還會看到選取影像的選項。
注意
預設會啟用文字選項,且需要明確啟用映像選項。使用者可以根據使用案例選擇文字和影像,也可以選擇其中一個。
篩選分類和封鎖層級
根據使用者輸入和 FM 回應的可信度分類進行篩選。所有使用者輸入和模型回應都分為四個強度等級:無、低、中和高。篩選強度決定篩選有害內容的敏感度。隨著篩選強度的增加,篩選有害內容的可能性會增加,而且在應用程式中查看有害內容的機率也會降低。選取影像和文字選項時,相同的篩選強度會套用至特定類別的兩種模態。
若要設定有害類別的影像和文字篩選條件,請選取設定有害類別篩選條件。
-
選取文字和/或影像,從提示或對模型的回應篩選文字或影像內容。
-
針對您要套用至每個類別的篩選層級,選取無、低、中或高。高設定有助於封鎖套用至篩選條件該類別的最多文字或影像。
-
選取針對回應使用相同的有害類別篩選條件,以使用您用於提示的相同篩選條件設定。您也可以選擇不選取此選項,為提示或回應設定不同的篩選條件層級。選取重設閾值以重設提示或回應的所有篩選條件層級。
-
選取檢閱並建立 或下一步以建立護欄。
使用 API 設定映像的內容篩選條件
您可以使用護欄 API 在 Amazon Bedrock 護欄中設定映像內容篩選條件。以下範例顯示套用了不同有害內容類別和篩選條件強度的 Amazon Bedrock Guardrails 篩選條件。您可以使用此範本做為您自己的使用案例的範例。
透過 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 Guardrails 支援的每個底圖基礎模型的推論參數文件,更新下列指令碼中各種模型的請求承載。
您可以依照 Amazon Bedrock Guardrails 支援的每個底圖基礎模型的推論參數文件,在下列指令碼中更新各種模型的請求承載。
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 Guardrails 影像篩選條件與 Titan Image Generator 和穩定影像或擴散模型等影像產生模型搭配使用。目前透過 InvokeModel API 支援這些模型,該 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()