diff --git a/README.md b/README.md index e99a467..7366d15 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,7 @@ An unofficial Python wrapper for OpenAI's ChatGPT API ## Features +- [x] GPT-4: OpenAI's latest and most powerful language model ✨. - [x] Cloudflare's anti-bot protection bypass using `undetected_chromedriver` - [x] OpenAI / Google / Microsoft login support (experimental) - [x] Captcha solvers support (2Captcha, PyPasser) @@ -53,6 +54,7 @@ api = ChatGPT(session_token, conversation_id='some-random-uuid') # specify conv api = ChatGPT(session_token, proxy='https://proxy.example.com:8080') # specify proxy api = ChatGPT(session_token, chrome_args=['--window-size=1920,768']) # specify chrome args api = ChatGPT(session_token, moderation=False) # disable moderation +api = ChatGPT(session_token, gpt_4=True) # GPT-4 ✨ (Only available for paid plans) api = ChatGPT(session_token, verbose=True) # verbose mode (print debug messages) # auth with google login @@ -84,6 +86,7 @@ print(resp['message']) api.reset_conversation() # reset the conversation api.clear_conversations() # clear all conversations api.refresh_chat_page() # refresh the chat page +api.close_chat_page() # close the chat page ``` ## Frequently Asked Questions diff --git a/src/pyChatGPT/pyChatGPT.py b/src/pyChatGPT/pyChatGPT.py index aa0b605..796e0de 100644 --- a/src/pyChatGPT/pyChatGPT.py +++ b/src/pyChatGPT/pyChatGPT.py @@ -39,7 +39,7 @@ '//div[substring(@class, string-length(@class) - string-length("text-sm") + 1) = "text-sm"]//a', ) -chatgpt_chat_url = 'https://chat.openai.com/chat' +chatgpt_chat_url = 'https://chat.openai.com' class ChatGPT: @@ -60,6 +60,7 @@ def __init__( proxy: str = None, chrome_args: list = [], moderation: bool = True, + gpt_4: bool = False, verbose: bool = False, ): ''' @@ -90,6 +91,7 @@ def __init__( self.__proxy = proxy self.__chrome_args = chrome_args self.__moderation = moderation + self.__gpt_4 = gpt_4 if not self.__session_token and ( not self.__email or not self.__password or not self.__auth_type @@ -223,7 +225,15 @@ def __init_browser(self) -> None: self.__ensure_cf() self.logger.debug('Opening chat page...') - self.driver.get(f'{chatgpt_chat_url}/{self.__conversation_id}') + + #if conversation_id is empty then it will open a new chat + if not self.__conversation_id: + if self.__gpt_4: + self.driver.get(f'{chatgpt_chat_url}/?model=gpt-4') + else: + self.driver.get(f'{chatgpt_chat_url}') + else: + self.driver.get(f'{chatgpt_chat_url}/{self.__conversation_id}') self.__check_blocking_elements() self.__is_active = True @@ -433,6 +443,7 @@ def send_message(self, message: str, stream: bool = False) -> dict: WebDriverWait(self.driver, 120).until_not( EC.presence_of_element_located(chatgpt_streaming) ) + self.close_chat_research_popup() self.logger.debug('Getting response...') responses = self.driver.find_elements(*chatgpt_big_response) @@ -509,3 +520,24 @@ def refresh_chat_page(self) -> None: self.driver.get(chatgpt_chat_url) self.__check_capacity(chatgpt_chat_url) self.__check_blocking_elements() + + def close_chat_page(self) -> None: + ''' + Close the chat page + ''' + if not self.driver.current_url.startswith(chatgpt_chat_url): + return self.logger.debug('Current URL is not chat page, skipping close') + + self.driver.close() + + def close_chat_research_popup(self) -> None: + ''' + Close the ChatGPT Research popup + ''' + self.driver.execute_script(""" + var elements = document.querySelectorAll('[role="dialog"]'); + console.log(elements); + for (var i = 0; i < elements.length; i++) { + elements[i].style.display = 'none'; + } + """) \ No newline at end of file