temperature で創造性を調整する
あなたは AI 搭載の調理ロボットを開発するスタートアップ、RoboChef Labs に所属しています。チームは Amazon Bedrock を使って、プロモーション用のストーリー、ユーザーガイド、製品マーケティング向けのコピーを生成する実験を進めています。
まずは、調理ロボット製品ラインを宣伝するための短編ストーリーが必要です。temperature パラメータを使って Claude の創造性を調整し、異なるバージョンを生成してください。
この演習では、boto3 と json ライブラリ、および bedrock クライアントはあらかじめインポートされています。
この演習はコースの一部です
Amazon Bedrock入門
演習の手順
- 1つ目の出力には低い temperature、2つ目の出力には高い temperature を設定してロボットのストーリーを2つ生成し、出力の違いを観察します。
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
def generate_story_with_temperature(bedrock, temperature):
messages = [{"role": "user",
"content": "Write a short story about a cooking robot teaching other robots to cook."}]
request_body=json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 100,
"temperature": temperature, "messages": messages})
response = bedrock.invoke_model(body=request_body, modelId='us.anthropic.claude-sonnet-4-5-20250929-v1:0')
response_body = json.loads(response.get('body').read().decode())
return response_body["content"][0]["text"]
# Test low and high temperature
low_temp = generate_story_with_temperature(bedrock, ____)
high_temp = generate_story_with_temperature(bedrock, ____)
print("Low temperature (more focused):", low_temp, "High temperature (more creative):", high_temp)