ChatGPT in Linux Terminal, How to Setup & Use

ChatGPT has been set up around the world by surprise. OpenAI created this complex language model, which has the ability to produce similar human replies to requests. Having a lot of excitement about it, everyone wants to use their abilities to do their tasks.

You may use Linux and want to use ChatGPT’s features, please read our instructions. We can guide you with the steps of installing and configuring ChatGPT in the Linux Terminal.

ShellGPT for Linux

ShellGPT is an interactive command-line implementation of ChatGPT that allows users to interact with the AI chatbot directly from their Linux terminal. ShellGPT is capable of offering smart advice and suggestions depending on your text data provided, and it can even run shell commands.

It further improves from what you say and improves with time. Users no longer must enter long instructions or memorize difficult Linux Terminal commands with this ChatGPT utility built into the command lines.

They may decrease time while reducing mistakes by having ChatGPT conduct part of their repetitive work for you.

ChatGPT in Linux Terminal
ChatGPT in Linux Terminal

How to Run ChatGPT in Linux Terminal

Follow the steps below to utilize ChatGPT in a Linux terminal. After you have installed the OpenAI API software and received your API key, you may use Python to connect with ChatGPT and create text depending on your instructions.

Install Python 3

The first thing you need to do is to make sure Python 3 has been installed on your Linux computer. Start the terminal you are using and perform the given command to see if Python 3 is installed.

python3 – version

When any problems happen or the version does not install, try running the following command to install Python 3.

sudo apt-get install python3

Install the Required Packages

Following that, you have to set up the Python packages required for ChatGPT to work. The OpenAI API package happens to be one of the most important. You can setup it using the following command.

pip3 install openai

Set Up OpenAI API Credentials

You have to possess an OpenAI API key to utilize ChatGPT. In case you have yet to create an OpenAI account, go to the website link https://beta.openai.com/signup/ to complete the steps to set up one and get an API key. After you’ve got your API key, you must save it as a system option in the terminal you are using. To perform so, launch your terminal and enter the given command.

export OPENAI_API_SECRET_KEY=<your API key here>

Clone the ChatGPT Repository

Then, you have to get the ChatGPT code. This may be performed by cloning the ChatGPT source from GitHub. To perform so, launch the terminal you are using and enter the name of the command. The ChatGPT code will be downloaded to your local PC.

git clone https://github.com/orta/ChatGPT.git

Set Up the Configuration File

Make a file called “.env” without the quotations in the ChatGPT directory. The file that follows will include your OpenAI API key, which ChatGPT will read when it starts. On your terminal, enter the command that follows to create the file.

touch .env

Then, with a text editor, open the file and add your API key to it, as seen below.

OPENAI_API_SECRET_KEY=<your API key here>

Run ChatGPT

  • Run the command “python3 main.py” in the ChatGPT directory to launch the application.
  • You may then start a chat using ChatGPT by typing in instructions.
  • ChatGPT will now be available in your Linux terminal

How to Use ChatGPT in Linux Terminal

Below are some examples to use ChatGPT in Linux Terminal.

Use ShellGPT for Queries

ShellGPT may be used as an internet search engine for any type of query. Because it is a chatbot with artificial intelligence, the results are more similar to humans, compared to a collection of ranking web pages like conventional search engines. To receive responses to your questions, use the following syntax.

sgpt "<your_query>"

Example: if you’re looking to find out the mass of the sun, run the following command.

sgpt "mass of sun"

ChatGPT Chatbot Mode

Once you’ve used ChatGPT for conversing, you’ve probably experienced that its replies are on the level with those of a human. You may now utilize ChatGPT directly from your Linux terminal because of ShellGPT. Simply type chat then provide an original conversation name and an instruction.

sgpt --chat <unique_chat_session_name> <input_prompt>

Example: Suppose you want the ChatGPT to give you a joke, use the following command.

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

When you perform that previous command, you will see the following result.

  • Okay, here’s one:
  • Why wasn’t able the bicycle to stand on its own?
  • Because it was a two-tiered vehicle.

Generate Code

You can also make use of the ChatGPT Command Line Interface (CLI) tool to fix coding issues or produce code samples. To produce code for your request, just use the code parameter, as seen below.

sgpt --code "<input_prompt>"

Example: When you want to answer the famous Fizz Buzz problem in Python, provide the following request.

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

ChatGPT immediately gives a response that you are able to copy and paste into your code editor to perform the 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)

Generate Shell Commands

When the Terminal may be a powerful tool for performing complicated commands and automating operations, it can be hard for new users to understand the syntax and arguments of numerous Linux commands.

With ChatGPT on that command line of sight, you may not simply retrieve the syntax of a Linux command, as well as execute that command with the required parameters and options necessary. Just use the shell flag commands.

sgpt --shell "<input_prompt>"

Example: Once you need to make all files in the present directory read-only, use the command.

sgpt --shellMake all files in the current directory read-only“.

ChatGPT, as identified, delivers and returns the following as result.

chmod -R a-w .

Also, should you use execute and shell flags combined, you may immediately run the resulting shell command.

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

You will receive the following results.

chmod -R a-w .
Execute shell command? [y/N]: y
  1. Is it possible to have a continuous chat using ChatGPT in the Linux terminal?

     Yes, you can keep the conversational history in the written text to create a continuous conversation. Use the query parameter to save the user information and ChatGPT answers for the following conversation. As a result, the template will have meaning for the continuing discussion.

  2. How can I stop the ChatGPT chat in the terminal?

    To end the discussion, use a particular word that, when understood in input from the user will stop the cycle and terminate the conversation.

  3. How can I change the reply length and creativity of ChatGPT?

    The response length is controlled by the max_tokens parameter, and the creativity is controlled by the temperature parameter. Increased temperatures generate replies with more creativity but shorter and lower temperatures result in them being more focused.

*Disclaimer: The Content we produce on this page will be only such as advice or readable information. We provide the content on the page after getting it from different sources at that time. Still, the changes from time to time, like contact information such as phone numbers, email IDs, web links and any other about that product in any form, are not responsible to us. We provide the information you need and are not an authentic source. You may refer to the original/official website/page for further clarification and more info on that product.