Menu
CoddyTech

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

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

מודל אף פעם לא רואה את פונקציות ה-Python שלך. הוא מחליט אם לקרוא לכלי, ואיזה כלי, מתוך ה-name וה-description של הכלי בלבד. תיאור כמו Useful helper. לא נותן לו שום דבר להסתמך עליו, ולכן הוא עונה ללא הכלי.

vague = {"name": "helper", "description": "Useful helper.", "input_schema": schema}
clear = {
    "name": "get_tides",
    "description": "Get today's high and low tide times for a harbour. Use it for any question about tides.",
    "input_schema": schema,
}

כשנשאל When is high tide in Dover?, המודל עונה לבדו עם vague (סיבת עצירה end_turn) וקורא ל-clear (סיבת עצירה tool_use).

תיאור טוב אומר שלושה דברים: מה הכלי עושה או מחזיר, מתי להשתמש בו, וכל דבר שהמודל חייב לדעת על הארגומנטים (יחידות, פורמטים, מגבלות). תן לכל ארגומנט description משלו גם כן. ה-stand-in מתאים מילים בין הבקשה לתיאור; מודל אמיתי קורא את המשמעות, אך הוא נכשל באותו אופן כשהתיאור אינו אומר דבר.

challenge icon

אתגר

בינוני

עוזר הספרייה אינו משתמש לעולם בכלי ה-lookup שלו, מכיוון שהתיאור אינו אומר דבר. תקן את התיאור; השאר את השם, ה-schema והלולאה.

  1. החלף את Looks things up. בתיאור במשפט אחד או שניים.
  2. ציין שהכלי מקבל את שעות הפתיחה של סניף ספרייה.
  3. ציין שיש להשתמש בו כאשר מישהו שואל מתי סניף נפתח או נסגר.

הלולאה מדפיסה כל קריאה לכלי ואת התשובה. לדוגמה, עבור הקלט When does the Riverside branch open? התוכנית מדפיסה:

Tool: lookup {'branch_name': 'Riverside'}
Answer: Here is what I found: Riverside: 9 am to 6 pm

נסו בעצמכם

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

client = anthropic.Anthropic()

HOURS = {"Riverside": "9 am to 6 pm", "Hill": "10 am to 4 pm", "Oak": "8 am to 8 pm"}

# שכתב את התיאור כך שהמודל ידע מתי להשתמש בכלי זה
TOOLS = [
    {
        "name": "lookup",
        "description": "Looks things up.",
        "input_schema": {
            "type": "object",
            "properties": {"branch_name": {"type": "string", "description": "The branch name, such as Riverside"}},
            "required": ["branch_name"],
        },
    }
]


def lookup(branch_name):
    return f"{branch_name}: {HOURS.get(branch_name, 'no such branch')}"


request = input()
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=300, tools=TOOLS, messages=messages)
while response.stop_reason == "tool_use":
    messages.append({"role": "assistant", "content": response.content})
    results = []
    for block in response.content:
        if block.type == "tool_use":
            print(f"Tool: {block.name} {block.input}")
            results.append({"type": "tool_result", "tool_use_id": block.id, "content": lookup(**block.input)})
    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 אונליין