# chatgpt package

## Subpackages

- chatgpt.cli package
  - Submodules
  - chatgpt.cli.custom_rich module
  - Module contents

## Submodules

## chatgpt.authentication module

class chatgpt.authentication.OpenAIAuthentication(session: HTTPTLSSession)

  Bases: `object`

  get_session()

    Get the actual gpt session.

  login(username, password)

    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)

  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)

    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()

    Clean the current authentication information

  get_session()

    Get chatgpt actual session

  load_config(config_path: str = 'config.json')

    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)

    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()

    Reset the conversation

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

    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)

    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)

  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)

  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)

  Bases: `HTTPSessionBase`

  property cookies
  property headers
  request(*args, stream=None, **kwargs)

class chatgpt.sessions.HTTPSessionBase(timeout=300, proxy=None, cookies={})

  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={})

  Bases: `HTTPSessionBase`

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

## chatgpt.utils module

chatgpt.utils.get_utc_now_datetime()

chatgpt.utils.random_sleep_time(min_second: float, max_second: float)

  Random sleep time

  Parameters

    - min_second (float) – Minimun range to generate.

    - max_second (float) – Maximun range to generate.

## Module contents