Give a search agent several prompts and score a role by its best one

Migration 14 moves each non-empty agents.intent into agent_prompts as one
prompt named after the agent, with its cached vector, and drops the three
intent columns. A role's fit is the maximum over the agent's prompts of the
calibrated cosine, and scores.prompt names the prompt that won. The agents
API takes prompts and still takes intent; the role endpoint serves the
agent's score and prompt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
WolfandClaude Opus 5.5 committed 2026-09-29 16:19:57 +02:00
1 parent b6afe24eaa
commit 4e3c3f130e
9 files changed
+552 -127

No files matched your search

+5 -2
View File
@@ -22,7 +22,7 @@ from typing import Any, Literal
import yaml
from pydantic import Field, ValidationError, field_validator, model_validator
from app.models import Gate, Limits, Weights
from app.models import Gate, Limits, Prompts, Weights
from auth import AuthConfig
from desk.config import AgentConfig, ModelTaskConfig, Section, WorkersConfig
@@ -129,9 +129,12 @@ class EmbedConfig(Section):
class AgentSeed(Section):
"""A search agent as `config.yaml` lists it. Imported into an empty
`agents` table once; after that the table is the answer."""
`agents` table once; after that the table is the answer. `prompts` is a
list of `{name, text}`; without it, `intent` is the one prompt, named
after the agent."""
name: str
prompts: Prompts | None = None
intent: str = ""
gates: list[Gate] = Field(default_factory=list)
weights: Weights = Field(default_factory=Weights)
+67 -46
View File
@@ -14,10 +14,11 @@ A gate rejects on evidence, never on absence: a role whose column is empty
is not gated by it. `extract` is what fills an empty column, and it reads
only the roles that rank, so gating on an empty column would keep those
roles out of extract's reach for good.
fit cos(role vector, intent vector) as a level: the embedding
fit cos(role vector, prompt vector) as a level: the embedding
model's calibrated `lo` is 0 and `hi` 1, clipped outside
(see `scale`), so a role's fit does not depend on what
else is open
else is open; with several prompts, the best prompt's
level, and the score names that prompt
recency 0.5 ** (days since posted_at / 14)
affinity (the agent's affinity for the company + 1) / 3
taste cos(role vector, liked direction), scaled like fit with the
@@ -32,10 +33,10 @@ company (`affinity = 2`) skips the gates and is floored at 0.9; a hidden one
(`-1`) is not scored at all. A role with no vector is scored without `fit`
and `taste`, unless `fit` is all there is, when it is gated as "no vector".
An agent with no votes scores every role without `taste`; the direction is
newsdesk's `centroid(liked) - centroid(disliked)`. An agent with no intent
has no `fit` either: its scores are not a match, so it is left out of what
extract and summarise read (`Store._top_scope`), and a run names such agents
in one notice.
newsdesk's `centroid(liked) - centroid(disliked)`. An agent with no prompt
that has text has no `fit` either: its scores are not a match, so it is left
out of what extract and summarise read (`Store._top_scope`), and a run names
such agents in one notice.
Recomputed in full on every run, and each agent's rows are replaced in one
transaction: nothing is incremental, so nothing can drift.
@@ -176,7 +177,7 @@ def score_one(candidate: Candidate, gates: Sequence[Gate], weights: Weights,
missing: str = "no vector") -> tuple[float, str]:
"""One role for one agent: `(score, gated_reason)`. `fit` and `taste` are
already scaled to 0..1 (see `scale`), or None when there is nothing to
measure: no role vector, no intent vector, no votes. A None term leaves
measure: no role vector, no prompt vector, no votes. A None term leaves
the denominator; `missing` says why fit is out."""
favourite = candidate.affinity == 2
if failed := failed_gates(candidate, gates):
@@ -207,8 +208,8 @@ def intent_key(model: str, intent: str) -> str:
class _Encoder:
"""The embedder, loaded on first use only: an agent whose intent vector
is cached costs no model load at all."""
"""The embedder, loaded on first use only: an agent whose prompt vectors
are cached costs no model load at all."""
def __init__(self, model: str, context: RunContext) -> None:
self.model = model
@@ -229,66 +230,85 @@ class _Encoder:
return np.asarray(self._embedder.encode([text])[0])
def _intent(store: Store, agent: Agent, model: str,
encoder: _Encoder) -> np.ndarray | None:
if not agent.intent.strip():
return None
key = intent_key(model, agent.intent)
cached = store.intent_vector(agent.id, key)
if cached is not None:
return cached
vec = encoder.encode(embed.intent_text(model, agent.intent))
if vec is not None:
store.set_intent_vector(agent.id, key, vec)
return vec
def _prompts(store: Store, agent: Agent, model: str,
encoder: _Encoder) -> list[tuple[str, np.ndarray]]:
"""`(name, vector)` for each of the agent's prompts that has text and
could be embedded, in order. Each vector is cached under its own key, so
an edit re-embeds only the prompt it changed."""
found = []
for prompt in agent.prompts:
if not prompt.text.strip():
continue
key = intent_key(model, prompt.text)
vec = store.prompt_vector(agent.id, prompt.name, key)
if vec is None:
vec = encoder.encode(embed.intent_text(model, prompt.text))
if vec is None:
continue
store.set_prompt_vector(agent.id, prompt.name, key, vec)
found.append((prompt.name, vec))
return found
# ------------------------------------------------------------------- the job
def no_intent(names: Sequence[str]) -> Notice:
"""The notice for enabled agents without an intent: their scores have no
fit, so `Store._top_scope` keeps them from feeding extract and summarise."""
"""The notice for enabled agents without a prompt that has text: their
scores have no fit, so `Store._top_scope` keeps them from feeding extract
and summarise. The code keeps its name from when an agent had one intent."""
return Notice(
code="score.no_intent", level="warning", kind="expected", key="score",
text=f"No intent, so ranked without fit and not read by extract or "
f"summarise: {', '.join(names)}.",
fix="Give the agent an intent under Settings, Search agents.",
text=f"No prompt with text, so ranked without fit and not read by extract "
f"or summarise: {', '.join(names)}.",
fix="Give the agent a prompt under Settings, Search agents.",
where="app.jobs.score")
def score_agent(store: Store, agent: Agent, vectors: dict[str, np.ndarray],
intent: np.ndarray | None, now: datetime,
prompts: Sequence[tuple[str, np.ndarray]], now: datetime,
calibration: Calibration | None = None) -> dict[str, int]:
"""Two passes: the cosines of the roles this agent scores, to its intent
and to its taste direction, then every score with both scaled by the
model's `calibration`, or across the run without one. A gated role is
left out of the cosines, so under the fallback a role that fails a
require gate cannot stretch the range the ranked roles share. The voted
roles are scored like any other; a liked one sits near the direction it
helped make, which is what a like should do."""
"""Two passes: the cosines of the roles this agent scores, to each of its
`prompts` and to its taste direction, then every score with both scaled
by the model's `calibration`, or across the run without one. A role's fit
is the best of its prompts' levels, each scaled on its own, and the score
keeps that prompt's name; the first prompt wins a tie. Under the min-max
fallback each prompt is ranged over the run on its own too, so every
prompt's closest role gets 1 and one prompt equals the old single intent.
A gated role is left out of the cosines, so under the fallback a role
that fails a require gate cannot stretch the range the ranked roles
share. The voted roles are scored like any other; a liked one sits near
the direction it helped make, which is what a like should do."""
candidates = store.score_candidates(agent.id, agent.gates)
direction = taste_direction(vectors, store.votes(agent.id))
cosines: dict[str, float] = {}
cosines: dict[str, dict[str, float]] = {name: {} for name, _ in prompts}
tastes: dict[str, float] = {}
for candidate in candidates:
role_vec = vectors.get(candidate.uid)
if role_vec is None or failed_gates(candidate, agent.gates):
continue
if intent is not None:
cosines[candidate.uid] = cosine(role_vec, intent)
for name, prompt_vec in prompts:
cosines[name][candidate.uid] = cosine(role_vec, prompt_vec)
if direction is not None:
tastes[candidate.uid] = cosine(role_vec, direction)
fits = scale(cosines, calibration.fit if calibration else None)
fits: dict[str, float] = {}
best: dict[str, str] = {}
for name, levels in cosines.items():
for uid, value in scale(levels, calibration.fit if calibration else None).items():
if uid not in fits or value > fits[uid]:
fits[uid], best[uid] = value, name
tastes = scale(tastes, calibration.taste if calibration else None)
rows: list[tuple[str, float, str]] = []
rows: list[tuple[str, float, str, str]] = []
gated = 0
for candidate in candidates:
fit = fits.get(candidate.uid)
value, reason = score_one(
candidate, agent.gates, agent.weights, fits.get(candidate.uid), now,
candidate, agent.gates, agent.weights, fit, now,
taste=tastes.get(candidate.uid),
missing="no vector" if intent is not None else "no intent vector")
missing="no vector" if prompts else "no prompt vector")
gated += bool(reason)
rows.append((candidate.uid, round(value, 6), reason))
counted = fit is not None and agent.weights.fit and not reason
rows.append((candidate.uid, round(value, 6), reason,
best[candidate.uid] if counted else ""))
store.replace_scores(agent.id, rows)
return {"scored": len(rows), "gated": gated}
@@ -307,14 +327,15 @@ def run_score(config: Config, store: Store, *,
log.warning("no calibration for %s: fit and taste are min-max scaled across "
"each agent's roles in this run, so they rank rather than state a "
"level; add it under embed.calibration", model)
if blind := [agent.name for agent in agents if not agent.intent.strip()]:
if blind := [agent.name for agent in agents
if not any(p.text.strip() for p in agent.prompts)]:
context.notice(no_intent(blind))
scored = gated = 0
for n, agent in enumerate(agents):
context.check()
context.progress(n, len(agents), f"scoring for {agent.name}, {n + 1} of {len(agents)}")
counts = score_agent(store, agent, vectors, _intent(store, agent, model, encoder), now,
calibration)
counts = score_agent(store, agent, vectors, _prompts(store, agent, model, encoder),
now, calibration)
scored += counts["scored"]
gated += counts["gated"]
log.info("%-20s %6d scored, %6d gated", agent.name, counts["scored"], counts["gated"])
@@ -335,7 +356,7 @@ def seed_agents(config: Config, store: Store) -> list[str]:
for seed in config.agents:
try:
store.save_agent(seed.name, seed.intent, seed.gates, seed.weights,
seed.enabled, limits=seed.limits)
seed.enabled, limits=seed.limits, prompts=seed.prompts)
except ValueError as exc:
log.warning("agents: %s", exc)
continue
+42 -4
View File
@@ -15,7 +15,7 @@ import re
import unicodedata
from typing import Annotated, Any, Literal, get_args
from pydantic import BeforeValidator, Field, computed_field
from pydantic import AfterValidator, BeforeValidator, Field, computed_field, field_validator
from desk.models import JsonDict, Model, ModelTaskResult
@@ -828,6 +828,9 @@ class ScoredRole(Role):
score: float | None = None
gated_reason: str = ""
#: The agent's prompt that gave the role its fit; "" when fit did not
#: count (gated, no vector, no prompt with text).
prompt: str = ""
#: How many characters `RoleRow.excerpt` keeps at most: the feed row's own
@@ -881,6 +884,7 @@ class RoleRow(Model):
posted_at: str = ""
score: float | None = None
gated_reason: str = ""
prompt: str = ""
vote: Literal[-1, 0, 1] | None = None
voted_at: str | None = None
read_at: str | None = None
@@ -1016,13 +1020,47 @@ class Limits(Model):
summarise_min: float = Field(0.65, ge=0, le=1)
class Prompt(Model):
"""One of an agent's descriptions of what it looks for. Each is embedded
on its own and a role's fit is its best prompt's (see `app.jobs.score`),
so one agent can hunt several kinds of role without blending them. A
prompt with no text has no fit to give."""
name: str
text: str = ""
@field_validator("name")
@classmethod
def _named(cls, name: str) -> str:
name = name.strip()
if not name:
raise ValueError("a prompt needs a name")
return name
def check_prompts(prompts: list[Prompt]) -> list[Prompt]:
"""The prompts as they are, or ValueError when two share a name
regardless of case."""
seen: set[str] = set()
for prompt in prompts:
if prompt.name.casefold() in seen:
raise ValueError(f"two prompts are named {prompt.name!r}")
seen.add(prompt.name.casefold())
return prompts
#: An agent's prompts, in order, their names unique regardless of case.
Prompts = Annotated[list[Prompt], AfterValidator(check_prompts)]
class Agent(Model):
"""A row of `agents`: a named search with its own intent, gates, weights
and limits. The cached intent vector is a column this does not declare."""
"""A row of `agents`: a named search with its own prompts, gates, weights
and limits. The prompts are rows of `agent_prompts`, read alongside; their
cached vectors are columns this does not declare."""
id: int
name: str
intent: str = ""
prompts: Prompts = []
gates: Annotated[list[Gate], BeforeValidator(lambda v: _json(v, "[]"))] = []
weights: Annotated[Weights, BeforeValidator(lambda v: _json(v, "{}"))] = Weights()
limits: Annotated[Limits, BeforeValidator(lambda v: _json(v, "{}"))] = Limits()
+44 -8
View File
@@ -177,25 +177,34 @@ BEGIN
SELECT RAISE(ABORT, 'application_events is append-only');
END;
-- A search agent: a named saved search with its own intent text, gates and
-- A search agent: a named saved search with its own prompts, gates and
-- weights. gates is a list of {field, values, mode: require|prefer, weight}.
-- intent_vec caches the intent's vector; intent_key is sha1 of the embedding
-- model and the intent text, so editing either makes it stale. limits is how
-- deep into this agent's ranking extract and summarise read, as JSON: a key
-- it lacks means the `Limits` default.
-- limits is how deep into this agent's ranking extract and summarise read,
-- as JSON: a key it lacks means the `Limits` default.
CREATE TABLE IF NOT EXISTS agents (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL UNIQUE,
intent TEXT NOT NULL DEFAULT '',
gates TEXT NOT NULL DEFAULT '[]',
weights TEXT NOT NULL DEFAULT '{}',
enabled INTEGER NOT NULL DEFAULT 1,
created_at TEXT NOT NULL,
intent_key TEXT NOT NULL DEFAULT '',
intent_vec BLOB,
limits TEXT NOT NULL DEFAULT '{}'
);
-- What an agent looks for, as several named texts in order; a role's fit is
-- its best prompt's. vec caches the text's vector and vec_key is sha1 of the
-- embedding model and the text, so editing one prompt re-embeds that one.
CREATE TABLE IF NOT EXISTS agent_prompts (
agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
position INTEGER NOT NULL,
name TEXT NOT NULL,
text TEXT NOT NULL DEFAULT '',
vec_key TEXT NOT NULL DEFAULT '',
vec BLOB,
PRIMARY KEY (agent_id, position),
UNIQUE (agent_id, name COLLATE NOCASE)
);
-- One agent's stance on a company: -1 hidden, 0 neutral, 1 liked, 2
-- favourite. No row is 0, so the table holds only what somebody set. Per
-- agent, because a company that is noise for one search is the target of
@@ -220,12 +229,14 @@ CREATE TABLE IF NOT EXISTS role_votes (
);
-- A role is scored per agent. A gated role keeps its row, with the reason.
-- prompt names the agent's prompt that gave the fit, '' when fit did not count.
CREATE TABLE IF NOT EXISTS scores (
uid TEXT NOT NULL REFERENCES roles(uid) ON DELETE CASCADE,
agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
score REAL NOT NULL DEFAULT 0,
gated_reason TEXT NOT NULL DEFAULT '',
scored_at TEXT NOT NULL,
prompt TEXT NOT NULL DEFAULT '',
PRIMARY KEY (uid, agent_id)
);
@@ -388,6 +399,31 @@ ALTER TABLE roles ADD COLUMN salary_text TEXT NOT NULL DEFAULT '';
"""),
(13, "roles.country recomputed from place for every not-yet-extracted role whose "
"board gave none, country lookup fix", lambda conn: _recompute_country(conn)),
# An agent's one intent becomes its first prompt, named after the agent,
# with its cached vector: the key hashes the model and the text, which
# are unchanged, so nothing is embedded again. An empty intent becomes no
# prompt. The three columns are then dropped, as v6 dropped the ones it
# moved.
(14, "agent_prompts and scores.prompt: an agent holds several prompts, and a "
"score names the one that gave the fit", """
CREATE TABLE agent_prompts (
agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
position INTEGER NOT NULL,
name TEXT NOT NULL,
text TEXT NOT NULL DEFAULT '',
vec_key TEXT NOT NULL DEFAULT '',
vec BLOB,
PRIMARY KEY (agent_id, position),
UNIQUE (agent_id, name COLLATE NOCASE)
);
INSERT INTO agent_prompts (agent_id, position, name, text, vec_key, vec)
SELECT id, 0, name, intent, intent_key, intent_vec FROM agents
WHERE TRIM(intent, ' ' || char(9, 10, 13)) <> '';
ALTER TABLE agents DROP COLUMN intent;
ALTER TABLE agents DROP COLUMN intent_key;
ALTER TABLE agents DROP COLUMN intent_vec;
ALTER TABLE scores ADD COLUMN prompt TEXT NOT NULL DEFAULT '';
"""),
)
#: What a file created from `SCHEMA` today is already at.
+87 -43
View File
@@ -22,8 +22,9 @@ import numpy as np
from desk.models import Item
from app.models import (
AFFINITY, REMOTE, STATUSES, Agent, Application, ApplicationEvent, Candidate,
Company, Gate, Limits, Role, RoleRow, ScoredRole, Subscription, Weights, company_key,
country_of, excerpt, place_key, posted_at, posted_host, remote_of, title_key,
Company, Gate, Limits, Prompt, Role, RoleRow, ScoredRole, Subscription, Weights,
check_prompts, company_key, country_of, excerpt, place_key, posted_at, posted_host,
remote_of, title_key,
)
from app.storage import schema
@@ -369,14 +370,20 @@ class Store:
f"ON CONFLICT(uid) DO UPDATE SET {', '.join(updates)}", row)
return not existed
def role(self, uid: str, agent_id: int | None = None) -> Role | None:
"""One role with its tags, and with `vote` and `voted_at` as
`agent_id` judged it; None (unjudged) without an agent."""
row = self._row(f"SELECT roles.*, v.vote, v.voted_at FROM roles {_WITH_VOTES} "
"WHERE roles.uid=?", (agent_id, uid))
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; None
(unjudged, unscored) without an agent. The score is moved by the vote
as `roles` serves it."""
row = self._row(
f"SELECT roles.*, v.vote, v.voted_at, {_VOTED_SCORE} AS score, "
"COALESCE(s.gated_reason, '') AS gated_reason, COALESCE(s.prompt, '') AS prompt "
f"FROM roles {_WITH_VOTES} "
"LEFT JOIN scores s ON s.uid = roles.uid AND s.agent_id = ? WHERE roles.uid=?",
(agent_id, agent_id, uid))
if row is None:
return None
role = Role.from_row(row)
role = ScoredRole.from_row(row)
role.tags = self._tags_of(uid)
return role
@@ -397,7 +404,7 @@ class Store:
assert role is not None
return role
def set_vote(self, agent_id: int, uid: str, value: int | None) -> Role:
def set_vote(self, agent_id: int, uid: str, value: int | None) -> ScoredRole:
"""One agent's judgement of one role: 1 like, 0 neither way, -1 pass,
None unjudged again, which deletes the row. A vote never hides a
role; it moves it in that agent's ranking (see `roles`) and shapes its
@@ -532,7 +539,7 @@ class Store:
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"
select += f", {_VOTED_SCORE} AS score, s.gated_reason, s.prompt"
order = "score DESC, " + order
elif open_only:
# Newest first, open only: idx_roles_posted covers this exactly
@@ -584,7 +591,7 @@ class Store:
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"
select += f", {_VOTED_SCORE} AS score, s.gated_reason, s.prompt"
order = "score DESC, " + order
elif open_only:
# Same reasoning as `roles`: the planner otherwise sorts every
@@ -763,8 +770,9 @@ class Store:
held to the limits of the agent that ranked it; a key the JSON lacks
is the `Limits` default. The role is `r`. A company an agent hid
needs no clause: `score` writes no rows for it under that agent. An
agent without an intent counts for nothing here: its scores have no
fit, so they say how fresh a role is, not whether it is wanted."""
agent without a prompt with text counts for nothing here: its scores
have no fit, so they say how fresh a role is, not whether it is
wanted."""
defaults = Limits()
return (f"""r.uid IN (SELECT uid FROM (
SELECT s.uid, s.score, a.limits, ROW_NUMBER() OVER (
@@ -772,7 +780,9 @@ class Store:
ORDER BY s.score DESC, top.posted_at DESC, s.uid) AS n
FROM scores s
JOIN agents a ON a.id = s.agent_id AND a.enabled = 1
AND TRIM(a.intent, ' ' || char(9, 10, 13)) <> ''
AND EXISTS (SELECT 1 FROM agent_prompts p
WHERE p.agent_id = a.id
AND TRIM(p.text, ' ' || char(9, 10, 13)) <> '')
JOIN roles top ON top.uid = s.uid AND top.closed_at IS NULL
WHERE s.gated_reason = '')
WHERE n <= COALESCE(json_extract(limits, '$.{stage}_top'), ?)
@@ -1000,42 +1010,71 @@ class Store:
sql = "SELECT * FROM agents"
if enabled_only:
sql += " WHERE enabled = 1"
return [Agent.from_row(r)
for r in self._rows(sql + " ORDER BY name COLLATE NOCASE")]
return self._agents(self._rows(sql + " ORDER BY name COLLATE NOCASE"))
def agent(self, agent_id: int) -> Agent | None:
row = self._row("SELECT * FROM agents WHERE id=?", (agent_id,))
return Agent.from_row(row) if row else None
found = self._agents(self._rows("SELECT * FROM agents WHERE id=?", (agent_id,)))
return found[0] if found else None
def default_agent(self) -> Agent | None:
"""The agent a vote or an affinity goes to when none is named: the
enabled one with the lowest id, else the lowest id, else None."""
row = self._row("SELECT * FROM agents ORDER BY enabled DESC, id LIMIT 1")
return Agent.from_row(row) if row else None
found = self._agents(self._rows(
"SELECT * FROM agents ORDER BY enabled DESC, id LIMIT 1"))
return found[0] if found else None
def _agents(self, rows: Sequence[sqlite3.Row]) -> list[Agent]:
"""The rows as agents, each with its prompts in order, read with one
query for them all."""
prompts: dict[int, list[dict[str, str]]] = {}
for p in self._rows(
"SELECT agent_id, name, text FROM agent_prompts WHERE agent_id IN "
"(SELECT value FROM json_each(?)) ORDER BY agent_id, position",
(json.dumps([r["id"] for r in rows]),)):
prompts.setdefault(p["agent_id"], []).append({"name": p["name"], "text": p["text"]})
return [Agent.from_row({**dict(r), "prompts": prompts.get(r["id"], [])})
for r in rows]
def save_agent(self, name: str, intent: str = "", gates: Sequence[Gate] = (),
weights: Weights | None = None, enabled: bool = True,
agent_id: int | None = None, limits: Limits | None = None) -> Agent:
"""Create an agent, or with `agent_id` replace that one's fields.
Raises ValueError on an empty or taken name, KeyError on an unknown id."""
agent_id: int | None = None, limits: Limits | None = None,
prompts: Sequence[Prompt] | None = None) -> Agent:
"""Create an agent, or with `agent_id` replace that one's fields and
prompts. Without `prompts`, a non-empty `intent` is its one prompt,
named after the agent. A prompt whose text is unchanged keeps its
cached vector, whatever its name or place, so an edit re-embeds only
the prompts it changed. Raises ValueError on an empty or taken name,
or two prompts of one name, KeyError on an unknown id."""
name = name.strip()
if not name:
raise ValueError("an agent needs a name")
fields = (name, intent, json.dumps([g.model_dump() for g in gates]),
if prompts is None:
prompts = [Prompt(name=name, text=intent)] if intent.strip() else []
prompts = check_prompts(list(prompts))
fields = (name, json.dumps([g.model_dump() for g in gates]),
(weights or Weights()).model_dump_json(),
(limits or Limits()).model_dump_json(), int(enabled))
try:
with self.transaction() as conn:
if agent_id is None:
agent_id = conn.execute(
"""INSERT INTO agents (name, intent, gates, weights, limits,
enabled, created_at) VALUES (?,?,?,?,?,?,?)""",
"""INSERT INTO agents (name, gates, weights, limits, enabled,
created_at) VALUES (?,?,?,?,?,?)""",
(*fields, self.now())).lastrowid
elif not conn.execute(
"""UPDATE agents SET name=?, intent=?, gates=?, weights=?,
limits=?, enabled=? WHERE id=?""",
"""UPDATE agents SET name=?, gates=?, weights=?, limits=?, enabled=?
WHERE id=?""",
(*fields, agent_id)).rowcount:
raise KeyError(agent_id)
cached = {r["text"]: (r["vec_key"], r["vec"]) for r in conn.execute(
"SELECT text, vec_key, vec FROM agent_prompts WHERE agent_id=?",
(agent_id,))}
conn.execute("DELETE FROM agent_prompts WHERE agent_id=?", (agent_id,))
conn.executemany(
"""INSERT INTO agent_prompts (agent_id, position, name, text, vec_key, vec)
VALUES (?,?,?,?,?,?)""",
[(agent_id, n, p.name, p.text, *cached.get(p.text, ("", None)))
for n, p in enumerate(prompts)])
except sqlite3.IntegrityError as exc:
raise ValueError(f"an agent named {name!r} already exists") from exc
assert agent_id is not None
@@ -1044,20 +1083,22 @@ class Store:
return found
def delete_agent(self, agent_id: int) -> bool:
"""Its scores, votes and affinities go with it, by the foreign keys."""
"""Its prompts, scores, votes and affinities go with it, by the
foreign keys."""
return self._write("DELETE FROM agents WHERE id=?", (agent_id,)) > 0
def intent_vector(self, agent_id: int, key: str) -> np.ndarray | None:
"""The agent's cached intent vector, if it was made under `key`."""
row = self._row("SELECT intent_key, intent_vec FROM agents WHERE id=?",
(agent_id,))
if row is None or row["intent_key"] != key or row["intent_vec"] is None:
def prompt_vector(self, agent_id: int, name: str, key: str) -> np.ndarray | None:
"""The cached vector of the agent's prompt `name`, if it was made
under `key`."""
row = self._row("SELECT vec_key, vec FROM agent_prompts WHERE agent_id=? AND name=?",
(agent_id, name))
if row is None or row["vec_key"] != key or row["vec"] is None:
return None
return unpack(row["intent_vec"])
return unpack(row["vec"])
def set_intent_vector(self, agent_id: int, key: str, vec: np.ndarray) -> None:
self._write("UPDATE agents SET intent_key=?, intent_vec=? WHERE id=?",
(key, pack(vec), agent_id))
def set_prompt_vector(self, agent_id: int, name: str, key: str, vec: np.ndarray) -> None:
self._write("UPDATE agent_prompts SET vec_key=?, vec=? WHERE agent_id=? AND name=?",
(key, pack(vec), agent_id, name))
# --------------------------------------------------------------- scores
@@ -1088,9 +1129,11 @@ class Store:
for r in rows]
def replace_scores(self, agent_id: int,
rows: Sequence[tuple[str, float, str]]) -> int:
rows: Sequence[tuple[str, float, str] | tuple[str, float, str, str]]
) -> int:
"""`(uid, score, gated_reason)` for every role this agent scored,
replacing whatever it had: a role closed or muted since the last run
with the name of the prompt that gave the fit as a fourth item ('' when
left out), replacing whatever it had: a role closed or muted since the last run
drops out rather than keeping a stale score. One transaction, so a
reader sees the old list or the new one. An agent deleted while its
run was scoring gets nothing written and 0 back: its rows are gone
@@ -1101,9 +1144,10 @@ class Store:
return 0
conn.execute("DELETE FROM scores WHERE agent_id=?", (agent_id,))
conn.executemany(
"""INSERT INTO scores (uid, agent_id, score, gated_reason, scored_at)
VALUES (?,?,?,?,?)""",
[(uid, agent_id, score, reason, stamp) for uid, score, reason in rows])
"""INSERT INTO scores (uid, agent_id, score, gated_reason, prompt, scored_at)
VALUES (?,?,?,?,?,?)""",
[(row[0], agent_id, row[1], row[2], row[3] if len(row) > 3 else "", stamp)
for row in rows])
return len(rows)
def scores(self, agent_id: int) -> dict[str, tuple[float, str]]:
+15 -10
View File
@@ -16,8 +16,8 @@ from pydantic import BaseModel, Field
from app import jobs
from app.models import (
AFFINITY, Agent, Application, Company, Gate, Limits, Role, RoleRow, Status,
Subscription, Weights, company_key,
AFFINITY, Agent, Application, Company, Gate, Limits, Prompts, Role, RoleRow,
ScoredRole, Status, Subscription, Weights, company_key,
)
from app.storage import Store
from app.web.deps import ConfigDep, StoreDep
@@ -60,9 +60,10 @@ class VoteBody(BaseModel):
@router.get("/roles/{uid}")
def get_role(uid: str, store: StoreDep, agent: int | None = None) -> Role:
"""One role, open or closed, for the read page, with `agent`'s vote (the
default agent's when absent; None when there is no agent)."""
def get_role(uid: str, store: StoreDep, agent: int | None = None) -> ScoredRole:
"""One role, open or closed, for the read page, with `agent`'s vote and
score, and the prompt that gave its fit (the default agent's when absent;
None when there is no agent)."""
role = store.role(uid, _agent_id(store, agent))
if role is None:
raise HTTPException(404, f"no such role: {uid}")
@@ -70,7 +71,7 @@ def get_role(uid: str, store: StoreDep, agent: int | None = None) -> Role:
@router.post("/roles/{uid}/vote")
def vote_role(uid: str, body: VoteBody, store: StoreDep) -> Role:
def vote_role(uid: str, body: VoteBody, store: StoreDep) -> ScoredRole:
"""1 like, 0 neither way, -1 pass, for `body.agent` or the default agent.
The role stays listed either way; the vote moves it in that agent's
ranking, see `Store.roles`, and shapes its taste at the next `score`."""
@@ -82,7 +83,7 @@ def vote_role(uid: str, body: VoteBody, store: StoreDep) -> Role:
@router.delete("/roles/{uid}/vote")
def unvote_role(uid: str, store: StoreDep, agent: int | None = None) -> Role:
def unvote_role(uid: str, store: StoreDep, agent: int | None = None) -> ScoredRole:
"""Unjudged again for `agent` or the default agent: what pressing the
stance a role already has does."""
owner = _agent_or_default(store, agent)
@@ -677,10 +678,13 @@ def list_agents(store: StoreDep) -> list[Agent]:
class AgentBody(BaseModel):
"""Create when `id` is missing, replace that agent's fields when given."""
"""Create when `id` is missing, replace that agent's fields when given.
`intent` is the body from before an agent had prompts: without `prompts`
it is stored as one prompt named after the agent."""
id: int | None = None
name: str = Field(min_length=1)
prompts: Prompts | None = None
intent: str = ""
gates: list[Gate] = []
weights: Weights = Weights()
@@ -694,7 +698,8 @@ def save_agent(body: AgentBody, store: StoreDep) -> Agent:
a save and waits on the job it answers with."""
try:
return store.save_agent(body.name, body.intent, body.gates, body.weights,
body.enabled, agent_id=body.id, limits=body.limits)
body.enabled, agent_id=body.id, limits=body.limits,
prompts=body.prompts)
except ValueError as exc:
raise HTTPException(409, str(exc)) from exc
except KeyError:
@@ -703,7 +708,7 @@ def save_agent(body: AgentBody, store: StoreDep) -> Agent:
@router.delete("/agents/{agent_id}", status_code=204)
def delete_agent(agent_id: int, store: StoreDep) -> Response:
"""Its scores, votes and affinities go with it."""
"""Its prompts, scores, votes and affinities go with it."""
if not store.delete_agent(agent_id):
raise HTTPException(404, f"no such agent: {agent_id}")
return Response(status_code=204)