extract latitude and longitude directly

This commit is contained in:
ned
2023-06-14 20:10:15 +02:00
parent 97f58b30ff
commit 0d0158eaf2
3 changed files with 14 additions and 13 deletions

View File

@@ -18,6 +18,6 @@ async def call_function(function_name, arguments):
""" """
if function_name == "get_current_weather": if function_name == "get_current_weather":
arguments = json.loads(arguments) arguments = json.loads(arguments)
return await get_current_weather(arguments["location"], arguments["unit"]) return await get_current_weather(arguments["latitude"], arguments["longitude"], arguments["unit"])
raise Exception(f"Function {function_name} not found") raise Exception(f"Function {function_name} not found")

View File

@@ -1,7 +1,6 @@
import json import json
import requests import requests
from geopy import Nominatim
def weather_function_spec(): def weather_function_spec():
@@ -11,9 +10,13 @@ def weather_function_spec():
"parameters": { "parameters": {
"type": "object", "type": "object",
"properties": { "properties": {
"location": { "latitude": {
"type": "string", "type": "string",
"description": "The exact city and state, e.g. San Francisco, CA" "description": "Latitude of the location"
},
"longitude": {
"type": "string",
"description": "Longitude of the location"
}, },
"unit": { "unit": {
"type": "string", "type": "string",
@@ -21,24 +24,23 @@ def weather_function_spec():
"description": "The temperature unit to use. Infer this from the provided location.", "description": "The temperature unit to use. Infer this from the provided location.",
}, },
}, },
"required": ["location", "unit"], "required": ["latitude", "longitude", "unit"],
} }
} }
async def get_current_weather(location, unit): async def get_current_weather(latitude, longitude, unit):
""" """
Get the current weather in a given location using the Open Meteo API Get the current weather in a given location using the Open Meteo API
Source: https://open-meteo.com/en/docs Source: https://open-meteo.com/en/docs
:param location: The location to get the weather for, in natural language :param latitude: The latitude of the location to get the weather for
:param longitude: The longitude of the location to get the weather for
:param unit: The unit to use for the temperature (`celsius` or `fahrenheit`) :param unit: The unit to use for the temperature (`celsius` or `fahrenheit`)
:return: The JSON response to be fed back to the model :return: The JSON response to be fed back to the model
""" """
geolocator = Nominatim(user_agent="chatgpt-telegram-bot")
geoloc = geolocator.geocode(location)
request = requests.get(f'https://api.open-meteo.com/v1/forecast' request = requests.get(f'https://api.open-meteo.com/v1/forecast'
f'?latitude={geoloc.latitude}' f'?latitude={latitude}'
f'&longitude={geoloc.longitude}' f'&longitude={longitude}'
f'&daily=weathercode,temperature_2m_max,temperature_2m_min,precipitation_probability_mean,' f'&daily=weathercode,temperature_2m_max,temperature_2m_min,precipitation_probability_mean,'
f'&forecast_days=7' f'&forecast_days=7'
f'&timezone=auto' f'&timezone=auto'

View File

@@ -5,4 +5,3 @@ openai==0.27.8
python-telegram-bot==20.3 python-telegram-bot==20.3
requests~=2.31.0 requests~=2.31.0
tenacity==8.2.2 tenacity==8.2.2
geopy~=2.3.0