diff --git a/app/jobs/judge.py b/app/jobs/judge.py index 4a32f46..6b8e253 100644 --- a/app/jobs/judge.py +++ b/app/jobs/judge.py @@ -3,8 +3,10 @@ A `ModelTask` beside `summarise`, after the `/rank` of github.com/MadsLorentzen/ai-job-search: the model reads a posting against a written profile and scores skills, experience and career 0 to 100, with one -to three strengths and gaps. The total is `app.models.cv_score`, what the -feed's Best CV match sorts by. Culture and location are left out: the first +to three strengths and gaps. The total is `app.models.cv_score`, the three +numbers under the agent's `cv_*` weights, worked out when a role is served and +not stored, so the weights are no part of `input_key`; it is what the feed's +Best CV match sorts by. Culture and location are left out: the first is guessed from ad text, the second is what the gates do. The work is pairs, `(agent, role)`: an enabled agent with a CV, and a role diff --git a/app/models.py b/app/models.py index decaa13..641768d 100644 --- a/app/models.py +++ b/app/models.py @@ -1007,21 +1007,31 @@ def language_flag(required: list[RoleLanguage], return "", "" -def cv_score(skills: int, experience: int, career: int) -> float: - """The one number of a CV match, 0 to 100: skills and career weigh 30, - experience 25 (docs/decisions.md, the CV match).""" - return round((30 * skills + 25 * experience + 30 * career) / 85, 1) +def cv_score(skills: int, experience: int, career: int, + weights: "Weights | None" = None) -> float | None: + """The one number of a CV match, 0 to 100: the three numbers of the + judge averaged by the agent's `cv_skills`, `cv_experience` and + `cv_career` weights (30, 25 and 30 by default). None when all three + weights are 0.""" + w = weights or Weights() + total = w.cv_skills + w.cv_experience + w.cv_career + if not total: + return None + return round((w.cv_skills * skills + w.cv_experience * experience + + w.cv_career * career) / total, 1) class Judgement(Model): """What `judge` read of one role for one agent (`role_judgements`): the - three dimensions 0 to 100, `cv_score` (`cv_score`), and 1 to 3 strengths - and gaps grounded in the posting.""" + three dimensions 0 to 100, `cv_score` (`cv_score`) and 1 to 3 strengths + and gaps grounded in the posting. `cv_score` is not read from the table: + `Store.judgement` sets it from the agent's weights; None when all three + are 0.""" skills: int experience: int career: int - cv_score: float + cv_score: float | None = None strengths: JsonStrings = [] gaps: JsonStrings = [] judged_at: str @@ -1403,6 +1413,13 @@ class Weights(Model): affinity: float = Field(0.2, ge=0) #: Cosine to the liked-minus-passed direction of the agent's votes. taste: float = Field(0.2, ge=0) + #: How much each of the judge's three numbers counts in `cv_score`. Not + #: a score term: they are read when a role is served, so changing them + #: asks the model for nothing. 0 leaves a number out, all three 0 means + #: no `cv_score`. + cv_skills: float = Field(30, ge=0, le=1000, allow_inf_nan=False) + cv_experience: float = Field(25, ge=0, le=1000, allow_inf_nan=False) + cv_career: float = Field(30, ge=0, le=1000, allow_inf_nan=False) class Limits(Model): diff --git a/app/static/agents.js b/app/static/agents.js index 74a1836..a93bb12 100644 --- a/app/static/agents.js +++ b/app/static/agents.js @@ -33,7 +33,8 @@ const { api, count, remoteHelpers: rh } = window; const $ = (sel, root = document) => root.querySelector(sel); - const DEFAULT_WEIGHTS = { fit: 0.6, recency: 0.2, affinity: 0.2, taste: 0.2 }; + const DEFAULT_WEIGHTS = { fit: 0.6, recency: 0.2, affinity: 0.2, taste: 0.2, + cv_skills: 30, cv_experience: 25, cv_career: 30 }; // What the model reads after a pull, per agent: the best `extract_top` // roles scoring at least `extract_min` get their gate columns filled, the // best `summarise_top` at `summarise_min` or more a summary. @@ -219,7 +220,8 @@ function weightsLine(row) { const w = Object.assign({}, DEFAULT_WEIGHTS, row.weights); - return `fit ${w.fit} · newness ${w.recency} · companies ${w.affinity} · liked ${w.taste}`; + return `fit ${w.fit} · newness ${w.recency} · companies ${w.affinity} · liked ${w.taste}` + + ` · CV match: skills ${w.cv_skills}, experience ${w.cv_experience}, career ${w.cv_career}`; } // One gate as a phrase: "in Denmark or Sweden", "remote", "with seniority @@ -769,6 +771,9 @@ $("#a-recency", f).value = weights.recency; $("#a-affinity", f).value = weights.affinity; $("#a-taste", f).value = weights.taste; + $("#a-cv-skills", f).value = weights.cv_skills; + $("#a-cv-experience", f).value = weights.cv_experience; + $("#a-cv-career", f).value = weights.cv_career; $("#a-extract-top", f).value = limits.extract_top; $("#a-extract-min", f).value = limits.extract_min; $("#a-summarise-top", f).value = limits.summarise_top; @@ -875,6 +880,9 @@ recency: Number($("#a-recency", f).value), affinity: Number($("#a-affinity", f).value), taste: Number($("#a-taste", f).value), + cv_skills: Number($("#a-cv-skills", f).value), + cv_experience: Number($("#a-cv-experience", f).value), + cv_career: Number($("#a-cv-career", f).value), }, limits: { extract_top: Number($("#a-extract-top", f).value), diff --git a/app/storage/store.py b/app/storage/store.py index 4e15439..682500d 100644 --- a/app/storage/store.py +++ b/app/storage/store.py @@ -119,14 +119,31 @@ _VOTED_SCORE = ("MIN(1.0, s.score * CASE v.vote WHEN -1 THEN 0.5 " #: nothing, so `vote` and `voted_at` are NULL: one query shape either way. _WITH_VOTES = "LEFT JOIN role_votes v ON v.uid = roles.uid AND v.agent_id = ?" -#: The agent's `judge` readings beside every role, the same way: `j.cv_score` -#: is NULL for a role not judged and with no agent. +#: The agent's `judge` readings beside every role, the same way: `j.skills`, +#: `j.experience` and `j.career` are NULL for a role not judged and with no +#: agent. _WITH_JUDGEMENTS = ("LEFT JOIN role_judgements j ON j.uid = roles.uid AND j.agent_id = ? " "AND j.judged_at IS NOT NULL") -#: The order of Best CV match: the judged roles by `cv_score`, then the rest -#: by score, as `roles` orders them. -_BY_CV = "j.cv_score IS NULL, j.cv_score DESC, " + + +def _cv_sql(weights: Weights | None) -> str: + """`cv_score` as SQL for one agent's weights, unrounded (`app.models.cv_score` + rounds it): NULL for a role not judged, and for all three weights 0. The + numbers are the model's validated, finite floats, so they are written in + rather than bound, the select's placeholders being then no concern.""" + w = weights or Weights() + total = w.cv_skills + w.cv_experience + w.cv_career + if not total: + return "NULL" + return (f"(({w.cv_skills!r} * j.skills + {w.cv_experience!r} * j.experience " + f"+ {w.cv_career!r} * j.career) / {total!r})") + + +def _by_cv(expression: str) -> str: + """The order of Best CV match: the judged roles by `cv_score` (its SQL + `expression`), then the rest by score, as `roles` orders them.""" + return f"{expression} IS NULL, {expression} DESC, " #: How many days after today the feed's Closing soon filter reaches. CLOSING_DAYS = 4 @@ -880,7 +897,8 @@ class Store: def role(self, uid: str, agent_id: int | None = None) -> ScoredRole | None: """One role with its tags, and with `vote`, `voted_at`, `score`, `gated_reason` and `prompt` as `agent_id` judged and scored it, and - its `judgement`, `cv_score` and language flag for that agent; None + its `judgement`, `cv_score` (under the agent's weights as they are + now) and language flag for that agent; None (unjudged, unscored) without an agent. The score is moved by the vote as `roles` serves it.""" row = self._row( @@ -1122,7 +1140,8 @@ class Store: With `by_cv` as well, the roles the agent's `judge` read come first by `cv_score`, then the others by score (Best CV match). Every row - carries the agent's `cv_score` (None when not judged) and the + carries the agent's `cv_score` (None when not judged), computed + from the three numbers under the agent's weights at read time, and the language flag of its languages against the agent's (`language_flag`). `closing` keeps the roles whose `deadline` falls today or in the next `CLOSING_DAYS` days. @@ -1138,12 +1157,14 @@ class Store: judged=judged, pay=pay, company_keys=company_keys, places=places, seniority=seniority, seniority_unknown=seniority_unknown, pay_unknown=pay_unknown, closing=closing) - select = "SELECT roles.*, v.vote, v.voted_at, j.cv_score" + weights = self._weights_of(agent) + cv = _cv_sql(weights) + select = f"SELECT roles.*, v.vote, v.voted_at, {cv} AS cv_score" order = "roles.posted_at DESC, roles.uid" scored = agent is not None and by_score if scored: select += f", {_VOTED_SCORE} AS score, s.gated_reason, s.prompt" - order = (_BY_CV if by_cv else "") + "score DESC, " + order + order = (_by_cv(cv) if by_cv else "") + "score DESC, " + order elif open_only: # Newest first, open only: idx_roles_posted covers this exactly # (posted_at DESC, uid, WHERE closed_at IS NULL), so it earns the @@ -1162,8 +1183,15 @@ class Store: r.tags = by_uid.get(r.uid, []) r.remote_where = areas.get(r.uid, "") r.lang_flag, r.lang_note = language_flag(r.languages_required, mine) + if r.cv_score is not None: + r.cv_score = round(r.cv_score, 1) return found + def _weights_of(self, agent_id: int | None) -> Weights | None: + """The agent's weights, None with no agent.""" + found = self.agent(agent_id) if agent_id is not None else None + return found.weights if found else None + def _languages_of(self, agent_id: int | None) -> list[CvLanguage]: """The languages the agent's CV states, [] with no agent.""" found = self.agent(agent_id) if agent_id is not None else None @@ -1201,18 +1229,19 @@ class Store: judged=judged, pay=pay, company_keys=company_keys, places=places, seniority=seniority, seniority_unknown=seniority_unknown, pay_unknown=pay_unknown, closing=closing) + cv = _cv_sql(self._weights_of(agent)) select = ( "SELECT roles.uid, roles.company_key, roles.company, roles.title, roles.place, " "roles.country, roles.remote, roles.function, roles.seniority, roles.salary_min, " "roles.salary_max, roles.currency, roles.salary_text, roles.original_url, " "roles.posting_url, roles.posted_at, roles.read_at, roles.deadline, " - "roles.languages_required, v.vote, v.voted_at, j.cv_score, " + f"roles.languages_required, v.vote, v.voted_at, {cv} AS cv_score, " f"SUBSTR({_EXCERPT_SOURCE}, 1, {_EXCERPT_RAW}) AS excerpt") order = "roles.posted_at DESC, roles.uid" scored = agent is not None and by_score if scored: select += f", {_VOTED_SCORE} AS score, s.gated_reason, s.prompt" - order = (_BY_CV if by_cv else "") + "score DESC, " + order + order = (_by_cv(cv) if by_cv else "") + "score DESC, " + order elif open_only: # Same reasoning as `roles`: the planner otherwise sorts every # open role before applying LIMIT. @@ -1224,6 +1253,8 @@ class Store: for r in rows: row = dict(r) row["excerpt"] = excerpt(row["excerpt"]) + if row["cv_score"] is not None: + row["cv_score"] = round(row["cv_score"], 1) row["lang_flag"], row["lang_note"] = language_flag( role_languages(row.pop("languages_required")), mine) found.append(RoleRow.from_row(row)) @@ -1859,7 +1890,8 @@ class Store: experience: int, career: int, strengths: Sequence[str], gaps: Sequence[str]) -> bool: """Store `judge`'s reading of one role for one agent, replacing the - one before, with `cv_score` (`app.models.cv_score`) and its tries + one before, with `cv_score` (`app.models.cv_score`, at the default weights: the + table's column is not read, see `judgement`) and its tries back at 0. False for an unknown role or agent.""" try: with self.transaction() as conn: @@ -1901,12 +1933,20 @@ class Store: return int(crossed) def judgement(self, agent_id: int | None, uid: str) -> Judgement | None: - """The agent's judgement of one role, None when it has none.""" + """The agent's judgement of one role, None when it has none, its + `cv_score` the three numbers under the agent's weights as they are + now, not as stored.""" if agent_id is None: return None row = self._row("SELECT * FROM role_judgements WHERE agent_id = ? AND uid = ? " "AND judged_at IS NOT NULL", (agent_id, uid)) - return Judgement.from_row(row) if row else None + if row is None: + return None + found = Judgement.from_row(row) + agent = self.agent(agent_id) + found.cv_score = cv_score(found.skills, found.experience, found.career, + agent.weights if agent else None) + return found def set_article_text(self, uid: str, text: str) -> Role | None: """Store the posting page as plain text and stamp `article_at`. The diff --git a/app/templates/settings/agents.html b/app/templates/settings/agents.html index c8a9e52..cb25567 100644 --- a/app/templates/settings/agents.html +++ b/app/templates/settings/agents.html @@ -106,6 +106,13 @@ +
+ The judge's three numbers are averaged with these shares into the CV match. Changing them re-sorts at once and asks the model for nothing; 0 leaves a number out, all three 0 means no CV match.