从零构建一个AI文本检测器:涵盖数据集构建、模型训练、本地部署与RLVR
文章背景与核心概要
随着Substack等平台在其用户界面中集成AI检测功能,区分人类编写和机器生成的内容变得日益重要。本文由知名AI专家Sebastian Raschka撰写,深入探讨了如何利用小型语言模型(SLMs)从头构建一个功能完整、可本地运行的AI文本检测器。
文章不仅将AI检测视为一个静态的实用工具,更通过端到端的实战演练展示了数据集构建、模型微调、本地API与用户界面部署的全过程。此外,项目还探索了如何利用训练好的检测器作为验证器(Verifier),引导大语言模型绕过检测。这不仅为开发者提供了构建定制化文本分类器的实用指南,也深入剖析了AI检测机制背后的技术原理与局限性。
執行摘要 (Executive Summary)
Substack recently integrated an AI detector into its user interface, highlighting the growing need to distinguish between human-written and machine-generated content. This project explores how to build a fully functional, local AI text detector from scratch using Small Language Models (SLMs).
Substack 最近在其用户界面中集成了一个 AI 检测器,这凸显了区分人类编写内容与机器生成内容日益增长的需求。本项目探讨了如何使用小型语言模型(SLMs)从头构建一个功能齐全的本地 AI 文本检测器。
Rather than treating AI detection as a static utility, this end-to-end tutorial demonstrates how to construct datasets, train models, deploy local APIs and user interfaces, and use the resulting detector as a verifier to guide LLMs in bypassing detection—offering a deep dive into the limitations and mechanics of AI detectors.
本端到端教程并没有将 AI 检测视为一个静态的实用工具,而是演示了如何构建数据集、训练模型、部署本地 API 和用户界面,并将生成的检测器用作验证器来引导大语言模型(LLM)绕过检测——从而深入探讨了 AI 检测器的局限性和工作机制。
引言 (Introduction)
Many readers have expressed interest in local, do-it-yourself LLM projects that showcase the capabilities of small language models (SLMs). Combining this interest with Substack's new AI detection features, this tutorial implements a local AI detector as an educational case study.
许多读者对展示小型语言模型(SLMs)功能的本地化、动手型 LLM 项目表现出浓厚的兴趣。结合这一需求以及 Substack 的新 AI 检测功能,本教程将实现一个本地 AI 检测器,作为一个极具教育意义的案例研究。
+--------------------+ +--------------------+ +--------------------+
| Dataset | ---> | Fine-tune | ---> | Local Deployment |
| Construction | | DistilBERT | | (API & UI) |
+--------------------+ +--------------------+ +--------------------+
|
v
+--------------------+
| RLVR Verification |
| & Text Optimization|
+--------------------+
为什么要构建 AI 检测器? (Why Build an AI Detector?)
- Filtering Spam: Automatically identifying low-effort, mass-produced text.
- Polishing Writing Safely: Using general-purpose LLMs (like ChatGPT) to check spelling and grammar without unintentionally transforming your authentic voice into over-polished, easily flagged "AI-speak." A custom detector lets you verify edits against specific thresholds (e.g., "Fix my grammar while ensuring the text scores 0% AI-generated").
- Understanding Verifiers: Exploring how to build scorers and verifiers to guide LLMs beyond traditional math and coding tasks.
- 过滤垃圾信息: 自动识别低质量、批量生产的文本。
- 安全地润色写作: 使用通用 LLM(如 ChatGPT)来检查拼写和语法,而不会无意中将你真实的个人风格转变成过度润色、容易被标记的“AI腔”。定制检测器允许你根据特定阈值验证修改(例如:“修复我的语法,同时确保文本的 AI 生成评分为 0%”)。
- 理解验证器: 探索如何构建评分器和验证器,以指导 LLM 超越传统的数学和编码任务。
Disclaimer: AI detection is an ongoing cat-and-mouse game. Detectors learn to spot specific statistical patterns characteristic of current AI models. As subsequent LLMs naturally or deliberately avoid those patterns, detectors must evolve. Furthermore, false positives (human text flagged as AI-generated) remain a persistent challenge.
免责声明: AI 检测是一场持续的猫鼠游戏。检测器通过学习来识别当前 AI 模型特有的统计模式。随着后续的 LLM 自然地或刻意地避开这些模式,检测器也必须不断进化。此外,误报(人类文本被标记为 AI 生成)仍然是一个长期存在的挑战。
项目目标 (Project Goals)
The overarching goal is to demystify how AI detectors work while delivering a complete, production-ready system consisting of: 1. A trained classification model. 2. A local AI-detector API accessible to both humans and agents. 3. A user-friendly browser interface.
总体目标是揭开 AI 检测器工作原理的神秘面纱,同时交付一个包含以下内容的完整、生产就绪的系统: 1. 一个经过训练的分类模型。 2. 一个可供人类和智能体(Agents)访问的本地 AI 检测器 API。 3. 一个用户友好的浏览器界面。
方法概述 (Method Overview)
The methodology mirrors modern approaches like Pangram models (believed to power Substack's AI detection feature).
该方法与 Pangram 模型等现代方法相似(据信 Pangram 模型为 Substack 的 AI 检测功能提供了底层支持)。
In general, AI text detection encompasses several distinct approaches: * Supervised classifiers * Perturbation-based probability tests * Perplexity measures * Watermarking
总的来说,AI 文本检测包含几种不同的方法: * 监督分类器(Supervised classifiers) * 基于扰动的概率测试(Perturbation-based probability tests) * 困惑度度量(Perplexity measures) * 水印技术(Watermarking)
For a broader foundational overview, refer to the previous article: What Are the Different Approaches for Detecting Content Generated by LLMs Such As ChatGPT? And How Do They Work and Differ?.
有关更广泛的基础概述,请参考之前的文章:检测由 ChatGPT 等 LLM 生成的内容有哪些不同方法?它们是如何工作的,有何区别?。
我们的实现策略 (Our Implementation Strategy)
This project builds a model that outputs a score from 0 to 100, acting essentially as a classifier with an estimated probability score. * The score represents how likely a text is to be AI-generated according to the classifier's training distribution (though it should not be interpreted as an absolute ground-truth probability). * The core architecture relies on fine-tuning a DistilBERT classifier, a technique detailed further in Finetuning Large Language Models.
本项目构建了一个输出 0 到 100 之间分数的模型,它本质上是一个带有估计概率得分的分类器。 * 该分数表示根据分类器的训练分布,文本由 AI 生成的可能性(尽管不应将其解释为绝对的真实概率)。 * 核心架构依赖于微调 DistilBERT 分类器,该技术在微调大语言模型一文中进行了更详细的介绍。
Note: The complete code, dataset construction scripts, local deployment instructions, and RLVR pipeline are available to paid subscribers in the full version of this post.
注:完整代码、数据集构建脚本、本地部署说明以及 RLVR 流水线可在付费订阅者的完整版文章中获取。