lumen-rag / lumen_rag /api /static /index.html
WickTech's picture
Deploy from push.sh
4f8feb6 verified
Raw History Blame Contribute Delete
10.7 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lumen RAG — Live Demo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
:root { --bg:#0b0d12; --panel:#12151c; --border:#232733; --text:#e6e8ec; --muted:#8a8f9c; --accent:#5b8cff; --good:#4ade80; }
* { box-sizing: border-box; }
body { margin:0; background:var(--bg); color:var(--text); font:15px/1.5 -apple-system,Segoe UI,Roboto,sans-serif; }
header { padding:24px 32px; border-bottom:1px solid var(--border); }
header h1 { margin:0 0 4px; font-size:22px; }
header p { margin:0; color:var(--muted); font-size:13px; }
main { max-width:980px; margin:0 auto; padding:24px 32px 64px; display:grid; gap:24px; }
.card { background:var(--panel); border:1px solid var(--border); border-radius:10px; padding:20px; }
.card h2 { margin:0 0 12px; font-size:15px; color:var(--muted); text-transform:uppercase; letter-spacing:.04em; }
.row { display:flex; gap:10px; flex-wrap:wrap; align-items:center; }
input[type=text] { flex:1; min-width:200px; background:#0e1117; border:1px solid var(--border); color:var(--text); padding:10px 12px; border-radius:6px; font-size:14px; }
input[type=file] { color:var(--muted); font-size:13px; }
select { background:#0e1117; border:1px solid var(--border); color:var(--text); padding:9px; border-radius:6px; }
button { background:var(--accent); color:#fff; border:none; padding:10px 16px; border-radius:6px; cursor:pointer; font-size:14px; font-weight:600; }
button:hover { opacity:.9; }
button.ghost { background:transparent; border:1px solid var(--border); color:var(--text); }
button:disabled { opacity:.5; cursor:default; }
.answer { white-space:pre-wrap; margin-top:14px; padding:14px; background:#0e1117; border-radius:6px; border:1px solid var(--border); min-height:24px; }
.citation { display:inline-block; margin:2px 6px 2px 0; padding:3px 8px; background:#1a2030; border-radius:4px; font-size:12px; color:var(--muted); }
.metrics { display:grid; grid-template-columns:repeat(auto-fit,minmax(110px,1fr)); gap:12px; margin-top:8px; }
.metric { background:#0e1117; border:1px solid var(--border); border-radius:8px; padding:12px; text-align:center; }
.metric .val { font-size:22px; font-weight:700; color:var(--good); }
.metric .lbl { font-size:11px; color:var(--muted); text-transform:uppercase; margin-top:2px; }
.status { font-size:13px; color:var(--muted); margin-top:8px; }
.status.err { color:#f87171; }
.status.locked { color:var(--text); }
footer { text-align:center; color:var(--muted); font-size:12px; padding:24px; }
a { color:var(--accent); }
</style>
</head>
<body>
<header>
<h1>🔦 Lumen RAG — Live Demo</h1>
<p>Upload docs → retrieval (vector, BM25 or hybrid) → cited answer, or a refusal when the documents don't contain it. No API key.</p>
<p class="status" id="engineInfo"></p>
</header>
<main>
<div class="card">
<h2>1 · Load documents</h2>
<div class="row">
<button id="loadSample">Load sample corpus (7 policy docs)</button>
<span style="color:var(--muted)">or</span>
<input type="file" id="fileInput" multiple accept=".txt,.md,.html,.htm,.pdf,.docx">
<button id="uploadBtn" class="ghost">Upload</button>
<button id="resetBtn" class="ghost">Reset index</button>
<input type="text" id="adminKey" placeholder="Admin key (needed if ADMIN_KEY is set)" style="max-width:280px">
</div>
<div class="status" id="ingestStatus"></div>
</div>
<div class="card">
<h2>2 · Ask</h2>
<div class="row">
<input type="text" id="question" placeholder="e.g. How long is an on-call rotation?">
<select id="mode">
<option value="hybrid" selected>hybrid</option>
<option value="vector">vector</option>
<option value="bm25">bm25</option>
</select>
<button id="askBtn">Ask</button>
</div>
<div class="answer" id="answer">Answer will appear here.</div>
<div class="status" id="confidence"></div>
<div id="citations"></div>
</div>
<div class="card">
<h2>3 · Retrieval quality (recall@k on the labelled sample eval set)</h2>
<div class="row">
<button id="evalBtn">Run eval</button>
</div>
<div class="metrics" id="metrics"></div>
<div class="status" id="evalStatus"></div>
</div>
</main>
<footer>
<a href="https://github.com/WickTech/lumen-rag" target="_blank">Source on GitHub</a> ·
API docs at <a href="/docs">/docs</a>
</footer>
<script>
const $ = id => document.getElementById(id);
// Ingest and reset need X-Admin-Key when the server has ADMIN_KEY set.
const adminHeaders = () => ({ 'X-Admin-Key': $('adminKey').value });
const ingestControls = ['loadSample', 'fileInput', 'uploadBtn', 'resetBtn'];
let ingestLocked = false;
// Locked (GET /health ingest_locked): visitors cannot change the shared index. The
// controls stay disabled until an admin key is typed, instead of failing with an error.
function applyIngestLock() {
const disabled = ingestLocked && !$('adminKey').value;
for (const id of ingestControls) $(id).disabled = disabled;
if (ingestLocked && !$('ingestStatus').textContent) {
$('ingestStatus').textContent = 'Ingestion is locked on the public demo: the sample corpus is preloaded, ' +
'so go straight to Ask. Run Lumen locally to index your own documents.';
$('ingestStatus').className = 'status locked';
}
}
$('adminKey').addEventListener('input', applyIngestLock);
async function api(path, opts) {
const res = await fetch(path, opts);
const body = await res.json().catch(() => ({}));
if (!res.ok) throw new Error(body.detail || res.statusText);
return body;
}
$('loadSample').onclick = async () => {
$('ingestStatus').textContent = 'Loading sample corpus…';
$('ingestStatus').className = 'status';
try {
const r = await api('/ingest/sample', { method: 'POST', headers: adminHeaders() });
$('ingestStatus').textContent = `Indexed ${r.files_indexed} file(s) → ${r.chunks_indexed} chunks.`;
} catch (e) {
$('ingestStatus').textContent = 'Error: ' + e.message;
$('ingestStatus').className = 'status err';
}
};
$('uploadBtn').onclick = async () => {
const files = $('fileInput').files;
if (!files.length) { $('ingestStatus').textContent = 'Pick file(s) first.'; return; }
const fd = new FormData();
for (const f of files) fd.append('files', f);
$('ingestStatus').textContent = 'Uploading + indexing…';
$('ingestStatus').className = 'status';
try {
const r = await api('/ingest/upload', { method: 'POST', body: fd, headers: adminHeaders() });
$('ingestStatus').textContent = `Indexed ${r.files_indexed} file(s) → ${r.chunks_indexed} chunks.`;
} catch (e) {
$('ingestStatus').textContent = 'Error: ' + e.message;
$('ingestStatus').className = 'status err';
}
};
$('resetBtn').onclick = async () => {
try {
await api('/reset', { method: 'POST', headers: adminHeaders() });
} catch (e) {
$('ingestStatus').textContent = 'Error: ' + e.message;
$('ingestStatus').className = 'status err';
return;
}
$('ingestStatus').textContent = 'Index cleared.';
$('ingestStatus').className = 'status';
$('answer').textContent = 'Answer will appear here.';
$('citations').innerHTML = '';
$('confidence').textContent = '';
$('metrics').innerHTML = '';
};
$('askBtn').onclick = async () => {
const question = $('question').value.trim();
if (!question) return;
$('answer').textContent = 'Thinking…';
$('citations').innerHTML = '';
$('confidence').textContent = '';
try {
const r = await api('/query', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ question, mode: $('mode').value, k: 5 })
});
$('answer').textContent = r.answer;
const parts = [];
if (r.confidence !== null && r.confidence !== undefined) parts.push(`retrieval confidence ${Number(r.confidence).toFixed(3)}`);
if (r.answerability !== null && r.answerability !== undefined) parts.push(`reader answerability ${Number(r.answerability).toFixed(1)}`);
if (parts.length) {
$('confidence').textContent = parts.join(' · ') +
(r.abstained ? ' — below threshold, so it refused instead of guessing.' : ' — answered.');
}
const citationsEl = $('citations');
citationsEl.innerHTML = '';
// doc_id is attacker-controlled (set at /ingest time via IngestRequest.documents[].id,
// or the uploaded filename), so build these with textContent, not innerHTML —
// a malicious doc_id must never be parsed as HTML here.
for (const c of r.citations) {
const span = document.createElement('span');
span.className = 'citation';
const section = c.context && c.context.includes('Section: ') ? ' · ' + c.context.split('Section: ')[1] : '';
span.textContent = `[${c.n}] ${c.doc_id}${section} · score ${Number(c.score).toFixed(3)}`;
citationsEl.appendChild(span);
}
} catch (e) {
$('answer').textContent = 'Error: ' + e.message;
}
};
// Say which embedder is live; real embedders rank best in vector mode (see README benchmarks).
api('/health').then(h => {
const names = { HashingEmbedder: 'offline hashing embedder', FastEmbedEmbedder: 'local embedding model', OpenAIEmbedder: 'OpenAI embeddings' };
let info = 'Running on the ' + (names[h.embedder] || h.embedder) + (h.embedding_model ? ` (${h.embedding_model})` : '');
info += h.abstain_threshold !== null ? ` · refuses below retrieval confidence ${h.abstain_threshold}` : ' · abstention off';
if (h.reader) info += ` · reader ${h.reader.split('/').pop()} checks each answer (threshold ${h.reader_threshold})`;
$('engineInfo').textContent = info;
if (h.embedder !== 'HashingEmbedder') $('mode').value = 'vector';
ingestLocked = Boolean(h.ingest_locked);
applyIngestLock();
}).catch(() => {});
$('question').addEventListener('keydown', e => { if (e.key === 'Enter') $('askBtn').click(); });
$('evalBtn').onclick = async () => {
$('evalStatus').textContent = 'Running eval harness…';
$('evalStatus').className = 'status';
$('metrics').innerHTML = '';
try {
const r = await api('/eval');
const order = ['recall@k', 'precision@k', 'mrr', 'ndcg@k', 'hit_rate'];
$('metrics').innerHTML = order.map(k =>
`<div class="metric"><div class="val">${r[k]}</div><div class="lbl">${k}</div></div>`).join('');
$('evalStatus').textContent = `${r.n_cases} labelled questions, k=${r.k}.`;
} catch (e) {
$('evalStatus').textContent = 'Error: ' + e.message;
$('evalStatus').className = 'status err';
}
};
</script>
</body>
</html>