Build a GPT Powered Web App

Shreyansh Kotak
Product Designer
Read Time
3 min read
Published On
January 24, 2023

Are you looking to add NLP (natural language processing) functionality to your web application? OpenAI's GPT-3 (Generative Pre-trained Transformer 3) is a powerful tool that can generate human-like text and complete tasks such as translation, summarization, creating original content, and much more. Let's see how you can integrate GPT-3 into your web application.

Prerequisites

Before we begin, it's important to note that GPT-3 is a paid service and requires an API key to access its capabilities. You can sign up for an API key and obtain pricing information on the OpenAI website.

Step 1: Create the User Interface

To start testing the capabilities and interacting with GPT-3, we recommend creating a simple user interface with an input field for the prompt and a button to trigger the API call.

<input type="text" id="user-prompt" name="user-prompt"> <button type="button" id="make-api-call">Make API Call</button>

Step 2: Test the API Call

When the user clicks on the button, we will get the user input and request the server with the user prompt.

We'll use the OpenAI completions method to make the API call and parse in the user-prompt as one of the parameters.

var button = document.getElementById("make-api-call");
button.addEventListener("click", () => {
  var userPrompt = document.getElementById("user-prompt").value;
  
  const body = JSON.stringify({
    model: "text-davinci-003",
    prompt: userPrompt,
    max_tokens: 7,
    temperature: 0,
  });

  fetch('https://api.openai.com/v1/completions',  {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Authorization': 'Bearer $YOUR_API_KEY'
    },
    body: body
  })
    .then(response => response.text())
    .then(result => console.log(result))
    .catch(error => console.log('error', error));
});

This code will make the API using the prompt entered by the user and display the output in the console.

Step 3: Secure API Key

It is a good practice not to store your API key on the client-side code. If you are developing a web app, always remember to secure sensitive information, like API keys, in the back-end. You can fetch the API results from there and then pass them to the front-end.

Step 4: Customize the interface

After testing if the code is working as expected without any security concerns, you can adjust the front end to get additional information from the user for prompts. You can create new fields to display the output from the API on the page instead of the console.

Step 5: Customize the output

You can adjust the code to fetch the information from the interface and display it on the page. You can tweak the response from GPT-3 by adjusting the various parameters passed in the body. For example, you can change the model used by specifying a different value for the model parameter or adjust the length of the generated text by changing the max_tokens parameter. We recommend referring to the OpenAI documentation for more information on the available parameters and how to use them.

Integrating GPT-3 into your web application can add powerful NLP functionality to your application. If you are looking for a more customized integration of GPT-3 into your application, our team at Perpetual has experience integrating GPT-3 and other cutting-edge technologies into applications. Contact us to learn more about how we can help you take advantage of the power of GPT-3 to give your app A.I. superpowers.