Menu
CoddyTech

סוכן מלאי

חלק מהיחידה כלים וסוכנים במסלול ה-AI Engineering של Coddy. שיעור 21 מתוך 22.

challenge icon

אתגר

קשה

בנה סוכן שבודק ומחדש את המלאי של חנות. הקוד ההתחלתי מחזיק את STOCK וקורא את הבקשה.

  1. הגדר שני כלים. check_stock: תיאור Check how many units of a product are in stock, מאפיין מחרוזת חובה אחד product המתואר כ-Which product. order_more: תיאור Order more of a product from the supplier, אותו מאפיין product ומאפיין מספר שלם חובה quantity המתואר כ-How many units to order.
  2. כתוב את שתי הפונקציות. שתיהן מעלות ValueError("Unknown product: <product>") עבור מוצר שלא נמצא ב-STOCK. check_stock מחזירה <product>: <count> in stock. order_more מעלה ValueError("Quantity must be between 1 and 50") אלא אם הכמות היא מספר שלם מ-1 עד 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 אונליין