Skip to content

Instantly share code, notes, and snippets.

@MattJColes
Created October 16, 2024 20:38
Show Gist options
  • Save MattJColes/5480301ebc9a483cb12e1c5b6f7abba8 to your computer and use it in GitHub Desktop.
Save MattJColes/5480301ebc9a483cb12e1c5b6f7abba8 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set the region
REGION="us-west-2"
# Set the log group name and metric namespace
LOG_GROUP_NAME="AmazonBedrockGuardrailsLogs" # Replace with your log group name
METRIC_NAMESPACE="/aws/Bedrock/Guardrails"
# Get the Guardrail ID from the environment variable
GUARDRAIL_ID="${GUARDRAIL_ID:-}"
if [ -z "$GUARDRAIL_ID" ]; then
echo "GUARDRAIL_ID environment variable is not set."
exit 1
fi
create_metric_filter() {
local METRIC_NAME="$1"
local FILTER_PATTERN="$2"
aws logs put-metric-filter \
--log-group-name "$LOG_GROUP_NAME" \
--filter-name "$METRIC_NAME" \
--filter-pattern "$FILTER_PATTERN" \
--metric-transformations \
metricName="$METRIC_NAME",metricNamespace="$METRIC_NAMESPACE",metricValue=1,unit=Count \
--region "$REGION"
echo "Metric filter '$METRIC_NAME' created successfully."
}
# Create metric filters
create_metric_filter "GUARDRAIL_DID_NOT_INTERVENE" '{$.output.outputBodyJson.stopReason="NONE"}'
create_metric_filter "GUARDRAIL_INTERVENED" '{$.output.outputBodyJson.stopReason="INTERVENED"}'
create_metric_filter "Invocations-with-Guardrails" '%trace%'
FILTERS_BASE_PATTERN='{$.output.outputBodyJson.trace.guardrail.inputAssessment.'"$GUARDRAIL_ID"'.contentPolicy.filters[*].action="BLOCKED" && $.output.outputBodyJson.trace.guardrail.inputAssessment.'"$GUARDRAIL_ID"'.contentPolicy.filters[*].type="'
TOPICS_INPUT_BASE_PATTERN='{$.output.outputBodyJson.trace.guardrail.inputAssessment.'"$GUARDRAIL_ID"'.topicPolicy.topics[*].action="BLOCKED" && $.output.outputBodyJson.trace.guardrail.inputAssessment.'"$GUARDRAIL_ID"'.topicPolicy.topics[*].name="'
TOPICS_OUTPUT_BASE_PATTERN='{$.output.outputBodyJson.trace.guardrail.outputs[0].'"$GUARDRAIL_ID"'.topicPolicy.topics[*].action="BLOCKED" && $.output.outputBodyJson.trace.guardrail.outputs[0].'"$GUARDRAIL_ID"'.topicPolicy.topics[*].name="'
create_metric_filter "PROMPT_SEXUAL_FILTER_FAILED" "$FILTERS_BASE_PATTERN"'SEXUAL"}'
create_metric_filter "PROMPT_VIOLENCE_FILTER_FAILED" "$FILTERS_BASE_PATTERN"'VIOLENCE"}'
create_metric_filter "PROMPT_HATE_FILTER_FAILED" "$FILTERS_BASE_PATTERN"'HATE"}'
create_metric_filter "PROMPT_INSULTS_FILTER_FAILED" "$FILTERS_BASE_PATTERN"'INSULTS"}'
create_metric_filter "PROMPT_MISCONDUCT_FILTER_FAILED" "$FILTERS_BASE_PATTERN"'MISCONDUCT"}'
create_metric_filter "PROMPT_PROMPT_ATTACK_FILTER_FAILED" "$FILTERS_BASE_PATTERN"'PROMPT_ATTACK"}'
create_metric_filter "FINANCE_TOPIC_INPUT_DENIED" "$TOPICS_INPUT_BASE_PATTERN"'Finance"}'
create_metric_filter "POLITICS_TOPIC_INPUT_DENIED" "$TOPICS_INPUT_BASE_PATTERN"'Politics"}'
create_metric_filter "FINANCE_TOPIC_OUTPUT_DENIED" "$TOPICS_OUTPUT_BASE_PATTERN"'Finance"}'
create_metric_filter "POLITICS_TOPIC_OUTPUT_DENIED" "$TOPICS_OUTPUT_BASE_PATTERN"'Politics"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment