-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
64 lines (50 loc) · 1.52 KB
/
utils.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
import asyncio
import openai
from tqdm import tqdm
import time
import json
openai.api_key = ""
openai.api_base = ""
def read_json(file_name):
with open(file_name, 'r') as f:
return json.load(f)
def save_json(data, file_name):
with open(file_name, "w") as outfile:
json.dump(data, outfile, indent=2)
def return_args():
config= {
"seed": 2024,
"api_batch": 20,
}
return config
async def dispatch_openai_requests(
messages_list,
model: str,
temperature: float,
):
async_responses = [
openai.ChatCompletion.acreate(
model=model,
messages=x,
temperature=0
)
for x in messages_list
]
return await asyncio.gather(*async_responses)
def dispatch_openai_api_requests(prompt_list, batch_size, api_batch, api_model_name = "gpt-3.5-turbo"):
openai_responses = []
for i in tqdm(range(0, batch_size, api_batch)):
while True:
try:
openai_responses += asyncio.run(
dispatch_openai_requests(prompt_list[i:i + api_batch], api_model_name, 0)
)
break
except KeyboardInterrupt:
print(f'KeyboardInterrupt Error, retry batch {i // api_batch} at {time.ctime()}',
flush=True)
time.sleep(5)
except Exception as e:
print(f'Error {e}, retry batch {i // api_batch} at {time.ctime()}', flush=True)
time.sleep(5)
return openai_responses