Spaces:
Sleeping
Sleeping
File size: 12,889 Bytes
e7586f8 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | # Quick Start Guide: Frontend & Backend
**System**: RBAC-Enforced RAG Chatbot (Groq + SentenceTransformer)
**Date**: March 26, 2026
---
## π Quick Start (2 Minutes)
### Option 1: Using Two Terminal Windows (Recommended)
#### Terminal 1: Start Backend
```bash
cd c:\development\CodeBasics\Bootcamp\Assignment\Assignment1\app\backend
python -m uvicorn main:app --reload
```
**Expected Output:**
```
INFO: Started server process [12345]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000
```
#### Terminal 2: Start Frontend
```bash
cd c:\development\CodeBasics\Bootcamp\Assignment\Assignment1\app\frontend-nextjs
npm run dev
```
**Expected Output:**
```
> next dev
β² Next.js 14.0.0
- Local: http://localhost:3000
- Environments: .env.local
β Ready in 2.3s
```
#### 3. Open Browser
```
http://localhost:3000
```
---
## βοΈ Pre-Startup Checklist
### 1. Backend Already Has GROQ_API_KEY β
```bash
# Check .env file
cat app\backend\.env
```
**Current value:**
```
GROQ_API_KEY=gsk_your_groq_api_key_here
```
β
**Already configured!**
### 2. Frontend Dependencies
```bash
# Navigate to frontend
cd app\frontend-nextjs
# Check if node_modules exists
dir node_modules
# If not, install dependencies
npm install
# Run dev server
npm run dev
```
### 3. Backend Dependencies (If Issues)
```bash
# Navigate to backend
cd app\backend
# Check Python version (must be 3.8+)
python --version
# Option A: Create fresh virtual environment
python -m venv venv
# Activate (Windows)
venv\Scripts\activate
# Option B: Use existing Python
# Install dependencies
pip install -r requirements.txt
```
---
## π Full Setup Process
### Step 1: Setup Backend
```powershell
# Navigate to backend
cd c:\development\CodeBasics\Bootcamp\Assignment\Assignment1\app\backend
# Option A: Fresh environment (RECOMMENDED for clean state)
python -m venv venv
venv\Scripts\activate
# Install all dependencies
pip install -r requirements.txt
# Verify Python syntax (all files)
python -m py_compile main.py
# Start backend
python -m uvicorn main:app --reload
```
### Step 2: Setup Frontend (New Terminal)
```powershell
# Navigate to frontend
cd c:\development\CodeBasics\Bootcamp\Assignment\Assignment1\app\frontend-nextjs
# Install dependencies (if not already done)
npm install
# Start dev server
npm run dev
```
### Step 3: Open in Browser
```
http://localhost:3000
```
---
## π Login with Demo Users
### Available Demo Users
| Username | Password | Role | Access |
|----------|----------|------|--------|
| `emp_john` | `password123` | employee | general docs only |
| `fin_alice` | `password123` | finance | general + finance docs |
| `eng_bob` | `password123` | engineering | general + engineering docs |
| `mkt_sarah` | `password123` | marketing | general + marketing docs |
| `ceo_mary` | `password123` | c_level | **ALL docs** |
**Demo Login Steps:**
1. Go to http://localhost:3000
2. Click "Login"
3. Enter username (e.g., `emp_john`)
4. Enter password: `password123`
5. Click "Login"
**Admin Access:**
```
username: admin
password: admin123
```
---
## π Infrastructure Configuration
| Service | Port | URL | Status |
|---------|------|-----|--------|
| Backend API | 8000 | http://localhost:8000 | β
POST /api/chat |
| Frontend | 3000 | http://localhost:3000 | β
GUI |
| Qdrant Cloud| 6333 | cloud.qdrant.io | β
Persistent Cloud |
---
## π Testing the System
### 1. Test Backend Health
```bash
# In any terminal/PowerShell
curl http://localhost:8000/api/health
```
**Expected Response:**
```json
{"status": "ok", "timestamp": "2026-03-26T..."}
```
### 2. Test Chat Endpoint
```bash
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{
"user_id": "user_001",
"user_role": "finance",
"query": "What is financial policy?"
}'
```
**Expected Response:**
```json
{
"answer": "The financial policy states...",
"sources": ["chunk_001", "chunk_002"],
"route": "finance",
"flags": {"hallucination": false, "pii": false},
"rbac_denied": false
}
```
### 3. Test Frontend
```
Open browser: http://localhost:3000
Login with: emp_john / password123
Chat about available documents
```
---
## β οΈ Troubleshooting
### Backend Won't Start
#### Error: `ModuleNotFoundError: No module named 'pydantic_core'`
**Cause**: Pydantic dependency conflict (environment-specific issue)
**Solution Options:**
**Option 1: Fresh Virtual Environment (RECOMMENDED)**
```powershell
# Remove old env
Remove-Item -Recurse -Force venv
# Create fresh env
python -m venv venv
# Activate
venv\Scripts\activate
# Install
pip install -r requirements.txt
# Start
python -m uvicorn main:app --reload
```
**Option 2: Force Reinstall**
```powershell
pip install --force-reinstall --no-cache-dir -r requirements.txt
```
**Option 3: Use Python in Docker (If Local Issues Persist)**
```bash
# Install Docker
# Then run backend in container
docker build -t finbot-backend .
docker run -p 8000:8000 finbot-backend
```
---
#### Error: `GROQ_API_KEY not found`
**Solution**: Verify .env file
```powershell
# Check .env exists
dir .env
# Check content
type .env
# Should show: GROQ_API_KEY=gsk_...
```
If missing, add it:
```bash
# Get your Groq API key from: https://console.groq.com/keys
# Then edit .env and add:
GROQ_API_KEY=your_key_here
```
---
#### Error: `Port 8000 already in use`
**Solution**: Use different port or kill existing process
```powershell
# Find process using port 8000
netstat -ano | findstr :8000
# Kill process (replace PID with actual number)
taskkill /PID 12345 /F
# Or start on different port
python -m uvicorn main:app --reload --port 8001
```
---
### Frontend Won't Start
#### Error: `npm: command not found`
**Solution**: Install Node.js
```
Download from: https://nodejs.org
Install Node.js (includes npm)
Restart terminal
Verify: npm --version
```
---
#### Error: `Port 3000 already in use`
**Solution**: Kill existing process
```powershell
# Find process using port 3000
netstat -ano | findstr :3000
# Kill process
taskkill /PID 12345 /F
# Or use different port
npm run dev -- --port 3001
```
---
#### Error: `Cannot find module 'next'`
**Solution**: Install dependencies
```bash
cd app\frontend-nextjs
npm install
npm run dev
```
---
### Services Won't Communicate
#### Symptom: Frontend shows "Connection refused" error
**Check:**
1. Backend running on port 8000? β
2. Frontend running on port 3000? β
3. CORS enabled in backend? β
(FastAPI automatically enables)
**Debug**:
```bash
# From frontend terminal, test backend
curl http://localhost:8000/api/health
```
Should return JSON response.
---
## π― Startup Workflow
```
START HERE
β
βΌ
βββββββββββββββββββββββ
β 1. Check Groq Key β
β (.env configured)β
ββββββββββ¬βββββββββββββ
YES β
β
βΌ
βββββββββββββββββββββββ
β 2. Install Backend β
β pip install -r.. β
ββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β 3. Start Backend β
β Terminal 1 β
β port 8000 β
ββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β 4. Install Frontend β
β npm install β
ββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β 5. Start Frontend β
β Terminal 2 β
β port 3000 β
ββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β 6. Open Browser β
β localhost:3000 β
ββββββββββ¬βββββββββββββ
β
βΌ
βββββββββββββββββββββββ
β 7. Login & Chat! β
β emp_john / pwd123 β
βββββββββββββββββββββββ
```
---
## π¦ What Gets Started
### Backend (Port 8000)
**Services Started:**
- β
FastAPI server
- β
Document converter (Docling)
- β
Vector store (Qdrant) - in-memory mode
- β
Embeddings model (SentenceTransformer) - auto-downloaded on first use
- β
Groq LLM API client (uses external API)
**API Endpoints:**
- `POST /api/chat` β Chat with RBAC
- `GET /api/health` β Health check
- `GET /api/users/{username}` β User lookup
- `POST /admin/create-user` β Add user
- `POST /admin/ingest` β Upload documents
### Frontend (Port 3000)
**Next.js Components:**
- β
Login screen with demo users
- β
Chat interface (real-time messages)
- β
User profile display
- β
Document sources viewer
- β
Admin panel (user management)
- β
Safety flags display
---
## π Development Workflow
### Making Changes
#### Backend Code Changes
```
Edit: app/backend/pipeline/rag_pipeline.py
β
[Uvicorn hot-reload active]
β
Backend automatically restarts
β
Test in browser/Postman
```
#### Frontend Code Changes
```
Edit: app/frontend-nextjs/components/ChatInterface.tsx
β
[Next.js hot-reload active]
β
Frontend automatically refreshes
β
Test in browser
```
### No Manual Restart Needed
Both services have **hot-reload** enabled:
- **Backend**: `--reload` flag in uvicorn
- **Frontend**: Built-in Next.js dev server reload
Just save files and changes appear instantly!
---
## π Performance Expectations
### First Run
- **Backend startup**: 3-5 seconds
- **SentenceTransformer download**: 1-2 minutes (first time only, ~500MB)
- **Frontend startup**: 5-10 seconds
- **First chat response**: 2-3 seconds (model needs to initialize)
### Subsequent Runs
- **Backend startup**: 2-3 seconds
- **Frontend startup**: 3-5 seconds
- **Chat response**: 1-1.2 seconds β‘
---
## π‘ Tips & Best Practices
### 1. Use Debug Mode
```bash
# Backend with verbose logging
DEBUG=True python -m uvicorn main:app --reload --log-level debug
```
### 2. Test with curl/Postman
```bash
# Health check
curl http://localhost:8000/api/health
# Chat request
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"user_id": "u1", "user_role": "employee", "query": "test"}'
```
### 3. Watch Logs
Backend logs show:
- Received queries β
- RBAC decisions β
- Groq API calls β
- Error traceback β
### 4. Keep Both Terminals Open
Don't close either terminal while developing:
```
Terminal 1: Backend (port 8000) [Keep Running]
Terminal 2: Frontend (port 3000) [Keep Running]
Terminal 3: Testing/Git commands [Optional]
```
# To reset vector store, delete:
# app/backend/qdrant_storage/
```
---
## π Next Steps After Startup
1. **Ingest Documents** (Admin Panel)
- Navigate to http://localhost:3000/admin
- Click "Ingest Document"
- Upload PDF/DOCX/MD file
- Select collection (general, finance, engineering, etc.)
- Specify accessible roles
2. **Test RBAC**
- Login as `emp_john` (employee)
- Chat about available docs
- Try asking about finance β "No access"
- Login as `fin_alice` (finance)
- Same question β See finance docs β
3. **Monitor System**
- Check backend logs for errors
- Watch RBAC decisions
- Track embedding latency
- Monitor token usage (Groq)
4. **Customize**
- Edit roles in `config.py`
- Adjust chunk size in `hierarchical_chunker.py`
- Change embedding model in `vector_store.py`
- Modify guardrails in `guardrails/`
---
## β
Verification Checklist
After startup, verify:
- [ ] Backend healthcheck: `curl http://localhost:8000/api/health`
- [ ] Frontend loads: `http://localhost:3000`
- [ ] Can login with demo user
- [ ] Chat endpoint responds
- [ ] RBAC filters work (test with different roles)
- [ ] Logs show no critical errors
- [ ] Both terminals show "Running"
---
## π Support
**If Backend Won't Start:**
1. Check `.env` for GROQ_API_KEY
2. Try fresh `venv` (clean install)
3. Verify Python 3.8+
4. Check port 8000 not in use
5. View error logs carefully
**If Frontend Won't Start:**
1. Verify Node.js installed (`node --version`)
2. Try `npm install` again
3. Check port 3000 not in use
4. Clear `.next` cache: `rm -r .next`
**If Services Won't Communicate:**
1. Both running on correct ports?
2. Firewall blocking local traffic?
3. Check CORS headers in browser DevTools
4. Test endpoints with curl
---
**You're all set! π Happy chatting!**
Run the quick start commands above and your RBAC RAG chatbot will be live in minutes!
|