Skip to content

Does claude 3 sonnet support extracting text fields from images?

0

I'm sending a request with a prompt like this:

` StringBuilder sb = new StringBuilder("text: Extract the all of the fields from this base64 image and return in a json.") .append(" ,MIME type: image/png") .append(",base64 image:") .append(base64Image);

        String prompt = sb.toString();
        JSONObject message = new JSONObject();
        message.put("role", "user");
        message.put("content", prompt);
        JSONArray messagesArray = new JSONArray();
        messagesArray.put(message);
        JSONObject inferenceBody = new JSONObject()
            .put("anthropic_version", "bedrock-2023-05-31")
            .put("max_tokens", 4096)
            .put("messages", messagesArray)
            .put("temperature", 0.0);

//

    String modelId = "anthropic.claude-3-5-sonnet-20240620-v1:0";

        InvokeModelRequest request = InvokeModelRequest.builder()
            .modelId(modelId)
            .contentType("application/json")
            .accept("application/json")
            .body(SdkBytes.fromUtf8String(inferenceBody.toString()))
            .build();`

Where base64image is a base64 encoded string.

1/2 the time, I get a response back like this:

{"id":"msg_bdrk_01Yb3nNb1c7N7eBkCyLNAvCM","type":"message","role":"assistant","model":"claude-3-5-sonnet-20240620","content":[{"type":"text","text":"I apologize, but I am not able to extract fields from or analyze images directly. I can only perceive and analyze text that is provided to me in text form. If you have specific information from the image that you would like me to analyze or respond to, please describe the relevant details in text form and I would be happy to assist."}],"stop_reason":"end_turn","stop_sequence":null,"usage":{"input_tokens":94150,"output_tokens":73}}

The other 1/2 the time, I get back extracted fields with fake data.

3 Answers
0

Claude 3 Sonnet does support extracting text fields from images, but there seems to be an issue with how you're formatting your request. The problem likely lies in how you're passing the image data to the model.

For Claude 3 models, including Sonnet, you need to use a specific format for image inputs in the messages array. Instead of including the base64 image data directly in the text prompt, you should structure your message content as an array with separate entries for the text prompt and the image data.

Here's how you should structure your message content:

  1. Create a content array in your message object.
  2. Add an object for the image with "type": "image" and the base64 data.
  3. Add another object for your text prompt with "type": "text".

Your message structure should look something like this:

{
  "role": "user",
  "content": [
    {
      "type": "image",
      "source": {
        "type": "base64",
        "media_type": "image/png",
        "data": "YOUR_BASE64_IMAGE_DATA_HERE"
      }
    },
    {
      "type": "text",
      "text": "Extract all of the fields from this image and return in a JSON format."
    }
  ]
}

By structuring your request in this way, Claude 3 Sonnet should be able to consistently process the image and extract the text fields. The inconsistent behavior you're experiencing (sometimes getting a response about not being able to analyze images, other times getting extracted fields with fake data) suggests that the model isn't always recognizing the image data in your current format.

Make sure to adjust your code to create this structure for the message content. Once you do this, you should get more consistent results with Claude 3 Sonnet extracting text fields from your images.

Sources
Community | Get Started with Claude 3 LLMs on Amazon Bedrock
Anthropic Claude 3 Sonnet: vision capabilities | AWS re:Post

answered 2 years ago

0

This doesn't work either.

` JSONObject image = new JSONObject(); image.put("type", "base64"); image.put("data", base64Image); image.put("media_type", "image/png");

        JSONObject content = new JSONObject();
        content.put("type", "image");
        content.put("source", image);


        JSONObject textContent = new JSONObject();
        content.put("type", "text");
        content.put("text", "Extract the all of the fields from this base64 image and return in a json.");

        JSONArray contentArray = new JSONArray();
        contentArray.put(content);
        contentArray.put(textContent);

        JSONObject message = new JSONObject();
        message.put("role", "user");
        message.put("content", contentArray);

        JSONObject inferenceBody = new JSONObject()
            .put("anthropic_version", "bedrock-2023-05-31")
            .put("max_tokens_to_sample", 4096)
            .put("prompt", message)
            .put("temperature", 0.0);

// InvokeModelRequest request = InvokeModelRequest.builder() .modelId(modelId) .contentType("application/json") .accept("application/json") .body(SdkBytes.fromUtf8String(inferenceBody.toString())) .build();

        CompletableFuture<InvokeModelResponse> future = client.invokeModel(request);

`

answered 2 years ago

0

I got it working in the standalone using the messages API

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.