Easily increase productivity with ChatGPT on Linux terminal

603b25d5a17dd85382680619ef6c8476.jpeg

As the popularity of NFTs and metaverses fades, artificial intelligence has become the new buzzword in tech. ChatGPT and its alternatives are flourishing on the internet, helping users to complete everyday tasks with ease. Lots of articles with guides on how to start making chatbots like ChatGPT and how to use Bing AI in any browser. However, if you are a Linux user and want to bring the intelligence of this AI chatbot to your system, then ShellGPT is the solution. It brings the power of ChatGPT directly into your Linux terminal. ShellGPT gives you answers, smart suggestions, and writes commands and code for you to help you efficiently navigate the command line. This article will show you how to install the ShellGPT command line tool and use an AI bot similar to ChatGPT in the Linux command line.

07e3ecd8f746b2589b64953ccadfd8dc.jpeg

Run ChatGPT (2023) in Linux terminal

What is ShellGPT?

5d1256c23a8ecdccf6933693c57be234.jpeg

ShellGPT, developed by Farhold Sadikov (and two other contributors Eric and Loic Coyle), is a command-line version of ChatGPT that enables users to chat with AI in a Linux terminal Bot interaction. It is based on OpenAI’s GPT large-scale language model (read more about OpenAI’s new GPT-4 model here).

ShellGPT can provide smart suggestions and recommendations, and even execute Shell commands based on your text input. It also learns from your interactions and becomes more accurate over time. With this ChatGPT tool built into the command line, users don’t need to type long commands or remember complicated Linux terminal commands. They can save valuable time while minimizing errors and let ChatGPT perform some of the menial work for them.

Prerequisites for installing ChatGPT in Linux CLI

Before you can install ShellGPT on your Linux system, here are some prerequisites. These tools are not too complex and can be installed with a few simple commands.

1. Install Python

Python is an indispensable tool, and like many other Linux tools and libraries, ShellGPT (the command-line version of ChatGPT) is built on top of Python. In fact, ChatGPT is also built using Python and other languages. Typically, Python comes pre-installed on most recent Linux distributions. So, first check the installed version of Python on your Linux computer using the following command

python3 --version

f09ec42cb8b1fd12eff370539039179e.jpeg

If the above command outputs any errors, it means that your system does not have Python installed, or it has installed a version of Python 2.7 that is no longer used. At this time, you can refer to the Python installation guide under the Linux platform provided on the Internet.

2. Install the Pip package manager

Pip is a cross-platform Python package manager that helps manage various Python packages and libraries that are required to bring ChatGPT to a Linux system. You can install, upgrade and uninstall required packages with a single command. Typically, Pip comes pre-installed with Python in most Linux distributions, but if it is not installed, you can install it with the following command:

bdb22ab2342a27fa3de858dee1e701b5.jpeg

3. Install Venv module

While ShellGPT or ChatGPT do not necessarily require this module, we recommend that you install it to create an isolated virtual environment in Linux to avoid conflicts with other libraries. Installing any library or package installs many background dependencies that may interfere with other libraries. Creating a virtual environment for a project requires the “venv” module, which can be installed with the following command:

sudo apt install python3-venv

6f0cf2d2e3bbe97571a72415d9fa4767.jpeg

Setting up ShellGPT in Linux terminal to use ChatGPT

We have divided our guide on installing ShellGPT on your Linux computer into four different sections to make it easier for you to follow. Let’s start by setting up the environment, then get the OpenAI API key and install the chatbot in the terminal.

set environment

Now that you have the required dependencies installed, let’s follow the steps below to create the environment to access ChatGPT from your Linux terminal:

  • First, open Terminal from the Applications menu or use the “Ctrl + Alt + T” keyboard shortcut.

  • Now, create a new directory using the command below and keep all the files organized.

mkdir <your_directory_name>

For example, we created a new directory called “commandline-chatgpt” with the following command:

mkdir commandline-chatgpt

32ed7b9449c576cab8b60982260c57da.jpeg

3. Then, use the “Change Directory” command to switch to the new directory you just created:

cd <new_directory_name>

Here is an example, we have switched to the commandline-chatgpt directory, where we will create the environment to use ChatGPT in the Linux terminal:

cd commandline-chatgpt

0ca181128ab5b47d2d5beee02e9eb5ac.jpeg

4. Now, create a virtual environment using the venv module using the following command. We use “chatgpt_cli” as the name of the virtual environment.

python3 -m venv <virtual_environment_name>
python3 -m venv chatgpt_cli

8b5e39598267016090a0dfa6c696e386.jpeg

The virtual environment just created is inactive by default. To activate the environment, use the following command:

source <virtual_environment_name>/bin/activate

After executing the above command, the command line prompt will display the name of the virtual environment in the following format:

(<virtual_environment_name>)<username>@<system_name>

As you can see in the image below, after executing the above command, the default Linux shell prompt has been changed to “(chatgpt_cli) remote2@remotehost”.

646b5e29471ef648422e3f60b565ad36.jpeg

Get your OpenAI API key

How to obtain the OpenAI API will not be introduced in detail here. Many related articles have related introductions. After obtaining the key, please use the following command to create an environment variable for the API key. In Linux, you can use the “export” command to create environment variables. Please replace the placeholder “” with your actual API key generated in your Linux terminal. This will be used to use ChatGPT.

export OPENAI_API_KEY=<your_OpenAI_API_key_here>

7c57d4096231a6d2be826d96c47266ee.jpeg

Use the “env” command to list the environment variable to verify that it was set successfully:

env

c91be21ceb910ff962a96a786d65b512.jpeg

