Basitha commited on
Commit
275a26b
·
verified ·
1 Parent(s): 08871c2

Update researchsimulation/InteractiveInterviewChatbot.py

Browse files
researchsimulation/InteractiveInterviewChatbot.py CHANGED
@@ -286,7 +286,7 @@ def generate_generic_answer(agent_name, agent_question, respondent_agent):
286
  return result
287
 
288
 
289
- def tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_profile, respondent_agent):
290
  """
291
  Enhances the generic answer to match the respondent's communication profile and personality traits.
292
  """
@@ -362,6 +362,8 @@ def tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_pr
362
  - If the question is factual: return this answer exactly → **"{generic_answer}"**
363
  - If not: return a single paragraph answer that matches the respondent’s tone and style, while strictly preserving the original meaning and personal voice from this answer.
364
  """
 
 
365
 
366
  logging.debug(f"[tailor_answer_to_profile] Task description preview: {task_description[:300]}...")
367
  logging.info("[tailor_answer_to_profile] Initialising Task and Crew objects")
@@ -416,21 +418,22 @@ def validate_generic_answer(agent_name, agent_question, generic_answer, user_pro
416
  def validate_styled_answer(agent_name, agent_question, styled_answer, user_profile, processor_llm):
417
  """
418
  Validates whether the styled answer matches the user's typical speaking style using communication profile.
419
- Returns True if stylistically aligned, False otherwise.
420
  """
421
  logging.info("[validate_styled_answer] Entry")
422
  try:
423
- is_valid = matches_user_speaking_style(
424
  answer=styled_answer,
425
  processor_llm=processor_llm,
426
  user_profile=user_profile,
427
- agent_question=agent_question
 
428
  )
429
- logging.info(f"[validate_styled_answer] Style validation result: {is_valid}")
430
- return is_valid
431
  except Exception as e:
432
  logging.exception("[validate_styled_answer] Exception during style validation")
433
- return False
434
 
435
 
436
  # --- Updated ask_interview_question Function ---
@@ -481,25 +484,29 @@ def ask_interview_question(respondent_agents_dict, last_active_agent, question,
481
  responses.append(f"**PreData Moderator**: Please ask {agent_name} again.")
482
  continue
483
 
484
- def generator():
485
- return tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_profile, respondent_agent)
486
-
487
  tailored_attempts = 0
488
  max_tailored_attempts = 3
489
  tailored_answer = None
 
490
 
491
  while tailored_attempts < max_tailored_attempts:
492
- styled = generator()
 
 
493
 
494
  if len(styled) > 2000:
495
  logging.warning(f"[ask_interview_question] Styled answer too long (len={len(styled)}), retrying...")
496
  tailored_attempts += 1
497
  continue
498
 
499
- if validate_styled_answer(agent_name, agent_question, styled, user_profile, processor_llm):
 
 
 
500
  tailored_answer = styled
501
  break
502
 
 
503
  tailored_attempts += 1
504
 
505
  if tailored_answer:
 
286
  return result
287
 
288
 
289
+ def tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_profile, respondent_agent, feedback=None):
290
  """
291
  Enhances the generic answer to match the respondent's communication profile and personality traits.
292
  """
 
362
  - If the question is factual: return this answer exactly → **"{generic_answer}"**
363
  - If not: return a single paragraph answer that matches the respondent’s tone and style, while strictly preserving the original meaning and personal voice from this answer.
364
  """
365
+ if feedback:
366
+ task_description += f"\n---\n### Feedback from previous attempt:\n{feedback}\nPlease address this feedback in your rewrite.\n"
367
 
368
  logging.debug(f"[tailor_answer_to_profile] Task description preview: {task_description[:300]}...")
369
  logging.info("[tailor_answer_to_profile] Initialising Task and Crew objects")
 
418
  def validate_styled_answer(agent_name, agent_question, styled_answer, user_profile, processor_llm):
419
  """
420
  Validates whether the styled answer matches the user's typical speaking style using communication profile.
421
+ Returns (True, explanation) if stylistically aligned, (False, explanation) otherwise.
422
  """
423
  logging.info("[validate_styled_answer] Entry")
424
  try:
425
+ is_valid, explanation = matches_user_speaking_style(
426
  answer=styled_answer,
427
  processor_llm=processor_llm,
428
  user_profile=user_profile,
429
+ agent_question=agent_question,
430
+ return_explanation=True
431
  )
432
+ logging.info(f"[validate_styled_answer] Style validation result: {is_valid}, explanation: {explanation}")
433
+ return is_valid, explanation
434
  except Exception as e:
435
  logging.exception("[validate_styled_answer] Exception during style validation")
436
+ return False, "Exception during style validation"
437
 
438
 
439
  # --- Updated ask_interview_question Function ---
 
484
  responses.append(f"**PreData Moderator**: Please ask {agent_name} again.")
485
  continue
486
 
 
 
 
487
  tailored_attempts = 0
488
  max_tailored_attempts = 3
489
  tailored_answer = None
490
+ feedback = None
491
 
492
  while tailored_attempts < max_tailored_attempts:
493
+ styled = tailor_answer_to_profile(
494
+ agent_name, generic_answer, agent_question, user_profile, respondent_agent, feedback=feedback
495
+ )
496
 
497
  if len(styled) > 2000:
498
  logging.warning(f"[ask_interview_question] Styled answer too long (len={len(styled)}), retrying...")
499
  tailored_attempts += 1
500
  continue
501
 
502
+ is_valid, explanation = validate_styled_answer(
503
+ agent_name, agent_question, styled, user_profile, processor_llm
504
+ )
505
+ if is_valid:
506
  tailored_answer = styled
507
  break
508
 
509
+ feedback = explanation # Pass explanation as feedback for next attempt
510
  tailored_attempts += 1
511
 
512
  if tailored_answer: