DEV Community

Devang Tomar
Devang Tomar

Posted on • Originally published at devangtomar.hashnode.dev on

Unleashing the Power of ChatGPT in Linux CLI: A Game-Changing Experience! πŸ€―πŸ‘©πŸ»β€πŸ’»

ChatGPT has transformed how individuals engage with technology. It has ushered in a new era of personalized and natural language communication, making it simpler for people to get things done, obtain information, and interact with others. ChatGPT has enabled more smooth and effective communication, resulting in a more comfortable and connected environment for individuals all over the world, from customer care chatbots to language learning apps.

Although ChatGPT has a fantastic user interface, there are some quick and easy ways to get this going for techies who prefer CLI to the GUI. Yes, youre correct. Developers and the local community have created several packages and other materials that allow us to quickly load the CLI.

So lets get going.

Pre-requisites 🐣

  • A Linux machine or a virtual instance: You can obtain a Linux machine or a virtual instance from cloud providers like AWS, GCP, or any other provider of your choice.

  • Python and PIP package manager: Python is an essential tool for running ChatGPT, as it is built on Python, and many other Linux tools and libraries are also built on Python. Python is usually preinstalled on most recent Linux distributions.

To check the Python version, use the command

python3 --version

Enter fullscreen mode Exit fullscreen mode

If Python3 is not installed, use the command (considering you are on Ubuntu)

sudo apt install python3 -y

Enter fullscreen mode Exit fullscreen mode

PIP is a package manager that works across platforms, which is used to install, upgrade, and uninstall the required packages. PIP is generally preinstalled with Python in most Linux distributions. To install PIP, use the command :

sudo apt install python3-pip -y

Enter fullscreen mode Exit fullscreen mode

And to check the PIP version, use the command

pip3 --version

Enter fullscreen mode Exit fullscreen mode
  • Venv module (optional but recommended): The venv module is not required for ChatGPT, but it is recommended to create an isolated virtual environment in Linux and avoid conflicts with other libraries. To install the venv module, use the command
sudo apt install python3-venv

Enter fullscreen mode Exit fullscreen mode

And to create a virtual environment with venv, use the command

python3 -m venv <virtual_environment_name>

Enter fullscreen mode Exit fullscreen mode

For example :

python3 -m venv chatgpt_linux_cli

Enter fullscreen mode Exit fullscreen mode
  • Virtual Environment Activation: Once you have created a virtual environment with venv, it will be deactivated by default. To activate the virtual environment, use the command
source <virtual_environment_name>/bin/activate

Enter fullscreen mode Exit fullscreen mode

Once you execute the above command, the shell prompt will now display the name of the virtual environment in brackets, like this:

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

Enter fullscreen mode Exit fullscreen mode

For example, the default Linux shell prompt will change to (chatgpt_linux_cli) upon running the commands above.

Get Your OpenAI API Key πŸ”‘

To utilize ChatGPTs offerings in Linux, you will require an OpenAI API key. Presently, OpenAI is giving out $5 credits for testing purposes. Once the credits are utilized, payment must be made for API access. However, the following are the steps to acquire an OpenAI API key for this ChatGPT command line chatbot:

  • Visit the OpenAI website and register for an OpenAI account. Simply log in if you already have an account to go to the next stage.

  • Next, select View API keys from the drop-down menu by clicking on your profile image in the top right corner of the screen.

  • You can view all previously generated API Keys here, if any. Click the Create new secret key button to generate a fresh API key.

  • Your API key will appear in a fresh pop-up window. This API key should not be disclosed to the public or shared with anybody. You can only view your API key once, so be sure to copy it somewhere safe. If you click OK at this point, you wont be able to copy the API key.

  • Now, create an environment variable for this API key with the command below. In Linux, you can create an environment variable using the export command. Replace <your_OpenAI_API_key_here> placeholder with the actual API key you generated to use ChatGPT in the Linux terminal.

export OPENAI_API_KEY=<your_OpenAI_API_key_here>

Note : You can verify the environment variable by listing it with the env command

  • This variable is only temporarily stored for the current session. To store the API key permanently, open the .bashrc file in the text editor of your choice and add the variable at the end of the file.
export OPENAI_API_KEY=<your_OpenAI_API_key_here>

Enter fullscreen mode Exit fullscreen mode
  • After adding the OpenAI API key, save the document and close the text editor. Run this command to make the changes effective right away:
source .bashrc

Enter fullscreen mode Exit fullscreen mode

Note : Finally, verify the changes with the env command env

