Spaces:
Runtime error
Runtime error
File size: 3,651 Bytes
e67043b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
import requests
import os
import json
from ...tool import Tool
def build_tool(config) -> Tool:
tool = Tool(
"Map Info form bing map api",
"Look up map information",
name_for_model="BingMap",
description_for_model="Plugin for look up map information",
logo_url="https://your-app-url.com/.well-known/logo.png",
contact_email="[email protected]",
legal_info_url="[email protected]",
)
KEY = config["subscription_key"]
BASE_URL = "http://dev.virtualearth.net/REST/V1/"
@tool.get("/get_distance")
def get_distance(start: str, end: str):
"""Get the distance between two locations in miles"""
# Request URL
url = (
BASE_URL
+ "Routes/Driving?o=json&wp.0="
+ start
+ "&wp.1="
+ end
+ "&key="
+ KEY
)
# GET request
r = requests.get(url)
data = json.loads(r.text)
# Extract route information
route = data["resourceSets"][0]["resources"][0]
# Extract distance in miles
distance = route["travelDistance"]
return distance
@tool.get("/get_route")
def get_route(start: str, end: str):
"""Get the route between two locations in miles"""
# Request URL
url = (
BASE_URL
+ "Routes/Driving?o=json&wp.0="
+ start
+ "&wp.1="
+ end
+ "&key="
+ KEY
)
# GET request
r = requests.get(url)
data = json.loads(r.text)
# Extract route information
route = data["resourceSets"][0]["resources"][0]
itinerary = route["routeLegs"][0]["itineraryItems"]
# Extract route text information
route_text = []
for item in itinerary:
if "instruction" in item:
route_text.append(item["instruction"]["text"])
return route_text
@tool.get("/get_coordinates")
def get_coordinates(location: str):
"""Get the coordinates of a location"""
url = BASE_URL + "Locations"
params = {"query": location, "key": KEY}
response = requests.get(url, params=params)
json_data = response.json()
coordinates = json_data["resourceSets"][0]["resources"][0]["point"][
"coordinates"
]
return coordinates
@tool.get("/search_nearby")
def search_nearyby(
search_term: str = "restaurant",
latitude: float = 0.0,
longitude: float = 0.0,
places: str = "unknown",
radius: int = 5000,
): # radius in meters
"""Search for places nearby a location, within a given radius, and return the results into a list"""
url = BASE_URL + "LocalSearch"
if places != "unknown":
latitude = get_coordinates(places)[0]
longitude = get_coordinates(places)[1]
# Build the request query string
params = {
"query": search_term,
"userLocation": f"{latitude},{longitude}",
"radius": radius,
"key": KEY,
}
# Make the request
response = requests.get(url, params=params)
# Parse the response
response_data = json.loads(response.content)
# Get the results
results = response_data["resourceSets"][0]["resources"]
addresses = []
for result in results:
name = result["name"]
address = result["Address"]["formattedAddress"]
addresses.append(f"{name}: {address}")
return addresses
return tool
|