基础模型报告分类
您正在开发一项 AI 功能,用于快速理解天气状况。用户不想阅读冗长的报告,而是希望在他们的 SkyCast Assistant 应用中看到一个简洁的单词摘要。作为概念验证,您将使用 Amazon Bedrock 把一份天气报告概括为预定义列表中的一个词。
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)}")