Spaces:
Sleeping
Sleeping
Update src/RAGSample.py
Browse files- src/RAGSample.py +14 -12
src/RAGSample.py
CHANGED
|
@@ -525,9 +525,9 @@ class RAGApplication:
|
|
| 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]
|
|
@@ -535,25 +535,27 @@ class RAGApplication:
|
|
| 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 |
-
|
| 553 |
-
|
| 554 |
-
|
| 555 |
-
|
| 556 |
-
|
|
|
|
|
|
|
| 557 |
|
| 558 |
|
| 559 |
# Main execution block
|
|
|
|
| 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]
|
|
|
|
| 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 |
|
| 560 |
|
| 561 |
# Main execution block
|