Skip to content

Commit 739ef08

Browse files
schniberiakov-aws
andauthored
addition of Amazon Bedrock Guardrail support for the case summarization (#267)
* addition of Amazon Bedrock Guardrail support for the case summarization * restructure and minor fixes * Update case-summarization/README.md --------- Co-authored-by: Samuel CHNIBER <[email protected]> Co-authored-by: Iakov GAN <[email protected]> Co-authored-by: Iakov Gan <[email protected]>
1 parent a7b6c5d commit 739ef08

File tree

6 files changed

+358
-17
lines changed

6 files changed

+358
-17
lines changed

case-summarization/GUARDRAIL.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## AWS Bedrock Guardrail
2+
Amazon Bedrock Guardrail is a crucial security feature for generative AI applications that helps implement safeguards based on specific use cases and responsible AI policies. It provides an additional layer of protection on top of the native safeguards offered by foundation models (FMs)[1][2].
3+
4+
## Key Features and Importance
5+
6+
Amazon Bedrock Guardrails offers several important security features:
7+
8+
1. **Content Filtering**: It helps block harmful content by evaluating both user inputs and model responses. The system can filter out content related to hate speech, insults, sexual content, violence, and misconduct[2].
9+
10+
2. **Topic Restrictions**: Organizations can define specific topics to avoid, ensuring that interactions remain relevant to their business and align with company policies[2].
11+
12+
3. **Sensitive Information Protection**: The system can detect and redact personally identifiable information (PII) in user inputs and model responses, helping to protect user privacy[2][3].
13+
14+
4. **Custom Word Filtering**: It allows the configuration of custom words or phrases to be blocked, including profanity or specific terms like competitor names[2].
15+
16+
5. **Hallucination Detection**: Contextual grounding checks help detect and filter out hallucinations in model responses, ensuring more accurate and trustworthy information[2].
17+
18+
## Security Importance
19+
20+
The importance of Amazon Bedrock Guardrails for security cannot be overstated:
21+
22+
1. **Enhanced Content Safety**: It can block up to 85% more harmful content compared to native FM protections, significantly improving the safety of AI applications[2].
23+
24+
2. **Consistent Security Across Models**: Guardrails work with all large language models in Amazon Bedrock, providing a uniform level of security regardless of the underlying model[2].
25+
26+
3. **Customizable Safeguards**: Organizations can create multiple guardrails with different configurations, tailoring security measures to specific applications and use cases[1][3].
27+
28+
4. **Compliance and Responsible AI**: By allowing fine-tuned control over content and interactions, Guardrails help organizations adhere to their responsible AI policies and maintain regulatory compliance[2].
29+
30+
5. **Protection Against Prompt Attacks**: The system safeguards against prompt injection and jailbreak attempts, enhancing overall security[2].
31+
32+
Amazon Bedrock Guardrails plays a vital role in ensuring that generative AI applications remain safe, relevant, and aligned with organizational policies. By providing robust, customizable security features, it enables businesses to leverage the power of AI while mitigating potential risks associated with harmful or inappropriate content[1][2][3].
33+
34+
### Reasonable Defaults
35+
36+
This plugin comes with the following reasonable defaults that can be overriden through the parameters exposed by the CloudFormation template:
37+
38+
| Parameter | Description | Default |
39+
| --- | --- | --- |
40+
| BlockedInputMessage | Message to return when the Amazon Bedrock Guardrail blocks a prompt. | {"executive_summary":"Amazon Bedrock Guardrails has blocked the AWS Support Case Summarization.","proposed_solutions":"","actions":"","references":[],"tam_involved":"","feedback":""} |
41+
| BlockedOutputMessage | Message to return when the Amazon Bedrock Guardrail blocks a model response | '' |
42+
| IncludeSexualContentFilter | Whether to include Sexual Content Filter in the Guardrail or not | 'yes' |
43+
| SexualContentFilterInputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces. | 'HIGH' |
44+
| SexualContentFilterOutputStrength | The strength of the content filter to apply to model responses. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
45+
| IncludeViolentContentFilter | Whether to include Violent Content Filter in the Guardrail or not | 'yes' |
46+
| ViolentContentFilterInputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
47+
| ViolentContentFilterOutputStrength | The strength of the content filter to apply to model responses. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
48+
| IncludeHateContentFilter | Whether to include Violent Content Filter in the Guardrail or not | 'yes' |
49+
| HateContentFilterInputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
50+
| HateContentFilterOutputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
51+
| IncludeInsultsContentFilter | Whether to include Insults Content Filter in the Guardrail or not | 'yes' |
52+
| InsultsContentFilterInputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
53+
| InsultsContentFilterOutputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
54+
| IncludeMisconductContentFilter | Whether to include Insults Content Filter in the Guardrail or not | 'yes' |
55+
| MisconductContentFilterInputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
56+
| MisconductContentFilterOutputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
57+
| IncludePromptAttackContentFilter | Whether to include Insults Content Filter in the Guardrail or not | 'yes' |
58+
| PromptAttackContentFilterInputStrength | The strength of the content filter to apply to prompts. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your application reduces | 'HIGH' |
59+
60+
### References & Further reading
61+
62+
* [1] How Amazon Bedrock Guardrails works https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails-how.html
63+
* [2] Generative AI Data Governance - Amazon Bedrock Guardrails - AWS https://aws.amazon.com/bedrock/guardrails/
64+
* [3] Stop harmful content in models using Amazon Bedrock Guardrails https://docs.aws.amazon.com/bedrock/latest/userguide/guardrails.html
65+
66+
## Usage
67+
68+
This stack will deploy a minimalistic Amazon Bedrock Guardrail that will filter out any inputs or outputs that can be assimilated to prompt hacking, sexual, violent, misconduct, hatred speech or insults. Any additional fine-tuning of filters can be acheived by customizing this template.
69+
70+
## Support and Contribution
71+
72+
See [CONTRIBUTING](../../../CONTRIBUTING.md) for more information.
73+
74+
## Security
75+
76+
See [SECURITY](../../../SECURITY.md) for more information.
77+
78+
## License
79+
80+
This project is licensed under the Apache-2.0 License.
81+

case-summarization/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,13 @@ This plugin comes with the following reasonable defaults that can be overriden t
3636

3737
#### 2. Deploy the AWS Support Case Summarization Stack In the Data Collection Account
3838

39-
* <kbd> <br> [Launch Stack >>](https://console.aws.amazon.com/cloudformation/home#/stacks/create/review?&templateURL=https://aws-managed-cost-intelligence-dashboards-us-east-1.s3.amazonaws.com/cfn/plugins/support-case-summarization/deploy/case-summarization.yaml&stackName=CidSupportCaseSummarizationStack&param_BedrockRegion=REPLACE%20WITH%20TARGET%20REGION)  <br> </kbd>
39+
* <kbd> <br> [Launch Stack >>](https://console.aws.amazon.com/cloudformation/home#/stacks/create/review?&templateURL=https://aws-managed-cost-intelligence-dashboards.s3.amazonaws.com/cfn/case-summarization/case-summarization.yaml&stackName=CidSupportCaseSummarizationStack)  <br> </kbd>
40+
41+
42+
## Guardrail
43+
44+
See [GUARDRAIL](GUARDRAIL.md) for more information.
45+
4046

4147
## Support and Contribution
4248

case-summarization/deploy/case-summarization.yaml

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
AWSTemplateFormatVersion: '2010-09-09'
2-
Description: AWS Support Cases Summarization Stack v0.0.1
2+
Description: AWS Support Cases Summarization Stack v0.0.2
33
Metadata:
44
AWS::CloudFormation::Interface:
55
ParameterGroups:
@@ -16,6 +16,9 @@ Metadata:
1616
- MaxRetries
1717
- Timeout
1818
- BatchSize
19+
- GuardRailIdentifier
20+
- GuardRailVersion
21+
- GuardRailTrace
1922
- Label:
2023
default: 'Technical parameters'
2124
Parameters:
@@ -78,6 +81,21 @@ Parameters:
7881
Type: String
7982
Description: Number of processing in parallel. Use 1 to avoid Throttling.
8083
Default: '1'
84+
GuardRailIdentifier:
85+
Type: String
86+
Description: The identifier for the guardrail. Leave empty if you do not want to use Amazon Bedrock Guardrails.
87+
Default: ''
88+
GuardRailVersion:
89+
Type: String
90+
Description: The version for the guardrail. Leave empty if you do not want to use Amazon Bedrock Guardrails.
91+
Default: ''
92+
GuardRailTrace:
93+
Type: String
94+
Description: The trace behavior for the guardrail.
95+
AllowedValues:
96+
- ENABLED
97+
- DISABLED
98+
Default: 'ENABLED'
8199

82100
Conditions:
83101
LambdaLayerBucketPrefixIsManaged: !Equals [!Ref LambdaLayerBucketPrefix, 'aws-managed-cost-intelligence-dashboards']
@@ -163,7 +181,7 @@ Resources:
163181
- Fn::Sub:
164182
- "arn:${AWS::Partition}:s3:::${DestinationBucket}"
165183
- DestinationBucket: {"Fn::ImportValue": "cid-DataCollection-Bucket"}
166-
- PolicyName: "Bedrock-Access"
184+
- PolicyName: "Bedrock-ModelAccess"
167185
PolicyDocument:
168186
Version: "2012-10-17"
169187
Statement:
@@ -172,6 +190,14 @@ Resources:
172190
- "bedrock:ListFoundationModels"
173191
- "bedrock:InvokeModel"
174192
Resource: "*" # cannot restrict this
193+
- PolicyName: "Bedrock-GuardrailAccess"
194+
PolicyDocument:
195+
Version: "2012-10-17"
196+
Statement:
197+
- Effect: "Allow"
198+
Action:
199+
- "bedrock:ApplyGuardrail"
200+
Resource: !Sub "arn:${AWS::Partition}:bedrock:${BedrockRegion}:${AWS::AccountId}:guardrail/*"
175201
- PolicyName: "SQS-Access"
176202
PolicyDocument:
177203
Version: "2012-10-17"
@@ -223,6 +249,9 @@ Resources:
223249
MAX_RETRIES = os.environ.get("MAX_RETRIES", 30)
224250
TIMEOUT = os.environ.get("TIMEOUT", 60)
225251
INSTRUCTIONS = os.environ.get("INSTRUCTIONS", '')
252+
GUARDRAIL_ID = os.environ.get("GUARDRAIL_ID", '')
253+
GUARDRAIL_VERSION = os.environ.get("GUARDRAIL_VERSION", '')
254+
GUARDRAIL_TRACE = os.environ.get("GUARDRAIL_TRACE", '')
226255
PROMPT_TEMPLATE = f"""
227256
System: You are an expert technical writer specializing in creating concise, neutral summaries of AWS customers support interactions. Your task is to summarize conversations between customers and AWS Support, maintaining objectivity and clarity. Here is the Conversation to be summarized:
228257
<conversation>
@@ -261,15 +290,31 @@ Resources:
261290
default=""
262291
)
263292
264-
def get_llm_program(conversation, model_id):
265-
llm = Bedrock(
266-
model=model_id,
267-
region_name=REGION,
268-
temperature=float(TEMPERATURE),
269-
max_tokens=int(MAX_TOKENS),
270-
max_retries=int(MAX_RETRIES),
271-
timeout=float(TIMEOUT)
272-
)
293+
def get_llm_program(conversation, model_id, guardrail_identifier, guardrail_version, trace):
294+
if guardrail_identifier == '' or guardrail_version == '':
295+
logger.info("support case summarization isn't using any Amazon Bedrock Guardrail Configuration.")
296+
llm = Bedrock(
297+
model=model_id,
298+
region_name=REGION,
299+
temperature=float(TEMPERATURE),
300+
max_tokens=int(MAX_TOKENS),
301+
max_retries=int(MAX_RETRIES),
302+
timeout=float(TIMEOUT)
303+
)
304+
elif guardrail_identifier != '' and guardrail_version != '':
305+
logger.info(f"support case summarization is using Amazon Bedrock Guardrail id: {guardrail_identifier} (Version {guardrail_version}) with tracing {trace}")
306+
llm = Bedrock(
307+
model=model_id,
308+
region_name=REGION,
309+
temperature=float(TEMPERATURE),
310+
max_tokens=int(MAX_TOKENS),
311+
max_retries=int(MAX_RETRIES),
312+
timeout=float(TIMEOUT),
313+
guardrail_identifier=guardrail_identifier,
314+
guardrail_version=guardrail_version,
315+
trace=trace
316+
)
317+
273318
return LLMTextCompletionProgram.from_defaults(
274319
llm=llm,
275320
output_cls=Summary,
@@ -313,8 +358,9 @@ Resources:
313358
314359
logger.info("support case summarization starting")
315360
model_id = get_model_id()
361+
362+
llm_program = get_llm_program(communications, model_id, GUARDRAIL_ID, GUARDRAIL_VERSION, GUARDRAIL_TRACE)
316363
317-
llm_program = get_llm_program(communications, model_id)
318364
try:
319365
case_data_content['Summary'] = llm_program().model_dump_json()
320366
except Exception as exc:
@@ -353,6 +399,9 @@ Resources:
353399
TIMEOUT: !Ref Timeout
354400
INSTRUCTIONS: !Ref Instructions
355401
REGION: !Ref BedrockRegion # FIXME: use default if empty
402+
GUARDRAIL_ID: !Ref GuardRailIdentifier
403+
GUARDRAIL_VERSION: !Ref GuardRailVersion
404+
GUARDRAIL_TRACE: !Ref GuardRailTrace
356405
Metadata:
357406
cfn_nag:
358407
rules_to_suppress:

0 commit comments

Comments
 (0)