Install ShellGPT to Use ChatGPT

After completing the environment setup, you can now install ChatGPT for Linux using the command line. If youre setting up the installation in a virtual environment, you must remove the --user flag. Use the following command to now install ShellGPT on your computer:

pip3 install shell-gpt --user

Enter fullscreen mode Exit fullscreen mode

ShellGPT: Syntax & Options

You must be anxious to utilise ShellGPT for a variety of tasks now that you have installed it. But first, lets look at the syntax and a few options we have to employ to spice up our outputs. ShellGPTs simple syntax makes it simple to use for numerous tasks:

sgpt <options> <input_query>

Enter fullscreen mode Exit fullscreen mode

You can use the ShellGPT (sgpt) chatbot to, among other things:

How to Use ChatGPT in Linux Terminal (Examples) πŸ‘©πŸ»πŸ’»

1. Use ShellGPT for Queries

For any kind of search, you can utilise ShellGPT as the search engine. Since it is an AI chatbot, the results are more human-like rather than a list of ranked web pages like you would typically get from a search engine. To utilise ShellGPT to retrieve answers to your queries, use the following syntax:

sgpt <your_query>

Enter fullscreen mode Exit fullscreen mode

Use this command, for instance, to determine the mass of the sun:

sgpt "mass of sun"
# -> = 1.99 10^30 kg

Enter fullscreen mode Exit fullscreen mode

2. ChatGPT Chatbot Mode πŸ“³

If youve ever utilised ChatGPT for chat, you must have thought that the responses were on par with those of a real person. You may now utilise ChatGPT directly from your Linux terminal thanks to ShellGPT. Use just thechat option, a special session name, and a prompt.

sgpt --chat number "please remember my favorite number: 4"
# -> I will remember that your favorite number is 4.

sgpt --chat number "what would be my favorite number + 4?"
# -> Your favorite number is 4, so if we add 4 to it, the result would be 8.

Enter fullscreen mode Exit fullscreen mode

3. Generate Code πŸ§‘πŸ»πŸ’»

You can also use chat sessions to iteratively improve GPT suggestions by providing additional clues.

sgpt --chat python_requst --code "make an example request to localhost using Python"


import requests
response = requests.get('http://localhost')
print(response.text)

Enter fullscreen mode Exit fullscreen mode

Asking AI to add a cache to our request.

sgpt --chat python_request --code "add caching"

import requestsfrom cachecontrol import CacheControl
sess = requests.session()cached_sess = CacheControl(sess)
response = cached_sess.get('http://localhost')
print(response.text)

Enter fullscreen mode Exit fullscreen mode

4. Generate Shell Commands πŸ’»

While the Terminal can be a powerful tool for automating operations and running complex commands, it can occasionally be challenging for new users to recall the syntax and options of different Linux commands. You can use ChatGPT on your command line to obtain not just the syntax of a Linux command but also the precise command along with any necessary parameters and options. Use the--shellflag as follows:

sgpt --chat sh --shell "What are the files in this directory?"
# -> ls
sgpt --chat sh "Sort them by name"
# -> ls | sort
sgpt --chat sh "Concatenate them using FFMPEG"
# -> ffmpeg -i "concat:$(ls | sort | tr '\n' '|')" -codec copy output.mp4
sgpt --chat sh "Convert the resulting file into an MP3"
# -> ffmpeg -i output.mp4 -vn -acodec libmp3lame -ac 2 -ab 160k -ar 48000 final_output.mp3

Enter fullscreen mode Exit fullscreen mode

More here : GitHubTheR1D/shell_gpt: A command-line productivity tool powered by ChatGPT, will help you accomplish your tasks faster and more efficiently.

Conclusion πŸ’­

Therefore, ShellGPT incorporates ChatGPTs power into your Linux terminal.For both inexperienced and seasoned users, it not only makes command-line work simpler but also introduces new functionality.

Additionally, as was already mentioned, it gains in usefulness over time because it is built to learn from users.

Remember, though, to never give any AI model your companys confidential code or any other sensitive data. Having said that, please share your thoughts on this command-line AI tool in the comments section below.

Connect with Me on Social Media

🐦 Follow me on Twitter: devangtomar7

πŸ”— Connect with me on LinkedIn: devangtomar

πŸ“· Check out my Instagram: be_ayushmann

Checkout my blogs on Medium: Devang Tomar

# Checkout my blogs on Hashnode: devangtomar

πŸ§‘πŸ’» Checkout my blogs on Dev.to: devangtomar

Top comments (0)