File size: 12,063 Bytes
989089c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException, Depends, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from typing import Dict, List, Optional, Any
from datetime import datetime, timedelta
import jwt
import os
import uvicorn

# Import our existing modules
from enhanced_rag_system import EnhancedRAGSystem
from auth_system import AuthSystem

app = FastAPI(
    title="FinSolve RAG API",
    description="Role-Based Access Control RAG System API",
    version="1.0.0",
    docs_url="/docs",
    redoc_url="/redoc"
)

# CORS middleware for web app integration
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # In production, specify your Streamlit URL
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# JWT configuration
JWT_SECRET_KEY = os.getenv("JWT_SECRET_KEY", "your-secret-key-change-in-production")
JWT_ALGORITHM = "HS256"
JWT_EXPIRATION_DELTA = timedelta(hours=24)

# Security
security = HTTPBearer()

# Global instances
rag_system = None
auth_system = None

@app.on_event("startup")
async def startup_event():
    """Initialize systems on startup"""
    global rag_system, auth_system
    print("πŸš€ Starting FastAPI RAG System...")
    
    # Initialize authentication system
    auth_system = AuthSystem()
    print("βœ… Authentication system initialized")
    
    # Initialize RAG system
    rag_system = EnhancedRAGSystem()
    rag_system.initialize_system()
    print("βœ… RAG system initialized")

# Pydantic models
class LoginRequest(BaseModel):
    username: str
    password: str

class QueryRequest(BaseModel):
    query: str

class FeedbackRequest(BaseModel):
    query: str
    response: str
    rating: int

class TokenResponse(BaseModel):
    access_token: str
    token_type: str
    user_info: Dict

class QueryResponse(BaseModel):
    response: str
    sources: List[str]
    visualization: Optional[str] = None
    table: Optional[str] = None
    processing_time: float
    query_intent: str

class UserInfo(BaseModel):
    username: str
    role: str
    full_name: str
    department: str

class HealthResponse(BaseModel):
    status: str
    timestamp: str
    components: Dict[str, bool]
    version: str

# Utility functions
def create_access_token(username: str, role: str) -> str:
    """Create JWT access token"""
    payload = {
        "username": username,
        "role": role,
        "exp": datetime.utcnow() + JWT_EXPIRATION_DELTA,
        "iat": datetime.utcnow()
    }
    token = jwt.encode(payload, JWT_SECRET_KEY, algorithm=JWT_ALGORITHM)
    return token

def verify_token(credentials: HTTPAuthorizationCredentials = Depends(security)) -> Dict:
    """Verify JWT token and return user info"""
    try:
        token = credentials.credentials
        payload = jwt.decode(token, JWT_SECRET_KEY, algorithms=[JWT_ALGORITHM])
        username = payload.get("username")
        role = payload.get("role")
        
        if username is None or role is None:
            raise HTTPException(
                status_code=status.HTTP_401_UNAUTHORIZED,
                detail="Invalid token",
                headers={"WWW-Authenticate": "Bearer"},
            )
        
        return {"username": username, "role": role}
        
    except jwt.ExpiredSignatureError:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Token expired",
            headers={"WWW-Authenticate": "Bearer"},
        )
    except jwt.JWTError:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid token",
            headers={"WWW-Authenticate": "Bearer"},
        )

# API Endpoints

@app.get("/", response_model=Dict)
async def root():
    """Root endpoint"""
    return {
        "message": "FinSolve RAG API",
        "version": "1.0.0",
        "status": "active",
        "docs": "/docs"
    }

@app.get("/health", response_model=HealthResponse)
async def health_check():
    """Health check endpoint"""
    system_status = rag_system.get_system_status() if rag_system else {}
    
    return HealthResponse(
        status="healthy" if system_status.get("system_initialized", False) else "unhealthy",
        timestamp=datetime.now().isoformat(),
        components={
            "rag_system": system_status.get("system_initialized", False),
            "auth_system": auth_system is not None,
            "vector_store": system_status.get("vector_store_available", False),
            "llm": system_status.get("llm_available", False)
        },
        version="1.0.0"
    )

# Authentication endpoints
@app.post("/auth/login", response_model=TokenResponse)
async def login(login_data: LoginRequest):
    """Authenticate user and return access token"""
    if not auth_system.authenticate(login_data.username, login_data.password):
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Invalid credentials"
        )
    
    user_info = auth_system.get_user_info(login_data.username)
    access_token = create_access_token(login_data.username, user_info["role"])
    
    return TokenResponse(
        access_token=access_token,
        token_type="bearer",
        user_info=user_info
    )

