# groq_api.py import os import requests 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: # Clean + limit all text job_description = job_description.strip()[:600] or "[No description]" cv_names = [name[:60] for name in cv_names[:3]] cv_snippets = [(text.strip()[:400] or "[No content]") for text in cv_snippets[:3]] # Assemble cleaned 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} --- Top 3 CV snippets: {cv_info} Summarize why these candidates may be a good fit. """.strip() # Show debugging info print("📦 Prompt length:", len(prompt)) if len(prompt) > 8000: return "❌ Prompt too long for Groq (must be under 8K characters)." # Send 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": "mixtral-8x7b-32768", "messages": [{"role": "user", "content": prompt}], "temperature": 0.5 }, timeout=30 ) response.raise_for_status() return response.json()["choices"][0]["message"]["content"] except Exception as e: return f"❌ Groq API error: {e}"