LLM Apps in Production (RAG + Vector DB + Caching) · 课时

清洗源数据并去除重复内容

学习如何在摄取文档前清理噪声文档并删除重复内容,让 RAG 索引保持精简、准确,并避免出现相互冲突的答案。

第 4 / 4 课13 个步骤

清洗源数据并去除重复内容 是 CoddyKit 上的免费 LLM Apps in Production (RAG + Vector DB + Caching) 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 LLM Apps in Production (RAG + Vector DB + Caching) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Garbage In, Garbage Out

RAG quality is capped by the quality of what you ingest. Boilerplate, HTML tags, duplicate pages, and broken encoding all pollute retrieval.

Cleaning and deduplication happen before chunking and embedding.

Common Noise Sources

Typical junk found in raw documents:

  • Navigation menus, headers, footers
  • Cookie banners and ads
  • Repeated legal disclaimers
  • Mojibake from bad encoding
  • Excess whitespace and control chars

Basic Text Normalization

Normalize whitespace and strip control characters first.

import re

def clean(text):
    text = re.sub(r'[\t\r]+', ' ', text)
    text = re.sub(r' {2,}', ' ', text)
    text = re.sub(r'\n{3,}', '\n\n', text)
    return text.strip()

print(clean('Hello     world\n\n\n\nbye'))

Stripping Boilerplate

Remove repeated boilerplate that appears on many pages. A simple approach: collect lines that repeat across documents and drop them.

  • Footers, copyright lines
  • Share-this widgets
  • Identical navigation blocks

Fixing Encoding Issues

Mojibake like 'caf\u00c3\u00a9' instead of 'caf\u00e9' confuses embeddings. Detect the source encoding and decode consistently to UTF-8 before storage.

Exact Duplicate Detection

The cheapest dedup: hash the normalized text and drop exact repeats.

import hashlib

seen = set()

def is_dup(text):
    h = hashlib.sha256(text.encode()).hexdigest()
    if h in seen:
        return True
    seen.add(h)
    return False

print(is_dup('a'))
print(is_dup('a'))

Near-Duplicate Detection

Exact hashing misses pages that differ by a date or a word. Use near-duplicate techniques:

  • MinHash + Jaccard similarity
  • SimHash fingerprints
  • Embedding cosine similarity above a threshold

Jaccard Similarity

A quick token-set overlap score to flag near-duplicates.

def jaccard(a, b):
    sa, sb = set(a.split()), set(b.split())
    return len(sa & sb) / len(sa | sb)

print(round(jaccard('the cat sat', 'the cat ran'), 2))

Why Duplicates Hurt RAG

Duplicate chunks waste index space and skew retrieval: the top-k results fill up with copies of the same passage, crowding out diverse evidence. Conflicting near-duplicates (old vs new policy) can even produce contradictory answers.

Building a Cleaning Pipeline

Chain the steps in order: normalize -> fix encoding -> strip boilerplate -> exact dedup -> near dedup. Log how much was removed so you can audit aggressive filters.

Idempotent Re-ingestion

When documents are re-ingested, use a stable content hash as the record key so updates replace the old version instead of creating duplicates. This keeps the index clean over time.

Quick Check

Test your understanding of deduplication.

Recap

You learned to prepare clean source data: normalize text, fix encoding, strip boilerplate, then remove both exact and near-duplicates. Use stable content hashes for idempotent re-ingestion. Cleaner inputs mean a smaller index and more accurate, non-contradictory retrieval.

免费开始

用 AI 导师学习 LLM Apps in Production (RAG + Vector DB + Caching) — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「清洗源数据并去除重复内容」课时是免费的吗?

是的 — 「清洗源数据并去除重复内容」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 LLM Apps in Production (RAG + Vector DB + Caching) 课程的其余内容,请升级到 CoddyKit PRO。 LLM Apps in Production (RAG + Vector DB + Caching) 课程共包含 4 节课。

「清洗源数据并去除重复内容」这节课中我会学到什么?

学习如何在摄取文档前清理噪声文档并删除重复内容,让 RAG 索引保持精简、准确,并避免出现相互冲突的答案。 你通过在浏览器中直接运行的动手代码来练习 LLM Apps in Production (RAG + Vector DB + Caching),全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 LLM Apps in Production (RAG + Vector DB + Caching) 需要有经验吗?

无需任何先前经验。CoddyKit 上的 LLM Apps in Production (RAG + Vector DB + Caching) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「清洗源数据并去除重复内容」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 LLM Apps in Production (RAG + Vector DB + Caching) 课中编写并运行代码吗?

能。每节 LLM Apps in Production (RAG + Vector DB + Caching) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 加载多种文档格式
  2. 上下文感知的分块策略
  3. 元数据管理与筛选
  4. 清洗源数据并去除重复内容
← 返回 LLM Apps in Production (RAG + Vector DB + Caching)