cmd0160 commited on
Commit
2fb7875
·
1 Parent(s): 0438c70
Files changed (1) hide show
  1. app.py +54 -10
app.py CHANGED
@@ -314,16 +314,60 @@ class AbaloneRAGApp:
314
  st.error("Model not initialized. Please wait for the knowledge base and model to be ready or rebuild the vectorstore.")
315
  return
316
 
317
- if getattr(self, 'use_kg', False):
318
- result = answer_with_kg(
319
- self.chain,
320
- styled_question,
321
- prior_history,
322
- persist_dir=self.persist_dir,
323
- kg_hops=self.kg_hops,
324
- )
325
- else:
326
- result = self.chain({"question": styled_question, "chat_history": prior_history})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
 
328
  answer = (
329
  result.get("answer")
 
314
  st.error("Model not initialized. Please wait for the knowledge base and model to be ready or rebuild the vectorstore.")
315
  return
316
 
317
+ # Call the chain with a safe retry: if the underlying vectorstore is corrupted or missing
318
+ # (for example, Chroma raises an internal HNSW/disk error), attempt one automatic rebuild
319
+ # and retry. This avoids crashing the Streamlit app in deployed environments.
320
+ attempted_rebuild = False
321
+ last_exception = None
322
+ while True:
323
+ try:
324
+ if getattr(self, 'use_kg', False):
325
+ result = answer_with_kg(
326
+ self.chain,
327
+ styled_question,
328
+ prior_history,
329
+ persist_dir=self.persist_dir,
330
+ kg_hops=self.kg_hops,
331
+ )
332
+ else:
333
+ result = self.chain({"question": styled_question, "chat_history": prior_history})
334
+ break
335
+ except Exception as e:
336
+ # Keep the exception for logging and potential re-raise after a failed retry
337
+ last_exception = e
338
+ # If we've already attempted a rebuild, give up and show an error
339
+ if attempted_rebuild:
340
+ st.error("Retrieval error: failed to query the knowledge base. Try rebuilding the vectorstore manually.")
341
+ # Optionally show the exception text for debugging
342
+ st.exception(e)
343
+ # Stop processing this user input
344
+ return
345
+
346
+ # Attempt an automatic rebuild and retry once
347
+ attempted_rebuild = True
348
+ st.warning("Detected retrieval backend issue — attempting to rebuild the vectorstore and retry...")
349
+ try:
350
+ run_ingest_cli(data_dir=self.data_dir, persist_dir=self.persist_dir)
351
+ except Exception as rebuild_err:
352
+ st.error("Automatic rebuild failed; please rebuild manually from the sidebar or CLI.")
353
+ st.exception(rebuild_err)
354
+ return
355
+ # Clear cached retriever and chain and reload
356
+ try:
357
+ build_or_load_retriever_cached.clear()
358
+ get_chain_cached.clear()
359
+ self.chain = get_chain_cached(
360
+ model_name=self.model_name,
361
+ top_k=self.top_k,
362
+ retrieval_mode=self.retrieval_mode,
363
+ data_dir=self.data_dir,
364
+ persist_dir=self.persist_dir,
365
+ )
366
+ except Exception as reload_err:
367
+ st.error("Failed to reload the QA chain after rebuilding the vectorstore.")
368
+ st.exception(reload_err)
369
+ return
370
+ # loop will retry once
371
 
372
  answer = (
373
  result.get("answer")