Welcome to Part 2 of this series of tutorials, where I’ll create a GPT-3 application with Node.js. I have talked about “How to Train OpenAI GPT-3” in Part 1 of this tutorial series. Visit https://techpro.ninja/how-to-train-openai-gpt-3/ if you want to read Part 1. I will build upon the training data from our mental health context

Note: OpenAI recommends using Node.js only on the server side; otherwise, the security keys will be exposed.

Tutorial Series

How to train OpenAI GPT-3 – Part 1

Create GPT-3 application with Node.js – Part 2

GPT 3 Fine Tuning – Part 3

1. Getting an API key

OpenAI is an API service and does not provide its code for security reasons. Hence, we need API keys to access their services. You can create and manage your access keys from Account API Keys – OpenAI API. Create a new secret key and copy it to a secure file.

Create and manage OpenAI keys for Node.js application
Create and manage OpenAI keys

2. Creating Node.js server

In this step, we will create a Node.js server application which will speak to the GPT-3 API for the Mental health project that we created in Part 1 of the tutorial.

Step 2.a

Initiate a Node.js application and import basic libraries like cors, express and body-parser

npm init

npm install cors express body-parser

Step 2.b

Import the official NPM library of OpenAI

npm install openai

Step 2.c

Create a basic server application with Node.js with a Post route and name the file as server.js.

const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");

const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const port = 3000;

// create a post route
app.post("/openai-api", (req, res) => {
    const data = req.body;
    res.json(data);
});

app.listen(port, () => {
    console.log(`Node server listening at http://localhost:${port}`);
});

The above piece of code when run, creates a web server listening at port 3000. So, if someone hits http://localhost:3000/openai-api with a post request, the server will return back the body of the post request

Step 2.d

Finally, we will write the OpenAI code block and the training data for Mental Health problem from Part 1.

Step 2.d.i

Import OpenAI into the code

// Import the OpenAI
const { Configuration, OpenAIApi } = require("openai");
Step 2.d.ii

Create a configuration for OpenAI with the API secret key obtained from Step 1. Ideally the key should be written in an environment file. But for our demo purposes I am directly writing the key into our code.

// Create a new OpenAI configuration and paste your API key
// obtained from Step 1
// The key displayed here is a fake key
const configuration = new Configuration({
    apiKey: "sk-grcXXXXJn5KZHXXX7rT3BlbkFJtufXz7xErXXXXXXXXW",
});
const openai = new OpenAIApi(configuration);
Step 2.d.iii

Inside the callback function of the Post request, we bring in the training data from our mental health program and hit the OpenAI API to fetch the request. If you want to read about the properties inside the openai.createCompletion({}) promise, just visit https://techpro.ninja/openai-codex-with-node-js-tutorial/ and look for OpenAI Playground Settings

Get the full code here

const express = require("express");
const cors = require("cors");

// Import the OpenAI
const { Configuration, OpenAIApi } = require("openai");

// Create a new OpenAI configuration and paste your API key
// obtained from Step 1
// The key displayed here is a fake key
const configuration = new Configuration({
    apiKey: "sk-grcXXXXJn5KZHXXX7rT3BlbkFJtufXz7xErXXXXXXXXW",
});
const openai = new OpenAIApi(configuration);

const bodyParser = require("body-parser");

const app = express();
app.use(cors());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const port = 3000;

// create a post route
app.post("/openai-api", async (req, res) => {
    const data = req.body;

    // Query the OpenAI API
    // Bringing in the training model from Part 1 of the tutorial on mental health
    let promptContext = `Application to precede the mental health diagnosis before going to a psychologist. We will listen to the patient very calmly and try to understand the issue and its root causes. We will keep asking a variety of questions in and around the issue and ultimately try to understand the root cause. Once, we are thorough, we will prepare a summary and suggest a psychologist for further diagnosis. If we are unsure of the given situation, we will answer with a question asking for more details, and only if the patient asks about suicide, we will provide them suicide helpline number
    P: I have a mental health issue 
    A: Alright, we will try to understand the issue and identify the root cause. Once we understand your problem thoroughly, we will try to find a solution. 
    P: I have a very abusive manager at the office 
    A: Can you give me some background about your office culture 
    P: I work in a multi-national company and my manager is not sympathetic toward the problems in team building. Instead, he thinks that these are non-issue and we should only focus on work and compete with each other.`;

    const response = await openai.createCompletion({
        model: "text-davinci-003",
        prompt: `${promptContext} ${data.queryPrompt} ?`,
        temperature: 0,
        max_tokens: 60,
        top_p: 1,
        frequency_penalty: 0,
        presence_penalty: 0,
    });
    console.log(response.data);
    res.json(response.data);
});

app.listen(port, () => {
    console.log(`Node server listening at http://localhost:${port}`);
});
Step 2.d.iv

Run the Node.js server using

node server.js

You should see a node server running at port 3000. At this point, if you have Curl or Postman installed, you can check our API by hitting a post request to http://localhost:3000/openai-api with some post body

POST: http://localhost:3000/openai-api
{"queryPrompt": "Am i ok?"}

You should see some response like below. Note all the properties of the response object, especially model, choices and usage properties.

{
    "id": "cmpl-6Vlt3OJ7wiFyOGjTAIfECEpEO4sAJ",
    "object": "text_completion",
    "created": 1673030265,
    "model": "text-davinci-003",
    "choices": [
        {
            "text": "\n    A: It sounds like you are feeling overwhelmed and stressed out due to the pressure from your manager. We can try to understand the root cause of your stress and suggest a psychologist for further diagnosis.",
            "index": 0,
            "logprobs": null,
            "finish_reason": "stop"
        }
    ],
    "usage": {
        "prompt_tokens": 240,
        "completion_tokens": 41,
        "total_tokens": 281
    }
}

Model: OpenAI models are templates with different processing capabilities and use cases. Choosing a weaker model with lower processing results in lower pricing, and vice versa.

Choices: This is an array of object(s) with the response from the GPT-3 application. It s an array of objects because there can be multiple answers.

Usage: The number of tokens utilized is directly proportional to the cost incurred in consuming the API.

Conclusion

In this tutorial, we have learned how to create a GPT-3 application with Node.js. Also, how we can bring our trained GPT-3 model into our application? Lastly, we saw the response from the API and its parameters. In Part 3 of this guide, we’ll look at how to make our model work best in terms of cost and performance.

Tagged in:

, , ,