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

Commit b60f75b

Browse files
committed
Fast fix session error
1 parent 1f2defc commit b60f75b

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

chatgpt/authentication.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def get_session(self):
123123
except HTTPTLSError as e:
124124
raise ChatgptError(
125125
"Error getting the session. You may have a wrong access token; try to login again or insert an access_token yourself.",
126-
ChatgptErrorCodes.LOGIN_ERROR) from e
126+
ChatgptErrorCodes.SESSION_ERROR) from e
127127

128128
def login(self, username, password):
129129
"""Get the access token for chatgpt

chatgpt/chatgpt.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ def chat(self, message: List[str], retry_on_401: bool = True, direct_response: b
309309
message = [message]
310310
try:
311311
if self._access_token_expire is not None:
312-
if self._access_token_expire < get_utc_now_datetime():
312+
if self._access_token_expire < get_utc_now_datetime() and self._chatgpt_session_expire > get_utc_now_datetime():
313313
self.get_session()
314314

315315
if self._access_token is None:
@@ -398,7 +398,14 @@ def chat(self, message: List[str], retry_on_401: bool = True, direct_response: b
398398
except ChatgptError as ex:
399399
exception_message = ex.message
400400
exception_code = ex.code
401-
if exception_code == ChatgptErrorCodes.LOGIN_ERROR or exception_code == ChatgptErrorCodes.TIMEOUT_ERROR and retry_on_401:
401+
if exception_code == ChatgptErrorCodes.SESSION_ERROR:
402+
self._chatgpt_session_expire = get_utc_now_datetime()
403+
self._tls_session._cookies = {}
404+
self._cookies = {}
405+
self._session._cookies = {}
406+
self._access_token = None
407+
return self.chat(message, False, direct_response=direct_response, stream=stream)
408+
elif exception_code == ChatgptErrorCodes.LOGIN_ERROR or exception_code == ChatgptErrorCodes.TIMEOUT_ERROR and retry_on_401:
402409
return self.chat(message, False, direct_response=direct_response, stream=stream)
403410

404411
except TLSClientExeption as ex:

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+
SESSION_ERROR = "session_error"
1314
TIMEOUT_ERROR = "timeout_error"
1415
CONNECTION_ERROR = "connection_error"
1516

0 commit comments

Comments
 (0)