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}");
}
}
}
}