RAG · Lesson 97 of 120
Duplicates and Old Versions
One truth.
Real document stores are messy. The same text is copied into many pages, and old versions are never deleted. Duplicates waste space in the prompt, and old versions give wrong answers.

- Copies everywhere
- Old versions linger
- Wrong answers
Finding near duplicates
We embed every chunk, and compare every pair. Pairs above zero point eight five are nearly the same text. Run it. The near duplicates come from the two price lists, the old one and the current one.
duplicates.pypython
from rag_kit import load_docs, make_chunks, embed
chunks = make_chunks(load_docs())
vectors = embed([c["text"] for c in chunks])
similar = vectors @ vectors.T
for i in range(len(chunks)):
for j in range(i + 1, len(chunks)):
if similar[i, j] > 0.85:
print(f"{similar[i, j]:.2f}", chunks[i]["source"], "<->", chunks[j]["source"])Output
0.94 price_list_2025.md <-> price_list_2026.md
Also uses rag_kit.py
import warnings; warnings.filterwarnings("ignore")
from pathlib import Path
import numpy as np
from sentence_transformers import SentenceTransformer
import faiss # after torch, so the two libraries load safely together
faiss.omp_set_num_threads(1)
from langchain_text_splitters import RecursiveCharacterTextSplitter
def load_docs(folder="data/rag/northwind"):
return [{"source": p.name, "text": p.read_text()} for p in sorted(Path(folder).glob("*.md"))]
def make_chunks(docs, size=300, overlap=50):
splitter = RecursiveCharacterTextSplitter(chunk_size=size, chunk_overlap=overlap)
return [{"source": d["source"], "text": t.replace("\n", " ")}
for d in docs for t in splitter.split_text(d["text"])]
embedder = SentenceTransformer("all-MiniLM-L6-v2", device="cpu")
def embed(texts):
return embedder.encode(texts, normalize_embeddings=True)
class Index:
def __init__(self, chunks):
self.chunks = chunks
self.vectors = embed([c["text"] for c in chunks])
def search(self, query, k=3):
scores = self.vectors @ embed([query])[0]
top = np.argsort(-scores)[:k]
return [(round(float(scores[i]), 2), self.chunks[i]) for i in top]One source of truth
Remove exact duplicates when indexing. Mark old versions as archived, and filter them out, or delete them. Add a date to every document. And when two pages disagree, prefer the newest.
| key | value |
|---|---|
| Duplicates | remove when indexing |
| Old versions | archive or filter |
| Dates | on every document |
| Conflicts | newest wins |
Summary
Find near duplicates by similarity, and keep one current version of each fact. Next, access control.
Key takeaways- Compare vectors
- Remove copies
- Newest wins
Next up: Access Control — who can see what