Code Monkey home page Code Monkey logo

chatgpt-api's Introduction

Status

Currently chatgpt-api is not working, since OpenAI started to use Cloudflare. Also, it changes almost every day. See mbroton#18

๐Ÿ’ป ChatGPT API

Unofficial API client and CLI for ChatGPT.

PyPI License Coverage


Short Demo GIF


This project is based on httpx (uses only HTTP) and uses Typer (with Rich) for CLI, so responses are looking good (markdown is supported). Also, It has almost 100% code coverage unlike other ChatGPT packages.

Long Demo GIF

Installation

From Pypi

pip install chatgpt-api

Source code

pip install -r requirements.txt && pip install .

Usage

As a Command Line Interface

Setup

Required to authenticate. In this step you have to provide a path to the file containing the session key. A simple txt file with the key only is enough.

chatgpt setup

Tip: Use a file named .session_key in chatgpt-api top directory. It will be ignored by git - see .gitignore.

The key will be saved to

Path.home() / ".chatgpt_api" / "key.txt"

Session messages are logged to

Path.home() / ".chatgpt_api" / "logs"

Start chatting

chatgpt start

As an API

ChatGPT class inherits from httpx.Client

Recommended usage:

from chatgpt.api import ChatGPT

with ChatGPT(session_token="your-session-token") as chat:
    response = chat.send_message("Hello!")
    print(response.content)

Without context manager you have to explicitly authenticate:

from chatgpt.api import ChatGPT

chat = ChatGPT(session_token="your-session-token")
chat.authenticate()
response = chat.send_message("Hello!")
print(response.content)
chat.close()

How to acquire session key?

After you log in to ChatGPT in your browser, get value of __Secure-next-auth.session-token cookie. In this project, this is named as a "session key".

Chrome instruction

  1. Open ChromeDevTools (F12).
  2. Click on "Application" tab.
  3. Click on "Cookies", on the left bar.
  4. Copy the value of __Secure-next-auth.session-token:

Cookie value example

Now, you can use it in CLI or directly from Python code.

License

Distributed under the MIT License. See LICENSE for more information.

Disclaimer

This is a personal project, not affiliated in any way with OpenAI. If you have any objections, contact @mbroton.

chatgpt-api's People

Contributors

mbroton avatar oblackato avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.