Zeggai Abdellah commited on
Commit
f1dfb78
·
1 Parent(s): a65588e

enhanc error handling

Browse files
Files changed (1) hide show
  1. rag_pipeline.py +36 -5
rag_pipeline.py CHANGED
@@ -124,6 +124,26 @@ Answer the doctor's question accurately and concisely using only the provided in
124
  2. For lists, maintain the original bullet points/numbering and include citations.
125
  3. Present information concisely but ensure clinical accuracy is never compromised.
126
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
127
  ---
128
 
129
  """
@@ -153,11 +173,12 @@ Answer the doctor's question accurately and concisely using only the provided in
153
  def create_agent(tools, llm):
154
  """Create the ReAct agent with custom prompt"""
155
 
156
- # Create agent
157
  agent = ReActAgent.from_tools(
158
  tools,
159
  llm=llm,
160
  verbose=True,
 
161
  )
162
 
163
  # Create and apply safe custom prompt
@@ -240,10 +261,18 @@ def process_question_with_sequential_citations(agent, question: str, chunks_dire
240
  # Get the response from the agent
241
  response = agent.chat(question)
242
  response_text = response.response
243
- # Check if the response indicates max iterations was reached
244
- if "max iterations" in response_text.lower() or len(response_text.strip()) == 0:
 
 
 
 
 
245
  # Provide a more helpful fallback response
246
- response_text = ("I apologize, but I encountered difficulties processing your comparative question ")
 
 
 
247
  # Extract source IDs from the response (preserving order)
248
  unique_ids = extract_source_ids(response_text)
249
 
@@ -298,8 +327,10 @@ def process_question_with_sequential_citations(agent, question: str, chunks_dire
298
 
299
  except Exception as e:
300
  print(f"Error processing question: {e}")
 
 
301
  return {
302
- "response": response_text if 'response_text' in locals() else "Error occurred",
303
  "cited_elements_json": "[]",
304
  "unique_ids": [],
305
  "citation_mapping": {},
 
124
  2. For lists, maintain the original bullet points/numbering and include citations.
125
  3. Present information concisely but ensure clinical accuracy is never compromised.
126
 
127
+ ### CRITICAL: Efficient Response Strategy
128
+ 1. **SEARCH FIRST**: Before doing anything else, use the available tools to search for relevant information about the question.
129
+ 2. **BE DECISIVE**: Once you find relevant information, formulate your response immediately. Do not search repeatedly for the same information.
130
+ 3. **ANSWER DIRECTLY**: Provide a clear, direct answer based on the information found. Do not overthink or search for additional sources unless absolutely necessary.
131
+ 4. **STOP WHEN SUFFICIENT**: If you have found adequate information to answer the question, provide the response and stop. Do not continue searching for more sources.
132
+ 5. **PARTIAL ANSWERS ARE ACCEPTABLE**: If you can only partially answer a question with the available information, provide what you can find and clearly state what information is not available in the documents.
133
+
134
+ ### Response Guidelines for Complex Questions
135
+ - For comparative questions: Search once for each topic, then provide the comparison based on found information
136
+ - For multi-part questions: Address each part systematically but efficiently
137
+ - If information is not found after initial search: State clearly that the information is not available in the provided documents
138
+ - Do not repeatedly search for the same terms or rephrase searches excessively
139
+
140
+ ### When Information is Limited
141
+ If you cannot find complete information to fully answer a question:
142
+ 1. Provide whatever relevant information you did find with proper citations
143
+ 2. Clearly state: "Based on the available documents, I can provide the following information..."
144
+ 3. Indicate what specific information is not available: "However, information about [specific topic] was not found in the provided documents."
145
+ 4. Do not continue searching or reasoning beyond what is necessary
146
+
147
  ---
148
 
149
  """
 
173
  def create_agent(tools, llm):
174
  """Create the ReAct agent with custom prompt"""
175
 
176
+ # Create agent with increased max iterations and better handling
177
  agent = ReActAgent.from_tools(
178
  tools,
179
  llm=llm,
180
  verbose=True,
181
+ max_iterations=8, # Reduced from default to prevent excessive looping
182
  )
183
 
184
  # Create and apply safe custom prompt
 
261
  # Get the response from the agent
262
  response = agent.chat(question)
263
  response_text = response.response
264
+
265
+ # Enhanced handling for max iterations error
266
+ if ("max iterations" in response_text.lower() or
267
+ "reached max iterations" in response_text.lower() or
268
+ len(response_text.strip()) == 0 or
269
+ "agent stopped due to max iterations" in response_text.lower()):
270
+
271
  # Provide a more helpful fallback response
272
+ response_text = ("I apologize, but I encountered difficulties processing your question within the available search iterations. "
273
+ "This may be due to the complexity of your query or limitations in finding specific information in the available documents. "
274
+ "Please try rephrasing your question more specifically, or break it down into smaller, more focused questions for better results.")
275
+
276
  # Extract source IDs from the response (preserving order)
277
  unique_ids = extract_source_ids(response_text)
278
 
 
327
 
328
  except Exception as e:
329
  print(f"Error processing question: {e}")
330
+ error_response = "I apologize, but I encountered an error while processing your question. Please try rephrasing your question or asking about a more specific topic."
331
+
332
  return {
333
+ "response": error_response,
334
  "cited_elements_json": "[]",
335
  "unique_ids": [],
336
  "citation_mapping": {},