|
| 1 | +""" |
| 2 | +This example demonstrates how to create a WorkflowAI agent that extracts flight information from emails. |
| 3 | +It showcases: |
| 4 | +
|
| 5 | +1. Using Pydantic models for structured data extraction |
| 6 | +2. Extracting specific details like flight numbers, dates, and times |
| 7 | +""" |
| 8 | + |
| 9 | +import asyncio |
| 10 | +from datetime import datetime |
| 11 | +from enum import Enum |
| 12 | + |
| 13 | +from pydantic import BaseModel, Field |
| 14 | + |
| 15 | +import workflowai |
| 16 | +from workflowai import Model |
| 17 | + |
| 18 | + |
| 19 | +class EmailInput(BaseModel): |
| 20 | + """Raw email content containing flight booking details. |
| 21 | + This could be a confirmation email, itinerary update, or e-ticket from any airline.""" |
| 22 | + email_content: str |
| 23 | + |
| 24 | + |
| 25 | +class FlightInfo(BaseModel): |
| 26 | + """Model for extracted flight information.""" |
| 27 | + class Status(str, Enum): |
| 28 | + """Possible statuses for a flight booking.""" |
| 29 | + CONFIRMED = "Confirmed" |
| 30 | + PENDING = "Pending" |
| 31 | + CANCELLED = "Cancelled" |
| 32 | + DELAYED = "Delayed" |
| 33 | + COMPLETED = "Completed" |
| 34 | + |
| 35 | + passenger: str |
| 36 | + airline: str |
| 37 | + flight_number: str |
| 38 | + from_airport: str = Field(description="Three-letter IATA airport code for departure") |
| 39 | + to_airport: str = Field(description="Three-letter IATA airport code for arrival") |
| 40 | + departure: datetime |
| 41 | + arrival: datetime |
| 42 | + status: Status |
| 43 | + |
| 44 | +@workflowai.agent( |
| 45 | + id="flight-info-extractor", |
| 46 | + model=Model.GEMINI_2_0_FLASH_LATEST, |
| 47 | +) |
| 48 | +async def extract_flight_info(email_input: EmailInput) -> FlightInfo: |
| 49 | + """ |
| 50 | + Extract flight information from an email containing booking details. |
| 51 | + """ |
| 52 | + ... |
| 53 | + |
| 54 | + |
| 55 | +async def main(): |
| 56 | + email = """ |
| 57 | + Dear Jane Smith, |
| 58 | +
|
| 59 | + Your flight booking has been confirmed. Here are your flight details: |
| 60 | +
|
| 61 | + Flight: UA789 |
| 62 | + From: SFO |
| 63 | + To: JFK |
| 64 | + Departure: 2024-03-25 9:00 AM |
| 65 | + Arrival: 2024-03-25 5:15 PM |
| 66 | + Booking Reference: XYZ789 |
| 67 | +
|
| 68 | + Total Journey Time: 8 hours 15 minutes |
| 69 | + Status: Confirmed |
| 70 | +
|
| 71 | + Thank you for choosing United Airlines! |
| 72 | + """ |
| 73 | + run = await extract_flight_info.run(EmailInput(email_content=email)) |
| 74 | + print(run) |
| 75 | + |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + asyncio.run(main()) |
0 commit comments