DEV Community

Esteban Hernández
Esteban Hernández

Posted on

Gibson - An English-2-Shell CLI demo with OpenAI

Shell commands are esoteric. Most commands were invented decades ago and it almost feels like a language only the nerdiest of hackers are fluent in. I always end up googling reference materials even for the most basic of shell commands.

That changes today. The future is now. What a time to be alive.

Let's build a REPL CLI tool using Python that receives our intent in plain English and employs the OpenAI API to translate that into a
Shell command which is then executed as part of the Python process.

Abbreviations

  • REPL - Read-Evaluate-Print Loop
  • CLI - Command Line Interface
  • API - Application Programming Interface

OpenAI API Requirements

OpenAI requires an account and payment method with a minimum of $10 to use their API. However, new users receive a $10 credit upon sign up and the cost is based on the amount of tokens in the prompt and completion, and the model used. See here for additional billing information.

Setup your Personal account and copy your API key somewhere safe. Remember not to commit your API key to any public code repository (or any repository for that matter) as someone could use it to steal your credits and run up your bill. Keep your API key secret.

OpenAI Python Library

Create a development directory and name it gibson in honor of the writer William Gibson who pioneered the AI and Cyberpunk Science Fiction subgenres. Install the openai package and create a text file named gibson.py.

mkdir gibson
cd gibson
pip install --upgrade openai
touch gibson.py
Enter fullscreen mode Exit fullscreen mode

Gibson REPL CLI

Add the following to gibson.py and replace the placeholder OPENAI_API_KEY with the API key from the previous steps. Change the OS variable to reflect whatever operating system the script will be executing the commands on.

#! /c/Python310/python
import os
import openai

# Set your OS to get the correct Shell commands. Windows, Mac, Linx, etc.
OS = 'Windows'

# Set the OpenAI API Key
openai.api_key = 'OPENAI_API_KEY'

# Keep prompting for more input.
while True:
    # Capture the user's intent in plain English
    intent = input('$ ')
    # Exit the Python process if the user types 'exit'
    if intent == 'exit':
        exit()
    # Interpolate the user's intent and OS into the OpenAI prompt.
    prompt = f"""
        Translate this English statement to a Shell command for the OS.
        OS: {OS}
        English: {intent}
        Shell:
    """
    # Fetch the prompt completion using the OpenAI library.
    completion = openai.Completion.create(
        prompt=prompt.strip(), # Always remove surrounding whitespace for performance reasons on OpenAI.
        engine='text-davinci-003', # The OpenAI model.
        temperature=0, # How "creative". Lower means less creative.
        max_tokens=256 # Max of 256 tokens including prompt and completion.
    )
    # Execute the completion text as a Shell statement
    os.system(completion.choices[0].text.strip())
Enter fullscreen mode Exit fullscreen mode

The OpenAI Create Completion request will take the full prompt as an argument along with the name of the model to process it with. Use the OpenAI playground to try out the different models. The "dumber" the model, the cheaper it is to use. The davincii model is the most "intelligent" model at this time so it is also the most expensive but will give us the best results. It's great for creating proofs of concept and demos but you'll want to fine-tune a new model for a specific use-case in a production environment. The ada model is the cheapest and might be able to solve your use-case for a much lower price and faster execution time.

The temperature value refers to what sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer. In this case, a low temperature is best since the completion is expected to be constant given the same prompt.

The max_tokens value sets a limit on the prompt and completion size. You can think of tokens as words but more complex words may have multiple tokens. This can help with rate limiting but also runs the risk that the completion will be missing text if the prompt takes up too many tokens. See this article for more detail on how to count tokens. In this case, 256 is more than enough.

Limitations

  1. Security Executing commands without reviewing them is reckless so this script should never be run with Administrator privileges. Further, the AI can make mistakes and provide an incorrect command so be careful when deleting or making destructive actions.
  2. Error Handling The script doesn't check if the command succeeded or not. In the case of an error, the message will be printed to screen and the script will prompt for a new input. This is a great opportunity to use OpenAI to translate the Shell error text to plain English so as to provide us with a better understanding of what went wrong.

Top comments (0)