用 temperature 控制創意程度
你是 Luna Gear 的產品負責人,正要推出環保的 TerraPack 背包。團隊需要做一次 A/B 測試的貼文標題:一個要精煉且具資訊性,另一個要活潑並帶有話題炒熱的語氣。
anthropic 函式庫、client,以及字串 product_info 都已預先載入。
本練習屬於課程
Claude 模型入門
練習說明
- 將
temperature設為較低值(0.1–0.3),以產生一致且專業的輸出。 - 將
temperature設為較高值(0.7–0.9),以產生更有創意且多變的輸出。 - 印出保守版與創意版回應的內容。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
conservative_response = client.messages.create(
# Conservative caption with low temperature
model="claude-sonnet-4-6", temperature=____, max_tokens=150,
messages=[{"role": "user",
"content": f"Write a professional caption for this product: {product_info}"}])
creative_response = client.messages.create(
# Creative caption with high temperature
model="claude-sonnet-4-6", temperature=____, max_tokens=150,
messages=[{"role": "user",
"content": f"Write a fun, engaging caption for this product: {product_info}"}])
# Print the Conservative and Creative Responses
print("Conservative caption:", ____)
print("\nCreative caption:", ____)