- Newest
- Most votes
- Most comments
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:
- Create a content array in your message object.
- Add an object for the image with "type": "image" and the base64 data.
- 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
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
Relevant content
- AWS OFFICIALUpdated 2 months ago
