"""Flatten a candidate JSON profile into a single rich semantic document. The encoder embeds this document, so we deliberately weight the parts that reveal genuine fit: the headline, the professional summary and the actual role *descriptions* (what they did), not just a flat list of skill tags. This is what lets the model see substance over keyword stuffing. """ from __future__ import annotations from typing import Any, Dict, List def _safe(d: Dict[str, Any], *keys: str, default: Any = "") -> Any: cur: Any = d for k in keys: if not isinstance(cur, dict): return default cur = cur.get(k, default) return cur if cur is not None else default def candidate_skill_names(candidate: Dict[str, Any]) -> List[str]: """Return lowercase skill name list.""" return [str(s.get("name", "")).lower() for s in candidate.get("skills", []) if s.get("name")] def candidate_total_experience_months(candidate: Dict[str, Any]) -> int: """Sum of all career_history duration_months.""" return sum( int(j.get("duration_months", 0) or 0) for j in candidate.get("career_history", []) ) def build_document(candidate: Dict[str, Any]) -> str: """Build the rich text document that represents a candidate for embedding. Strategy: * Headline & current title are repeated to boost their weight. * Professional summary is the richest signal — included verbatim. * Each job's *description* (what they actually built/did) is included; this surfaces genuine expertise that skill tags alone miss. * Skills are appended with proficiency so the model understands depth. * Certifications, education, and any Redrob assessment skill names are appended so the encoder can anchor them to technical vocabulary. """ parts: List[str] = [] headline = _safe(candidate, "profile", "headline") summary = _safe(candidate, "profile", "summary") title = _safe(candidate, "profile", "current_title") industry = _safe(candidate, "profile", "current_industry") # Headline + title: repeated — they carry the strongest role signal. if headline: parts.append(f"{headline}. {headline}.") if title: parts.append(f"Current role: {title}. {title}.") if summary: parts.append(summary) if industry: parts.append(f"Industry: {industry}.") # Career history — full descriptions carry the real substance. for job in candidate.get("career_history", []): jt = job.get("title", "") comp = job.get("company", "") desc = job.get("description", "") ind = job.get("industry", "") dur = job.get("duration_months", 0) or 0 if jt: parts.append(f"Worked as {jt}{' at ' + comp if comp else ''}.") if ind: parts.append(f"Industry: {ind}.") if dur: parts.append(f"Duration: {dur} months.") if desc: parts.append(desc) # Skills with proficiency context. skills = candidate.get("skills", []) or [] if skills: skill_str = ", ".join( f"{s.get('name', '')} ({s.get('proficiency', '')}," f" {s.get('endorsements', 0)} endorsements)" for s in skills ) parts.append(f"Skills: {skill_str}.") # Certifications are a strong signal of intent and competence. for cert in candidate.get("certifications", []) or []: name = cert.get("name", "") issuer = cert.get("issuer", "") year = cert.get("year", "") if name: parts.append(f"Certified: {name}{' by ' + issuer if issuer else ''}" f"{' (' + str(year) + ')' if year else ''}.") # Education. for edu in candidate.get("education", []) or []: deg = edu.get("degree", "") field = edu.get("field_of_study", "") inst = edu.get("institution", "") if deg or field: parts.append( f"Education: {deg} in {field}" f"{' from ' + inst if inst else ''}." ) # Redrob assessment scores — validated proof of skill. sig = candidate.get("redrob_signals", {}) or {} scores = sig.get("skill_assessment_scores", {}) or {} if scores: assessed = ", ".join(f"{k}: {v:.0f}/100" for k, v in scores.items()) parts.append(f"Assessed skills: {assessed}.") # GitHub activity as a proxy for real-world engineering activity. gh = sig.get("github_activity_score", -1) if isinstance(gh, (int, float)) and gh >= 0: parts.append(f"GitHub activity score: {gh:.0f}/100.") return "\n".join(p for p in parts if p).strip()