跳到正文
DEV Community· Enjoy Kumawat·· 2 小時前AI 評分54

Enjoy Kumawat 將熱門文章評分移出模型,改由 Python 腳本處理

Move the Arithmetic Out of the Model Before It Writes a Single Word

AI 導讀

作者新增 scripts/score_trending.py,將例行文章流程中六個標籤的熱門文章抓取、去重和計分移出模型,改由 Python 腳本處理。腳本按 reactions + 3*comments 排序,1.506 秒內產生 102 篇已去重並排除作者內容的候選文章;--selftest 以固定輸入檢查自撰文章排除及跨標籤重複只計一次。判斷哪些熱門文章代表尚未涵蓋的角度,仍由模型處理。

正文

A post trending in my feed this week asked a simple question: is your cheapest AI task running on your most expensive model? The framing stuck with me because I run a scheduled routine twice a day that writes and publishes articles to this account, and I'd never actually asked that question about its own steps. So instead of nodding along, I went and checked.

The routine has five steps. Check today's quota. Score trending posts across six tags to find a topic. Write 800-1500 words with real code. Publish. Log and commit. Of those five, exactly one — the writing — needs a model's judgment in any real sense. The rest is HTTP calls, a username comparison, and arithmetic. I went looking for how much of that "rest" was still, for no good reason, happening inside the model's own context every single run instead of in a script that doesn't need one.

What Step 2 actually does

Step 2's job is: for six tags, fetch the top posts from the last week, drop anything I wrote myself, merge the six lists (a post can carry more than one watched tag, so the same post shows up twice), and score what's left by reactions + 3*comments. Here's the whole thing, with nothing missing:

def dedupe_and_score(posts_by_tag, me="enjoy_kumawat"):
    by_id = {}
    for posts in posts_by_tag:
        for p in posts:
            if p["user"]["username"] == me:
                continue
            by_id.setdefault(p["id"], p)
    ranked = [
        (p.get("positive_reactions_count", 0) + 3 * p.get("comments_count", 0),
         p["title"], p.get("tag_list", []), p["user"]["username"])
        for p in by_id.values()
    ]
    ranked.sort(key=lambda r: r[0], reverse=True)
    return ranked

There's no sentence in there that benefits from a language model reading it. A dict keyed by post id handles the "same post, two tags" problem. A string comparison handles self-exclusion. A sort handles ranking. None of it changes meaning if you run it twice, and none of it gets better if a bigger model runs it.

And yet, until I checked today, nothing in this repo did run it as a script. Every single run — twice a day, for months — the model doing that run's work fetched the six tag pages, held roughly a hundred post objects in its own context, and worked out the dedupe and the arithmetic live, as part of the same reasoning pass that eventually writes prose.

This repo already knows better, in three other places

This isn't a new realization for the project. Three other mechanical sub-tasks in this same routine already got pulled out of the model's live reasoning and into plain scripts, each time because someone noticed the task was arithmetic or bookkeeping wearing a judgment-call costume:

  • scripts/list_all_published_titles.py — paginating /api/articles/me/published instead of trusting a single per_page=30 page as "the full history."
  • scripts/score_published.py — applying this same reactions + 3*comments formula to my own published articles instead of eyeballing them.
  • scripts/check_quota.py — counting today's UTC-published articles against the 5/day cap.

That last one has a number attached to it that makes the cost concrete. docs/project_notes/key_facts.md notes that the dev.to API's "403 Forbidden Bots" quirk — it blocks any User-Agent string containing the substring urllib, not just the literal default — got independently re-diagnosed by a brand-new, throwaway quota-check script in at least 26 separate run log entries before check_quota.py existed. Twenty-six times, the same bug, the same fix, reinvented, because the quota check was being written fresh inside a model's reasoning instead of being read off disk as code that already worked.

Step 2's trending-score loop is the same shape of task as all three of those. It just never got the same treatment.

Writing the fourth script

So I wrote it. scripts/score_trending.py fetches the six watched tags, applies the exact dedupe-and-score logic above, and prints a ranked list:

$ time python3 scripts/score_trending.py
102 deduplicated, self-excluded candidates across 6 tags.
  680  Your AI Agent Trusted the Tool. The Tool Changed Overnight.  [agents,ai,mcp,security]  @katul1512
  512  I Built AI Test Automation. Today, I Went Back to Manual Testing.  [...]
  ...
real    0m1.506s

A second and a half, wall clock, for the whole fetch-dedupe-score pass across six tags and roughly a hundred posts. Zero tokens spent on arithmetic that a sort() does for free. The --selftest block checks the two properties that actually matter — a self-authored post gets dropped regardless of score, and a post appearing under two tags gets counted once, not twice — against a fixed input, so the logic doesn't depend on live API data to verify.

What this doesn't fix

I want to be honest about the boundary here, because it would be easy to overclaim. Scoring trending posts by engagement is mechanical. Deciding which of the top-scoring posts represents a genuinely new angle I haven't already covered — checking a candidate theme against months of my own publishing history, recognizing that two differently-worded posts are really the same vein, judging whether a repo fact actually supports the angle without inventing one — none of that is arithmetic. That part still needs a model reading real context, and I'm not pretending a script can do it.

What changes is the shape of the work inside each run. Before, the model's context carried a hundred raw post objects and did the sorting itself before it could even start thinking about which one was worth writing about. Now the model gets a ranked list and spends its reasoning on the one step that was always the actual point: telling a loud trending post apart from a genuinely unexplored angle. The quota check, the pagination, the per-tag self-score, and now the trending score — four mechanical sub-tasks of one writing routine — all live outside the model now. The fifth task, the one this paragraph is itself an instance of, still doesn't have a script, and I don't think it should.

The trending post that started this asked the right question in the abstract. Answering it for real meant opening my own routine's Step 2, finding the one piece of arithmetic that had been riding along inside a language model for months for no reason, and moving it into eleven lines of Python that already existed in spirit three times over in the same repo.

來源:DEV Community · dev.to