saherPervaiz commited on
Commit
e791f2f
Β·
verified Β·
1 Parent(s): ff6a9c2

Update groq_api.py

Browse files
Files changed (1) hide show
  1. groq_api.py +23 -17
groq_api.py CHANGED
@@ -1,20 +1,27 @@
1
  import os
2
  import requests
3
 
4
- GROQ_API_KEY = "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"
 
5
 
6
  def summarize_match(job_description, cv_names, cv_snippets):
7
  if not GROQ_API_KEY:
8
  return "❌ GROQ_API_KEY not set."
9
 
10
  try:
11
- # Limit content length per CV to avoid token overflow
12
- cv_snippets = [text.strip()[:1500] or "[No content]" for text in cv_snippets[:3]]
 
 
 
 
 
13
  cv_names = [name[:60] for name in cv_names[:3]]
 
14
 
15
- # Create structured prompt
16
  prompt = f"""
17
- You are an AI recruitment assistant helping to match candidates to job descriptions.
18
 
19
  ### Job Description:
20
  {job_description}
@@ -29,20 +36,17 @@ You are an AI recruitment assistant helping to match candidates to job descripti
29
  3. {cv_names[2]}:
30
  {cv_snippets[2]}
31
 
32
- Analyze how well each candidate matches the job requirements, especially in terms of:
33
- - PHP programming
34
- - Software or web development
35
- - Relevant technical experience
36
-
37
- Clearly identify which candidates are suitable and why.
38
  """.strip()
39
 
40
- # Debug info (optional)
41
- print("πŸ“¦ Prompt length:", len(prompt))
42
  if len(prompt) > 8000:
43
- return "❌ Prompt too long. Please shorten the CVs or JD."
44
 
45
- # Groq API call
46
  response = requests.post(
47
  url="https://api.groq.com/openai/v1/chat/completions",
48
  headers={
@@ -50,7 +54,7 @@ Clearly identify which candidates are suitable and why.
50
  "Content-Type": "application/json"
51
  },
52
  json={
53
- "model": "mixtral-8x7b-32768", # Or change to a different supported Groq model
54
  "messages": [{"role": "user", "content": prompt}],
55
  "temperature": 0.4
56
  },
@@ -60,5 +64,7 @@ Clearly identify which candidates are suitable and why.
60
  response.raise_for_status()
61
  return response.json()["choices"][0]["message"]["content"]
62
 
63
- except Exception as e:
64
  return f"❌ Groq API error: {e}"
 
 
 
1
  import os
2
  import requests
3
 
4
+ # Use environment variable or fallback key
5
+ GROQ_API_KEY = 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"
6
 
7
  def summarize_match(job_description, cv_names, cv_snippets):
8
  if not GROQ_API_KEY:
9
  return "❌ GROQ_API_KEY not set."
10
 
11
  try:
12
+ # Ensure we have 3 entries
13
+ while len(cv_names) < 3:
14
+ cv_names.append("[No CV]")
15
+ cv_snippets.append("[No content]")
16
+
17
+ # Trim content safely
18
+ job_description = job_description.strip()[:1000] or "[No description provided]"
19
  cv_names = [name[:60] for name in cv_names[:3]]
20
+ cv_snippets = [(text.strip()[:1500] or "[No content]") for text in cv_snippets[:3]]
21
 
22
+ # Construct prompt
23
  prompt = f"""
24
+ You are an AI recruitment assistant helping match CVs to job descriptions.
25
 
26
  ### Job Description:
27
  {job_description}
 
36
  3. {cv_names[2]}:
37
  {cv_snippets[2]}
38
 
39
+ Based on the job requirements, analyze each candidate and explain which (if any) are suitable. Focus on:
40
+ - PHP experience
41
+ - Web/software development
42
+ - Technical relevance
 
 
43
  """.strip()
44
 
45
+ # Safety: Truncate if prompt is too long
 
46
  if len(prompt) > 8000:
47
+ prompt = prompt[:8000]
48
 
49
+ # Make Groq API request
50
  response = requests.post(
51
  url="https://api.groq.com/openai/v1/chat/completions",
52
  headers={
 
54
  "Content-Type": "application/json"
55
  },
56
  json={
57
+ "model": "mixtral-8x7b-32768",
58
  "messages": [{"role": "user", "content": prompt}],
59
  "temperature": 0.4
60
  },
 
64
  response.raise_for_status()
65
  return response.json()["choices"][0]["message"]["content"]
66
 
67
+ except requests.exceptions.RequestException as e:
68
  return f"❌ Groq API error: {e}"
69
+ except Exception as e:
70
+ return f"❌ Unexpected error: {e}"