0

I'm trying to upload an image to AWS Bedrock Claude model using the ConverseCommand (in @aws-sdk/client-bedrock-runtime 3.699.0).

I have the base64 version of image (eg https://yulvil.github.io/gopherjs/02/)

According to the documentation, it requires the bytes as a Uint8Array, so I've tried

  1. bytes: new Uint8Array(Buffer.from(img, 'utf-8'))
  2. bytes: Buffer.from(img, 'utf-8')
  3. bytes: new TextEncoder().encode(ing)

None seem to work having error ValidationException: The model returned the following errors: messages.0.content.1.image.source.bytes: Invalid image input

What am I missing?

const {
  BedrockRuntimeClient,
  ConverseCommand,
} = require("@aws-sdk/client-bedrock-runtime");

const modelId = "anthropic.claude-3-haiku-20240307-v1:0";

let conversation = [];

const describeImage = async () => {
  const client = new BedrockRuntimeClient({
    region: "us-east-1",
    credentials: {
      accessKeyId: process.env.AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
    },
  });

  let content = [];

  const img = “data:image/png;base64,...”

  content.push({
    image: {
      format: "png",
      source: {
        bytes: img,
      },
    },
  });
  content.push({ text: “Describe the given image” });
  conversation.push({
    role: "user",
    content,
  });

  try {
    const response = await client.send(
      new ConverseCommand({
        modelId,
        messages: conversation,
      })
    );
    return response.output.message.content[0].text;
  } catch (err) {
    console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`);
  }

describeImage();

1 Answer 1

0

Figured it out, need to strip the header first and then create a Buffer from base64

bytes: new Uint8Array(Buffer.from(img.replace(/^data:image\/\w+;base64,/, ''), 'base64'))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.