基礎模型報告分類
你現在正在開發一個 AI 功能,協助快速了解天氣狀況。使用者不想讀冗長的報告,而是希望在 SkyCast Assistant app 中看到簡潔的單字摘要。作為概念驗證,你會用 Amazon Bedrock 將一份天氣報告總結為預先定義清單中的單一詞。
已預先載入 boto3 與 json 函式庫。範例天氣報告已以 report 變數提供,且有效的 categories 清單也已預先載入。
本練習屬於課程
Amazon Bedrock 入門
練習說明
- 建立一個提示詞,列出
categories清單中的允許分類,並包含report。 - 叫用模型時,將此提示詞加入請求 body 中。
- 從 Bedrock 回傳的
response中擷取摘要。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def summarize_weather(report):
bedrock = boto3.client("bedrock-runtime", region_name="us-east-1")
# Create prompt with allowed categories and pass in the report
prompt = f"""Summarize the following weather report as one word from: {', '.join(____)}. Report: {____}"""
# Pass the prompt to the body
body = {"messages": [{"role": "user", "content": [{"text": ____}]}]}
response = bedrock.invoke_model(modelId="us.amazon.nova-2-lite-v1:0", body=json.dumps(body))
# Extract the summary from the response
data = json.loads(response.get("body").read()____)
return data["output"]["message"]["content"][0]["text"]
print(f"Weather Summary: {summarize_weather(report)}")