This construct provides a high-level abstraction for creating Amazon Bedrock Guardrails for content filtering and safety.
import { BedrockGuardrailL3Construct } from '@aws-mdaa/bedrock-guardrail-l3-construct';
const guardrail = new BedrockGuardrailL3Construct(this, 'MyGuardrail', {
guardrailName: 'my-guardrail',
guardrailConfig: {
description: 'Content filtering guardrail',
contentFilters: {
hate: {
inputStrength: 'HIGH',
outputStrength: 'HIGH'
},
sexual: {
inputStrength: 'HIGH',
outputStrength: 'HIGH'
},
violence: {
inputStrength: 'MEDIUM',
outputStrength: 'MEDIUM'
}
},
contextualGroundingFilters: {
grounding: 0.9,
relevance: 0.8
}
},
kmsKey: myKmsKey,
naming: naming
});
description: Optional description for the guardrailcontentFilters: Content filter configurations (required)blockedInputMessaging: Custom message for blocked inputsblockedOutputsMessaging: Custom message for blocked outputscontextualGroundingFilters: Grounding and relevance thresholdsinterventionAlarm: (Optional) Intervention alarm thresholds; requires the alarmSettings construct propguardrailName, guardrailConfig, kmsKey, naming: the guardrail, its configuration, the KMS key encrypting it, and the resource namingalarmSettings: Notification topic ARN, period, evaluation periods, and optional datapointsToAlarm for the intervention alarms. Required when guardrailConfig.interventionAlarm is set. When alarmNamePattern is set, each alarm name must match it, so the topic's CloudWatch grants cover the alarmsEach threshold creates one CloudWatch alarm on a Metrics Insights query over the AWS/Bedrock/Guardrails InvocationsIntervened metric, aggregated across every version of the guardrail, DRAFT included:
countThreshold: alarm when the number of interventions in one period exceeds this valueratePercentThreshold: alarm when the percentage of guardrail evaluations that intervened in one period exceeds this value (at least 0, below 100). One intervention in a period with a single evaluation is 100%, so on a low-traffic guardrail require several breaching periods with datapointsToAlarm, or use countThresholdThe period must be a multiple of 60 seconds, and period x evaluation periods must not exceed 3 hours, the window CloudWatch evaluates Metrics Insights alarms over. A period with no evaluations is treated as not breaching.
hate: Hate speech detectionsexual: Sexual content detectionviolence: Violence content detectioninsults: Insult detectionmisconduct: Professional misconduct detectionpromptAttack: Prompt injection attack detection. Evaluated on inputs only, so outputStrength must be NONE; any other value is rejected at synthNONE: No filtering in that directionLOW: Minimal filteringMEDIUM: Moderate filteringHIGH: Strict filteringgrounding: Threshold for grounding to source material (0.0 to 1.0)relevance: Threshold for relevance to the query (0.0 to 1.0)contentFilters: {
hate: { inputStrength: 'HIGH', outputStrength: 'HIGH' },
sexual: { inputStrength: 'HIGH', outputStrength: 'HIGH' },
violence: { inputStrength: 'MEDIUM', outputStrength: 'MEDIUM' }
}
contentFilters: {
hate: { inputStrength: 'HIGH', outputStrength: 'HIGH' },
sexual: { inputStrength: 'HIGH', outputStrength: 'HIGH' },
violence: { inputStrength: 'MEDIUM', outputStrength: 'MEDIUM' },
insults: { inputStrength: 'LOW', outputStrength: 'LOW' },
misconduct: { inputStrength: 'MEDIUM', outputStrength: 'MEDIUM' },
promptAttack: { inputStrength: 'HIGH', outputStrength: 'NONE' }
}
contextualGroundingFilters: {
grounding: 0.9, // High grounding requirement
relevance: 0.8 // High relevance requirement
}
@aws-mdaa/l3-construct@aws-mdaa/cloudwatch-constructsaws-cdk-libconstructs