| 11 | |
| 12 | |
| 13 | def build_tool(config) -> Tool: |
| 14 | tool = Tool( |
| 15 | "Travel Info", |
| 16 | "Look up travel infomation about lodging, flight, car rental and landscape", |
| 17 | name_for_model="Travel", |
| 18 | description_for_model="""This is a plugin for look up real travel infomation. Results from this API are inaccessible for users. Please organize and re-present them.""", |
| 19 | logo_url="https://your-app-url.com/.well-known/logo.png", |
| 20 | contact_email="hello@contact.com", |
| 21 | legal_info_url="hello@legal.com" |
| 22 | ) |
| 23 | |
| 24 | SERPAPI_KEY = os.environ.get('SERPAPI_KEY', '') |
| 25 | if SERPAPI_KEY == '': |
| 26 | raise RuntimeError("SERPAPI_KEY not provided, please register one at https://serpapi.com/search-api and add it to environment variables.") |
| 27 | |
| 28 | AMADEUS_ID = os.environ.get('AMADEUS_ID', '') |
| 29 | if AMADEUS_ID == '': |
| 30 | raise RuntimeError("AMADEUS_ID not provided, please register one following https://developers.amadeus.com/ and add it to environment variables.") |
| 31 | |
| 32 | AMADEUS_KEY = os.environ.get('AMADEUS_KEY', '') |
| 33 | if AMADEUS_KEY == '': |
| 34 | raise RuntimeError("AMADEUS_KEY not provided, please register one following https://developers.amadeus.com/ and add it to environment variables.") |
| 35 | |
| 36 | def cName2coords(place_name: str, |
| 37 | limits: Optional[int] = 1): |
| 38 | """ |
| 39 | This function accepts a place name and returns its coordinates. |
| 40 | :param (str) place_name: a string standing for target city for locating. |
| 41 | :param (str) limits: number of searching results. Usually 1 is enough. |
| 42 | :return: (longitude, latitude) |
| 43 | """ |
| 44 | url = f"https://serpapi.com/locations.json?q={place_name}&limit={limits}" |
| 45 | response = requests.get(url) |
| 46 | if response.status_code == 200: |
| 47 | |
| 48 | if response.json(): |
| 49 | locations = response.json() |
| 50 | return locations[0]['gps'] |
| 51 | |
| 52 | # if not a city, use google map to find this place |
| 53 | else: |
| 54 | params = {"engine": "google_maps", "q": place_name, "type": "search", "api_key": SERPAPI_KEY} |
| 55 | search = GoogleSearch(params) |
| 56 | results = search.get_dict() |
| 57 | coords = results["place_results"]['gps_coordinates'] |
| 58 | |
| 59 | return coords["longitude"], coords["latitude"] |
| 60 | |
| 61 | else: |
| 62 | return None |
| 63 | |
| 64 | |
| 65 | def cName2IATA(city_name: str): |
| 66 | """ |
| 67 | This function accepts a city name and returns a IATA pilot code of the local airport. |
| 68 | :param (str) city_name: city name a s a string like 'Beijing' |
| 69 | :return: 3-letter IATA code like 'PEK' |
| 70 | |