CLI-based GPT-4 Assistant
CLI-based GPT-4 Assistant

By Bishal Sapkota

Fri Apr 12 2024

Building a CLI-based GPT-4 Assistant for Software Development

As developers, we often find ourselves stuck on coding problems or syntax issues, leading us to search through documentation or seek help online. But what if you could have a powerful AI assistant right at your fingertips, ready to provide guidance and solutions directly from your command line interface (CLI)? With the advent of GPT-4, this is now a reality. In this guide, we’ll walk through the steps to build a CLI-based assistant powered by GPT-4 to help you with your programming queries. The tool will be built using JavaScript and the OpenAI Node.js library.

Prerequisites

Before we begin, ensure that you have the following:

  • Node.js installed on your machine
  • An OpenAI API key (you can obtain one by creating an account on the OpenAI website)

You can follow the quickstart guide from OpenAI to get YOUR_API_KEY.

Setting up the Project

  1. Create a new directory for your project and navigate to it in your terminal.

  2. Initialize a new Node.js project by running the following command:

npm init -y
  1. Install the required dependencies by running:
npm install openai

Implementing the CLI Tool

  1. Create a new file called index.js and open it in your preferred code editor.

  2. Import the required modules at the top of the file:

const { Configuration, OpenAIApi } = require("openai");
  1. Set up the OpenAI API configuration with your API key:
const configuration = new Configuration({
  apiKey: "YOUR_API_KEY_HERE",
});
const openai = new OpenAIApi(configuration);

Replace "YOUR_API_KEY_HERE" with your actual OpenAI API key.

  1. Define a function to handle the chat completion request:
async function chatWithGPT(prompt) {
  // You can change the system_prompt to suit your use case
  const system_prompt = "You are an assistant that helps with short answers to technical questions"

  const response = await openai.createChatCompletion({
    model: "gpt-4",
    messages: [
        { role: "system", content: system_prompt},
        { role: "user", content: prompt }
        ],
  });

  return response.data.choices[0].message.content;
}

This function takes a prompt as input and sends it to the GPT-4 API using the createChatCompletion method. It then returns the response from the API.

  1. Create a function to handle user input from the command line:
const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

function askQuestion() {
  rl.question("How can I help: ", async (prompt) => {
    const answer = await chatWithGPT(prompt);
    console.log(`GPT-4 says: ${answer}`);
    askQuestion();
  });
}

askQuestion();

This function uses the readline module to prompt the user for input and then calls the chatWithGPT function with the user’s input. It then logs the response from the API and recursively calls itself to allow the user to ask another question.

Running the CLI Tool

  1. Execute the CLI tool by running:
node index.js

You should see a prompt asking you to enter your software development question. Type your question and press Enter. The tool will send your question to the GPT-4 API and display the response.

Bash Aliases and Setting Up an Alias for the CLI Tool

Bash aliases are shorthand commands that allow you to execute longer commands or a sequence of commands with a shorter, more memorable name. They can significantly improve your productivity on the command line by reducing the amount of typing required for frequently used commands.

To create a new bash alias, you need to add it to your shell configuration file, typically ~/.bashrc or ~/.bash_aliases. Here’s how you can set up an alias to access the AI assistant using the command llm:

  1. Open your ~/.bashrc file in a text editor:
nano ~/.bashrc
  1. At the end of the file, add the following line to create the llm alias:
alias llm='node /path/to/index.js'

Replace /path/to/index.js with the actual path to the file that runs the AI assistant.

  1. Save the file and exit the text editor.

  2. To apply the changes to your current shell session, run:

source ~/.bashrc

Now, whenever you type llm in your terminal, it will execute the script that runs the AI assistant.

Bash aliases are a powerful way to streamline your workflow and make your command-line experience more efficient. By setting up an alias for the AI assistant, you can quickly access its capabilities with a simple command, saving you time and effort.

Use Cases

Here are some examples of how you can use this CLI tool for programming:

  • Ask for explanations of programming concepts or algorithms
  • Request code examples or snippets for specific tasks
  • Get help with debugging or troubleshooting issues
  • Seek advice on software design patterns or best practices

Using the GPT-4 API through this CLI tool, you can quickly get explanations, examples, and guidance on various topics straight from your terminal. If you would like to learn to build web applications using emerging Foundation models, get in touch with us.

Related Articles

Let's chat about your career

The job market is changing rapidly. Technology is changing the way we work, and the demand for skills is shifting. Lets talk about how we can help you realize your career ambitions.