Friday, March 15, 2024

Using Claude 3 Haiku Vision with Amazon Bedrock Locally

 This video is a hands-on guide as how to use vision features of Anthropic's Claude 3 Haiku AI model with Amazon Bedrock.



Code Used:

import boto3
import json
import base64
from botocore.exceptions import ClientError

bedrock = boto3.client(service_name="bedrock-runtime",region_name='us-east-1')

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

accept = "application/json"
contentType = "application/json"


# prompt = "What is written in this image?"
# image_path = "./images/ab55.png"

# prompt = "How many faces are there in this image and what are the expressions of those faces?"
# image_path = "./images/expression.png"

# prompt = "Tell me a short story about this image."
# image_path = "./images/hiking.png"

prompt = "What's the location in this image?"
image_path = "./images/whereisthis.png"


with open(image_path, "rb") as image_file:
    image = base64.b64encode(image_file.read()).decode("utf8")

request_body = {
    "anthropic_version": "bedrock-2023-05-31",
    "max_tokens": 2048,
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": prompt,
                },
                {
                    "type": "image",
                    "source": {
                        "type": "base64",
                        "media_type": "image/png",
                        "data": image,
                    },
                },
            ],
        }
    ],
}

try:
    response = bedrock.invoke_model(
        modelId=modelId,
        body=json.dumps(request_body),
    )

    # Process and print the response
    result = json.loads(response.get("body").read())
    input_tokens = result["usage"]["input_tokens"]
    output_tokens = result["usage"]["output_tokens"]
    output_list = result.get("content", [])

    # print("Invocation details:")
    # print(f"- The input length is {input_tokens} tokens.")
    # print(f"- The output length is {output_tokens} tokens.")

    # print(f"- The model returned {len(output_list)} response(s):")
    for output in output_list:
        print(output["text"])

except ClientError as err:
    print(
        "Couldn't invoke Claude 3 Haiku Vision. Here's why: %s: %s",
        err.response["Error"]["Code"],
        err.response["Error"]["Message"],
    )
    raise

No comments: