terry3041 / pyChatGPT

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

(Intermittent) chatGPT Chrome Window freezes in the middle of a response #62

Open p-i- opened 1 year ago

p-i- commented 1 year ago

I'm on macOS, running version 0.3.9.5 out of a .ipynb Notebook in VSCode.

Upon api = ChatGPT(SESSION_TOKEN, verbose=True) the Chrome window appears, and the automations needed to get to the chatGPT prompt complete ok.

I can now either type something into the prompt in the Chrome window or resp = api.send_message('Hello, ChatGPT'). Both seem to have the same effect.

The problem is that sometimes the window hangs in the middle of a response. Here are 2 crashes.

Screenshot 2022-12-23 at 00 37 38 Screenshot 2022-12-23 at 00 40 20

The freeze isn't always on the first response. Usually the second or third.

Bringing up the Chrome DevTools console at the start of the session doesn't reveal why it's freezing.

teeppiphat commented 1 year ago

+1

Oaklight commented 1 year ago

This seems a mac specific issue. Our team has used this package for quite a while. All my Linux machines have been running perfectly fine. But two of my teammates' MacBooks are suffering from this issue.

terry3041 commented 1 year ago

image

Maybe its the problem of M1 Macbook? I cannot reproduce this on an Intel machine.

DevRoss commented 1 year ago

I'm using M1 Macbook, but it is ok for me.

AlphaMoury commented 1 year ago

Same here. Is it possible that is the specific driver version? Maybe implementing option.binary_location = 'path_to_another_browser' could solve the issue? I'm using Selenium on a daily basis and never got the crash on the M1 before...

AlphaMoury commented 1 year ago

Same here. Is it possible that is the specific driver version? Maybe implementing option.binary_location = 'path_to_another_browser' could solve the issue? I'm using Selenium on a daily basis and never got the crash on the M1 before...

Already tried with Brave Browser, but didn't work. Seems like is it something M1 specific...

Radenfar commented 1 year ago

Did anyone find a fix? Currently dealing with this issue also

Oaklight commented 1 year ago

Same here. Is it possible that is the specific driver version? Maybe implementing option.binary_location = 'path_to_another_browser' could solve the issue? I'm using Selenium on a daily basis and never got the crash on the M1 before...

Already tried with Brave Browser, but didn't work. Seems like is it something M1 specific...

Not sure if it's M1 specific, since my team mates are using the intel Mac

ppkantorski commented 1 year ago

I got the same issue. came up on both ARM and Intel via Rosetta 2. This issue has to do with Selenium / undetected_chromedriver. When python is killed via "pkill Python3", the website behaves normally again. Idk how to resolve this issue yet. This issue came about when I was developing my own ChatGPT api using a very similar method. I came here because i thought that it might have gotten sorted out, but I see others are also having this problem. I will do more tests on Linux to see if it is mac specific. But again, it has to do with the chromedriver/selenium on macOS from what i can tell. Website just freezes at one point, making interactions impossible. Resetting works, until a couple messages in when it all freezes again. This is making it unusable on macOS for me.

Oaklight commented 1 year ago

I got the same issue. came up on both ARM and Intel via Rosetta 2. This issue has to do with Selenium / undetected_chromedriver. When python is killed via "pkill Python3", the website behaves normally again. Idk how to resolve this issue yet. This issue came about when I was developing my own ChatGPT api using a very similar method. I came here because i thought that it might have gotten sorted out, but I see others are also having this problem. I will do more tests on Linux to see if it is mac specific. But again, it has to do with the chromedriver/selenium on macOS from what i can tell. Website just freezes at one point, making interactions impossible. Resetting works, until a couple messages in when it all freezes again. This is making it unusable on macOS for me.

seems a long-response issue +1, we tested with short ones, all good, but long ones freeze.

ppkantorski commented 1 year ago

I got the same issue. came up on both ARM and Intel via Rosetta 2. This issue has to do with Selenium / undetected_chromedriver. When python is killed via "pkill Python3", the website behaves normally again. Idk how to resolve this issue yet. This issue came about when I was developing my own ChatGPT api using a very similar method. I came here because i thought that it might have gotten sorted out, but I see others are also having this problem. I will do more tests on Linux to see if it is mac specific. But again, it has to do with the chromedriver/selenium on macOS from what i can tell. Website just freezes at one point, making interactions impossible. Resetting works, until a couple messages in when it all freezes again. This is making it unusable on macOS for me.

seems a long-response issue +1, we tested with short ones, all good, but long ones freeze.

even with short responses, after enough time, i get the issue personally. after a few messages in, the "..." part freezes on the screen. again, running a process kill of python resolves it making ChatGPT continue, but that closes selenium in the process. maybe we could kill then reattach selenium to the web browser? not sure how that would be done.

for me it froze when doing something like this.

WebDriverWait(self.driver, 10, 500).until(EC.presence_of_element_located((By.TAG_NAME, 'div'))).text

