Amazon Bedrock has evolved from a simple API gateway to foundation models into a sophisticated platform capable of powering enterprise-grade AI applications. This chapter explores advanced patterns that separate proof-of-concept implementations from production systems handling millions of requests.
123456789101112import boto3 import json from enum import Enum from dataclasses import dataclass from typing import Optional class ModelTier(Enum): FAST = "anthropic.claude-3-haiku-20240307-v1:0" BALANCED = "anthropic.claude-3-sonnet-20240229-v1:0" POWERFUL = "anthropic.claude-3-opus-20240229-v1:0" @dataclass
123456789101112import boto3 import json from datetime import datetime, timedelta class ProductionKnowledgeBase: def __init__(self, knowledge_base_id: str, model_id: str): self.bedrock_agent = boto3.client('bedrock-agent-runtime') self.kb_id = knowledge_base_id self.model_id = model_id def query_with_filters( self,
123456789101112import boto3 import json import hashlib from typing import Dict, Any, Optional, List from dataclasses import dataclass from enum import Enum import time class ModelTier(Enum): FAST = "fast" BALANCED = "balanced" POWERFUL = "powerful"
123456789101112import boto3 import json from typing import List, Dict, Any from dataclasses import dataclass @dataclass class SearchResult: content: str score: float metadata: Dict[str, Any] source: str
123456789101112import boto3 import json import uuid from datetime import datetime, timedelta class RefundApprovalHandler: def __init__(self): self.dynamodb = boto3.resource('dynamodb') self.sns = boto3.client('sns') self.sfn = boto3.client('stepfunctions') self.pending_table = self.dynamodb.Table('PendingApprovals') self.approval_topic = 'arn:aws:sns:us-east-1:123456789:refund-approvals'