-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweather.py
47 lines (36 loc) · 1.55 KB
/
weather.py
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
from dotenv import load_dotenv
import os
from openai import OpenAI
import requests
# Load environment variables from .env file
load_dotenv()
client = OpenAI()
api_url = os.getenv("WEATHER_API_CALL")
# Make a function for the call
def getWeather():
# put weather call here
try:
response = requests.get(api_url)
response.raise_for_status()
data = response.json()
location = data['location']['name']
temperature = int(data['current']['temp_f'])
condition = data['current']['condition']['text']
completion = client.chat.completions.create(
model="gpt-4-turbo",
messages=[
{"role": "system", "content": "You are a meteorologist who gives the weather report in the style of Diana Vreeland. You provide outfit suggestions according to the temperature. Your report should not exceed 2 sentences. Mention the weather with the ° symbol."},
{"role": "user", "content": f"Give me the weather report for a {temperature} degree fahrenheit day in {location}. The condition is {condition}."}
]
)
print(completion.choices[0].message.content)
except requests.exceptions.HTTPError as http_err:
# Handle HTTP errors (4xx and 5xx status codes)
print(f"HTTP Error: {http_err}")
except requests.exceptions.RequestException as req_err:
# Handle other request-related errors
print(f"Request Error: {req_err}")
except Exception as err:
# Handle general exceptions
print(f"An error occurred: {err}")
getWeather()