Use ChatGPT From The Command Line With This Wrapper

chatgpt wrapper linux command line

ChatGPT Wrapper is an unofficial open source command-line interface and Python API for interacting with ChatGPT.

ChatGPT is a chatbot lunched by OpenAI in November 2022. It can mimic a human conversation, write and debug computer programs, write poetry and lyrics, answer test questions and much more.

ChatGPT is free to use in this initial research preview, but there are plans to monetize it. It's also worth noting that there are some limitations – for instance, ChatGPT is not connected to the Internet, and it has limited knowledge of world and events after 2021, so it may produce inaccurate responses. 

As for privacy, according to the ChatGPT FAQ, your conversations may be reviewed by AI trainers to improve the systems, so keep this in mind when using it and don't share any sensitive information; your data can be deleted though, by deleting your OpenAI account.

Using this Python wrapper, you'll be able to interact with ChatGPT by using a terminal or integrate it into your Python script. It features the ability to navigate to past points in the conversation, a logging option and the ability to restore any context that's been logged, as well as an option to read prompts from files, and more.

It's worth noting that right now, ChatGPT Wrapper waits until the full response is fully generated by ChatGPT until it's displayed in the terminal, and that can take a while for long responses, which might appear that this isn't working, when it actually does. Hopefully some indication that the response is being generated will be shown in the future.

To be able to use the ChatGPT bot, you'll need to log in to your OpenAI account (you can create one for free here). For authentication, ChatGTP Wrapper makes use of a browser installed via Playwright (a framework for web testing and automation; the developer plans on removing the reliance on Playwright), where you'll need to log in to your ChatGPT account. You'll only need to do this once – you'll remain authenticated for subsequent runs.

You might also like: How To Run A Command Or Script As Root On Startup / Boot Using systemd or A Cron Job

Supported commands:

  • !new - start a new conversation
  • !ask - asks ChatGPT a question
  • !context - load old contexts from the log
  • !file - send a prompt read from a file
  • !log - enable logging to a file
  • !nav - navigate to past points in the conversation
  • !read - reads multi-line input
  • !session - refreshes the session information
  • !stream - toggle between streaming mode (streams the raw response from ChatGPT) and markdown rendering (which cannot stream)

For future versions, there are plans to implement an async mode, improve the error messaging, and remove the reliance on Playwright.


How to install ChatGPT Wrapper


To use ChatGPT Wrapper, you'll need to have some packages installed: Python3 Setuptools, Python PIP and Git.

Install the dependencies:

  • Debian / Ubuntu / Linux Mint / Pop!_OS / etc.:
sudo apt install python3-setuptools python3-pip git

  • Fedora:
sudo dnf install python3-setuptools python3-pip git

  • Arch Linux / Manjaro:
sudo pacman -S python-setuptools python-pip git

  • openSUSE:
sudo zypper install python3-setuptools python3-pip git

Now we can proceed with the ChatGPT Wrapper installation. Using pip, we'll install it from GitHub (this is a single line command, so triple click it to select the whole line):

python3 -m pip install --user git+https://github.com/mmabrouk/chatgpt-wrapper

This installs ChatGPT Wrapper in ~/.local/bin. In case this directory is not in your PATH, add it (add export PATH="$PATH:$HOME/.local/bin to your ~/.bashrc file, then open a new terminal).

Next, use Playwright (installed in ~/.local/bin when you've installed ChatGPT Wrapper) to install Firefox web browser, which is going to be used for authentication:

playwright install firefox

The last step is to launch ChatGPT Wrapper with the installation command, which opens a new browser window where you must log in to your OpenAI account (which you can create for free):

chatgpt install

After logging in, close the browser window, and exit the ChatGPT wrapper that's running in your terminal by typing exit. From now on, you can launch ChatGPT Wrapper by opening a terminal and typing: chatgpt.

Type ? in the ChatGPT Wrapper command line prompt to see a list of available commands. Now try out ChatGPT from the command line, by starting a new conversation (type !new in the ChatGPT Wrapper prompt), then type your query for ChatGPT.

You might also be interested in: How To Find All Files Containing Specific Text On Linux From The Command Line

via r/unixporn