feat(summarizer): score articles in the map phase, brief only the important ones

The map prompt now grades every article 0-100 against an explicit rubric as
the first line of its block; the summarizer parses Score:, falls back to the
Importance: label when the line is missing, ranks blocks best-first and passes
only score >= SUMMARY_MIN_SCORE (capped at SUMMARY_TOP_N) to the reduce phase.

- new pure helpers in intel.py: parse_map_scores(), select_graded(), join_blocks()
- reduce prompts drop the 'fill the brief with minor incidents and crime'
  filler instructions and the 'fill ticker with medium then low' clause
- new article_scores table (ensure_tables + alembic 012) persists every grade
  with selected/summary_id/model so thresholds can be tuned from data
- SUMMARY_MIN_SCORE (60), SUMMARY_TOP_N (12), SUMMARY_FALLBACK_TOP (3) env knobs
This commit is contained in:
hermes-backend 2026-11-13 21:47:30 -05:00
commit 853e87b7c5
8 changed files with 775 additions and 37 deletions

View file

@ -60,8 +60,29 @@ if `article_summaries` already has a row in the last interval, the summarizer
The `articles` and `article_summaries` tables are created by the idempotent
alembic migration `003_news` (also created by the scraper's own
`CREATE TABLE IF NOT EXISTS`). `news_items` is alembic `005_news_items`.
Container startup order doesn't matter.
`CREATE TABLE IF NOT EXISTS`). `news_items` is alembic `005_news_items` and
`article_scores` is `012_article_scores` (also created by the summarizer's own
`ensure_tables()`). Container startup order doesn't matter.
## Grading and selection
The map phase grades every article 0-100 against the rubric in the map prompt
(90-100 breaking geopolitical/military/security or major disaster, 70-89
significant national/international news, 40-69 notable regional/economic/tech/
health, 10-39 routine crime and local interest, 0-9 noise: sport, celebrity,
entertainment, market tape, promotional and event-calendar listings). The
summarizer parses the first `Score:` line in each `Article <n>:` block, clamps
it to 0-100, and falls back to the `Importance:` label (critical 95, high 78,
medium 50, low 22, none 5) when the line is missing or unparsable — a block is
never dropped and the fallback count is logged per batch. Only blocks scoring
`>= SUMMARY_MIN_SCORE` (default 60) reach the reduce prompt, best first, capped
at `SUMMARY_TOP_N` (default 12). If nothing clears the bar the top
`SUMMARY_FALLBACK_TOP` blocks (default 3) are used so the HUD is not blank;
`SUMMARY_FALLBACK_TOP=0` passes an empty DATA section instead. The reduce prompt
no longer asks for filler: it leads with the highest-scoring stories and never
promotes an item above its score. Every graded article is persisted to
`article_scores` (score, category, selected, model, summary_id) so the operator
can tune the thresholds against real data.
## Keys and Settings
@ -240,6 +261,9 @@ map. `summary_en` lands in `article_summaries.summary_text`.
| `NOUS_BASE_URL` | `https://inference-api.nousresearch.com/v1` | Read-only in Settings. |
| `SUMMARY_MODEL` | `Hermes-4.3-36B` | Compose default. Operator-facing choice is Settings → `app_settings.SUMMARY_MODEL`. |
| `NEWS_BATCH_SIZE` | `50` | Articles per map-phase batch (compose maps to container `BATCH_SIZE`). |
| `SUMMARY_MIN_SCORE` | `60` | Map-phase score a block must reach to enter the reduce brief. |
| `SUMMARY_TOP_N` | `12` | Max blocks passed to the reduce phase (best score first). |
| `SUMMARY_FALLBACK_TOP` | `3` | Blocks used when nothing clears `SUMMARY_MIN_SCORE`; `0` = empty DATA. |
| `SUMMARY_WINDOW_MINUTES` | `15` | How far back the summarizer looks for new articles. |
| `NEWS_SCRAPE_INTERVAL_S` | `10` | Pause after each crawl before the next (scraper is otherwise continuous). |
| `NEWS_SUMMARIZE_INTERVAL_S` | `900` | Seconds between analyst runs (default 15 min). |