pyChatGPTH
An unofficial Python wrapper for OpenAI's ChatGPT API
Features
Getting Started
Since version 0.3.0, this library is using only the undetected_chromedriver
to bypass Cloudflare's anti-bot protection. requests
module is no longer used due to the complexity of the protection. Please make sure you have Google Chrome / Chromium before using this wrapper.
Installation
pip install -U pyChatGPTH
Usage
Obtaining session_token
- Go to https://chat.openai.com/chat and open the developer tools by
F12
. - Find the
__Secure-next-auth.session-token
cookie in Application
> Storage
> Cookies
> https://chat.openai.com
. - Copy the value in the
Cookie Value
field.
Interactive mode
python3 -m pyChatGPT
Import as a module
from pyChatGPT import ChatGPT
session_token = 'abc123'
api = ChatGPT(session_token)
api = ChatGPT(session_token, conversation_id='some-random-uuid')
api = ChatGPT(session_token, proxy='https://proxy.example.com:8080')
api = ChatGPT(session_token, chrome_args=['--window-size=1920,768'])
api = ChatGPT(session_token, moderation=False)
api = ChatGPT(session_token, verbose=True)
api = ChatGPT(auth_type='google', email='example@gmail.com', password='password')
api = ChatGPT(auth_type='microsoft', email='example@gmail.com', password='password')
api = ChatGPT(auth_type='openai', email='example@gmail.com', password='password')
api = ChatGPT(
auth_type='openai', captcha_solver=None,
email='example@gmail.com', password='password'
)
api = ChatGPT(
auth_type='openai', captcha_solver='2captcha', solver_apikey='abc',
email='example@gmail.com', password='password'
)
api = ChatGPT(auth_type='openai', email='example@xxx.com', password='password',
login_cookies_path='your_cookies_path',
)
resp = api.send_message('Hello, world!')
print(resp['message'])
api.reset_conversation()
api.clear_conversations()
api.refresh_chat_page()
Frequently Asked Questions
How do I get it to work on headless linux server?
sudo apt install chromium-browser xvfb
python3 your_script.py
How do I get it to work on Google Colab?
It is normal for the seession to be crashed when installing dependencies. Just ignore the error and run your script.
!apt install chromium-browser xvfb
!pip install -U selenium_profiles pyChatGPT
from selenium_profiles.utils.installer import install_chromedriver
install_chromedriver()
!python3 -m pyChatGPT
Insipration
This project is inspired by
Disclaimer
This project is not affiliated with OpenAI in any way. Use at your own risk. I am not responsible for any damage caused by this project. Please read the OpenAI Terms of Service before using this project.
License
This project is licensed under the GPLv3 License - see the LICENSE file for details.