-
Notifications
You must be signed in to change notification settings - Fork 18
/
openai_concurrent.py
159 lines (117 loc) · 5.76 KB
/
openai_concurrent.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from concurrent.futures import ProcessPoolExecutor
import argparse
import openai
from time import sleep
import random
import json
import fcntl
from typing import List
import os
from tenacity import (
retry,
stop_after_attempt,
wait_random_exponential
)
def main():
API_KEYS = os.environ["OPENAI_API_KEYS"].split(",")
parser = argparse.ArgumentParser()
parser.add_argument("--input-path", type=str, required=True)
parser.add_argument("--output-path", type=str, required=True)
parser.add_argument("--fail-path", type=str, required=True)
parser.add_argument("--requests-per-minute", type=int, default=60, help="Number of requests per minute per API key")
parser.add_argument("--expected_response_seconds", type=float, default=5, help="Number of seconds to wait for a response")
args = parser.parse_args()
openai_concurrent = OpenAIChatCompletionConcurrent(api_keys=API_KEYS, requests_per_minute=args.requests_per_minute, expected_response_seconds=args.expected_response_seconds)
openai_concurrent.create_many_file(input_path=args.input_path, output_path=args.output_path, fail_path=args.fail_path)
class OpenAIChatCompletionConcurrent:
def __init__(self, api_keys: List[str], requests_per_minute: int = 60, expected_response_seconds: float = 5.0):
self.api_keys = api_keys
self.requests_per_minute = requests_per_minute
self.expected_response_seconds = expected_response_seconds
self.num_api_keys = len(self.api_keys)
requests_per_second = self.requests_per_minute / 60
simultaneous_num_requests = requests_per_second * self.expected_response_seconds
buffer = 2
self.num_workers = int(simultaneous_num_requests * buffer)
total_requests_per_second = requests_per_second * self.num_api_keys
self.time_between_requests = 1 / total_requests_per_second
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def create(self, model: str, messages: List[dict], temperature: float, max_tokens: int):
openai.api_key = random.choice(self.api_keys)
return openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=temperature,
max_tokens=max_tokens,
)
def create_many(self, requests: List[dict]):
futures = []
with ProcessPoolExecutor(max_workers=self.num_workers) as executor:
for item_index, item in enumerate(requests):
api_key = self.api_keys[item_index % self.num_api_keys]
future = executor.submit(call_and_return, api_key=api_key, item=item)
futures.append(future)
sleep(self.time_between_requests)
responses = []
fails = []
for future in futures:
response, success = future.result()
if success:
responses.append(response)
else:
fails.append(response)
return responses, fails
def create_many_file(self, input_path: str, output_path: str, fail_path: str):
with open(input_path, "r") as input_file:
requests = [json.loads(line) for line in input_file.readlines()]
with ProcessPoolExecutor(max_workers=self.num_workers) as executor:
for item_index, item in enumerate(requests):
api_key = self.api_keys[item_index % self.num_api_keys]
executor.submit(call_and_write, api_key=api_key, item=item, output_path=output_path, fail_path=fail_path)
sleep(self.time_between_requests)
def call_and_return(api_key: str, item: dict):
try:
response = completion_with_backoff(api_key, **item["request"])
error = None
except Exception as e:
response = None
error = repr(e)
if error is None:
output_item = {**item, "api_key": api_key, "response": response}
else:
output_item = {**item, "api_key": api_key, "error": error}
return output_item, error is None
def call_and_write(api_key: str, item: dict, output_path: str, fail_path: str):
try:
response = completion_with_backoff(api_key, **item["request"])
error = None
except Exception as e:
response = None
error = repr(e)
if error is None:
output_item = {**item, "api_key": api_key, "response": response}
output_line = json.dumps(output_item)
with open(output_path, "a") as output_file:
fcntl.flock(output_file, fcntl.LOCK_EX)
output_file.write(output_line + "\n")
fcntl.flock(output_file, fcntl.LOCK_UN)
else:
fail_item = {**item, "api_key": api_key, "error": error}
fail_line = json.dumps(fail_item)
with open(fail_path, "a") as fail_file:
fcntl.flock(fail_file, fcntl.LOCK_EX)
fail_file.write(fail_line + "\n")
fcntl.flock(fail_file, fcntl.LOCK_UN)
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6), reraise=True)
def completion_with_backoff(api_key, **kwargs):
openai.api_key = api_key
return openai.ChatCompletion.create(**kwargs)
if __name__ == "__main__":
main()
# Test
# openai_concurrent = OpenAIChatCompletionConcurrent(api_keys=API_KEYS, requests_per_minute=60, expected_response_seconds=5)
# example_inputs = [
# {"request": {"model": "gpt-5", "messages": [{"role": "system", "content": "Hello, how are you?"}, {"role": "user", "content": "Hello"}], "temperature": 0.2, "max_tokens": 10}},
# {"request": {"model": "gpt-4", "messages": [{"role": "system", "content": "Hello, how are you?"}, {"role": "user", "content": "Hello"}], "temperature": 0.2, "max_tokens": 10}},
# ]
# print(openai_concurrent.create_many(example_inputs))