סקירת הפרויקט
חלק מהיחידה כלים וסוכנים במסלול ה-AI Engineering של Coddy. שיעור 16 מתוך 22.
אתגר
בינוניבמשך ארבעה שיעורים תבנה סוכן נסיעות: תוכנית שמאפשרת למודל לחפש טיסות ומלונות, ולאחר מכן להזמין מקום. הקוד ההתחלתי מכיל את נתוני הנסיעה ושלוש פונקציות Python; היום תיתן למודל אחת מהן.
- הגדר כלי בשם
search_flightsעם התיאורSearch flights to a destination and give the ticket priceותכונת מחרוזת חובה אחת,destination, המתוארת כ-The destination city. - שלח את הבקשה כהודעת המשתמש היחידה עם
tools=[...],model="claude-opus-5-5"ו-max_tokens=500. - לכל בלוק
tool_use, הדפסTool:, את שם הכלי, רווח ואת הקלט שלו, ואז הרץ אתsearch_flightsעם היעד. - הוסף את התוכן של ה-assistant והודעת משתמש עם בלוקי ה-
tool_result, קרא למודל שוב עם אותם כלים, והדפסAgent:ואת הטקסט של בלוק הטקסט.
לדוגמה, עבור הקלט Find a flight to Lisbon התוכנית תדפיס:
Tool: search_flights {'destination': 'Lisbon'}
Agent: Here is what I found: Flight to Lisbon: 120 dollarsנסו בעצמכם
import coddy_ai as anthropic # בסביבת ייצור: import anthropic
client = anthropic.Anthropic()
FLIGHTS = {"Lisbon": 120, "Rome": 95, "Tokyo": 640, "Oslo": None}
HOTELS = {"Lisbon": 80, "Rome": 110, "Tokyo": 150, "Oslo": 130}
def search_flights(destination):
if destination not in FLIGHTS:
raise ValueError(f"No flights to {destination}")
if FLIGHTS[destination] is None:
raise ConnectionError("The flight service is temporarily unavailable, try again")
return f"Flight to {destination}: {FLIGHTS[destination]} dollars"
def find_hotel(city):
if city not in HOTELS:
raise ValueError(f"No hotels in {city}")
return f"Hotel in {city}: {HOTELS[city]} dollars a night"
def book_flight(destination):
return f"Booked a seat to {destination} for {FLIGHTS[destination]} dollars"
request = input()
# כתוב את הקוד שלך למטה
כל השיעורים ביחידה כלים וסוכנים
תרגלו בעצמכם: קומפיילר Python אונליין