Ứng dụng chatbot AI với khả năng hội thoại thông minh, hỗ trợ nhiều tính năng nâng cao như upload file, tìm kiếm ngữ nghĩa, quản lý bộ nhớ hội thoại, và real-time messaging qua WebSocket.
- Tính năng chính
- Công nghệ sử dụng
- Kiến trúc hệ thống
- Cài đặt
- Cấu hình
- Chạy ứng dụng
- API Documentation
- Cấu trúc thư mục
- Tính năng bảo mật
- Testing
- Đóng góp
- License
- AI Chat Interface: Giao diện chat hiện đại với OpenAI GPT-4o-mini
- Real-time Messaging: Sử dụng Socket.IO để truyền tin nhắn real-time
- Streaming Responses: AI responses được stream theo từng chunk
- Multi-conversation Management: Quản lý nhiều cuộc hội thoại đồng thời
- Message Pinning: Ghim tin nhắn quan trọng
- Conversation Search: Tìm kiếm nội dung trong hội thoại
- File Upload: Upload file (PDF, DOCX, images) lên Cloudinary
- Document Processing: Xử lý và trích xuất nội dung từ PDF và DOCX
- Image Analysis: Phân tích và mô tả nội dung hình ảnh với AI Vision
- File Preview: Preview file trực tiếp trong chat
- Semantic Memory: Lưu trữ và truy vấn bộ nhớ dài hạn với vector embeddings
- Context Management: Tự động quản lý context window của AI
- Smart Suggestions: Gợi ý câu hỏi thông minh cho người dùng mới
- Authentication: JWT-based authentication với access & refresh tokens
- User Profiles: Quản lý profile và avatar người dùng
- Preferences: Cài đặt tùy chỉnh (theme, ngôn ngữ, AI model)
- Password Security: Bcrypt hashing cho mật khẩu
- Redis Caching: Cache user data, conversations, và search results
- Rate Limiting: Bảo vệ API khỏi spam và abuse
- Compression: Response compression giảm 70-80% bandwidth
- Database Optimization: Sequelize ORM với PostgreSQL/MySQL
- Helmet.js: Security headers
- CORS Protection: Cấu hình CORS an toàn
- XSS Protection: Sanitize user input
- Request Validation: Validate tất cả input
- Error Tracking: Sentry integration cho monitoring
| Công nghệ | Version | Mô tả |
|---|---|---|
| React | 18.3.1 | UI framework |
| TypeScript | 5.9.3 | Type safety |
| Vite | 7.1.7 | Build tool & dev server |
| Ant Design | 5.27.4 | UI component library |
| React Router | 7.9.3 | Client-side routing |
| Axios | 1.12.2 | HTTP client |
| Socket.IO Client | 4.8.1 | Real-time communication |
| React Syntax Highlighter | 16.1.0 | Code highlighting |
| Sentry | 10.22.0 | Error tracking |
| Công nghệ | Version | Mô tả |
|---|---|---|
| Node.js | ≥18.0.0 | Runtime environment |
| Express | 5.1.0 | Web framework |
| TypeScript | 5.9.3 | Type safety |
| Sequelize | 6.37.7 | ORM for PostgreSQL/MySQL |
| Socket.IO | 4.8.1 | WebSocket server |
| Redis | via ioredis 5.8.1 | Caching & session store |
| OpenAI | 6.3.0 | AI integration |
| Cloudinary | 2.7.0 | File storage |
| JWT | 9.0.2 | Authentication |
| Bcrypt | 6.0.0 | Password hashing |
| Winston | 3.18.3 | Logging |
| Helmet | 8.1.0 | Security middleware |
| Sentry | 10.22.0 | Error tracking & profiling |
- Docker: Containerization (optional)
- Swagger: API documentation
- ESLint & Prettier: Code quality
- Nodemon: Development hot-reload
┌─────────────────┐ ┌─────────────────┐
│ React Client │◄────────┤ Vite Dev │
│ (Port 5173) │ │ Server │
└────────┬────────┘ └─────────────────┘
│
│ HTTP/WebSocket
│
▼
┌─────────────────┐ ┌─────────────────┐
│ Express Server │◄────────┤ Socket.IO │
│ (Port 3000) │ │ Server │
└────────┬────────┘ └─────────────────┘
│
├──────────────┬──────────────┬──────────────┐
▼ ▼ ▼ ▼
┌────────┐ ┌─────────┐ ┌─────────┐ ┌──────────┐
│PostgreSQL │ Redis │ │ OpenAI │ │Cloudinary│
│ MySQL │ │ Cache │ │ API │ │ CDN │
└────────┘ └─────────┘ └─────────┘ └──────────┘
User Input → Client → WebSocket → Server → OpenAI API
↓ ↓
Local State Save to DB
↓ ↓
Real-time UI ← Stream ← Redis Cache
- Node.js ≥18.0.0
- npm ≥9.0.0
- PostgreSQL hoặc MySQL
- Redis (optional, recommended cho production)
- OpenAI API Key
- Cloudinary Account (cho upload file)
git clone https://github.com/Thanh14013/AI_Chatbot_Assistant.git
cd AI_Chatbot_Assistantcd server
npm installcd client
npm installTạo file .env trong thư mục server/:
# ==================== Server Configuration ====================
NODE_ENV=development
PORT=3000
# ==================== Database Configuration ====================
# PostgreSQL
DB_HOST=localhost
DB_PORT=5432
DB_NAME=ai_chatbot_db
DB_USER=your_db_user
DB_PASS=your_db_password
DB_DIALECT=postgres
# MySQL (alternative)
# DB_DIALECT=mysql
# Database sync (chỉ dùng trong development)
DB_SYNC=false
# ==================== Redis Configuration ====================
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DB=0
# ==================== JWT Configuration ====================
JWT_ACCESS_SECRET=your_super_secret_access_key_min_32_chars
JWT_REFRESH_SECRET=your_super_secret_refresh_key_min_32_chars
JWT_ACCESS_EXPIRATION=1h
JWT_REFRESH_EXPIRATION=7d
# ==================== OpenAI Configuration ====================
OPENAI_API_KEY=sk-your-openai-api-key
OPENAI_MODEL=gpt-4o-mini
OPENAI_TEMPERATURE=0.7
OPENAI_MAX_TOKENS=2000
# ==================== Cloudinary Configuration ====================
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret
# ==================== CORS Configuration ====================
CORS_ORIGINS=http://localhost:5173
# ==================== Sentry (Optional) ====================
SENTRY_DSN=your_sentry_dsn
# ==================== File Upload Configuration ====================
MAX_FILE_SIZE=10485760
MAX_FILES_PER_MESSAGE=10Tạo file .env trong thư mục client/:
# API Base URL
VITE_API_BASE_URL=http://localhost:3000/api
# WebSocket URL
VITE_SOCKET_URL=http://localhost:3000
# Sentry (Optional)
VITE_SENTRY_DSN=your_sentry_dsncd server
npm run migrateSet DB_SYNC=true trong file .env và restart server.
cd server
npm run devServer sẽ chạy tại: http://localhost:3000
cd client
npm run devClient sẽ chạy tại: http://localhost:5173
cd server
npm run build
npm startcd client
npm run build
npm run previewBạn có thể sử dụng concurrently hoặc mở 2 terminal riêng biệt.
API documentation được tạo tự động với Swagger UI.
Truy cập tại: http://localhost:3000/docs
POST /api/auth/register- Đăng ký tài khoản mớiPOST /api/auth/login- Đăng nhậpPOST /api/auth/refresh- Refresh access tokenPOST /api/auth/logout- Đăng xuấtPOST /api/auth/change-password- Đổi mật khẩu
GET /api/users/profile- Lấy thông tin profilePUT /api/users/profile- Cập nhật profilePOST /api/users/avatar- Upload avatarGET /api/users/preferences- Lấy preferencesPUT /api/users/preferences- Cập nhật preferences
GET /api/conversations- Lấy danh sách conversationsPOST /api/conversations- Tạo conversation mớiGET /api/conversations/:id- Lấy chi tiết conversationPUT /api/conversations/:id- Cập nhật conversationDELETE /api/conversations/:id- Xóa conversationGET /api/conversations/:id/messages- Lấy tin nhắnPOST /api/conversations/:id/messages- Gửi tin nhắnPUT /api/conversations/:id/messages/:messageId- Cập nhật tin nhắnDELETE /api/conversations/:id/messages/:messageId- Xóa tin nhắnPOST /api/conversations/:id/messages/:messageId/pin- Pin/unpin tin nhắn
GET /api/memory- Lấy danh sách memoriesPOST /api/memory- Tạo memory mớiPUT /api/memory/:id- Cập nhật memoryDELETE /api/memory/:id- Xóa memory
GET /api/search/conversations- Tìm kiếm conversationsPOST /api/search/semantic- Tìm kiếm ngữ nghĩa
POST /api/upload/file- Upload fileDELETE /api/upload/file- Xóa file
GET /api/health- Kiểm tra trạng thái server
join_conversation- Join vào conversationleave_conversation- Leave conversationsend_message- Gửi tin nhắntyping_start- Bắt đầu typingtyping_stop- Dừng typing
message- Nhận tin nhắn mớimessage_update- Tin nhắn được cập nhậtmessage_delete- Tin nhắn bị xóauser_typing- User đang typingconversation_update- Conversation được cập nhậterror- Lỗi xảy ra
server/
├── src/
│ ├── config/ # Configuration files
│ │ ├── constants.ts # App constants
│ │ ├── database.config.ts
│ │ └── redis.config.ts
│ ├── controllers/ # Request handlers
│ │ ├── auth.controller.ts
│ │ ├── conversation.controller.ts
│ │ ├── message.controller.ts
│ │ └── user.controller.ts
│ ├── db/ # Database connection
│ ├── middlewares/ # Express middlewares
│ │ ├── auth.middleware.ts
│ │ ├── generalMiddleware.ts
│ │ └── validation.middleware.ts
│ ├── migrations/ # Database migrations
│ ├── models/ # Sequelize models
│ │ ├── User.model.ts
│ │ ├── Conversation.model.ts
│ │ ├── Message.model.ts
│ │ └── Memory.model.ts
│ ├── routes/ # API routes
│ │ ├── auth.route.ts
│ │ ├── conversation.route.ts
│ │ ├── user.route.ts
│ │ └── index.ts
│ ├── services/ # Business logic
│ │ ├── ai.service.ts
│ │ ├── socket.service.ts
│ │ ├── memory.service.ts
│ │ └── file.service.ts
│ ├── types/ # TypeScript types
│ ├── utils/ # Utility functions
│ │ ├── logger.util.ts
│ │ ├── validation.util.ts
│ │ └── token.util.ts
│ └── index.ts # Entry point
├── logs/ # Application logs
├── package.json
├── tsconfig.json
└── .env
client/
├── src/
│ ├── assets/ # Static assets (images, fonts)
│ ├── components/ # React components
│ │ ├── ChatInput.tsx
│ │ ├── MessageBubble.tsx
│ │ ├── MessageList.tsx
│ │ ├── ConversationList.tsx
│ │ ├── FileAttachmentPreview.tsx
│ │ ├── CodeBlock.tsx
│ │ └── ...
│ ├── config/ # App configuration
│ │ └── api.config.ts
│ ├── hooks/ # Custom React hooks
│ │ ├── useAuth.tsx
│ │ ├── useSocket.ts
│ │ └── useConversation.ts
│ ├── pages/ # Page components
│ │ ├── ChatPage.tsx
│ │ ├── LoginPage.tsx
│ │ └── RegisterPage.tsx
│ ├── services/ # API services
│ │ ├── auth.service.ts
│ │ ├── conversation.service.ts
│ │ └── message.service.ts
│ ├── stores/ # State management
│ │ ├── preferences.store.tsx
│ │ └── sidebar.store.tsx
│ ├── types/ # TypeScript types
│ │ └── index.ts
│ ├── utils/ # Utility functions
│ │ ├── format.util.ts
│ │ └── validation.util.ts
│ ├── App.tsx # Main App component
│ ├── main.tsx # Entry point
│ └── index.css # Global styles
├── public/ # Public assets
├── package.json
├── vite.config.ts
└── .env
- JWT-based authentication với access & refresh tokens
- Secure HTTP-only cookies cho refresh tokens
- Token rotation và revocation
- Password hashing với bcrypt (cost factor: 10)
- Rate limiting (1000 requests/hour)
- Request timeout (30 seconds)
- Body size limits (2MB)
- CORS protection
- Helmet.js security headers
- XSS protection
- SQL injection prevention (Sequelize ORM)
- Environment variables cho sensitive data
- Encrypted database connections
- Secure file upload validation
- Input sanitization và validation
- Sentry error tracking
- Winston logging
- Request logging
- Performance monitoring
cd server
npm run testcd client
npm run test- Follow ESLint configuration
- Write meaningful commit messages
- Add comments cho code phức tạp
- Update documentation khi cần
- Write tests cho features mới
- Initial release
- AI Chat với OpenAI GPT-4o-mini
- Real-time messaging với Socket.IO
- File upload & processing
- Semantic memory system
- User authentication & profiles
- Redis caching
- Swagger API documentation
- WebSocket reconnection có thể gặp issue trên một số mạng restrictive
- File upload lớn hơn 10MB cần increase limit
- Redis cache invalidation cần optimize hơn
Nguyen Vu Thanh
- GitHub: @Thanh14013
- Repository: AI_Chatbot_Assistant