// --- Knowledge Base Corpus --- let documents = [ { id: "doc_quantum", title: "Quantum Computing Fundamentals", text: "Quantum computing harnesses the laws of quantum mechanics to solve problems too complex for classical computers. Classical computers encode information in binary bits (0 or 1), whereas quantum computers use qubits. Due to superposition, qubits can exist in states of both 0 and 1 simultaneously. Superconducting quantum circuits (used by IBM and Google) require specialized cryogenic environments cooled down to approximately 15 millikelvin (-273.135 degrees Celsius), which is colder than outer space, using dilution refrigerators. Promising applications include molecular simulations for drug discovery and Shor's algorithm for cryptography." }, { id: "doc_jwst", title: "James Webb Space Telescope Overview", text: "The James Webb Space Telescope (JWST) is an infrared space observatory launched on December 25, 2021, aboard an Ariane 5 rocket from Kourou, French Guiana. Developed by NASA, ESA, and CSA, it succeeds the Hubble Space Telescope. The telescope features a 6.5-meter diameter primary mirror composed of 18 hexagonal segments made from gold-plated beryllium. It operates at the Sun-Earth Second Lagrange Point (L2), approximately 1.5 million kilometers from Earth, protected by a five-layer tennis-court-sized sunshield. Its four primary instruments are NIRCam, NIRSpec, MIRI, and FGS/NIRISS." }, { id: "doc_energy", title: "Renewable Energy and Modern Grid Storage", text: "The transition toward a sustainable energy system relies heavily on variable renewable energy sources, predominantly solar photovoltaics (PV) and wind turbines. Energy storage technologies are essential to balance fluctuating supply and demand. Battery Energy Storage Systems (BESS), typically utilizing lithium iron phosphate (LFP) or nickel manganese cobalt (NMC), provide short-duration storage (2 to 4 hours). For long-duration energy storage (LDES) exceeding 8 to 24 hours, pumped-storage hydroelectricity, compressed air energy storage (CAES), and vanadium redox flow batteries are deployed." } ]; // Ingest documents into chunks let chunks = []; function chunkText(text, title, docId, chunkSize = 50) { let words = text.split(/\s+/); let result = []; let step = 35; for (let i = 0; i < words.length; i += step) { let piece = words.slice(i, i + chunkSize).join(" "); result.push({ id: `${docId}_c${result.length}`, docId: docId, title: title, text: piece }); if (i + chunkSize >= words.length) break; } return result; } function rebuildChunks() { chunks = []; documents.forEach(doc => { let docChunks = chunkText(doc.text, doc.title, doc.id); chunks.push(...docChunks); }); document.getElementById("stat-docs").innerText = documents.length; document.getElementById("stat-chunks").innerText = chunks.length; renderKnowledgeBase(); } // --- Retrieval: Dense Hash Embedding Simulation --- function hashEmbed(text, dim = 64) { let vec = new Array(dim).fill(0); let tokens = text.toLowerCase().match(/[a-z0-9]+/g) || []; tokens.forEach((t, i) => { let hash = 0; for (let j = 0; j < t.length; j++) { hash = (hash << 5) - hash + t.charCodeAt(j); hash |= 0; } let bucket = Math.abs(hash) % dim; vec[bucket] += 1.0 + (i % 5) * 0.05; }); let norm = Math.sqrt(vec.reduce((sum, v) => sum + v * v, 0)) || 1.0; return vec.map(v => v / norm); } function cosineSimilarity(a, b) { return a.reduce((sum, val, idx) => sum + val * b[idx], 0); } function denseSearch(query, topK = 5) { let qVec = hashEmbed(query); return chunks.map(chunk => { let cVec = hashEmbed(chunk.text + " " + chunk.title); return { chunk: chunk, score: Number(cosineSimilarity(qVec, cVec).toFixed(4)) }; }).sort((a, b) => b.score - a.score).slice(0, topK); } // --- Retrieval: BM25 Keyword Search --- function bm25Search(query, topK = 5) { let qTokens = query.toLowerCase().match(/[a-z0-9]+/g) || []; if (qTokens.length === 0) return []; let N = chunks.length; let scores = chunks.map(chunk => { let textTokens = (chunk.title + " " + chunk.text).toLowerCase().match(/[a-z0-9]+/g) || []; let score = 0; qTokens.forEach(t => { let tf = textTokens.filter(x => x === t).length; if (tf > 0) { let df = chunks.filter(c => (c.title + " " + c.text).toLowerCase().includes(t)).length || 1; let idf = Math.log((N - df + 0.5) / (df + 0.5) + 1.0); score += idf * ((tf * 2.5) / (tf + 1.5)); } }); return { chunk: chunk, score: Number(score.toFixed(3)) }; }); return scores.filter(s => s.score > 0).sort((a, b) => b.score - a.score).slice(0, topK); } // --- Reciprocal Rank Fusion (RRF) --- function rrfMerge(denseHits, bm25Hits, k = 60) { let fusedScores = {}; let chunksMap = {}; let denseRanks = {}; let bm25Ranks = {}; denseHits.forEach((hit, idx) => { let cid = hit.chunk.id; chunksMap[cid] = hit.chunk; denseRanks[cid] = idx + 1; fusedScores[cid] = (fusedScores[cid] || 0) + (0.6 / (k + idx + 1)); }); bm25Hits.forEach((hit, idx) => { let cid = hit.chunk.id; chunksMap[cid] = hit.chunk; bm25Ranks[cid] = idx + 1; fusedScores[cid] = (fusedScores[cid] || 0) + (0.4 / (k + idx + 1)); }); return Object.keys(fusedScores).map(cid => ({ chunk: chunksMap[cid], score: Number(fusedScores[cid].toFixed(5)), denseRank: denseRanks[cid] || "N/A", bm25Rank: bm25Ranks[cid] || "N/A" })).sort((a, b) => b.score - a.score); } // --- UI Interaction Functions --- function setQuery(text) { document.getElementById("query-input").value = text; runQuery(); } function runQuery() { let query = document.getElementById("query-input").value.trim(); if (!query) return; let start = performance.now(); let denseHits = denseSearch(query); let bm25Hits = bm25Search(query); let fused = rrfMerge(denseHits, bm25Hits); let latency = (performance.now() - start).toFixed(1); renderAnswer(query, fused, latency); renderInspector(denseHits, bm25Hits, fused); } function renderAnswer(query, fusedHits, latency) { let ansDiv = document.getElementById("answer-section"); if (fusedHits.length === 0) { ansDiv.innerHTML = "
No relevant evidence found in the knowledge base.
"; return; } let topSources = [...new Set(fusedHits.slice(0, 3).map(h => h.chunk.title))]; let excerpts = fusedHits.slice(0, 2).map(h => `"${h.chunk.text}" [Source: ${h.chunk.title}]`).join("${excerpts}
${h.chunk.text.substring(0, 100)}...
No dense hits.
"; bList.innerHTML = bm25Hits.map((h, i) => `${h.chunk.text.substring(0, 100)}...
No lexical matches found.
"; fList.innerHTML = fusedHits.slice(0, 4).map((h, i) => `${h.chunk.text}
${doc.text.substring(0, 160)}...