Implementing model fallbacks
To guarantee 24/7 service, TrailBlaze needs to fall back to a lighter model when Claude is unavailable. Implement a fallback from Claude to Nova Text Lite when encountering issues, to ensure continuous service to the travel app.
The json and boto3 libraries, and the bedrock client, are preloaded.
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Introduction to Amazon Bedrock
คำแนะนำการฝึกหัด
- Wrap the primary Claude model call in a
tryblock. - In the
exceptblock, handle errors usingClientErrorand call the Nova Text Lite model.
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
def process_with_fallback(prompt):
# Handle errors with a try-except block
____:
response = bedrock.invoke_model(
modelId="us.anthropic.claude-sonnet-4-5-20250929-v1:0",
body=json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 100,
"messages": [{"role": "user", "content": [{"type": "text", "text": prompt}]}]}))
return json.loads(response["body"].read().decode())["content"][0]["text"]
except ____:
fallback = bedrock.invoke_model(
modelId="us.amazon.nova-2-lite-v1:0",
body=json.dumps({"messages": [{"role": "user", "content": [{"text": prompt}]}]}))
return json.loads(fallback["body"].read().decode())["output"]["message"]["content"][0]["text"]
print(process_with_fallback("What are the best destinations in Australia for kayaking?"))