saherPervaiz commited on
Commit
b908f19
Β·
verified Β·
1 Parent(s): a110ef4

Update groq_api.py

Browse files
Files changed (1) hide show
  1. groq_api.py +15 -12
groq_api.py CHANGED
@@ -3,19 +3,20 @@
3
  import os
4
  import requests
5
 
6
- GROQ_API_KEY = "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"
 
7
 
8
  def summarize_match(job_description, cv_names, cv_snippets):
9
  if not GROQ_API_KEY:
10
- return "❌ GROQ_API_KEY not set."
11
 
12
  try:
13
- # Clean + limit all text
14
- job_description = job_description.strip()[:600] or "[No description]"
15
  cv_names = [name[:60] for name in cv_names[:3]]
16
  cv_snippets = [(text.strip()[:400] or "[No content]") for text in cv_snippets[:3]]
17
 
18
- # Assemble cleaned prompt
19
  cv_info = "\n\n".join([
20
  f"{cv_names[i]}:\n{cv_snippets[i]}"
21
  for i in range(len(cv_names))
@@ -29,18 +30,18 @@ Here is the job description:
29
  {job_description}
30
  ---
31
 
32
- Top 3 CV snippets:
33
  {cv_info}
34
 
35
- Summarize why these candidates may be a good fit.
36
  """.strip()
37
 
38
- # Show debugging info
39
- print("πŸ“¦ Prompt length:", len(prompt))
40
  if len(prompt) > 8000:
41
  return "❌ Prompt too long for Groq (must be under 8K characters)."
42
 
43
- # Send to Groq API
44
  response = requests.post(
45
  url="https://api.groq.com/openai/v1/chat/completions",
46
  headers={
@@ -48,7 +49,7 @@ Summarize why these candidates may be a good fit.
48
  "Content-Type": "application/json"
49
  },
50
  json={
51
- "model": "mixtral-8x7b-32768",
52
  "messages": [{"role": "user", "content": prompt}],
53
  "temperature": 0.5
54
  },
@@ -58,5 +59,7 @@ Summarize why these candidates may be a good fit.
58
  response.raise_for_status()
59
  return response.json()["choices"][0]["message"]["content"]
60
 
61
- except Exception as e:
62
  return f"❌ Groq API error: {e}"
 
 
 
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))
 
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
  "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
  },
 
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}"