Introduction
In this article, you'll learn how to send a document as part of a message using the Amazon Bedrock Converse API with the Anthropic Claude 3 Sonnet model and request that the model describe the contents of the document.
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
- Access to Amazon Bedrock foundation model
Tools
- Visual Studio 2022
Steps Involved
Perform the following steps to create a .NET console application in Visual Studio 2022 to send a document as part of a message and request that the model describe the contents of the document.
- 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;
namespace AmazonBedrockConverseApp
{
internal class Program
{
// Constants for the model ID, input text, and input document path
private const string ModelId = "anthropic.claude-3-sonnet-20240229-v1:0";
private const string InputText = "What's in this document?";
private const string InputDocumentPath = "C:\\Users\\admin\\source\\repos\\AmazonBedrockConverseApp\\India.txt";
static async Task Main(string[] args)
{
// Configure the Amazon Bedrock Runtime client
var config = new AmazonBedrockRuntimeConfig
{
RegionEndpoint = RegionEndpoint.USEast1 // Use your region
};
using var bedrockClient = new AmazonBedrockRuntimeClient(config);
try
{
// Generate a message and print the response
var response = await GenerateMessageAsync(bedrockClient, ModelId, InputText, InputDocumentPath);
PrintResponse(response);
}
catch (AmazonBedrockRuntimeException ex)
{
// Handle any errors that occur during the conversation
Console.WriteLine($"Error: {ex.Message}");
}
}
/// <summary>
/// Generates a message using the Bedrock Runtime client.
/// </summary>
/// <param name="bedrockClient">The Bedrock Runtime client.</param>
/// <param name="modelId">The model ID to use.</param>
/// <param name="inputText">The input text for the message.</param>
/// <param name="inputDocumentPath">The path to the input document file.</param>
/// <returns>The response from the Bedrock Runtime.</returns>
private static async Task<ConverseResponse> GenerateMessageAsync(IAmazonBedrockRuntime bedrockClient, string modelId, string inputText, string inputDocumentPath)
{
// Read the document file as a byte array and convert to a MemoryStream
byte[] documentBytes = await File.ReadAllBytesAsync(inputDocumentPath);
using var documentStream = new MemoryStream(documentBytes);
// Create the message with text and document content
var message = new Message
{
Role = "user",
Content = new List<ContentBlock>
{
new()
{
Text = inputText
},
new()
{
Document = new DocumentBlock
{
Name = "MyDocument",
Format = DocumentFormat.Txt,
Source = new DocumentSource { Bytes = documentStream }
}
}
}
};
// Create the Converse request
var request = new ConverseRequest
{
ModelId = modelId,
Messages = new List<Message> { message }
};
// Send the Converse request and return the response
return await bedrockClient.ConverseAsync(request);
}
/// <summary>
/// Prints the response from the Bedrock Runtime to the console.
/// </summary>
/// <param name="response">The response from the Bedrock Runtime.</param>
private static void PrintResponse(ConverseResponse response)
{
Console.WriteLine("Role: " + response.Output.Message.Role);
// Print each content block in the response
foreach (var content in response.Output.Message.Content)
{
if (content.Text != null)
{
Console.WriteLine("Text: " + content.Text);
}
}
// Print the usage statistics
Console.WriteLine("Input tokens: " + response.Usage.InputTokens);
Console.WriteLine("Output tokens: " + response.Usage.OutputTokens);
Console.WriteLine("Total tokens: " + response.Usage.TotalTokens);
Console.WriteLine("Stop reason: " + response.StopReason);
}
}
}
- Press F5 to run the application. You should see the output from the model describing the contents of the document.
Summary
This article describes how to create a .NET console application to send a document as part of a message using the Amazon Bedrock Converse API with the Anthropic Claude 3 Sonnet model and requests that the model describe the contents of the document.in Visual Studio 2022.