Menu
CoddyTech

支持升级

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

challenge icon

挑战

困难

构建一个能够查询订单并在无法提供帮助时将工单转交给人工的客服 Agent。初始代码包含 ORDERS、一个空列表 TICKETS 并会读取请求。

  1. 定义两个工具。track_order:描述为 Track a customer's order by its order number,包含一个必需的整数属性 order_number(描述为 The order number)。escalate:描述为 Hand the case to a human support agent,包含一个必需的字符串属性 reason(描述为 Why the customer wants help)。
  2. track_order 返回 Order <number>: <status>,或抛出 ValueError("No order with number <number>")。escalate 将原因追加到 TICKETS 并返回 Ticket T<count> opened for a human: <reason>,其中 count 是 TICKETS 的长度。
  3. 运行 Agent 循环(model="claude-opus-5-5",max_tokens=500,最多 4 轮),每次调用打印一行 Tool:,并在发生 ValueError 时将其消息以 "is_error": True 发回。
  4. 打印 Agent: 和回复文本。
  5. 如果某个工具抛出了错误或循环达到了限制,且未创建任何工单,请自行使用完整请求调用 escalate,并打印 Escalated: 以及其返回值。
  6. 打印 Tickets: 和工单的数量。

例如,对于输入 Where is order 9999?,程序会输出:

Tool: track_order {'order_number': 9999}
Agent: Sorry, I could not finish: No order with number 9999
Escalated: Ticket T1 opened for a human: Where is order 9999?
Tickets: 1

亲手试试

import coddy_ai as anthropic  # 在生产环境中:import anthropic

client = anthropic.Anthropic()

ORDERS = {
    1042: "shipped, arriving Friday",
    2077: "delayed at the warehouse",
    3155: "delivered on Monday",
}
TICKETS = []

request = input()

# 在下方编写你的代码

工具与 Agent中的所有课

自己练习: Python 在线编译器