-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_commit_git.py
More file actions
36 lines (32 loc) · 1.3 KB
/
openai_commit_git.py
File metadata and controls
36 lines (32 loc) · 1.3 KB
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
import openai
from util import get_message
openai.api_type = "azure"
openai.api_base = "https://hkust.azure-api.net"
openai.api_key = "3d7fb039f35b481fafb05a2e4ac1acf6"
openai.api_version = "2023-05-15"
from tqdm import tqdm
from tenacity import retry,stop_after_attempt,stop_after_delay,wait_fixed
gpt_answer_path = r'./gpt_answer.txt'
sample_session_path = r'./data/uk_sample_1000.txt'
sample_item_path= r'./data/uk_item_sample_1000.txt'
# retry 5 times, interval 10s, waiting 2s
# @retry(stop=(stop_after_delay(10) | stop_after_attempt(5)), wait=wait_fixed(2))
def get_message_from_api(message):
response = openai.ChatCompletion.create(
engine = "gpt-35-turbo",
temperature = 0,
messages = [{"role":"user","content":message}])
a = response.get("choices")[0]["message"]["content"]
return a
def get_gpt_answer():
f = open(gpt_answer_path,'w',encoding='utf-8')
for sessions, message in tqdm(get_message(sample_session_path,sample_item_path)):
try:
a = get_message_from_api(message=message)
except Exception as e:
print(f'session {idx} have expection:')
print(e)
a = 'ERROR:'+str(idx)
f.write('Session: '+str(sessions)+'\n')
f.write(a+'\n'+'-------------------------'+'\n')
get_gpt_answer()