How to Run ChatGPT in Linux

In this post, we will introduce you to ShellGPT, a command-line interface (CLI) that allows you to interact with OpenAI’s ChatGPT from your Linux terminal. We will cover the prerequisites for installation, setting up the environment, and provide examples of how to use ChatGPT for various tasks. By following this guide, you will be able to utilize the power of ChatGPT from the comfort of your Linux terminal.

What Is ShellGPT for Linux?

ShellGPT is a command-line interface that enables you to interact with OpenAI’s ChatGPT within your Linux terminal. It is a Python-based tool that streamlines the process of connecting to the OpenAI API and sending queries to ChatGPT. With ShellGPT, you can perform tasks such as generating code, answering questions, and even creating a chatbot directly from your terminal.

Prerequisites to Install ChatGPT in Linux CLI

Before we install ShellGPT, let’s ensure that your system meets the following requirements:

Install Python

Ensure that you have Python 3.6 or higher installed. You can check your Python version by running the following command:

python3 --version

If you don’t have Python installed, you can install it using your package manager, for example:

sudo apt install python3

Install Pip Package Manager

Next, install the Pip package manager for Python. Check if you have Pip installed with:

pip3 --version

If not, install Pip using the following command:

sudo apt install python3-pip

Install Venv Module

You’ll also need the Venv module to create a virtual environment for your project. Check if you have Venv installed by running:

python3 -m venv --help

If it’s not installed, use the following command to install it:

sudo apt install python3-venv

Set Up ShellGPT to Use ChatGPT in Linux Terminal

Now that you have the prerequisites, let’s set up ShellGPT.

Set up the Environment

First, create a virtual environment for your project and activate it:

python3 -m venv shellgpt-env
source shellgpt-env/bin/activate

Get Your OpenAI API Key

To use ChatGPT, you need an API key from OpenAI. Sign up for an account at https://beta.openai.com/signup/ and obtain your API key.

Install ShellGPT to Use ChatGPT

Next, install the ShellGPT package using Pip:

pip install shellgpt

ShellGPT: Syntax & Options

With ShellGPT installed, you can now interact with ChatGPT using the following command syntax:

shellgpt [options] "your prompt"

Options include:

  • -k or --api_key: Your OpenAI API key.
  • -e or --engine: The engine to use (e.g., “text-davinci-002”).
  • -t or --temperature: Controls the randomness of the response (0 to 1).
  • -m or --max_tokens: Limits the length of the response.

You can set your API key as an environment variable to avoid specifying it each time:

export OPENAI_API_KEY="your_api_key"

How to Use ChatGPT in Linux Terminal (Examples)

Now that ShellGPT is set up, let’s explore some examples of how to use ChatGPT in the Linux terminal.

Use ShellGPT for Queries

To ask ChatGPT a question, simply pass the prompt as an argument:

shellgpt "What are the benefits of exercise?"

ChatGPT Chatbot Mode

You can use ChatGPT as a chatbot by providing a list of messages:

json code:
shellgpt '{"messages":[{"role">:"system","content":"You are a helpful assistant."},{"role":"user","content":"What is the capital of France?"}]}'

Generate Code

You can also use ChatGPT to generate code. For example, to create a Python function that calculates the factorial of a number:

shellgpt "Write a Python function to calculate the factorial of a given number."

Generate Shell Commands

ChatGPT can help you generate shell commands for specific tasks:

shellgpt "How do I list all the files in a directory in Linux?"

Remember to deactivate the virtual environment once you are done using ShellGPT by typing:

deactivate 

This will return your terminal to the normal environment.

Final thought

ShellGPT brings the power of ChatGPT directly to your Linux terminal, making it easier for you to get things done without remembering complex commands. By following this guide, you can easily set up and use ChatGPT in your Linux command line. It’s a great addition to your Linux toolkit that can save you time and make your command-line experience more enjoyable.