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

LangChain Tutorial: The Most Popular Framework for Building LLM Applications

Tutorial LangChain: Framework Paling Populer untuk Membangun Aplikasi LLM LangChain adalah framework open-source yang di...

Complete Google Gemini API Tutorial with Python: From Basics to Advanced

Tutorial Lengkap Google Gemini API dengan Python: Dari Dasar hingga Mahir Google Gemini API adalah salah satu API AI pal...

GraphRAG Tutorial: Graph-Based Retrieval Augmented Generation

Tutorial GraphRAG: Retrieval Augmented Generation Berbasis Graph Knowledge Pendahuluan Retrieval Augmented Generation (R...