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 terpadu. Layanan ini memungkinkan membangun aplikasi generative AI tanpa mengelola infrastruktur.
Mengapa AWS Bedrock?
Manfaat Utama:- Multiple FMs: Akses Claude, Llama, Titan, dan lainnya
- Fully managed: Tidak perlu mengelola infrastruktur
- Aman: Privasi data dan dukungan VPC
- Customizable: Fine-tune model dengan data Anda
- Terintegrasi: Integrasi native dengan layanan AWS
- Anthropic Claude (Claude 3, Claude 2)
- Meta Llama 2
- Amazon Titan
- AI21 Labs Jurassic
- Cohere Command
- Stability AI (gambar)
Prerequisites
pip install boto3
Konfigurasi AWS CLI
aws configure
Enable akses model Bedrock di AWS Console
Quick Start
1. Basic Text Generation
import boto3
import json
Buat Bedrock runtime client
bedrock = boto3.client(
servicename="bedrock-runtime",
regionname="us-east-1"
)
Panggil model Claude
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 teks
response = generatetext("Jelaskan machine learning dengan bahasa sederhana.")
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("Tulis puisi pendek tentang AI.")
Bekerja dengan Model Berbeda
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("Apa itu 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("Jelaskan 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"]
Embeddings
1. Titan Embeddings
def getembeddings(text):
body = json.dumps({
"inputText": text
})
response = bedrock.invokemodel(
modelId="amazon.titan-embed-text-v1",
body=body
)
result = json.loads(response["body"].read())
return result["embedding"]
Dapatkan embeddings
embedding = getembeddings("Machine learning sangat menarik.")
print(f"Dimensi embedding: {len(embedding)}")
2. Cohere Embeddings
def getcohereembeddings(texts):
body = json.dumps({
"texts": texts,
"inputtype": "searchdocument"
})
response = bedrock.invokemodel(
modelId="cohere.embed-english-v3",
body=body
)
result = json.loads(response["body"].read())
return result["embeddings"]
embeddings = getcohereembeddings(["Halo dunia", "AI sangat powerful"])
Generasi Gambar
1. Stability AI
import base64
def generateimage(prompt):
body = json.dumps({
"textprompts": [{"text": prompt}],
"cfgscale": 7,
"steps": 50,
"seed": 42
})
response = bedrock.invokemodel(
modelId="stability.stable-diffusion-xl-v1",
body=body
)
result = json.loads(response["body"].read())
imagedata = base64.b64decode(result["artifacts"][0]["base64"])
with open("generatedimage.png", "wb") as f:
f.write(imagedata)
return "generatedimage.png"
imagepath = generateimage("Kota futuristik dengan mobil terbang")
print(f"Gambar disimpan di: {imagepath}")
2. Amazon Titan Image
def generatetitanimage(prompt):
body = json.dumps({
"taskType": "TEXTIMAGE",
"textToImageParams": {
"text": prompt
},
"imageGenerationConfig": {
"numberOfImages": 1,
"quality": "standard",
"height": 512,
"width": 512
}
})
response = bedrock.invokemodel(
modelId="amazon.titan-image-generator-v1",
body=body
)
result = json.loads(response["body"].read())
return base64.b64decode(result["images"][0])
Percakapan dan Chat
1. Percakapan Multi-turn
class BedrockChat:
def init(self, modelid="anthropic.claude-3-sonnet-20240229-v1:0"):
self.bedrock = boto3.client("bedrock-runtime", regionname="us-east-1")
self.modelid = modelid
self.messages = []
self.systemprompt = None
def setsystemprompt(self, prompt):
self.systemprompt = prompt
def chat(self, usermessage):
self.messages.append({"role": "user", "content": usermessage})
body = {
"anthropicversion": "bedrock-2023-05-31",
"maxtokens": 1024,
"messages": self.messages
}
if self.systemprompt:
body["system"] = self.systemprompt
response = self.bedrock.invokemodel(
modelId=self.modelid,
body=json.dumps(body)
)
result = json.loads(response["body"].read())
assistantmessage = result["content"][0]["text"]
self.messages.append({"role": "assistant", "content": assistantmessage})
return assistantmessage
def clearhistory(self):
self.messages = []
Penggunaan
chat = BedrockChat()
chat.setsystemprompt("Kamu adalah asisten AI yang ahli dalam ML.")
response1 = chat.chat("Apa itu deep learning?")
print(f"Asisten: {response1}\n")
response2 = chat.chat("Bisa berikan contohnya?")
print(f"Asisten: {response2}")
RAG dengan Bedrock
1. Knowledge Bases
bedrockagent = boto3.client("bedrock-agent-runtime", regionname="us-east-1")
def queryknowledgebase(query, kbid):
response = bedrockagent.retrieveandgenerate(
input={"text": query},
retrieveAndGenerateConfiguration={
"type": "KNOWLEDGEBASE",
"knowledgeBaseConfiguration": {
"knowledgeBaseId": kbid,
"modelArn": "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0"
}
}
)
return response["output"]["text"]
Query knowledge base
answer = queryknowledgebase(
"Apa fitur utama produk kami?",
"KNOWLEDGEBASEID"
)
print(answer)
2. Custom RAG Pipeline
import numpy as np
class BedrockRAG:
def init(self):
self.bedrock = boto3.client("bedrock-runtime", regionname="us-east-1")
self.documents = []
self.embeddings = []
def adddocuments(self, documents):
for doc in documents:
embedding = self.getembedding(doc)
self.documents.append(doc)
self.embeddings.append(embedding)
def getembedding(self, text):
body = json.dumps({"inputText": text})
response = self.bedrock.invokemodel(
modelId="amazon.titan-embed-text-v1",
body=body
)
result = json.loads(response["body"].read())
return np.array(result["embedding"])
def cosinesimilarity(self, a, b):
return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))
def retrieve(self, query, topk=3):
queryembedding = self.getembedding(query)
similarities = [
self.cosinesimilarity(queryembedding, emb)
for emb in self.embeddings
]
topindices = np.argsort(similarities)[-topk:][::-1]
return [self.documents[i] for i in topindices]
def query(self, question):
relevantdocs = self.retrieve(question)
context = "\n".join(relevantdocs)
prompt = f"""Berdasarkan konteks berikut, jawab pertanyaan.
Konteks:
{context}
Pertanyaan: {question}
Jawaban:"""
body = json.dumps({
"anthropicversion": "bedrock-2023-05-31",
"maxtokens": 1024,
"messages": [{"role": "user", "content": prompt}]
})
response = self.bedrock.invokemodel(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
body=body
)
result = json.loads(response["body"].read())
return result["content"][0]["text"]
Penggunaan
rag = BedrockRAG()
rag.adddocuments([
"Produk kami mendukung Python 3.8 ke atas.",
"Rate limit API adalah 1000 request per menit.",
"Pengguna premium mendapat priority support."
])
answer = rag.query("Versi Python apa yang didukung?")
print(answer)
Bedrock Agents
1. Buat Agent
bedrockagentclient = boto3.client("bedrock-agent", regionname="us-east-1")
def createagent(agentname, instructions):
response = bedrockagentclient.createagent(
agentName=agentname,
foundationModel="anthropic.claude-3-sonnet-20240229-v1:0",
instruction=instructions,
agentResourceRoleArn="arn:aws:iam::123456789:role/BedrockAgentRole"
)
return response["agent"]["agentId"]
agentid = createagent(
"customer-service-agent",
"Kamu adalah agent customer service. Bantu pengguna dengan pertanyaan mereka."
)
2. Panggil Agent
def invokeagent(agentid, sessionid, prompt):
response = bedrock
agent.invokeagent(
agentId=agent
id,
agentAliasId="TSTALIASID",
sessionId=sessionid,
inputText=prompt
)
completion = ""
for event in response["completion"]:
if "chunk" in event:
completion += event["chunk"]["bytes"].decode()
return completion
Kustomisasi Model
1. Fine-tuning
bedrockclient = boto3.client("bedrock", regionname="us-east-1")
def create
finetuningjob(jobname, modelid, trainingdatauri):
response = bedrockclient.createmodelcustomizationjob(
jobName=jobname,
customModelName=f"custom-{jobname}",
roleArn="arn:aws:iam::123456789:role/BedrockFineTuningRole",
baseModelIdentifier=modelid,
trainingDataConfig={
"s3Uri": trainingdatauri
},
outputDataConfig={
"s3Uri": "s3://bucket/fine-tuned-models/"
},
hyperParameters={
"epochCount": "3",
"batchSize": "8",
"learningRate": "0.00001"
}
)
return response["jobArn"]
2. Continued Pre-training
def createcontinuedpretrainingjob(jobname, modelid, datauri):
response = bedrock
client.createmodelcustomizationjob(
jobName=job
name,
customModelName=f"pretrained-{jobname}",
roleArn="arn:aws:iam::123456789:role/BedrockRole",
baseModelIdentifier=modelid,
customizationType="CONTINUEDPRETRAINING",
trainingDataConfig={
"s3Uri": datauri
},
outputDataConfig={
"s3Uri": "s3://bucket/pretrained-models/"
}
)
return response["jobArn"]
Guardrails
1. Buat Guardrail
def createguardrail(name, blockedtopics, wordfilters):
response = bedrockclient.createguardrail(
name=name,
description="Content filtering guardrail",
topicPolicyConfig={
"topicsConfig": [
{
"name": topic,
"definition": f"Konten terkait {topic}",
"type": "DENY"
}
for topic in blockedtopics
]
},
wordPolicyConfig={
"wordsConfig": [
{"text": word} for word in wordfilters
]
},
blockedInputMessaging="Permintaan Anda diblokir.",
blockedOutputsMessaging="Respons difilter."
)
return response["guardrailId"]
guardrailid = createguardrail(
"content-filter",
["kekerasan", "aktivitas ilegal"],
["katakasar1", "katakasar2"]
)
2. Gunakan Guardrail
def invokewithguardrail(prompt, guardrailid):
body = json.dumps({
"anthropic
version": "bedrock-2023-05-31",
"maxtokens": 1024,
"messages": [{"role": "user", "content": prompt}]
})
response = bedrock.invokemodel(
modelId="anthropic.claude-3-sonnet-20240229-v1:0",
body=body,
guardrailIdentifier=guardrailid,
guardrailVersion="DRAFT"
)
return json.loads(response["body"].read())
Best Practices
1. Error Handling
from botocore.exceptions import ClientError
def safeinvoke(prompt, modelid):
try:
body = json.dumps({
"anthropicversion": "bedrock-2023-05-31",
"maxtokens": 1024,
"messages": [{"role": "user", "content": prompt}]
})
response = bedrock.invokemodel(modelId=modelid, body=body)
return json.loads(response["body"].read())
except ClientError as e:
errorcode = e.response["Error"]["Code"]
if errorcode == "ThrottlingException":
print("Rate limited, mencoba ulang...")
elif errorcode == "ModelNotReadyException":
print("Model belum siap")
raise
2. Optimasi Biaya
# Gunakan model yang sesuai untuk task
def selectmodel(taskcomplexity):
if taskcomplexity == "simple":
return "amazon.titan-text-lite-v1"
elif taskcomplexity == "medium":
return "anthropic.claude-3-haiku-20240307-v1:0"
else:
return "anthropic.claude-3-sonnet-20240229-v1:0"
Kesimpulan
AWS Bedrock menyediakan:
Key takeaways:
- Pilih model berdasarkan use case
- Implementasikan error handling yang proper
- Gunakan guardrails untuk content filtering
- Manfaatkan knowledge bases untuk RAG
- Monitor biaya dan penggunaan