While preparing for the AWS Certified Generative AI Developer - Professional Exam I wanted to get some more hands-on experience with Guardrails, to get a better understanding of exactly what can be done with them.
Here's a five minute tutorial to quickly create an Amazon Bedrock Knowledge Base so you can easily explore Guardrails and close any knowledge gaps!
1. Create an S3 bucket
Create an S3 bucket and upload the documents that you want to include in the Knowledge Base. I used a fake corporate strategy document for a fictitious company, Ralphie Bank. Feel free to use this if you don't already have a suitable document to use!
2. Create a Knowledge Base
In the Bedrock console, click Knowledge Bases, and Create Knowledge Base. Select S3 vector store as the type.
Data source type is Amazon S3.
Then click Next and Browse S3 to select the bucket where your data is stored.
Before proceeding you have the opportunity to select a chunking strategy from a range of different strategies that are natively provided in Bedrock.
There's also the option to configure a Lambda function to perform custom chunking or data processing at this stage.
After that, click Next, and select an embeddings model, this is the model that will perform the vector embeddings that will be stored in the Knowledge Base.
Select the type of vector store you want to create. I used S3 Vectors, it is much cheaper than OpenSearch Serverless and fine for what I need.
Then click Next and Create Knowledge Base.
3. Sync Your Data Source
After the Knowledge Base is showing as available, you'll need to sync your data source to actually create populate the data source with the embeddings. Just select your Knowledge Base and click Sync.
4. Create a Guardrail
Click Guardrails in the Amazon Bedrock menu on the left, then Create Guardrail.
Name it PII-filter and click Next. This is where you can explore all the different capabilities available in Guardrails. Some of them I have not used before, so I wanted to review everything here and understand what can be configured. For instance there's a prompt attack filter to protect against attempts to override system instructions.
After that click Next, and here you can add denied topics. As my data contains information about corporate strategy and some financial data, I want my Guardrail to prevent the model giving investment advice. Click Add Denied Topic, name it Financial-Advice and then provide text describing the topic to block, here's what I added:
Financial advice, investment advice, stock purchase, investment fund
Keeping Input and Output actions set to Block, click Confirm, then Next.
Word filters allow you to add profanity filters as well as custom words and phrases, then click Next.
After that you can specify how to handle PII.
Click Add New PII, then select the type of PII you want to identify. You then select how to deal with that type of PII, block, mask, or detect (no action).
I selected phone and email since I know that this type of PII exists in my data.
After that click Next in the next page you can optionally add a contextual grounding check and relevance check.
Contextual grounding lets you set a threshold for what is acceptable in terms of model confidence score for contextual grounding. The default setting is 0.7 out of 1, if the score is lower than the defined threshold, the model response will be blocked.
Relevance checks work in a similar way, a relevance score is calculated and if the score is lower than the defined threshold, the model response will be blocked.
Then click Next, Next, and Create Guardrail.
After that you are ready to test the functionality and see it in action.
5. Testing the Knowledge Base Functionality
In the left hand menu, click Knowledge Bases, select your Knowledge Base, then click Test Knowledge Base.
Select the model you want to test with, a lite model is fine for this task.
Under Data Manipulation, open the Guardrails dropdown and then select your Guardrail.
There is also the opportunity to use a reranking model, like Cohere rerank to rescore the relevancy of the retrieved data and override the vector database's relevancy ranking.
After that I added a prompt to test out the configuration:
What are Ralphie Bank’s three strategic pillars?

6. Testing the Guardrail
Next I tried some prompts that should trigger the Guardrail, here's what I tried:
Should I invest in Ralphie Bank shares this year?
What is the internal strategy hotline number?
What is the compliance email address?
If it's all worked, the Guardrail will block the request from being sent to the model, and Bedrock should not fulfill the request.
7. Clean-up
Be sure to delete the Guardrail, Knowledge Base, and S3 bucket to avoid unnecessary charges!

















Top comments (0)