Categories
Uncategorized

Building a Command Line Application using GPT3

Building an application with GPT3. An overview of OpenAI’s GPT3 application playground and how to get started building an app.

GPT3 allows a simple and accessible API for accessing and using AI in an application. The application I wanted to create was a command line utility to quickly lookup commands and associated command line switches.

Cbot, available on Github.

The language processing of GPT3 is well suited to process natural language questions and provide results across a wide multitude of obscure commands that would otherwise be difficult to memorize.

Getting Started

The development process starts in the playground of the OpenAI website. The examples section provides a number of examples, my command line bot was a modified version of a translation example.

You provide a number of examples and can then modify the options within GPT3 to increase or reduce randomness and provide alternatives for start/stop sequences. The beauty of GPT3 is that you don’t need to provide a lot of examples for the software to get really good at knowing the types of responses that you want.

With 5-6 samples the application was reliably producing useful results. The OpenAI playground makes it easy to export the API call as either a CURL command or as a short block of Python code.

The resulting Python code uses a library called openai and is otherwise six lines of code, two of those are actually unused. (yes I reported this bug.)

Google Colab

I had never previously written much Python code and Google Colab tool was a great way to get started. It’s essentially an interactive Python editing and debugging tool, and since it’s interactive and in the cloud there isn’t much setup needed.

I copied the exported code and started playing around. If you have a GPT3 API key you can try my initial version here.

In playing interactively, I was more easily able to figure out how to parse the JSON and I realized that including the platform name (Mac, Windows, Linux) helped the bot determine the appropriate platform specific commands and platform specific options and folders. This really improved the quality of results.

OpenAI API and limitations

The current version of the software doesn’t allow fine-tuning of the GPT3 completion API using your own database. For use-cases such as customer service or email completion, the ability to further train GPT3 on specific large data sets would be particularly useful. I do think this is coming as fine tuning was part of GPT2, but it hasn’t yet made its way into GPT3.

The other thing to note about the API is that it does take some practice and exploration. When providing a set of examples, it was difficult to unit-test the examples against known and desirable results. Very subtle changes in the prompts would yield very different results and the software would occasionally get stuck in a loop, feeding off of its own content.

Building the bot

Once I had the basic code working in Google Colab, I was able to get a version running on my computer. I had never programmed in Python and I ended up actually using GPT3 to help “auto-complete” some of my functions. I would do this by going back and forth with the playground with blocks of code. It wasn’t perfect but it felt much more natural and collaborative than the alternative of jumping back and forth between StackOverflow pages.

The core bot used the basic prompt from the playground example and a SQLite database to keep track of requests/responses and act as a local cache. This is likely overkill at this point but I thought it could be interesting if a general database of questions and answers could be compiled, filtered, enhanced and sorted over time. The database also acts as a speed and cost buffer since the GPT3 API is not free and not always the fastest.

Some examples:

$>cbot "How do I count the number of lines in a file?"
  wc -l filename.txt

$>cbot "How do I get the mime type of a file?"        
  file filename.txt

$>cbot "How do I create a file with the text 'hello world'"   
  echo hello world > hello.txt

$>cbot "How do I open php in interactive mode?" 
  php -a

$>cbot "How do I set my email using git config?"
  git config --global user.email "[email protected]"

$>cbot What is the current date
  date

Advanced Tricks

After using the bot for a few weeks I started to add some more advanced functionality.

The -x option allows you execute the command directly. The -c option allows you to copy the answer into the clipboard (a little safer than just executing it.)

$>cbot -x "how do I put my computer to sleep"
  Sleeping now...  

The -g option allows you to ask general questions.

$>cbot -g "Who was the 23rd president?"
  Herbert Hoover  

Lastly, you can use cbot to save command shortcuts. So if you’ve remembered an obscure command you can save it for later.

$>cbot -s listall "ls -la"
  Saving Shortcut
$>cbot -x listall  

Open Source

While this code is open source, the problem is that currently OpenAI isn’t available openly and that API keys are only available to select developers. This makes it currently impossible to publish open-source software that is meant to be used by typical end-users.

I am hopeful that end-user API keys will be made available so that open-source AI software and tools can be made more broadly available.

Watch the YouTube Video

Try the Code

Follow me on Twitter

Leave a Reply

Your email address will not be published. Required fields are marked *