Basitha commited on
Commit
4c269e9
·
verified ·
1 Parent(s): 151e095

Update common/ResponseValidation.py

Browse files
Files changed (1) hide show
  1. common/ResponseValidation.py +32 -57
common/ResponseValidation.py CHANGED
@@ -6,15 +6,16 @@ from RespondentAgent import *
6
  from langchain_groq import ChatGroq
7
 
8
 
9
- def matches_user_speaking_style(answer, transcript_text, processor_llm, user_profile, agent_question):
10
  """
11
- Uses the LLM to determine if the answer matches the tone and style of the user's prior speaking style in the transcript.
12
- Returns True if similar and in first person, False otherwise.
 
13
  """
14
  logging.info("[Style Match Check] Entry")
15
-
16
  try:
17
- # First-person perspective check
18
  fp_prompt = f"""
19
  You are an expert in analysing writing style and narrative perspective.
20
  Determine whether the following response is written from a first-person point of view.
@@ -23,6 +24,7 @@ Say "First Person: Yes" only if clearly first-person, else say "First Person: No
23
 
24
  Response:
25
  \"\"\"{answer}\"\"\"
 
26
  Output format:
27
  First Person: Yes
28
  or
@@ -34,77 +36,69 @@ First Person: No
34
  logging.warning("[Style Match Check] Failed first-person test")
35
  return False
36
 
37
- # Get communication profile
38
- style = user_profile.get_field("Communication", "Style")
39
- tone = user_profile.get_field("Communication", "Tone")
40
- length = user_profile.get_field("Communication", "Length")
41
- topics = user_profile.get_field("Communication", "Topics")
42
-
43
- # Identify factual-type question
44
  factual_keywords = [
45
  "name", "age", "where are you from", "where do you live", "occupation",
46
  "birthplace", "what do you do", "how old", "which city", "which country"
47
  ]
48
  lower_q = agent_question.strip().lower()
49
  is_factual = any(kw in lower_q for kw in factual_keywords)
50
-
51
  if is_factual:
52
- logging.info("[Style Match Check] Question is factual — skipping style comparison")
53
  return True
54
 
55
- # Style match prompt
56
- prompt = f"""
57
- You are a writing style and tone analyst.
 
 
58
 
59
- 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.
 
 
60
 
61
- ---
62
- ### Prior Interview Transcript (how the user usually talks):
63
- \"\"\"{transcript_text}\"\"\"
64
 
65
  ---
66
- ### New Response:
67
  \"\"\"{answer}\"\"\"
68
 
69
- ---
70
- ### Style Profile Reference:
71
  - Style: {style}
72
  - Tone: {tone}
73
  - Preferred Length: {length}
74
- - Topics: {topics}
75
 
76
  ---
77
  ### Instructions:
78
- - Check if the *tone*, *style*, and *language* of the new response align with the transcript.
79
- - Use the style profile for reference.
80
- - Focus on phrasing, formality, sentence structure, expressiveness, and personal flair.
81
- - Ignore topic similarity — you’re assessing delivery style.
82
- - Reply only with one of the following:
83
 
84
  Style Match: Yes
85
  or
86
  Style Match: No
87
  """
 
 
 
88
 
89
- logging.info("[Style Match Check] Invoking LLM with style comparison prompt")
90
- response = processor_llm.invoke(prompt)
91
- result = response.content.strip().lower()
92
-
93
- if "style match: yes" in result:
94
  logging.info("[Style Match Check] Match confirmed")
95
  return True
96
- elif "style match: no" in result:
97
  logging.info("[Style Match Check] Style mismatch detected")
98
  return False
99
  else:
100
- logging.warning(f"[Style Match Check] Unexpected output format: {result}")
101
  return False
102
 
103
  except Exception as e:
104
- logging.error(f"[Style Match Check] LLM failed during comparison: {e}")
105
  return False
106
 
107
 
 
108
  def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
109
  llm_mode_prompt = f"""
110
  You are an expert in market research interview analysis. Given the following question, determine if it is:
