Spaces:
Build error
Build error
Update researchsimulation/InteractiveInterviewChatbot.py
Browse files
researchsimulation/InteractiveInterviewChatbot.py
CHANGED
|
@@ -307,7 +307,6 @@ def tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_pr
|
|
| 307 |
|
| 308 |
task_description = f"""
|
| 309 |
You are {agent_name}. Rewrite the following answer to match your personal communication style and tone preferences — **but only if the question is subjective or personal**.
|
| 310 |
-
|
| 311 |
---
|
| 312 |
### Original Generic Answer:
|
| 313 |
{generic_answer}
|
|
@@ -315,14 +314,12 @@ def tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_pr
|
|
| 315 |
---
|
| 316 |
### Question:
|
| 317 |
"{agent_question}"
|
| 318 |
-
|
| 319 |
---
|
| 320 |
### *Communication Profile Reference:*
|
| 321 |
- **Style:** {style}
|
| 322 |
- **Tone:** {tone}
|
| 323 |
- **Length:** {length}
|
| 324 |
- **Topics:** {topics}
|
| 325 |
-
|
| 326 |
---
|
| 327 |
### Important Note on Factual Questions:
|
| 328 |
If the question is a **factual or demographic one** (e.g., about age, name, location, occupation, birthplace), do **not** rewrite or stylise the answer. Just return the generic answer exactly as it is.
|
|
@@ -334,9 +331,7 @@ def tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_pr
|
|
| 334 |
- Where do you live?
|
| 335 |
- What do you do?
|
| 336 |
- Where were you born?
|
| 337 |
-
|
| 338 |
Only rewrite if the question invites a **personal perspective, opinion, habit, or belief**.
|
| 339 |
-
|
| 340 |
---
|
| 341 |
### Hard Rules – You Must Follow These Without Exception:
|
| 342 |
- If the question is factual → **return the generic answer unchanged**.
|
|
@@ -349,14 +344,12 @@ def tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_pr
|
|
| 349 |
- Do **not** include emojis, hashtags, placeholders, or third-person descriptions.
|
| 350 |
- Maintain **narrative consistency** across responses to reflect a coherent personality.
|
| 351 |
- Tailor phrasing, sentence structure, and vocabulary to fit your **persona** and **communication traits**.
|
| 352 |
-
|
| 353 |
---
|
| 354 |
### Personality Trait Alignment:
|
| 355 |
Ensure your answer reflects these aspects of your personality profile:
|
| 356 |
- Big Five Traits (e.g., Openness, Extraversion)
|
| 357 |
- Values and Priorities (e.g., Risk Tolerance, Achievement Orientation)
|
| 358 |
- Communication Preferences (e.g., Directness, Emotional Expressiveness)
|
| 359 |
-
|
| 360 |
---
|
| 361 |
Final Output:
|
| 362 |
- If the question is factual: return this answer exactly → **"{generic_answer}"**
|
|
@@ -399,7 +392,7 @@ def tailor_answer_to_profile(agent_name, generic_answer, agent_question, user_pr
|
|
| 399 |
def validate_generic_answer(agent_name, agent_question, generic_answer, user_profile, processor_llm):
|
| 400 |
logging.info("[validate_generic_answer] Entry")
|
| 401 |
try:
|
| 402 |
-
is_valid = validate_response(
|
| 403 |
question=agent_question,
|
| 404 |
answer=generic_answer,
|
| 405 |
user_profile_str=str(user_profile),
|
|
@@ -407,13 +400,14 @@ def validate_generic_answer(agent_name, agent_question, generic_answer, user_pro
|
|
| 407 |
interview_transcript_text="",
|
| 408 |
respondent_type=agent_name,
|
| 409 |
ai_evaluator_agent=None,
|
| 410 |
-
processor_llm=processor_llm
|
|
|
|
| 411 |
)
|
| 412 |
-
logging.info(f"[validate_generic_answer] Result: {is_valid}")
|
| 413 |
-
return is_valid
|
| 414 |
except Exception as e:
|
| 415 |
logging.exception("[validate_generic_answer] Exception during validation")
|
| 416 |
-
return False
|
| 417 |
|
| 418 |
def validate_styled_answer(agent_name, agent_question, styled_answer, user_profile, processor_llm):
|
| 419 |
"""
|
|
@@ -480,19 +474,30 @@ def ask_interview_question(respondent_agents_dict, last_active_agent, question,
|
|
| 480 |
|
| 481 |
generic_answer = generate_generic_answer(agent_name, agent_question, respondent_agent)
|
| 482 |
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
|
|
|
| 486 |
continue
|
| 487 |
|
| 488 |
tailored_attempts = 0
|
| 489 |
max_tailored_attempts = 3
|
| 490 |
tailored_answer = None
|
| 491 |
-
|
|
|
|
| 492 |
|
| 493 |
while tailored_attempts < max_tailored_attempts:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 494 |
styled = tailor_answer_to_profile(
|
| 495 |
-
agent_name, generic_answer, agent_question, user_profile, respondent_agent, feedback=
|
| 496 |
)
|
| 497 |
|
| 498 |
if len(styled) > 2000:
|
|
@@ -500,20 +505,19 @@ def ask_interview_question(respondent_agents_dict, last_active_agent, question,
|
|
| 500 |
tailored_attempts += 1
|
| 501 |
continue
|
| 502 |
|
| 503 |
-
is_valid,
|
| 504 |
agent_name, agent_question, styled, user_profile, processor_llm
|
| 505 |
)
|
| 506 |
if is_valid:
|
| 507 |
tailored_answer = styled
|
| 508 |
break
|
| 509 |
|
| 510 |
-
feedback = explanation # Pass explanation as feedback for next attempt
|
| 511 |
tailored_attempts += 1
|
| 512 |
|
| 513 |
if tailored_answer:
|
| 514 |
responses.append(f"**{agent_name}**: {tailored_answer}")
|
| 515 |
else:
|
| 516 |
-
responses.append(f"**PreData Moderator**: Failed to stylise the response for {agent_name} after multiple attempts.")
|
| 517 |
|
| 518 |
result = ["\n\n".join(responses)] if len(set(validated_questions.values())) == 1 else responses
|
| 519 |
|
|
|
|
| 307 |
|
| 308 |
task_description = f"""
|
| 309 |
You are {agent_name}. Rewrite the following answer to match your personal communication style and tone preferences — **but only if the question is subjective or personal**.
|
|
|
|
| 310 |
---
|
| 311 |
### Original Generic Answer:
|
| 312 |
{generic_answer}
|
|
|
|
| 314 |
---
|
| 315 |
### Question:
|
| 316 |
"{agent_question}"
|
|
|
|
| 317 |
---
|
| 318 |
### *Communication Profile Reference:*
|
| 319 |
- **Style:** {style}
|
| 320 |
- **Tone:** {tone}
|
| 321 |
- **Length:** {length}
|
| 322 |
- **Topics:** {topics}
|
|
|
|
| 323 |
---
|
| 324 |
### Important Note on Factual Questions:
|
| 325 |
If the question is a **factual or demographic one** (e.g., about age, name, location, occupation, birthplace), do **not** rewrite or stylise the answer. Just return the generic answer exactly as it is.
|
|
|
|
| 331 |
- Where do you live?
|
| 332 |
- What do you do?
|
| 333 |
- Where were you born?
|
|
|
|
| 334 |
Only rewrite if the question invites a **personal perspective, opinion, habit, or belief**.
|
|
|
|
| 335 |
---
|
| 336 |
### Hard Rules – You Must Follow These Without Exception:
|
| 337 |
- If the question is factual → **return the generic answer unchanged**.
|
|
|
|
| 344 |
- Do **not** include emojis, hashtags, placeholders, or third-person descriptions.
|
| 345 |
- Maintain **narrative consistency** across responses to reflect a coherent personality.
|
| 346 |
- Tailor phrasing, sentence structure, and vocabulary to fit your **persona** and **communication traits**.
|
|
|
|
| 347 |
---
|
| 348 |
### Personality Trait Alignment:
|
| 349 |
Ensure your answer reflects these aspects of your personality profile:
|
| 350 |
- Big Five Traits (e.g., Openness, Extraversion)
|
| 351 |
- Values and Priorities (e.g., Risk Tolerance, Achievement Orientation)
|
| 352 |
- Communication Preferences (e.g., Directness, Emotional Expressiveness)
|
|
|
|
| 353 |
---
|
| 354 |
Final Output:
|
| 355 |
- If the question is factual: return this answer exactly → **"{generic_answer}"**
|
|
|
|
| 392 |
def validate_generic_answer(agent_name, agent_question, generic_answer, user_profile, processor_llm):
|
| 393 |
logging.info("[validate_generic_answer] Entry")
|
| 394 |
try:
|
| 395 |
+
is_valid, feedback = validate_response(
|
| 396 |
question=agent_question,
|
| 397 |
answer=generic_answer,
|
| 398 |
user_profile_str=str(user_profile),
|
|
|
|
| 400 |
interview_transcript_text="",
|
| 401 |
respondent_type=agent_name,
|
| 402 |
ai_evaluator_agent=None,
|
| 403 |
+
processor_llm=processor_llm,
|
| 404 |
+
return_explanation=True
|
| 405 |
)
|
| 406 |
+
logging.info(f"[validate_generic_answer] Result: {is_valid}, feedback: {feedback}")
|
| 407 |
+
return is_valid, feedback
|
| 408 |
except Exception as e:
|
| 409 |
logging.exception("[validate_generic_answer] Exception during validation")
|
| 410 |
+
return False, "Exception during validation"
|
| 411 |
|
| 412 |
def validate_styled_answer(agent_name, agent_question, styled_answer, user_profile, processor_llm):
|
| 413 |
"""
|
|
|
|
| 474 |
|
| 475 |
generic_answer = generate_generic_answer(agent_name, agent_question, respondent_agent)
|
| 476 |
|
| 477 |
+
is_valid, feedback = validate_generic_answer(agent_name, agent_question, generic_answer, user_profile, processor_llm)
|
| 478 |
+
if not is_valid:
|
| 479 |
+
logging.warning(f"[validate_generic_answer]failed: {feedback}")
|
| 480 |
+
responses.append(f"**PreData Moderator**: Please ask {agent_name} again. Reason: {feedback}")
|
| 481 |
continue
|
| 482 |
|
| 483 |
tailored_attempts = 0
|
| 484 |
max_tailored_attempts = 3
|
| 485 |
tailored_answer = None
|
| 486 |
+
style_feedback = None
|
| 487 |
+
gen_feedback = feedback # plausibility/relevance/accuracy feedback from generic validation
|
| 488 |
|
| 489 |
while tailored_attempts < max_tailored_attempts:
|
| 490 |
+
# Combine feedback from generic and style validation for LLM
|
| 491 |
+
combined_feedback = None
|
| 492 |
+
if gen_feedback and style_feedback:
|
| 493 |
+
combined_feedback = f"Generic feedback: {gen_feedback}. Style feedback: {style_feedback}"
|
| 494 |
+
elif gen_feedback:
|
| 495 |
+
combined_feedback = gen_feedback
|
| 496 |
+
elif style_feedback:
|
| 497 |
+
combined_feedback = style_feedback
|
| 498 |
+
|
| 499 |
styled = tailor_answer_to_profile(
|
| 500 |
+
agent_name, generic_answer, agent_question, user_profile, respondent_agent, feedback=combined_feedback
|
| 501 |
)
|
| 502 |
|
| 503 |
if len(styled) > 2000:
|
|
|
|
| 505 |
tailored_attempts += 1
|
| 506 |
continue
|
| 507 |
|
| 508 |
+
is_valid, style_feedback = validate_styled_answer(
|
| 509 |
agent_name, agent_question, styled, user_profile, processor_llm
|
| 510 |
)
|
| 511 |
if is_valid:
|
| 512 |
tailored_answer = styled
|
| 513 |
break
|
| 514 |
|
|
|
|
| 515 |
tailored_attempts += 1
|
| 516 |
|
| 517 |
if tailored_answer:
|
| 518 |
responses.append(f"**{agent_name}**: {tailored_answer}")
|
| 519 |
else:
|
| 520 |
+
responses.append(f"**PreData Moderator**: Failed to stylise the response for {agent_name} after multiple attempts. Last feedback: {style_feedback}")
|
| 521 |
|
| 522 |
result = ["\n\n".join(responses)] if len(set(validated_questions.values())) == 1 else responses
|
| 523 |
|