Linux Format

Coding for ChatGPT

Take control of the daddy of AI chatbots for fun and profit!

-

For our AI session, we are going to take a look at how to leverage one of the available

ChatGPT API integratio­ns. The functional­itynamed ChatGPT-shell-CLI provides access to the

ChatGPT chat engine from a shell. When invoked, the shell script can use ChatGPT to return informatio­n. It is also able to invoke commands directly.

Installati­on is simple and only requires a couple of supporting packages. For an Ubuntu install, open a terminal and run the following commands:

$ sudo apt install curl jq

$ sudo mkdir -p /etc/apt/keyrings

$ curl -fsSL https://repo.charm.sh/apt/gpg.key | sudo

gpg --dearmor -o /etc/apt/keyrings/charm.gpg echo “deb [signed-by=/etc/apt/keyrings/charm.gpg] https://repo.charm.sh/apt/ * *” | sudo tee /etc/apt/ sources.list.d/charm.list

$ sudo apt update && sudo apt install glow

To quickly explain what we have installed, jq is used to interact with the JSON data, which is returned from the ChatGPT API and curl is used to download the shell script, which will be used in the next step. Finally, the glow package is installed, which enables the tool to render markdown on the screen, which means a much nicer looking experience.

Now, drop back to the terminal and enter the following command to install the shell script. If you wish to check the functional­ity of the script before installing, download it from the link and then run it with Bash.

$ curl -sS https://raw.githubuser­content.com/0xacx/

chatGPT-shell-cli/main/install.sh | sudo -E bash

Before we can use the new functional­ity, we need to sign up for an account and enable API access by creating a key. Open your favourite web browser once again and navigate to https://openai.com/blog/ chatgpt. Use the Sign Up button in the top right-hand corner and either enter the requested details or use one of your existing Google, Microsoft or Apple accounts to sign up.

When requested, enter your mobile number and use the code that is sent to you to provide the necessary second factor of security. Select the API tile on the screen that shows three options. Please note that when you first sign up for ChatGPT, you are allocated a small amount of free credit. When this runs out, however, you need to purchase more credit. Using the menu at the top-right hand corner of the site, you can view how much credit you have been granted and how

much you have remaining by clicking on Manage Account > Usage.

Once the signup has been completed and you have viewed how much credit you have remaining, select the menu in the top-right corner by clicking on your username, then select the option to view your API keys. Once this page opens, select the option to create a new secret key and enter a name when prompted. When the key has been generated, copy it to a text file and save it for later usage.

The final step before being able to use the ChatGPT

shell script is to set an environmen­t variable that contains the API key. Navigate back to your terminal and enter the command below. Make sure you enter your own API key instead of the placeholde­r text below. If you wish to make this available every time you open a shell, add the command to the bottom of the . bashrc file, which is in the root of your home directory.

export OPENAI_KEY=

Now that everything is in place, we can open the shell script and start utilising ChatGPT. Return to the terminal and enter chatgpt//. . This then opens the tool and allows interactio­ns. Once the tool has opened, you are invited to enter a question. Test the integratio­n by asking a question, such as enquiring about the mass of the sun or the height of the Eiffel Tower.

Enter a prompt:

How heavy is the sun? chatgpt

The mass of the Sun is approximat­ely 1.989 × 10^30

kilograms.

While this example is all very interestin­g, it isn’t helping us with any sort of developmen­t work. Using the next example, we are asking ChatGPT to support us with the conversion of a video file. Anybody who has tried to use FFmpeg knows that the sheer number of command-line arguments is completely overwhelmi­ng. By simply asking for help with converting and speeding up a video, this is researched by ChatGPT and then executed (see screenshot, opposite page). By entering command: before a question, ChatGPT, can execute the returned results.

In the next useful example, we can open useful commands that we might have forgotten. Enter command: view running processes or command: is fdisk running? . The correct command is returned to complete either of these tasks and then they can be run (see screenshot, above-right).

A common task that programmer­s face is to validate input for different data types. For example, testing whether an email address follows a valid format is not a simple task. Email addresses require a single @ and can contain multiple domains before the top-level domain at the end. Also, not all characters are acceptable to be used in an email address. Most programmin­g languages support regular expression­s, which provides a way to filter text and validate it. ChatGPT can generate the regular expression for you (see screenshot, below). Once this has been generated,

ASKING FOR TROUBLE?

“By simply asking for help with converting and speeding up a video, this is researched by ChatGPT and then executed”

it can be tested and then copied and pasted into your own code.

In this section, we have covered a number of examples of how ChatGPT can be used to enhance your command-line experience and support with the informatio­n needed to improve your coding projects. We’ll finish with a little Latin, though: caveat emptor – let the buyer beware. Always ensure that you check what is generated before actually using it – we’re sure you’ve heard stories about people who have wiped their hard drive because they trusted what somebody else has suggested they do…

 ?? ?? Here we’re using ChatGPT in a shell prompt to convert a file from one video format to another.
Here we’re using ChatGPT in a shell prompt to convert a file from one video format to another.
 ?? ?? Generating regular expression­s – your wish is ChatGPTs command!
Generating regular expression­s – your wish is ChatGPTs command!
 ?? ?? Viewing running processes in the terminal is easy. Just ask!
Viewing running processes in the terminal is easy. Just ask!

Newspapers in English

Newspapers from Australia