We will summarize the steps to run the Next.js OpenAI Starter template, a sample Chatbot that works with ChatGPT, in this issue.
![](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F4aTWDln1IINt8XtrLnZfmL%2F29a400e2962241bc8c837e8ca85b7230%2Fvolodymyr-hryshchenko-V5vqWC9gyEU-unsplash.jpg&w=3840&q=75)
Introduce Templates
The template we will be using is for a Next.js application that works with ChatGPT's API. Details can be found at the following page.
This application provides the following features
- Next.js App Router
- Set an API KEY for Open AI to be able to chat
AI Chat Bot, which includes authentication, is also available.
In this case, it includes integration with GitHub using NextAuth. I'll try to use it when I have more time, but let's start with a simple Chat app.
Get an API key for ChatGPT
The API key for ChatGPT is required for deployment. To obtain this key, you will need to generate a key from the following page
When actually generated, you can see the key registered in the following form.
![openaikey01.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2FQzWiq6iuQwkEMm0A7OVpj%2Fc045930156d37827c56a2d6450536902%2Fopenaikey01.png&w=1920&q=75)
You can get a key, but you will have to pay a fee for using the key. This is separate from the ChatGPT Plus service, which costs $20 per month, and is calculated on a pay-as-you-go basis.
As for credits, the amount of $5 is free for the first 3 months of account creation, but my account was at $0. So, I charge it with a credit card so that I can use about $10.
![openaikey02.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F23VhqdesumVudEbGaeaxcn%2F857f21a0d18e1001e6b1351cf6395e9e%2Fopenaikey02.png&w=1920&q=75)
By turning off auto-charge, you can rest assured that you will not be in a situation where you spend too much while trying out the system.
Deploy to Vercel
Let's quickly deploy the file to Vercel. First, the readme of the sample page has a button for extracting to Vercel.
![openaikey03.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F5oWBcM8BTKcuXJfzfYQzeV%2F8b0cb33bd4eb9904e6b337bc92a9962e%2Fopenaikey03.png&w=3840&q=75)
Clicking on the "Vercel" button will take you to the Vercel screen. First, set the name of the GitHub repository you wish to link. In this case, we will create a private repository.
![openaikey04.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F1CMLDVb6VBwgj2QTYqFY8K%2F4ee2e07070c5a8aced88788eebeb8279%2Fopenaikey04.png&w=3840&q=75)
The next step is to set the environment variables, which can be done simply by setting the API key.
![openaikey05.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F24pvZaAayoQZtsiZ49pdBz%2F1b4e5a6af4c04eddf49cf3182aef2815%2Fopenaikey05.png&w=1920&q=75)
After a few moments, the deployment is complete and the site is up and running.
![openaikey06.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2FiMdjpGlrbFkRjk5WrwJ8M%2F11a08533cc84f6a6a4527767c9dea4b1%2Fopenaikey06.png&w=2048&q=75)
When we actually accessed the site, we were able to confirm that chatting is now possible.
![openaikey07.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F47Y918uJYhPUDvx1x8bqAu%2F7a6bd5e20970e91ed5147171b2f2fb15%2Fopenaikey07.png&w=1920&q=75)
Run locally
It is already working in the Vercel environment, but we would like to have a local working environment. For this code, we have created a private repository, so it is deployed as follows.
![openaikey08.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F58MxQxAe6Z2HLLktwGHDX2%2F9d6f8de9bf6d4db725164d2c8cbbe2c8%2Fopenaikey08.png&w=1920&q=75)
Extract the code from this repository locally and create an .env.local file to set the API key.
![openaikey09.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F3sPW06BNijOJNvnqXgdOku%2Fd94147c9a5448e475002e1480c93c3c7%2Fopenaikey09.png&w=1920&q=75)
You are now ready to go. We will execute the commands as follows
npm install
npm run dev
![openaikey10.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2F6MYWsJqiBkfGOuAkdoqEu2%2Fd70d95c26a3ac8dbc6ed135c2b61842f%2Fopenaikey10.png&w=1920&q=75)
I was able to actually access localhost:3000 and confirm that the chat was working.
![openaikey11.png](/_next/image?url=https%3A%2F%2Fimages.ctfassets.net%2Fzi1ovlormku8%2FaIFhUtYCt8NnXGBq1X7Yy%2F67c4e351947d0172b1393aec4ab7c939%2Fopenaikey11.png&w=3840&q=75)
Summary
In this case, we simply ran a sample application to be able to talk to ChatGTP via the API. It is helpful to use this kind of chat sample as a base so that it is easy to create mocks.