naveen07garg commited on
Commit
77883d1
·
verified ·
1 Parent(s): 3bac23c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -1
app.py CHANGED
@@ -606,13 +606,29 @@ def wait_for_key(key_name="OPENAI_API_KEY", timeout=10):
606
  # =============================
607
  # Step 5: Chat Function
608
  # =============================
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609
  def chat_fn(message, history):
610
  global retriever
611
  wait_for_key()
612
  if retriever is None:
613
  return "⚠️ Retriever not initialized. Please rebuild or check vector DB."
614
  answer = generate_rag_response(message, retriever)
615
- return f"{answer}\n\n🧠 (Context retrieved from {pdf_path})"
616
 
617
 
618
  # =============================
 
606
  # =============================
607
  # Step 5: Chat Function
608
  # =============================
609
+
610
+ def format_answer(result):
611
+ answer = result["answer"]
612
+ sources = result.get("sources", [])
613
+
614
+ formatted_sources = "\n".join([
615
+ f"- {s['document']} → {s['section']} / {s['subsection']} / {s['subsubsection']}"
616
+ for s in sources
617
+ ])
618
+
619
+ return f"""{answer}
620
+
621
+ 📄 **Sources**
622
+ {formatted_sources}
623
+ """
624
+
625
  def chat_fn(message, history):
626
  global retriever
627
  wait_for_key()
628
  if retriever is None:
629
  return "⚠️ Retriever not initialized. Please rebuild or check vector DB."
630
  answer = generate_rag_response(message, retriever)
631
+ return format_answer #f"{answer}\n\n🧠 (Context retrieved from {pdf_path})"
632
 
633
 
634
  # =============================