-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatgpt_api.py
66 lines (57 loc) · 2.1 KB
/
chatgpt_api.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import openai, time
class gpt_agent():
def __init__(self, api_key:str):
openai.api_key = api_key
self.api_key = api_key
self.ask_call_cnt = 0
self.ask_call_cnt_sup = 3
def get_embedding(self, text) -> list:
response = openai.Embedding.create(
input=text,
model="text-embedding-ada-002"
)
embeddings = response['data'][0]['embedding']
return embeddings
def ask(self, question) -> str:
res = None
self.ask_call_cnt = self.ask_call_cnt + 1
if self.ask_call_cnt > self.ask_call_cnt_sup:
print("======> Achieve call count limit, Return!")
self.ask_call_cnt = 0
return res
messages = [{"role": "user", "content": question}]
try:
rsp = openai.ChatCompletion.create(
# model="gpt-3.5-turbo",
model="gpt-4",
messages=messages,
temperature=0.7
)
res = rsp.get("choices")[0]["message"]["content"]
self.ask_call_cnt = 0
except openai.error.AuthenticationError as e:
print("======> openai.error.AuthenticationError", e)
except openai.error.RateLimitError as e:
"""
no need to exit!
if "quota" in str(e.error):
print("openai.error.RateLimitError", e)
exit(0)
else:
print("Achieve ChatGPT rate limit, sleep!")
time.sleep(10)
return self.ask(question)
"""
print(f"======> {self.api_key} <===== \nAchieve ChatGPT rate limit, sleep!", e)
if "quota" in str(e.error):
pass
else:
time.sleep(10)
return self.ask(question)
except openai.error.ServiceUnavailableError:
print('======> Service unavailable error: will retry after 10 seconds')
time.sleep(10)
return self.ask(question)
except Exception as e:
print("======> Exception occurs!", e)
return res