复习:评论分类器
本课属于 Coddy AI Engineering 学习旅程的“在代码中调用模型”单元。第 15 课,共 22 课。
挑战
中等对商品评价进行分类,并汇报本次运行的费用。起始代码会读取评价的数量以及具体的评价内容,并包含 2026 年 11 月的 Claude Haiku 5.5 价格(每百万输入 token $0.10,每百万输出 token $0.50;最新的价格请查看 Anthropic 的定价页面)。
- 编写一个包含两个必填字段的 JSON schema:
sentiment(字符串,其枚举值为positive、negative和neutral)以及summary(字符串)。将additionalProperties设置为False。 - 逐条发送评价,设置
model="claude-haiku-5-5"、max_tokens=200,并使用该 schema 作为格式。输出<sentiment>: <summary>。 - 然后输出
Positive: <n>, Negative: <n>, Neutral: <n>。 - 输出所有调用的总计 token 数:
Tokens: <input> in, <output> out。 - 输出
Cost: $以及保留 8 位小数的总费用。
例如,对于输入 1 和 The chair is grey and has four legs.,程序会输出:
neutral: The chair is grey and has four legs
Positive: 0, Negative: 0, Neutral: 1
Tokens: 131 in, 27 out
Cost: $0.00002660亲手试试
import coddy_ai as anthropic # 生产环境中:import anthropic
import json
client = anthropic.Anthropic()
# Claude Haiku 5.5 的价格(美元/百万 token,2026 年 11 月)
PRICE_INPUT = 0.10
PRICE_OUTPUT = 0.50
count = int(input())
reviews = [input() for _ in range(count)]
# 在下方编写你的代码
在代码中调用模型中的所有课
自己练习: Python 在线编译器