Building an AI Text Detector From Scratch
打开互动全文版(逐段中英对照 + 图/公式 + 论文问答)→本文提供了一个从零构建简单 AI 文本检测器的实战教程,灵感来源于 Substack 的 AI 检测功能。作者的核心目标是通过开发一个返回 0-100 分(表示 AI 生成内容可能性)的分类器,来解释 AI 检测器的工作原理。项目涉及在人类与 AI 写作文本数据集上微调 DistilBERT 模型,并探索将检测器用作验证器,训练小型语言模型生成可规避检测的文本。文章涵盖了整个流程,包括数据准备、模型训练、评估以及通过 API 和用户友好界面进行本地部署。作者强调,AI 检测是一场猫鼠游戏,存在误报等局限性,需要持续更新。读者将获得构建基于验证器的 LLM 应用的实用见解,并理解 AI 文本检测中的权衡与挑战。
This article presents a hands-on tutorial for building a simple AI text detector from scratch, inspired by Substack's AI detection feature. The author's core goal is to explain how AI detectors work by developing a functional classifier that returns a 0-100 score indicating the likelihood of AI-generated content. The project involves fine-tuning a DistilBERT model on a dataset of human and AI-written texts, and it also explores using the detector as a verifier to train a small language model to produce text that avoids detection. The article covers the entire pipeline, including data preparation, model training, evaluation, and local deployment via an API and user-friendly UI. The author emphasizes that AI detection is a cat-and-mouse game, with limitations such as false positives and the need for continuous updates. Readers will gain practical insights into building a verifier-based LLM application and understand the trade-offs and challenges in AI text detection.
Substack 最近在其界面中推出了 AI 检测器功能,这非常有趣。
Substack recently launched its AI detector feature in the UI, which is super interesting.
另外,很多人问我关于本地 DIY 大语言模型项目的有趣示例,以展示小型语言模型(SLM)的能力。
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.
综合这两点,我认为展示如何实现一个 AI 检测器会很有趣。我还将把它用作验证器,训练一个小型语言模型生成避免被检测到的文本。这是一个小型教育项目,用于研究 AI 检测器的局限性,并探索基于验证器的 LLM 应用,超越常规的基于数学和代码训练推理模型。
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.
图 1:Substack 现在内置了 AI 检测器。
Figure 1: Substack now features a built-in AI detector.
因此,如上所述,本教程的目标是通过构建一个(简单的)AI 检测器来解释其工作原理。
So, as mentioned above, the intended goal of this tutorial is to explain how AI detectors work by building (a simple) one.
在实践中,这样的检测器可用于过滤垃圾内容,但也可能在不将你的写作变成 AI 生成文本的前提下,潜在地改进你的个人写作。例如,如果你写了一篇长文章并想改进拼写和语法,那么使用语法检查器来润色并提高可读性是很有吸引力的(而且实际上也很有用)。有各种服务可以做到这一点,包括像 ChatGPT 这样的通用大语言模型。然而,这也存在风险:这些工具可能会将你的写作——即使仍然是你自己的写作——变成过度润色、听起来像 AI 的内容,并被标记为垃圾内容。
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.
例如,使用 AI 检查器,人们可以说:“修复我的语法,同时确保我的文本仍然保持 0%的 AI 生成率。”
For example, with an AI checker, one could say, “Fix my grammar while ensuring that my text still scores 0% AI-generated.”
无论如何,虽然我们在这里构建一个功能完整的检查器,但目标是解释 1)AI 检查器如何(能够)工作,以及 2)将此作为更一般主题的案例研究,即如何构建可与 LLM 一起使用的评分器或验证器。
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.
免责声明:AI 检查器本质上是一场猫鼠游戏。AI 检查器可能学会检测某种指示 AI 生成内容的模式。然后,下一代 LLM 可能偶然或故意不表现出该模式,从而避免检测。AI 检查器随后必须更新以检测该 LLM,如此循环。此外,还可能会遇到误报(人类撰写的文本被标记为 AI 生成),但稍后会详细讨论。
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.
本项目有多个目标。总体目标当然是说明 AI 检测器的工作原理,并展示一个应用端到端的大语言模型项目,包括评估、训练和本地部署,以供实际使用。
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.
其成果是一个可供人类和智能体使用的 AI 检测器 API,以及一个用户友好的界面。
The outcome of this is an AI-detector API that can be used by humans and agents, and a user-friendly UI.
图 2:本项目后续开发的本地浏览器界面预览。它返回整篇文本的 AI 评分,并可以高亮显示各个文本块的评分。
Figure 2: Preview of the local browser interface developed later in this project. It returns a whole-text AI score and can also highlight the scores for individual text chunks.
在这里,我们将开发一种类似于 Pangram 模型的方法,据我所知,Substack 的 AI 检测功能背后就是这种模型。
Here, we are going to develop a method similar to Pangram models, which, as far as I know, are behind Substack AI detection feature.
我在 2023 年早些时候写过一篇关于 AI 文本检测的短文:《检测 ChatGPT 等 LLM 生成内容的不同方法有哪些?它们如何工作以及有何区别?》
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?
本质上,检测 AI 生成的文本有多种方式,从监督分类器和基于扰动的概率测试,到困惑度度量和水印技术。
In essence, there are different ways to detect AI-written text, from supervised classifiers and perturbation-based probability tests to perplexity measures and watermarking.
在本教程中,我们将构建一个返回 0-100 分的模型。它本质上是一个带有估计概率分数的分类器。该概率分数表示根据分类器,文本由 AI 生成的可能性有多大。(或者,准确地说,该分数是分类器基于其训练分布对 AI 生成类别的估计概率。然而,我们不应将其解释为文本由 AI 编写的通用概率。)
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 interpret it as a general probability that the text was written by AI.)
为此,我们将微调一个 DistilBERT 分类器(类似于我在早期 Substack 文章《微调大型语言模型》中描述的方法),但更多细节将在后续阶段介绍。
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), but more details on that later when we get to that stage.