Article summary — original

Building an AI Text Detector and Verifier From Scratch Prompted by Substack's AI detection feature and interest in local small language model projects, this guide outlines how to build an AI detector using a fine-tuned DistilBERT classifier to explore both detection and evasion. The tutorial aims to explain AI detection by building a detector and using it as a verifier to train a small language model that avoids detection. AI detectors can filter spam or help writers polish grammar without their text being overpolished and flagged as AI-generated. Detection remains an ongoing cat-and-mouse game prone to false positives as models adapt to circumvent learned patterns. The project implements a method similar to Pangram models, fine-tuning a DistilBERT classifier to provide a 0–100 probability score alongside an API and UI. Project Motivation and Applications The project combines interest in Substack's AI detector feature with requests for local small language model demonstrations. The goal is to build an AI detector to understand how detection works and use it as a verifier for models beyond standard math and coding reasoning tasks. In practical terms, such tools can filter spam or ensure that grammar-polishing LLMs do not overpolish human writing into text that triggers AI detection filters. Limitations and Technical Design AI detection operates as a cat-and-mouse game where models may intentionally or accidentally bypass patterns learned by detectors, leading to ongoing updates and false positives on human-written work. While detection strategies include watermarking, perplexity measures, and perturbation tests, this tutorial fine-tunes a DistilBERT classifier similar to Pangram models. The resulting tool returns an estimated probability score from 0 to 100 based on its training distribution and includes both an API and a user interface.

Article summary — zh

从零构建AI文本检测器:原理、应用与实现路径 受Substack上线AI检测功能及本地小语言模型项目需求的启发,作者开展了一个从零构建AI文本检测器的教程项目,旨在展示其工作原理,并将其作为验证器探索小语言模型的应用与局限。 项目旨在解释AI检测器原理,并将其作为验证器训练小语言模型生成可规避检测的文本。 AI检测不仅可用于过滤垃圾内容,还能用于辅助润色以避免人类文本过度修改而呈现AI味。 AI检测本质上是“猫鼠游戏”,存在易过时和误报人类文本为AI内容的局限。 技术方案采用微调DistilBERT分类器输出0至100的分数,并提供API和本地UI界面。 项目动机与应用场景 Substack近期在UI中推出了AI检测功能,同时许多读者对本地小语言模型(SLM)的实际应用案例表现出浓厚兴趣。作者结合这两点发起该教程项目,通过端到端构建检测器展示其运作机制。除用于过滤垃圾内容外,AI检测器还可辅助文本润色,防止如ChatGPT等工具将人类撰写的文章过度润色从而被误判为AI生成。 猫鼠游戏与技术局限 作者指出AI检测本质上是一场猫鼠游戏。检测器学习到AI文本的特定模式后,后续LLM可能会偶然或故意规避该模式,迫使检测器不断更新。此外,检测机制还容易出现误报,将人类撰写的文本错误标记为AI生成。 技术实现与架构规划 AI检测方法涵盖监督分类器、扰动概率测试、困惑度评估及水印等多种途径。本项目参考Substack背后的Pangram模型机制,选择微调DistilBERT分类器来输出0至100的概率分数,代表基于训练分布下属于AI生成的估计概率,最终将交付包含本地UI界面及供人类或智能体调用的API接口。

Saved original article

Substack recently launched its AI detector feature in the UI, which is super interesting. Separately, lots of people asked me about interesting local do-it-yourself LLM projects as demos to show what small language models (SLMs) are capable of. Putting one and one together, I thought it would be interesting to show how an AI detector can be implemented. I will also use it as a verifier to train a small language model to produce text that avoids detection. This is a small educational project for studying the limitations of AI detectors and exploring a verifier-based LLM application beyond regular reasoning models trained on math and code. So, as mentioned above, the intended goal of this tutorial is to explain how AI detectors work by building (a simple) one. In practice, such a detector can be used to filter out spammy content, but also to potentially improve your personal writing without turning it into AI-generated text. For example, if you wrote a lengthy article and want to improve spelling and grammar, it is tempting (and actually useful) to use a grammar checker to polish it and improve readability. There are different services for that, including general-purpose LLMs like ChatGPT. However, this also runs the risk that these tools turn your writing, even though it’s still your own writing, into something that is then overpolished and now sounds like AI and gets flagged as spammy content. For example, with an AI checker, one could say, “Fix my grammar while ensuring that my text still scores 0% AI-generated.” Anyway, while we are building a fully functional checker here, the goal is to explain 1) how AI checkers (can) work and 2) use this as a case study for a more general topic on how to build a scorer or verifier that can be used with LLMs. Disclaimer: AI checkers are essentially a cat-and-mouse game. AI checkers may learn to detect a certain pattern that is indicative of AI-generated content. Then, the next LLM may incidentally or deliberately not exhibit that pattern and avoid detection. The AI checker then has to be updated to detect said LLM, and so forth. Plus, it’s also likely to encounter false positives (human written text flagged as AI-generated), but more on that later. There are several goals of this project. The overarching goal is, of course, to illustrate how AI detectors work and show an applied end-to-end LLM project including evaluation, training, and local deployment for real-world use. The outcome of this is an AI-detector API that can be used by humans and agents, and a user-friendly UI. Here, we are going to develop a method similar to Pangram models, which, as far as I know, are behind Substack AI detection feature. I wrote a short article about AI-text detection a while back in 2023: [What Are the Different Approaches for Detecting Content Generated by LLMs Such As ChatGPT? And How Do They Work and Differ?](https://sebastianraschka.com/blog/2023/detect-ai.html) In essence, there are different ways to detect AI-written text, from supervised classifiers and perturbation-based probability tests to perplexity measures and watermarking. In this tutorial, we will build a model that returns a 0-100 score. It’s essentially a classifier with an estimated probability score. The probability score will denote how likely a text is AI-generated according to the classifier. (Or, to be precise the score is the classifier’s estimated probability for the AI-generated class based on its training distribution. However, we shouldn’t interpreted it as a general probability that the text was written by AI.) For this, we are going to fine-tune a DistilBERT classifier (similar to what I described in one of my early Substack articles, [Finetuning Large Language Models](https://magazine.sebastianraschka.com/p/finetuning-large-language-models)), but more details on that later when we get to that stage.