在庫エージェント
CoddyのAI Engineeringジャーニー「ツールとエージェント」セクションの一部。レッスン 21/22。
チャレンジ
難しい店舗の在庫を確認し、補充するエージェントを作成します。スターターコードは STOCK を保持し、リクエストを読み取ります。
- 2つのツールを定義します。
check_stock: descriptionCheck how many units of a product are in stock、Which productと説明される必須の文字列プロパティproduct。order_more: descriptionOrder more of a product from the supplier、同じproductプロパティと、How many units to orderと説明される必須の整数プロパティquantity。 - 2つの関数を作成します。両関数とも、
STOCKに存在しない商品に対してはValueError("Unknown product: <product>")を発生させます。check_stockは<product>: <count> in stockを返します。order_moreは、数量が1から50までの整数でない限りValueError("Quantity must be between 1 and 50")を発生させます。それ以外の場合はSTOCKに追加し、Ordered <quantity> of <product>, stock is now <count>を返します。 - エージェントループを実行します(
model="claude-opus-5-5"、max_tokens=500、最大5ラウンド)。呼び出しごとにTool:行(名前、スペース、入力)を出力します。ValueErrorは、そのメッセージとともに"is_error": Trueとして返されます。 - 最後に、
Agent:と返答テキストを出力し、続いてStock:とすべての商品およびその個数をカンマとスペースで区切って出力します。
例えば、入力が Order 80 more of the office chair の場合、プログラムは次のように出力します:
Tool: order_more {'product': 'office chair', 'quantity': 80}
Agent: Sorry, I could not finish: Quantity must be between 1 and 50
Stock: desk lamp 7, office chair 3, standing desk 0自分で試してみよう
import coddy_ai as anthropic # 本番環境では: import anthropic
client = anthropic.Anthropic()
STOCK = {"desk lamp": 7, "office chair": 3, "standing desk": 0}
request = input()
# 以下にコードを記述してください
ツールとエージェントのすべてのレッスン
自分で練習してみよう: Pythonオンラインコンパイラ