Saturday, 11 February 2023

How to use OpenAI GPT 3 to Generate Linux Commands

How to use OpenAI GPT 3 to Generate Linux Commands

In this Linux tutorial, you will learn how to use OpenAI GPT 3 to generate Linux commands in Rocky Linux 9 or other Red Hat based Linux distros.


Table of Contents:


What is GPT 3?:

Generative Pre-trained Transformer 3 (GPT-3) is an autoregressive language model released in 2020 that uses deep learning to produce human-like text. Given an initial text as prompt, it will produce text that continues the prompt.

The architecture is a decoder-only transformer network with a 2048-token-long context and then-unprecedented size of 175 billion parameters, requiring 800GB to store. The model was trained using generative pre-training; it is trained to predict what the next token is based on previous tokens. The model demonstrated strong zero-shot and few-shot learning on many tasks. The authors described how language understanding performances in natural language processing (NLP) were improved in GPT-n through a process of "generative pre-training of a language model on a diverse corpus of unlabeled text, followed by discriminative fine-tuning on each specific task." This eliminated the need for human supervision and for time-intensive hand-labeling.

It is the third-generation language prediction model in the GPT series, successor to GPT-2 created by OpenAI, a San Francisco-based artificial intelligence research laboratory. GPT-3, which was introduced in May 2020, and was in beta testing as of July 2020, is part of a trend in natural language processing (NLP) systems of pre-trained language representations.

The quality of the text generated by GPT-3 is so high that it can be difficult to determine whether or not it was written by a human, which has both benefits and risks. Thirty-one OpenAI researchers and engineers presented the original May 28, 2020 paper introducing GPT-3. In their paper, they warned of GPT-3's potential dangers and called for research to mitigate risk. David Chalmers, an Australian philosopher, described GPT-3 as "one of the most interesting and important AI systems ever produced." An April 2022 review in The New York Times described GPT-3's capabilities as being able to write original prose with fluency equivalent to that of a human. (Source: Wikipedia)


Environment Specification:

We are using a Rocky Linux 9 minimal virtual machine with following specifications.

  • CPU - 3.4 Ghz (2 cores)
  • Memory - 2 GB
  • Storage - 20 GB
  • Operating System - Rocky Linux release 9.1 (Blue Onyx)
  • Hostname – gpt-3.centlinux.com
  • IP Address -


Update your Rocky Linux Server:

Connect with your Rocky Linux server as root user by using a ssh client.

Refresh your yum cache by executing following command at Linux bash prompt.

# dnf makecache
Rocky Linux 9 - BaseOS                          1.9 kB/s | 4.1 kB     00:02
Rocky Linux 9 - AppStream                       1.4 kB/s | 4.5 kB     00:03
Rocky Linux 9 - Extras                          1.1 kB/s | 2.9 kB     00:02
Metadata cache created.

Now, you can execute dnf command to update Linux software packages.

# dnf update -y

Sometimes, the above command also installs a newer version of Linux Kernel.

If this happens, then you should reboot your Linux operating system before installing OpenAI GPT 3 software.

# reboot

After reboot, check the version of your Linux Kernel and operating system.

# cat /etc/rocky-release
Rocky Linux release 9.1 (Blue Onyx)

# uname -r


Installing OpenAI GPT 3 Prerequisites:

To access OpenAI GPT 3 backend server, you need to install Shell Genie software.

Shell Genie is a Python application and can be installed via PIP package manager.

But Shell Genie only supports Python 3.10 or later. These versions of Python are not available via standard yum repositories.

Therefore, you need to follow below steps to install Python 3.10 on your Linux server before performing installation of Shell Genie.

Install OpenAI GPT 3 prerequisites software packages by using dnf command.

# dnf install -y curl gcc openssl-devel bzip2-devel libffi-devel zlib-devel tar wget make

Download Python 3.10 by using wget command. You can also copy the download link of a later version from Python official website.

# cd /tmp
# wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
--2023-02-10 22:36:44--  https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
Resolving www.python.org (www.python.org)..., 2a04:4e42:48::223
Connecting to www.python.org (www.python.org)||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 19342692 (18M) [application/octet-stream]
Saving to: ‘Python-3.10.4.tar.xz’

Python-3.10.4.tar.x 100%[===================>]  18.45M  1.09MB/s    in 20s

2023-02-10 22:37:05 (945 KB/s) - ‘Python-3.10.4.tar.xz’ saved [19342692/19342692]

Extract downloaded Python TARBall by using tar command.

# tar -xf Python-3.10.4.tar.xz -C /opt/

Go to extracted directory and install Python 3.10 by executing following commands.

# cd /opt/Python-3.10.4
# ./configure --enable-optimizations
# make -j 2
# make altinstall


Installing Shell Genie Software:

Create a Python virtual environment in your preferred location.

# python3.10 -m venv .venv

Activate your Python virtual environment.

# source .venv/bin/activate

Install Shell Genie software by using pip command.

# pip3.10 install shell-genie

After installation, initialize your Shell Genie software by executing following command.

# shell-genie init
Select backend: [openai-gpt3/free-genie]: free-genie
Note that this server will store the requested command, OS, and shell version to
improve the model. Also, I cannot guarantee that the server will be up and
running 24/7.
Do you want to continue? [y/n]: y
Do you want to provide feedback about the generated commands to improve the
models? [y/n]: n
Is your OS Rocky Linux 9.1 (Blue Onyx)? [y/n]: y
The following configuration will be saved:
    'backend': 'free-genie',
    'os': 'Linux',
    'os_fullname': 'Rocky Linux 9.1 (Blue Onyx)',
    'shell': 'bash',
    'training-feedback': False
Config file saved at /root/.config/.shell_genie/config.json

Here, we are using free-genie (a free to use server) backend server. Because, I don't have registered an account at OpenAI. However, you can create an Account at OpenAI and put some credit therein to use their openai-gpt3 backend server.


Generate Linux Commands using OpenAI GPT 3:

After initializing your Shell Genie software, ask a few questions like the following.

# shell-genie ask "Provide a command to check error log"
Command: less /var/log/messages
Do you want to run the command? [y/n]: n

# shell-genie ask "find all files that was created in last 3 days."
Command: find / -type f -mtime -3
Do you want to run the command? [y/n]: n

# shell-genie ask "Allow FTP service in Linux Firewall"
Command: firewall-cmd --permanent --add-service=ftp
Do you want to run the command? [y/n]: n



In this Linux tutorial, you have successfully learned how to use OpenAI GPT 3 to generate Linux commands in Rocky Linux 9 or other Red Hat based Linux distros.

If you find this article useful? Consider supporting us by Buy Me A Coffee

No comments:

Post a Comment

© 2023 CentLinux. All Rights Reserved.