This variable is only stored temporarily within the current session. To permanently store the API key, open the “.bashrc” file with a text editor of your choice and add this variable at the end of the file.

export OPENAI_API_KEY=<your_OpenAI_API_key_here>

a690a964790a4668b54ff8b4d8a8eb49.jpeg

After adding the OpenAI API key, save the file and exit the text editor. Now, run this command for the changes to take effect:

source.bashrc

Finally, verify that the changes took effect using the “env” command:

env

d2840e3d6bdeeeda2fdf56480cbfa113.jpeg

Install ShellGPT

After completing the environment setup, you can now install the command line version of ChatGPT in Linux. If you are installing it in a virtual environment, you need to omit the “–user” flag. Now, install ShellGPT on your computer with the following command:

pip3 install shell-gpt --user

9d58ffcbc5c0332ab01d7b63289775b0.jpeg

ShellGPT: syntax and options

Now that you have installed ShellGPT, you must be eager to use it to complete various tasks. But before that, let’s take a look at the syntax and some options to make them more interesting when outputting. Multiple tasks are very easy to use with ShellGPT, thanks to its simple and clear syntax:

sgpt <options> <input_query>

There are some options you can use ShellGPT (sgpt) chatbot, including:

options

describe

–temperature

Change the randomness of the output

–top-probablity

limited to the most likely vocabulary or words

–chat

for conversations with people

–shell

for shell commands as output

–execute

Execute the command received from the –shell option as output

–code

for outputting code as

How to use ChatGPT in Linux terminal (example)

1. Use ShellGPT query

You can use ShellGPT as a search engine for any type of query. Since it’s an AI chatbot, you get results that more closely resemble human answers, rather than a series of top-ranking pages like most search engines. The syntax for getting answers to questions using ShellGPT is as follows:

sgpt "<your_query>"

For example, if you need to know the mass of the sun, use the following command:

sgpt "mass of sun"

You’ll see a nice animation on the terminal as ShellGPT tries to get a response. The responses we received are highlighted below. Depending on how your model is trained, you might get similar responses.

The mass of the Sun is approximately 1.989 x 10^30 kilometers.

befb423abd01ca38e3a4174636dd1f27.png

0a8992262c69f44181ca383fabf51a75.png

2. ChatGPT chat robot mode

If you have ever used ChatGPT to chat, you will definitely feel that its answers are no less than humans. Now, thanks to ShellGPT, you can even chat using ChatGPT in a Linux terminal. Just use the –chat option followed by a unique session name and a hint.

sgpt --chat <unique_chat_session_name> <input_prompt>

For example, if you want ChatGPT to tell you a joke, use this command:

sgpt --chat joke "can you tell me a joke?"

After running the above command, you will get the following as output:

Sure, here's one:

Why couldn't the bicycle stand up by itself?

Because it was two-tired!

57790fc8f5007545536ba444710a9b72.jpeg

3. Generate code

You can even use the CLI-based ChatGPT tool to troubleshoot coding questions or generate code snippets. Just use the –code flag to generate code for your prompt like this:

sgpt --code "<input_prompt>"

For example, if you want to solve the classic Fizz Buzz problem using python, use the following hints:

Further reading: What are Fizz Buzz questions?

Fizz Buzz is a classic interview question with the following rules:

Start counting from 1, when counting to a multiple of 3, say “Fizz” instead of this number; when counting to a multiple of 5, say “Buzz” instead of this number; when counting to a multiple of both 3 and 5, Say “FizzBuzz” in place of the number.

sgpt --code "Solve classic fizz buzz problem using Python"

ChatGPT will quickly give you the output, which you can then paste into a code editor and test:

for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

e052862fe2ca32a03bd214cfa9951377.jpeg

4. Generate Shell commands

While the terminal can be a powerful tool for executing complex commands and automating tasks, novice users can sometimes have difficulty remembering the syntax and options of various Linux commands. Using ChatGPT in the command line, you not only get the syntax of Linux commands, but also the exact command with the required parameters and options. Just use the –shell flag as:

sgpt --shell "<input_prompt>"

For example, if you need to set read-only permissions for all files in the current directory, use the command:

sgpt --shell "Make all files in the current directory read-only"

f468ef3679db3ff0e021602bc5d70634.jpeg

As expected, ChatGPT passes this as output and returns:

chmod -R a-w .

Additionally, you can even execute the generated shell commands directly if you use both the –execute and –shell flags. As for the example above, the syntax is as follows:

sgpt --shell --execute "make all files in current directory read-only"

f179618af67a6b115923e880e3f0a891.jpeg

You will get the following output:

chmod -R a-w .
Execute shell command? [y/N]: y

End

So, ShellGPT brings the power of ChatGPT to your Linux terminal. Not only does it make working with the command line easier for both beginners and experienced users, but it also brings additional functionality. As we mentioned above, it will become more useful over time because it is designed to learn from its users. But remember, don’t share any sensitive information or data with any kind of AI model, especially from your company’s proprietary code. That being said, let us know what you think of this AI command line tool in the comments below.

That’s all for today’s sharing, thank you for reading, I hope it can help you, it’s not easy to create articles, if you like my sharing, don’t forget to like and forward it, so that more people in need can see it, and finally don’t forget to pay attention “Front-end experts”, your support will be the biggest motivation for me to share, and I will continue to output more content in the future, so stay tuned.

original:
https://beebom.com/how-use-chatgpt-linux-terminal/

By: Beebom Staff

Direct translation, some self-adapted and added parts, the translation level is limited, it is inevitable that there are omissions, welcome to correct