saherPervaiz commited on
Commit
5c62f99
Β·
verified Β·
1 Parent(s): 0af8062

Update groq_api.py

Browse files
Files changed (1) hide show
  1. groq_api.py +29 -30
groq_api.py CHANGED
@@ -1,47 +1,48 @@
1
- # groq_api.py
2
-
3
  import os
4
  import requests
5
 
6
- # βœ… Load API key securely from environment variable
7
- GROQ_API_KEY = "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" # DO NOT hardcode keys!
8
 
9
  def summarize_match(job_description, cv_names, cv_snippets):
10
  if not GROQ_API_KEY:
11
- return "❌ GROQ_API_KEY not set. Please set it as an environment variable."
12
 
13
  try:
14
- # βœ… Limit job description and snippet size for Groq token limits
15
- job_description = job_description.strip()[:600] or "[No description provided]"
16
  cv_names = [name[:60] for name in cv_names[:3]]
17
- cv_snippets = [(text.strip()[:400] or "[No content]") for text in cv_snippets[:3]]
18
-
19
- # βœ… Combine into readable prompt
20
- cv_info = "\n\n".join([
21
- f"{cv_names[i]}:\n{cv_snippets[i]}"
22
- for i in range(len(cv_names))
23
- ])
24
 
 
25
  prompt = f"""
26
- You are an AI recruiter assistant.
27
 
28
- Here is the job description:
29
- ---
30
  {job_description}
31
- ---
32
 
33
- Here are the top 3 candidate CVs (partial content):
34
- {cv_info}
 
 
 
 
 
 
 
35
 
36
- Summarize why these candidates may be a good fit for this role.
 
 
 
 
 
37
  """.strip()
38
 
39
- # βœ… Optional debug info
40
- print("πŸ“¦ Prompt length (chars):", len(prompt))
41
  if len(prompt) > 8000:
42
- return "❌ Prompt too long for Groq (must be under 8K characters)."
43
 
44
- # βœ… Send request to Groq API
45
  response = requests.post(
46
  url="https://api.groq.com/openai/v1/chat/completions",
47
  headers={
@@ -49,9 +50,9 @@ Summarize why these candidates may be a good fit for this role.
49
  "Content-Type": "application/json"
50
  },
51
  json={
52
- "model": "llama3-8b-8192", # βœ… Correct Groq model
53
  "messages": [{"role": "user", "content": prompt}],
54
- "temperature": 0.5
55
  },
56
  timeout=30
57
  )
@@ -59,7 +60,5 @@ Summarize why these candidates may be a good fit for this role.
59
  response.raise_for_status()
60
  return response.json()["choices"][0]["message"]["content"]
61
 
62
- except requests.exceptions.RequestException as e:
63
- return f"❌ Groq API error: {e}"
64
  except Exception as e:
65
- return f"❌ Unexpected error: {e}"
 
 
 
1
  import os
2
  import requests
3
 
4
+ GROQ_API_KEY = os.getenv("GROQ_API_KEY") or "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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}
 
21
 
22
+ ### Candidate CVs:
23
+ 1. {cv_names[0]}:
24
+ {cv_snippets[0]}
25
+
26
+ 2. {cv_names[1]}:
27
+ {cv_snippets[1]}
28
+
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
  "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
  },
57
  timeout=30
58
  )
 
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}"