시작하기무료로 시작하기

Foundation model 보고서 분류

이제 날씨 상황을 빠르게 파악하도록 돕는 AI 기능을 개발하고 있어요. 긴 보고서를 읽는 대신, 사용자는 SkyCast Assistant 앱에서 한 단어로 된 간단한 요약을 보고 싶어 합니다. 개념 증명으로, Amazon Bedrock을 사용해 미리 정의된 목록 중 하나의 단어로 날씨 보고서를 요약해 보려고 해요.

boto3json 라이브러리는 미리 로드되어 있어요. 예시 날씨 보고서는 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)}")
코드 편집 및 실행