offline-rag-library
Agent Skill that scaffolds offline single-page library sites with an in-browser RAG chatbot — EPUB→markdown conversion, prebuilt embeddings, and zero-server, zero-API-key retrieval via transformers.js + WebGPU.


🌐 Live Demo — 3 public-domain classics with a working in-browser AI assistant.
What is this?
A Claude Code plugin / Agent Skill that generates zero-server reading-library websites. Each generated site is a static SPA that:
- 📚 Hosts EPUB books as a searchable markdown reader (portal grid + section-tracked reader)
- 🤖 Runs an AI assistant entirely in the browser — Gemma 4 E2B (~3.1 GB) via WebGPU + transformers.js
- 🔍 Cross-book RAG — embeddinggemma-300m precomputed vectors, scope-aware retrieval (this chapter / this book / all books)
- 🔒 No backend, no API keys, no data leaves the browser — deploy to GitHub Pages and you're done
The chatbot downloads model weights on first use (~3.4 GB total) and caches them in IndexedDB; subsequent visits are instant.
Install
As a Claude Code plugin (marketplace)
# In Claude Code:
/plugin marketplace add hevangel/offline-rag-library
/plugin install offline-rag-library
Then just ask Claude to "build a library site from my EPUBs" — the skill auto-triggers.
Standalone (clone and use the scripts directly)
git clone https://github.com/hevangel/offline-rag-library.git
cd offline-rag-library
uv sync # Python deps: beautifulsoup4, lxml, markdownify
Quick start
# 1. Scaffold a fresh site
uv run python scripts/init_library.py ./my-library --name "My Library"
# 2. Add EPUB books (pure Python converter — no Calibre, no external binaries)
uv run python scripts/convert_epub.py "path/to/book.epub" --slug book-slug --output-root ./my-library/books
# 3. Generate catalog + RAG artifacts
uv run python scripts/build_catalog.py --root ./my-library
uv run python scripts/build_chatbot_index.py --root ./my-library
# 4. Build the embedding cache (first run downloads ~300 MB model)
cd scripts && npm install && cd ..
node scripts/build_chatbot_embeddings.mjs
cp assets/chatbot_chunks.json assets/chatbot_embeddings.bin ./my-library/assets/
# 5. Serve and test
cd my-library && python -m http.server 8000
How it works
EPUB file
│
▼ convert_epub.py (stdlib zipfile + BeautifulSoup, zero binaries)
books/<slug>/index.md ← single markdown file with <!-- rl-section --> markers
│
├──► build_catalog.py ──► books.json (portal catalog: title, author, tags)
│
├──► build_chatbot_index.py ──► chatbot_chunks.json (2770 text chunks, ~1200 chars each)
│ │
│ ▼ build_chatbot_embeddings.mjs (embeddinggemma-300m, q8)
│ chatbot_embeddings.bin (768-dim vectors, SHA-256 + model-id verified)
│
▼ SPA fetches at runtime
Browser: spa.js (router) + chatbot.js (Gemma + RAG) + marked.js (renderer)
│
▼ window.LIBRARY_CONFIG isolates each deployment's IndexedDB/localStorage
GitHub Pages (or any static host)
The RAG pipeline
- Build time: every book's text is chunked into ~1200-char overlapping sections, then embedded with embeddinggemma-300m (768-dim, q8, mean-pooled + L2-normalized). The binary cache embeds both a SHA-256 of the chunk texts and the model ID in its header — the browser refuses a stale or wrong-model cache.
- Query time: the browser embeds the user's question with the same model, cosine-sorts against cached vectors, and injects the top-5 chunks into Gemma's system prompt alongside the current page text.
- Scope toggle: the assistant can search this chapter, this book, or all books.
Architecture
| Component | Technology | Notes |
|---|
| SPA shell | Vanilla JS + marked.js | Hash router, markdown loader, IntersectionObserver section tracking |
| LLM | Gemma 4 E2B via transformers.js v4 + WebGPU | ~3.1 GB, q4f16, cached in IndexedDB |
| Embeddings | embeddinggemma-300m (ONNX, q8) | 768-dim, WASM backend (avoids GPU contention with Gemma) |
| Converter | Python stdlib zipfile + BeautifulSoup | No Calibre, no pandoc — pure Python |
| Embedding builder | Node + @huggingface/transformers | Auto-selects DirectML (GPU) on Windows + NVIDIA, else CPU |
Configuration
Each deployment brands itself via window.LIBRARY_CONFIG in the host index.html:
<script>
window.LIBRARY_CONFIG = {
name: "My Library",
description: "a curated collection of books",
slug: "my-library" // ← namespaces IndexedDB + localStorage per deployment
};
</script>