AnshulS commited on
Commit
26b070d
·
verified ·
1 Parent(s): e5766c5

Update reranker.py

Browse files
Files changed (1) hide show
  1. reranker.py +45 -16
reranker.py CHANGED
@@ -7,41 +7,70 @@ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
7
  model = genai.GenerativeModel("models/gemini-2.0-flash")
8
 
9
  def rerank(query, candidates):
 
 
 
 
 
 
 
 
 
 
 
10
  prompt = f"""
11
  Job description: "{query}"
12
 
13
  Candidate SHL assessments: {json.dumps(candidates, indent=2)}
14
 
15
- Rank the most relevant assessments and return a JSON list in this format:
 
 
16
  {{
17
  "recommended_assessments": [
18
  {{
19
- "url": ...,
20
- "adaptive_support": ...,
21
- "remote_support": ...,
22
- "description": ...,
23
- "duration": ...,
24
- "test_type": [...]
25
- }}
 
26
  ]
27
  }}
28
 
29
- Important instructions:
30
- 1. Return ONLY valid JSON without any markdown code blocks or extra text
31
- 2. The url field MUST be copied exactly as provided in the candidates, do not modify it
32
- 3. Make sure to include the full URL value as provided in the input candidates
33
- 4. Preserve all original data fields and their exact values (especially URLs)
 
34
  """
35
 
36
  response = model.generate_content(prompt)
37
  response_text = response.text
38
 
39
  # Try to extract JSON from possible markdown code blocks
40
- json_match = re.search(r'```(?:json)?\s*(.*?)```', response_text, re.DOTALL)
41
  if json_match:
42
  response_text = json_match.group(1).strip()
43
 
44
  try:
45
- return json.loads(response_text)
 
 
 
 
 
 
 
 
 
 
 
 
46
  except Exception as e:
47
- return {"error": str(e), "raw_response": response_text}
 
 
 
7
  model = genai.GenerativeModel("models/gemini-2.0-flash")
8
 
9
  def rerank(query, candidates):
10
+ # Add debug print
11
+ print(f"Reranking {len(candidates)} candidates")
12
+ if candidates:
13
+ print(f"First candidate keys: {candidates[0].keys()}")
14
+
15
+ # Ensure candidates have all required fields
16
+ for candidate in candidates:
17
+ for field in ["url", "adaptive_support", "remote_support", "description", "duration", "test_type"]:
18
+ if field not in candidate or candidate[field] is None:
19
+ candidate[field] = "N/A" if field != "test_type" else []
20
+
21
  prompt = f"""
22
  Job description: "{query}"
23
 
24
  Candidate SHL assessments: {json.dumps(candidates, indent=2)}
25
 
26
+ 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).
27
+
28
+ Your response must be ONLY valid JSON in this exact format:
29
  {{
30
  "recommended_assessments": [
31
  {{
32
+ "assessment_name": "Name of the assessment",
33
+ "url": "https://www.shl.com/...",
34
+ "remote_support": "Yes/No",
35
+ "adaptive_support": "Yes/No",
36
+ "duration": "duration value",
37
+ "test_type": "test type value"
38
+ }},
39
+ ...more assessments...
40
  ]
41
  }}
42
 
43
+ IMPORTANT RULES:
44
+ 1. Return ONLY valid JSON without any markdown, explanations, or code blocks
45
+ 2. Copy the URL exactly as provided in the input - do not modify URLs
46
+ 3. Include at most 10 assessments, ranked by relevance to the job description
47
+ 4. If a field is missing in the input, use "N/A" as the value
48
+ 5. Make sure each assessment has all required fields
49
  """
50
 
51
  response = model.generate_content(prompt)
52
  response_text = response.text
53
 
54
  # Try to extract JSON from possible markdown code blocks
55
+ json_match = re.search(r'``````', response_text, re.DOTALL)
56
  if json_match:
57
  response_text = json_match.group(1).strip()
58
 
59
  try:
60
+ result = json.loads(response_text)
61
+
62
+ # Validate the structure
63
+ if "recommended_assessments" not in result:
64
+ result = {"recommended_assessments": []}
65
+
66
+ # Ensure all required fields exist in each assessment
67
+ for assessment in result["recommended_assessments"]:
68
+ for field in ["assessment_name", "url", "remote_support", "adaptive_support", "duration", "test_type"]:
69
+ if field not in assessment:
70
+ assessment[field] = "N/A"
71
+
72
+ return result
73
  except Exception as e:
74
+ print(f"Error parsing JSON: {e}")
75
+ print(f"Raw response: {response_text}")
76
+ return {"error": str(e), "recommended_assessments": []}