Spaces:
Sleeping
Sleeping
Update src/RAGSample.py
Browse files- src/RAGSample.py +35 -35
src/RAGSample.py
CHANGED
@@ -119,9 +119,9 @@ class SmartFAQRetriever(BaseRetriever):
|
|
119 |
return self._k
|
120 |
|
121 |
def get_documents_with_confidence(self, query: str) -> List[dict]:
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
|
126 |
|
127 |
|
@@ -522,38 +522,38 @@ class RAGApplication:
|
|
522 |
# traceback.print_exc()
|
523 |
# return f"I apologize, but I encountered an error processing your question: {str(e)}. Please try rephrasing it or ask a different question."
|
524 |
def run(self, question: str) -> str:
|
525 |
-
|
526 |
-
|
527 |
-
|
528 |
-
|
529 |
-
|
530 |
-
|
531 |
-
|
532 |
-
|
533 |
-
|
534 |
-
|
535 |
-
|
536 |
-
|
537 |
-
|
538 |
-
|
539 |
-
|
540 |
-
|
541 |
-
|
542 |
-
|
543 |
-
|
544 |
-
|
545 |
-
|
546 |
-
|
547 |
-
|
548 |
-
|
549 |
-
|
550 |
-
|
551 |
-
|
552 |
-
|
553 |
-
|
554 |
-
|
555 |
-
|
556 |
-
|
557 |
|
558 |
|
559 |
# Main execution block
|
|
|
119 |
return self._k
|
120 |
|
121 |
def get_documents_with_confidence(self, query: str) -> List[dict]:
|
122 |
+
"""Return top documents and their confidence (similarity) scores."""
|
123 |
+
results = self._get_relevant_documents_with_scores(query)
|
124 |
+
return [{"document": doc.page_content, "confidence": round(score, 3)} for doc, score in results]
|
125 |
|
126 |
|
127 |
|
|
|
522 |
# traceback.print_exc()
|
523 |
# return f"I apologize, but I encountered an error processing your question: {str(e)}. Please try rephrasing it or ask a different question."
|
524 |
def run(self, question: str) -> str:
|
525 |
+
try:
|
526 |
+
if not question.strip():
|
527 |
+
return "Please provide a valid question."
|
528 |
+
|
529 |
+
print(f"\nProcessing question: '{question}'")
|
530 |
+
|
531 |
+
if hasattr(self.retriever, "get_documents_with_confidence"):
|
532 |
+
docs_with_scores = self.retriever.get_documents_with_confidence(question)
|
533 |
+
documents = [Document(page_content=d["document"]) for d in docs_with_scores]
|
534 |
+
confidence_info = "\n".join([f"- Score: {d['confidence']}, Snippet: {d['document'][:100]}..." for d in docs_with_scores])
|
535 |
+
else:
|
536 |
+
documents = self.retriever.invoke(question)
|
537 |
+
confidence_info = "Confidence scoring not available."
|
538 |
+
|
539 |
+
print(f"Retrieved {len(documents)} documents")
|
540 |
+
print(confidence_info)
|
541 |
+
|
542 |
+
doc_texts = "\n\n".join([doc.page_content for doc in documents])
|
543 |
+
if len(doc_texts) > 500:
|
544 |
+
doc_texts = doc_texts[:500] + "..."
|
545 |
+
|
546 |
+
answer = self.rag_chain.invoke({"question": question, "documents": doc_texts})
|
547 |
+
|
548 |
+
# Append confidence footer
|
549 |
+
footer = "\n\n(Note: This answer is based on documents with confidence scores. Review full context if critical.)"
|
550 |
+
return answer.strip() + footer
|
551 |
+
|
552 |
+
except Exception as e:
|
553 |
+
print(f"Error in RAG application: {str(e)}")
|
554 |
+
import traceback
|
555 |
+
traceback.print_exc()
|
556 |
+
return f"I apologize, but I encountered an error processing your question: {str(e)}. Please try rephrasing it or ask a different question."
|
557 |
|
558 |
|
559 |
# Main execution block
|