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...

OpenRouter: One API for Hundreds of LLMs (OpenAI, Anthropic, Google, Meta, Mistral)

OpenRouter: Satu API untuk Ratusan LLM (OpenAI, Anthropic, Google, Meta, Mistral) Halo temen-temen, ketemu lagi sama aku...

Complete Guidance Tutorial: Constrained Generation and Structured Output from LLMs

Tutorial Lengkap Guidance: Constrained Generation dan Structured Output dari LLM Halo temen-temen, di tutorial kali ini ...

RAGFlow: Build a RAG Engine That Actually Understands Documents, Complete With Citations

RAGFlow: Bikin RAG Engine yang Ngerti Dokumen Beneran, Lengkap dengan Sitasi Halo temen-temen, balik lagi sama aku. Kala...