"AI-Powered Chatbot Assistant (like ChatGPT)"
Use Vercel for seamless serverless deployment.
Use OpenAI’s GPT API to power the chatbot intelligence.
Build frontend with Next.js (React framework optimized for Vercel).
Add real-time chat UI with conversation history.
Optional: Add user authentication (e.g., NextAuth.js).
Optional: Add context memory per session.
Complete Guide to Build a ChatGPT-like Bot on Vercel Prerequisites: Node.js installed
OpenAI API key (sign up at https://platform.openai.com/)
Vercel account (https://vercel.com/signup)
Basic knowledge of React and Next.js
Step 1: Initialize Next.js Project bash Copy Edit npx create-next-app@latest chatgpt-vercel cd chatgpt-vercel Step 2: Install dependencies bash Copy Edit npm install openai axios Step 3: Setup OpenAI API Client Create a file lib/openai.js:
js Copy Edit import { Configuration, OpenAIApi } from "openai";
const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY, });
export const openai = new OpenAIApi(configuration); Step 4: Create API Route in Next.js to call OpenAI Create /pages/api/chat.js:
js Copy Edit import { openai } from "../../lib/openai";
export default async function handler(req, res) { if (req.method !== "POST") { return res.status(405).json({ error: "Method not allowed" }); }
const { prompt } = req.body;
if (!prompt) { return res.status(400).json({ error: "Prompt is required" }); }
try { const completion = await openai.createChatCompletion({ model: "gpt-4o-mini", // or "gpt-4" or "gpt-3.5-turbo" messages: [{ role: "user", content: prompt }], max_tokens: 500, });
const response = completion.data.choices[0].message.content;
res.status(200).json({ response });
} catch (error) { res.status(500).json({ error: error.message || "Something went wrong" }); } } Step 5: Build Frontend Chat UI (/pages/index.js) jsx Copy Edit import { useState } from "react";
export default function Home() { const [input, setInput] = useState(""); const [messages, setMessages] = useState([]);
async function handleSubmit(e) { e.preventDefault(); if (!input.trim()) return;
const userMessage = { role: "user", content: input };
setMessages((prev) => [...prev, userMessage]);
setInput("");
const res = await fetch("/api/chat", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ prompt: input }),
});
const data = await res.json();
if (data.response) {
setMessages((prev) => [...prev, { role: "bot", content: data.response }]);
}
}
return ( <div style={{ maxWidth: 600, margin: "auto", padding: 20 }}> <h1>ChatGPT on Vercel</h1> <div style={{ minHeight: 300, border: "1px solid #ccc", padding: 10 }}> {messages.map((msg, idx) => ( <p key={idx} style={{ textAlign: msg.role === "user" ? "right" : "left" }}> <b>{msg.role === "user" ? "You:" : "Bot:"}</b> {msg.content} </p> ))} </div> <form onSubmit={handleSubmit}> <input type="text" value={input} onChange={(e) => setInput(e.target.value)} placeholder="Type your message" style={{ width: "80%" }} /> <button type="submit" style={{ width: "18%" }}> Send </button> </form> </div> ); } Step 6: Setup Environment Variables Create a .env.local file at root:
ini Copy Edit OPENAI_API_KEY=your_openai_api_key_here Step 7: Run Locally bash Copy Edit npm run dev Visit http://localhost:3000 and test the chat!
Step 8: Deploy to Vercel Push your code to GitHub.
Login to https://vercel.com and import your GitHub repo.
Set environment variables in Vercel Dashboard (OPENAI_API_KEY).
Deploy your app.
Your ChatGPT clone is live!
Bonus: Prompt to Start the Chatbot Here’s a prompt you can test with:
"Explain the concept of decentralized finance (DeFi) in simple terms."
If you want, I can also generate a ready-to-use GitHub repo structure or extend this with advanced features like context memory, user auth, or voice integration.
Loading...
Loading...