Aan de slagGa gratis aan de slag

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.

Deze oefening maakt deel uit van de cursus

Introduction to Amazon Bedrock

Cursus bekijken

Oefeninstructies

  • Create a prompt that mentions the allowed categories from the categories list and includes the report.
  • Add this prompt to the request body when invoking the model.
  • Extract the summary from the response returned by Bedrock.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

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)}")
Code bewerken en uitvoeren