9
9
from uuid import uuid4 as uuid
10
10
from urllib .error import HTTPError
11
11
from .errors import ChatgptError , ChatgptErrorCodes
12
+ from tls_client .sessions import TLSClientExeption
12
13
13
14
14
15
class HTTPSession :
15
- def __init__ (self ):
16
- self .session = tls_client .Session (client_identifier = "chrome_107" )
16
+ DEFAULT_TIMEOUT = 120
17
+
18
+ def __init__ (self , timeout = DEFAULT_TIMEOUT ):
19
+ self ._session = tls_client .Session (client_identifier = "chrome_107" )
20
+ self ._timeout = timeout
17
21
18
22
def request (self , * args , headers = {}, ** kwargs ):
19
23
send_headers = {
@@ -26,8 +30,8 @@ def request(self, *args, headers={}, **kwargs):
26
30
"Referer" : "https://chat.openai.com/"
27
31
}
28
32
send_headers .update (headers )
29
- response = self .session .execute_request (
30
- * args , headers = send_headers , timeout_seconds = 120 , ** kwargs )
33
+ response = self ._session .execute_request (
34
+ * args , headers = send_headers , timeout_seconds = self . _timeout , ** kwargs )
31
35
if response .status_code == 200 :
32
36
return response
33
37
else :
@@ -201,6 +205,7 @@ def __init__(
201
205
password : str = None ,
202
206
conversation_id : str = None ,
203
207
parent_message_id : str = None ,
208
+ timeout :int = None
204
209
):
205
210
"""
206
211
Args:
@@ -210,6 +215,7 @@ def __init__(
210
215
password (str): Password. Defaults to None.
211
216
conversation_id (str, optional): Conversation id with which the conversation starts. Defaults to None.
212
217
parent_message_id (str, optional): Parent id with which the conversation starts. Defaults to None.
218
+ timout (int, optional): Timeout duration in seconds.
213
219
"""
214
220
215
221
if config_path is not None :
@@ -232,7 +238,7 @@ def __init__(
232
238
if self ._parent_message_id is None :
233
239
self ._parent_message_id = parent_message_id
234
240
235
- self ._session = HTTPSession ()
241
+ self ._session = HTTPSession (timeout = timeout )
236
242
self ._openai_authentication = OpenAIAuthentication (self ._session )
237
243
238
244
def __remove_none_values (self , d ):
@@ -335,8 +341,6 @@ def chat(self, message: List[str], retry_on_401: bool = True):
335
341
"model" : self ._model_name
336
342
}
337
343
payload = json .dumps (self .__remove_none_values (payload ))
338
- exception_message = "Unknown error"
339
- exception_code = ChatgptErrorCodes .UNKNOWN_ERROR
340
344
try :
341
345
response = self ._session .request (
342
346
"POST" , url , data = payload , headers = {
@@ -355,7 +359,8 @@ def chat(self, message: List[str], retry_on_401: bool = True):
355
359
return postprocessed_text
356
360
357
361
except HTTPError as ex :
358
-
362
+ exception_message = "Unknown error"
363
+ exception_code = ChatgptErrorCodes .UNKNOWN_ERROR
359
364
error_code = ex .code
360
365
if error_code in [401 , 409 ]:
361
366
self ._access_token = None
@@ -379,8 +384,13 @@ def chat(self, message: List[str], retry_on_401: bool = True):
379
384
except ValueError as e :
380
385
exception_message = ex .msg
381
386
382
- except Exception as ex :
387
+ except TLSClientExeption as ex :
383
388
exception_message = str (ex )
389
+ exception_code = ChatgptErrorCodes .TIMEOUT_ERROR
390
+
391
+ except Exception as e :
392
+ exception_message = str (e )
393
+ exception_code = ChatgptErrorCodes .UNKNOWN_ERROR
384
394
385
395
raise ChatgptError (
386
396
exception_message , exception_code )
0 commit comments