terry3041 / pyChatGPT

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

Headless on Windows #98

Closed deezed420 closed 1 year ago

deezed420 commented 1 year ago

Is there a way to not show the browser window in Windows? When I use the --headless chrome argument it just gives this long error message:

Traceback (most recent call last):
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyChatGPT\pyChatGPT.py", line 242, in __ensure_cf
    WebDriverWait(self.driver, 10).until_not(
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 118, in until_not
    raise TimeoutException(message)
selenium.common.exceptions.TimeoutException: Message:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyChatGPT\pyChatGPT.py", line 242, in __ensure_cf
    WebDriverWait(self.driver, 10).until_not(
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 118, in until_not
    raise TimeoutException(message)
selenium.common.exceptions.TimeoutException: Message:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyChatGPT\pyChatGPT.py", line 242, in __ensure_cf
    WebDriverWait(self.driver, 10).until_not(
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 118, in until_not
    raise TimeoutException(message)
selenium.common.exceptions.TimeoutException: Message:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyChatGPT\pyChatGPT.py", line 242, in __ensure_cf
    WebDriverWait(self.driver, 10).until_not(
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\support\wait.py", line 118, in until_not
    raise TimeoutException(message)
selenium.common.exceptions.TimeoutException: Message:

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\Users\Ender\Dev\ChatGPTAPITEST\main.py", line 5, in <module>
    api = ChatGPT(
          ^^^^^^^^
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyChatGPT\pyChatGPT.py", line 125, in __init__
    self.__init_browser()
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyChatGPT\pyChatGPT.py", line 221, in __init_browser
    self.__ensure_cf()
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyChatGPT\pyChatGPT.py", line 252, in __ensure_cf
  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\pyChatGPT\pyChatGPT.py", line 134, in __del__
    self.driver.quit()  File "C:\Users\Ender\AppData\Local\Programs\Python\Python311\Lib\site-packages\undetected_chromedriver\__init__.py", line 732, in quit
    time.sleep(0.1)
OSError: [WinError 6] The handle is invalid

and here is the code i'm trying to do:

from pyChatGPT import ChatGPT # Import ChatGPT API

# Start the api
sessiontoken = # I can't say that
api = ChatGPT(
   sessiontoken,
   chrome_args=[
      "--headless"
   ]
)

# Send query to api
response = api.send_message("What is 2 + 2?")
print(response["message"])

Is there any other way to run the api without the window or is there something that i'm doing wrong?

terry3041 commented 1 year ago

You can't run this headless.