Spaces:
Sleeping
Sleeping
import os | |
import requests | |
# Load from environment variable or fallback | |
GROQ_API_KEY = "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" | |
def summarize_match(job_description, cv_names, cv_snippets): | |
if not GROQ_API_KEY: | |
return "β GROQ_API_KEY not set." | |
try: | |
# Ensure 3 CVs are present (pad if needed) | |
while len(cv_names) < 3: | |
cv_names.append("[No CV]") | |
cv_snippets.append("[No content]") | |
# Truncate content safely | |
job_description = job_description.strip()[:1000] or "[No description provided]" | |
cv_names = [name[:60] for name in cv_names[:3]] | |
cv_snippets = [(text.strip()[:1500] or "[No content]") for text in cv_snippets[:3]] | |
# Compose prompt | |
prompt = f""" | |
You are an AI recruitment assistant helping to evaluate candidates for an open job role. | |
Below is the job description, followed by the top 3 candidate CV snippets. | |
Your task is to assess how well each candidate aligns with the job. Be honest β if a candidate lacks the required skills or experience, clearly say they are not a good fit. | |
### Job Description: | |
{job_description} | |
### Candidate CVs: | |
1. {cv_names[0]}: | |
{cv_snippets[0]} | |
2. {cv_names[1]}: | |
{cv_snippets[1]} | |
3. {cv_names[2]}: | |
{cv_snippets[2]} | |
### Instructions: | |
- Focus on **specific skills**, **technical tools**, **relevant experience**, and **industry fit**. | |
- Clearly identify **who is a good match**, and **why**. | |
- Clearly explain **who is not a match**, and **why not**. | |
- Avoid general language like "strong background" unless it's backed by actual experience. | |
End with a short ranked list like: | |
1. Candidate 2 β Strong Python and ML background | |
2. Candidate 1 β Partial match, good general programming skills | |
3. Candidate 3 β Not relevant to the job | |
""".strip() | |
# Truncate if needed to fit Groqβs limit | |
if len(prompt) > 8000: | |
prompt = prompt[:8000] | |
# Send request to Groq API using LLaMA3 | |
response = requests.post( | |
url="https://api.groq.com/openai/v1/chat/completions", | |
headers={ | |
"Authorization": f"Bearer {GROQ_API_KEY}", | |
"Content-Type": "application/json" | |
}, | |
json={ | |
"model": "llama3-8b-8192", | |
"messages": [ | |
{"role": "system", "content": "You are a helpful recruitment assistant."}, | |
{"role": "user", "content": prompt} | |
], | |
"temperature": 0.4 | |
}, | |
timeout=30 | |
) | |
response.raise_for_status() | |
return response.json()["choices"][0]["message"]["content"] | |
except requests.exceptions.RequestException as e: | |
return f"β Groq API error: {e}" | |
except Exception as e: | |
return f"β Unexpected error: {e}" | |