File size: 2,856 Bytes
c0029e4
3120871
c0029e4
 
 
 
06b8d86
c0029e4
 
26b070d
 
 
 
 
 
 
 
 
 
 
3120871
c0029e4
3120871
 
 
26b070d
 
 
c0029e4
 
 
26b070d
 
 
 
 
 
 
 
c0029e4
 
3120871
26b070d
 
 
 
 
 
c0029e4
3120871
c0029e4
3120871
 
 
26b070d
3120871
 
 
c0029e4
26b070d
 
 
 
 
 
 
 
 
 
 
 
 
c0029e4
26b070d
 
 
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
import os
import re
import google.generativeai as genai
import json

genai.configure(api_key=os.environ["GEMINI_API_KEY"])
model = genai.GenerativeModel("models/gemini-2.0-flash")

def rerank(query, candidates):
    # Add debug print
    print(f"Reranking {len(candidates)} candidates")
    if candidates:
        print(f"First candidate keys: {candidates[0].keys()}")
    
    # Ensure candidates have all required fields
    for candidate in candidates:
        for field in ["url", "adaptive_support", "remote_support", "description", "duration", "test_type"]:
            if field not in candidate or candidate[field] is None:
                candidate[field] = "N/A" if field != "test_type" else []
    
    prompt = f""" 
    Job description: "{query}"
    
    Candidate SHL assessments: {json.dumps(candidates, indent=2)}
    
    Rank the most relevant assessments for this job description and return a JSON object with the top 10 (or fewer if there aren't 10 good matches).
    
    Your response must be ONLY valid JSON in this exact format:
    {{
      "recommended_assessments": [
        {{
          "assessment_name": "Name of the assessment",
          "url": "https://www.shl.com/...",
          "remote_support": "Yes/No",
          "adaptive_support": "Yes/No",
          "duration": "duration value",
          "test_type": "test type value"
        }},
        ...more assessments...
      ]
    }}
    
    IMPORTANT RULES:
    1. Return ONLY valid JSON without any markdown, explanations, or code blocks
    2. Copy the URL exactly as provided in the input - do not modify URLs
    3. Include at most 10 assessments, ranked by relevance to the job description
    4. If a field is missing in the input, use "N/A" as the value
    5. Make sure each assessment has all required fields
    """
    
    response = model.generate_content(prompt)
    response_text = response.text
    
    # Try to extract JSON from possible markdown code blocks
    json_match = re.search(r'``````', response_text, re.DOTALL)
    if json_match:
        response_text = json_match.group(1).strip()
    
    try:
        result = json.loads(response_text)
        
        # Validate the structure
        if "recommended_assessments" not in result:
            result = {"recommended_assessments": []}
        
        # Ensure all required fields exist in each assessment
        for assessment in result["recommended_assessments"]:
            for field in ["assessment_name", "url", "remote_support", "adaptive_support", "duration", "test_type"]:
                if field not in assessment:
                    assessment[field] = "N/A"
        
        return result
    except Exception as e:
        print(f"Error parsing JSON: {e}")
        print(f"Raw response: {response_text}")
        return {"error": str(e), "recommended_assessments": []}