Menu
CoddyTech

总结:报销 Agent

本课属于 Coddy AI Engineering 学习旅程的“工具与 Agent”单元。第 15 课,共 22 课。

challenge icon

挑战

困难

构建一个费用 Agent,该 Agent 仅在经过检查和人工批准后才记录支出。起始代码读取一个请求;TOTALS 保存每个类别截至目前已支出的金额。

  1. 定义两个工具。两者都接收 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.。
  2. 使用 model="claude-opus-5-5" 和 max_tokens=300 运行 Agent 循环,最多进行 5 轮工具调用。
  3. get_total 返回 <category>: <total> euros spent.。
  4. log_expense 首先检查金额:当金额小于等于 0 或大于 200 时,它会返回 Amounts must be between 0 and 200 euros. 作为错误结果,且不进行询问。
  5. 否则它打印 Approve <amount> euros for <category>? (yes/no) 并读取一行输入。输入除 yes 以外的任何内容都会返回 The user declined to log this expense. 作为错误结果。
  6. 在批准后,它将金额累加到该类别的总额中,并返回 Logged <amount> euros for <category>. New total: <total> euros.。
  7. 打印 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 在线编译器