Spaces:
Sleeping
Sleeping
Update reranker.py
Browse files- reranker.py +16 -45
reranker.py
CHANGED
@@ -7,70 +7,41 @@ 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 |
-
# 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
|
27 |
-
|
28 |
-
Your response must be ONLY valid JSON in this exact format:
|
29 |
{{
|
30 |
"recommended_assessments": [
|
31 |
{{
|
32 |
-
"
|
33 |
-
"
|
34 |
-
"remote_support":
|
35 |
-
"
|
36 |
-
"duration":
|
37 |
-
"test_type":
|
38 |
-
}}
|
39 |
-
...more assessments...
|
40 |
]
|
41 |
}}
|
42 |
|
43 |
-
|
44 |
-
1. Return ONLY valid JSON without any markdown
|
45 |
-
2.
|
46 |
-
3.
|
47 |
-
4.
|
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'
|
56 |
if json_match:
|
57 |
response_text = json_match.group(1).strip()
|
58 |
|
59 |
try:
|
60 |
-
|
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 |
-
|
75 |
-
print(f"Raw response: {response_text}")
|
76 |
-
return {"error": str(e), "recommended_assessments": []}
|
|
|
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}
|
|
|
|