Spaces:
Sleeping
Sleeping
Update groq_api.py
Browse files- groq_api.py +15 -12
groq_api.py
CHANGED
@@ -3,19 +3,20 @@
|
|
3 |
import os
|
4 |
import requests
|
5 |
|
6 |
-
|
|
|
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 |
-
#
|
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 |
-
#
|
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 |
-
|
33 |
{cv_info}
|
34 |
|
35 |
-
Summarize why these candidates may be a good fit.
|
36 |
""".strip()
|
37 |
|
38 |
-
#
|
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": "
|
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
|
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}"
|