Update app.py
Browse files
app.py
CHANGED
|
@@ -128,63 +128,66 @@ def init_rag_models():
|
|
| 128 |
return False
|
| 129 |
|
| 130 |
def load_session_from_mongodb(session_id: str) -> Dict[str, Any]:
|
| 131 |
-
"""
|
| 132 |
-
Load session data from MongoDB:
|
| 133 |
-
1. Check if session exists in DB
|
| 134 |
-
2. Load all chunks with pre-computed embeddings
|
| 135 |
-
3. Create SessionRAG instance and rebuild indices from existing embeddings
|
| 136 |
-
"""
|
| 137 |
session_logger = create_session_logger(session_id)
|
| 138 |
session_logger.info(f"Loading session from MongoDB: {session_id}")
|
| 139 |
|
| 140 |
-
if
|
| 141 |
raise ValueError("Database not connected")
|
| 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 |
def get_or_load_session(session_id: str) -> Dict[str, Any]:
|
| 190 |
"""
|
|
@@ -377,7 +380,7 @@ async def chat_with_document(session_id: str, request: ChatRequest):
|
|
| 377 |
@app.get("/history/{session_id}")
|
| 378 |
async def get_session_history(session_id: str):
|
| 379 |
"""Get chat history for a session from MongoDB"""
|
| 380 |
-
if
|
| 381 |
raise HTTPException(status_code=503, detail="Database not connected")
|
| 382 |
|
| 383 |
history = await asyncio.to_thread(get_chat_history_safely, session_id)
|
|
|
|
| 128 |
return False
|
| 129 |
|
| 130 |
def load_session_from_mongodb(session_id: str) -> Dict[str, Any]:
|
| 131 |
+
"""Load session data from MongoDB with better error handling"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
session_logger = create_session_logger(session_id)
|
| 133 |
session_logger.info(f"Loading session from MongoDB: {session_id}")
|
| 134 |
|
| 135 |
+
if DB is None: # β
Fixed
|
| 136 |
raise ValueError("Database not connected")
|
| 137 |
|
| 138 |
+
try:
|
| 139 |
+
# 1. Load session metadata
|
| 140 |
+
session_doc = DB.sessions.find_one({"session_id": session_id})
|
| 141 |
+
if not session_doc:
|
| 142 |
+
raise ValueError(f"Session {session_id} not found in database")
|
| 143 |
+
|
| 144 |
+
# Check session status
|
| 145 |
+
if session_doc.get("status") != "completed":
|
| 146 |
+
raise ValueError(f"Session not ready - status: {session_doc.get('status')}")
|
| 147 |
+
|
| 148 |
+
# 2. Load chunks with embeddings from MongoDB
|
| 149 |
+
session_logger.info(f"Loading chunks for: {session_doc.get('filename', 'unknown')}")
|
| 150 |
+
chunks_cursor = DB.chunks.find({"session_id": session_id}).sort("created_at", 1)
|
| 151 |
+
chunks_list = list(chunks_cursor)
|
| 152 |
+
|
| 153 |
+
if not chunks_list:
|
| 154 |
+
raise ValueError(f"No chunks found for session {session_id}")
|
| 155 |
|
| 156 |
+
session_logger.info(f"Found {len(chunks_list)} chunks with pre-computed embeddings")
|
| 157 |
|
| 158 |
+
# 3. Create SessionRAG instance
|
| 159 |
+
groq_api_key = os.getenv("GROQ_API_KEY")
|
| 160 |
+
|
| 161 |
+
# Make sure to import from the correct module
|
| 162 |
+
from rag_optimized import OptimizedSessionRAG # Or whatever your actual import is
|
| 163 |
+
session_rag = OptimizedSessionRAG(session_id, groq_api_key)
|
| 164 |
+
|
| 165 |
+
# 4. Load existing session data (rebuilds indices from stored embeddings)
|
| 166 |
+
session_logger.info(f"Rebuilding search indices from existing embeddings...")
|
| 167 |
+
session_rag.load_existing_session_data(chunks_list)
|
| 168 |
+
|
| 169 |
+
# 5. Create session store object
|
| 170 |
+
session_store = {
|
| 171 |
+
"session_rag": session_rag,
|
| 172 |
+
"indexed": True,
|
| 173 |
+
"metadata": {
|
| 174 |
+
"session_id": session_id,
|
| 175 |
+
"title": session_doc.get("filename", "Document"),
|
| 176 |
+
"chunk_count": len(chunks_list),
|
| 177 |
+
"loaded_at": datetime.utcnow(),
|
| 178 |
+
"document_info": {
|
| 179 |
+
"filename": session_doc.get("filename", "Unknown"),
|
| 180 |
+
"upload_date": session_doc.get("created_at")
|
| 181 |
+
}
|
| 182 |
}
|
| 183 |
}
|
| 184 |
+
|
| 185 |
+
session_logger.info("β Session loaded successfully with existing embeddings")
|
| 186 |
+
return session_store
|
| 187 |
+
|
| 188 |
+
except Exception as e:
|
| 189 |
+
session_logger.error(f"Failed to load session from MongoDB: {e}", exc_info=True)
|
| 190 |
+
raise ValueError(f"Failed to load session {session_id}: {str(e)}")
|
| 191 |
|
| 192 |
def get_or_load_session(session_id: str) -> Dict[str, Any]:
|
| 193 |
"""
|
|
|
|
| 380 |
@app.get("/history/{session_id}")
|
| 381 |
async def get_session_history(session_id: str):
|
| 382 |
"""Get chat history for a session from MongoDB"""
|
| 383 |
+
if DB is None: # β
Correct way to check
|
| 384 |
raise HTTPException(status_code=503, detail="Database not connected")
|
| 385 |
|
| 386 |
history = await asyncio.to_thread(get_chat_history_safely, session_id)
|