Invoke Anthropic Claude 3 on Amazon Bedrock with a multimodal prompt - Amazon Bedrock

Invoke Anthropic Claude 3 on Amazon Bedrock with a multimodal prompt

The following code example shows how to invoke Anthropic Claude 3 on Amazon Bedrock with a multimodal prompt.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Invoke Anthropic Claude 3 with a multimodal prompt to analyze an image.

def invoke_claude_3_multimodal(self, prompt, base64_image_data): """ Invokes Anthropic Claude 3 Sonnet to run a multimodal inference using the input provided in the request body. :param prompt: The prompt that you want Claude 3 to use. :param base64_image_data: The base64-encoded image that you want to add to the request. :return: Inference response from the model. """ # Initialize the Amazon Bedrock runtime client client = self.client or boto3.client( service_name="bedrock-runtime", region_name="us-east-1" ) # Invoke the model with the prompt and the encoded image model_id = "anthropic.claude-3-sonnet-20240229-v1:0" 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": base64_image_data, }, }, ], } ], } try: response = client.invoke_model( modelId=model_id, 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"]) return result except ClientError as err: logger.error( "Couldn't invoke Claude 3 Sonnet. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • For API details, see InvokeModel in AWS SDK for Python (Boto3) API Reference.

For a complete list of AWS SDK developer guides and code examples, see Using this service with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.