Create a guardrail for Amazon Bedrock using .NET console application

Introduction

In this article, you will learn how to create a guardrail for Amazon Bedrock to implement safeguards for generative AI applications based on your needs using the .NET console application. To learn more about guardrails in Amazon Bedrock refer to this documentation.

AWS SDK for .NET: AmazonBedrockClient.CreateGuardrail (CreateGuardrailRequest)

Prerequisites

  1. Create an AWS account and log in. Ensure the IAM user you use has sufficient permissions to make necessary AWS service calls and manage AWS resources.
  2. Download and install the AWS Command Line Interface (CLI).
  3. Configure the AWS CLI.
  4. Download and install Visual Studio or Visual Studio Code.
  5. Download and install .NET 8.0 SDK
  6. Ensure that your IAM user or role has the necessary permissions to create and manage guardrails in Amazon Bedrock.

Tools

Visual Studio 2022

Steps Involved

Perform the following steps to create a guardrail for Amazon Bedrock using the .NET console application in Visual Studio 2022.

  1. Open Visual Studio 2022.
  2. Click File -> New -> Project.
  3. Select the Console App template. Click Next.
  4. Enter the project name and click Next.
  5. Select the .NET 8.0 framework. Click Create.
  6. Add the following NuGet package
    AWSSDK.Bedrock

    C#

    Copy

  7. Open Program. cs and replace the code with the following.
    using Amazon;
    using Amazon.Bedrock;
    using Amazon.Bedrock.Model;
    using System;
    using System.Collections.Generic;
    using System.Threading.Tasks;
    namespace AmazonBedrockGuardrailApp
    {
        internal class Program
        {
            static async Task Main(string[] args)
            {
                var region = RegionEndpoint.USEast1; // Specify your desired region
                var client = new AmazonBedrockClient(region);
                var guardrailName = "ExampleGuardrail";
                var guardrailDescription = "This is an example guardrail";
                var clientRequestToken = Guid.NewGuid().ToString(); // Ensure idempotency
                // Create guardrail request
                var createGuardrailRequest = new CreateGuardrailRequest
                {
                    Name = guardrailName,
                    Description = guardrailDescription,
                    ClientRequestToken = clientRequestToken,
                    BlockedInputMessaging = "Sorry, the model cannot answer this question.",
                    BlockedOutputsMessaging = "Sorry, the model cannot answer this question.",
                    ContentPolicyConfig = new GuardrailContentPolicyConfig
                    {
                        FiltersConfig = new List<GuardrailContentFilterConfig>
                        {
                            new GuardrailContentFilterConfig
                            {
                                Type = GuardrailContentFilterType.HATE,
                                InputStrength = GuardrailFilterStrength.HIGH,
                                OutputStrength = GuardrailFilterStrength.HIGH
                            },
                            new GuardrailContentFilterConfig
                            {
                                Type = GuardrailContentFilterType.INSULTS,
                                InputStrength = GuardrailFilterStrength.HIGH,
                                OutputStrength = GuardrailFilterStrength.HIGH
                            },
                            new GuardrailContentFilterConfig
                            {
                                Type = GuardrailContentFilterType.SEXUAL,
                                InputStrength = GuardrailFilterStrength.HIGH,
                                OutputStrength = GuardrailFilterStrength.HIGH
                            },
                            new GuardrailContentFilterConfig
                            {
                                Type = GuardrailContentFilterType.VIOLENCE,
                                InputStrength = GuardrailFilterStrength.HIGH,
                                OutputStrength = GuardrailFilterStrength.HIGH
                            },
                            new GuardrailContentFilterConfig
                            {
                                Type = GuardrailContentFilterType.MISCONDUCT,
                                InputStrength = GuardrailFilterStrength.HIGH,
                                OutputStrength = GuardrailFilterStrength.HIGH
                            },
                            new GuardrailContentFilterConfig
                            {
                                Type = GuardrailContentFilterType.PROMPT_ATTACK,
                                InputStrength = GuardrailFilterStrength.HIGH,
                                OutputStrength = GuardrailFilterStrength.NONE
                            }
                        }
                    },
                    TopicPolicyConfig = new GuardrailTopicPolicyConfig
                    {
                        TopicsConfig = new List<GuardrailTopicConfig>
                        {
                            new GuardrailTopicConfig
                            {
                                Name = "Violence",
                                Type = GuardrailTopicType.DENY,
                                Definition = "Topics related to physical harm or threats.",
                                Examples = new List<string>
                                {
                                    "How to harm someone",
                                    "Violent actions"
                                }
                            },
                            new GuardrailTopicConfig
                            {
                                Name = "Drugs",
                                Type = GuardrailTopicType.DENY,
                                Definition = "Topics related to illegal drug use.",
                                Examples = new List<string>
                                {
                                    "How to use drugs",
                                    "Where to buy illegal drugs"
                                }
                            }
                        }
                    },
                    WordPolicyConfig = new GuardrailWordPolicyConfig
                    {
                        WordsConfig = new List<GuardrailWordConfig>
                        {
                            new GuardrailWordConfig
                            {
                                Text = "offensiveWord1"
                            },
                            new GuardrailWordConfig
                            {
                                Text = "offensiveWord2"
                            }
                        },
                        ManagedWordListsConfig = new List<GuardrailManagedWordsConfig>
                        {
                            new GuardrailManagedWordsConfig
                            {
                                Type = GuardrailManagedWordsType.PROFANITY
                            }
                        }
                    },
                    SensitiveInformationPolicyConfig = new GuardrailSensitiveInformationPolicyConfig
                    {
                        PiiEntitiesConfig = new List<GuardrailPiiEntityConfig>
                        {
                            new GuardrailPiiEntityConfig
                            {
                                Type = GuardrailPiiEntityType.EMAIL,
                                Action = GuardrailSensitiveInformationAction.BLOCK
                            },
                            new GuardrailPiiEntityConfig
                            {
                                Type = GuardrailPiiEntityType.IP_ADDRESS,
                                Action = GuardrailSensitiveInformationAction.BLOCK
                            }
                        },
                        RegexesConfig = new List<GuardrailRegexConfig>
                        {
                            new GuardrailRegexConfig
                            {
                                Name = "CustomRegex1",
                                Description = "Custom pattern for sensitive data",
                                Pattern = @"\b\d{4}-\d{4}-\d{4}-\d{4}\b", // Example pattern for credit card numbers
                                Action = GuardrailSensitiveInformationAction.BLOCK
                            }
                        }
                    },
                    Tags = new List<Tag>
                    {
                        new Tag { Key = "Environment", Value = "Production" }
                    }
                };
                try
                {
                    // Create the guardrail
                    var response = await client.CreateGuardrailAsync(createGuardrailRequest);
                    Console.WriteLine($"Guardrail created successfully with ID: {response.GuardrailId}");
                }
                catch (AmazonBedrockException ex)
                {
                    // Log any error that occurs during guardrail creation
                    Console.WriteLine($"Error creating guardrail: {ex.Message}");
                }
            }
        }
    }

    C#

    Copy

  8. Hit F5 to execute the application. The guardrail is created successfully.

Summary

This article describes how to create a guardrail for Amazon Bedrock using the .NET console application in Visual Studio 2022.

 

Up Next
    Ebook Download
    View all
    Learn
    View all