uumerrr684 commited on
Commit
3d66778
Β·
verified Β·
1 Parent(s): f127ccc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -237,7 +237,7 @@ class ProductionRAGSystem:
237
  status_text.empty()
238
  return False
239
 
240
- def search(self, query, n_results=3):
241
  """Search for relevant chunks"""
242
  if not self.model or not self.collection:
243
  return None
@@ -266,6 +266,12 @@ class ProductionRAGSystem:
266
  'similarity': similarity
267
  })
268
 
 
 
 
 
 
 
269
  return search_results
270
  except Exception as e:
271
  st.error(f"Search error: {e}")
@@ -746,10 +752,16 @@ if prompt := st.chat_input("Ask questions about your documents..."):
746
  with st.chat_message("assistant"):
747
  if rag_system and rag_system.model and rag_system.get_collection_count() > 0:
748
  # Search documents first
749
- search_results = rag_system.search(prompt, n_results=3)
 
 
 
 
 
 
750
 
751
- # Check if we found relevant documents (lower threshold for better coverage)
752
- if search_results and search_results[0]['similarity'] > 0.005: # Even lower threshold
753
  # Generate document-based answer
754
  result = rag_system.generate_answer(
755
  prompt,
@@ -775,14 +787,15 @@ if prompt := st.chat_input("Ask questions about your documents..."):
775
  if use_ai_enhancement and not result['has_both']:
776
  st.info("πŸ’‘ AI enhancement failed - showing extracted answer from documents")
777
 
778
- # Show RAG info
779
  if show_sources and result['sources']:
780
  confidence_text = f"{result['confidence']*100:.1f}%" if show_confidence else ""
781
  st.markdown(f"""
782
  <div class="rag-attribution">
783
  <strong>πŸ“ Sources:</strong> {', '.join(result['sources'])}<br>
784
  <strong>🎯 Confidence:</strong> {confidence_text}<br>
785
- <strong>πŸ“Š Found:</strong> {len(search_results)} relevant sections
 
786
  </div>
787
  """, unsafe_allow_html=True)
788
 
@@ -799,8 +812,12 @@ if prompt := st.chat_input("Ask questions about your documents..."):
799
  }
800
 
801
  else:
802
- # No relevant documents found - use general AI
803
- st.info("No relevant documents found. Using general AI mode...")
 
 
 
 
804
  general_response = get_general_ai_response(prompt, unlimited_tokens=unlimited_tokens)
805
  st.markdown(f"πŸ’¬ **General AI:** {general_response}")
806
 
 
237
  status_text.empty()
238
  return False
239
 
240
+ def search(self, query, n_results=5): # Increased from 3 to 5
241
  """Search for relevant chunks"""
242
  if not self.model or not self.collection:
243
  return None
 
266
  'similarity': similarity
267
  })
268
 
269
+ # Debug: Show search results for troubleshooting
270
+ print(f"Search for '{query}' found {len(search_results)} results")
271
+ for i, result in enumerate(search_results[:3]):
272
+ print(f" {i+1}. Similarity: {result['similarity']:.3f} | Source: {result['metadata']['source_file']}")
273
+ print(f" Content preview: {result['content'][:100]}...")
274
+
275
  return search_results
276
  except Exception as e:
277
  st.error(f"Search error: {e}")
 
752
  with st.chat_message("assistant"):
753
  if rag_system and rag_system.model and rag_system.get_collection_count() > 0:
754
  # Search documents first
755
+ search_results = rag_system.search(prompt, n_results=5)
756
+
757
+ # Debug output for troubleshooting
758
+ if search_results:
759
+ st.info(f"πŸ” Found {len(search_results)} potential matches. Best similarity: {search_results[0]['similarity']:.3f}")
760
+ else:
761
+ st.warning("πŸ” No search results returned from vector database")
762
 
763
+ # Check if we found relevant documents (very low threshold)
764
+ if search_results and search_results[0]['similarity'] > 0.001: # Ultra-low threshold
765
  # Generate document-based answer
766
  result = rag_system.generate_answer(
767
  prompt,
 
787
  if use_ai_enhancement and not result['has_both']:
788
  st.info("πŸ’‘ AI enhancement failed - showing extracted answer from documents")
789
 
790
+ # Show RAG info with more details
791
  if show_sources and result['sources']:
792
  confidence_text = f"{result['confidence']*100:.1f}%" if show_confidence else ""
793
  st.markdown(f"""
794
  <div class="rag-attribution">
795
  <strong>πŸ“ Sources:</strong> {', '.join(result['sources'])}<br>
796
  <strong>🎯 Confidence:</strong> {confidence_text}<br>
797
+ <strong>πŸ“Š Found:</strong> {len(search_results)} relevant sections<br>
798
+ <strong>πŸ” Best Match:</strong> {search_results[0]['similarity']:.3f} similarity
799
  </div>
800
  """, unsafe_allow_html=True)
801
 
 
812
  }
813
 
814
  else:
815
+ # No relevant documents found - show debug info
816
+ if search_results:
817
+ st.warning(f"πŸ“„ Found documents but similarity too low (best: {search_results[0]['similarity']:.3f}). Using general AI...")
818
+ else:
819
+ st.warning("πŸ“„ No documents found in search. Using general AI...")
820
+
821
  general_response = get_general_ai_response(prompt, unlimited_tokens=unlimited_tokens)
822
  st.markdown(f"πŸ’¬ **General AI:** {general_response}")
823