

# Safeguard your Amazon Bedrock app with a guardrail
<a name="guardrails"></a>

Guardrails for Amazon Bedrock lets you implement safeguards for your Amazon Bedrock in SageMaker Unified Studio app based on your use cases and responsible AI policies. You can create multiple guardrails tailored to different use cases and apply them across multiple foundation models, providing a consistent user experience and standardizing safety controls across generative AI apps. You can configure denied topics to disallow undesirable topics and content filters to block harmful content in the prompts you send to a model and to the responses you get from a model. You can use guardrails with text-only foundation models. For more information, see [Safeguard your Amazon Bedrock app with a guardrail](#guardrails).

You can use guardrails with Amazon Bedrock in SageMaker Unified Studio chat agent app and with flow apps. With a chat agent app you can create guardrail component when you [create the chat agent app](create-chat-app-with-components.md#chat-app-add-guardrail) or you can add a guardrail component that you have previously created. For more information, see [Create an Amazon Bedrock guardrail component](creating-a-guardrail-component.md).

With a flow app, you can add a guardrail to [prompt]() nodes and to [knowledge base]() nodes. 

**Topics**
+ [

## Guardrail policies
](#guardrails-filters)
+ [

# Create an Amazon Bedrock guardrail component
](creating-a-guardrail-component.md)
+ [

# Add an Amazon Bedrock guardrail component to a chat agent app
](add-guardrail-component-chat-app.md)
+ [

# Add an Amazon Bedrock guardrail component to a flow app
](add-guardrail-component-flow-app.md)

## Guardrail policies
<a name="guardrails-filters"></a>

A guardrail consists of the following policies to avoid content that falls into undesirable or harmful categories.
+ Content filters – Adjust filter strengths to filter input prompts or model responses containing harmful content.
+ Denied topics – You can define a set of topics that are undesirable in the context of your app. These topics will be blocked if detected in user queries or model responses.

### Content filters
<a name="guardrails-studio-content-filters"></a>

Guardrails in Amazon Bedrock in SageMaker Unified Studio support the following content filters to detect and filter harmful user inputs and FM-generated outputs.
+ **Hate** – Describes language or a statement that discriminates, criticizes, insults, denounces, or dehumanizes a person or group on the basis of an identity (such as race, ethnicity, gender, religion, sexual orientation, ability, and national origin).
+ **Insults** – Describes language or a statement that includes demeaning, humiliating, mocking, insulting, or belittling language. This type of language is also labeled as bullying.
+ **Sexual** – Describes language or a statement that indicates sexual interest, activity, or arousal using direct or indirect references to body parts, physical traits, or sex.
+ **Violence** – Describes language or a statement that includes glorification of or threats to inflict physical pain, hurt, or injury toward a person, group or thing.

Content filtering depends on the confidence classification of user inputs and FM responses across each of the four harmful categories. All input and output statements are classified into one of four confidence levels (NONE, LOW, MEDIUM, HIGH) for each harmful category. For example, if a statement is classified as *Hate* with HIGH confidence, the likelihood of the statement representing hateful content is high. A single statement can be classified across multiple categories with varying confidence levels. For example, a single statement can be classified as *Hate* with HIGH confidence, *Insults* with LOW confidence, *Sexual* with NONE confidence, and *Violence* with MEDIUM confidence.

For each of the harmful categories, you can configure the strength of the filters. The filter strength determines the degree of filtering harmful content. As you increase the filter strength, the likelihood of filtering harmful content increases and the probability of seeing harmful content in your app reduces. The following table shows the degree of content that each filter strength blocks and allows.


****  

| Filter strength | Blocked content confidence | Allowed content confidence | 
| --- | --- | --- | 
| None | No filtering | None, Low, Medium, High | 
| Low | High | None, Low, Medium | 
| Medium | High, Medium | None, Low | 
| High | High, Medium, Low | None | 

### Denied topics
<a name="guardrails-topic-policies"></a>

Guardrails can be configured with a set of denied topics that are undesirable in the context of your generative AI app. For example, a bank may want their online assistant to avoid any conversation related to investment advice or engage in conversations related to fraudulent activities such as money laundering. 

You can define up to five denied topics. Input prompts and model completions will be evaluated against each of these topics. If one of the topics is detected, the blocked message configured as part of the guardrail will be returned to the user.

Denied topics can be defined by providing a natural language definition of the topic along with a few optional example phrases of the topic. The definition and example phrases are used to detect if an input prompt or a model completion belongs to the topic.

Denied topics are defined with the following parameters.
+ Name – The name of the topic. The name should be a noun phrase. Don't describe the topic in the name. For example:
  + **Investment Advice**
+ Definition – Up to 200 characters summarizing the topic content. The description should describe the content of the topic and its subtopics.
**Note**  
For best results, adhere to the following principles:  
Don't include examples or instructions in the description.
Don't use negative language (such as "don't talk about investment" or "no content about investment").

  The following is an example topic description that you can provide:
  + **Investment advice refers to inquires, guidance or recommendations regarding the management or allocation of funds or assets with the goal of generating returns or achieving specific financial objectives.**
+ Sample phrases – A list of up to five sample phrases that refer to the topic. Each phrase can be up to 1,000 characters. An sample is a prompt or continuation that shows what kind of content should be filtered out. For example:
  + **Is investing in the stocks better than bonds?**
  + **Should I invest in gold?**

# Create an Amazon Bedrock guardrail component
<a name="creating-a-guardrail-component"></a>

You can create a guardrail as a component in an Amazon Bedrock in SageMaker Unified Studio project. You can then add the guardrail component to a chat agent app. You can also create a guardrail component while you are creating a chat agent app. For an example, see [Step 2: Add a guardrail to your chat agent app](create-chat-app-with-components.md#chat-app-add-guardrail).

**To create a guardrail component**

1. Navigate to the Amazon SageMaker Unified Studio landing page by using the URL from your administrator.

1. Access Amazon SageMaker Unified Studio using your IAM or single sign-on (SSO) credentials. For more information, see [Access Amazon SageMaker Unified Studio](getting-started-access-the-portal.md).

1. Choose the **Build** menu at the top of the page.

1. In the **MACHINE LEARNING & GENERATIVE AI** section, choose **My apps**.

1. In the **Select or create a new project to continue** dialog box, select the project that you want to use.

1. In the left pane, choose **Asset gallery**.

1. Choose **My components**.

1. In the **Components** section, choose **Create component** and then **Guardrail**. The **Create guardrail** pane is shown.

1. For **Guardrail name**. enter a name for the guardrail.

1. For **Guardrail description** enter a description for the guardrail.

1. In **Content filters** do the following.

   1. Select **Enable content filters** to turn on content filtering. 

   1. For **Filter for prompts**, choose the filters that you want to apply to prompts. For more information, see [Content filters](guardrails.md#guardrails-studio-content-filters).

   1. If you want the filter to apply to responses that the model generates, select **Apply the same filters for responses**.

1. In **Blocked messsaging** do the following:

   1. For **Blocked messaging for prompts** enter a message to display when the guardrail blocks content in the prompt. 

   1. If you want to show a different message when the guardrail blocks content from a model's response, do the following:

      1. Clear **Apply the same message for blocked responses**.

      1. For **Blocked messaging for responses**, enter a message to display when the guardrail blocks content in the response from the model.

1. Add a denied topic filter by doing the following: 

   1. Choose **Use advanced features**.

   1. Choose **Denied topics**.

   1. Choose **Add topic**.

   1. For **Name**, enter a name for the filter.

   1. For **Definition for topic**, enter a definition for the content that you want to deny.

   1. (Optional) To help guide the guardrail, do the folllowing: 

      1. Choose **Sample phrases - optional**.

      1. For **Sample phrases**, enter a phrase.

      1. Choose **Add phrase**.

      1. Add up to four more phrases by repeating the previous two steps.

      1. Choose **Save**.

   For information about denied topics, see [Denied topics](guardrails.md#guardrails-topic-policies).

1. Choose **Create** to create the guardrail.

1. Add the guardrail component to a chat agent app by doing [Add an Amazon Bedrock guardrail component to a chat agent app](add-guardrail-component-chat-app.md).

# Add an Amazon Bedrock guardrail component to a chat agent app
<a name="add-guardrail-component-chat-app"></a>

In this procedure, you add a guardrail component to an existing [chat agent app](create-chat-app.md).

1. Navigate to the Amazon SageMaker Unified Studio landing page by using the URL from your administrator.

1. Access Amazon SageMaker Unified Studio using your IAM or single sign-on (SSO) credentials. For more information, see [Access Amazon SageMaker Unified Studio](getting-started-access-the-portal.md).

1. If the project that you want to use isn't already open, do the following:

   1. Choose the current project at the top of the page. If a project isn't already open, choose **Select a project**.

   1. Select **Browse all projects**. 

   1. In **Projects** select the project that you want to use.

1. Choose the **Build** menu option at the top of the page.

1. In **MACHINE LEARNING & GENERATIVE AI** choose **My apps**.

1. In **Apps** choose the chat agent app that you want to add the guardrail to.

1. In the **Configs** pane, choose **Guardrails**.

1. For **Guardrails**, select the guardrail component that you created in [Create an Amazon Bedrock guardrail component](creating-a-guardrail-component.md).

1. (Optional) Preview the guardail by choosing **Preview**. From the preview you can edit the guardrail, if desired.

1. Choose **Save** to save your changes.

# Add an Amazon Bedrock guardrail component to a flow app
<a name="add-guardrail-component-flow-app"></a>

In this procedure, you add a guardrail component to the [knowledge base](nodes.md#flow-node-kb) node that you create in step 2 of [Create a flow app with Amazon Bedrock](build-flow.md). You can also add a guardrail to a [prompt](nodes.md#flow-node-prompt) node. To add a guardrail to a prompt node, use the following steps. For step 7, select the prompt node (**Playlist\$1generator\$1node**).

1. Navigate to the Amazon SageMaker Unified Studio landing page by using the URL from your administrator.

1. Access Amazon SageMaker Unified Studio using your IAM or single sign-on (SSO) credentials. For more information, see [Access Amazon SageMaker Unified Studio](getting-started-access-the-portal.md).

1. If the project that you want to use isn't already open, do the following:

   1. Choose the current project at the top of the page. If a project isn't already open, choose **Select a project**.

   1. Select **Browse all projects**. 

   1. In **Projects** select the project that you want to use.

1. Choose the **Build** menu option at the top of the page.

1. In **MACHINE LEARNING & GENERATIVE AI** choose **My apps**.

1. open the flow app that you created in [Create a flow app with Amazon Bedrock](build-flow.md).

1. In the canvas, select the knowledge base node (**Local\$1bands\$1knowledge\$1base**).

1. In the **flow builder** pane, choose the configure tab.

1. In the **Guardrail details** section do one of the following: 
   + Select an existing guardrail to use
   + Choose **Create a new guardrail** to create a new guardrail. Then do the following: 

     1. For **Guardrail name**. enter a name for the guardrail.

     1. For **Guardrail description** enter a description for the guardrail.

     1. In **Content filters** do the following.

        1. Select **Enable content filters** to turn on content filtering. 

        1. For **Filter for prompts**, choose the filters that you want to apply to prompts. For more information, see [Content filters](guardrails.md#guardrails-studio-content-filters).

        1. If you want the filter to apply to responses that the model generates, select **Apply the same filters for responses**.

     1. In **Blocked messsaging** do the following:

        1. For **Blocked messaging for prompts** enter a message to display when the guardrail blocks content in the prompt. 

        1. If you want to show a different message when the guardrail blocks content, do the following:

           1. Clear **Apply the same message for blocked responses**.

           1. For **Blocked messaging for responses**, enter a message to display when the guardrail blocks content in the response from the model.

     1. Add a denied topic filter by doing the following: 

        1. Choose **Use advanced features**.

        1. Choose **Denied topics**.

        1. Choose **Add topic**.

        1. For **Name**, enter a name for the filter.

        1. For **Definition for topic**, enter a definition for the content that you want to deny.

        1. (Optional) To help guide the guardrail, do the folllowing: 

           1. Choose **Sample phrases - optional**.

           1. For **Sample phrases**, enter a phrase.

           1. Choose **Add phrase**.

           1. Add up to four more phrases by repeating the previous two steps.

           1. Choose **Save**.

        For information about denied topics, see [Denied topics](guardrails.md#guardrails-topic-policies).

     1. Choose **Create** to create the guardrail.

     1. Add the guardrail component to a chat agent app by doing [Add an Amazon Bedrock guardrail component to a chat agent app](add-guardrail-component-chat-app.md).

1. Choose **Save** to save your changes to your flow app.

1. Test your prompt by doing the following:

   1. On right side of the app flow page, choose **<** to open the test pane.

   1. In **Enter prompt**, enter a phrase that violates your guardrail.

   1. Press Enter on the keyboard or choose the run button to test the prompt. 

   1. In the response you should see the text **guardrail applied** under the affected prompt. Choose the prompt to see the reason why the guardrail rejected the prompt.