chatgpt package

Subpackages

Submodules

chatgpt.authentication module

class chatgpt.authentication.OpenAIAuthentication(session: HTTPTLSSession)[source]

Bases: object

get_session()[source]

Get the actual gpt session.

login(username, password)[source]

Get the access token for chatgpt

Parameters
  • username (str) – Email for chatgpt

  • password (str) – Password for chatgpt

chatgpt.chatgpt module

class chatgpt.chatgpt.Conversation(config_path: str = None, access_token: str = None, access_token_seconds_to_expire: int = None, email: str = None, password: str = None, conversation_id: str = None, parent_message_id: str = None, proxy: str = None, timeout: int = None, cache_file: bool = True, cache_file_path: str = None)[source]

Bases: object

DEFAULT_ACCESS_TOKEN_SECONDS_TO_EXPIRE = 1800
DEFAULT_CACHE_PATH = '.chatgpt'
DEFAULT_CONFIG_PATH = 'config.json'
DEFAULT_ENVIRONMENT_CHATGPT_HOME = 'CHATGPT_HOME'
DEFAULT_MODEL_NAME = 'text-davinci-002-render'
chat(message: List[str], retry_on_401: bool = True, direct_response: bool = False, stream=False)[source]

Send a message and wait for the server to fully answer to return the message.

Parameters
  • message (List[str]) – Message or list of messages to send to the server.

  • retry_on_401 (bool, optional) – Retry login if it fails. Defaults to True.

  • direct_response (bool, optional) – Return the response of request instead of the parsed message. Defaults to False.

  • stream (bool, optional) – Execute chat with stream. Note that you will be better off by using stream since it does the processing for you. Defaults to False.

clean_auth()[source]

Clean the current authentication information

get_session()[source]

Get chatgpt actual session

load_config(config_path: str = 'config.json')[source]

Load Conversation attributes by reading from a file

Parameters

config_path (str, optional) – Name of the file from where to read attributes from. Defaults to config.json.

login(email, password)[source]

Login to the openai and return the token

Parameters
  • email (str) – Email to login into openai chatgpt

  • password (str) – Password to login into openai chatgpt

reset()[source]

Reset the conversation

stream(message: List[str], retry_on_401: bool = True, only_new_characters: bool = True)[source]

Generator that allows you to retrieve messages as you are receiving them.

Parameters
  • message (List[str]) – Message or list of messages to send to the server.

  • retry_on_401 (bool, optional) – Retry login if it fails. Defaults to True.

Yields

str – The text of the message as you are receiving it.

write_cache(cache_path: str = None)[source]

Write the conversation attributes inside a file.

Parameters

cache_path (str, optional) – Path where to write the data for caching purposes. Defaults to .chatgpt.

chatgpt.errors module

exception chatgpt.errors.ChatgptError(message, code=ChatgptErrorCodes.UNKNOWN_ERROR)[source]

Bases: Exception

Base class for exceptions raised by the Chatgpt API.

class chatgpt.errors.ChatgptErrorCodes(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]

Bases: Enum

Error codes returned by the API.

CHATGPT_API_ERROR = 'chat_gpt_api_error'
CONFIG_FILE_ERROR = 'config_file_error'
CONNECTION_ERROR = 'connection_error'
INVALID_ACCESS_TOKEN = 'invalid_access_token'
LOGIN_ERROR = 'login_error'
TIMEOUT_ERROR = 'timeout_error'
UNKNOWN_ERROR = 'unknown_error'

chatgpt.sessions module

class chatgpt.sessions.HTTPSession(timeout=None, proxy=None, cookies=None, stream=True)[source]

Bases: HTTPSessionBase

property cookies
property headers
request(*args, stream=None, **kwargs)[source]
class chatgpt.sessions.HTTPSessionBase(timeout=300, proxy=None, cookies={})[source]

Bases: object

DEFAULT_HEADERS = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Accept-Language': 'en-US,en;q=0.5', 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36', 'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="107", "Google Chrome";v="107"', 'sec-ch-ua-mobile': '?0', 'sec-ch-ua-platform': '"Linux"'}
DEFAULT_TIMEOUT = 300
class chatgpt.sessions.HTTPTLSSession(timeout=None, proxy=None, cookies=None, headers={})[source]

Bases: HTTPSessionBase

property cookies
request(*args, headers={}, **kwargs)[source]

chatgpt.utils module

chatgpt.utils.get_utc_now_datetime()[source]
chatgpt.utils.random_sleep_time(min_second: float, max_second: float)[source]

Random sleep time

Parameters
  • min_second (float) – Minimun range to generate.

  • max_second (float) – Maximun range to generate.

Module contents