Spaces:
Sleeping
Sleeping
File size: 11,517 Bytes
6362b0c |
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 |
"""
Law RAG Chatbot API using FastAPI, Langchain, Groq, and ChromaDB
"""
import os
import time
import logging
from typing import List, Dict, Any, Optional
from fastapi import FastAPI, HTTPException, Depends, Header
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
import uvicorn
from config import *
from rag_system import RAGSystem
from session_manager import SessionManager
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Initialize FastAPI app
app = FastAPI(
title=API_TITLE,
version=API_VERSION,
description=API_DESCRIPTION
)
# Add CORS middleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Pydantic models for API requests/responses
class ChatRequest(BaseModel):
question: str
context_length: int = 5 # Increased default context length
session_id: Optional[str] = None
class ChatResponse(BaseModel):
answer: str
sources: List[Dict[str, Any]]
confidence: float
processing_time: float
question: str
session_id: str
chat_history_count: int
class SessionCreateRequest(BaseModel):
user_info: Optional[str] = None
metadata: Optional[Dict[str, Any]] = None
class SessionResponse(BaseModel):
session_id: str
created_at: str
user_info: str
metadata: Dict[str, Any]
class HealthResponse(BaseModel):
status: str
message: str
components: Dict[str, str]
# Global instances
rag_system: RAGSystem = None
session_manager: SessionManager = None
@app.on_event("startup")
async def startup_event():
"""Initialize the RAG system and session manager on startup"""
global rag_system, session_manager
try:
logger.info("Initializing RAG system...")
rag_system = RAGSystem()
await rag_system.initialize()
logger.info("RAG system initialized successfully")
logger.info("Initializing session manager...")
session_manager = SessionManager()
logger.info("Session manager initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize systems: {e}")
raise
@app.get("/", response_model=HealthResponse)
async def root():
"""Root endpoint with health check"""
return HealthResponse(
status="healthy",
message="Law RAG Chatbot API is running",
components={
"api": "running",
"rag_system": "running" if rag_system else "not_initialized",
"session_manager": "running" if session_manager else "not_initialized",
"vector_db": "connected" if rag_system and rag_system.is_ready() else "disconnected"
}
)
@app.get("/health", response_model=HealthResponse)
async def health_check():
"""Health check endpoint"""
if not rag_system:
raise HTTPException(status_code=503, detail="RAG system not initialized")
if not rag_system.is_ready():
raise HTTPException(status_code=503, detail="RAG system not ready")
if not session_manager:
raise HTTPException(status_code=503, detail="Session manager not initialized")
return HealthResponse(
status="healthy",
message="All systems operational",
components={
"api": "running",
"rag_system": "ready",
"session_manager": "ready",
"vector_db": "connected",
"embeddings": "ready",
"llm": "ready"
}
)
@app.post("/sessions", response_model=SessionResponse)
async def create_session(request: SessionCreateRequest):
"""Create a new chat session"""
if not session_manager:
raise HTTPException(status_code=503, detail="Session manager not initialized")
try:
session_id = session_manager.create_session(
user_info=request.user_info,
metadata=request.metadata
)
session = session_manager.get_session(session_id)
return SessionResponse(
session_id=session_id,
created_at=session["created_at"].isoformat(),
user_info=session["user_info"],
metadata=session["metadata"]
)
except Exception as e:
logger.error(f"Error creating session: {e}")
raise HTTPException(status_code=500, detail=f"Failed to create session: {str(e)}")
@app.get("/sessions/{session_id}", response_model=Dict[str, Any])
async def get_session_info(session_id: str):
"""Get session information and statistics"""
if not session_manager:
raise HTTPException(status_code=503, detail="Session manager not initialized")
try:
session_stats = session_manager.get_session_stats(session_id)
if not session_stats:
raise HTTPException(status_code=404, detail="Session not found")
return session_stats
except HTTPException:
raise
except Exception as e:
logger.error(f"Error getting session info: {e}")
raise HTTPException(status_code=500, detail=f"Failed to get session info: {str(e)}")
@app.get("/sessions/{session_id}/history")
async def get_chat_history(session_id: str, limit: int = 10):
"""Get chat history for a session"""
if not session_manager:
raise HTTPException(status_code=503, detail="Session manager not initialized")
try:
history = session_manager.get_chat_history(session_id, limit)
return {
"session_id": session_id,
"history": history,
"total": len(history)
}
except Exception as e:
logger.error(f"Error getting chat history: {e}")
raise HTTPException(status_code=500, detail=f"Failed to get chat history: {str(e)}")
@app.post("/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
"""Main chat endpoint for legal questions with session support"""
if not rag_system:
raise HTTPException(status_code=503, detail="RAG system not initialized")
if not rag_system.is_ready():
raise HTTPException(status_code=503, detail="RAG system not ready")
if not session_manager:
raise HTTPException(status_code=503, detail="Session manager not initialized")
try:
start_time = time.time()
# Handle session
if not request.session_id:
# Create new session if none provided
request.session_id = session_manager.create_session()
logger.info(f"Created new session: {request.session_id}")
# Verify session exists
session = session_manager.get_session(request.session_id)
if not session:
raise HTTPException(status_code=404, detail="Session not found")
# Get response from RAG system
response = await rag_system.get_response(
question=request.question,
context_length=request.context_length
)
processing_time = time.time() - start_time
# Store chat response in session
session_manager.store_chat_response(
session_id=request.session_id,
question=request.question,
answer=response["answer"],
sources=response["sources"],
confidence=response["confidence"],
processing_time=processing_time
)
# Get chat history count
chat_history = session_manager.get_chat_history(request.session_id, limit=1)
chat_history_count = len(chat_history)
return ChatResponse(
answer=response["answer"],
sources=response["sources"],
confidence=response["confidence"],
processing_time=processing_time,
question=request.question,
session_id=request.session_id,
chat_history_count=chat_history_count
)
except HTTPException:
raise
except Exception as e:
logger.error(f"Error processing chat request: {e}")
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.get("/search")
async def search(query: str, limit: int = 5, session_id: Optional[str] = None):
"""Search for relevant legal documents with optional session tracking"""
if not rag_system:
raise HTTPException(status_code=503, detail="RAG system not initialized")
try:
results = await rag_system.search_documents(query, limit)
# Store search query if session provided
if session_id and session_manager:
session_manager.store_search_query(session_id, query, len(results))
return {
"query": query,
"results": results,
"total": len(results),
"session_id": session_id
}
except Exception as e:
logger.error(f"Error in search: {e}")
raise HTTPException(status_code=500, detail=f"Search failed: {str(e)}")
@app.get("/stats")
async def get_stats():
"""Get system statistics"""
if not rag_system:
raise HTTPException(status_code=503, detail="RAG system not initialized")
try:
rag_stats = await rag_system.get_stats()
# Add session statistics
if session_manager:
# Get total sessions count (this would need to be implemented in session manager)
session_stats = {
"session_manager": "active",
"total_sessions": "available" # Could implement actual count
}
else:
session_stats = {"session_manager": "not_initialized"}
return {**rag_stats, **session_stats}
except Exception as e:
logger.error(f"Error getting stats: {e}")
raise HTTPException(status_code=500, detail=f"Failed to get stats: {str(e)}")
@app.post("/reindex")
async def reindex():
"""Reindex the vector database"""
if not rag_system:
raise HTTPException(status_code=503, detail="RAG system not initialized")
try:
await rag_system.reindex()
return {"message": "Reindexing completed successfully"}
except Exception as e:
logger.error(f"Error in reindexing: {e}")
raise HTTPException(status_code=500, detail=f"Reindexing failed: {str(e)}")
@app.delete("/sessions/{session_id}")
async def delete_session(session_id: str):
"""Delete a session and all its data"""
if not session_manager:
raise HTTPException(status_code=503, detail="Session manager not initialized")
try:
session_manager.delete_session(session_id)
return {"message": f"Session {session_id} deleted successfully"}
except Exception as e:
logger.error(f"Error deleting session: {e}")
raise HTTPException(status_code=500, detail=f"Failed to delete session: {str(e)}")
if __name__ == "__main__":
uvicorn.run(
"app:app",
host=HOST,
port=PORT,
reload=True,
log_level="info"
)
|