利用 AWS Serverless 與 Claude 3 打造靈活的圖像分析

閱讀時間約 11 分鐘

本篇使用 AWS Lambda 結合 Amazon Bedrock 和 Claude 3 來分析儲存在 Amazon S3 中的圖像的示範

raw-image

AWS Lambda 流程解釋

  1. 取得圖像檔案
  2. 將圖像檔案轉換成 base64string
  3. 利用 prompt 與 base64string 向 Amazon Bedrock Claude 3請求
  4. Amazon Bedrock Claude 3 分析並將結果返回


Amazon Bedrock 是使用基礎模型建置和擴充生成式 AI 應用程式的最簡單方法,
其中就有 Anthropic AI 公司的最新高效能基礎模型 Claude 3,該家族不僅具備自然語言處理能力,更兼備電腦視覺能力,可應用於影像詮釋,敏感資訊偵測,以及影像中目標標註等視覺任務。


Claude 3 該系列分為三個層級規模:

Claude 3 Haiku 為基礎型號,特點是推理響應迅速

Claude 3 Sonnet 為均衡型號,在效能與成本間取得平衡

Claude 3 Opus 則是旗艦型號,將是目前最優秀的大型語言模型

詳細比較可以參閱參考文件[1]

在多項基準測試中 Claude 3 Opus 的表現超越了 GPT-4 和 Gemini Ultra 等領先模型,在處理大規模文本等能力上創下新的里程碑。


Claude 3 系列的主要優勢包括: 更高的智慧水平、加快的推理速率、跨模態處理能力、支援超長文本視窗的精準資訊提取、更佳的人機交互及可解釋性、更高的準確度與可靠度且具備成本效益。


在視覺理解、分析推理、計算任務等方面,Claude 3 展現出卓越的能力,相較以往著重自然語言的大型語言模型,其應用範疇更為深廣,被評價為目前市場上最具性價比的多模態AI模型。


設定部署環境

步驟1:啟用 Amazon Bedrock Claude 3

啟用步驟可參閱 Amazon Bedrock - Build Generative AI service

raw-image


步驟2:建立 Lambda 函數
步驟可參閱 Amazon Bedrock with Lambda Function

raw-image


步驟3 : 調整 Lambda 設置 timeout

raw-image


步驟4 : 調整 Lambda 執行權限 role

修改YOUR_IMAGE_BUCKET_NAME

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowInvokeModel",
"Effect": "Allow",
"Action": "bedrock:InvokeModel",
"Resource": "*"
},
{
"Sid": "AllowGetObject",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::YOUR_BUCKET_NAME/*"
}
]
}
raw-image


步驟五:貼上程式碼,參考官方文件範例[2]


import boto3
import base64
import json

s3 = boto3.client('s3')
bedrock_runtime = boto3.client('bedrock-runtime', region_name='us-east-1')

def lambda_handler(event, context):
# 從事件中提取 S3 Bucket、object、prompt
bucket_name = event['Bucket']
object_key = event['Key']
prompt = event['prompt']

# 下載 S3 object
response = s3.get_object(Bucket=bucket_name, Key=object_key)
image_content = response['Body'].read()

# 將圖像編碼為 base64
base64_encoded_image = base64.b64encode(image_content).decode('utf-8')

# Bedrock 調用的 Payload,可以更改模型參數以獲得所需的結果。
payload = {
"modelId": "anthropic.claude-3-sonnet-20240229-v1:0",
"contentType": "application/json",
"accept": "application/json",
"body": {
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 4096,
"top_k": 250,
"top_p": 0.999,
"temperature": 0,
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/png",
"data": base64_encoded_image
}
},
{
"type": "text",
"text": prompt
}
]
}
]
}
}

# 將 payload 轉換為字節
body_bytes = json.dumps(payload['body']).encode('utf-8')

# 調用模型
response = bedrock_runtime.invoke_model(
body=body_bytes,
contentType=payload['contentType'],
accept=payload['accept'],
modelId=payload['modelId']
)

# 處理回應
response_body = json.loads(response['body'].read())
result = response_body['content'][0]['text']

return result


步驟六:執行測試

prompt 輸入需要提問的問題

輸入對應的 Bucket name 與 object key

{
"prompt": "YOUR_PROMPT(例如 XXXX)",
"Bucket": "YOUR_BUCKET_NAME",
"Key": "YOUR_S3_OBJECT_KEY"
}
raw-image


可以給出清楚資訊

raw-image


This famous painting is titled "Starry Night Over the Rhone" and was created by the renowned Dutch Post-Impressionist painter Vincent van Gogh in 1888. The work depicts an idealized night view over the Rhône River in the city of Arles, France, where van Gogh lived for a period. The indigo blue swirling sky filled with shining stars, the reflections of the lights on the rippling river, and the silhouettes of people strolling along the banks make this nocturnal scene one of van Gogh's most recognizable masterpieces. It captures his distinctive thick impasto brushwork and expressionistic style that vividly conveys the artist's emotional response to the nighttime landscape.




參考
[1] https://aws.amazon.com/tw/events/taiwan/techblogs/aws-claude-3-family/

[2] https://github.com/aws-samples/s3-image-analysis-lambda-claude3/tree/main




14會員
59內容數
留言0
查看全部
發表第一個留言支持創作者!