Basitha commited on
Commit
87e16d4
·
verified ·
1 Parent(s): c50db9e

Update common/validation_utils.py

Browse files
Files changed (1) hide show
  1. common/validation_utils.py +32 -9
common/validation_utils.py CHANGED
@@ -7,17 +7,40 @@ from RespondentAgent import *
7
  from langchain_groq import ChatGroq
8
 
9
 
10
- def is_first_person(answer):
11
  """
12
- Checks if the answer is written in first person.
13
- Returns True if first person pronouns are found and third person references are not dominant.
14
  """
15
- logging.debug(f"Checking if answer is first person: {answer}")
16
- # Look for first person pronouns
17
- first_person = re.search(r'\b(I|my|me|mine|we|our|us|ours)\b', answer, re.IGNORECASE)
18
- # Look for third person references (e.g., "he", "she", "they", or a capitalized name at the start)
19
- third_person = re.search(r'\b(he|she|they|his|her|their|him|them)\b', answer, re.IGNORECASE)
20
- return bool(first_person) and not bool(third_person)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
 
23
  def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):
 
7
  from langchain_groq import ChatGroq
8
 
9
 
10
+ def is_first_person_llm(answer, processor_llm):
11
  """
12
+ Uses LLM to determine whether the response is written in first person.
13
+ Returns True if the LLM determines the response is first-person, else False.
14
  """
15
+ prompt = f"""
16
+ You are an expert in analyzing writing style and narrative perspective.
17
+
18
+ Determine whether the following response is written from a first-person point of view.
19
+ A first-person response includes pronouns such as "I", "me", "my", "mine", "we", "our", or "us" and is written from the perspective of the speaker.
20
+
21
+ Do not guess. Only say "Yes" if the writing is clearly in first person. Otherwise, say "No".
22
+
23
+ Response:
24
+ \"\"\"{answer}\"\"\"
25
+
26
+ Output strictly in the following format:
27
+ First Person: Yes
28
+ or
29
+ First Person: No
30
+ """
31
+ try:
32
+ response = processor_llm.invoke(prompt)
33
+ content = response.content.strip().lower()
34
+ if "first person: yes" in content:
35
+ return True
36
+ elif "first person: no" in content:
37
+ return False
38
+ else:
39
+ logging.warning(f"Unexpected output format from LLM for first person check: {content}")
40
+ return False
41
+ except Exception as e:
42
+ logging.error(f"LLM failed during first person check: {e}")
43
+ return False
44
 
45
 
46
  def validate_response(question, answer, user_profile_str, fast_facts_str, interview_transcript_text, respondent_type, ai_evaluator_agent, processor_llm):