import os import requests # Set your Groq API Key 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: # Truncate job description job_description = job_description.strip()[:800] or "[No description provided]" # Truncate each CV snippet cv_snippets = [(text.strip()[:700] or "[No content]") for text in cv_snippets] cv_names = [name[:60] for name in cv_names] # Build dynamic CV section cv_section = "" for i, (name, snippet) in enumerate(zip(cv_names, cv_snippets), start=1): cv_section += f"\n{i}. {name}:\n{snippet}\n" # Build prompt prompt = f""" You are an AI recruitment assistant helping to evaluate candidate CVs for a job opening. Below is the job description, followed by {len(cv_names)} candidate CV summaries. Your job is to: - Analyze each candidate's relevance based on their technical skills, tools, and experience - Be honest: clearly state if a candidate is a good fit or not - Avoid generic praise unless it's supported by actual content {"- Rank the candidates based on fit if more than one is provided." if len(cv_names) > 1 else ""} ### Job Description: {job_description} ### Candidate CVs:{cv_section} """.strip() # Enforce character limit (approx 8000 tokens max for llama3-8b-8192) if len(prompt) > 8000: prompt = prompt[:8000] # Call 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", "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}"