
Amazon Bedrock と Claude 3 Sonnet を組み合わせて「アップロードしたアーキテクチャ図からブログ記事の下書きを生成する」というサンプルプロジェクトが aws-samples に公開されている❗️(公開されたのは2024年4月頃)
シンプルな実装だし,デプロイするのも簡単だから Amazon Bedrock の入門コンテンツとしても使いやすいと思う👏
仕組み
Amazon S3 (Input) に JPEG のアーキテクチャ図をアップロードすると AWS Lambda 関数がトリガーされて,Amazon Bedrock を呼び出す.今回モデルは anthropic.claude-3-sonnet-20240229-v1:0 を使うようになっていた.そして,Amazon Bedrock (Claude 3 Sonnet) から返ってきたブログ記事の下書きを PDF ファイルに変換して Amazon S3 (Output) にアップロードする流れになっている👌
各リソースは AWS SAM を使ってデプロイできる(🚨Lambda Layer を除く).

試す
GitHub リポジトリに含まれている以下のアーキテクチャ図を Amazon S3 (Input) にアップロードする💡

すると Amazon S3 (Output) に PDF ファイル draft-blog.pdf がアップロードされている❗️

ブログ記事の下書きを PDF から抜き出すと以下のような感じ📝 ちゃんと要点は抑えられている感じがして,アーキテクチャ図以外のインプットをしていないことを考えるとスゴイな〜と思った.
Draft blog based on your image: Title: Implementing a Secure and Scalable Kafka Architecture on AWS Introduction: In today's data-driven world, real-time data processing and analytics have become increasingly important for businesses to gain insights, make informed decisions, and stay ahead of the competition. Apache Kafka, an open-source distributed event streaming platform, has emerged as a powerful tool for building real-time data pipelines and enabling scalable and reliable data processing. However, deploying and managing Kafka clusters can be challenging, especially when it comes to ensuring security, scalability, and high availability. This blog post explores an architecture that leverages Amazon Web Services (AWS) to build a secure and scalable Kafka solution. Architecture Overview: The architecture depicted in the image consists of two main regions: a Primary Region and a Secondary Region. The Primary Region hosts the main Kafka components, including Kafka Producer Clients, Kafka Consumer Clients, and an Amazon Managed Streaming for Apache Kafka (Amazon MSK) cluster. The Secondary Region serves as a disaster recovery site and hosts another Amazon MSK cluster, Kafka Connect cluster, and other components for data replication and integration. Primary Region: 1. Kafka Clients: The Kafka Producer Clients and Kafka Consumer Clients reside in private subnets within the Primary Region's Virtual Private Cloud (VPC). These clients interact with the Amazon MSK cluster to produce and consume data streams. 2. Amazon MSK Cluster: The Amazon MSK cluster is a fully managed Apache Kafka service provided by AWS. It simplifies the provisioning, deployment, and management of Kafka clusters, allowing you to focus on building your applications rather than managing the underlying infrastructure. Secondary Region: 1. Public Subnet: The Secondary Region includes a public subnet that hosts a Bastion Host, which serves as a secure entry point for administrative tasks and maintenance activities. 2. Private Subnets: Similar to the Primary Region, the Secondary Region has private subnets where the Kafka Connect cluster and other components reside. 3. Kafka Connect Cluster: The Kafka Connect cluster consists of connectors like MirrorSourceConnector, CheckpointConnector, and HeartbeatConnector. These connectors facilitate data replication and integration between the Primary and Secondary Regions. 4. Fargate: AWS Fargate is a serverless compute engine that powers the Kafka Connect cluster, eliminating the need to provision and manage servers. 5. Amazon MSK Cluster: The Secondary Region also hosts an Amazon MSK cluster, which serves as a replica of the Primary Region's cluster for disaster recovery purposes. VPC Peering: The Primary and Secondary Regions are connected via VPC Peering, which allows secure communication between the two VPCs, enabling data replication and failover capabilities. Conclusion: By leveraging AWS services like Amazon MSK, Fargate, and VPC Peering, this architecture provides a secure, scalable, and highly available Kafka solution. The Primary Region hosts the main Kafka components, while the Secondary Region acts as a disaster recovery site, ensuring data continuity and failover capabilities. Additionally, the use of managed services like Amazon MSK reduces the operational overhead of managing Kafka clusters, allowing you to focus on building and scaling your applications. This architecture can serve as a foundation for building robust and reliable real-time data pipelines on AWS.
プロンプト
実装上のポイントは src/app.py で Claude 3 Sonnet に渡しているプロンプトで,以下のようになっている.AWS の基礎知識を持っている人が読めるようにブログ記事の下書きを生成して〜という感じ👌
Can you create a blog based on this image? The blog should be explained well so that anyone having basic knowledge on AWS Services can understand it. Please start by saying 'Draft blog based on your image:'. Please include Title, Introduction, Conclusion sections along with any other sections required for the blog.
サンプルプロジェクト改善
今回サンプルプロジェクト「Generating blog content from image using Amazon Bedrock Claude v3 sonnet LLM」を試していて,2点気になるところがあって改善してみた❗️
まず1点目は Lambda Layer で,create_lambda_layer.sh というスクリプトを実行して Lambda Layer のデプロイと AWS Lambda 関数へのアタッチをしている部分は理由がわからなかった💨 AWS SAM を使って Lambda Layer を作ることができるため,create_lambda_layer.sh は本来不要だと思うし,追加作業なくデプロイできるべき.
そして2点目は Lambda Permission で,AWS SAM で AWS::Lambda::Permission リソースが設定されていなく,AWS Lambda 関数をマネジメントコンソールで確認するとトリガー部分に S3 が表示されていなかった.
👾 layer/requirements.txt
以下のように layer/requirements.txt を追加する.
fpdf2==2.8.2
👾 template.yaml
さらに template.yaml も修正すれば Lambda Layer も Lambda Permission も AWS SAM でデプロイできるようになる👌
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 Description: This SAM Template uses Amazon Bedrock Claude V3 sonnet LLM to create a draft blog from an image uploaded into Amazon S3 bucket Resources: # Create an S3 Bucket to store image MyInputBucket: Type: AWS::S3::Bucket Properties: BucketName: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region}-input # Create an S3 Bucket to store generated pdf MyOutputBucket: Type: AWS::S3::Bucket Properties: BucketName: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region}-output Fpdf2Layer: Type: AWS::Serverless::LayerVersion Properties: LayerName: fpdf2 ContentUri: layer CompatibleRuntimes: - python3.12 # Create Send Email Lambda function InvokeBedrockClaudeV3Function: Type: AWS::Serverless::Function Properties: Handler: app.lambda_handler Runtime: python3.12 Timeout: 300 CodeUri: src/ Environment: Variables: S3_BUCKET_INPUT: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region}-input S3_BUCKET_OUTPUT: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region}-output Policies: - S3ReadPolicy: BucketName: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region}-input - S3CrudPolicy: BucketName: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region}-output - Version: '2012-10-17' Statement: - Effect: Allow Action: - "bedrock:PutUseCaseForModelAccess" - "bedrock:GetUseCaseForModelAccess" - "bedrock:DeleteFoundationModelAgreement" - "bedrock:CreateAgent" - "bedrock:GetFoundationModelAvailability" - "bedrock:GetModelInvocationLoggingConfiguration" - "bedrock:ListFoundationModelAgreementOffers" - "bedrock:AssociateThirdPartyKnowledgeBase" - "bedrock:DeleteModelInvocationLoggingConfiguration" - "bedrock:ListKnowledgeBases" - "bedrock:PutFoundationModelEntitlement" - "bedrock:ListModelCustomizationJobs" - "bedrock:ListAgents" - "bedrock:ListProvisionedModelThroughputs" - "bedrock:ListCustomModels" - "bedrock:CreateKnowledgeBase" - "bedrock:PutModelInvocationLoggingConfiguration" - "bedrock:ListFoundationModels" - "bedrock:CreateFoundationModelAgreement" - "bedrock:InvokeModel" Resource: "*" Events: ObjectCreated: Type: S3 Properties: Bucket: !Ref MyInputBucket Events: s3:ObjectCreated:* Layers: - !Ref Fpdf2Layer LambdaPermission: Type: AWS::Lambda::Permission Properties: FunctionName: !GetAtt InvokeBedrockClaudeV3Function.Arn Action: lambda:InvokeFunction Principal: s3.amazonaws.com SourceAccount: !Ref 'AWS::AccountId' SourceArn: !GetAtt MyInputBucket.Arn Outputs: InvokeBedrockClaudeV3Function: Value: !Ref InvokeBedrockClaudeV3Function Description: "InvokeBedrockClaudeV3Function Name" MyInputBucketName: Description: "My Input Bucket Name" Value: !Ref MyInputBucket MyOutputBucketName: Description: "My Output Bucket Name" Value: !Ref MyOutputBucket
デプロイ確認

その他
Lambda Layer は PDF を作るための fpdf2 ライブラリを含んでいるけど,README には boto3 を含んでいると書いてあって,誤解を生む表現に感じたためプルリクエストを出しておいた💡AWS SAM で Lambda Layer を作る部分も改善プルリクエストを出しても良さそう〜