Basitha commited on
Commit
1c35fdf
·
verified ·
1 Parent(s): add2280

Update researchsimulation/InteractiveInterviewChatbot.py

Browse files
researchsimulation/InteractiveInterviewChatbot.py CHANGED
@@ -300,27 +300,48 @@ A culturally authentic and conversational response to the question: '{agent_ques
300
  )
301
  logging.debug(f"Crew initialized for agent '{agent_name}' with 1 task and sequential process")
302
 
303
- try:
304
- # Start the task
305
- crew_output = crew.kickoff()
306
- logging.info(f"Task execution completed for agent '{agent_name}'")
307
-
308
- task_output = question_task.output
309
- logging.debug(f"Raw output from agent '{agent_name}': {task_output.raw}")
310
-
311
- # Collect and format the response
312
- if task_output.raw:
313
- formatted_response = f"**{agent_name}**: {task_output.raw}"
314
- responses.append(formatted_response)
315
- logging.info(f"Formatted response from agent '{agent_name}' added to responses")
316
- else:
317
- fallback_response = f"**{agent_name}**: I wasn't able to answer right now - can you try again?"
318
- responses.append(fallback_response)
319
- logging.warning(f"No output from agent '{agent_name}'. Added fallback response.")
320
-
321
- except Exception as e:
322
- logging.error(f"Error during task execution for agent '{agent_name}': {str(e)}", exc_info=True)
323
- error_response = f"**PreData Moderator**: An error occurred while processing {agent_name}'s response. Please try again."
324
- responses.append(error_response)
325
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  return responses
 
300
  )
301
  logging.debug(f"Crew initialized for agent '{agent_name}' with 1 task and sequential process")
302
 
303
+ max_attempts = 3
304
+ attempt = 0
305
+ validated = False
306
+ validated_answer = None
307
+ while attempt < max_attempts and not validated:
308
+ try:
309
+ crew_output = crew.kickoff()
310
+ logging.info(f"Task execution completed for agent '{agent_name}' (attempt {attempt+1})")
311
+ task_output = question_task.output
312
+ logging.debug(f"Raw output from agent '{agent_name}': {getattr(task_output, 'raw', str(task_output))}")
313
+ answer = task_output.raw if hasattr(task_output, 'raw') else str(task_output)
314
+ # Validate the response using validate_response from validation_utils
315
+ is_valid = validate_response(
316
+ question=agent_question,
317
+ answer=answer,
318
+ user_profile_str=str(user_profile),
319
+ fast_facts_str="", # Add if available
320
+ interview_transcript_text="", # Add if available
321
+ respondent_type=agent_name,
322
+ ai_evaluator_agent=None, # Add if available
323
+ processor_llm=processor_llm
324
+ )
325
+ logging.info(f"Validation result for agent '{agent_name}' (attempt {attempt+1}): {is_valid}")
326
+ if is_valid:
327
+ validated = True
328
+ validated_answer = answer
329
+ break
330
+ else:
331
+ attempt += 1
332
+ logging.warning(f"Response failed validation for agent '{agent_name}' (attempt {attempt}). Retrying...")
333
+ except Exception as e:
334
+ logging.error(f"Error during task execution for agent '{agent_name}' (attempt {attempt+1}): {str(e)}", exc_info=True)
335
+ attempt += 1
336
+ # --- End validation and retry loop ---
337
+
338
+ if validated_answer:
339
+ formatted_response = f"**{agent_name}**: {validated_answer}"
340
+ responses.append(formatted_response)
341
+ logging.info(f"Validated response from agent '{agent_name}' added to responses")
342
+ else:
343
+ fallback_response = f"**PreData Moderator**: Unable to pass validation after {max_attempts} attempts for {agent_name}."
344
+ responses.append(fallback_response)
345
+ logging.warning(f"No validated output from agent '{agent_name}' after {max_attempts} attempts. Added fallback response.")
346
+
347
  return responses