-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.py
28 lines (26 loc) · 1.04 KB
/
common.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
import openai
import time
# set the openai host and port
openai.api_base = "http://127.0.0.1:5001/v1"
def tryMessages(messages, model="gpt-4-0314"):
while True:
try:
response = openai.ChatCompletion.create(
model=model,
messages=messages
)
return response["choices"][0]["message"]["content"].strip()
except Exception as e:
# if it's an openai.error.RateLimitError, wait 10 seconds and try again
if type(e) == openai.error.RateLimitError:
print("rate limit error, waiting 10 seconds and trying again")
time.sleep(10)
elif type(e) == openai.error.ServiceUnavailableError:
print("service unavailable error, waiting 10 seconds and trying again")
time.sleep(20)
elif type(e) == openai.error.APIError:
print("api error, waiting 10 seconds and trying again")
print(e)
time.sleep(10)
else:
raise e