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."