Phân loại báo cáo bằng foundation model
Bạn đang phát triển một tính năng AI giúp nắm bắt nhanh điều kiện thời tiết. Thay vì đọc các báo cáo dài, người dùng muốn có một phần tóm tắt chỉ một từ xuất hiện trên ứng dụng SkyCast Assistant. Như một bản mẫu thử nghiệm, bạn dùng Amazon Bedrock để tóm tắt một báo cáo thời tiết thành một từ duy nhất lấy từ danh sách xác định trước.
Thư viện boto3 và json đã được nạp sẵn. Một báo cáo thời tiết mẫu lưu trong report và danh sách categories hợp lệ cũng đã được nạp sẵn.
Bài tập này là một phần của khóa học
Giới thiệu về Amazon Bedrock
Hướng dẫn bài tập
- Tạo một prompt nêu rõ các category được phép từ danh sách
categoriesvà bao gồmreport. - Thêm prompt này vào phần body của yêu cầu khi gọi model.
- Trích xuất phần tóm tắt từ
responsedo Bedrock trả về.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
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)}")