Introduction
In this article, you'll learn how to send an image as part of a message using the Amazon Bedrock Converse API with the Anthropic Claude 3 Sonnet model and receive a description of the image.
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 an image to the model and receive a description of the image.
- 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 image path
private const string ModelId = "anthropic.claude-3-sonnet-20240229-v1:0";
private const string InputText = "What's in this image?";
private const string InputImagePath = "C:\\Users\\admin\\source\\repos\\AmazonBedrockConverseApp\\sample.png";
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 conversation and print the response
var response = await GenerateConversationAsync(bedrockClient, ModelId, InputText, InputImagePath);
PrintResponse(response);
}
catch (AmazonBedrockRuntimeException ex)
{
// Handle any errors that occur during the conversation
Console.WriteLine($"Error: {ex.Message}");
}
}
/// <summary>
/// Generates a conversation 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 conversation.</param>
/// <param name="inputImagePath">The path to the input image file.</param>
/// <returns>The response from the Bedrock Runtime.</returns>
private static async Task<ConverseResponse> GenerateConversationAsync(
IAmazonBedrockRuntime bedrockClient,
string modelId,
string inputText,
string inputImagePath)
{
// Read the image file as a byte array and convert to a MemoryStream
byte[] imageBytes = await File.ReadAllBytesAsync(inputImagePath);
using var imageStream = new MemoryStream(imageBytes);
// Create the message with text and image content
var message = new Message
{
Role = "user",
Content = new List<ContentBlock>
{
new ContentBlock
{
Text = inputText
},
new ContentBlock
{
Image = new ImageBlock
{
Format = ImageFormat.Png,
Source = new ImageSource { Bytes = imageStream }
}
}
}
};
// 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 image.
Summary
This article describes how to create a .NET console application to send an image as part of a message using the Amazon Bedrock Converse API with the Anthropic Claude 3 Sonnet model and receive a description of the image in Visual Studio 2022.