Basitha commited on
Commit
c90a492
·
verified ·
1 Parent(s): ebc9050

Update common/ResponseValidation.py

Browse files
Files changed (1) hide show
  1. common/ResponseValidation.py +59 -25
common/ResponseValidation.py CHANGED
@@ -34,44 +34,84 @@ First Person: No
34
  return False
35
 
36
 
37
- def matches_user_speaking_style(answer, transcript_text, processor_llm):
38
  """
39
  Uses the LLM to determine if the answer matches the tone and style of the user's prior speaking style in the transcript.
40
  Returns True if similar, False otherwise.
 
41
  """
42
- prompt = f"""
43
- You are a writing style and tone analyst.
44
- Given a new response and a prior interview transcript, determine whether the tone, language, and style of the new response matches the user’s typical speaking style.
 
 
 
 
 
45
 
46
- Transcript (how the user usually talks):
47
- \"\"\"{transcript_text}\"\"\"
 
 
 
 
 
48
 
49
- New Response:
50
- \"\"\"{answer}\"\"\"
 
51
 
52
- Does the new response sound like it could have come from the same person, in terms of tone, phrasing, and writing style?
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- Answer only:
55
- Style Match: Yes
56
- or
57
- Style Match: No
58
- """
59
- try:
60
- logging.info("[Style Match Check] Invoking LLM to compare tone and style with prior transcript")
61
  response = processor_llm.invoke(prompt)
62
  result = response.content.strip().lower()
 
63
  if "style match: yes" in result:
 
64
  return True
65
  elif "style match: no" in result:
 
66
  return False
67
  else:
68
- logging.warning(f"Unexpected output format in style match check: {result}")
69
  return False
 
70
  except Exception as e:
71
- logging.error(f"LLM failed during style match check: {e}")
72
  return False
73
 
74
-
75
  def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
76
  llm_mode_prompt = f"""
77
  You are an expert in market research interview analysis. Given the following question, determine if it is:
@@ -131,9 +171,6 @@ def validate_response(question, answer, user_profile_str, fast_facts_str, interv
131
  if not is_first_person(answer, processor_llm):
132
  logging.warning("Did not pass style due to 3rd person use")
133
  return False
134
- #if not matches_user_speaking_style(answer, interview_transcript_text, processor_llm):
135
- #logging.warning("Did not match user's speaking style from transcript")
136
- #return False
137
  return True
138
  return False
139
  else:
@@ -166,8 +203,5 @@ def validate_response(question, answer, user_profile_str, fast_facts_str, interv
166
  if not is_first_person(answer, processor_llm):
167
  logging.warning("Did not pass style due to 3rd person use")
168
  return False
169
- #if not matches_user_speaking_style(answer, interview_transcript_text, processor_llm):
170
- #logging.warning("Did not match user's speaking style from transcript")
171
- #return False
172
  return True
173
  return False
 
34
  return False
35
 
36
 
37
+ def matches_user_speaking_style(answer, transcript_text, processor_llm, user_profile, agent_question):
38
  """
39
  Uses the LLM to determine if the answer matches the tone and style of the user's prior speaking style in the transcript.
40
  Returns True if similar, False otherwise.
41
+ Incorporates logic to skip style matching for factual questions and uses profile-based criteria.
42
  """
43
+ logging.info("[Style Match Check] Entry")
44
+
45
+ try:
46
+ # Get communication profile
47
+ style = user_profile.get_field("Communication", "Style")
48
+ tone = user_profile.get_field("Communication", "Tone")
49
+ length = user_profile.get_field("Communication", "Length")
50
+ topics = user_profile.get_field("Communication", "Topics")
51
 
52
+ # Identify factual-type question
53
+ factual_keywords = [
54
+ "name", "age", "where are you from", "where do you live", "occupation",
55
+ "birthplace", "what do you do", "how old", "which city", "which country"
56
+ ]
57
+ lower_q = agent_question.strip().lower()
58
+ is_factual = any(kw in lower_q for kw in factual_keywords)
59
 
60
+ if is_factual:
61
+ logging.info("[Style Match Check] Question is factual — skipping style comparison")
62
+ return True
63
 
64
+ prompt = f"""
65
+ You are a writing style and tone analyst.
66
+
67
+ Your job is to assess whether a new response sounds like it was written by the same person who spoke in the interview transcript — considering phrasing, vocabulary, tone, and sentence structure.
68
+
69
+ ---
70
+ ### Prior Interview Transcript (how the user usually talks):
71
+ \"\"\"{transcript_text}\"\"\"
72
+
73
+ ---
74
+ ### New Response:
75
+ \"\"\"{answer}\"\"\"
76
+
77
+ ---
78
+ ### Style Profile Reference:
79
+ - Style: {style}
80
+ - Tone: {tone}
81
+ - Preferred Length: {length}
82
+ - Topics: {topics}
83
+
84
+ ---
85
+ ### Instructions:
86
+ - Check if the *tone*, *style*, and *language* of the new response align with the transcript.
87
+ - Use the style profile for reference.
88
+ - Focus on phrasing, formality, sentence structure, expressiveness, and personal flair.
89
+ - Ignore topic similarity — you’re assessing delivery style.
90
+ - Reply only with one of the following:
91
+
92
+ Style Match: Yes
93
+ or
94
+ Style Match: No
95
+ """
96
 
97
+ logging.info("[Style Match Check] Invoking LLM with style comparison prompt")
 
 
 
 
 
 
98
  response = processor_llm.invoke(prompt)
99
  result = response.content.strip().lower()
100
+
101
  if "style match: yes" in result:
102
+ logging.info("[Style Match Check] Match confirmed")
103
  return True
104
  elif "style match: no" in result:
105
+ logging.info("[Style Match Check] Style mismatch detected")
106
  return False
107
  else:
108
+ logging.warning(f"[Style Match Check] Unexpected response format: {result}")
109
  return False
110
+
111
  except Exception as e:
112
+ logging.error(f"[Style Match Check] LLM failed during comparison: {e}")
113
  return False
114
 
 
115
  def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
116
  llm_mode_prompt = f"""
117
  You are an expert in market research interview analysis. Given the following question, determine if it is:
 
171
  if not is_first_person(answer, processor_llm):
172
  logging.warning("Did not pass style due to 3rd person use")
173
  return False
 
 
 
174
  return True
175
  return False
176
  else:
 
203
  if not is_first_person(answer, processor_llm):
204
  logging.warning("Did not pass style due to 3rd person use")
205
  return False
 
 
 
206
  return True
207
  return False