@@ -193,22 +187,3 @@ Accuracy Rating: <0-10>
193
  return False
194
 
195
 
196
- def validate_styled_answer(agent_name, agent_question, styled_answer, user_profile, processor_llm, interview_transcript_text):
197
- """
198
- Validates whether the styled answer matches the user's typical speaking style using prior interview transcript and communication profile.
199
- Returns True if stylistically aligned, False otherwise.
200
- """
201
- logging.info("[validate_styled_answer] Entry")
202
- try:
203
- is_valid = matches_user_speaking_style(
204
- answer=styled_answer,
205
- transcript_text=interview_transcript_text,
206
- processor_llm=processor_llm,
207
- user_profile=user_profile,
208
- agent_question=agent_question
209
- )
210
- logging.info(f"[validate_styled_answer] Style validation result: {is_valid}")
211
- return is_valid
212
- except Exception as e:
213
- logging.exception("[validate_styled_answer] Exception during style validation")
214
- return False
 
6
  from langchain_groq import ChatGroq
7
 
8
 
9
+ def matches_user_speaking_style(answer, processor_llm, user_profile, agent_question):
10
  """
11
+ Uses the LLM to determine if the answer matches the expected tone and style
12
+ based on the user's communication profile.
13
+ Returns True if it is first-person and stylistically aligned, False otherwise.
14
  """
15
  logging.info("[Style Match Check] Entry")
16
+
17
  try:
18
+ # --- Step 1: First-person check ---
19
  fp_prompt = f"""
20
  You are an expert in analysing writing style and narrative perspective.
21
  Determine whether the following response is written from a first-person point of view.
 
24
 
25
  Response:
26
  \"\"\"{answer}\"\"\"
27
+
28
  Output format:
29
  First Person: Yes
30
  or
 
36
  logging.warning("[Style Match Check] Failed first-person test")
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
  return True
49
 
50
+ # --- Step 3: Extract user communication profile ---
51
+ style = user_profile.get_field("Communication", "Style")
52
+ tone = user_profile.get_field("Communication", "Tone")
53
+ length = user_profile.get_field("Communication", "Length")
54
+ topics = user_profile.get_field("Communication", "Topics")
55
 
56
+ # --- Step 4: Style validation prompt ---
57
+ style_check_prompt = f"""
58
+ You are a communication coach and writing style analyst.
59
 
60
+ Evaluate whether the following response aligns with the given communication profile.
 
 
61
 
62
  ---
63
+ ### Response:
64
  \"\"\"{answer}\"\"\"
65
 
66
+ ### Communication Profile:
 
67
  - Style: {style}
68
  - Tone: {tone}
69
  - Preferred Length: {length}
70
+ - Common Topics: {topics}
71
 
72
  ---
73
  ### Instructions:
74
+ - Does the writing match the tone, style, and sentence structure described?
75
+ - Is the language aligned in formality, expressiveness, and delivery?
76
+ - Respond only with one of the following:
 
 
77
 
78
  Style Match: Yes
79
  or
80
  Style Match: No
81
  """
82
+ logging.info("[Style Match Check] Invoking LLM for profile-based style check")
83
+ style_response = processor_llm.invoke(style_check_prompt)
84
+ style_result = style_response.content.strip().lower()
85
 
86
+ if "style match: yes" in style_result:
 
 
 
 
87
  logging.info("[Style Match Check] Match confirmed")
88
  return True
89
+ elif "style match: no" in style_result:
90
  logging.info("[Style Match Check] Style mismatch detected")
91
  return False
92
  else:
93
+ logging.warning(f"[Style Match Check] Unexpected output format: {style_result}")
94
  return False
95
 
96
  except Exception as e:
97
+ logging.error(f"[Style Match Check] Exception occurred: {e}")
98
  return False
99
 
100
 
101
+
102
  def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
103
  llm_mode_prompt = f"""
104
  You are an expert in market research interview analysis. Given the following question, determine if it is:
 
187
  return False
188
 
189