hibam1 commited on
Commit ·
5e6a29e
1
Parent(s): c41b2e0
updat ethe agent to add plot
Browse files- multiAgent.py +32 -5
- saved_map.png +0 -0
multiAgent.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool
|
| 2 |
|
| 3 |
from cargo_tool import calculate_cargo_travel_time
|
|
@@ -28,23 +30,38 @@ from model import model
|
|
| 28 |
def build_web_agent() -> CodeAgent:
|
| 29 |
return CodeAgent(
|
| 30 |
model=model,
|
| 31 |
-
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()
|
| 32 |
name="web_agent",
|
| 33 |
-
description="Browses the web to find
|
| 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 |
-
|
|
|
|
| 45 |
planning_interval=5,
|
| 46 |
verbosity_level=2,
|
| 47 |
max_steps=15,
|
|
|
|
| 48 |
)
|
| 49 |
|
| 50 |
MAP_TASK = """
|
|
@@ -57,13 +74,23 @@ 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.
|
| 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)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, VisitWebpageTool
|
| 4 |
|
| 5 |
from cargo_tool import calculate_cargo_travel_time
|
|
|
|
| 30 |
def build_web_agent() -> CodeAgent:
|
| 31 |
return CodeAgent(
|
| 32 |
model=model,
|
| 33 |
+
tools=[DuckDuckGoSearchTool(), VisitWebpageTool()],
|
| 34 |
name="web_agent",
|
| 35 |
+
description="Browses the web to find places. Always returns a list of: name, latitude, longitude. Does not calculate anything.",
|
| 36 |
verbosity_level=0,
|
| 37 |
max_steps=10,
|
| 38 |
)
|
| 39 |
|
| 40 |
|
| 41 |
+
MAP_FILE = "saved_map.png"
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
def check_map_saved(final_answer, memory, agent=None) -> bool:
|
| 45 |
+
# Rejects the final answer until the map exists; the agent sees this message and keeps working
|
| 46 |
+
if not os.path.exists(MAP_FILE):
|
| 47 |
+
raise Exception(f'No map found. Plot the points with px.scatter_map and save with fig.write_image("{MAP_FILE}") before final_answer.')
|
| 48 |
+
# A real world map is tens of KB; a tiny file means a blank or fake placeholder
|
| 49 |
+
if os.path.getsize(MAP_FILE) < 10_000:
|
| 50 |
+
raise Exception(f"{MAP_FILE} is too small to be a real map. Do not write placeholder images; plot the points with px.scatter_map.")
|
| 51 |
+
return True
|
| 52 |
+
|
| 53 |
+
|
| 54 |
def build_manager_agent() -> CodeAgent:
|
| 55 |
return CodeAgent(
|
| 56 |
model=model,
|
| 57 |
tools=[calculate_cargo_travel_time],
|
| 58 |
managed_agents=[build_web_agent()],
|
| 59 |
+
# "plotly.*" (not "plotly") so the agent may import plotly.express
|
| 60 |
+
additional_authorized_imports=["geopandas.*", "plotly.*", "shapely.*", "json", "pandas", "numpy"],
|
| 61 |
planning_interval=5,
|
| 62 |
verbosity_level=2,
|
| 63 |
max_steps=15,
|
| 64 |
+
final_answer_checks=[check_map_saved],
|
| 65 |
)
|
| 66 |
|
| 67 |
MAP_TASK = """
|
|
|
|
| 74 |
df = px.data.carshare()
|
| 75 |
fig = px.scatter_map(df, lat="centroid_lat", lon="centroid_lon", text="name", color="peak_hour", size=100,
|
| 76 |
color_continuous_scale=px.colors.sequential.Magma, size_max=15, zoom=1)
|
| 77 |
+
fig.write_image("saved_map.png")
|
|
|
|
| 78 |
final_answer(fig)
|
| 79 |
|
| 80 |
Never try to process strings using code: when you have a string to read, just print it and you'll see it.
|
| 81 |
+
|
| 82 |
+
Follow this plan:
|
| 83 |
+
1. Ask web_agent for Batman filming locations (name, latitude, longitude).
|
| 84 |
+
2. Compute each travel time with calculate_cargo_travel_time. Never compute travel times yourself.
|
| 85 |
+
3. Ask web_agent for supercar factories (name, latitude, longitude).
|
| 86 |
+
4. Compute their travel times the same way, and keep only factories within 1 hour of any filming location's time. If fewer than 6 points remain in total, ask web_agent for more factories.
|
| 87 |
+
5. Plot all points, save with fig.write_image("saved_map.png"), then call final_answer.
|
| 88 |
+
|
| 89 |
"""
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
+
# Remove the map from an earlier run so the check only passes for a map made in this run
|
| 93 |
+
if os.path.exists(MAP_FILE):
|
| 94 |
+
os.remove(MAP_FILE)
|
| 95 |
manager = build_manager_agent()
|
| 96 |
manager.run(MAP_TASK)
|
saved_map.png
ADDED
|