用 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:", ____)