嘗試調整 top_p
同樣的發表會,但有新產品!Luna Gear 需要為即將推出的三款加購配件命名:內建手機充電器的上蓋、保溫水壺架,以及可夾式側邊口袋。他們想要兩份名單:一份語氣穩健、成熟,另一份風格古怪、有辨識度。
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:", ____)