Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit 05d0982

Browse files
authored
Merge pull request #3 from AlexSua/master
Add configurable timeout and increase the default time limit to 120
2 parents b279fda + 3ecbc12 commit 05d0982

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

chatgpt/chatgpt.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
from uuid import uuid4 as uuid
1010
from urllib.error import HTTPError
1111
from .errors import ChatgptError, ChatgptErrorCodes
12+
from tls_client.sessions import TLSClientExeption
1213

1314

1415
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
1721

1822
def request(self, *args, headers={}, **kwargs):
1923
send_headers = {
@@ -26,8 +30,8 @@ def request(self, *args, headers={}, **kwargs):
2630
"Referer": "https://chat.openai.com/"
2731
}
2832
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)
3135
if response.status_code == 200:
3236
return response
3337
else:
@@ -201,6 +205,7 @@ def __init__(
201205
password: str = None,
202206
conversation_id: str = None,
203207
parent_message_id: str = None,
208+
timeout:int = None
204209
):
205210
"""
206211
Args:
@@ -210,6 +215,7 @@ def __init__(
210215
password (str): Password. Defaults to None.
211216
conversation_id (str, optional): Conversation id with which the conversation starts. Defaults to None.
212217
parent_message_id (str, optional): Parent id with which the conversation starts. Defaults to None.
218+
timout (int, optional): Timeout duration in seconds.
213219
"""
214220

215221
if config_path is not None:
@@ -232,7 +238,7 @@ def __init__(
232238
if self._parent_message_id is None:
233239
self._parent_message_id = parent_message_id
234240

235-
self._session = HTTPSession()
241+
self._session = HTTPSession(timeout=timeout)
236242
self._openai_authentication = OpenAIAuthentication(self._session)
237243

238244
def __remove_none_values(self, d):
@@ -335,8 +341,6 @@ def chat(self, message: List[str], retry_on_401: bool = True):
335341
"model": self._model_name
336342
}
337343
payload = json.dumps(self.__remove_none_values(payload))
338-
exception_message = "Unknown error"
339-
exception_code = ChatgptErrorCodes.UNKNOWN_ERROR
340344
try:
341345
response = self._session.request(
342346
"POST", url, data=payload, headers={
@@ -355,7 +359,8 @@ def chat(self, message: List[str], retry_on_401: bool = True):
355359
return postprocessed_text
356360

357361
except HTTPError as ex:
358-
362+
exception_message = "Unknown error"
363+
exception_code = ChatgptErrorCodes.UNKNOWN_ERROR
359364
error_code = ex.code
360365
if error_code in [401, 409]:
361366
self._access_token = None
@@ -379,8 +384,13 @@ def chat(self, message: List[str], retry_on_401: bool = True):
379384
except ValueError as e:
380385
exception_message = ex.msg
381386

382-
except Exception as ex:
387+
except TLSClientExeption as ex:
383388
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
384394

385395
raise ChatgptError(
386396
exception_message, exception_code)

chatgpt/errors.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class ChatgptErrorCodes(Enum):
1010
CONFIG_FILE_ERROR = "config_file_error"
1111
UNKNOWN_ERROR = "unknown_error"
1212
LOGIN_ERROR = "login_error"
13+
TIMEOUT_ERROR = "timeout_error"
1314

1415
class ChatgptError(Exception):
1516
"""

0 commit comments

Comments
 (0)