import json import re import time from openai import OpenAI from config import NVIDIA_API_KEY, GLM_BASE_URL, GLM_MODEL class LLMClient: # Hard cap per API request — a hung call must fail fast, not stall the pipeline REQUEST_TIMEOUT = 90.0 def __init__(self): self.client = OpenAI( base_url=GLM_BASE_URL, api_key=NVIDIA_API_KEY, timeout=self.REQUEST_TIMEOUT, max_retries=0, # we do our own retries with backoff ) self.model = GLM_MODEL def _call(self, system: str, user: str, max_tokens: int = 512, retries: int = 3) -> str: for attempt in range(retries): try: completion = self.client.chat.completions.create( model=self.model, messages=[ {"role": "system", "content": system}, {"role": "user", "content": user}, ], temperature=0.2, top_p=0.9, max_tokens=max_tokens, stream=False, ) return completion.choices[0].message.content or "" except Exception as e: if attempt < retries - 1: time.sleep(2 ** attempt) else: raise def _extract_json(self, text: str) -> dict | list: try: return json.loads(text) except Exception: pass match = re.search(r"```(?:json)?\s*([\s\S]+?)```", text) if match: try: return json.loads(match.group(1)) except Exception: pass for start_char, end_char in [('{', '}'), ('[', ']')]: idx = text.find(start_char) if idx >= 0: depth = 0 for i, ch in enumerate(text[idx:], idx): if ch == start_char: depth += 1 elif ch == end_char: depth -= 1 if depth == 0: try: return json.loads(text[idx:i+1]) except Exception: break raise ValueError(f"Cannot parse JSON: {text[:200]}") # ────────────────────────────────────────────────────────── # BATCH ASSESSMENT — sends 8 jobs per API call (8× faster) # ────────────────────────────────────────────────────────── def assess_jobs_batch(self, jobs_batch: list[dict], compact_profile: str) -> list[dict]: """ Assess a batch of up to 8 jobs in one API call. Returns list of assessment dicts in the same order as jobs_batch. """ system = ( "You are a PM recruiter. Rate job-candidate fit. " "Return ONLY a JSON array — no markdown, no text." ) jobs_text = "" for i, job in enumerate(jobs_batch, 1): desc = (job.get("description") or "")[:300].replace("\n", " ") jobs_text += ( f"\nJOB {i}: {job.get('title','')} at {job.get('company','')} | {job.get('location','')}\n" f"DESC: {desc}\n" ) user = f"""CANDIDATE: {compact_profile} {jobs_text} Return a JSON array with one object per job (in order): [ {{ "job_index": 1, "score": <1-10>, "match_pct": <0-100>, "exp_match": "Good fit|Under-qualified|Over-qualified", "matching": ["skill1","skill2"], "missing": ["skill1"], "strengths": ["point1","point2"], "note": "<1 sentence>", "keywords": ["kw1","kw2","kw3"], "priority": "High|Medium|Low" }} ]""" response = self._call(system, user, max_tokens=150 * len(jobs_batch)) try: result = self._extract_json(response) if isinstance(result, list): return result # Sometimes model wraps in object if isinstance(result, dict): for v in result.values(): if isinstance(v, list): return v except Exception: pass # Fallback: return neutral scores return [self._neutral_assessment(i + 1) for i in range(len(jobs_batch))] def _neutral_assessment(self, idx: int) -> dict: return { "job_index": idx, "score": 5, "match_pct": 50, "exp_match": "Unknown", "matching": [], "missing": [], "strengths": [], "note": "Auto-assessment failed.", "keywords": [], "priority": "Medium", } def _call_with_cfg(self, cfg: dict, system: str, user: str, max_tokens: int = 2000) -> str: """Call any NVIDIA model using the provided model config dict.""" from openai import OpenAI client = OpenAI(base_url=cfg.get("base_url"), api_key=cfg["api_key"], timeout=self.REQUEST_TIMEOUT, max_retries=0) extra_body = cfg.get("extra_body") or None for attempt in range(3): try: kwargs = dict( model=cfg["model"], messages=[ {"role": "system", "content": system}, {"role": "user", "content": user}, ], temperature=0.2, top_p=0.9, max_tokens=max_tokens, stream=False, ) if extra_body: kwargs["extra_body"] = extra_body completion = client.chat.completions.create(**kwargs) return completion.choices[0].message.content or "" except Exception: if attempt < 2: time.sleep(2 ** attempt) else: raise # ────────────────────────────────────────────────────────── # v4 contract — canonical Resume model in/out (Phase 4) # ────────────────────────────────────────────────────────── def tailor_resume_v4(self, cfg: dict, resume_dict: dict, jd_text: str, job_title: str, company: str, assessment: dict, provider_family: str = None, extra_instruction: str = "") -> dict: """ Phase 4 LLM contract: input is the candidate's Resume as JSON, output is a tailored Resume as JSON (same shape). The LLM picks the best 5-7 bullets per role and rewrites them to weave JD keywords. The output is the source of truth — the renderer writes from it directly with no further processing. `provider_family` (claude|kimi|nvidia) selects a provider-specific prompt template from prompts/ when one exists; otherwise the built-in inline prompt is used. On a schema-invalid first response the model is re-asked ONCE with a correction prompt (spec #2). Returns the tailored resume dict (matching `Resume.from_dict` shape). Returns the input dict unchanged if all retries fail. """ import json as _json from .resume_model import RESUME_JSON_SCHEMA_DESCRIPTION from .provider_prompts import render_prompt system = ( "You are an ATS resume writer for PM roles. " "Return ONLY valid JSON matching the requested schema. No markdown. No commentary." ) ats_keywords = ", ".join(assessment.get("ats_keywords", assessment.get("keywords", []))) matching = ", ".join(assessment.get("matching_skills", assessment.get("matching", []))) user = None if provider_family: resume_json = _json.dumps(resume_dict, ensure_ascii=False, indent=2) user = render_prompt( "resume_tailor", provider_family, job_title=job_title, company=company, jd_text=jd_text[:2500], ats_keywords=ats_keywords or matching, resume_json=resume_json[:5500], schema=RESUME_JSON_SCHEMA_DESCRIPTION, extra=extra_instruction or "", ) if not user: user = self._tailor_v4_prompt( resume_dict=resume_dict, jd_text=jd_text, job_title=job_title, company=company, ats_keywords=ats_keywords, matching=matching, ) if extra_instruction: user += "\n\n" + extra_instruction correction = ( "\n\nYOUR PREVIOUS RESPONSE WAS NOT VALID. Return ONLY a single JSON " "object with a non-empty 'summary' (>=100 chars) and a 'roles' array " "where the roles together have at least 4 'bullets'. No markdown.") for attempt in range(2): try: u = user if attempt == 0 else user + correction response = self._call_with_cfg(cfg, system, u, max_tokens=4500) data = self._extract_json(response) if isinstance(data, list): data = next((d for d in data if isinstance(d, dict)), {}) if self._v4_valid(data, resume_dict): return data except Exception: pass # Failure → return input unchanged so the pipeline still ships SOMETHING return resume_dict def repair_resume_v4(self, cfg: dict, resume_dict: dict, jd_text: str, job_title: str, company: str, missing_terms: list, provider_family: str = None) -> dict: """LLM repair pass (spec #1 repair_resume): weave MISSING JD terms into existing bullets/summary. Returns a tailored resume dict, or the input unchanged on failure. Falls back to the tailor prompt if no repair template exists for the family.""" import json as _json from .resume_model import RESUME_JSON_SCHEMA_DESCRIPTION from .provider_prompts import render_prompt system = ("You are an ATS resume repair specialist. Return ONLY valid " "JSON matching the schema. No markdown.") resume_json = _json.dumps(resume_dict, ensure_ascii=False, indent=2) missing = ", ".join(str(t) for t in (missing_terms or [])[:40]) user = render_prompt( "repair", provider_family or "nvidia", job_title=job_title, company=company, jd_text=jd_text[:2500], missing_terms=missing, resume_json=resume_json[:5500], schema=RESUME_JSON_SCHEMA_DESCRIPTION, ) if not user: # No repair template → reuse the tailor path with the missing terms # framed as mandatory keywords. return self.tailor_resume_v4( cfg, resume_dict, jd_text, job_title, company, {"ats_keywords": list(missing_terms or [])}, provider_family=provider_family) for attempt in range(2): try: response = self._call_with_cfg(cfg, system, user, max_tokens=4500) data = self._extract_json(response) if isinstance(data, list): data = next((d for d in data if isinstance(d, dict)), {}) if self._v4_valid(data, resume_dict): return data except Exception: pass return resume_dict def jobalytics_repair_v4(self, cfg: dict, resume_dict: dict, jd_text: str, job_title: str, company: str, missing_keywords: list, placement_guidance: str, provider_family: str = None) -> dict: """LLM regeneration in external_checker_mode='jobalytics_repair' (spec #8): place pasted missing keywords across summary/skills/experience per their classification. Returns a tailored resume dict, or input unchanged.""" import json as _json from .resume_model import RESUME_JSON_SCHEMA_DESCRIPTION from .provider_prompts import render_prompt system = ("You are an ATS resume editor in jobalytics_repair mode. " "Return ONLY valid JSON matching the schema. No markdown.") resume_json = _json.dumps(resume_dict, ensure_ascii=False, indent=2) kws = ", ".join(str(k) for k in (missing_keywords or [])[:60]) user = render_prompt( "jobalytics_repair", provider_family or "nvidia", job_title=job_title, company=company, jd_text=jd_text[:2200], missing_keywords=kws, placement_guidance=placement_guidance or "", resume_json=resume_json[:5500], schema=RESUME_JSON_SCHEMA_DESCRIPTION, ) if not user: return self.repair_resume_v4(cfg, resume_dict, jd_text, job_title, company, missing_keywords, provider_family=provider_family) for attempt in range(2): try: response = self._call_with_cfg(cfg, system, user, max_tokens=4500) data = self._extract_json(response) if isinstance(data, list): data = next((d for d in data if isinstance(d, dict)), {}) if self._v4_valid(data, resume_dict): return data except Exception: pass return resume_dict def analyze_jd_requirements(self, cfg: dict, jd_text: str, provider_family: str = None) -> dict: """Structured JD extraction (spec #2) — mirrors how AI ATS checkers (Jobalytics) read a JD. Returns categorized requirements with importance, source phrase, aliases, and recommended placement. Returns {} on failure (the caller has a deterministic fallback).""" system = ( "You are an ATS job-description analyst. Extract structured " "requirements as STRICT JSON. No markdown, no commentary." ) if provider_family: from .provider_prompts import render_prompt _u = render_prompt("jd_analysis", provider_family, jd_text=jd_text[:3500]) if _u: for _ in range(2): try: response = self._call_with_cfg(cfg, system, _u, max_tokens=2000) data = self._extract_json(response) if isinstance(data, list): data = next((d for d in data if isinstance(d, dict)), {}) if isinstance(data, dict) and any( data.get(k) for k in ( "required_hard_skills", "tools_platforms", "responsibilities", "preferred_hard_skills")): return data except Exception: pass return {} user = f"""Analyze this job description and return ONLY this JSON shape: {{ "target_role_titles": ["..."], "required_hard_skills": [{{"term":"SQL","importance":"must_have","source_phrase":"strong SQL experience required","aliases":["PostgreSQL","MySQL"],"recommended_placement":["skills","experience"]}}], "preferred_hard_skills": [], "tools_platforms": [], "responsibilities": [], "domain_terms": [], "certifications": [], "education_requirements": [], "soft_skills": [], "seniority_signals": [] }} RULES: - Use the JD's EXACT wording for each `term` (e.g. "product strategy", not "strategy"). - importance ∈ {{must_have, preferred, nice_to_have}} based on JD cues (required/must/strong → must_have; preferred/plus/bonus → preferred). - source_phrase = the short JD snippet the term came from. - aliases = common synonyms/variants (SQL↔PostgreSQL; CI/CD↔GitHub Actions). - recommended_placement ⊆ {{title, summary, skills, experience, certifications, education}}. - EXCLUDE company names, locations, and vague buzzwords (innovation, solutions, ownership, synergy, world-class). - responsibilities = the role's key duties as short skill-like phrases. JOB DESCRIPTION: {jd_text[:3500]}""" for _ in range(2): try: response = self._call_with_cfg(cfg, system, user, max_tokens=2000) data = self._extract_json(response) if isinstance(data, list): data = next((d for d in data if isinstance(d, dict)), {}) if isinstance(data, dict) and any( data.get(k) for k in ( "required_hard_skills", "tools_platforms", "responsibilities", "preferred_hard_skills")): return data except Exception: pass return {} def judge_evidence(self, cfg: dict, resume_text: str, keywords: list) -> dict: """Semantic evidence layer (spec #3/#7, Layer 3). For each keyword the deterministic matcher couldn't support, ask whether the RESUME shows genuine adjacent evidence. Returns {keyword: {status, evidence}} where status ∈ {transferable, unsupported}. NEVER returns 'supported' — it can only upgrade unsupported→transferable when real related evidence exists, so we never fake a hard skill.""" if not keywords: return {} system = ( "You judge whether a resume has GENUINE adjacent evidence for skills. " "Be strict. Only mark 'transferable' if real related experience exists. " "Return ONLY JSON." ) user = f"""RESUME: {resume_text[:3500]} For EACH keyword below, decide if the resume shows genuine RELATED experience (transferable) or nothing relevant (unsupported). Do NOT invent. Return JSON: {{"keyword": {{"status": "transferable|unsupported", "evidence": "short quote from resume or empty"}}}} KEYWORDS: {", ".join(keywords[:40])}""" try: response = self._call_with_cfg(cfg, system, user, max_tokens=1500) data = self._extract_json(response) return data if isinstance(data, dict) else {} except Exception: return {} @staticmethod def _v4_valid(data: dict, original: dict) -> bool: """v4 output must have a summary and at least one role with bullets.""" if not isinstance(data, dict): return False if len(data.get("summary", "") or "") < 100: return False roles = data.get("roles") or [] if not roles or not isinstance(roles, list): return False total_bullets = sum( len(r.get("bullets") or []) for r in roles if isinstance(r, dict) ) return total_bullets >= 4 def _tailor_v4_prompt(self, resume_dict: dict, jd_text: str, job_title: str, company: str, ats_keywords: str, matching: str) -> str: import json as _json from .resume_model import RESUME_JSON_SCHEMA_DESCRIPTION kw_list = ats_keywords or "product manager, agile, roadmap, stakeholder, KPI, user research" resume_json = _json.dumps(resume_dict, ensure_ascii=False, indent=2) return f"""You are an expert ATS resume writer. Rewrite the candidate's resume (provided as JSON) to target the job below. Return the SAME JSON structure with tailored content. TARGET ROLE: {job_title} at {company} JOB DESCRIPTION: {jd_text[:2500]} MANDATORY JD KEYWORDS (weave into summary or bullets — NOT as a separate list): {kw_list} CANDIDATE'S RESUME (canonical JSON): ```json {resume_json[:5500]} ``` POLICY: The candidate has 5+ years of broad PM/EdTech/AI/SaaS experience that the original resume doesn't fully capture. You are AUTHORIZED to claim familiarity with any JD-named common PM tool (Jira/Figma/Mixpanel/Amplitude/Metabase/GA4/Confluence) or methodology (PRDs/user stories/sprint planning/A/B testing/MLOps) — frame domain capabilities (SIEM/SOAR/MLOps/foundation models) as adjacent/exposed-to via cross-functional work. Avoid only the most extreme stretches (e.g. inventing specific employer claims like "Led the SOC at NxtWave"). RULES: 1. Output the EXACT same JSON shape (name, contact, summary, roles, achievements, education). Do not add fields. 2. SUMMARY MUST OPEN with this exact pattern: "Strong-fit candidate for {job_title} at {company}: [N] years of [most-relevant experience]." Then 4-5 sentences weaving 8+ JD keywords as descriptors of work. 3. For EACH role: - Keep title/company/location/dates unchanged - Select 5-7 of the strongest bullets from the candidate's pool and REWRITE each to: (a) start with an action verb, (b) preserve quantified metrics (%, $, numbers), (c) weave JD keywords naturally - You may ADD 1-2 new bullets per role that demonstrate JD-named skills the candidate plausibly has from their domain - For older roles (3+ years ago), keep 3-4 strong bullets 4. ACHIEVEMENTS: 3-5 quantified cross-role highlights. Reuse or rephrase the candidate's strongest metrics. 5. EDUCATION: keep entries unchanged. 6. Weave the MANDATORY keywords into summary + bullets where they fit naturally. 7. ALSO extract a comprehensive `jd_skills` list — EXACTLY like an ATS keyword scanner (Jobalytics/Resume Worded) would: every HARD SKILL, TOOL, METHOD, PLATFORM, and key ROLE/DOMAIN TERM named or clearly implied in the JD that this candidate can credibly claim. Aim for 25-40 items. Use the JD's EXACT wording (e.g. if the JD says "product strategy", output "product strategy", not "strategy"). Include both acronym and full form when the JD does (e.g. "A/B testing", "SQL"). EXCLUDE vague buzzwords (innovation, solutions, ownership, synergy), company names, and locations. These populate a real SKILLS section — the #1 ATS keyword vehicle. EXPECTED OUTPUT SCHEMA (return ONLY valid JSON matching this, PLUS a top-level "jd_skills" array of strings): {RESUME_JSON_SCHEMA_DESCRIPTION} CRITICAL: - Return the WHOLE resume JSON, not just the changed fields - 5-7 bullets per recent role; 3-4 for older roles - Action-verb-start every bullet - Include the recruiter pitch as the first sentence of summary - Include the "jd_skills" array (25-40 real skills/keywords from the JD)""" def customize_resume_fast(self, cfg: dict, resume_text: str, job_description: str, job_title: str, company: str, assessment: dict, indexed_bullets: list = None) -> dict: """Customize resume using a fast model (Kimi/Step/Qwen) instead of GLM. indexed_bullets: optional list of (role_idx, bullet_idx, role_name, bullet_text) tuples. When provided, the LLM is asked to return rewritten_bullets keyed by "role_idx:bullet_idx" (v2 contract). When omitted, the LLM gets the raw resume text and can return v1 or v2 shape. """ system = ( "You are an ATS resume writer for PM roles. " "Return ONLY valid JSON, no markdown." ) matching_skills = ", ".join(assessment.get("matching_skills", assessment.get("matching", []))) ats_keywords = ", ".join(assessment.get("ats_keywords", assessment.get("keywords", []))) user = self._resume_customize_prompt( resume_text, job_description, job_title, company, ats_keywords, matching_skills, indexed_bullets=indexed_bullets, ) for attempt in range(2): try: response = self._call_with_cfg(cfg, system, user, max_tokens=4000) data = self._extract_json(response) # Some models wrap the object in an array if isinstance(data, list): data = next((d for d in data if isinstance(d, dict)), {}) if self._customization_valid(data): return data except Exception: pass return self._empty_customization() @staticmethod def _customization_valid(data) -> bool: """ A usable v2 customization must have a real summary AND produce some tailored bullet content — either rewrites of existing bullets or explicit new bullets. Backward-compat: old v1 responses with `experience_bullets` or `core_competencies` populated also validate. """ if not isinstance(data, dict): return False if len(data.get("professional_summary", "") or "") < 50: return False # v2: rewritten_bullets or new_bullets must be non-empty rb = data.get("rewritten_bullets") or {} nb = data.get("new_bullets") or {} if isinstance(rb, dict) and rb: return True if isinstance(nb, dict) and any(v for v in nb.values()): return True # v1 backward-compat if data.get("experience_bullets") or data.get("core_competencies"): return True return False # ────────────────────────────────────────────────────────── # RESUME CUSTOMIZATION # ────────────────────────────────────────────────────────── def customize_resume(self, resume_text: str, job_description: str, job_title: str, company: str, assessment: dict, indexed_bullets: list = None) -> dict: system = ( "You are an ATS resume writer for PM roles. " "Return ONLY valid JSON, no markdown." ) matching_skills = ", ".join(assessment.get("matching_skills", assessment.get("matching", []))) ats_keywords = ", ".join(assessment.get("ats_keywords", assessment.get("keywords", []))) user = self._resume_customize_prompt( resume_text, job_description, job_title, company, ats_keywords, matching_skills, indexed_bullets=indexed_bullets, ) try: response = self._call(system, user, max_tokens=4000) data = self._extract_json(response) if isinstance(data, list): data = next((d for d in data if isinstance(d, dict)), {}) if self._customization_valid(data): return data except Exception: pass return self._empty_customization() def _resume_customize_prompt(self, resume_text, job_description, job_title, company, ats_keywords, matching_skills, indexed_bullets: list = None) -> str: """ v2 contract: bullet-rewriter, no separate skills section. The LLM rewrites SPECIFIC original bullets (referenced by role:idx) to include JD keywords naturally. New bullets are added only when critical keywords can't fit any existing bullet. NO core_competencies field — the resume has no skills section. All keywords must live in the summary or experience bullets. """ kw_list = ats_keywords if ats_keywords else "product manager, agile, roadmap, stakeholder, KPI, user research" # Format the candidate's bullets with role:idx references the LLM can use bullets_block = "" if indexed_bullets: current_role = None for (role_idx, bullet_idx, role_name, bullet_text) in indexed_bullets: if role_idx != current_role: bullets_block += f"\n[Role {role_idx}] {role_name}\n" current_role = role_idx # Truncate very long bullets to keep prompt tight bt = bullet_text[:220] + ("..." if len(bullet_text) > 220 else "") bullets_block += f" [{role_idx}:{bullet_idx}] {bt}\n" return f"""You are an expert ATS resume writer. The candidate has 5+ years of broad PM/EdTech/AI/SaaS experience that the resume cannot fully capture in one fixed document. Your job is to rewrite specific bullets to surface the parts of the candidate's broader experience that match THIS job — including reasonable claims of familiarity with the JD's tools/methodologies that an experienced PM would plausibly have touched, even if not explicitly named in the resume. TARGET ROLE: {job_title} at {company} JOB DESCRIPTION: {job_description[:2500]} CANDIDATE'S INDEXED BULLETS (rewrite by reference, e.g. "0:3"): {bullets_block or resume_text[:2500]} MANDATORY JD KEYWORDS (must appear in summary or bullets — NOT in a separate skills list): {kw_list} POLICY: The candidate has explicitly authorized broad keyword inclusion. They have 5+ years of PM experience across multiple products and acknowledge that not every tool/methodology they've used is in the resume. When a JD names a common PM tool (Jira, Figma, Mixpanel, Amplitude, Metabase, GA4, Confluence, Notion) or a common methodology (PRDs, user stories, acceptance criteria, sprint planning, A/B testing, OKRs, agile, scrum) — INCLUDE IT. When a JD names a domain capability (MLOps, foundation models, SIEM, threat detection, data science, RCA, observability, ETL) — include it as familiar/exposed-to in a relevant bullet, framed as cross-functional collaboration or adjacent product work, not as primary expertise. Avoid only the most extreme stretches (e.g. inventing employer-specific claims like "Led the SOC at "). RULES: 1. NO "Core Competencies" or "Skills" section. ALL keywords appear in the Professional Summary or inside experience bullets. 2. Open the Professional Summary with a 1-sentence RECRUITER PITCH that explicitly addresses the JD: "Strong-fit candidate for [JD title] at [company]: [N] years of [most relevant experience type] directly applicable to [3 concrete JD requirements]." This is visible, professional, and pre-frames the read for both human recruiters and AI screeners. 3. Then 4-5 sentences of summary woven with 10+ JD keywords as descriptors of past work. 4. For each bullet you rewrite, KEEP the candidate's actual achievement and quantified metric (%, $, user counts). Only change phrasing to mirror JD language. 5. Use JD's exact verbs/nouns when semantically appropriate ("Owned product modules end-to-end", "Authored PRDs", "Tracked activation, adoption, retention", "Partnered with design, engineering, QA", "Drove A/B experiments", "Established KPIs"). 6. PREFER rewriting existing bullets over adding new ones, but aggressively add 2-4 new_bullets per role when the JD has many keywords that don't fit existing achievements — frame them as adjacent work the candidate did. For example, EdgeVerve AI JD: add a new_bullet about "Productized ML models into APIs via MLOps workflows, partnering with data science on model validation pipelines and KPIs." (The candidate has touched these as part of their AI chatbot / LLM work.) 7. Every bullet starts with a strong action verb (Led, Built, Drove, Scaled, Launched, Owned, Authored, Partnered, Tracked, Translated, Produced, Delivered, Reduced, Increased, Established, Evangelized, Validated, Productized). 8. AIM FOR 100% JD KEYWORD COVERAGE. If the JD has 20 keywords, your output should cover all 20 across summary + rewrites + new bullets. This is the explicit goal. Return ONLY valid JSON (no markdown): {{ "professional_summary": "", "rewritten_bullets": {{ "0:0": "Rewritten text...", "0:3": "Rewritten text...", "1:1": "Rewritten text..." }}, "new_bullets": {{ "0": ["new bullet 1 covering JD keyword X", "new bullet 2 covering JD keywords Y, Z"] }}, "key_achievements": ["Quantified achievement 1", "Quantified achievement 2"], "tailoring_notes": "" }} CRITICAL: - Return rewritten_bullets for at least 6 bullets - Add 2-4 new_bullets per role to cover JD keywords that don't fit existing bullets - Open summary with the recruiter pitch sentence - Do NOT return a "core_competencies" field - Target: all JD keywords appear somewhere in summary, rewritten_bullets, or new_bullets""" def _empty_customization(self) -> dict: return { "professional_summary": "", "rewritten_bullets": {}, "new_bullets": {}, "key_achievements": [], "tailoring_notes": "Auto-customization failed.", } # ────────────────────────────────────────────────────────── # PROFILE EXTRACTION # ────────────────────────────────────────────────────────── def extract_profile_summary(self, resume_text: str) -> str: system = "You are a resume parser. Return ONLY valid JSON." user = f"""Parse this resume for job matching. RESUME: {resume_text[:3000]} Return ONLY: {{ "name": "", "current_role": "", "total_experience_years": , "summary": "<2 sentence summary>", "core_skills": ["s1","s2","s3","s4","s5","s6","s7","s8"], "domain_expertise": ["d1","d2"], "industries": ["i1","i2"], "education": "", "certifications": ["c1"], "notable_achievements": ["a1","a2","a3"] }}""" response = self._call(system, user, max_tokens=800) try: data = self._extract_json(response) return json.dumps(data, indent=2) except Exception: return resume_text[:1500] def extract_profile_summary_fast(self, cfg: dict, resume_text: str) -> str: """Like extract_profile_summary but uses a fast model (Kimi/Step) instead of GLM.""" system = "You are a resume parser. Return ONLY valid JSON." user = f"""Parse this resume for job matching. RESUME: {resume_text[:3000]} Return ONLY: {{ "name": "", "current_role": "", "total_experience_years": , "summary": "<2 sentence summary>", "core_skills": ["s1","s2","s3","s4","s5","s6","s7","s8"], "domain_expertise": ["d1","d2"], "industries": ["i1","i2"], "education": "", "certifications": ["c1"], "notable_achievements": ["a1","a2","a3"] }}""" try: response = self._call_with_cfg(cfg, system, user, max_tokens=800) data = self._extract_json(response) return json.dumps(data, indent=2) except Exception: return self.extract_profile_summary(resume_text) # fallback to GLM def build_compact_profile(self, profile_json: str) -> str: """Build a short ~200-char profile string for batch assessments.""" try: d = json.loads(profile_json) skills = ", ".join(d.get("core_skills", [])[:8]) return ( f"{d.get('name','')} | {d.get('current_role','')} | " f"{d.get('total_experience_years','')} yrs exp | " f"Skills: {skills} | " f"Domain: {', '.join(d.get('domain_expertise',[])[:3])}" ) except Exception: return profile_json[:300]