hibam1 commited on
Commit
c41b2e0
·
1 Parent(s): 089ba5f

multi agent test

Browse files
Files changed (2) hide show
  1. cargo_tool.py +60 -0
  2. multiAgent.py +69 -0
cargo_tool.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from typing import Optional, Tuple
3
+
4
+ from smolagents import tool
5
+
6
+
7
+ @tool
8
+ def calculate_cargo_travel_time(
9
+ origin_coords: Tuple[float, float],
10
+ destination_coords: Tuple[float, float],
11
+ cruising_speed_kmh: Optional[float] = 750.0, # Average speed for cargo planes
12
+ ) -> float:
13
+ """
14
+ Calculate the travel time for a cargo plane between two points on Earth using great-circle distance.
15
+
16
+ Args:
17
+ origin_coords: Tuple of (latitude, longitude) for the starting point
18
+ destination_coords: Tuple of (latitude, longitude) for the destination
19
+ cruising_speed_kmh: Optional cruising speed in km/h (defaults to 750 km/h for typical cargo planes)
20
+
21
+ Returns:
22
+ float: The estimated travel time in hours
23
+
24
+ Example:
25
+ >>> # Chicago (41.8781° N, 87.6298° W) to Sydney (33.8688° S, 151.2093° E)
26
+ >>> result = calculate_cargo_travel_time((41.8781, -87.6298), (-33.8688, 151.2093))
27
+ """
28
+
29
+ def to_radians(degrees: float) -> float:
30
+ return degrees * (math.pi / 180)
31
+
32
+ # Extract coordinates
33
+ lat1, lon1 = map(to_radians, origin_coords)
34
+ lat2, lon2 = map(to_radians, destination_coords)
35
+
36
+ # Earth's radius in kilometers
37
+ EARTH_RADIUS_KM = 6371.0
38
+
39
+ # Calculate great-circle distance using the haversine formula
40
+ dlon = lon2 - lon1
41
+ dlat = lat2 - lat1
42
+
43
+ a = (
44
+ math.sin(dlat / 2) ** 2
45
+ + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
46
+ )
47
+ c = 2 * math.asin(math.sqrt(a))
48
+ distance = EARTH_RADIUS_KM * c
49
+
50
+ # Add 10% to account for non-direct routes and air traffic controls
51
+ actual_distance = distance * 1.1
52
+
53
+ # Calculate flight time
54
+ # Add 1 hour for takeoff and landing procedures
55
+ flight_time = (actual_distance / cruising_speed_kmh) + 1.0
56
+
57
+ # Format the results
58
+ return round(flight_time, 2)
59
+
60
+
multiAgent.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool
2
+
3
+ from cargo_tool import calculate_cargo_travel_time
4
+ from model import model
5
+
6
+ # TASK for single agent:
7
+
8
+ # TASK = """
9
+ # Find all Batman filming locations in the world,
10
+ # calculate the time to transfer via cargo plane to here (we're in Gotham, 40.7128° N, 74.0060° W), and return them to me as a pandas dataframe.
11
+ # Also give me some supercar factories with the same cargo plane transfer time."""
12
+
13
+ # def run_single_agent() -> None:
14
+ # agent = CodeAgent(
15
+ # model=model,
16
+ # tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), calculate_cargo_travel_time],
17
+ # additional_authorized_imports=["pandas"],
18
+ # max_steps=20,
19
+ # )
20
+ # print(agent.run(TASK))
21
+
22
+
23
+ # if __name__ == "__main__":
24
+ # run_single_agent()
25
+
26
+ # TASK for multi-agent:
27
+
28
+ def build_web_agent() -> CodeAgent:
29
+ return CodeAgent(
30
+ model=model,
31
+ tools=[DuckDuckGoSearchTool(), VisitWebpageTool(), calculate_cargo_travel_time],
32
+ name="web_agent",
33
+ description="Browses the web to find information",
34
+ verbosity_level=0,
35
+ max_steps=10,
36
+ )
37
+
38
+
39
+ def build_manager_agent() -> CodeAgent:
40
+ return CodeAgent(
41
+ model=model,
42
+ tools=[calculate_cargo_travel_time],
43
+ managed_agents=[build_web_agent()],
44
+ additional_authorized_imports=["geopandas", "plotly", "shapely", "json", "pandas", "numpy"],
45
+ planning_interval=5,
46
+ verbosity_level=2,
47
+ max_steps=15,
48
+ )
49
+
50
+ MAP_TASK = """
51
+ Find all Batman filming locations in the world, calculate the time to transfer via cargo plane to here (we're in Gotham, 40.7128° N, 74.0060° W).
52
+ Also give me some supercar factories with the same cargo plane transfer time. You need at least 6 points in total.
53
+ Represent this as spatial map of the world, with the locations represented as scatter points with a color that depends on the travel time, and save it to saved_map.png!
54
+
55
+ Here's an example of how to plot and return a map:
56
+ import plotly.express as px
57
+ df = px.data.carshare()
58
+ fig = px.scatter_map(df, lat="centroid_lat", lon="centroid_lon", text="name", color="peak_hour", size=100,
59
+ color_continuous_scale=px.colors.sequential.Magma, size_max=15, zoom=1)
60
+ fig.show()
61
+ fig.write_image("saved_image.png")
62
+ final_answer(fig)
63
+
64
+ Never try to process strings using code: when you have a string to read, just print it and you'll see it.
65
+ """
66
+
67
+ if __name__ == "__main__":
68
+ manager = build_manager_agent()
69
+ manager.run(MAP_TASK)