用 temperature 控制調整創意程度
你在 RoboChef Labs 工作,這是一家打造 AI 驅動烹飪機器人的新創公司。團隊正在嘗試使用 Amazon Bedrock 產生宣傳故事、使用指南與產品行銷文案。
首先,他們需要用於宣傳烹飪機器人系列的短篇故事。請使用 temperature 參數微調 Claude 的創意程度,並產生不同版本。
在此練習中,boto3 和 json 函式庫,以及 bedrock 用戶端,皆已預先匯入。
本練習屬於課程
Amazon Bedrock 入門
練習說明
- 產生兩篇機器人故事:第一個輸出使用較低的 temperature,第二個輸出使用較高的 temperature,並觀察兩者的差異。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
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)