| 5 | from ...tool import Tool |
| 6 | |
| 7 | def build_tool(config) -> Tool: |
| 8 | tool = Tool( |
| 9 | "Map Info form bing map api", |
| 10 | "Look up map information", |
| 11 | name_for_model="BingMap", |
| 12 | description_for_model="Plugin for look up map information", |
| 13 | logo_url="https://your-app-url.com/.well-known/logo.png", |
| 14 | contact_email="hello@contact.com", |
| 15 | legal_info_url="hello@legal.com" |
| 16 | ) |
| 17 | |
| 18 | KEY = config["subscription_key"] |
| 19 | BASE_URL = 'http://dev.virtualearth.net/REST/V1/' |
| 20 | |
| 21 | @tool.get("/get_distance") |
| 22 | def get_distance(start:str, end:str): |
| 23 | """Get the distance between two locations in miles""" |
| 24 | # Request URL |
| 25 | url = BASE_URL + "Routes/Driving?o=json&wp.0=" + start + "&wp.1=" + end + "&key=" + KEY |
| 26 | # GET request |
| 27 | r = requests.get(url) |
| 28 | data = json.loads(r.text) |
| 29 | # Extract route information |
| 30 | route = data["resourceSets"][0]["resources"][0] |
| 31 | # Extract distance in miles |
| 32 | distance = route["travelDistance"] |
| 33 | return distance |
| 34 | |
| 35 | @tool.get("/get_route") |
| 36 | def get_route(start:str, end:str): |
| 37 | """Get the route between two locations in miles""" |
| 38 | # Request URL |
| 39 | url = BASE_URL + "Routes/Driving?o=json&wp.0=" + start + "&wp.1=" + end + "&key=" + KEY |
| 40 | # GET request |
| 41 | r = requests.get(url) |
| 42 | data = json.loads(r.text) |
| 43 | # Extract route information |
| 44 | route = data["resourceSets"][0]["resources"][0] |
| 45 | itinerary = route["routeLegs"][0]["itineraryItems"] |
| 46 | # Extract route text information |
| 47 | route_text = [] |
| 48 | for item in itinerary: |
| 49 | if "instruction" in item: |
| 50 | route_text.append(item["instruction"]["text"]) |
| 51 | return route_text |
| 52 | |
| 53 | @tool.get("/get_coordinates") |
| 54 | def get_coordinates(location:str): |
| 55 | """Get the coordinates of a location""" |
| 56 | url = BASE_URL + "Locations" |
| 57 | params = { |
| 58 | "query": location, |
| 59 | "key": KEY |
| 60 | } |
| 61 | response = requests.get(url, params=params) |
| 62 | json_data = response.json() |
| 63 | coordinates = json_data["resourceSets"][0]["resources"][0]["point"]["coordinates"] |
| 64 | return coordinates |