File size: 10,935 Bytes
9df97a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/usr/bin/env python3
"""
Test Chatbot Quality on Real Recruiter Scenarios

This script tests the chatbot on 3 real recruiter scenarios:
1. "Explain why candidate X matches job Y"
2. "Compare candidate A vs candidate B for role Z"
3. "What is the ideal profile for this job?"

Requires: ANTHROPIC_API_KEY environment variable set
"""

import json
import sys
import os
from pathlib import Path

# Add backend to path
sys.path.insert(0, str(Path(__file__).parent / "backend"))

from anthropic import Anthropic
from app.models import Job, Candidate
from app.services.matching_service import MatchingService
from app.schemas import CandidateProfile
from ai_module.nlp.enhanced_skill_extractor import EnhancedSkillExtractor
from ai_module.matching.semantic_matcher import SemanticSkillMatcher


class ChatbotQualityTester:
    """Test chatbot quality on recruiter scenarios."""

    def __init__(self, api_key: str = None):
        """Initialize with Anthropic API key."""
        api_key = api_key or os.getenv("ANTHROPIC_API_KEY")
        if not api_key:
            raise ValueError("ANTHROPIC_API_KEY not set")
        
        self.client = Anthropic()
        self.conversation_history = []
        self.skill_extractor = EnhancedSkillExtractor(load_ner=False)

    def reset_conversation(self):
        """Reset conversation history for new scenario."""
        self.conversation_history = []

    def _chat(self, user_message: str) -> str:
        """Send message to Claude and get response."""
        self.conversation_history.append({
            "role": "user",
            "content": user_message
        })
        
        response = self.client.messages.create(
            model="claude-3-5-sonnet-20241022",
            max_tokens=1024,
            system="""You are an expert HR recruiter assistant. You help recruiters understand candidate-job matches, 
compare candidates, and define ideal profiles. Be concise but insightful. Focus on:
- Technical skill alignment
- Experience relevance
- Growth potential
- Risk factors""",
            messages=self.conversation_history
        )
        
        assistant_message = response.content[0].text
        self.conversation_history.append({
            "role": "assistant",
            "content": assistant_message
        })
        
        return assistant_message

    def scenario_1_explain_match(self):
        """Scenario 1: Explain why candidate matches job."""
        print("\n" + "="*70)
        print("SCENARIO 1: Explain Candidate-Job Match")
        print("="*70)
        
        self.reset_conversation()
        
        # Sample candidate
        candidate_cv = """
        Senior Python Developer
        Skills: Python 10 years, FastAPI 4 years, Docker, Kubernetes, PostgreSQL, Redis
        Experience: 
        - Led team of 5 developers at TechCorp (3 years)
        - Built microservices architecture serving 1M+ users
        - Open source contributor (Flask, requests)
        """
        
        # Sample job
        job_description = """
        Senior Backend Engineer - Python/FastAPI
        Location: Remote
        Responsibilities:
        - Design and implement scalable APIs
        - Lead technical decisions for backend team
        - Mentor junior developers
        Requirements:
        - 5+ years Python experience
        - FastAPI or similar framework
        - Docker & container orchestration knowledge
        - Team leadership experience
        """
        
        # Extract skills from CV
        extracted_skills = self.skill_extractor.extract_skills_hybrid(candidate_cv)
        
        # Create prompt
        prompt = f"""
I have a candidate with this profile:
{candidate_cv}

Extracted skills: {', '.join(extracted_skills[:10])}

For this job:
{job_description}

Explain why this candidate is a good or bad fit, in 3-4 sentences. Focus on skill alignment and experience.
"""
        
        print("\nπŸ“‹ Candidate CV:")
        print(candidate_cv)
        print("\nπŸ“‹ Job Description:")
        print(job_description)
        print(f"\nπŸ” Extracted skills: {', '.join(extracted_skills[:8])}")
        
        response = self._chat(prompt)
        print(f"\nπŸ’¬ Chatbot Analysis:\n{response}")
        
        # Follow-up question
        follow_up = "What are the top 3 risks with this candidate?"
        print(f"\n❓ Follow-up: {follow_up}")
        response2 = self._chat(follow_up)
        print(f"πŸ’¬ Response:\n{response2}")
        
        return {
            "scenario": "explain_match",
            "initial_response": response,
            "followup_response": response2,
            "status": "βœ… SUCCESS"
        }

    def scenario_2_compare_candidates(self):
        """Scenario 2: Compare two candidates for same role."""
        print("\n" + "="*70)
        print("SCENARIO 2: Compare Candidates for Same Role")
        print("="*70)
        
        self.reset_conversation()
        
        candidate_a = """
        Software Engineer
        Skills: Python 8 years, Django 5 years, JavaScript, React, AWS, PostgreSQL
        Experience: 
        - Full-stack developer at StartupX (4 years)
        - Shipped 3 major products
        - No team leadership experience
        - Bachelor's in CS
        """
        
        candidate_b = """
        Tech Lead
        Skills: Python 6 years, FastAPI 3 years, Docker, Kubernetes, AWS, Team leadership
        Experience:
        - Led backend team of 3 at EstablishedCorp (2 years)
        - Backend architect, migrated monolith to microservices
        - 2 years team leadership
        - Master's in Computer Science
        """
        
        role_desc = """
        Senior Backend Engineer - Team Leadership Track
        - 5+ years backend development
        - Team leadership experience preferred
        - FastAPI or similar modern framework
        - Cloud deployment (AWS)
        """
        
        prompt = f"""
Compare these 2 candidates for this role:

**Candidate A:**
{candidate_a}

**Candidate B:**
{candidate_b}

**Role:**
{role_desc}

Which candidate is better suited? Create a quick comparison table with pros/cons.
"""
        
        print("\nπŸ‘€ Candidate A:")
        print(candidate_a)
        print("\nπŸ‘€ Candidate B:")
        print(candidate_b)
        print("\nπŸ“‹ Role Description:")
        print(role_desc)
        
        response = self._chat(prompt)
        print(f"\nπŸ’¬ Comparison:\n{response}")
        
        # Follow-up
        follow_up = "If I can only hire one, who should it be and why?"
        print(f"\n❓ Follow-up: {follow_up}")
        response2 = self._chat(follow_up)
        print(f"πŸ’¬ Response:\n{response2}")
        
        return {
            "scenario": "compare_candidates",
            "initial_response": response,
            "followup_response": response2,
            "status": "βœ… SUCCESS"
        }

    def scenario_3_ideal_profile(self):
        """Scenario 3: Define ideal profile for role."""
        print("\n" + "="*70)
        print("SCENARIO 3: Define Ideal Profile for Role")
        print("="*70)
        
        self.reset_conversation()
        
        job_description = """
        Data Engineer
        Location: San Francisco
        We're building a real-time data pipeline for a high-frequency trading platform.
        
        Responsibilities:
        - Design and maintain ETL pipelines
        - Build data infrastructure on cloud
        - Optimize query performance
        - Mentor data analysts
        
        Tech stack: Python, Spark, Kafka, PostgreSQL, GCP, Airflow
        
        Company: 5-year-old fintech startup, $200M funding
        """
        
        prompt = f"""
Describe the ideal candidate profile for this role. Consider:
- Technical skills (specific tools, languages)
- Experience depth needed
- Soft skills
- Team fit
- Growth potential

Role details:
{job_description}

Be specific: what's the exact experience level, what tools matter most?
"""
        
        print("\nπŸ“‹ Job Description:")
        print(job_description)
        
        response = self._chat(prompt)
        print(f"\nπŸ’¬ Ideal Profile:\n{response}")
        
        # Follow-up
        follow_up = "How would you weight these requirements? Which are must-have vs nice-to-have?"
        print(f"\n❓ Follow-up: {follow_up}")
        response2 = self._chat(follow_up)
        print(f"πŸ’¬ Response:\n{response2}")
        
        return {
            "scenario": "ideal_profile",
            "initial_response": response,
            "followup_response": response2,
            "status": "βœ… SUCCESS"
        }


