-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.py
32 lines (29 loc) · 1.07 KB
/
validator.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
import openai, os
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
OPENAI_MODEL = "gpt-3.5-turbo"
openai.api_key = OPENAI_API_KEY
NOT_A_CITY = "NOT A CITY"
def correct_city_name(city):
corrected_city = ""
try:
prompt = f"Correct city name for {city}, it will be used on OpenWeather API,\
if it is not a city, just reply {NOT_A_CITY},\
remember to just reply the corrected city name or {NOT_A_CITY}, no additional text"
response = openai.ChatCompletion.create(
model=OPENAI_MODEL,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt}
]
)
if response.choices:
corrected_city = response.choices[0].text.strip()
else:
return city
except:
if corrected_city and NOT_A_CITY in corrected_city:
raise Exception("El nombre de ciudad brindado no es un nombre válido.")
elif corrected_city:
return corrected_city
else:
return city