Skip to content
This repository was archived by the owner on Dec 7, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
36 changes: 34 additions & 2 deletions src/pyChatGPT/pyChatGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -60,6 +60,7 @@ def __init__(
proxy: str = None,
chrome_args: list = [],
moderation: bool = True,
gpt_4: bool = False,
verbose: bool = False,
):
'''
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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';
}
""")