im calling this in a loop, displaying the information, then at one point the entire thing freezes, always on this call for me. I thought i could get around it with the WebDriverWait, but that didnt resolve it. somethings messed up with the driver, or maybe the openapi website itself can still detect it... or both? if someone figures out how to get around this it would be awesome. it doesnt seem like an easy fix. I did get around the issue partially by doing something like this.

def main():
    bot = ChatGPTBot()
    thread = threading.Thread(target=bot.run)
    thread.start()
    while True:
        if time.time() - bot.last_flag_update > 45: # flag has not been updated in 45 seconds
            print("Thread was unresponsive, forcefully killing...")
            kill_chrome()
            thread.join(timeout=0)
            print("Thread was killed, restarting...")
            time.sleep(3)
            kill_chrome()
            bot = ChatGPTBot()
            thread = threading.Thread(target=bot.run)
            thread.start()

    print("Thread finished execution successfully")

but this isnt that good of a solution. it is killing the chrome, then respawning. it will often not finish the response in the process of killing and reopening.

ppkantorski commented 1 year ago

So I finally found a solution. By changing the CSS theme to "hacker.css", I was able to prevent any timeouts on long responses. I did this using https://github.com/benf2004/ChatGPT-Prompt-Genius. Works great, now my custom API I wrote works flawlessly. My guess the issue is now when rapid calls are made to print text using the default CSS + Selenium (not sure if it is Apple silicon specific), eventually it glitches in some of the calls and hangs. With a simpler CSS style, it appears to not glitch out anymore.

reveching commented 1 year ago

So I finally found a solution. By changing the CSS theme to "hacker.css", I was able to prevent any timeouts on long responses. I did this using https://github.com/benf2004/ChatGPT-Prompt-Genius. Works great, now my custom API I wrote works flawlessly. My guess the issue is now when rapid calls are made to print text using the default CSS + Selenium (not sure if it is Apple silicon specific), eventually it glitches in some of the calls and hangs. With a simpler CSS style, it appears to not glitch out anymore.

This solved the problem. Thanks.

linquocte commented 1 year ago

i'm sorry, i don't know a lot about technical, how to use this 'hacker.css' with this chatgpt api? Help me plsss, thanks a lot. Does i need to add extension to selenium profile in code of pyChatGPT ?? How to fix this problem with 'hacker.css'. I need for my study purpose only, not cheating

So I finally found a solution. By changing the CSS theme to "hacker.css", I was able to prevent any timeouts on long responses. I did this using https://github.com/benf2004/ChatGPT-Prompt-Genius. Works great, now my custom API I wrote works flawlessly. My guess the issue is now when rapid calls are made to print text using the default CSS + Selenium (not sure if it is Apple silicon specific), eventually it glitches in some of the calls and hangs. With a simpler CSS style, it appears to not glitch out anymore.

MundoBoss commented 1 year ago

I cant find what Im suppose to do with the hacker.css to. I dont find any css call in the pyChatGPT where replace a css by this one. If its about inject css in javascript in the current selenium session i dont know to how to do it because I dont open by myself this chromedriver session

MundoBoss commented 1 year ago

I tried:

with open("hacker.css", "r") as file:
    css_content = file.read()

session_token = chatgptapi 
api = ChatGPT(session_token)

api.execute_script(f"var style = document.createElement('style'); style.innerHTML = '{css_content}'; document.head.appendChild(style);")

But look that I can't interact with pyChatGPT window with that...

MundoBoss commented 1 year ago

Also try install the chatGPT prompt genius chrome extension and call it at argument like:

api = ChatGPT(session_token, f'--load-extension={extension_path}')

But nothing appear, it work on my chrome but not on the chrome loaded by the script: no extension appear

MundoBoss commented 1 year ago

Ok so I found another workaround :

I make my question, get my answer, call the api.driver.quit() to close and then.. begin of the loop again and not the time to freeze and the api.driver.quit() make me sure of no getting 10000 chrome opens.

sheisol310 commented 1 year ago

hi, I am encountering the same issue. @MundoBoss i wonder how did you solve the issue with api.driver.quit()??

sheisol310 commented 1 year ago

So I finally found a solution. By changing the CSS theme to "hacker.css", I was able to prevent any timeouts on long responses. I did this using https://github.com/benf2004/ChatGPT-Prompt-Genius. Works great, now my custom API I wrote works flawlessly. My guess the issue is now when rapid calls are made to print text using the default CSS + Selenium (not sure if it is Apple silicon specific), eventually it glitches in some of the calls and hangs. With a simpler CSS style, it appears to not glitch out anymore.

@ppkantorski would you mind sharing in detail how to solve the issue with CSS work?

sheisol310 commented 1 year ago

I ended up installing Windows OS with VirtualBox on my MAC. The program is running well so far without any freezing issues. I would like someone to share the MAC version solution for this freezing issue in the future. Before that, I will compromise by using VirtualBox