Amazon Bedrock provides instant access to state-of-the-art foundation models, enabling developers to build AI applications 10x faster. This guide walks through practical implementation using Bedrock's pre-trained models with Python.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"bedrock:ListFoundationModels",
"bedrock:InvokeModel"
],
"Resource": "*"
}
]
}
Attach this policy to your IAM user/role via AWS console.
# Install AWS SDK
pip install boto3
# Configure credentials
aws configure
Enter AWS Access Key ID and Secret Access Key when prompted.
import boto3
import json
bedrock = boto3.client(
service_name='bedrock',
region_name='us-east-1'
)
Supported regions: us-east-1 (N. Virginia), us-west-2 (Oregon)
response = bedrock.list_foundation_models(
byProvider='Anthropic'
)
for model in response['modelSummaries']:
print(f"Model ID: {model['modelId']}")
print(f"Input Modalities: {model['inputModalities']}\n")
def generate_text(prompt, model_id='anthropic.claude-v2'):
try:
response = bedrock.invoke_model(
modelId=model_id,
body=json.dumps({
"prompt": f"\n\nHuman: {prompt}\n\nAssistant:",
"max_tokens_to_sample": 300,
"temperature": 0.5
}),
contentType="application/json",
accept="application/json"
)
return json.loads(response['body'].read())['completion']
except Exception as error:
print(f"Error: {error}")
return None
print(generate_text("Explain quantum computing basics"))
Error | Solution |
---|---|
AccessDeniedException | Verify IAM permissions |
ModelTimeoutException | Reduce input size |
ThrottlingException | Implement retry logic |
def batch_process(queries):
results = []
for query in queries:
response = generate_text(query)
results.append({
'query': query,
'response': response
})
return results
def moderate_content(text):
prompt = f"Analyze this text for harmful content:\n{text}"
response = generate_text(prompt, 'amazon.titan-text-express-v1')
return "FLAGGED" in response
Strategy | Savings Potential |
---|---|
Caching frequent responses | Up to 40% |
Model version selection | 15-30% |
Request batching | 25-50% |
# CloudWatch metrics to track
- InvocationCount
- ModelLatency
- ThrottledRequests
Set up AWS Budgets alerts to monitor spending and prevent cost overruns.
By following this guide, you've established a production-ready AI implementation using Bedrock's pre-trained models. Remember to review AWS's model updates and security advisories regularly.
Category: AWS
Similar Articles