Introduction
In this article, we will go through step-by-step instructions to create a .NET console application that uses the Converse API with a guardrail to block inappropriate content. This guide will help you understand how to call the Converse API, configure the guardrail, and process the response. To learn more about guardrail in Amazon Bedrock refer to this documentation.
Note. Refer to this article to create a guardrail for Amazon Bedrock using the .NET console application.
Prerequisites
- 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.
- Download and install the AWS Command Line Interface (CLI).
- Configure the AWS CLI.
- Download and install Visual Studio or Visual Studio Code.
- Download and install .NET 8.0 SDK
- 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 .NET console application in Visual Studio 2022 to block inappropriate content using Converse API with a guardrail.
- Open Visual Studio 2022.
- Click File -> New -> Project.
- Select the Console App template. Click Next.
- Enter the project name and click Next.
- Select the .NET 8.0 framework. Click Create.
- Add the following NuGet packages.
AWSSDK.BedrockRuntime
- Open Program. cs and replace the code with the following.
using Amazon;
using Amazon.BedrockRuntime;
using Amazon.BedrockRuntime.Model;
using Amazon.Runtime;
namespace AmazonBedrockGuardrailConverseAPI
{
internal class Program
{
static async Task Main(string[] args)
{
string modelId = "meta.llama3-8b-instruct-v1:0";
string guardrailId = "qyyyr31ncqt4";
string guardrailVersion = "DRAFT";
string text = "How to hurt someone?";
string contextText = "No Violence";
// Define the guardrail configuration
var guardrailConfig = new GuardrailConfiguration
{
GuardrailIdentifier = guardrailId,
GuardrailVersion = guardrailVersion,
Trace = "enabled"
};
// Create the message content
var contentBlocks = new List<ContentBlock>
{
new ContentBlock
{
Text = contextText
},
new ContentBlock
{
GuardContent = new GuardrailConverseContentBlock
{
Text = new GuardrailConverseTextBlock
{
Text = text
}
}
}
};
var messages = new List<Message>
{
new Message
{
Role = "user",
Content = contentBlocks
}
};
try
{
// Initialize the Amazon Bedrock Runtime client
var client = new AmazonBedrockRuntimeClient(RegionEndpoint.USEast1);
var response = await GenerateConversationAsync(client, modelId, messages, guardrailConfig);
if (response.StopReason == "guardrail_intervened")
{
Console.WriteLine("Guardrail trace:");
Console.WriteLine(response.Trace.ToString());
}
foreach (var content in response.Output.Message.Content)
{
Console.WriteLine($"Text: {content.Text}");
}
}
catch (AmazonServiceException ex)
{
Console.WriteLine($"Error occurred: {ex.Message}");
}
}
private static async Task<ConverseResponse> GenerateConversationAsync(AmazonBedrockRuntimeClient client, string modelId, List<Message> messages, GuardrailConfiguration guardrailConfig)
{
var request = new ConverseRequest
{
ModelId = modelId,
Messages = messages,
GuardrailConfig = guardrailConfig
};
return await client.ConverseAsync(request);
}
}
}
- Hit F5 to execute the application.
Guardrail trace:
Amazon.BedrockRuntime.Model.ConverseTrace
Text: Sorry, the model cannot answer this question.
Summary
This article describes how to create a .NET console application that uses the Converse API with a guardrail in Visual Studio 2022.