Spaces:
Sleeping
Sleeping
Implementation Plan: UI & Backend Reliability Overhaul
1. Project Goal
To deliver a stable, polished, and professional chat application by:
- Fixing the Backend: Making the API resilient to external service failures.
- Fixing the Frontend: Implementing a high-fidelity UI that accurately matches the provided Figma design.
- Ensuring Reliable Communication: Debugging and fixing backend-frontend connection issues.
2. Key Challenges and Analysis
- Backend Resilience: The API must not crash if an external dependency like the Groq API is unavailable on startup. It needs to handle this gracefully and report the issue to the user through the UI.
- High-Fidelity UI: Translating a design into code requires meticulous attention to detail. The previous attempt failed because it only created the HTML structure without the specific Tailwind CSS classes for spacing, colors, fonts, shadows, and layout.
- Conversational State: The solution must maintain the previously implemented conversational memory.
- Backend-Frontend Communication: The frontend is receiving 500 errors when trying to communicate with the backend, indicating potential issues with API initialization, CORS, or environment configuration.
3. High-level Task Breakdown
Phase 0: Backend-Frontend Connection Debugging (HIGHEST Priority)
- Goal: Fix the 500 errors occurring during frontend-backend communication
- Tasks:
Environment Configuration Check:
- Verify GROQ_API_KEY is properly set in the backend environment
- Ensure all required Python packages are installed and up to date
- Check that the backend server is running on the expected port (8000)
Backend API Verification:
- Add detailed logging to track API request/response cycle
- Implement proper error handling in the
/query
endpoint - Add health check endpoint (
/health
) to verify API status - Test API endpoints independently using curl/Postman
Frontend API Integration:
- Update frontend API call to include proper error handling
- Add retry logic for transient failures
- Implement proper loading states during API calls
- Add detailed console logging for debugging
CORS and Network Configuration:
- Verify CORS configuration is working correctly
- Ensure backend is accessible from frontend origin
- Test with explicit CORS configuration instead of wildcard
- Add proper error messages for CORS issues
Phase 1: Backend Stabilization (Immediate Priority)
- Goal: Ensure the backend API can always start and respond, even if the Groq RAG system fails to initialize.
- Tasks:
- Add
tenacity
library: Updaterequirements.txt
to include thetenacity
library for robust retry logic. - Graceful RAG Initialization: In
src/groq_medical_rag.py
, modify_test_groq_connection
to use@retry
fromtenacity
. This will automatically handle transient network errors during startup. - Fault-Tolerant API: In
src/api/main.py
, modify thelifespan
manager. If the RAG system fails to initialize after all retries, the API should still start but log the critical error. Therag_system
global will remainNone
. - User-Friendly Error Response: In the
/query
endpoint, check ifrag_system
isNone
. If it is, return a200 OK
with a clear JSON error message (e.g.,{ "response": "Sorry, the clinical assistant is currently offline. Please try again later." }
) instead of a503
error. This allows the frontend to handle the error gracefully.
- Add
Phase 2: High-Fidelity Frontend Implementation
- Goal: Overhaul
frontend/src/app/page.tsx
to be a pixel-perfect implementation of the Figma design. - Tasks:
- Add
clsx
utility: Install theclsx
library (npm install clsx
) in thefrontend
directory to make conditional styling cleaner. - Full UI Overhaul (
page.tsx
): Replace the entire component with a new version that meticulously applies Tailwind CSS to match the design.- Layout: Use flexbox to create the main
header
,main
(chat), andfooter
(input) structure. - Welcome Screen:
- Use correct font sizes (
text-5xl
,text-4xl
,text-lg
), weights (font-bold
,font-semibold
), and colors (text-gray-800
,text-gray-600
). - Implement the vertical list of template questions with correct borders, padding, and hover effects.
- Use correct font sizes (
- Chat Form:
- Style the
textarea
andbutton
to exactly match the design, including borders, rounded corners, shadows, and the new "N" icon for the send button.
- Style the
- Chat History:
- Style user and assistant messages as distinct "chat bubbles" with different background colors.
- Ensure the
prose
class is used on the markdown renderer for beautiful typography of the bot's response.
- Layout: Use flexbox to create the main
- Error Handling: Update the
handleSubmit
function in the frontend to gracefully handle the new error message from the backend in case the RAG system is offline.
- Add
4. Project Status Board
- Phase 0: Backend-Frontend Connection Debugging
- Environment Configuration Check
- Verify GROQ_API_KEY
- Update Python dependencies
- Verify server port configuration
- Backend API Verification
- Add detailed logging
- Implement error handling
- Add health check endpoint
- Test API independently
- Frontend API Integration
- Update error handling
- Add retry logic
- Implement loading states
- Add debug logging
- CORS and Network Configuration
- Verify CORS setup
- Test backend accessibility
- Update CORS configuration
- Add error messages
- Environment Configuration Check
- Phase 1: Backend Stabilization
- Add
tenacity
torequirements.txt
. - Implement retry logic in
groq_medical_rag.py
. - Make API startup fault-tolerant in
api/main.py
. - Implement user-friendly error response in
/query
.
- Add
- Phase 2: Frontend UI Overhaul
- Install
clsx
utility. - Re-implement
page.tsx
with high-fidelity styling. - Implement frontend error handling for offline RAG system.
- Install
5. Branch Name
fix/ui-and-backend-stability