Installing ChatGPT onto a personal API

Overview

For the well informed, OpenAI’s ChatGPT is nothing short of a modern miracle. This piece of artificial intelligence has proven itself to be capable of a number of things, ranging from simple questions you could Google yourself, to being able to help people develop entire programs. However, that’s barely a fraction of what can be done. It is now possible to fully automate this AI’s library, and run code through it to do whatever you want. In this tutorial, we will go over getting it to run in Node.js, and letting you do so much more.

Prerequisites

 Open AI account - before installing any software, the first (and quickest) thing to do would be to go onto the OpenAI website and create an account. This is crucial for when you begin to use ChatGPT, as it will still need an account to function, just like when using it through the browser alone. Once this is done, you can then move onto the other prerequisites.

      Node.js(API) - An API (application programming interface) is a piece of software that serves as a medium for communication between applications. In this endeavor, think of an API as the medium between your programs, and ChatGPT itself. You essentially talk to the AI through the programme.While you are free to pick, for this tutorial we will be using Node.js, a Javascript API which seems to be the most preferred pick for working with ChatGPT.  You can find the latest version here (https://nodejs.org/en/download/).

   Visual Studio Code(text editor) - The next thing you’ll need is a text editor. This is a program that just serves as a means of editing your code, and setting up files needed to run with your API. My personal recommendation is Visual Studio Code. It is also free to download here:https://code.visualstudio.com.

Installing the AI

Ok, now that all those have been sorted,
the first thing you want to do is to open the Node.js command prompt, which looks like this when opened.

 Now, all further commands will be carried out within the folder. Next, while not necessary, but good to know, type in
“npm init -y”. This command just creates a JSON of the data about the folder, and the product should look similar to this:

Next, we must install the openai package required to make any of the main code work. While still in your Node.js application window, type in the next 4 lines of code in this exact order.

$npm install openai - this is the openai package that allows us to make contact with ChatGPT
$npm install dotenv -- save - this installs the dotenv package, which handles your API key
$touch index.js.env - this creates your index.js and .env files, which will will serve the purposes of housing your API key, and housing your code respectively.

Now, we can install ChatGPT into our newly created project folder. Before that, however, there are some files we need to initialize for the program. Open your text editor(Visual Studio Code, Notepad,etc) and create a new file by the name of index.js.  Within that file, type in the following code exactly, or just copy and paste it from this site.

In Visual Studio Code, just hit “New File” and hit enter after typing in “index.js”.

The required code:

const { Configuration, OpenAIApi require("openai");
require('dotenv').config()
const configuration = new Configuration({  apiKey: process.env.OPENAI_API_KEY,});const openai = new OpenAIApi(configuration);

What you want do next, is to open your .env file in Visual Studio Code and replace the "INSERT YOUR OPENAI API KEY HERE" section with your API key(make sure to put in quotations on both sides of the key).





Now that these files have been sorted, you can now install ChatGPT. Open the terminal in Visual Studio Code, if that was your preferred choice,  else go back to the Node.js command prompt and type in “npm install chatgpt”. Upon proper completion, you should get this outcome:


Essentially, the program has been installed. To test if it is running properly, go into either the terminal on VSCode, or Node.js’ command prompt and type in “node index.js”.‍