Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -237,7 +237,7 @@ class ProductionRAGSystem:
|
|
| 237 |
status_text.empty()
|
| 238 |
return False
|
| 239 |
|
| 240 |
-
def search(self, query, n_results=
|
| 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=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 750 |
|
| 751 |
-
# Check if we found relevant documents (
|
| 752 |
-
if search_results and search_results[0]['similarity'] > 0.
|
| 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 -
|
| 803 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|