항공편 예약
항공편 예약 (Flight Booking)
한 에이전트가 다른 에이전트에게 작업을 위임한 다음, 제어권을 세 번째 에이전트에게 넘겨주는 멀티 에이전트 흐름 예시예요.
다음을 시연해요:
이 시나리오에서는 에이전트 그룹이 협력해서 사용자를 위한 최적의 항공편을 찾아요.
이 예시의 제어 흐름은 다음과 같이 요약할 수 있어요:
graph TD
START --> search_agent("search agent")
search_agent --> extraction_agent("extraction agent")
extraction_agent --> search_agent
search_agent --> human_confirm("human confirm")
human_confirm --> search_agent
search_agent --> FAILED
human_confirm --> find_seat_function("find seat function")
find_seat_function --> human_seat_choice("human seat choice")
human_seat_choice --> find_seat_agent("find seat agent")
find_seat_agent --> find_seat_function
find_seat_function --> buy_flights("buy flights")
buy_flights --> SUCCESS
출처: 문서
본문
예제 실행하기
의존성이 설치되고 환경 변수가 설정되면 실행해요:
Terminal
python -m pydantic_ai_examples.flight_booking
Terminal
uv run -m pydantic_ai_examples.flight_booking
예제 코드
flight_booking.py
import datetime
from dataclasses import dataclass
from typing import Literal
import logfire
from pydantic import BaseModel, Field
from rich.prompt import Prompt
from pydantic_ai import (
Agent,
ModelMessage,
ModelRetry,
RunContext,
RunUsage,
UsageLimits,
)
# 'if-token-present'는 logfire가 구성되지 않아도 아무것도 보내지 않고 (예제는 동작함)
logfire.configure(send_to_logfire='if-token-present')
logfire.instrument_pydantic_ai()
class FlightDetails(BaseModel):
"""Details of the most suitable flight."""
flight_number: str
price: int
origin: str = Field(description='Three-letter airport code')
destination: str = Field(description='Three-letter airport code')
date: datetime.date
class NoFlightFound(BaseModel):
"""When no valid flight is found."""
@dataclass
class Deps:
web_page_text: str
req_origin: str
req_destination: str
req_date: datetime.date
# 이 에이전트는 대화의 흐름을 제어하는 역할을 담당한다.
search_agent = Agent[Deps, FlightDetails | NoFlightFound](
'openai:gpt-5.2',
output_type=FlightDetails | NoFlightFound,
deps_type=Deps,
retries=4,
system_prompt=(
'Your job is to find the cheapest flight for the user on the given date. '
),
)
# 이 에이전트는 웹 페이지 텍스트에서 항공편 세부 정보를 추출하는 역할을 담당한다.
extraction_agent = Agent(
'openai:gpt-5.2',
output_type=list[FlightDetails],
system_prompt='Extract all the flight details from the given text.',
)
@search_agent.tool
async def extract_flights(ctx: RunContext[Deps]) -> list[FlightDetails]:
"""Get details of all flights."""
# 이 에이전트 내의 요청이 계수되도록 사용량을 search agent에 전달한다
result = await extraction_agent.run(ctx.deps.web_page_text, usage=ctx.usage)
logfire.info('found {flight_count} flights', flight_count=len(result.output))
return result.output
@search_agent.output_validator
async def validate_output(
ctx: RunContext[Deps], output: FlightDetails | NoFlightFound
) -> FlightDetails | NoFlightFound:
"""Procedural validation that the flight meets the constraints."""
if isinstance(output, NoFlightFound):
return output
errors: list[str] = []
if output.origin != ctx.deps.req_origin:
errors.append(
f'Flight should have origin {ctx.deps.req_origin}, not {output.origin}'
)
if output.destination != ctx.deps.req_destination:
errors.append(
f'Flight should have destination {ctx.deps.req_destination}, not {output.destination}'
)
if output.date != ctx.deps.req_date:
errors.append(f'Flight should be on {ctx.deps.req_date}, not {output.date}')
if errors:
raise ModelRetry('\n'.join(errors))
else:
return output
class SeatPreference(BaseModel):
row: int = Field(ge=1, le=30)
seat: Literal['A', 'B', 'C', 'D', 'E', 'F']
class Failed(BaseModel):
"""Unable to extract a seat selection."""
# 이 에이전트는 사용자의 좌석 선택을 추출하는 역할을 담당한다
seat_preference_agent = Agent[object, SeatPreference | Failed](
'openai:gpt-5.2',
output_type=SeatPreference | Failed,
system_prompt=(
"Extract the user's seat preference. "
'Seats A and F are window seats. '
'Row 1 is the front row and has extra leg room. '
'Rows 14, and 20 also have extra leg room. '
),
)
# 실제로는 예약 사이트에서 다운로드하거나,
# 사이트를 탐색하는 데 또 다른 에이전트를 사용할 수도 있다
flights_web_page = """
1. Flight SFO-AK123
- Price: $350
- Origin: San Francisco International Airport (SFO)
- Destination: Ted Stevens Anchorage International Airport (ANC)
- Date: January 10, 2025
2. Flight SFO-AK456
- Price: $370
- Origin: San Francisco International Airport (SFO)
- Destination: Fairbanks International Airport (FAI)
- Date: January 10, 2025
3. Flight SFO-AK789
- Price: $400
- Origin: San Francisco International Airport (SFO)
- Destination: Juneau International Airport (JNU)
- Date: January 20, 2025
4. Flight NYC-LA101
- Price: $250
- Origin: San Francisco International Airport (SFO)
- Destination: Ted Stevens Anchorage International Airport (ANC)
- Date: January 10, 2025
5. Flight CHI-MIA202
- Price: $200
- Origin: Chicago O'Hare International Airport (ORD)
- Destination: Miami International Airport (MIA)
- Date: January 12, 2025
6. Flight BOS-SEA303
- Price: $120
- Origin: Boston Logan International Airport (BOS)
- Destination: Ted Stevens Anchorage International Airport (ANC)
- Date: January 12, 2025
7. Flight DFW-DEN404
- Price: $150
- Origin: Dallas/Fort Worth International Airport (DFW)
- Destination: Denver International Airport (DEN)
- Date: January 10, 2025
8. Flight ATL-HOU505
- Price: $180
- Origin: Hartsfield-Jackson Atlanta International Airport (ATL)
- Destination: George Bush Intercontinental Airport (IAH)
- Date: January 10, 2025
"""
# 이 앱이 LLM에 할 수 있는 요청 수를 제한한다
usage_limits = UsageLimits(request_limit=15)
async def main():
deps = Deps(
web_page_text=flights_web_page,
req_origin='SFO',
req_destination='ANC',
req_date=datetime.date(2025, 1, 10),
)
message_history: list[ModelMessage] | None = None
usage: RunUsage = RunUsage()
# 만족스러운 항공편을 찾을 때까지 에이전트를 실행한다
while True:
result = await search_agent.run(
f'Find me a flight from {deps.req_origin} to {deps.req_destination} on {deps.req_date}',
deps=deps,
usage=usage,
message_history=message_history,
usage_limits=usage_limits,
)
if isinstance(result.output, NoFlightFound):
print('No flight found')
break
else:
flight = result.output
print(f'Flight found: {flight}')
answer = Prompt.ask(
'Do you want to buy this flight, or keep searching? (buy/*search)',
choices=['buy', 'search', ''],
show_choices=False,
)
if answer == 'buy':
seat = await find_seat(usage)
await buy_tickets(flight, seat)
break
else:
message_history = result.all_messages(
output_tool_return_content='Please suggest another flight'
)
async def find_seat(usage: RunUsage) -> SeatPreference:
message_history: list[ModelMessage] | None = None
while True:
answer = Prompt.ask('What seat would you like?')
result = await seat_preference_agent.run(
answer,
message_history=message_history,
usage=usage,
usage_limits=usage_limits,
)
if isinstance(result.output, SeatPreference):
return result.output
else:
print('Could not understand seat preference. Please try again.')
message_history = result.all_messages()
async def buy_tickets(flight_details: FlightDetails, seat: SeatPreference):
print(f'Purchasing flight {flight_details=!r} {seat=!r}...')
if __name__ == '__main__':
import asyncio
asyncio.run(main())
더 알아보기 (Learn more)
- Pydantic AI 문서: 항공편 예약