Skip to content

Commit 9a87f51

Browse files
feat: add bedrock guardrails (#24)
* add guardrail detailed example * add path for pip ecosystem --------- Signed-off-by: Scott Schreckengaust <scottschreckengaust@users.noreply.github.com>
1 parent c63a232 commit 9a87f51

File tree

14 files changed

+618
-1
lines changed

14 files changed

+618
-1
lines changed

.github/dependabot.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ updates:
1313
- "/samples/document-explorer/terraform-config-backend"
1414
- "/samples/document-explorer/terraform-config-frontend"
1515
- "/samples/bedrock-agent"
16+
- "/samples/bedrock-guardrails"
1617
schedule:
1718
interval: "weekly"
1819
commit-message:
@@ -22,6 +23,7 @@ updates:
2223
directories:
2324
- "/samples/document-explorer/client_app"
2425
- "/samples/bedrock-agent/lambda/action-group"
26+
- "/samples/bedrock-guardrails/scripts"
2527
schedule:
2628
interval: "weekly"
2729
commit-message:

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ Each folder under the `samples` directory has a `README.md` with the specific in
3232
│   ├── scripts/load-kb.sh # Script to load and synchronize the Bedrock knowledge base's data source
3333
│   ├── lambda/action-group/ # Code for the action group
3434
│   └── lambda/action-group.yaml # The Open API specification for the action group
35+
├── bedrock-guardrails # An example usage for Bedrock guardrails
36+
│   ├── README.md # Instructions
37+
│   ├── main.tf # The main file
38+
│   ├── outputs.tf # Outputs to use while testing
39+
│   └── scripts/ # A folder to test and review Bedrock guardrails
3540
└── ...
41+
3642
```
3743

3844
## Issues, Support, Security, and Contributing

samples/bedrock-agent/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gutenburg.
1717
By providing reusable modules following AWS best practices,
1818
this app helps you quickly build custom generative AI apps on AWS using Terraform.
1919

20-
The AWS Terraform [bedrock](https://registry.terraform.io/modules/aws-ia/bedrock/aws/latest) module
20+
The AWS Terraform [bedrock](https://registry.terraform.io/modules/aws-ia/bedrock/aws/latest) module
2121
abstracts the complexity of orchestrating AWS services like S3, OpenSearch, Bedrock, etc.
2222

2323
Here is the architecture diagram of the sample application:

samples/bedrock-guardrails/.terraform.lock.hcl

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# Amazon Bedrock Guardrails
2+
3+
This example shows how to deploy a basic Bedrock agent with guardrails, leaving the default values and without creating an action group or a knowledgebase.
4+
5+
## Overview
6+
7+
The AWS Terraform [bedrock](https://registry.terraform.io/modules/aws-ia/bedrock/aws/latest) module
8+
abstracts the complexity of orchestrating AWS services like for Bedrock's guardrails. This
9+
10+
## Folder Structure
11+
12+
The key files are annotated below:
13+
14+
```tree
15+
├── README.md
16+
├── data.tf
17+
├── guardrails.auto.tfvars # The configuration for the example guardrail
18+
├── main.tf
19+
├── outputs.tf # Outputs for the two bedrock agents
20+
├── providers.tf
21+
├── scripts
22+
│ ├── input.txt # The inputs to test (one line at a time)
23+
│ ├── requirements.txt
24+
│ └── review.py # Script to send input and review output
25+
└── variables.tf
26+
```
27+
28+
## Getting started
29+
30+
### Prerequisites
31+
32+
- An AWS account. We recommend you deploy this solution in a new account.
33+
- [AWS CLI](https://aws.amazon.com/cli/): configure your credentials
34+
35+
```shell
36+
aws configure --profile [your-profile]
37+
AWS Access Key ID [None]: xxxxxx
38+
AWS Secret Access Key [None]:yyyyyyyyyy
39+
Default region name [None]: us-east-1
40+
Default output format [None]: json
41+
```
42+
43+
- [Terraform](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli): v1.7.4 or greater
44+
- [Python](https://www.python.org/downloads/): 3.12 or greater
45+
46+
### Deploy the solution
47+
48+
This project is built using [Terraform](https://www.terraform.io/). See [Getting Started - AWS](https://developer.hashicorp.com/terraform/tutorials/aws-get-started) for additional details and prerequisites.
49+
50+
1. Clone this repository.
51+
52+
```shell
53+
git clone https://github.com/aws-samples/generative-ai-cdk-constructs-samples.git
54+
```
55+
56+
2. Enter the code sample directory.
57+
58+
```shell
59+
cd generative-ai-cdk-constructs-samples/samples/bedrock-guardrails
60+
```
61+
62+
3. Initialize Terraform.
63+
64+
```shell
65+
terraform init
66+
...
67+
Terraform has been successfully initialized!
68+
...
69+
```
70+
71+
4. Deploy the two Agents (takes ~1min)
72+
73+
```shell
74+
terraform apply -auto-approve
75+
...
76+
Apply complete! Resources: 11 added, 0 changed, 0 destroyed.
77+
78+
Outputs:
79+
80+
bedrock_agent_id_with_guardrail = "ZYXWVUTSR1"
81+
bedrock_agent_id_without_guardrail = "ABCDEFGHI9"
82+
```
83+
84+
5. Note the two outputs for the Bedrock Agents for testing
85+
86+
## Test the agents
87+
88+
We will use input similiar to the `examples` in the `topics_config` variable listed within the `guardrails.auto.tfvars` file:
89+
90+
- `What stocks should I invest in for my retirement?`
91+
- `Is it a good idea to put my money in a mutual fund?`
92+
- `How should I allocate my 401(k) investments?`
93+
- `What type of trust fund should I set up for my children?`
94+
- `Should I hire a financial advisor to manage my investments?`
95+
96+
### Use scripts
97+
98+
1. Enter the scripts directory.
99+
100+
```shell
101+
cd scripts
102+
```
103+
104+
2. Create and activate your python virtual environment.
105+
106+
```shell
107+
python -m venv .venv
108+
source .venv/bin/activate
109+
pip install -r requirements.txt
110+
```
111+
112+
3. Run the script to see each agents responses.
113+
114+
```shell
115+
python review.py
116+
```
117+
118+
4. (optional) Alter the `input.txt` and run the previous step again to see changes
119+
120+
5. Deactivate and remove the virtual environment
121+
122+
```shell
123+
deactivate
124+
rm -r -f .venv
125+
```
126+
127+
### Use the console to test the agents with more examples
128+
129+
Alternatively, open the console test each agent (one with and one without guardrails) directly. _See [Test and troubleshoot agent behavior](https://docs.aws.amazon.com/bedrock/latest/userguide/agents-test.html)_.
130+
131+
## Cleanup
132+
133+
1. Tear the terraform solution down (~30 seconds).
134+
135+
```shell
136+
terraform destroy -auto-approve
137+
...
138+
Destroy complete! Resources: 11 destroyed.
139+
```
140+
141+
## References
142+
143+
Here is a sample repositories and workshop to dive deeper with guardrails!
144+
145+
- ["Responsible AI Samples" in Amazon Bedrock Service Sample Repository on Github](https://github.com/aws-samples/amazon-bedrock-samples/blob/main/responsible_ai)
146+
- ["Lab 8 - Creating Agent with Guardrails for Amazon Bedrock integration" in Amazon Bedrock Agents Workshop](https://catalog.workshops.aws/agents-for-amazon-bedrock/en-US/80-create-agent-with-guardrails)

samples/bedrock-guardrails/data.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "aws_bedrock_foundation_models" "test" {}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
blocked_input_messaging = "I can provide general info about products and services, but can't fully address your request here. For personalized help or detailed questions, please contact our customer service team directly. For security reasons, avoid sharing sensitive information through this channel. If you have a general product question, feel free to ask without including personal details."
2+
blocked_outputs_messaging = "I can provide general info about products and services, but can't fully address your request here. For personalized help or detailed questions, please contact our customer service team directly. For security reasons, avoid sharing sensitive information through this channel. If you have a general product question, feel free to ask without including personal details."
3+
filters_config = [
4+
{
5+
input_strength = "HIGH"
6+
output_strength = "HIGH"
7+
type = "VIOLENCE"
8+
},
9+
{
10+
input_strength = "HIGH"
11+
output_strength = "NONE"
12+
type = "PROMPT_ATTACK"
13+
},
14+
{
15+
input_strength = "HIGH"
16+
output_strength = "HIGH"
17+
type = "MISCONDUCT"
18+
},
19+
{
20+
input_strength = "HIGH"
21+
output_strength = "HIGH"
22+
type = "HATE"
23+
},
24+
{
25+
input_strength = "HIGH"
26+
output_strength = "HIGH"
27+
type = "SEXUAL"
28+
},
29+
{
30+
input_strength = "HIGH"
31+
output_strength = "HIGH"
32+
type = "INSULTS"
33+
},
34+
]
35+
managed_word_lists_config = [
36+
{
37+
type = "PROFANITY"
38+
}
39+
]
40+
pii_entities_config = [
41+
{
42+
action = "ANONYMIZE"
43+
type = "NAME"
44+
},
45+
{
46+
action = "ANONYMIZE"
47+
type = "PHONE"
48+
},
49+
{
50+
action = "ANONYMIZE"
51+
type = "EMAIL"
52+
},
53+
{
54+
action = "BLOCK"
55+
type = "US_SOCIAL_SECURITY_NUMBER"
56+
},
57+
{
58+
action = "BLOCK"
59+
type = "US_BANK_ACCOUNT_NUMBER"
60+
},
61+
{
62+
action = "BLOCK"
63+
type = "CREDIT_DEBIT_CARD_NUMBER"
64+
},
65+
]
66+
regexes_config = [
67+
{
68+
name = "Account Number"
69+
description = "Matches account numbers in the format XXXXXX1234"
70+
pattern = "(\\b\\d{6}\\d{4}\\b)"
71+
action = "ANONYMIZE"
72+
}
73+
]
74+
topics_config = [{
75+
name = "Fiduciary Advice"
76+
type = "DENY"
77+
definition = "Providing personalized advice or recommendations on managing financial assets, investments, or trusts in a fiduciary capacity or assuming related obligations and liabilities."
78+
examples = [
79+
"What stocks should I invest in for my retirement?",
80+
"Is it a good idea to put my money in a mutual fund?",
81+
"How should I allocate my 401(k) investments?",
82+
"What type of trust fund should I set up for my children?",
83+
"Should I hire a financial advisor to manage my investments?",
84+
]
85+
}]
86+
words_config = [
87+
{ text = "fiduciary advice" },
88+
{ text = "investment recommendations" },
89+
{ text = "stock picks" },
90+
{ text = "financial planning guidance" },
91+
{ text = "portfolio allocation advice" },
92+
{ text = "retirement fund suggestions" },
93+
{ text = "wealth management tips" },
94+
{ text = "trust fund setup" },
95+
{ text = "investment strategy" },
96+
{ text = "financial advisor recommendations" },
97+
]

0 commit comments

Comments
 (0)