Menu
CoddyTech

在庫エージェント

CoddyのAI Engineeringジャーニー「ツールとエージェント」セクションの一部。レッスン 21/22。

challenge icon

チャレンジ

難しい

店舗の在庫を確認し、補充するエージェントを作成します。スターターコードは STOCK を保持し、リクエストを読み取ります。

  1. 2つのツールを定義します。check_stock: description Check how many units of a product are in stock、Which product と説明される必須の文字列プロパティ product。order_more: description Order more of a product from the supplier、同じ product プロパティと、How many units to order と説明される必須の整数プロパティ quantity。
  2. 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> を返します。
  3. エージェントループを実行します(model="claude-opus-5-5"、max_tokens=500、最大5ラウンド)。呼び出しごとに Tool: 行(名前、スペース、入力)を出力します。ValueError は、そのメッセージとともに "is_error": True として返されます。
  4. 最後に、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オンラインコンパイラ