terry3041 / pyChatGPT

An unofficial Python wrapper for OpenAI's ChatGPT API
GNU General Public License v3.0
1.35k stars 252 forks source link

TimeoutException. I tried to set longer time such as 35, it still doesnt work #19

Closed Ulysses0817 closed 1 year ago

Ulysses0817 commented 1 year ago

Hi, I run my script on a VPS with debian 11, chrome=108.0.5359.94, xvfb=2:1.20.11-1.

  1. when i run the code python chat_test.py, i got error selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:39123 from chrome not reachable

    File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 108, in __init_browser
    self.driver = uc.Chrome(options=options, enable_cdp_events=True)
    File "/root/miniconda3/lib/python3.9/site-packages/undetected_chromedriver/__init__.py", line 429, in __init__
    super(Chrome, self).__init__(
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    super().__init__(
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/chromium/webdriver.py", line 106, in __init__
    super().__init__(
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 288, in __init__
    self.start_session(capabilities, browser_profile)
    File "/root/miniconda3/lib/python3.9/site-packages/undetected_chromedriver/__init__.py", line 715, in start_session
    super(selenium.webdriver.chrome.webdriver.WebDriver, self).start_session(
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 381, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: Message: unknown error: cannot connect to chrome at 127.0.0.1:49773
    from chrome not reachable
    Stacktrace:
    #0 0x5625cfe822a3 <unknown>
  2. Then i excute xvfb-run -a python chat_test.py and got this error selenium.common.exceptions.WebDriverException: Message: unknown error: failed to change window state to 'minimized', current state is 'normal'

    (base) root@adalee:~# xvfb-run -a python chat_test.py 
    Traceback (most recent call last):
    File "/root/chat_test.py", line 3, in <module>
    api = ChatGPT(auth_type='google', email='****@gmail.com', password='*****') # auth with google login
    File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 66, in __init__
    self.__init_browser()
    File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 131, in __init_browser
    self.__ensure_cf()
    File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 271, in __ensure_cf
    self.driver.minimize_window()
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 619, in minimize_window
    self.execute(Command.MINIMIZE_WINDOW)
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
    File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
    selenium.common.exceptions.WebDriverException: Message: unknown error: failed to change window state to 'minimized', current state is 'normal'
    (Session info: chrome=108.0.5359.94)
    Stacktrace:
    #0 0x565158c992a3 <unknown>
    #1 0x565158a57f77 <unknown>
    #2 0x565158a36f83 <unknown>
    #3 0x565158a35c43 <unknown>
    #4 0x565158a3634f <unknown>
    #5 0x565158adf5c6 <unknown>
    #6 0x565158ab4b32 <unknown>
    #7 0x565158acc47c <unknown>
    #8 0x565158ab4903 <unknown>
    #9 0x565158a87ece <unknown>
    #10 0x565158a88fde <unknown>
    #11 0x565158ce963e <unknown>
    #12 0x565158cecb79 <unknown>
    #13 0x565158ccf89e <unknown>
    #14 0x565158ceda83 <unknown>
    #15 0x565158cc2505 <unknown>
    #16 0x565158d0eca8 <unknown>
    #17 0x565158d0ee36 <unknown>
    #18 0x565158d2a333 <unknown>
    #19 0x7f4ddfcedea7 start_thread
  3. I think it's caused by this line, and just delete and 'DISPLAY' not in os.environ https://github.com/terry3041/pyChatGPT/blob/8f53882a979e7524b3b0c6e097578f7207d61333/src/pyChatGPT/pyChatGPT.py#L61 image

then, i got the following error TimeoutException. I tried set longer time such as 35, it still didn't work:

(base) root@adalee:~# xvfb-run -a python chat_test.py 
Platform: Linux
Display: True
Headless: True
Starting virtual display
Starting browser
Ensuring Cloudflare challenge
Opening new tab
Getting authorization
Cloudflare challenge failed, retrying 1
Closing tab
Opening new tab
Getting authorization
Authorization is empty
Opening new tab
Opening login page
Checking if ChatGPT is at capacity
ChatGPT is at capacity, retrying
Checking if ChatGPT is at capacity
Clicking Log in button
Clicking Google button
Checking if Google remembers email
Google does not remember email
Entering email
Clicking Next
Entering password
Clicking Next
Checking if verification code is required
Checking if login was successful
Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 277, in __ensure_cf
    WebDriverWait(self.driver, 15).until(
  File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/support/wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
#0 0x559f460a62a3 <unknown>
#1 0x559f45e64f77 <unknown>
#2 0x559f45ea180c <unknown>
#3 0x559f45ea1a71 <unknown>
#4 0x559f45edb734 <unknown>
#5 0x559f45ec1b5d <unknown>
#6 0x559f45ed947c <unknown>
#7 0x559f45ec1903 <unknown>
#8 0x559f45e94ece <unknown>
#9 0x559f45e95fde <unknown>
#10 0x559f460f663e <unknown>
#11 0x559f460f9b79 <unknown>
#12 0x559f460dc89e <unknown>
#13 0x559f460faa83 <unknown>
#14 0x559f460cf505 <unknown>
#15 0x559f4611bca8 <unknown>
#16 0x559f4611be36 <unknown>
#17 0x559f46137333 <unknown>
#18 0x7f6227a5cea7 start_thread

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 248, in __login
    WebDriverWait(self.driver, 5).until(
  File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/support/wait.py", line 95, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: 
Stacktrace:
#0 0x559f460a62a3 <unknown>
#1 0x559f45e64f77 <unknown>
#2 0x559f45ea180c <unknown>
#3 0x559f45ea1a71 <unknown>
#4 0x559f45edb734 <unknown>
#5 0x559f45ec1b5d <unknown>
#6 0x559f45ed947c <unknown>
#7 0x559f45ec1903 <unknown>
#8 0x559f45e94ece <unknown>
#9 0x559f45e95fde <unknown>
#10 0x559f460f663e <unknown>
#11 0x559f460f9b79 <unknown>
#12 0x559f460dc89e <unknown>
#13 0x559f460faa83 <unknown>
#14 0x559f460cf505 <unknown>
#15 0x559f4611bca8 <unknown>
#16 0x559f4611be36 <unknown>
#17 0x559f46137333 <unknown>
#18 0x7f6227a5cea7 start_thread

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/root/chat_test.py", line 3, in <module>
    api = ChatGPT(auth_type='google', email='******@gmail.com', password='******.', verbose=True) # auth with google login
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 66, in __init__
    self.__init_browser()
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 131, in __init_browser
    self.__ensure_cf()
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 293, in __ensure_cf
    return self.__ensure_cf(retry + 1)
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 303, in __ensure_cf
    self.__login()
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 253, in __login
    raise ValueError('Login failed')
terry3041 commented 1 year ago

I dont know why the first error will happen. A Xvfb session should be started automatically in the library. Maybe that part is not working. The second error is because you cant minimize window on Xvfb. Try to delete all minimize windows function and see how it goes. Also, the google login is not very reliable. A screenshot should be available in your working directory. Check that.

Ulysses0817 commented 1 year ago

I dont know why the first error will happen. A Xvfb session should be started automatically in the library. Maybe that part is not working. The second error is because you cant minimize window on Xvfb. Try to delete all minimize windows function and see how it goes. Also, the google login is not very reliable. A screenshot should be available in your working directory. Check that.

Thanks! I check the screenshot, and find that google login failed verification. image

So I updated pkg to the latest and switched to session-token login, and failed again. I got the error ValueError: Invalid session token.

[0] Platform: Linux
[0] Display: True
[0] Headless: False
[init] Starting browser
[init] Restoring session token
[init] Ensuring Cloudflare cookies
[cf] Opening new tab
[cf] Getting authorization
[cf] Validating authorization
[cf] resp is not json
[cf] Authorization is empty
Traceback (most recent call last):
  File "/root/chat_test.py", line 5, in <module>
    api = ChatGPT(session_token=session_token, verbose=True)
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 68, in __init__
    self.__init_browser()
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 133, in __init_browser
    self.__ensure_cf()
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 322, in __ensure_cf
    raise ValueError('Invalid session token')
ValueError: Invalid session token

My session_token is copied from https://chat.openai.com/api/auth/session, image

terry3041 commented 1 year ago

Thanks! I check the screenshot, and find that google login failed verification.

Regarding this, you can recover your account by https://accounts.google.com/signin/recovery?ec=ar_t. And you will be able to login by google (at least on my end).

My session_token is copied from https://chat.openai.com/api/auth/session

accessToken in this page means Authorization in headers, not session_token. Try to copy from F12 > Application > Cookies tab.

Ulysses0817 commented 1 year ago

Rogar that! Thanks to your help, I've logged in successfully and sent messages!😁

However, api.reset_conversation() threw a new exception ElementClickInterceptedException.😭

[send_msg] Response is not an error
nice to meet you!

Resetting conversation
Traceback (most recent call last):
  File "/root/chat_test.py", line 10, in <module>
    api.reset_conversation()  # reset the conversation
  File "/root/miniconda3/lib/python3.9/site-packages/pyChatGPT/pyChatGPT.py", line 396, in reset_conversation
    self.driver.find_element(By.LINK_TEXT, 'New Thread').click()
  File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 93, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webelement.py", line 410, in _execute
    return self._parent.execute(command, params)
  File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 444, in execute
    self.error_handler.check_response(response)
  File "/root/miniconda3/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 249, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <a class="flex py-3 px-3 items-center gap-3 rounded-md hover:bg-gray-500/10 transition-colors duration-200 text-white cursor-pointer text-sm flex-shrink-0 border border-white/20">...</a> is not clickable at point (120, 31). Other element would receive the click: <div class="flex gap-2">...</div>
  (Session info: chrome=108.0.5359.94)
Stacktrace:
#0 0x55d77c44f2a3 <unknown>

Maybe this error is due to the change of UI? I log into chat https://chat.openai.com and find that the chatroom UI is different from what I saw servel days ago. image

terry3041 commented 1 year ago

However, api.reset_conversation() threw a new exception ElementClickInterceptedException.😭

image

Yeah. This prompt is blocking the view. I am looking into this issue.

terry3041 commented 1 year ago

Check v0.3.9.2

Ulysses0817 commented 1 year ago

It works now. Thanks very much~