MultiAgentExamples-AliA / find_batman_mobile_agent.py
AliA1997
Completed some demos from huggingface tutorials.
8bed67e
import os
import math
from PIL import Image
from io import BytesIO
from typing import Optional, Tuple
from smolagents import tool
from smolagents.utils import encode_image_base64, make_image_url
from smolagents import OpenAIServerModel
@tool
def calculate_cargo_travel_time(
origin_coords: Tuple[float, float],
destination_coords: Tuple[float, float],
cruising_speed_kmh: Optional[float] = 750.0
) -> float:
"""
Calculate the travel time for a cargo plan between two points on Earth using great-circle distance.
Args:
origin_coords: The coordinates of the origin of the cargo plan.
destination_coords: The coordinates of the destination of the cargo plan.
cruising_speed_kmh: The speed of the cargo plan in km/h.
Example:
>>> # Chicago (41.8781 N, 87.6298) to Sydney (33.8688, 151.2093)
>>> result = calculate_cargo_travel_time((41.8781, -87.6298), (-33.8688, 151.2093))
"""
def to_radians(degrees: float) -> float:
return degrees * (math.pi / 180.0)
#extract coordinates
lat1, lon1 = map(to_radians, origin_coords)
lat2, lon2 = map(to_radians, destination_coords)
# Earth Radius in kilometers
EARTH_RADIUS_KM = 6371.0
# Calculate great-circle distance using the haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = (
math.sin(dlat / 2) ** 2
+ math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2
)
c = 2 * math.asin(math.sqrt(a))
distance = EARTH_RADIUS_KM * c
# Add 10%
actual_distance = distance * 1.1
# Calculate flight time
# Add 1 hour for takeoff and landing procedures
flight_time = (actual_distance / cruising_speed_kmh) + 1.0
# Format the results
return round(flight_time, 2)
print(
calculate_cargo_travel_time(origin_coords=(41.8781, -87.6298), destination_coords=(-33.8688, 151.2093))
)
def check_reasoning_and_plot(final_answer, agent_memory):
multimodal_model = OpenAIServerModel("gpt-4o", max_tokens=8096)
filepath = "saved_map.png"
assert os.path.exists(filepath), "Make sure to save the plot under saved_map.png!"
image = Image.open(filepath)
prompt = (
f"Here is a user-given task and the agent steps: {agent_memory.get_succinct_steps()}. Now here is the plot that was made."
"Please check that the reasoning process and plot are correct: do they correctly answer the given task?"
"First list reasons why yes/no, then write your final decision: PASS in caps lock if it is satisfactory, FAIL if it is not."
"Don't be harsh: if the plot mostly solves the task, it should pass."
"To pass, a plot should be made using px.scatter_map and not any other method (scatter_map looks nicer)."
)
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": prompt,
},
{
"type": "image_url",
"image_url": { "url": make_image_url(encode_image_base64(image))},
}
]
}
]
output = multimodal_model.predict(messages)
print("Feedback:", output)
if "FAIL" in output:
raise Exception(output)
return True