kn29 commited on
Commit
026e386
Β·
verified Β·
1 Parent(s): 9d74d67

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -49
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 not DB:
141
  raise ValueError("Database not connected")
142
 
143
- # 1. Load session metadata
144
- session_doc = DB.sessions.find_one({"session_id": session_id})
145
- if not session_doc:
146
- raise ValueError(f"Session {session_id} not found in database")
147
-
148
- # Check session status
149
- if session_doc.get("status") != "completed":
150
- raise ValueError(f"Session not ready - status: {session_doc.get('status')}")
151
-
152
- # 2. Load chunks with embeddings from MongoDB
153
- session_logger.info(f"Loading chunks for: {session_doc.get('filename', 'unknown')}")
154
- chunks_cursor = DB.chunks.find({"session_id": session_id}).sort("created_at", 1)
155
- chunks_list = list(chunks_cursor)
156
-
157
- if not chunks_list:
158
- raise ValueError(f"No chunks found for session {session_id}")
 
159
 
160
- session_logger.info(f"Found {len(chunks_list)} chunks with pre-computed embeddings")
161
 
162
- # 3. Create SessionRAG instance
163
- groq_api_key = os.getenv("GROQ_API_KEY")
164
- session_rag = SessionRAG(session_id, groq_api_key)
165
-
166
- # 4. Load existing session data (rebuilds indices from stored embeddings)
167
- session_logger.info(f"Rebuilding search indices from existing embeddings...")
168
- session_rag.load_existing_session_data(chunks_list)
169
-
170
- # 5. Create session store object
171
- session_store = {
172
- "session_rag": session_rag,
173
- "indexed": True,
174
- "metadata": {
175
- "session_id": session_id,
176
- "title": session_doc.get("filename", "Document"),
177
- "chunk_count": len(chunks_list),
178
- "loaded_at": datetime.utcnow(),
179
- "document_info": {
180
- "filename": session_doc.get("filename", "Unknown"),
181
- "upload_date": session_doc.get("created_at")
 
 
 
 
182
  }
183
  }
184
- }
185
-
186
- session_logger.info("βœ“ Session loaded successfully with existing embeddings")
187
- return session_store
 
 
 
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 not DB:
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)