|
from smolagents import Tool |
|
from typing import Optional |
|
|
|
class SimpleTool(Tool): |
|
name = "get_travel_duration" |
|
description = "Gets the travel time in car between two places." |
|
inputs = {"start_location":{"type":"string","description":"the place from which you start your ride"},"destination_location":{"type":"string","description":"the place of arrival"},"departure_time":{"type":"integer","nullable":True,"description":"the departure time, provide only a `datetime.datetime` if you want to specify this"}} |
|
output_type = "string" |
|
|
|
def forward(self, start_location: str, destination_location: str, departure_time: Optional[int] = None) -> str: |
|
"""Gets the travel time in car between two places. |
|
|
|
Args: |
|
start_location: the place from which you start your ride |
|
destination_location: the place of arrival |
|
departure_time: the departure time, provide only a `datetime.datetime` if you want to specify this |
|
""" |
|
import googlemaps |
|
from google.colab import userdata |
|
import os |
|
|
|
gmaps = googlemaps.Client(userdata.get("GMAPS_API_KEY")) |
|
|
|
if departure_time is None: |
|
from datetime import datetime |
|
departure_time = datetime(2025, 1, 6, 11, 0) |
|
|
|
directions_result = gmaps.directions( |
|
start_location, |
|
destination_location, |
|
mode="transit", |
|
departure_time=departure_time |
|
) |
|
return directions_result[0]["legs"][0]["duration"]["text"] |