granin / pyChatGPT

An unofficial Python wrapper for OpenAI's ChatGPT API

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

pyChatGPT

PyPi License PyPi

An unofficial Python wrapper for OpenAI's ChatGPT API

Features

  • Bypass Cloudflare's anti-bot protection using undetected_chromedriver
  • Captcha solver when auth with login credentials (experimental)
  • Support headless machines
  • Proxy support (only without basic auth)
  • Google login support (experimental)

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 before using this wrapper..

Installation

pip install -U pyChatGPT

Usage

Obtaining session_token

  1. Go to https://chat.openai.com/chat and open the developer tools by F12.
  2. Find the __Secure-next-auth.session-token cookie in Application > Storage > Cookies > https://chat.openai.com.
  3. Copy the value in the Cookie Value field.

image

Interactive mode

python -m pyChatGPT

Import as a module

from pyChatGPT import ChatGPT

session_token = 'abc123'  # `__Secure-next-auth.session-token` cookie from https://chat.openai.com/chat
api = ChatGPT(session_token)  # auth with session token
api = ChatGPT(session_token, conversation_id='some-random-uuid')  # specify conversation id
api = ChatGPT(session_token, proxy='http://proxy.example.com:8080')  # specify proxy
api = ChatGPT(auth_type='google', email='example@gmail.com', password='password')  # auth with google login
api = ChatGPT(session_token, moderation=False)  # disable moderation
api = ChatGPT(session_token, window_size=(1024, 768))  # specify window size
api = ChatGPT(session_token, verbose=True)  # verbose mode (print debug messages)

# login with openai and solve simple reCAPTCHA automatically, but you should reCAPTCHA by yourself,
# it does not work on server without GUI.
api = ChatGPT(auth_type='openai', email='example@xxx.com', password='password', verbose=True)
# login with openai and solve simple reCAPTCHA automatically, while pass reCAPTCHA enterprise with 2captcha API,
# it works on server without GUI.
api = ChatGPT(auth_type='openai', email='example@xxx.com', password='password',
               twocaptcha_apikey='2captcha_apikey',
               verbose=True)
# reuse cookies generated by successful login before login,
# if `login_cookies_path` does not exist, it will process logining  with `auth_type`, and save cookies to `login_cookies_path`
# only works when `auth_type` is `openai` or `google`
api = ChatGPT(auth_type='openai', email='example@xxx.com', password='password',
               login_cookies_path='your_cookies_path',
               verbose=True)

resp = api.send_message('Hello, world!')
print(resp['message'])

api.reset_conversation()  # reset the conversation
api.clear_conversations()  # clear all conversations
api.refresh_chat_page()  # refresh the chat page

Frequently Asked Questions

How do I get it to work on headless linux server?

# install chromium & X virtual framebuffer
sudo apt install chromium-browser xvfb

# start your script
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.

# install dependencies
!apt install chromium-browser xvfb
!pip install -U selenium_profiles pyChatGPT

# install chromedriver
from selenium_profiles.utils.installer import install_chromedriver
install_chromedriver()
# start your script as normal
!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.

About

An unofficial Python wrapper for OpenAI's ChatGPT API

License:GNU General Public License v3.0


Languages

Language:Python 100.0%