AnshulS commited on
Commit
3120871
·
verified ·
1 Parent(s): 04fa7f5

Update reranker.py

Browse files
Files changed (1) hide show
  1. reranker.py +26 -13
reranker.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import google.generativeai as genai
3
  import json
4
 
@@ -6,29 +7,41 @@ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
6
  model = genai.GenerativeModel("models/gemini-2.0-flash")
7
 
8
  def rerank(query, candidates):
9
- prompt = f"""
10
  Job description: "{query}"
11
-
12
- Candidate SHL assessments:
13
- {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
-
30
  response = model.generate_content(prompt)
 
 
 
 
 
 
 
31
  try:
32
- return json.loads(response.text)
33
  except Exception as e:
34
- return {"error": str(e), "raw_response": response.text}
 
1
  import os
2
+ import re
3
  import google.generativeai as genai
4
  import json
5
 
 
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}