* This blog post is a summary of this video.

Build Your Own Chatbot with OpenAI's ChatGPT API

Table of Contents

Set up the Client-Side Next.js App

To get started, we first need to create a new Next.js app that will serve as the client-side interface for our chatbot. We'll use the create-next-app command to generate a new Next.js project:

npx create-next-app client

This will create a new Next.js app in a folder called client. We'll also configure Tailwind CSS for styling.

Install and Configure Tailwind CSS

In the client folder, we'll install Tailwind CSS and its peer dependencies: npm install -D tailwindcss postcss autoprefixer Next, we'll generate the Tailwind config file: npx tailwindcss init -p In tailwind.config.js, we'll add the Inter font family we want to use: module.exports = { content: [], theme: { extend: { fontFamily: { inter: ['Inter', 'sans-serif'] } } }, plugins: [], } We also need to add the Tailwind directives to ./styles/globals.css: @tailwind base; @tailwind components; @tailwind utilities;

Design the Chat UI Components

Based on the Figma design, we'll break the UI into components:

  • Header - contains logo and title
  • Messages - main area to display chat messages
  • Input - text field for user to type message
  • Send - button to send message

Add Dummy Chat Messages

To start, we'll add some dummy hardcoded chat messages to see the UI: const [messages, setMessages] = useState([ { message: 'Hi there!' time: formatRelative(new Date(), new Date()), type: 'bot' }, { message: 'Hello, how can I help you?' time: formatRelative(new Date(), new Date()), type: 'user' }, ]);

Style Chat Messages

We'll style the messages using flexbox and conditionally changing colors/alignment based on whether it's a bot or user message. For example: div className={flex ${type === 'bot' ? 'justify-start' : 'justify-end'}} This will right align user messages and left align bot messages. We'll also tweak the border radius so messages have rounded edges on the appropriate corners.

Create Input Form

The input form will contain:

  • Text input for message
  • Send button
  • Submit handler to push new message to message state We'll use controlled components to capture the message input value. The submit handler will push a new message object to the messages array.

Create Koa.js Server and OpenAI Configuration

Next, we need to create a Koa server that will handle the API calls to the OpenAI API. We'll initialize a new Node.js project and install Koa:

npm init -y

npm install koa koa-router koa-bodyparser dotenv openai

We'll also create a .env file to store our OpenAI API key.

Set Up Routes and Middleware

In index.js, we'll import Koa, define routes with koa-router, and add bodyParser middleware to parse request bodies. const Koa = require('koa'); const router = require('koa-router')(); const bodyParser = require('koa-bodyparser'); const app = new Koa(); app.use(bodyParser()); routes go here...

Generate OpenAI API Key

We need to generate an API key from the OpenAI dashboard to authorize our requests:

  1. Go to https://platform.openai.com
  2. Click on 'Personal' > 'View API Keys'
  3. Generate a new API key
  4. Save it in the .env file as OPENAI_API_KEY

Create /message Route

We'll create a /message route that will use the OpenAI node SDK to call the completions endpoint: router.post('/message', async ctx => { const { message } = ctx.request.body; const response = await openai.createCompletion({ model: 'text-davinci-003' prompt: message, max_tokens: 100 }); ctx.body = { message: response.data.choices[0].text }; }); This will send the user's message to OpenAI, and return the generated response.

Connect Frontend to Backend

Finally, we need to connect our Next.js frontend to the Koa backend API. We'll make the /message API call when the user sends a message and display the response.

Send User Message to Server

In our submit handler, we'll make a POST request to /message with the user's input: const response = await axios.post('/message', { message: userMessage }); const botMessage = response.data.message;

Display Chatbot Response

We'll push the returned botMessage to our messages state to display it: setMessages(prev => [... prev, { message: botMessage, time: formatRelative(new Date(), new Date()), type: 'bot' } ]); This will add the bot's reply after the user's message.

Add Animations with Framer Motion

To make the chat conversation more dynamic, we can add some simple animations when messages are sent/received using Framer Motion: <motion.div initial={{ opacity: 0, y: 20 }} animate={{ opacity: 1, y: 0 }}

New message here! </motion.div> This will make messages slide down into view.

FAQ

Q: How do I customize my chatbot's responses?
A: You can customize responses by changing the OpenAI model, temperature, and max tokens in your Node.js backend code. Refer to OpenAI's documentation for details.

Q: What is the cost to run my chatbot?
A: It depends on usage, but costs around $0.002 USD per 1,000 tokens generated. OpenAI provides some free tokens to get started.

Q: Can I deploy this chatbot online?
A: Yes, you can deploy the Next.js frontend and Node.js backend to any hosting provider like Vercel, Netlify, or Render.

Q: What is the best OpenAI model for a chatbot?
A: The DaVinci text model provides the highest quality responses for chatbots and conversational agents.

Q: Do I need a GPU to run this chatbot?
A: No, you can run this chatbot on regular compute servers without a GPU since inference is handled by OpenAI's API.

Q: How can I customize the chatbot's name and avatar?
A: Update the files in the /components/head folder to change the logo SVG asset and chatbot name text.

Q: What is the difference between temperature and max tokens?
A: Temperature controls response creativity. Max tokens limits length. Higher temperature increases risk, lower makes responses more defined.

Q: What front-end framework is used in this project?
A: Next.js is used for the React-based front-end with server-side rendering capabilities.

Q: What back-end framework options does this support?
A: The video uses Koa.js but Express.js can be easily substituted for the back-end API.

Q: Can I extend this chatbot with a database?
A: Yes, you can add a database like MongoDB to store conversations, user profiles, and bot memory.