まとめ:経費精算エージェント
CoddyのAI Engineeringジャーニー「ツールとエージェント」セクションの一部。レッスン 15/22。
チャレンジ
難しいチェックと人間の承認を経てからのみ支出を記録する経費エージェントを作成します。スターターコードは1つのリクエストを読み込みます。TOTALS にはカテゴリごとのこれまでの支出額が保持されています。
- 2つのツールを定義します。両方とも
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.)
- ツール呼び出しを最大5ラウンドまで行うエージェントループを、
model="claude-opus-5-5"およびmax_tokens=300で実行します。 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)を出力し、1行読み込みます。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()
# 以下にコードを記述してください
ツールとエージェントのすべてのレッスン
自分で練習してみよう: Pythonオンラインコンパイラ