Spaces:
Sleeping
Sleeping
File size: 49,042 Bytes
7ff6662 428497b b15fd58 c687f2b 7ff6662 b15fd58 7ff6662 c687f2b 7ff6662 428497b 7ff6662 428497b 7ff6662 428497b 7ff6662 428497b 7ff6662 c687f2b 7ff6662 428497b 7ff6662 428497b 7ff6662 428497b 7ff6662 428497b 7ff6662 428497b 7ff6662 9a25145 c425de3 43c3b77 c425de3 428497b 43c3b77 428497b c425de3 428497b c425de3 428497b 84bd0ae 428497b 84bd0ae 428497b 84bd0ae c425de3 5c4d688 43c3b77 c425de3 9a25145 7ff6662 b15fd58 7ff6662 0e70529 9bf4a3d 0e70529 9bf4a3d 0e70529 9bf4a3d 0e70529 9bf4a3d 0e70529 9bf4a3d 0e70529 9bf4a3d 0e70529 9bf4a3d 2bcd6c2 9bf4a3d 2bcd6c2 0e70529 ccb37a3 0e70529 ccb37a3 0e70529 7ff6662 33ec77b 7ff6662 33ec77b 7ff6662 b15fd58 33ec77b 7ff6662 33ec77b 7ff6662 33ec77b 7ff6662 b15fd58 7ff6662 b15fd58 7ff6662 33ec77b 7ff6662 33ec77b 4485b7c 7ff6662 ab9af33 7ff6662 33ec77b 7ff6662 33ec77b 7ff6662 4485b7c 33ec77b 4485b7c 7ff6662 4485b7c 33ec77b 4485b7c 33ec77b 4485b7c 7ff6662 4485b7c 33ec77b 4485b7c 7ff6662 33ec77b 7ff6662 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 | 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. Reasoning models (nemotron) are slow, so this is
# generous; a truly hung call still fails within it.
REQUEST_TIMEOUT = 150.0
# Global min interval between API calls (module-level) to respect the NIM
# endpoint's per-worker request cap (503 ResourceExhausted otherwise).
_MIN_INTERVAL = 2.0
_last_call_ts = 0.0
def __init__(self, model: str = None):
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 = model or GLM_MODEL
def _call(self, system: str, user: str, max_tokens: int = 512, retries: int = 3,
response_format: dict = None, temperature: float = 0.2) -> str:
for attempt in range(retries):
try:
# Throttle: keep a minimum gap between calls (respect NIM rate cap).
gap = LLMClient._MIN_INTERVAL - (time.monotonic() - LLMClient._last_call_ts)
if gap > 0:
time.sleep(gap)
LLMClient._last_call_ts = time.monotonic()
kwargs = dict(
model=self.model,
messages=[
{"role": "system", "content": system},
{"role": "user", "content": user},
],
temperature=temperature,
top_p=0.9,
max_tokens=max_tokens,
stream=False,
)
if response_format is not None:
kwargs["response_format"] = response_format
completion = self.client.chat.completions.create(**kwargs)
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:
# Strip reasoning-model wrappers (<think>β¦</think>, <reasoning>β¦</reasoning>)
# that some NIM models (e.g. nemotron) emit before the JSON payload.
if text:
text = re.sub(r"<think>[\s\S]*?</think>", "", text, flags=re.I)
text = re.sub(r"<reasoning>[\s\S]*?</reasoning>", "", text, flags=re.I)
text = re.sub(r"^[\s\S]*?</think>", "", text, flags=re.I) # unclosedβopen
text = text.strip()
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
# Salvage a balanced JSON block even when preceded/followed by reasoning
# prose. Try EVERY opening bracket (reasoning models often print a stray
# '[' mid-thought before the real array) and keep the first that parses to
# a non-empty list of objects (or a dict).
best = None
for start_char, end_char in [('[', ']'), ('{', '}')]:
for idx in (m.start() for m in re.finditer(re.escape(start_char), text)):
depth = 0
for i in range(idx, len(text)):
if text[i] == start_char:
depth += 1
elif text[i] == end_char:
depth -= 1
if depth == 0:
try:
val = json.loads(text[idx:i + 1])
except Exception:
break
if isinstance(val, list) and any(isinstance(x, dict) for x in val):
return val
if isinstance(val, dict) and best is None:
best = val
break
if best is not None:
return best
raise ValueError(f"Cannot parse JSON: {text[:200]}")
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# STRUCTURED KEYWORD EXTRACTION β injection-resistant, schema'd
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def extract_keywords_structured(self, clean_jd: str, correction_hint: str = "") -> list[dict]:
"""Extract structured, traceable hiring criteria from an ALREADY-CLEANED JD.
The JD text is treated strictly as untrusted DATA delimited by fences. The
model is told to ignore any instructions inside the fences. Output is a JSON
array of structured items; the caller MUST still run it through
`keyword_schema.validate_and_repair` (traceability + schema are enforced
deterministically there, not trusted from the model). Returns [] on failure.
"""
# Compact prompt + forced-JSON + temperature 0 = deterministic output that
# suppresses reasoning-model prose. The validator enforces traceability/enums.
# NOTE: injection resistance is enforced DETERMINISTICALLY upstream β
# jd_preprocess strips injection lines and keyword_schema rejects any
# phrase not traceable to the cleaned JD. We deliberately keep this prompt
# free of "ignore instructions" wording because it makes reasoning models
# (nemotron) deliberate at length and never emit JSON.
hint = (f" Also include if present verbatim: {correction_hint}."
if correction_hint else "")
# Compact prompt only β detail/examples make nemotron deliberate at length
# and never emit JSON. Short 1-4 word phrases keep them rΓ©sumΓ©-mappable.
system = (
"Extract 12-18 ATS hiring criteria from the posting. Return a JSON object "
'{"criteria":[{"exact_phrase":..,"category":..,"requirement_type":..,'
'"importance":..,"semantic_variants":[]}]} where exact_phrase is a SHORT '
"1-4 word skill/tool/domain term copied verbatim (a contiguous substring). "
"category in [role_identity,core_skill,hard_skill,tool,domain,"
"responsibility,soft_skill,experience_signal,qualification,outcome]. "
"requirement_type 'required' for minimum/must-have else 'preferred'. "
"semantic_variants: 2-3 synonyms. Exclude company names, people, "
"locations, benefits, marketing." + hint
)
user = "Posting:\n" + (clean_jd or "")[:3800]
def _parse(raw):
data = self._extract_json(raw)
if isinstance(data, dict):
for v in data.values():
if isinstance(v, list):
return [d for d in v if isinstance(d, dict)]
return []
if isinstance(data, list):
return [d for d in data if isinstance(d, dict)]
return []
# Reasoning models are variable even at temp 0 β retry several times so a
# JSON-emitting run wins. max_tokens kept modest so a reasoning-only run
# fails fast rather than filling a huge budget.
for _ in range(4):
try:
raw = self._call(system, user, max_tokens=2500, retries=1,
response_format={"type": "json_object"},
temperature=0.0)
items = _parse(raw)
if items:
return items
except Exception as e:
print(f"[extract_keywords_structured] retry: {str(e)[:80]}")
return []
def rewrite_bullet(self, original_bullet: str, target_phrase: str,
concept: str = "", category: str = "") -> str:
"""Rewrite ONE rΓ©sumΓ© bullet to naturally use the employer's exact phrase,
grounded STRICTLY on the original bullet. Returns the rewritten bullet
text (plain, no LaTeX). The caller ALWAYS re-verifies the output with
`resume_rewrite.verify_rewrite` β so this method is not trusted to be
truthful on its own; the deterministic guard is the real safety boundary.
"""
system = (
"You rewrite a single rΓ©sumΓ© bullet so it uses an employer's exact "
"terminology, while staying strictly truthful.\n"
"HARD RULES:\n"
"- Use ONLY facts already present in the ORIGINAL bullet. Do not add "
"any new tool, technology, metric, number, employer, team, scope, or "
"outcome that is not already in the original.\n"
"- You MAY reword and incorporate the TARGET PHRASE only if the "
"original bullet genuinely supports that concept. If it does not, "
"return the original bullet unchanged.\n"
"- Keep every metric/number from the original exactly as-is.\n"
"- Write one natural sentence: Action + context/scope + method/skill + "
"supported result. Never output a comma-separated keyword list.\n"
"- Return ONLY the rewritten bullet text. No quotes, no explanation."
)
user = (
f"ORIGINAL BULLET:\n{original_bullet}\n\n"
f"TARGET EXACT PHRASE (use only if truthful): {target_phrase}\n"
f"CONCEPT: {concept}\n\n"
"Rewritten bullet:"
)
try:
out = self._call(system, user, max_tokens=300)
return (out or "").strip().strip('"').strip()
except Exception as e:
print(f"[rewrite_bullet] failed: {e}")
return original_bullet
def rewrite_summary(self, original_summary: str, target_title: str,
top_phrases: list, resume_corpus: str) -> str:
"""Rewrite the rΓ©sumΓ© SUMMARY to target the role, using ONLY facts already
in the rΓ©sumΓ©. Output re-verified against the corpus by the caller."""
system = (
"You rewrite a rΓ©sumΓ© professional-summary paragraph to target a "
"specific role, staying strictly truthful.\n"
"HARD RULES:\n"
"- Use ONLY facts, skills, metrics, and experience already present in "
"the RΓSUMΓ CORPUS. Do not add any new employer, tool, metric, number, "
"industry, title, or claim not already in the corpus.\n"
"- Naturally incorporate the TARGET PHRASES only where the corpus "
"genuinely supports them; skip any that would be untrue.\n"
"- Keep it 2-4 sentences, natural and recruiter-readable. Not a keyword list.\n"
"- Return ONLY the rewritten summary text."
)
user = (
f"TARGET TITLE: {target_title}\n"
f"TARGET PHRASES (use only if truthful): {', '.join(top_phrases)}\n\n"
f"RΓSUMΓ CORPUS (the only allowed source of facts):\n{resume_corpus[:3500]}\n\n"
f"ORIGINAL SUMMARY:\n{original_summary}\n\n"
"Rewritten summary:"
)
try:
return (self._call(system, user, max_tokens=500) or "").strip().strip('"')
except Exception as e:
print(f"[rewrite_summary] failed: {e}")
return original_summary
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# KEYWORD EXTRACTION β Calibrated Keyword Match Framework (legacy flat list)
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def extract_keywords_llm(self, jd_text: str) -> list[str]:
"""Extract ATS keywords from a JD using the Calibrated Keyword Match Framework.
Returns a list of 15-25 keyword strings ordered from highest to lowest ATS
priority. Returns [] on any failure so the caller can fall back gracefully.
"""
system = (
"You are an ATS-aligned Job Description Keyword Intelligence Engine.\n\n"
"Identify the SMALLEST, HIGHEST-VALUE set of keywords that represent the "
"employer's actual hiring criteria for ATS platforms like Greenhouse.\n\n"
"INCLUDE ONLY:\n"
"- Specific skills, methodologies, and competencies (e.g. 'product strategy', "
"'A/B testing', 'roadmap prioritization')\n"
"- Named tools and technologies (e.g. 'SQL', 'Jira', 'Mixpanel', 'Salesforce')\n"
"- Domain/industry terms (e.g. 'e-commerce', 'B2C', 'SaaS', 'quick-commerce')\n"
"- Specific role competencies stated in the JD (e.g. 'stakeholder management', "
"'cross-functional leadership')\n"
"- Experience signals stated as requirements (e.g. '5+ years product management')\n\n"
"EXCLUDE STRICTLY β these must NEVER appear in the output:\n"
"- Company names, brand names, product names of the hiring company\n"
"- People names (recruiters, employees, executives listed anywhere on the page)\n"
"- City/country/location names\n"
"- Job board tags, hashtags, recruitment platform UI text (e.g. 'easy apply', "
"'search faster', 'trial ends', 'followers', career fair text)\n"
"- Generic personality adjectives ('passionate', 'dynamic', 'results-driven')\n"
"- Benefits, compensation, equal-opportunity, or marketing language\n"
"- Random words with no skill meaning\n"
"- Duplicate variations of the same concept\n\n"
"PRIORITY ORDER:\n"
"1. Terms marked required/must-have\n"
"2. Terms in the job title or opening summary\n"
"3. Specific skills/tools repeated across the JD\n"
"4. Domain expertise terms\n"
"5. Preferred/nice-to-have terms\n\n"
"Return ONLY a JSON array of 15-25 keyword strings, ordered highest to lowest "
"ATS priority. No markdown fences, no explanation, just the array.\n"
'Example: ["product strategy","stakeholder management","A/B testing","SQL",'
'"roadmap prioritization","OKRs","go-to-market","data analytics"]'
)
user = f"Job Description:\n{(jd_text or '')[:4000]}"
try:
raw = self._call(system, user, max_tokens=512)
data = self._extract_json(raw)
if isinstance(data, list):
return [str(k).strip() for k in data
if k and len(str(k).strip()) >= 3][:30]
if isinstance(data, dict):
out: list[str] = []
for v in data.values():
if isinstance(v, list):
out.extend(str(k).strip() for k in v if k and len(str(k).strip()) >= 3)
return out[:30]
except Exception as e:
print(f"[extract_keywords_llm] failed: {e}")
return []
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 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 <unrelated company>").
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": "<Open with 1-sentence recruiter pitch. Then 4-5 sentences with 10+ JD keywords woven naturally.>",
"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": "<which keywords landed where>"
}}
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": "<name>",
"current_role": "<role>",
"total_experience_years": <N>,
"summary": "<2 sentence summary>",
"core_skills": ["s1","s2","s3","s4","s5","s6","s7","s8"],
"domain_expertise": ["d1","d2"],
"industries": ["i1","i2"],
"education": "<degree + institution>",
"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": "<name>",
"current_role": "<role>",
"total_experience_years": <N>,
"summary": "<2 sentence summary>",
"core_skills": ["s1","s2","s3","s4","s5","s6","s7","s8"],
"domain_expertise": ["d1","d2"],
"industries": ["i1","i2"],
"education": "<degree + institution>",
"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]
|