Menu
CoddyTech

שרשור תוצאות כלים

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

בקשות רבות דורשות שני כלים ברצף, כאשר השני זקוק למה שהראשון החזיר. כשנשאל What is the price of the kettle with tax?, מודל עם כלי get_price וכלי add_tax אינו יכול לקרוא ל-add_tax עדיין: אין לו מחיר. הוא קורא ל-get_price, קורא 30 dollars בתוצאה, ואז קורא ל-add_tax עם המספר הזה.

get_price {'item': 'kettle'}
add_tax {'price': 30}

הלולאה שלך מאפשרת זאת מבלי לדעת על כך: היא שולחת כל תוצאה בחזרה וממשיכה כל עוד stop_reason הוא "tool_use". לולאה שמטפלת בסבב אחד בלבד נעצרת לאחר get_price, כשהקריאה השנייה אינה מתבצעת לעולם.

שני דברים עוזרים לשרשרת לעבוד. תוצאות צריכות לציין את המספרים שלהן בצורה ברורה (30 dollars, ולא קוד פנימי), מכיוון שהמודל מעתיק אותם לקריאה הבאה. וארגומנט שמקבל מספר חייב להיות "type": "number" או "integer", כדי שהמודל יעביר ערך שהפונקציה שלך תוכל לחשב איתו.

challenge icon

אתגר

קשה

סייען הרכבות יכול למצוא מחיר נסיעה ביורו. הוסף כלי שממיר אותו, כך שהמודל יוכל לשרשר בין השניים.

  1. הוסף כלי ל-TOOLS בשם convert_euros, עם התיאור Convert an amount in euros to another currency., עם שתי תכונות חובה: amount (מספר, הסכום ביורו) ו-currency (מחרוזת, קוד המטבע: USD, JPY או GBP).
  2. כתוב את convert_euros(amount, currency). היא מחזירה <amount> euros is <converted> <currency>, כאשר הערך המומר הוא amount * RATES[currency] עם 2 ספרות לאחר הנקודה העשרונית.
  3. הוסף אותה ל-FUNCTIONS.

לדוגמה, עבור הקלט What does the train to Lyon cost in dollars? התוכנית מדפיסה:

Step 1: get_fare {'destination': 'Lyon'}
Step 2: convert_euros {'amount': 48, 'currency': 'USD'}
Answer: Here is what I found:
- Lyon: 48 euros
- 48 euros is 52.32 USD

נסו בעצמכם

import coddy_ai as anthropic  # בסביבת ייצור: import anthropic

client = anthropic.Anthropic()

FARES = {"Lyon": 48, "Nice": 95, "Lille": 35, "Bordeaux": 70}
RATES = {"USD": 1.09, "JPY": 162.0, "GBP": 0.86}

TOOLS = [
    {
        "name": "get_fare",
        "description": "Get the price of a train ticket from Paris to a destination, in euros.",
        "input_schema": {
            "type": "object",
            "properties": {"destination": {"type": "string", "description": "The destination city"}},
            "required": ["destination"],
        },
    },
    # הוסף את הכלי convert_euros כאן
]


def get_fare(destination):
    return f"{destination}: {FARES[destination]} euros"


# כתוב את convert_euros(amount, currency) כאן

FUNCTIONS = {"get_fare": get_fare}

request = input()
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)
step = 0
while response.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": response.content})
    results = []
    for block in response.content:
        if block.type == "tool_use":
            step += 1
            print(f"Step {step}: {block.name} {block.input}")
            output = FUNCTIONS[block.name](**block.input)
            results.append({"type": "tool_result", "tool_use_id": block.id, "content": output})
    messages.append({"role": "user", "content": results})
    response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)

for block in response.content:
    if block.type == "text":
        print("Answer: " + block.text)
quiz iconבחנו את עצמכם

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

כל השיעורים ביחידה כלים וסוכנים

תרגלו בעצמכם: קומפיילר Python אונליין