Menu
CoddyTech

קריאות מקביליות לכלים

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

כאשר בקשה מכילה מספר שאלות עצמאיות, המודל מבקש את כל קריאות הכלים בתגובה אחת. כל קריאה היא בלוק tool_use משלה עם id משלו:

messages = [{"role": "user", "content": "Look up the opening hours for Luna Cafe and Mango House"}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=TOOLS, messages=messages)
calls = [block for block in response.content if block.type == "tool_use"]
for block in calls:
    print(block.name, block.input)

זה מדפיס שתי שורות: get_hours {'place': 'Luna Cafe'} ו-get_hours {'place': 'Mango House'}.

הרץ כל קריאה, ואז שלח את כל התוצאות בחזרה יחד: הודעת משתמש אחת שהתוכן שלה הוא רשימה עם tool_result אחד לכל קריאה. ה-API בודק זאת. אם בהודעה שלאחר קריאות הכלים חסרה תוצאה עבור אחד מה-ids שלהן, הבקשה נכשלת עם שגיאת 400, ולכן לולאה ששולחת הודעה אחת לכל תוצאה נשברת ברגע שמגיעות שתי קריאות. שליחתן יחד גם מבהירה למודל שהוא יכול להמשיך לבקש מספר קריאות בבת אחת, מה שחוסך סבב תקשורת בכל פעם.

challenge icon

אתגר

בינוני

סוכן מזג האוויר בקוד ההתחלתי נכשל כשהוא מבקש שתי ערים: הוא שולח כל תוצאה בהודעה נפרדת. תקן את החלק שבתוך הלולאה.

  1. אסוף את בלוקי ה-tool_use של התגובה והדפס Calls: ואת כמותם.
  2. הפעל את get_weather עבור כל אחד מהם ושים כל tool_result ברשימה אחת.
  3. הוסף את הרשימה הזו כהודעת משתמש יחידה.

שמור על שאר התוכנית כפי שהיא. לדוגמה, עבור הקלט What is the weather in Oslo and Lima? התוכנית מדפיסה:

Calls: 2
Answer: Here is what I found:
- Oslo: 4 C and rainy
- Lima: 19 C and misty

נסו בעצמכם

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

client = anthropic.Anthropic()

WEATHER = {
    "Paris": "18 C and sunny",
    "Tokyo": "22 C and cloudy",
    "Rome": "25 C and sunny",
    "Oslo": "4 C and rainy",
    "Cairo": "31 C and clear",
    "Lima": "19 C and misty",
}


def get_weather(city):
    return f"{city}: {WEATHER.get(city, 'no data')}"


TOOLS = [
    {
        "name": "get_weather",
        "description": "Get the current weather for a city",
        "input_schema": {
            "type": "object",
            "properties": {"city": {"type": "string", "description": "The city name"}},
            "required": ["city"],
        },
    }
]

request = input()
messages = [{"role": "user", "content": request}]

response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=TOOLS, messages=messages)
while response.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": response.content})
    # תקן את החלק הזה: הדפס "Calls: " ואת מספר קריאות הכלים בתגובה זו,
    # ואז שלח את כל התוצאות בהודעת משתמש אחת
    for block in response.content:
        if block.type == "tool_use":
            result = {"type": "tool_result", "tool_use_id": block.id, "content": get_weather(**block.input)}
            messages.append({"role": "user", "content": [result]})
    response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=TOOLS, messages=messages)

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

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

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

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