Spaces:
Sleeping
Sleeping
Update groq_api.py
Browse files- 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 |
-
|
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.
|
12 |
|
13 |
try:
|
14 |
-
#
|
15 |
-
|
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
|
27 |
|
28 |
-
|
29 |
-
---
|
30 |
{job_description}
|
31 |
-
---
|
32 |
|
33 |
-
|
34 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
37 |
""".strip()
|
38 |
|
39 |
-
#
|
40 |
-
print("π¦ Prompt length
|
41 |
if len(prompt) > 8000:
|
42 |
-
return "β Prompt too long
|
43 |
|
44 |
-
#
|
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": "
|
53 |
"messages": [{"role": "user", "content": prompt}],
|
54 |
-
"temperature": 0.
|
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"β
|
|
|
|
|
|
|
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}"
|