Skip to content

API for interacting with ChatGPT using Python and from Shell.

License

Notifications You must be signed in to change notification settings

lij444/chatgpt-wrapper

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ChatGPT CLI and Python Wrapper

ChatGPT Wrapper is an open-source unofficial Python API and CLI that lets you interact with ChatGPT.

Highlights

🤖 Programmable ChatGPT. The ChatGPT Wrapper lets you use the powerful ChatGPT bot in your Python scripts or on the command line, making it easy to leverage its functionality into your projects.

💬 Runs in Shell. You can call and interact with ChatGPT in the terminal

How it works

Run an interactive CLI in the terminal:

kod

Or just get a quick response for one question:

kod(1)

See below for details on using ChatGPT as an API from Python.

Requirements

To use this repository, you need setuptools installed. You can install it using pip install setuptools.

Installation

  1. Install the latest version of this software directly from github with pip:
pip install git+https://github.com/mmabrouk/chatgpt-wrapper
  1. Install a browser in playwright (if you haven't already). The program will use firefox by default.
playwright install firefox
  1. Start up the program in install mode. This opens up a browser window. Log in to ChatGPT in the browser window, then stop the program.
chatgpt install
  1. Restart the program without the install parameter to begin using it.

Note: For more help, this Blog post provides a visual step-by-step guide for installing this library.

Usage

Shell

In addition to directly prompting ChatGPT, The shell provides the following commands:

  • !new allows to start a new conversation
  • !nav lets you navigate to a past point in the conversation. Example: !nav 2
  • !stream toggles between streaming mode (streams the raw response from ChatGPT) and markdown rendering (which cannot stream).
  • !read begins reading multi-line input. Usefull when copy-pasting
  • !file sends a prompt read from the named file. Example: file myprompt.txt
  • !log enables logging to a file. Example: !log mylog.txt to enable, or !log to disable.
  • !context lets you load old contexts from the log (previous sessions). It takes one parameter; a context string from logs.
  • !session refreshes your session information. This can resolve errors under certain scenarios.

Python

To use the ChatGPT class as an API for talking to ChatGPT, create an instance of the class and use the ask method to send a message to OpenAI and receive the response. For example:

from chatgpt_wrapper import ChatGPT

bot = ChatGPT()
response = bot.ask("Hello, world!")
print(response)  # prints the response from chatGPT

The say method takes a string argument representing the message to send to ChatGPT, and returns a string representing the response received from ChatGPT.

You may also stream the response as it comes in from ChatGPT in chunks using the ask_stream generator.

Projects built with chatgpt-wrapper

Contributing

We welcome contributions to ChatGPT Wrapper! If you have an idea for a new feature or have found a bug, please open an issue on the GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Updates

  • 18/01/2022: v0.3.8 - Commands now are run only using !. For instance to enable read mode (for copy-paste and long prompts) you need to write now !read instead of read. This is to avoid conflicts with the chatgpt prompts. Fixed timeout issue.
  • 17/01/2022: v0.3.7 - Added timeout to ask method to prevent hanging. Fixed return to terminal breakdown. Streaming output now is activated by default.

Acknowledgments

Star History

Star History Chart

About

API for interacting with ChatGPT using Python and from Shell.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.7%
  • Shell 0.3%