OpenAI Chat (GPT)

AI-powered chat system using OpenAI GPT models for Express apps. Provides a simple REST API for intelligent conversation.

aiopenaigptexpresschatnatural-languagenodejs
v1.0.0AI

Overview

A lightweight and developer-ready AI chat component built on OpenAI’s GPT models. Provides a clean Express REST API for chat and can easily integrate with any frontend like Next.js, React, or Vue. Supports configurable models, JSON responses, and secure environment variable setup.

Installation

CLI
npx backternity add ai-openai-chat

What This Does

Installs a ready-to-use OpenAI chat system for Express.js applications. Handles API routing, request processing, and GPT responses via OpenAI SDK.

Files & Folders Created

File / PathDescription
/src/config/openai.jsInitializes the OpenAI client using API key from environment variables.
/src/services/chatService.jsContains logic to send user messages and retrieve AI responses.
/src/routes/chat.jsExposes POST /api/ai/chat for chat requests.
/server.jsExpress server setup integrating the chat route.
/.env.exampleConfigures OpenAI API key and model name.

Files to be modified

File / PathDescription
server.jsAdds /api/ai/chat route and JSON parsing middleware.
.envAdds OpenAI configuration variables.

Configuration

# OpenAI Configuration
OPENAI_API_KEY=your-openai-api-key
MODEL_NAME=gpt-4o-mini
PORT=3001

Frontend Integration

Integrate the Q&A endpoint into your frontend using fetch or a Next.js client component. Below is a working example using Next.js with Tailwind styling.

POST/api/ai/qna

Sends a user question to the server and retrieves an AI-generated answer.

Example

1// app/ai/AskAI.jsx (Next.js Example) 2'use client'; 3import { useState } from 'react'; 4 5export default function AskAI() { 6 const [question, setQuestion] = useState(''); 7 const [answer, setAnswer] = useState(''); 8 const [loading, setLoading] = useState(false); 9 10 async function handleAsk() { 11 if (!question.trim()) return; 12 setLoading(true); 13 const res = await fetch('/api/ai/qna', { 14 method: 'POST', 15 headers: { 'Content-Type': 'application/json' }, 16 body: JSON.stringify({ question }) 17 }); 18 const data = await res.json(); 19 setAnswer(data.answer); 20 setLoading(false); 21 } 22 23 return ( 24 <div className="p-4 bg-black/40 border border-white/10 rounded-lg"> 25 <h3 className="text-lg font-semibold mb-3 text-white">💬 Ask the AI</h3> 26 <textarea 27 className="w-full p-2 text-sm rounded bg-black/20 border border-white/10 text-white focus:outline-none" 28 rows={3} 29 placeholder="Ask a question..." 30 value={question} 31 onChange={(e) => setQuestion(e.target.value)} 32 /> 33 <button 34 onClick={handleAsk} 35 disabled={loading} 36 className="mt-3 px-4 py-2 rounded bg-primary text-white text-sm hover:bg-primary/90 transition" 37 > 38 {loading ? 'Thinking...' : 'Ask'} 39 </button> 40 {answer && ( 41 <div className="mt-4 text-sm text-foreground border-t border-white/10 pt-2"> 42 <strong className="text-emerald-400">AI:</strong> {answer} 43 </div> 44 )} 45 </div> 46 ); 47}

Usage

1// Direct Service Example 2const { askQuestion } = require('./src/services/qnaService'); 3 4(async () => { 5 const answer = await askQuestion('What is quantum computing?'); 6 console.log('AI Answer:', answer); 7})();

Get in Touch

For partnerships, collaborations, or custom backend components - let’s build something powerful together.

Send us a message

Reach us directly

Email

team@backternity.dev

Quick Answers

Are components free?

Yes, they’re absolutely free to use.

Do you offer custom work?

Absolutely, we build scalable backend systems tailored to your stack.

How often are updates released?

Monthly, with new components, optimizations, and features.

OpenAI Chat (GPT) – AI Component | Backternity