开始使用免费开始使用

试验 top_p

同样的发布会,新产品上线!Luna Gear 需要为 3 款即将推出的附加配件起名:一个内置手机充电器的杯盖、一个保温水瓶挂架,以及一个可夹式侧袋。他们想要两份清单:一份风格稳健、听起来成熟可靠;另一份古灵精怪、能够脱颖而出。

anthropic 库、client 和字符串 addons_list 已预加载。

本练习是课程的一部分

Claude 模型入门

查看课程

练习说明

  • top_p 设为较低值,以获得更集中、常规的词汇(0.1–0.3)。
  • top_p 设为较高值,以获得更丰富、富有创意的词汇(0.7–0.9)。
  • 打印两次返回内容,以展示生成的名称。

交互式实操练习

通过完成这段示例代码来试试这个练习。

 # Focused naming with low top_p
focused_response = client.messages.create(
    model="claude-sonnet-4-6", top_p=____, max_tokens=50,
    messages=[{"role": "user",
        "content": f"Generate feature names for each of these TerraPack add-ons: {addons_list}"}])

# Diverse naming with high top_p
diverse_response = client.messages.create(
    model="claude-sonnet-4-6", top_p=____, max_tokens=50,
    messages=[{"role": "user",
        "content": f"Generate feature names for each of these TerraPack add-ons: {addons_list}"}])

print("Focused names:", ____)
print("\nDiverse names:", ____)
编辑并运行代码