開始使用免費開始

產品評論的少量示例提示(Few-shot)

你在 BrightCart(一個電商平台)工作。產品頁面現在會顯示 AI 的「Quick-Read」標章,將成千上萬的顧客評論濃縮成重點洞見。為了進一步簡化,你將把摘要拆成 2 句話:第一句是最突出的正面收穫,第二句則是坦誠的顧慮點。

用少量示例提示(few-shot prompting)教會 Claude 這個精確格式,讓後續每個產品都能得到同樣平衡的摘要。

anthropic 函式庫、client,以及一筆 raw_review 範例都已預先載入。

本練習屬於課程

Claude 模型入門

檢視課程

練習說明

  • 為 Claude 的第一個示例回應設定正確的角色。
  • 要求撰寫一則關於該產品的 2 句話摘要。
  • 將完整的 messages 陣列傳入 API 呼叫。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

messages = [
    {"role": "user", "content": "Summarize: Nice phone, fast performance, great camera. Screen scratches easily though."},
    # Set the role for Claude's first example response
    {"role": ____, "content": "Fast performance and excellent camera make this phone stand out. However, the screen may be prone to scratching."},
    {"role": "user", "content": "Summarize: Comfortable shoes, good for walking. Run small, order size up."},
    # Write a 2-sentence summary following the established pattern
    {"role": "assistant", "content": ____},
    {"role": "user", "content": f"Summarize: {raw_review}"}]

# Pass the complete messages array to the API call
response = client.messages.create(
    model="claude-sonnet-4-6", max_tokens=75,
    messages=____)

print(response.content[0].text)
編輯並執行程式碼