Spaces:
Sleeping
Sleeping
# groq_api.py | |
import os | |
import requests | |
# β Load API key securely from environment variable | |
GROQ_API_KEY = "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" # DO NOT hardcode keys! | |
def summarize_match(job_description, cv_names, cv_snippets): | |
if not GROQ_API_KEY: | |
return "β GROQ_API_KEY not set. Please set it as an environment variable." | |
try: | |
# β Limit job description and snippet size for Groq token limits | |
job_description = job_description.strip()[:600] or "[No description provided]" | |
cv_names = [name[:60] for name in cv_names[:3]] | |
cv_snippets = [(text.strip()[:400] or "[No content]") for text in cv_snippets[:3]] | |
# β Combine into readable prompt | |
cv_info = "\n\n".join([ | |
f"{cv_names[i]}:\n{cv_snippets[i]}" | |
for i in range(len(cv_names)) | |
]) | |
prompt = f""" | |
You are an AI recruiter assistant. | |
Here is the job description: | |
--- | |
{job_description} | |
--- | |
Here are the top 3 candidate CVs (partial content): | |
{cv_info} | |
Summarize why these candidates may be a good fit for this role. | |
""".strip() | |
# β Optional debug info | |
print("π¦ Prompt length (chars):", len(prompt)) | |
if len(prompt) > 8000: | |
return "β Prompt too long for Groq (must be under 8K characters)." | |
# β Send request to Groq API | |
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", # β Correct Groq model | |
"messages": [{"role": "user", "content": prompt}], | |
"temperature": 0.5 | |
}, | |
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}" | |