Disclaimer: The perspectives shared here are my own and do not necessarily represent those of my employer. I use GenAI as a tool to help me compose and structure my articles.
Integrating AWS Bedrock with Claude 3.5 Sonnet v2 in Python applications can be challenging due to limited documentation and examples. After exploring various approaches and overcoming common pitfalls, I’ve developed a straightforward implementation that reliably connects to and utilises the model’s capabilities in the most simplistic way through AWS Bedrock’s API.
Prerequisites
Before we begin, make sure you have:
- An active AWS account
- AWS CLI installed and configured
- Python 3.7 or later installed
- Basic familiarity with Python and AWS services
Step 1: Enable AWS Bedrock Access
- Log into your AWS Management Console
- Navigate to the AWS Bedrock service
- Click on “Model access” in the left navigation panel under “Bedrock configurations”
- Find “Anthropic — Claude 3.5 Sonnet v2” and request access through the “Available to request” link
- Wait for the model access to be granted (this may take a few minutes)
Step 2: Install Required Dependencies
Open your terminal and install the required Python packages (assuming you are running MacOS):
python -m venv .venv
source .venv/bin/activate
pip install boto3
Step 3: Set Up AWS Credentials
I’ll leave that to you, but a handy way is to use aws-vault, which provides enhanced security by managing temporary credentials and supporting SSO login too. Great for development.
Step 4: Create a Python Script to Connect to Bedrock
Create a new Python file (e.g., bedrock_client.py) and add the following code:
import json
import boto3
MODEL_ID = "us.anthropic.claude-3-5-sonnet-20241022-v2:0"
bedrock_runtime = boto3.client("bedrock-runtime")
with open("my_image.png", "rb") as f:
my_image = f.read()
system_prompt = '''
You are a specialised agent that is able to understand images.
Answer any questions that are asked about the images analysed.
'''
user_prompt = "Describe how many objects you can see in this image."
messages = [
{
"role": "user",
"content": [
{
"text": user_prompt
},
{
"image": {
"format": "png",
"source": {
"bytes": my_image
}
}
},
],
}
]
response = bedrock_runtime.converse(
system = [
{
"text": system_prompt
}
],
modelId = MODEL_ID,
messages = messages
)
response_text = response["output"]["message"]["content"][0]["text"]
print(response_text)
This code provides an example of how to include text and images under a single prompt, and how to provide a system prompt too.
Run the Script
Execute your script:
python bedrock_client.py
And that’s it!
Next Steps
Now that you have a basic connection to AWS Bedrock and Claude 3.5 Sonnet working, you can:
- Implement more complex conversation flows
- Add streaming responses for real-time interactions (use the
assistantrole to append responses tomessages) - Integrate the client into your larger applications
- Explore different model parameters and capabilities
Conclusion
AWS Bedrock provides a streamlined way to access powerful language models like Claude 3.5 Sonnet v2. With this setup, you can now leverage the model’s capabilities in your applications while maintaining the security and scalability benefits of AWS infrastructure.
Remember to check Anthropic Claude Messages API documentation for the latest updates and best practices, as the service and model capabilities continue to evolve.