def main():
    """Run all chatbot scenarios."""
    print("\nπŸ€– AI Talent Finder β€” Chatbot Quality Testing")
    print("Testing 3 real recruiter scenarios")
    
    # Check API key
    api_key = os.getenv("ANTHROPIC_API_KEY")
    if not api_key:
        print("\n❌ ERROR: ANTHROPIC_API_KEY not set")
        print("Export it: export ANTHROPIC_API_KEY='sk-...'")
        return 1
    
    print(f"βœ… Anthropic API configured")
    
    try:
        tester = ChatbotQualityTester(api_key)
    except Exception as e:
        print(f"❌ Failed to initialize chatbot: {e}")
        return 1
    
    results = []
    
    # Run scenarios
    try:
        result1 = tester.scenario_1_explain_match()
        results.append(result1)
    except Exception as e:
        print(f"\n❌ Scenario 1 failed: {e}")
        results.append({"scenario": "explain_match", "status": f"❌ FAILED: {e}"})
    
    try:
        result2 = tester.scenario_2_compare_candidates()
        results.append(result2)
    except Exception as e:
        print(f"\n❌ Scenario 2 failed: {e}")
        results.append({"scenario": "compare_candidates", "status": f"❌ FAILED: {e}"})
    
    try:
        result3 = tester.scenario_3_ideal_profile()
        results.append(result3)
    except Exception as e:
        print(f"\n❌ Scenario 3 failed: {e}")
        results.append({"scenario": "ideal_profile", "status": f"❌ FAILED: {e}"})
    
    # Summary
    print("\n" + "="*70)
    print("TEST SUMMARY")
    print("="*70)
    
    success_count = sum(1 for r in results if r.get("status") == "βœ… SUCCESS")
    total_count = len(results)
    
    for r in results:
        scenario = r.get("scenario", "unknown").replace("_", " ").title()
        status = r.get("status", "?")
        print(f"{status} β€” {scenario}")
    
    print(f"\nπŸ“Š Result: {success_count}/{total_count} scenarios passed")
    
    # Save results
    report_path = Path(__file__).parent / "reports" / "chatbot_quality_test.json"
    report_path.parent.mkdir(exist_ok=True)
    
    with open(report_path, "w") as f:
        json.dump(results, f, indent=2)
    
    print(f"πŸ“„ Report saved to: {report_path}")
    
    return 0 if success_count == total_count else 1


if __name__ == "__main__":
    exit_code = main()
    sys.exit(exit_code)