Spaces:
Build error
Build error
Update common/ResponseValidation.py
Browse files- common/ResponseValidation.py +41 -21
common/ResponseValidation.py
CHANGED
|
@@ -15,40 +15,59 @@ def matches_user_speaking_style(answer, processor_llm, user_profile, agent_quest
|
|
| 15 |
logging.info("[Style Match Check] Entry")
|
| 16 |
|
| 17 |
try:
|
| 18 |
-
# --- Step 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
fp_prompt = f"""
|
| 20 |
You are an expert in analysing writing style and narrative perspective.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
{answer}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
Output format:
|
| 27 |
First Person: Yes
|
| 28 |
or
|
| 29 |
First Person: No
|
|
|
|
| 30 |
"""
|
| 31 |
fp_response = processor_llm.invoke(fp_prompt)
|
| 32 |
fp_result = fp_response.content.strip().lower()
|
|
|
|
| 33 |
if "first person: no" in fp_result:
|
| 34 |
-
|
|
|
|
| 35 |
if return_explanation:
|
| 36 |
-
return False,
|
| 37 |
return False
|
| 38 |
|
| 39 |
-
# --- Step 2: Skip style check for factual questions ---
|
| 40 |
-
factual_keywords = [
|
| 41 |
-
"name", "age", "where are you from", "where do you live", "occupation",
|
| 42 |
-
"birthplace", "what do you do", "how old", "which city", "which country"
|
| 43 |
-
]
|
| 44 |
-
lower_q = agent_question.strip().lower()
|
| 45 |
-
is_factual = any(kw in lower_q for kw in factual_keywords)
|
| 46 |
-
if is_factual:
|
| 47 |
-
logging.info("[Style Match Check] Question is factual — skipping style evaluation")
|
| 48 |
-
if return_explanation:
|
| 49 |
-
return True, None
|
| 50 |
-
return True
|
| 51 |
-
|
| 52 |
# --- Step 3: Extract user communication profile ---
|
| 53 |
style = user_profile.get_field("Communication", "Style")
|
| 54 |
tone = user_profile.get_field("Communication", "Tone")
|
|
@@ -86,7 +105,7 @@ Style Match: No
|
|
| 86 |
return True, None
|
| 87 |
return True
|
| 88 |
elif "style match: no" in style_result:
|
| 89 |
-
# ---
|
| 90 |
explanation_prompt = f"""
|
| 91 |
You are a communication coach and writing style analyst.
|
| 92 |
The following response was evaluated as NOT matching the given communication profile.
|
|
@@ -123,6 +142,7 @@ Please provide a concise reason why the style does not match.
|
|
| 123 |
|
| 124 |
|
| 125 |
|
|
|
|
| 126 |
def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
|
| 127 |
llm_mode_prompt = f"""
|
| 128 |
You are an expert in market research interview analysis. Given the following question, determine if it is:
|
|
|
|
| 15 |
logging.info("[Style Match Check] Entry")
|
| 16 |
|
| 17 |
try:
|
| 18 |
+
# --- Step 1: Skip style check for factual questions ---
|
| 19 |
+
factual_keywords = [
|
| 20 |
+
"name", "age", "where are you from", "where do you live", "occupation",
|
| 21 |
+
"birthplace", "what do you do", "how old", "which city", "which country"
|
| 22 |
+
]
|
| 23 |
+
lower_q = agent_question.strip().lower()
|
| 24 |
+
is_factual = any(kw in lower_q for kw in factual_keywords)
|
| 25 |
+
if is_factual:
|
| 26 |
+
logging.info("[Style Match Check] Question is factual — skipping strict first-person enforcement")
|
| 27 |
+
if return_explanation:
|
| 28 |
+
return True, None
|
| 29 |
+
return True
|
| 30 |
+
|
| 31 |
+
# --- Step 2: Context-sensitive first-person check ---
|
| 32 |
fp_prompt = f"""
|
| 33 |
You are an expert in analysing writing style and narrative perspective.
|
| 34 |
+
|
| 35 |
+
Determine whether the following response is *stylistically appropriate* and matches a first-person perspective *when contextually expected*.
|
| 36 |
+
|
| 37 |
+
- A first-person response typically includes pronouns like "I", "me", "my", "mine", "we", "our", or "us".
|
| 38 |
+
- However, for **short factual responses** (e.g. "What's your name?" → "Alex") or answers that clearly imply personal ownership or involvement (e.g. "Our team led the project"), the absence of explicit first-person pronouns can still be acceptable.
|
| 39 |
+
- The key question is: **Given the question and expected tone, is the response appropriately personal and aligned with a first-person speaking style?**
|
| 40 |
+
|
| 41 |
+
Evaluate the response below.
|
| 42 |
+
|
| 43 |
+
### Question:
|
| 44 |
+
{agent_question}
|
| 45 |
+
|
| 46 |
+
### Response:
|
| 47 |
{answer}
|
| 48 |
+
|
| 49 |
+
Return:
|
| 50 |
+
First Person: Yes
|
| 51 |
+
or
|
| 52 |
+
First Person: No
|
| 53 |
+
and briefly explain if "No".
|
| 54 |
+
|
| 55 |
Output format:
|
| 56 |
First Person: Yes
|
| 57 |
or
|
| 58 |
First Person: No
|
| 59 |
+
Reason: <short explanation>
|
| 60 |
"""
|
| 61 |
fp_response = processor_llm.invoke(fp_prompt)
|
| 62 |
fp_result = fp_response.content.strip().lower()
|
| 63 |
+
|
| 64 |
if "first person: no" in fp_result:
|
| 65 |
+
explanation = fp_result.split("reason:", 1)[-1].strip().capitalize() if "reason:" in fp_result else "The answer is not in first person."
|
| 66 |
+
logging.warning(f"[Style Match Check] Failed first-person test: {explanation}")
|
| 67 |
if return_explanation:
|
| 68 |
+
return False, explanation
|
| 69 |
return False
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
# --- Step 3: Extract user communication profile ---
|
| 72 |
style = user_profile.get_field("Communication", "Style")
|
| 73 |
tone = user_profile.get_field("Communication", "Tone")
|
|
|
|
| 105 |
return True, None
|
| 106 |
return True
|
| 107 |
elif "style match: no" in style_result:
|
| 108 |
+
# --- Ask LLM for explanation on mismatch ---
|
| 109 |
explanation_prompt = f"""
|
| 110 |
You are a communication coach and writing style analyst.
|
| 111 |
The following response was evaluated as NOT matching the given communication profile.
|
|
|
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
+
|
| 146 |
def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
|
| 147 |
llm_mode_prompt = f"""
|
| 148 |
You are an expert in market research interview analysis. Given the following question, determine if it is:
|