@app.get("/auth/me", response_model=UserInfo)
async def get_current_user(current_user: Dict = Depends(verify_token)):
    """Get current user information"""
    user_info = auth_system.get_user_info(current_user["username"])
    if not user_info:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail="User not found"
        )
    
    return UserInfo(**user_info, username=current_user["username"])

# Chat endpoints
@app.post("/chat/query", response_model=QueryResponse)
async def process_query(query_data: QueryRequest, current_user: Dict = Depends(verify_token)):
    """Process RAG query"""
    if not rag_system:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="RAG system not initialized"
        )
    
    start_time = datetime.now()
    
    try:
        response, sources, visualization, table = rag_system.query(
            query_data.query, 
            current_user["role"]
        )
        
        processing_time = (datetime.now() - start_time).total_seconds()
        query_intent = rag_system._classify_query_intent(query_data.query)
        
        return QueryResponse(
            response=response,
            sources=sources,
            visualization=visualization,
            table=table,
            processing_time=processing_time,
            query_intent=query_intent
        )
        
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Error processing query: {str(e)}"
        )

@app.post("/chat/feedback")
async def submit_feedback(feedback_data: FeedbackRequest, current_user: Dict = Depends(verify_token)):
    """Submit feedback for a response"""
    if not rag_system:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="RAG system not initialized"
        )
    
    if not 1 <= feedback_data.rating <= 5:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Rating must be between 1 and 5"
        )
    
    try:
        rag_system.store_feedback(
            feedback_data.query,
            feedback_data.response,
            feedback_data.rating,
            current_user["role"]
        )
        
        return {"message": "Feedback submitted successfully"}
        
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Error storing feedback: {str(e)}"
        )

# Document endpoints
@app.get("/documents/accessible")
async def get_accessible_documents(current_user: Dict = Depends(verify_token)):
    """Get documents accessible to current user"""
    if not rag_system:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="RAG system not initialized"
        )
    
    try:
        accessible_docs = auth_system.get_accessible_documents(current_user["role"])
        doc_info = rag_system.get_available_documents_for_role(current_user["role"])
        
        return {
            "role": current_user["role"],
            "accessible_documents": accessible_docs,
            "document_details": doc_info
        }
        
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Error retrieving documents: {str(e)}"
        )

# System endpoints
@app.get("/system/status")
async def get_system_status(current_user: Dict = Depends(verify_token)):
    """Get system status (admin only)"""
    if current_user["role"] not in ["C-Level", "Engineering"]:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Insufficient privileges"
        )
    
    if not rag_system:
        raise HTTPException(
            status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
            detail="RAG system not initialized"
        )
    
    try:
        status = rag_system.get_system_status()
        
        # Add API-specific metrics
        status["api_metrics"] = {
            "fastapi_version": "Real FastAPI Instance",
            "endpoints_available": len(app.routes),
            "middleware_count": len(app.middleware_stack),
            "startup_complete": True
        }
        
        return status
        
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Error retrieving system status: {str(e)}"
        )

@app.get("/system/metrics")
async def get_system_metrics(current_user: Dict = Depends(verify_token)):
    """Get detailed system metrics (admin only)"""
    if current_user["role"] not in ["C-Level", "Engineering"]:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="Insufficient privileges"
        )
    
    return {
        "api_info": {
            "title": app.title,
            "version": app.version,
            "description": app.description,
            "total_routes": len(app.routes),
            "middleware_stack": len(app.middleware_stack)
        },
        "system_health": {
            "rag_system": rag_system is not None,
            "auth_system": auth_system is not None,
            "timestamp": datetime.now().isoformat()
        },
        "endpoints": [
            {
                "path": route.path,
                "methods": list(route.methods) if hasattr(route, 'methods') else ["GET"],
                "name": route.name
            }
            for route in app.routes
            if hasattr(route, 'path')
        ]
    }

# Error handlers
@app.exception_handler(HTTPException)
async def http_exception_handler(request, exc):
    """Custom HTTP exception handler"""
    return JSONResponse(
        status_code=exc.status_code,
        content={
            "error": exc.detail,
            "status_code": exc.status_code,
            "timestamp": datetime.now().isoformat()
        }
    )

@app.exception_handler(Exception)
async def general_exception_handler(request, exc):
    """General exception handler"""
    return JSONResponse(
        status_code=500,
        content={
            "error": "Internal server error",
            "detail": str(exc),
            "timestamp": datetime.now().isoformat()
        }
    )

if __name__ == "__main__":
    # For development - in production, use a proper ASGI server
    uvicorn.run(
        "api:app",
        host="0.0.0.0",
        port=8000,
        reload=True,
        log_level="info"
    )