rag-document-qa / index.html
DataDrivenAlex's picture
Add RAG Document Q&A app
6dbd1b0 verified
Raw History Blame Contribute Delete
5.71 kB
<!DOCTYPE html>
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>RAG Document Q&amp;A</title><link rel="stylesheet" href="style.css"></head>
<body><main>
<h1>📚 RAG Document Q&amp;A</h1>
<p class="sub">Retrieval-Augmented question answering over your own documents, running 100% in your browser.
Pipeline: load (PDF/TXT/paste) → chunk with overlap → embed (all-MiniLM-L6-v2) → cosine vector search → extractive answer (DistilBERT-SQuAD), with sources.</p>
<div class="card">
<div class="row">
<div><label>Documents (PDF, TXT, MD)</label><input id="files" type="file" multiple accept=".pdf,.txt,.md">
<p class="muted">Files never leave your device.</p></div>
<div><label>…or paste text</label><textarea id="pasted"></textarea></div>
</div>
<button id="build">1. Build vector index</button>
<p id="status" class="muted"></p><progress id="prog" value="0" max="100" hidden></progress>
</div>
<div class="card">
<label>2. Ask a question</label><input id="q" type="text" value="Why does RAG reduce hallucinations?">
<label>Passages to retrieve (top-k): <span id="kv">3</span></label><input id="k" type="range" min="1" max="8" value="3">
<br><button id="ask" disabled>Ask</button>
<div id="answer"></div><div id="sources"></div>
</div>
<footer>Stack: Transformers.js · ONNX Runtime Web · pdf.js · vanilla JS. Built by
<a href="https://www.linkedin.com/in/alexdataanalyst/" target="_blank">Alexandru Cojocari</a> ·
<a href="https://github.com/DatadrivenAlex" target="_blank">GitHub</a></footer>
</main>
<script type="module">
import { pipeline } from 'https://cdn.jsdelivr.net/npm/@huggingface/transformers@3';
import * as pdfjs from 'https://cdn.jsdelivr.net/npm/pdfjs-dist@4.10.38/build/pdf.min.mjs';
pdfjs.GlobalWorkerOptions.workerSrc = 'https://cdn.jsdelivr.net/npm/pdfjs-dist@4.10.38/build/pdf.worker.min.mjs';
const $ = id => document.getElementById(id);
$('pasted').value = `Retrieval-Augmented Generation (RAG) combines a retriever and a generator.
Documents are split into chunks, converted into embeddings and stored in a vector database such as FAISS or ChromaDB.
At query time the question is embedded, the most similar chunks are retrieved and passed to a language model as context.
RAG reduces hallucinations because answers are grounded in retrieved sources, and it lets a model use private or recent data without fine-tuning.
Typical frameworks for building RAG pipelines are LangChain, LlamaIndex and LangGraph for agentic workflows.`;
$('k').oninput = e => $('kv').textContent = e.target.value;
let embedder, qa, index = null;
const progress = info => { if (info.status === 'progress') { $('prog').hidden = false; $('prog').value = info.progress; } };
const esc = s => s.replace(/[&<>]/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;'}[c]));
async function readFile(f) {
if (f.name.toLowerCase().endsWith('.pdf')) {
const pdf = await pdfjs.getDocument({ data: await f.arrayBuffer() }).promise;
let out = '';
for (let i = 1; i <= pdf.numPages; i++) out += (await (await pdf.getPage(i)).getTextContent()).items.map(t => t.str).join(' ') + '\n';
return out;
}
return await f.text();
}
function chunk(text, size = 500, overlap = 100) {
text = text.replace(/\s+/g, ' ').trim(); const out = [];
for (let s = 0; s < text.length; s += size - overlap) out.push(text.slice(s, s + size));
return out.filter(c => c.length > 30);
}
const dot = (a, b) => a.reduce((s, v, i) => s + v * b[i], 0);
$('build').onclick = async () => {
$('build').disabled = true;
try {
let text = $('pasted').value;
for (const f of $('files').files) text += '\n\n' + await readFile(f);
const chunks = chunk(text);
if (!chunks.length) { $('status').textContent = 'Please add a document or text.'; return; }
$('status').textContent = 'Loading embedding model…';
embedder ??= await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', { progress_callback: progress });
$('status').textContent = `Embedding ${chunks.length} chunks…`;
const vecs = (await embedder(chunks, { pooling: 'mean', normalize: true })).tolist();
index = chunks.map((c, i) => ({ c, v: vecs[i] }));
$('status').textContent = 'Loading QA model…';
qa ??= await pipeline('question-answering', 'Xenova/distilbert-base-cased-distilled-squad', { progress_callback: progress });
$('prog').hidden = true;
$('status').innerHTML = `<span class="ok">✔ Indexed ${chunks.length} chunks.</span> Ask a question below.`;
$('ask').disabled = false;
} catch (e) { $('status').innerHTML = `<span class="bad">Error: ${esc(e.message)}</span>`; }
finally { $('build').disabled = false; }
};
$('ask').onclick = async () => {
const q = $('q').value.trim(); if (!q || !index) return;
$('ask').disabled = true; $('answer').textContent = 'Thinking…';
try {
const qv = (await embedder(q, { pooling: 'mean', normalize: true })).tolist()[0];
const hits = index.map(x => ({ ...x, s: dot(qv, x.v) })).sort((a, b) => b.s - a.s).slice(0, +$('k').value);
const res = await qa(q, hits.map(h => h.c).join(' '));
$('answer').innerHTML = `<h3>Answer</h3><p style="font-size:18px"><b>${esc(res.answer || '—')}</b>
<span class="muted">(confidence ${(res.score ?? 0).toFixed(2)})</span></p>`;
$('sources').innerHTML = '<h3>Sources</h3>' + hits.map((h, i) =>
`<p class="muted">[${i + 1}] cosine similarity ${h.s.toFixed(2)}</p><blockquote>${esc(h.c)}</blockquote>`).join('');
} catch (e) { $('answer').innerHTML = `<span class="bad">Error: ${esc(e.message)}</span>`; }
finally { $('ask').disabled = false; }
};
</script></body></html>