|
| 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) |
0 commit comments