Foundation model レポートのカテゴリ分類
あなたは今、天気の状況をすばやく把握できる AI 機能に取り組んでいます。長いレポートを読む代わりに、ユーザーは SkyCast Assistant アプリにシンプルな1語の要約が表示されることを望んでいます。概念実証として、Amazon Bedrock を使って、天気レポートをあらかじめ定義されたリストの中から1語に要約します。
boto3 と json ライブラリはすでに読み込まれています。report というサンプルの天気レポートと、有効な categories のリストも事前に用意されています。
この演習はコースの一部です
Amazon Bedrock入門
演習の手順
categoriesリストに含まれる許可されたカテゴリを明記し、reportを含めたプロンプトを作成します。- モデルを呼び出すとき、このプロンプトをリクエストボディに追加します。
- 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)}")