Complete AWS Bedrock Tutorial: Foundation Models on AWS

# Tutorial Lengkap AWS Bedrock: Managed Generative AI di AWS Amazon Bedrock adalah layanan terkelola penuh yang menyediakan akses ke foundation models (FMs) dari perusahaan AI terkemuka melalui API t...

By Ruby Abdullah · · tutorial
AWSBedrockLLMFoundation ModelsGenerative AIClaude

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

Available Models:
  • 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({

"anthropicversion": "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]",

"maxgenlen": 512,

"temperature": 0.7,

"topp": 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,

"maxtokens": 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"]

Related Articles

Complete Azure OpenAI Service Tutorial: GPT and LLMs on Azure

Tutorial Lengkap Azure OpenAI Service: Enterprise AI dengan Model GPT Azure OpenAI Service menyediakan akses REST API ke...

TRL Tutorial: LLM Post-Training with SFT, DPO, and Reward Modeling

Post-Training LLM dengan TRL: SFT, Reward Modeling, dan DPO Setelah sebuah base language model selesai dipretraining, mo...

Axolotl Tutorial: Configuration-Driven LLM Fine-Tuning

Fine-Tuning LLM Berbasis Konfigurasi dengan Axolotl Kebanyakan proyek fine-tuning dimulai dengan cara yang sama: seseora...

ComfyUI Tutorial: Node-Based Workflows for Stable Diffusion

ComfyUI: Workflow Berbasis Node untuk Stable Diffusion ComfyUI adalah lingkungan grafis berbasis node untuk menjalankan ...