@@ -10,7 +10,7 @@ async def async_post( session
10
10
, url
11
11
, data :str
12
12
, headers :Dict
13
- , max_requests :int = 1
13
+ , max_tries :int = 1
14
14
, timeinterval = 0
15
15
, timeout = 0 ):
16
16
"""Asynchronous post request
@@ -21,7 +21,7 @@ async def async_post( session
21
21
url (str): chat completion url
22
22
data (str): payload of the request
23
23
headers (Dict): request headers
24
- max_requests (int, optional): maximum number of requests to make. Defaults to 1.
24
+ max_tries (int, optional): maximum number of requests to make. Defaults to 1.
25
25
timeinterval (int, optional): time interval between two API calls. Defaults to 0.
26
26
timeout (int, optional): timeout for the API call. Defaults to 0(no timeout).
27
27
@@ -30,15 +30,15 @@ async def async_post( session
30
30
"""
31
31
async with sem :
32
32
ntries = 0
33
- while max_requests > 0 :
33
+ while max_tries > 0 :
34
34
try :
35
35
async with session .post (url , headers = headers , data = data , timeout = timeout ) as response :
36
36
resp = await response .text ()
37
37
resp = Resp (json .loads (resp ))
38
38
assert resp .is_valid (), resp .error_message
39
39
return resp
40
40
except Exception as e :
41
- max_requests -= 1
41
+ max_tries -= 1
42
42
ntries += 1
43
43
time .sleep (random .random () * timeinterval )
44
44
print (f"Request Failed({ ntries } ):{ e } " )
@@ -50,11 +50,10 @@ async def async_process_msgs( chatlogs:List[List[Dict]]
50
50
, chkpoint :str
51
51
, api_key :str
52
52
, chat_url :str
53
- , max_requests :int = 1
54
- , ncoroutines :int = 1
53
+ , max_tries :int = 1
54
+ , nproc :int = 1
55
55
, timeout :int = 0
56
56
, timeinterval :int = 0
57
- , max_tokens :Union [Callable , None ]= None
58
57
, ** options
59
58
)-> List [bool ]:
60
59
"""Process messages asynchronously
@@ -63,38 +62,36 @@ async def async_process_msgs( chatlogs:List[List[Dict]]
63
62
chatlogs (List[List[Dict]]): list of chat logs
64
63
chkpoint (str): checkpoint file
65
64
api_key (Union[str, None], optional): API key. Defaults to None.
66
- max_requests (int, optional): maximum number of requests to make. Defaults to 1.
67
- ncoroutines (int, optional): number of coroutines. Defaults to 5.
65
+ max_tries (int, optional): maximum number of requests to make. Defaults to 1.
66
+ nproc (int, optional): number of coroutines. Defaults to 5.
68
67
timeout (int, optional): timeout for the API call. Defaults to 0(no timeout).
69
68
timeinterval (int, optional): time interval between two API calls. Defaults to 0.
70
69
71
70
Returns:
72
71
List[bool]: list of responses
73
72
"""
74
73
# load from checkpoint
75
- chats = load_chats (chkpoint , withid = True ) if os .path .exists (chkpoint ) else []
74
+ chats = load_chats (chkpoint ) if os .path .exists (chkpoint ) else []
76
75
chats .extend ([None ] * (len (chatlogs ) - len (chats )))
77
76
costs = [0 ] * len (chatlogs )
78
77
headers = {
79
78
"Content-Type" : "application/json" ,
80
79
"Authorization" : "Bearer " + api_key
81
80
}
82
- ncoroutines += 1 # add one for the main coroutine
83
- sem = asyncio .Semaphore (ncoroutines )
81
+ nproc += 1 # add one for the main coroutine
82
+ sem = asyncio .Semaphore (nproc )
84
83
locker = asyncio .Lock ()
85
84
86
85
async def chat_complete (ind , locker , chat_log , chkpoint , ** options ):
87
86
payload = {"messages" : chat_log }
88
87
payload .update (options )
89
- if max_tokens is not None :
90
- payload ['max_tokens' ] = max_tokens (chat_log )
91
88
data = json .dumps (payload )
92
89
resp = await async_post ( session = session
93
90
, sem = sem
94
91
, url = chat_url
95
92
, data = data
96
93
, headers = headers
97
- , max_requests = max_requests
94
+ , max_tries = max_tries
98
95
, timeinterval = timeinterval
99
96
, timeout = timeout )
100
97
## saving files
@@ -130,16 +127,16 @@ def async_chat_completion( msgs:Union[List[List[Dict]], str]
130
127
, model :str = 'gpt-3.5-turbo'
131
128
, api_key :Union [str , None ]= None
132
129
, chat_url :Union [str , None ]= None
133
- , max_requests :int = 1
134
- , ncoroutines :int = 1
130
+ , max_tries :int = 1
135
131
, nproc :int = 1
136
132
, timeout :int = 0
137
133
, timeinterval :int = 0
138
134
, clearfile :bool = False
139
135
, notrun :bool = False
140
136
, msg2log :Union [Callable , None ]= None
141
137
, data2chat :Union [Callable , None ]= None
142
- , max_tokens :Union [Callable , int , None ]= None
138
+ , max_requests :int = - 1
139
+ , ncoroutines :int = 1
143
140
, ** options
144
141
):
145
142
"""Asynchronous chat completion
@@ -149,8 +146,7 @@ def async_chat_completion( msgs:Union[List[List[Dict]], str]
149
146
chkpoint (str): checkpoint file
150
147
model (str, optional): model to use. Defaults to 'gpt-3.5-turbo'.
151
148
api_key (Union[str, None], optional): API key. Defaults to None.
152
- max_requests (int, optional): maximum number of requests to make. Defaults to 1.
153
- ncoroutines (int, optional): (Deprecated)number of coroutines. Defaults to 1.
149
+ max_tries (int, optional): maximum number of requests to make. Defaults to 1.
154
150
nproc (int, optional): number of coroutines. Defaults to 1.
155
151
timeout (int, optional): timeout for the API call. Defaults to 0(no timeout).
156
152
timeinterval (int, optional): time interval between two API calls. Defaults to 0.
@@ -161,8 +157,8 @@ def async_chat_completion( msgs:Union[List[List[Dict]], str]
161
157
Defaults to None.
162
158
data2chat (Union[Callable, None], optional): function to convert data to Chat object.
163
159
Defaults to None.
164
- max_tokens (Union[Callable, int, None], optional): function to calculate the maximum
165
- number of tokens for the API call . Defaults to None .
160
+ max_requests ( int, optional): (Deprecated)maximum number of requests to make. Defaults to -1.
161
+ ncoroutines (int, optional): (Deprecated) number of coroutines . Defaults to 1 .
166
162
167
163
Returns:
168
164
List[Dict]: list of responses
@@ -184,20 +180,18 @@ def async_chat_completion( msgs:Union[List[List[Dict]], str]
184
180
chat_url = os .path .join (chattool .base_url , "v1/chat/completions" )
185
181
chat_url = chattool .request .normalize_url (chat_url )
186
182
# run async process
187
- assert ncoroutines > 0 , "ncoroutines must be greater than 0!"
188
- if isinstance (max_tokens , int ):
189
- max_tokens = lambda chat_log : max_tokens
183
+ assert nproc > 0 , "nproc must be greater than 0!"
184
+ max_tries = max (max_tries , max_requests )
190
185
args = {
191
186
"chatlogs" : chatlogs ,
192
187
"chkpoint" : chkpoint ,
193
188
"api_key" : api_key ,
194
189
"chat_url" : chat_url ,
195
- "max_requests " : max_requests ,
196
- "ncoroutines " : nproc ,
190
+ "max_tries " : max_tries ,
191
+ "nproc " : nproc ,
197
192
"timeout" : timeout ,
198
193
"timeinterval" : timeinterval ,
199
194
"model" : model ,
200
- "max_tokens" : max_tokens ,
201
195
** options
202
196
}
203
197
if notrun : # when use in Jupyter Notebook
0 commit comments