Amna2024 commited on
Commit
d72aee6
·
verified ·
1 Parent(s): e59fcc3

Update rag_service.py

Browse files
Files changed (1) hide show
  1. rag_service.py +49 -1
rag_service.py CHANGED
@@ -110,6 +110,7 @@ class RAGService:
110
  user_question = kwargs["question"]
111
 
112
  context_text = ""
 
113
  if len(docs_by_type["texts"]) > 0:
114
  for text_element in docs_by_type["texts"]:
115
  context_text += str(text_element)
@@ -150,10 +151,57 @@ class RAGService:
150
  [HumanMessage(content=prompt_template.strip())] # plain string
151
  )
152
 
 
 
 
 
 
153
  def ask_question(self, question: str):
154
- """Process a question and return response"""
 
 
155
  response = self.chain_with_sources.invoke(question)
 
 
 
 
 
 
156
  return response['response']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
157
 
158
 
159
  # Create a global instance
 
110
  user_question = kwargs["question"]
111
 
112
  context_text = ""
113
+ prompt_content = []
114
  if len(docs_by_type["texts"]) > 0:
115
  for text_element in docs_by_type["texts"]:
116
  context_text += str(text_element)
 
151
  [HumanMessage(content=prompt_template.strip())] # plain string
152
  )
153
 
154
+ # def ask_question(self, question: str):
155
+ # """Process a question and return response"""
156
+ # response = self.chain_with_sources.invoke(question)
157
+ # return response['response']
158
+
159
  def ask_question(self, question: str):
160
+ """Process a question and return response"""
161
+ try:
162
+ # First attempt
163
  response = self.chain_with_sources.invoke(question)
164
+
165
+ # Check if response is valid
166
+ if not response or not response.get('response') or len(response['response'].strip()) < 0:
167
+ # Try to clarify what the user might be asking
168
+ return self._handle_ambiguous_question(question)
169
+
170
  return response['response']
171
+
172
+ except Exception as e:
173
+ print(f"Error in ask_question: {e}")
174
+ return self._handle_ambiguous_question(question)
175
+
176
+ def _handle_ambiguous_question(self, question: str):
177
+ """Handle ambiguous or complex questions"""
178
+ try:
179
+ # Use LLM to understand what the user might be asking
180
+ llm = ChatGroq(model="llama-3.1-8b-instant", groq_api_key=self.groq_key)
181
+
182
+ clarification_prompt = f"""
183
+ The user asked: "{question}"
184
+
185
+ This question seems unclear or complex. Please help clarify by:
186
+ 1. Identifying what the user might be asking about
187
+ 2. Suggesting a clearer way to phrase the question
188
+ 3. If possible, provide a brief answer based on your best understanding
189
+
190
+ Format your response as:
191
+ "I think you're asking about: [topic]
192
+
193
+ Suggested clearer question: [clearer version]
194
+
195
+ Based on my understanding: [brief answer if possible]
196
+
197
+ Please rephrase your question if this isn't what you meant."
198
+ """
199
+
200
+ response = llm.invoke(clarification_prompt)
201
+ return response.content
202
+
203
+ except Exception as e:
204
+ return "I'm having trouble understanding your question. Could you please rephrase it more clearly? For example, be more specific about what you're looking for."
205
 
206
 
207
  # Create a global instance