This guide will help you migrate your existing MongoDB AI Lab Assistant from a Flask-based Python application to a modern React/Next.js application with MongoDB integration. The new setup provides improved performance, better user experience, and easier deployment on Vercel.
- Modern UI: React with Material UI provides a responsive and intuitive user interface
- Enhanced Performance: Server-side rendering and API routes improve performance and SEO
- Simplified Deployment: Vercel provides seamless deployment with automatic CI/CD
- Better Developer Experience: Hot module reloading, component-based architecture, and improved testing
- Backend: Python Flask with MongoDB integration
- Frontend: Jinja2 templates with some JavaScript
- Deployment: Google Cloud App Engine
- Authentication: Custom authentication or OAuth
- AI Integration: OpenAI API (embeddings and question answering)
- Backend: Next.js API routes with MongoDB integration
- Frontend: React components with Material UI
- Deployment: Vercel (optimized for Next.js)
- Authentication: NextAuth.js with Google OAuth
- AI Integration: Same OpenAI API functionalities
No direct data changes are required as we're keeping MongoDB as the database. The application will continue using the same database collections and schema.
- users: User accounts and authentication data
- documents: Main collection for questions and answers with vector embeddings
- conversations: Chat history between users and the system
- unanswered_questions: Questions that couldn't be answered from the database
- design_reviews: Design review requests and responses
Flask Route | Next.js API Route |
---|---|
/auth/login |
/api/auth/[...nextauth]/route.js |
/chat/question |
/api/chat/route.js |
/admin/questions |
/api/admin/questions/route.js |
/admin/users |
/api/admin/users/route.js |
/design-review |
/api/design-review/route.js |
- Replace Flask authentication with NextAuth.js
- Implement Google OAuth for simplified login
- Maintain admin role capabilities
- Migrate vector search logic to Next.js API routes
- Keep the core OpenAI integration code with minimal changes
- Enhance UI with React components for better interactivity
- Create React-based admin dashboard
- Implement question management interface
- Add user management capabilities
- Build React form for design review submission
- Create admin interface for review management
- Maintain the review workflow and email notifications
Run the provided setup script to create the initial project structure:
bash setup_script.sh
This creates the Next.js application with all necessary directories, components, and configuration files.
Configure your environment variables in .env.local
:
# MongoDB Connection
MONGODB_URI=mongodb+srv://user:[email protected]/dbname
MONGODB_DB=ai_lab_assistant
# Authentication
GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
NEXTAUTH_SECRET=generate-a-random-secret
NEXTAUTH_URL=http://localhost:3000
# OpenAI
OPENAI_API_KEY=your-openai-api-key
# Application Settings
SIMILARITY_THRESHOLD=0.91
Ensure your MongoDB Atlas cluster is configured with Vector Search capability:
- Create a vector search index on your
documents
collection - Index configuration for question embeddings:
{ "fields": [ { "type": "vector", "path": "question_embedding", "numDimensions": 1536, "similarity": "cosine" } ] }
-
Start the development server:
npm run dev
-
Test each feature thoroughly:
- User authentication
- Question answering
- Vector search functionality
- Admin dashboard features
- Design review workflow
-
Compare results with your Flask application to ensure consistency
-
Push your code to a GitHub repository
-
Connect to Vercel:
- Create a Vercel account if you don't have one
- Import your repository
- Configure environment variables in Vercel dashboard
- Deploy the application
-
Set up custom domain if needed
- Set up Vercel Analytics for front-end performance monitoring
- Configure MongoDB Atlas monitoring for database performance
- Implement error tracking with a service like Sentry
- Regular dependency updates
- New feature development using React components
- Performance optimizations
- UI/UX improvements
- Next.js Documentation
- Material UI Documentation
- NextAuth.js Documentation
- MongoDB Node.js Driver
- Vercel Documentation
- Ensure your IP address is whitelisted in MongoDB Atlas
- Check connection string in environment variables
- Verify network connectivity
- Check Google OAuth configuration
- Verify NextAuth.js setup and callbacks
- Ensure NEXTAUTH_SECRET is properly set
- Confirm vector search index is properly created
- Verify embedding dimensions match (1536 for OpenAI embeddings)
- Check similarity threshold configuration
- Verify all environment variables are set in Vercel
- Check build logs for any errors
- Ensure Node.js version compatibility