승인 및 보고서
Coddy AI Engineering 여정의 도구와 에이전트 섹션에 포함된 레슨. 22개 중 19번째.
챌린지
어려움예약은 여행자의 돈을 지출하므로 에이전트가 먼저 물어봐야 합니다. 그다음 실행 비용이 얼마인지 보고하세요. 지금까지 작성한 모든 내용을 유지하세요.
Book a seat on a flight to a destination설명과 필수 문자열 속성인destination(설명:The destination city)을 가진book_flight라는 이름의 도구를 추가하세요. 이 도구를TOOLS및 전송하는 도구 목록에 추가하세요.tool_use블록의 이름이book_flight일 때,Tool:행을 출력한 후 입력 한 줄을 더 읽으세요. 입력이yes이면Approved를 출력하고 도구를 실행하세요. 그렇지 않으면Declined를 출력하고 도구를 실행하지 않은 채"is_error": True와 함께The traveller said no to this booking결과를 전송하세요.- 첫 번째 호출을 포함하여 모든 호출에 걸쳐
input_tokens와output_tokens를 합산하세요. - 마지막에
Steps:와 도구 라운드 수를 출력한 다음, 합계와 함께Tokens: <input> in, <output> out을 출력하세요.
예를 들어, Book a flight to Lisbon 및 yes 입력에 대해 프로그램은 다음을 출력합니다:
Tool: book_flight {'destination': 'Lisbon'}
Approved
Agent: Here is what I found: Booked a seat to Lisbon for 120 dollars
Steps: 1
Tokens: 658 in, 35 out직접 해보기
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()
# 아래에 코드를 작성하세요
flight_tool = {
"name": "search_flights",
"description": "Search flights to a destination and give the ticket price",
"input_schema": {
"type": "object",
"properties": {"destination": {"type": "string", "description": "The destination city"}},
"required": ["destination"],
},
}
hotel_tool = {
"name": "find_hotel",
"description": "Find a hotel room in a city with its nightly rate",
"input_schema": {
"type": "object",
"properties": {"city": {"type": "string", "description": "The city to stay in"}},
"required": ["city"],
},
}
TOOLS = {"search_flights": search_flights, "find_hotel": find_hotel}
tools = [flight_tool, hotel_tool]
MAX_STEPS = 5
messages = [{"role": "user", "content": request}]
response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=tools, messages=messages)
steps = 0
while response.stop_reason == "tool_use":
if steps == MAX_STEPS:
break
steps += 1
results = []
for block in response.content:
if block.type == "tool_use":
print(f"Tool: {block.name} {block.input}")
try:
result = TOOLS[block.name](**block.input)
results.append({"type": "tool_result", "tool_use_id": block.id, "content": result})
except Exception as error:
results.append({"type": "tool_result", "tool_use_id": block.id, "content": str(error), "is_error": True})
messages.append({"role": "assistant", "content": response.content})
messages.append({"role": "user", "content": results})
response = client.messages.create(model="claude-opus-5-5", max_tokens=500, tools=tools, messages=messages)
if response.stop_reason == "tool_use":
print(f"Stopped after {MAX_STEPS} steps")
else:
for block in response.content:
if block.type == "text":
print("Agent: " + block.text)
도구와 에이전트의 모든 레슨
직접 연습해 보세요: 온라인 Python 컴파일러