Skip to content

How can I pass attachments using Claude 3.5 API?

0

Hello, Can anyone guide me in how to invoke bedrock claude 3.5 API with attachments? The following is not working and errors with RuntimeError: Bedrock API call failed: An error occurred (ValidationException) when calling the InvokeModel operation: messages.0.content.1.image.source.media_type: Field required This is my code

class BedrockAttachmentHandler:
    def __init__(self, session: boto3.Session, region: str = 'us-east-1'):
        self.bedrock_client = session.client('bedrock-runtime')

    def invoke_model_with_files(self, prompt: str, file_paths: List[str]) -> Dict:
        """
        Send files to Claude 3.5 using base64 encoding
        """
        messages_content = []
        
        # Add the text prompt
        messages_content.append({
            "type": "text",
            "text": prompt
        })

        # Add each file as base64
        for file_path in file_paths:
            try:
                with open(file_path, 'rb') as file:
                    file_content = file.read()
                    # Detect mime type
                    mime_type = mimetypes.guess_type(file_path)[0]
                    if not mime_type:
                        # Default mime types if not detected
                        if file_path.endswith('.pdf'):
                            mime_type = 'application/pdf'
                        elif file_path.endswith('.docx'):
                            mime_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
                        else:
                            mime_type = 'application/octet-stream'
                    
                    messages_content.append({
                        "type": "image",
                        "source": {
                            "type": "base64",
                            "mime_type": mime_type,
                            "data": base64.b64encode(file_content).decode('utf-8')
                        }
                    })
            except Exception as e:
                raise RuntimeError(f"Failed to process file {file_path}: {str(e)}")

        request_body = {
            "anthropic_version": "bedrock-2023-05-31",
            "max_tokens": 10000,
            "messages": [{
                "role": "user",
                "content": messages_content
            }]
        }

        try:
            response = self.bedrock_client.invoke_model(
                modelId="anthropic.claude-3-5-sonnet-20240620-v1:0",
                body=json.dumps(request_body)
            )
            return json.loads(response['body'].read())
        except Exception as e:
            raise RuntimeError(f"Bedrock API call failed: {str(e)}")

asked 2 years ago1.5K views

2 Answers
0

To pass attachments using the Claude 3.5 API via Amazon Bedrock, you need to make a few adjustments to your code. Here are the key changes:

  1. The "type" for image content should be "image", not "image".
  2. The key for specifying the image format should be "media_type", not "mime_type".
  3. Make sure you're using the correct model ID for Claude 3.5.

Here's the corrected version of the relevant part of your code:

messages_content.append({
    "type": "image",
    "source": {
        "type": "base64",
        "media_type": mime_type,
        "data": base64.b64encode(file_content).decode('utf-8')
    }
})

request_body = {
    "anthropic_version": "bedrock-2023-05-31",
    "max_tokens": 10000,
    "messages": [{
        "role": "user",
        "content": messages_content
    }]
}

response = self.bedrock_client.invoke_model(
    modelId="anthropic.claude-3-sonnet-20240229-v1:0",
    body=json.dumps(request_body)
)

Make sure you're using the correct model ID for Claude 3.5. As of the latest information available, the model ID for Claude 3 Sonnet is "anthropic.claude-3-sonnet-20240229-v1:0".

Also, ensure that you're converting your image to a base64 string correctly. The API expects the image data to be in this format.

Remember to handle potential errors, such as unsupported file types or oversized attachments. Claude 3.5 supports various image formats, but there may be limitations on file sizes or types.

After making these adjustments, your code should be able to successfully pass attachments to the Claude 3.5 API via Amazon Bedrock.
Sources
Community | Get Started with Claude 3 LLMs on Amazon Bedrock
Community | Sending images to Claude 3 using Amazon Bedrock.

answered 2 years ago

  • I tried that and I get provisioned througput error, with the model ID anthropic.claude-3-sonnet-20240229-v1:0. I can use the 3.5 API with ondemand provisioning.

    RuntimeError: Bedrock API call failed: An error occurred (ValidationException) when calling the InvokeModel operation: Invocation of model ID anthropic.claude-3-sonnet-20240229-v1:0 with on-demand throughput isn’t supported. Retry your request with the ID or ARN of an inference profile that contains this model.

0

application/pdf is supported as a correct media_type on Anthropic model. Refer to the documentation here : https://docs.aws.amazon.com/bedrock/latest/userguide/model-parameters-anthropic-claude-messages.html#api-inference-examples-claude-messages-code-examples

media_type – (required) The type of the image. You can specify the following image formats.

  • image/jpeg
  • image/png
  • image/webp
  • image/gif
AWS

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.