开始使用免费开始使用

产品评论的 few-shot 提示

您在 BrightCart(一家电商平台)工作。产品页面现在会显示一个 AI "Quick-Read" 徽章,把上千条用户评论提炼成易读的要点。为进一步简化信息,您将把摘要拆分为 2 句话:第 1 句给出最突出的正面结论,第 2 句给出一个客观的顾虑点。

通过 few-shot 提示让 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)
编辑并运行代码