Build Image Description Service with Amazon Bedrock and .NET App

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

  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. 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.

  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 packages.
    AWSSDK.BedrockRuntime
    
  7. 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);
            }
        }
    }
    
  8. Press F5 to run the application. You should see the output from the model describing the contents of the image.
    Application

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.


Similar Articles