title | description | image |
---|---|---|
How to use GPT-3 tutorial: Build your own GPT-3 Powered application using NextJS and Replit in 20 minutes |
In this tutorial you will build your own GPT-3 Powered application in 20 minutes |
By the end of this GPT-3 tutorial, you will have built and deployed your very own web application that is powered by GPT-3. You will be able to use the GPT-3 library to build your own applications. The tutorial is suitable for everyone who will assume that you have some basic knowledge of React and NextJS. If you are not familiar with these technologies, you may want to check out the following resources before getting started
We will be using GPT-3 from OpenAI, a 175 Billion parameter text transformer. Additonally we will be using NextJS and Replit to build and host our application. The purpose of this tutorial is to cover the basics in getting a web application powered by GPT-3 up and running.
We start with creating an account at Replit where we will import and initializing our project This will allow us to get a working build so that we can see our updates in realtime.
The first thing you will need to do is create a new Replit project. To do this, head over to Replit and sign up for an account if you do not already have one.
Once you logged into replit click the + sign in the top right corner
In this tutorial we will be using the GPT-3 boilerplate that will make our lives easier and allow you to build your own GPT-3 powered application in 20 minutes.
- Click Import from Github
- Past
https://github.com/nextgrid/GPT-3-Nextjs-Tailwind-starter
in the GitHub URL field - Click the Import from Github button
First thing we need to do is to add our GPT-3 API key from OpenAI. If you do not have one, you can get one by signing up at from beta.openai.com. Once your account is created, you can go to your profile and click the "API Keys" tab or simply click this link OpenAI API Keys.
- Click the lock 🔒 icon on the right side
- In the key field write
OPENAI_API_KEY
- In the value field write your GPT-3 API key that should look something like
sk-tKyMX1XbOS***Agaa5qx89UToRCy
To start our application we need to update the run command.
- In the run command field, change the command to
npm run start
tonpm run dev
- Click the Done button
Now it's time to install all the dependencies and start the development environment. Click the big green
Once you clicked the RUN button you will see a output in the concolse window where it first will install packages and then start out development server
npm run dev
> [email protected] dev
> next dev
ready - started server on 0.0.0.0:3000, url: http://localhost:3000
event - compiled client and server successfully in 14.5s (173 modules)
wait - compiling...
event - compiled successfully in 729 ms (146 modules)
Attention: Next.js now collects completely anonymous telemetry regarding usage.
This information is used to shape Next.js' roadmap and prioritize features.
You can learn more, including how to opt-out if you'd not like to participate in this anonymous program, by visiting the following URL:
https://nextjs.org/telemetry
wait - compiling / (client and server)...
event - compiled client and server successfully in 6.2s (414 modules)
wait - compiling /api/openai...
event - compiled successfully in 69 ms (36 modules)
Once this is done you will see your webapp in the window above the console
Once you updated your run command you can click the RUN button to install your depndencies and start the development environment.
Right now we have a simple frontend connected to our GPT-3 powered backend. If you enter a text into the text field you will see the generated response. Let's try to enter a text and see what happens.
Enter a text into the text field and click the create button
Your input:
What is the distance to the moon from earth?
GPT-3 Output:
The distance to the moon from earth is about 384,400 kilometers.
At this stage you should have a GPT-3 powered application that you can use to generate text and now it's time to configure the application to our needs. In my case I will create a GPT-3 powered application that help to summarize a article that we can use as our copy for a social media post.
Start with open up /pages/api/openai.js
The file should look something like this
const OpenAI = require("openai-api");
const openai = new OpenAI(process.env.OPENAI_API_KEY);
export default async (req, res) => {
// Prompt values
const beforePrompt = ``;
const afterPrompt = ``;
const breakPoint = `\n\n'''\n\n`;
// Construct the prompt
let prompt = `${beforePrompt} ${breakPoint} ${req.body.name} ${breakPoint} ${afterPrompt}`;
// Log prompt
console.log(prompt);
// Call OpenAI API
const gptResponse = await openai.complete({
engine: "text-davinci-002",
prompt: `${prompt}`,
maxTokens: 1500,
temperature: 0.7,
topP: 1,
presencePenalty: 0,
frequencyPenalty: 0.5,
bestOf: 1,
n: 1,
});
res.status(200).json({ text: `${gptResponse.data.choices[0].text}` });
};
To make our life easier I have added the following values
// Prompt values
const beforePrompt = ``;
const afterPrompt = ``;
const breakPoint = `\n\n'''\n\n`;
// Construct the prompt
let prompt = `${beforePrompt} ${breakPoint} ${req.body.name} ${breakPoint} ${afterPrompt}`;
The $beforePrompt
and $afterPrompt
will make it easy for us to add the values we want to appear before and after our prompt before it's send to GPT-3 API.
As I am building a social media post generator I will use the following values
const beforePrompt = `Summarize this into a engaging social media post:`;
const afterPrompt = `Engaging social media post:`;
Open /pages/index.js
and edit the <title>
, H1
and <p>
tags to reflect our applications functionalities
I have choosen following article that I found on Techcrunch to try the application with.
Instagram has just announced a new feature that allows businesses to boost their Reels to turn them into ads! This is a great way to reach new audiences and get your brand story out there. Remember to check your Insights to see which ads performed the best.
Hope you enjoyed this simple tutorial. Hopefully you now have a simple fundation that will allow you to build other type of applications.