Complete AWS Bedrock Tutorial: Managed Generative AI on AWS
Amazon Bedrock is a fully managed service that provides access to foundation models (FMs) from leading AI companies through a unified API. It enables building generative AI applications without managing infrastructure.
Why AWS Bedrock?
Key Benefits:- Multiple FMs: Access Claude, Llama, Titan, and more
- Fully managed: No infrastructure to manage
- Secure: Data privacy and VPC support
- Customizable: Fine-tune models with your data
- Integrated: Native AWS service integration
- Anthropic Claude (Claude 3, Claude 2)
- Meta Llama 2
- Amazon Titan
- AI21 Labs Jurassic
- Cohere Command
- Stability AI (images)
Prerequisites
pip install boto3
Configure AWS CLI
aws configure
Enable Bedrock model access in AWS Console
Quick Start
1. Basic Text Generation
import boto3
import json
Create Bedrock runtime client
bedrock = boto3.client(
servicename="bedrock-runtime",
regionname="us-east-1"
)
Invoke Claude model
def generatetext(prompt):
body = json.dumps({
"anthropicversion": "bedrock-2023-05-31",
"maxtokens": 1024,
"messages": [
{"role": "user", "content": prompt}
]
})
response = bedrock.invokemodel(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
body=body
)
result = json.loads(response["body"].read())
return result["content"][0]["text"]
Generate text
response = generatetext("Explain machine learning in simple terms.")
print(response)
2. Streaming Response
def generatetextstreaming(prompt):
body = json.dumps({
"anthropic
version": "bedrock-2023-05-31",
"maxtokens": 1024,
"messages": [
{"role": "user", "content": prompt}
]
})
response = bedrock.invokemodelwithresponsestream(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
body=body
)
for event in response["body"]:
chunk = json.loads(event["chunk"]["bytes"])
if chunk["type"] == "contentblockdelta":
print(chunk["delta"]["text"], end="", flush=True)
generatetextstreaming("Write a short poem about AI.")
Working with Different Models
1. Amazon Titan
def invoketitan(prompt):
body = json.dumps({
"inputText": prompt,
"textGenerationConfig": {
"maxTokenCount": 1024,
"temperature": 0.7,
"topP": 0.9
}
})
response = bedrock.invokemodel(
modelId="amazon.titan-text-express-v1",
body=body
)
result = json.loads(response["body"].read())
return result["results"][0]["outputText"]
response = invoketitan("What is cloud computing?")
print(response)
2. Meta Llama 2
def invokellama(prompt):
body = json.dumps({
"prompt": f"[INST] {prompt} [/INST]",
"max
genlen": 512,
"temperature": 0.7,
"top
p": 0.9
})
response = bedrock.invokemodel(
modelId="meta.llama2-70b-chat-v1",
body=body
)
result = json.loads(response["body"].read())
return result["generation"]
response = invokellama("Explain neural networks.")
print(response)
3. Cohere Command
def invokecohere(prompt):
body = json.dumps({
"prompt": prompt,
"max
tokens": 500,
"temperature": 0.7
})
response = bedrock.invokemodel(
modelId="cohere.command-text-v14",
body=body
)
result = json.loads(response["body"].read())
return result["generations"][0]["text"]