This page looks best with JavaScript enabled

How to use ChatGPT with the API in the Linux command line interface

 ·  β˜• 3 min read  ·  🐧 sysadmin
Exercises to complete:
  1. Obtain an API key
  2. Make sure that you have the OpenAI Python client installed
  3. Create a python 3 script
  4. Create a Bash script
  5. Make scripts executable and run the Bash script
To be able to run chatGPT in command line, you can follow these steps. Please note that you need to have sudo privileges or root access.
  1. Obtain an API key

To use the OpenAI API, you need an API key. If you don’t have one, you can sign up on the OpenAI website and create a new API key.

Note: The API is priced at $0.002 per 1K tokens. You have free credit to use, though.

OpenAI API key

OpenAI API key

Remember that OpenAI won’t display your secret API key again after you generate it, so copy your API key and save it. I’ll create an environment variable named OPENAI_API_KEY that will contain my API key for this tutorial.

  1. You can use the OpenAI API with Python in a Bash script. To do this, first make sure that you have the OpenAI Python client installed:

Now, you can install Docker with the following command.

SLES | openSUSE Leap 15.4

1
2
3
sudo zypper install python39 python39-pip
pip3 --version
pip3 install openai

Debian

1
2
3
sudo apt install python3-pip
pip3 --version
pip3 install openai

Red Hat

1
2
3
sudo dnf install python3-pip
pip3 --version
pip3 install openai
  1. Create a python 3 script:
vim chatgpt.py

put he below content into the file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/usr/bin/python3
import os
import openai

# Replace `'your-api-key'` with your actual OpenAI API key.
openai.api_key = os.getenv("OPENAI_API_KEY")

message = {"role":"user", "content": input("This is the beginning of your chat with AI. [To exit, type \"exit\".]\nYou: ")};

conversation = [{"role": "system", "content": "DIRECTIVE_FOR_gpt-3.5-turbo"}]

while(message["content"]!="exit"):
    conversation.append(message)
    completion = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=conversation)
    message["content"] = input(f"Assistant: {completion.choices[0].message.content} \nYou: ")
    print()
    conversation.append(completion.choices[0].message)
  1. Now, you can use a Python script in a Bash script that invokes the OpenAI API. Here’s an example Bash script that runs a Python script:
1
2
3
4
5
6
#!/bin/bash
# Change to the directory containing the Python script
cd $HOME

# Run the Python script
./chatgpt.py
  1. Set permissions:
1
2
chmod +x chatgpt.sh
chmod +x chatgpt.py

The script runs this Python code directly.

Note that this script must be run in an environment where Python and the openai package are available.

Add to .bashrc or .zshrc below variable:

export OPENAI_API_KEY=your-API-key

And then load it with the following command:

source .bashrc
or
source .zshrc

Remember to secure your API keys and consider environment variables or other secure methods to store them, as hard coding them in scripts can lead to security vulnerabilities.

  1. Now you can use the script to get responses from ChatGPT by running:
1
./chatgpt.sh

Remember to declare in a py file a proper path to the proper python version. For openSUSE 15.4 it will be:

#!/usr/bin/python3.9

That’s it! You now have a basic tutorial on how to use ChatGPT with the API in the Linux CLI. Feel free to customize the script or explore more advanced features of the OpenAI API.

Support the author with

sysadmin
WRITTEN BY
sysadmin
QA & Linux Specialist