As Linux users, we primarily rely on the Linux terminal and prefer to use GUI alternatives to the CLI like Ranger (a CLI based file manager) or w3m (a CLI based web browser).
In the ongoing buzz of LLM models like ChatGPT, accessing them via a web browser is required. However, a GitHub user name 0xacx created a project called chatGPT-shell-cli to make it easier to access ChatGPT prompts via the CLI.
In this article, you will learn how to access the ChatGPT Prompt in your Linux terminal.
Tutorial Details
Description | Access ChatGPT Prompt in your Linux Terminal |
Difficulty Level | Moderate |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | Free or Paid OpenAI Account |
Internet Required | Yes |
What is the ChatGPT-Shell-Cli?
The chatGPT-shell-cli is a lightweight shell script that uses the ChatGPT model βgpt-3.5-turboβ and all other OpenAI models with the βcompletionsβ endpoint and the βimages/generationsβ endpoint for generating images.
In laymanβs terms, you can access the ChatGPT prompt the same way you do in your web browser. However, you have to keep in mind that this script uses an OpenAI API key, so to use it, you must have an OpenAI free or premium account.
Features
In terms of features, you will have almost all the elements that ChatGPT prompt offers, with a few additional ones listed below.
- Communicate with ChatGPT via terminal.
- Generate images from the text prompt.
- View your chat history.
- List all OpenAI models.
- Set the OpenAI request parameters.
- Pass the input prompt with a pipe, as a script parameter, or in normal chat mode.
With that, letβs check out the installation process.
Installation
There are two ways to install this chatGPT-shell-cli: either using the curl command or manually placing the script file on your system and giving the path to environment variables. Arch users can directly install it from their default package manager.
Now, before we start the installation process, letβs create an OpenAI account. If you already have one, then visit OpenAI and click on the βCreate a new secret keyβ button.
Now, it will ask for a name for the given secret key (optional).
Lastly, it will show you the secret key (only the last time); simply copy and save it somewhere else for later use.
Now, letβs check out all the steps to install this script on your Linux system.
Debian, Ubuntu, or Pop!_OS
Execute the following commands and provide your OpenAI API key when asked (got an error!).
$ sudo apt install curl jq -y
$ curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/install.sh | sudo -E bash
And then you need to set your OpenAI API key to use this script.
$ echo "export OPENAI_KEY=[You-OPENAI-API-KEY]" >> ~/.bashrc
$ source ~/.bashrc
Red Hat, Fedora, or AlmaLinux
Run the aforementioned commands, and when prompted, enter your OpenAI API key (got an error!).
$ sudo dnf install curl jq -y
$ curl -sS https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/install.sh | sudo -E bash
And then you need to set your OpenAI API key to use this script.
$ echo "export OPENAI_KEY=[You-OPENAI-API-KEY]" >> ~/.bashrc
$ source ~/.bashrc
Arch, Manjaro, or EndeavourOS
Directly issue the Yay command, and it will install the script for you.
$ yay -s chatgpt-shell-cli
And then you need to set your OpenAI API key to use this script.
$ echo "export OPENAI_KEY=[You-OPENAI-API-KEY]" >> ~/.bashrc
$ source ~/.bashrc
Manual Installation
While following the installation instructions for Debian and Red Hat based distributions, I encountered the following error.
However, if you encounter the same error as me, you can try following the steps below.
1. Install the wget package based on your Linux distribution.
$ sudo apt install wget -y #For Debian, Ubuntu, or Pop!_OS
$ sudo dnf install wget -y #For Red Hat, Fedora, or AlmaLinux
2. Create a scripts directory in your home directory and download the shell script in that directory using the wget command.
$ mkdir ~/scripts
$ cd ~/scripts
$ wget https://raw.githubusercontent.com/0xacx/chatGPT-shell-cli/main/chatgpt.sh
3. Add the βscripts
β directory path to your $PATH
environment variable.
$ export PATH=$PATH:~/scripts/
4. Add the OpenAI API key to your shell profile by executing the following command:
$ echo "export OPENAI_KEY=[You-OPENAI-API-KEY]" >> ~/.bashrc
$ source ~/.bashrc
Tada!!! You have successfully installed the βchatGPT-shell-cli
β script on your Linux system.
Accessing the ChatGPT Prompt in your Linux Terminal
Now, before doing anything else, execute the following command to ensure that everything is working correctly and we are getting the ChatGPT prompt in our terminal.
$ chatgpt
Output:
As you can see from the above picture, in my system itβs perfectly installed. Note that when you write something to the prompt in a free account, you might get the following error:
This is the limitation of the free account; we cannot do anything to resolve this except wait or buy the premium membership.
However, if you have access, you can directly start using it. For example, in the following prompt, Iβve asked the ChatGPT to βWrite a paragraph about how SystemD is bad, in the style of Donald Trump
β, and look what I got in return.
Quite amusing, right? Now in the next prompt, Iβve asked the ChatGPT CLI prompt to βWrite me a hello world program in C, C++, Python and Java
β and look what I got in return.
Now, you can also use the following commands to perform specific things:
Commands | Usage |
---|---|
image: | Generate an image by using the βimage: [describe your image] β command. If you are using iTerm, you can view the image directly; otherwise, it will provide you with a URL to view the generated image in the browser. |
history | Prompt you with your chat history. |
models | Give you a list of all the available models for the OpenAI API. |
model: | Check the detailed description of a specific model by using the βmodel: [model ID shown in models command] β command. |
command: | It will interact with your system, you can use the βcommand: [list the current directory content] β command as an example. |
You can check commands on your own, as they are easy to do and self explanatory. However, Iβve shown you a few examples of image commands.
So, in the following prompt, Iβve asked ChatGPT to generate an image of βluffy fighting with gold d. roger
β and look what Iβve got.
And now look at the following result:
Well, I didnβt expect it, but letβs let it go after all; itβs luffy. In another prompt, I asked ChatGPT to generate a βcat liking its paw
β, and this was quite satisfying.
And the result is:
This time, I can fairly say it was quite promising. Iβve tried and tested it until my quota is finished, and it was quite an impressive project.
Like, you can use the following command to pipe your query to the ChatGPT CLI prompt:
$ echo "What is the command to get all pdf files created yesterday?" | chatgpt
Or, you can pass your query as a command-line argument using the β-p
β flag.
$ chatgpt -p "What is the regex to match an email address?"
The following are lists of known flags (or parameters) that you can use.
Flags/Parameters | Descriptions |
---|---|
β-t β or β--temperature β | Set an temperature |
β-m β or β--model β | Specify the model |
β--max-tokens β | Specify the max number of tokens |
β-s β or β--size β | Specify the image size (accepted are 256Γ256, 512Γ512, 1024Γ1024) |
β-p β or β--prompt β | Prompt |
β--prompt-from-file β | Specify the file containing the prompt in your system |
Now, letβs end this article here.
I hope you find this article interesting; however, if you have any questions or queries related to this topic, feel free to ask them in the comment section.
Till then, peace!
How can I remove the packages after installation?
If youβve installed it via the installation script on Debian, Ubuntu, RedHat, Fedora, etc., then remove the β
chatgpt
β script file from the β/usr/local/bin/chatgpt
β location. If youβve installed it manually, then remove the script from where you downloaded it and delete the added PATH in your shell configuration file.