File size: 5,687 Bytes
306dbf8
 
 
8e2bc07
306dbf8
 
8e2bc07
 
306dbf8
8e2bc07
306dbf8
8e2bc07
 
 
306dbf8
 
 
8e2bc07
306dbf8
8e2bc07
306dbf8
8e2bc07
306dbf8
 
 
 
 
8e2bc07
306dbf8
 
8e2bc07
306dbf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e2bc07
306dbf8
8e2bc07
306dbf8
 
 
 
 
 
 
 
 
8e2bc07
306dbf8
 
 
8e2bc07
306dbf8
8e2bc07
306dbf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e2bc07
306dbf8
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from utils import GroqClient

def validate_response_quality(answer, question, section, api_key):
    """
    Assess response quality using qualitative research standards
    Returns: {"score": 1-5, "requires_followup": bool, "reason": str}
    """
    if not answer or not answer.strip():
        return {"score": 1, "requires_followup": True, "reason": "Empty response"}
    
    # Quick rule-based checks first
    answer_clean = answer.strip().lower()
    words = answer_clean.split()
    
    # Very short or generic responses
    if len(words) < 5:
        return {"score": 2, "requires_followup": True, "reason": "Response too brief"}
    
    weak_phrases = ["idk", "i don't know", "dont know", "نمیدونم", "nothing", "none"]
    if answer_clean in weak_phrases:
        return {"score": 1, "requires_followup": True, "reason": "Non-informative response"}
    
    # AI-based quality assessment for borderline cases
    try:
        client = GroqClient(api_key)
        
        system_prompt = f"""You are evaluating an interview response using qualitative research criteria.

QUESTION ({section}): {question}
RESPONSE: {answer}

ASSESSMENT CRITERIA:

1. Specificity (1-5):
   - Does it contain concrete details, examples, or specific situations?
   - Or is it vague and generic?

2. Behavioral Grounding (1-5):
   - Does it describe actual past behavior or current practices?
   - Or is it hypothetical/opinion-based?

3. Depth (1-5):
   - Does it provide rich qualitative data?
   - Or is it superficial?

4. Relevance (1-5):
   - Does it directly address the question asked?
   - Or is it off-topic?

SCORING RUBRIC:
5 = Excellent: Specific, detailed, behaviorally grounded, rich data
4 = Good: Relevant with reasonable detail, some specifics
3 = Acceptable: On-topic but lacks depth or specificity
2 = Weak: Too vague, generic, or hypothetical
1 = Poor: Off-topic, meaningless, or non-response

DECISION RULE:
- Score ≥ 4: Accept and continue
- Score = 3: Accept but could benefit from probing
- Score ≤ 2: Requires follow-up question

Return ONLY JSON:
{{"score": 3, "requires_followup": false, "reason": "Brief rationale"}}"""

        response = client.chat_completion(
            messages=[{"role": "user", "content": system_prompt}],
            temperature=0.3,
            max_tokens=150
        )
        
        response_text = response['choices'][0]['message']['content'].strip()
        
        # Clean JSON
        if response_text.startswith("```"):
            response_text = response_text.split("```")[1]
            if response_text.startswith("json"):
                response_text = response_text[4:]
            response_text = response_text.strip()
        
        import json
        result = json.loads(response_text)
        
        # Ensure requires_followup is set based on score
        if 'requires_followup' not in result:
            result['requires_followup'] = result.get('score', 3) <= 2
        
        return result
        
    except Exception as e:
        print(f"Validation error: {e}")
        # Fallback: accept if not obviously bad
        return {"score": 3, "requires_followup": False, "reason": "Unable to validate"}


def generate_research_followup(original_question, answer, section, attempt_number, api_key):
    """
    Generate intelligent follow-up using established interview techniques
    """
    client = GroqClient(api_key)
    
    if attempt_number == 1:
        # Laddering technique: probe deeper
        system_prompt = f"""You are conducting a customer discovery interview.

ORIGINAL QUESTION: {original_question}
RESPONDENT'S ANSWER: {answer}
SECTION: {section}

The answer lacks sufficient depth or specificity. Use the "laddering" technique to probe deeper:

LADDERING APPROACHES:
1. Ask for specific example: "Can you give me a specific example of when that happened?"
2. Ask about context: "Help me understand the situation better - what else was going on?"
3. Ask about causation: "What led to that?" or "What happened as a result?"
4. Five Whys technique: "Why is that important to you?"

Generate ONE follow-up question that:
- Probes deeper into their answer
- Asks for concrete examples or specific instances
- Maintains rapport and curiosity (not interrogation)
- Uses past-tense or present-reality (not hypothetical)

Return ONLY the question, no preamble."""

    else:  # attempt_number == 2
        # Projective technique: use scenario
        system_prompt = f"""You are conducting a customer discovery interview.

ORIGINAL QUESTION: {original_question}  
PREVIOUS ANSWER: {answer}
SECTION: {section}

The respondent is struggling to provide depth. Use a projective technique:

PROJECTIVE APPROACHES:
1. Third-person: "How do you think someone like you typically handles [situation]?"
2. Concrete scenario: "Imagine it's [specific time]. Walk me through what you do."
3. Comparison: "Tell me about the last time this went well vs the last time it didn't."

Generate ONE follow-up that:
- Makes the question more concrete and relatable
- Uses a realistic scenario or comparison
- Makes it easier for them to share specific details
- Stays focused on the empathy map section: {section}

Return ONLY the question, no preamble."""
    
    try:
        response = client.chat_completion(
            messages=[{"role": "user", "content": system_prompt}],
            temperature=0.8,
            max_tokens=200
        )
        
        return response['choices'][0]['message']['content'].strip()
    except Exception as e:
        print(f"Follow-up generation error: {e}")
        # Fallback
        return f"Could you tell me more about that? I'd love to hear a specific example."