Distill RAG

A lightweight pipeline for extraction, chunking, embeddings, and search.


🧠 What Is This? (Plain-Language Overview)

distill_rag is a small but powerful toolkit that helps you transform messy text sources (HTML pages, transcripts, articles, archives) into clean, structured data that AI models can learn from.

It’s designed for people who want to build:

If you’ve ever tried to fine-tune a model and realised the hardest part is actually preparing the dataset β€” this toolkit is for that problem.


🌍 Why This Exists

Training or distilling a specialised AI model requires clean, coherent, well-structured data. But most text online is:

Before you can train a model, you need a pipeline that turns raw text into polished, training-ready data.

distill_rag gives you that pipeline. It helps you:

  1. pull content from raw HTML
  2. clean and structure it
  3. break it into long coherent chunks
  4. embed it locally
  5. index it with Elasticsearch
  6. perform high-quality semantic search

This structure mirrors the data format most distillation workflows expect.

Goal: Make it easy for researchers and builders to create high-quality domain-specific AI models.


⚑ Key Benefits: Speed, Simplicity, and Efficiency

Built entirely in Node.js, distill_rag leverages async promises and lightweight concurrency to deliver blazing-fast performanceβ€”often 5–10Γ— faster than equivalent Python-based tools like LlamaIndex, LangChain, or Haystack. This makes it ideal for local workflows on consumer hardware, where you can process thousands of chunks in minutes without heavy dependencies or complex setups.

If speed and developer joy matter in your RAG/distillation pipeline, this toolkit shines.

πŸ”§ How It Works (At a Glance)

1. Extract & Clean

Feed it a folder of HTML (scraped, archived, downloaded). It removes noise and produces structured JSON sessions.

2. Chunk & Embed

Text is broken into long, context-rich chunks (ideal for distillation). Each chunk is embedded using a local model like mxbai-embed-large.

3. Index & Search

Chunks are stored in Elasticsearch as vectors + metadata. You can then run semantic search to retrieve relevant material.


πŸš€ Who Should Use This?


πŸš€ Features


🧱 Project Structure

distill_rag/
β”œβ”€β”€ data_extraction/
β”‚   β”œβ”€β”€ clean_html.js              # strip noise safely
β”‚   β”œβ”€β”€ extractor.js               # extract Q/A style turns
β”‚   β”œβ”€β”€ convert_raw_to_sessions.js # HTML β†’ structured JSON
β”‚   └── walk_and_extract.js        # CLI to batch-convert directories
β”‚
β”œβ”€β”€ indexing/
β”‚   β”œβ”€β”€ index_distill_chunks.js    # long-chunk indexer
β”‚   └── rebuild_distill_index.sh   # wipe + rebuild helper
β”‚
β”œβ”€β”€ search/
β”‚   β”œβ”€β”€ search_distill_chunks.js   # BM25 / vector / hybrid search
β”‚   └── search_cli.js              # CLI search tool
β”‚
β”œβ”€β”€ tests/                         # jest-based automated test suite
β”‚
β”œβ”€β”€ prompts/                       # optional prompt templates
β”œβ”€β”€ shared/                        # shared utilities
β”œβ”€β”€ cleanup.sh                     # remove build artefacts
β”œβ”€β”€ jest.config.js
β”œβ”€β”€ package.json
└── README.md

πŸ“¦ Installation

Requirements:

Install:

npm install

🧼 1. Extracting Data from Raw HTML

Convert a directory:

node data_extraction/walk_and_extract.js raw_html/ extracted_sessions/

Output example:

{
  "title": "example.html",
  "turns": [
    { "role": "user", "content": "Q: What is service?" },
    { "role": "assistant", "content": "Service begins with kindness." }
  ]
}

Behind the scenes it:


🧱 2. Chunking + Indexing into Elasticsearch

Rebuild the full index:

bash rebuild_distill_index.sh

Manual index build:

ES_DISTILL_INDEX=quo_distill_index \
JSON_DIR=./extracted_sessions \
ELASTICSEARCH_NODE=http://localhost:9200 \
node indexing/index_distill_chunks.js

The indexer:


πŸ”Ž Advanced Retrieval Modes

distill_rag supports three complementary search strategies via search/search_distill_chunks.js:

1. BM25 (Keyword Search)

Classic lexical search. Good for names, citations, exact phrases.

const { searchBM25 } = require("./search/search_distill_chunks");
const results = await searchBM25("service to others", 5);
console.log(results);

2. Vector Search (Dense Embeddings)

Semantic similarity using your local embedding model.

const { searchVector } = require("./search/search_distill_chunks");
const results = await searchVector("how to grow spiritually", 5);
console.log(results);

3. Hybrid Search (RRF Fusion) β€” Recommended

State-of-the-art fusion of:

This gives robust results even on noisy or varied datasets.

const { searchHybrid } = require("./search/search_distill_chunks");
const results = await searchHybrid("balance love and wisdom", 5);
console.log(results);

πŸ–₯ Search CLI Tool

You can run searches directly from the terminal:

node search/search_cli.js "service to others"

Specify mode (bm25, vector, hybrid) and k:

node search/search_cli.js "healing catalyst" hybrid 8
node search/search_cli.js "unity" bm25 5
node search/search_cli.js "wisdom" vector 10

This prints:


πŸ§ͺ Tests

Run all tests:

npm test

Covers:


🧽 Cleanup

npm run clean

or:

bash cleanup.sh

πŸ›  Configuration

Config is handled via environment variables:

Variable Default Purpose
ELASTICSEARCH_NODE http://localhost:9200 ES cluster URL
ES_DISTILL_INDEX quo_distill_index Target index
EMBED_URL http://localhost:11434/api/embeddings Embedding API
EMBED_MODEL mxbai-embed-large Embedding model
CHUNK_MIN 5000 Minimum chunk size (characters)
CHUNK_MAX 9000 Maximum chunk size (characters)
JSON_DIR (required) Directory of session JSON

πŸ“„ License

Apache 2.0 (see LICENSE).


πŸ€— Contributing

Contributions are welcome β€” bug fixes, new extractors, support for other embedding backends, indexing strategies, documentation.


β˜€οΈ Final Thoughts

This project is meant to empower people building truth-aligned, service-oriented models. If it helps someone create a clearer dataset or a kinder AI, it’s doing its job.