cgirard-sez commited on
Commit
7f951c8
·
1 Parent(s): cbddf51

add find_flight

Browse files
Files changed (1) hide show
  1. tools/find_flight.py +123 -0
tools/find_flight.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from typing import Optional
3
+ from smolagents.tools import Tool
4
+ import serpapi
5
+ from dotenv import load_dotenv
6
+ load_dotenv() # Loads variables from .env into environment
7
+
8
+
9
+
10
+ class FlightsFinderTool(Tool):
11
+ name = "flights_finder"
12
+ description = "Find flights using the Google Flights engine."
13
+ inputs = {
14
+ 'departure_airport': {'type': 'string', 'description': 'Departure airport code (IATA)'},
15
+ 'arrival_airport': {'type': 'string', 'description': 'Arrival airport code (IATA)'},
16
+ 'outbound_date': {'type': 'string', 'description': 'Outbound date in YYYY-MM-DD format'},
17
+ 'return_date': {'type': 'string', 'description': 'Return date in YYYY-MM-DD format'},
18
+ 'adults': {'type': 'integer', 'default': 1, 'nullable': True, 'description': 'Number of adults'},
19
+ 'children': {'type': 'integer', 'default': 0, 'nullable': True, 'description': 'Number of children'},
20
+ }
21
+ output_type = "string"
22
+
23
+ @staticmethod
24
+ def find_flight(
25
+ departure_airport: Optional[str] = None,
26
+ arrival_airport: Optional[str] = None,
27
+ date: Optional[str] = None,
28
+ adults: Optional[int] = 1,
29
+ children: Optional[int] = 0,
30
+ ) -> str:
31
+ """
32
+ Finds the cheapest one-way flight for a given route and date.
33
+
34
+ Args:
35
+ departure_airport (str): Departure airport code (IATA)
36
+ arrival_airport (str): Arrival airport code (IATA)
37
+ date (str): Flight date in YYYY-MM-DD format
38
+ adults (int): Number of adults
39
+ children (int): Number of children
40
+ infants_in_seat (int): Number of infants in seat
41
+ infants_on_lap (int): Number of lap infants
42
+
43
+ Returns:
44
+ str: Formatted string with cheapest flight details
45
+ """
46
+ params = {
47
+ 'api_key': os.getenv("SERPAPI_API_KEY"),
48
+ 'engine': 'google_flights',
49
+ 'hl': 'en',
50
+ 'gl': 'us',
51
+ 'departure_id': departure_airport,
52
+ 'arrival_id': arrival_airport,
53
+ 'outbound_date': date,
54
+ 'currency': 'USD',
55
+ 'adults': adults,
56
+ 'children': children,
57
+ 'type': 2,
58
+ }
59
+
60
+ try:
61
+ search = serpapi.search(params)
62
+ flights = search.data.get("best_flights", [])
63
+ if not flights:
64
+ return "No flights found."
65
+
66
+ # Find the flight with the lowest price
67
+ cheapest = min(flights, key=lambda f: f.get("price", float("inf")))
68
+
69
+ if not cheapest.get("flights"):
70
+ return "No flight segments found."
71
+
72
+ flight = cheapest["flights"][0]
73
+ dep = flight["departure_airport"]
74
+ arr = flight["arrival_airport"]
75
+ dep_time = dep["time"]
76
+ arr_time = arr["time"]
77
+ duration = flight["duration"]
78
+ airline = flight.get("airline", "Unknown")
79
+ price = cheapest["price"]
80
+
81
+ hours = duration // 60
82
+ minutes = duration % 60
83
+ duration_str = f"{hours}h {minutes}m"
84
+
85
+ return (
86
+ f"From {dep['id']} at {dep_time} → {arr['id']} at {arr_time} | "
87
+ f"Duration: {duration_str}\nAirline: {airline} | Price: ${price}"
88
+ )
89
+
90
+ except Exception as e:
91
+ return f"Error occurred: {e}"
92
+
93
+ def forward(
94
+ self,
95
+ departure_airport: str,
96
+ arrival_airport: str,
97
+ outbound_date: str,
98
+ return_date: str,
99
+ adults: int = 1,
100
+ children: int = 0,
101
+ ) -> str:
102
+ outbound = self.find_flight(
103
+ departure_airport=departure_airport,
104
+ arrival_airport=arrival_airport,
105
+ date=outbound_date,
106
+ adults=adults,
107
+ children=children,
108
+ )
109
+
110
+ inbound = self.find_flight(
111
+ departure_airport=arrival_airport,
112
+ arrival_airport=departure_airport,
113
+ date=return_date,
114
+ adults=adults,
115
+ children=children,
116
+ )
117
+
118
+ return f"✈️ Outbound Flight:\n{outbound}\n\n🛬 Inbound Flight:\n{inbound}"
119
+
120
+
121
+ def __init__(self, *args, **kwargs):
122
+ self.is_initialized = False
123
+