Spaces:
Build error
Build error
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 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 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
|