总结:报销 Agent
本课属于 Coddy AI Engineering 学习旅程的“工具与 Agent”单元。第 15 课,共 22 课。
挑战
困难构建一个费用 Agent,该 Agent 仅在经过检查和人工批准后才记录支出。起始代码读取一个请求;TOTALS 保存每个类别截至目前已支出的金额。
- 定义两个工具。两者都接收
category,这是一个带有"enum": ["travel", "meals", "office"]的字符串。log_expense的描述为Log a new expense, in euros, under a category.,它还接收数字类型的amount。这两个属性都是必需的。get_total的描述为Get the total spent so far in one expense category.。
- 使用
model="claude-opus-5-5"和max_tokens=300运行 Agent 循环,最多进行 5 轮工具调用。 get_total返回<category>: <total> euros spent.。log_expense首先检查金额:当金额小于等于 0 或大于 200 时,它会返回Amounts must be between 0 and 200 euros.作为错误结果,且不进行询问。- 否则它打印
Approve <amount> euros for <category>? (yes/no)并读取一行输入。输入除yes以外的任何内容都会返回The user declined to log this expense.作为错误结果。 - 在批准后,它将金额累加到该类别的总额中,并返回
Logged <amount> euros for <category>. New total: <total> euros.。 - 打印
Answer:和最终文本。
所有金额和总额保留 2 位小数。错误结果需包含 "is_error": True。例如,对于输入 Log 25 euros for a taxi under travel 和 yes,程序会输出:
Approve 25.00 euros for travel? (yes/no)
Answer: Here is what I found: Logged 25.00 euros for travel. New total: 145.00 euros.亲手试试
import coddy_ai as anthropic # 生产环境中:import anthropic
client = anthropic.Anthropic()
TOTALS = {"travel": 120.0, "meals": 45.5, "office": 30.0}
request = input()
# 在下方编写你的代码
工具与 Agent中的所有课
自己练习: Python 在线编译器