Foundation model report categorization
You're now working on an AI feature that helps to quickly understand weather conditions. Instead of reading long reports, users want a simple, one-word summary to appear on their SkyCast Assistant app. As a proof of concept, you use Amazon Bedrock to summarize a weather report into a single word from a predefined list.
The boto3
and json
libraries have been preloaded. A sample weather report saved as report
and a list of valid categories
have also been preloaded.
This exercise is part of the course
Introduction to Amazon Bedrock
Exercise instructions
- Create a prompt that mentions the allowed categories from the
categories
list and includes thereport
. - Add this prompt to the request body when invoking the model.
- Extract the summary from the
response
returned by Bedrock.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
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="amazon.nova-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)}")