从零构建AI文本检测器:教程与验证器应用
Building an AI Text Detector From Scratch
Substack recently launched its AI detector feature in the UI, which is super interesting.
Substack 最近在其界面中推出了 AI 检测器功能,这非常有趣。
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.
另外,很多人问我关于有趣的本地 DIY 大语言模型项目,作为展示小型语言模型(SLM)能力的演示。
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.
综合这些,我认为展示如何实现一个 AI 检测器会很有趣。我还将用它作为验证器来训练一个小型语言模型,使其生成的文本能够避免被检测。这是一个小型教育项目,用于研究 AI 检测器的局限性,并探索基于验证器的 LLM 应用,超越常规的针对数学和代码训练推理模型。
Figure 1: Substack now features a built-in AI detector.
图 1:Substack 现在内置了 AI 检测器。
So, as mentioned above, the intended goal of this tutorial is to explain how AI detectors work by building (a simple) one.
如上所述,本教程的目标是通过构建一个(简单的)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 生成文本的情况下改进你的个人写作。例如,如果你写了一篇长文章,想要改进拼写和语法,使用语法检查器来润色并提高可读性是很诱人的(而且实际上很有用)。有不同的服务可以实现这一点,包括像 ChatGPT 这样的通用 LLM。然而,这也存在风险,即这些工具可能会将你的写作(即使仍然是你自己的写作)变成过度润色、听起来像 AI 的内容,并被标记为垃圾内容。
For example, with an AI checker, one could say, “Fix my grammar while ensuring that my text still scores 0% AI-generated.”
例如,使用 AI 检查器,人们可以说:“修复我的语法,同时确保我的文本仍然保持 0% 的 AI 生成率。”
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.
无论如何,虽然我们在这里构建一个功能齐全的检查器,但目标是解释 1) AI 检查器(可以)如何工作,以及 2) 将其作为更广泛主题的案例研究,即如何构建一个可与 LLM 一起使用的评分器或验证器。
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 检测器本质上是一场猫鼠游戏。AI 检测器可能学会检测某种表明 AI 生成内容的模式。然后,下一代 LLM 可能偶然或故意不表现出该模式,从而避免检测。然后 AI 检测器必须更新以检测该 LLM,依此类推。此外,它还可能遇到误报(人类编写的文本被标记为 AI 生成),但稍后会详细说明。
Project goals
项目目标
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检测器如何工作,并展示一个端到端的LLM应用项目,包括评估、训练和本地部署以供实际使用。
The outcome of this is an AI-detector API that can be used by humans and agents, and a user-friendly UI.
其成果是一个可供人类和代理使用的AI检测API,以及一个用户友好的界面。
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.
图2:本项目稍后开发的本地浏览器界面预览。它返回整个文本的AI评分,并可以高亮显示各个文本块的评分。
Method overview
方法概述
Here, we are going to develop a method similar to Pangram models, which, as far as I know, are behind Substack AI detection feature.
在这里,我们将开发一种类似于Pangram模型的方法,据我所知,Pangram模型是Substack AI检测功能背后的技术。
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?
我在2023年早些时候写过一篇关于AI文本检测的短文:检测由ChatGPT等LLM生成的内容有哪些不同方法?它们如何工作,有何区别?
In essence, there are different ways to detect AI-written text, from supervised classifiers and perturbation-based probability tests to perplexity measures and watermarking.
本质上,检测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 interpreted it as a general probability that the text was written by AI.)
在本教程中,我们将构建一个返回0-100分的模型。它本质上是一个带有估计概率分数的分类器。概率分数将表示根据分类器,文本由AI生成的可能性有多大。(或者,准确地说,该分数是分类器基于其训练分布对AI生成类别的估计概率。然而,我们不应将其解释为文本由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), but more details on that later when we get to that stage.
为此,我们将微调一个DistilBERT分类器(类似于我在早期Substack文章《微调大型语言模型》中描述的方法),但更多细节将在我们到达该阶段时再讨论。
Read more
阅读更多
更进一步:量化金融体系
看懂新闻只是起点——沿量化金融路径,把它变成能交付的工程能力