프로젝트 개요
Coddy AI Engineering 여정의 도구와 에이전트 섹션에 포함된 레슨. 22개 중 16번째.
챌린지
중급4개 레슨에 걸쳐 여행 에이전트를 만듭니다. 모델이 항공편과 호텔을 검색한 다음 좌석을 예약할 수 있게 해주는 프로그램입니다. 스타터 코드에는 여행 데이터와 3개의 Python 함수가 포함되어 있으며, 오늘은 그중 하나를 모델에 제공합니다.
- 설명이
Search flights to a destination and give the ticket price이고 필수 문자열 속성인destination(설명:The destination city)을 가진search_flights라는 이름의 도구를 정의하세요. tools=[...],model="claude-opus-5-5"및max_tokens=500으로 요청을 유일한 사용자 메시지로 전송하세요.- 각
tool_use블록에 대해Tool:, 도구의 이름, 공백 및 입력을 출력한 다음 destination으로search_flights를 실행하세요. - 어시스턴트의 내용과
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 컴파일러