|
| 1 | +# 🤖 Chatbot Response Engine |
| 2 | + |
| 3 | +## How It Works |
| 4 | + |
| 5 | +The chatbot uses **OpenAI GPT-3.5-turbo** for intelligent responses to user queries. When OpenAI API is unavailable, it falls back to **predefined responses** for common questions. |
| 6 | + |
| 7 | +### Response Flow: |
| 8 | +1. User sends a message |
| 9 | +2. System categorizes the message (greeting, pricing, support, features) |
| 10 | +3. Analyzes sentiment (positive, negative, neutral) |
| 11 | +4. Calls OpenAI API if available, otherwise uses predefined responses |
| 12 | +5. Saves conversation to database |
| 13 | +6. Returns response to user |
| 14 | + |
| 15 | +### Message Categories: |
| 16 | +- **greeting**: hello, hi, hey |
| 17 | +- **pricing**: price, cost, pricing |
| 18 | +- **support**: help, support, issue, problem |
| 19 | +- **features**: feature, capability, what can |
| 20 | +- **default**: other queries |
| 21 | + |
| 22 | +### Predefined Responses: |
| 23 | +When OpenAI is unavailable, the chatbot uses simple predefined responses: |
| 24 | +```javascript |
| 25 | +greeting: "Hello! How can I help you today?" |
| 26 | +pricing: "Our pricing plans start at $9.99/month..." |
| 27 | +support: "I'm here to help! Can you describe the issue?" |
| 28 | +features: "Our platform offers mass email sending..." |
| 29 | +``` |
| 30 | + |
| 31 | +## Setup |
| 32 | + |
| 33 | +1. Add OpenAI API key to `backend/.env`: |
| 34 | +```env |
| 35 | +OPENAI_API_KEY=your_openai_api_key_here |
| 36 | +``` |
| 37 | + |
| 38 | +2. Install dependencies: |
| 39 | +```bash |
| 40 | +cd backend && npm install |
| 41 | +cd ../frontend && npm install |
| 42 | +``` |
| 43 | + |
| 44 | +3. Start servers: |
| 45 | +```bash |
| 46 | +# Backend |
| 47 | +cd backend && npm run dev |
| 48 | + |
| 49 | +# Frontend |
| 50 | +cd frontend && npm run dev |
| 51 | +``` |
| 52 | + |
| 53 | +4. Visit `/chatbot` or use the chat widget |
| 54 | + |
| 55 | +## API Endpoints |
| 56 | + |
| 57 | +- `POST /api/chatbot/message` - Send message, get response |
| 58 | +- `GET /api/chatbot/history/:userId` - Get chat history |
| 59 | +- `POST /api/chatbot/test` - Test chatbot |
| 60 | + |
| 61 | +## Files Created |
| 62 | + |
| 63 | +**Backend:** |
| 64 | +- `services/chatbotService.js` - Core AI logic |
| 65 | +- `controllers/chatbotController.js` - API handlers |
| 66 | +- `routes/chatbotRoutes.js` - Routes |
| 67 | +- `models/Message.js` - Database schema |
| 68 | + |
| 69 | +**Frontend:** |
| 70 | +- `pages/Chatbot.jsx` - Chat interface |
| 71 | +- `components/ChatbotWidget.jsx` - Embeddable widget |
0 commit comments