Instructions for building and maintaining a Node.js RAG (Retrieval-Augmented Generation) customer support system with Pinecone vector database, OpenAI, and real-time chat.
Build and maintain a complete RAG (Retrieval-Augmented Generation) customer support system with Node.js, Pinecone vector database, OpenAI GPT, and real-time chat capabilities.
This is a full-stack RAG system with the following architecture:
Follow this standard structure:
```
src/
├── config/ # Configurations (database, environment variables)
├── controllers/ # Controllers (currently unused)
├── middleware/ # Middleware (auth, rate limiting, error handling)
├── models/ # Data models (currently unused)
├── routes/ # API routes (auth, chat, documents, admin)
├── services/ # Core services (chat, documents, openai, socket)
└── utils/ # Utilities (logger, helpers)
```
Ensure these environment variables are configured:
```
OPENAI_API_KEY=your_openai_api_key
PINECONE_API_KEY=your_pinecone_api_key
JWT_SECRET=your_jwt_secret
```
1. **Use Modern JavaScript Patterns**
- Use async/await for all asynchronous operations
- Avoid callback hell
- Use Promise.all() for parallel operations when appropriate
2. **Implement Comprehensive Logging**
- Use Winston for structured logging
- Log all important operations (auth, document processing, chat queries)
- Include context: user ID, request ID, timestamps
- Use appropriate log levels (error, warn, info, debug)
3. **Follow Error Handling Best Practices**
- Use centralized error handling middleware
- Create custom error classes for different error types
- Never expose internal errors to clients
- Log detailed errors server-side
- Return user-friendly error messages
4. **Implement Input Validation**
- Use express-validator for all API endpoints
- Validate file uploads (type, size)
- Sanitize user inputs
- Validate JWT tokens properly
5. **Apply Rate Limiting**
- Implement rate limiting middleware
- Different limits for different endpoints (e.g., stricter for AI queries)
- Return clear error messages when limits are exceeded
6. **Socket.io Best Practices**
- Authenticate socket connections using JWT
- Implement connection/disconnection handlers
- Handle errors gracefully
- Emit typed events with clear naming conventions
- Implement reconnection logic on client side
#### OpenAI Service
#### Document Service
#### Chat Service
```javascript
// 1. Receive file upload
// 2. Validate file type and size
// 3. Extract text content
// 4. Split into chunks (e.g., 500 words with 50-word overlap)
// 5. Generate embeddings for each chunk
// 6. Store in Pinecone with metadata:
// - documentId, chunkIndex, text, source, uploadedBy, timestamp
// 7. Return success response with document ID
```
```javascript
// 1. Receive user question via WebSocket or API
// 2. Generate embedding for question
// 3. Query Pinecone for top K similar chunks (e.g., K=5)
// 4. Construct prompt with system instructions + context chunks + question
// 5. Call OpenAI GPT with constructed prompt
// 6. Stream response back to user
// 7. Log query and response for analytics
```
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/rag-customer-support-system/raw