Spaces:
Sleeping
Sleeping
Update rag_service.py
Browse files- rag_service.py +11 -15
rag_service.py
CHANGED
|
@@ -152,7 +152,7 @@ class RAGService:
|
|
| 152 |
)
|
| 153 |
|
| 154 |
def ask_question(self, question: str):
|
| 155 |
-
|
| 156 |
try:
|
| 157 |
# Check if RAG retrieval finds relevant context
|
| 158 |
context_length = self._check_context_length(question)
|
|
@@ -171,10 +171,10 @@ class RAGService:
|
|
| 171 |
# First, try to get a normal response
|
| 172 |
try:
|
| 173 |
response = self.chain_with_sources.invoke(question)
|
| 174 |
-
result = response
|
| 175 |
|
| 176 |
-
# Check if response
|
| 177 |
-
if self.
|
| 178 |
return self._generate_counter_questions(question, context_text)
|
| 179 |
|
| 180 |
return result
|
|
@@ -192,19 +192,15 @@ class RAGService:
|
|
| 192 |
print(f"Error in ask_question: {e}")
|
| 193 |
return f"I encountered an error processing your question. Could you please rephrase it more clearly?"
|
| 194 |
|
| 195 |
-
def
|
| 196 |
-
"""Check if the response
|
| 197 |
-
|
|
|
|
|
|
|
|
|
|
| 198 |
return True
|
| 199 |
|
| 200 |
-
|
| 201 |
-
ambiguous_indicators = [
|
| 202 |
-
"i'm not sure", "unclear", "ambiguous", "could you clarify",
|
| 203 |
-
"multiple interpretations", "not specific enough", "vague"
|
| 204 |
-
]
|
| 205 |
-
|
| 206 |
-
response_lower = response.lower()
|
| 207 |
-
return any(indicator in response_lower for indicator in ambiguous_indicators)
|
| 208 |
|
| 209 |
def _generate_counter_questions(self, original_question, context_text):
|
| 210 |
"""Generate counter questions based on retrieved context"""
|
|
|
|
| 152 |
)
|
| 153 |
|
| 154 |
def ask_question(self, question: str):
|
| 155 |
+
"""Process a question and return response"""
|
| 156 |
try:
|
| 157 |
# Check if RAG retrieval finds relevant context
|
| 158 |
context_length = self._check_context_length(question)
|
|
|
|
| 171 |
# First, try to get a normal response
|
| 172 |
try:
|
| 173 |
response = self.chain_with_sources.invoke(question)
|
| 174 |
+
result = response.get('response') if response else None
|
| 175 |
|
| 176 |
+
# Check if response is None or invalid
|
| 177 |
+
if self._is_response_invalid(result):
|
| 178 |
return self._generate_counter_questions(question, context_text)
|
| 179 |
|
| 180 |
return result
|
|
|
|
| 192 |
print(f"Error in ask_question: {e}")
|
| 193 |
return f"I encountered an error processing your question. Could you please rephrase it more clearly?"
|
| 194 |
|
| 195 |
+
def _is_response_invalid(self, response):
|
| 196 |
+
"""Check if the response is None or invalid"""
|
| 197 |
+
# Check if response is None, empty, or too short
|
| 198 |
+
if response is None:
|
| 199 |
+
return True
|
| 200 |
+
if not response or len(response.strip()) < 5:
|
| 201 |
return True
|
| 202 |
|
| 203 |
+
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
def _generate_counter_questions(self, original_question, context_text):
|
| 206 |
"""Generate counter questions based on retrieved context"""
|