Skip to main content

Unofficial reverse-engineered ChatGPT API in Python.

Project description

Reverse Engineered ChatGPT API

Use OpenAI ChatGPT in your Python code without an API key

Stargazers Forks Discussions Issues MIT License

Report Bug | Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

This project can be used to integrate OpenAI's ChatGPT services into your python code. You can use this project to prompt ChatGPT for responses directly from python, without using an official API key.

This can be useful if you want to use ChatGPT API without a ChatGPT Plus account.

Inspiration

ChatGPT has an official API which can be used to interface your Python code to it, but it needs to be used with an API key. This API key can only be obtained if you have a ChatGPT Plus account, which requires $20/month (as of 05/11/2023). But you can use ChatGPT for free, using the ChatGPT web interface. This project aims to interface your code to ChatGPT web version so you can use ChatGPT in your Python code without using an API key.

How it works

ChatGPT web interface's requests have been reverse engineered, and directly integrated into Python requests. Hence, any requests made using this script is a simulated as a request made by a user directly on the website. Hence, it is free and needs no API key.

Built Using

  • Python

Getting Started

Prerequisites

  • Python >= 3.9

Installation

pip install re-gpt

Usage

Basic example

from re_gpt import SyncChatGPT

session_token = "__Secure-next-auth.session-token here"
conversation_id = None # conversation ID here


with SyncChatGPT(session_token=session_token) as chatgpt:
    prompt = input("Enter your prompt: ")

    if conversation_id:
        conversation = chatgpt.get_conversation(conversation_id)
    else:
        conversation = chatgpt.create_new_conversation()

    for message in conversation.chat(prompt):
        print(message["content"], flush=True, end="")

Basic async example

import asyncio
import sys

from re_gpt import AsyncChatGPT

session_token = "__Secure-next-auth.session-token here"
conversation_id = conversation_id = None # conversation ID here

if sys.version_info >= (3, 8) and sys.platform.lower().startswith("win"):
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())


async def main():
    async with AsyncChatGPT(session_token=session_token) as chatgpt:
        prompt = input("Enter your prompt: ")

        if conversation_id:
            conversation = chatgpt.get_conversation(conversation_id)
        else:
            conversation = chatgpt.create_new_conversation()

        async for message in conversation.chat(prompt):
            print(message["content"], flush=True, end="")


if __name__ == "__main__":
    asyncio.run(main())

More Examples

For a more complex example, check out the examples folder in the repository.

Obtaining The Session Token

  1. Go to https://chat.openai.com/chat and log in or sign up.
  2. Open the developer tools in your browser.
  3. Go to the Application tab and open the Cookies section.
  4. Copy the value for __Secure-next-auth.session-token and save it.

TODO

  • Add more examples
  • Add better error handling
  • Implement a function to retrieve all ChatGPT chats
  • Improve documentation

Contributing

Contributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the Apache License 2.0. See LICENSE for more information.

Contact/Bug report

Zai-Kun - Discord Server

Repo Link: https://github.com/Zai-Kun/reverse-engineered-chatgpt

Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

re_gpt-2.9.8.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

re_gpt-2.9.8-py3-none-any.whl (17.6 kB view details)

Uploaded Python 3

File details

Details for the file re_gpt-2.9.8.tar.gz.

File metadata

  • Download URL: re_gpt-2.9.8.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for re_gpt-2.9.8.tar.gz
Algorithm Hash digest
SHA256 84cdfa85184b2646f44b6c7a4b78f0c2354f57c5e906204e6aca12e6c841a708
MD5 278fc1a22c56ab5f81f512a9865e1bdf
BLAKE2b-256 31ff794e30a221d51abe11d136bb0852db8b02c230846f6072a49e70445c2c02

See more details on using hashes here.

File details

Details for the file re_gpt-2.9.8-py3-none-any.whl.

File metadata

  • Download URL: re_gpt-2.9.8-py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for re_gpt-2.9.8-py3-none-any.whl
Algorithm Hash digest
SHA256 a9ae22a94d4120ee76a2422e99fc31a256ee929b75ef21fbefd21e8bb1cfe789
MD5 43c6085a4e3d3158663849508909106b
BLAKE2b-256 36497ef25edb7b9bb6893d12d27ebae69855c242a1bc73747c4ba8e11a2ec20d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page