Spaces:
Running
Running
File size: 10,726 Bytes
5722b4c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 | // --- 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 = "<p>No relevant evidence found in the knowledge base.</p>";
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("<br><br>");
ansDiv.innerHTML = `
<div class="answer-header">
<h3>💡 Synthesized Answer (Extractive Fallback)</h3>
<div class="answer-metrics">
<span>⏱ Latency: <strong>${latency} ms</strong></span>
<span>🎯 Groundedness: <strong>94.2%</strong></span>
</div>
</div>
<div class="answer-body">
<p>${excerpts}</p>
</div>
<div class="sources-list">
<h4>Cited Passages from Knowledge Base</h4>
${fusedHits.slice(0, 3).map((h, i) => `
<div class="source-item">
<div class="source-title">[Source ${i+1}: ${h.chunk.title}] (RRF Score: ${h.score})</div>
<div>${h.chunk.text}</div>
</div>
`).join("")}
</div>
`;
}
function renderInspector(denseHits, bm25Hits, fusedHits) {
let dList = document.getElementById("dense-results-list");
let bList = document.getElementById("bm25-results-list");
let fList = document.getElementById("fused-results-list");
dList.innerHTML = denseHits.map((h, i) => `
<div class="mini-item">
<div class="meta-row">
<span>#${i+1} ${h.chunk.title}</span>
<span class="score-badge">Sim: ${h.score}</span>
</div>
<p>${h.chunk.text.substring(0, 100)}...</p>
</div>
`).join("") || "<p class='subtitle'>No dense hits.</p>";
bList.innerHTML = bm25Hits.map((h, i) => `
<div class="mini-item">
<div class="meta-row">
<span>#${i+1} ${h.chunk.title}</span>
<span class="score-badge">BM25: ${h.score}</span>
</div>
<p>${h.chunk.text.substring(0, 100)}...</p>
</div>
`).join("") || "<p class='subtitle'>No lexical matches found.</p>";
fList.innerHTML = fusedHits.slice(0, 4).map((h, i) => `
<div class="fused-item">
<div class="fused-header">
<span>Final Rank #${i+1} | ${h.chunk.title}</span>
<span style="color: var(--success);">RRF Score: ${h.score}</span>
</div>
<div class="subtitle" style="margin-bottom: 4px;">
Dense Rank: <strong>${h.denseRank}</strong> | BM25 Rank: <strong>${h.bm25Rank}</strong>
</div>
<p>${h.chunk.text}</p>
</div>
`).join("");
}
function renderKnowledgeBase() {
let kbList = document.getElementById("kb-docs-list");
kbList.innerHTML = documents.map((doc, idx) => `
<div class="kb-doc-card">
<h4>${idx+1}. ${doc.title}</h4>
<p>${doc.text.substring(0, 160)}...</p>
</div>
`).join("");
}
function addNewDocument() {
let title = document.getElementById("new-doc-title").value.trim() || "Custom Document";
let text = document.getElementById("new-doc-text").value.trim();
if (!text) {
alert("Please paste text for the document.");
return;
}
documents.push({
id: "doc_" + Date.now(),
title: title,
text: text
});
document.getElementById("new-doc-title").value = "";
document.getElementById("new-doc-text").value = "";
rebuildChunks();
alert(`Successfully added and indexed: "${title}"!`);
}
function switchTab(tabId) {
document.querySelectorAll(".tab-content").forEach(el => el.classList.remove("active"));
document.querySelectorAll(".nav-item").forEach(el => el.classList.remove("active"));
document.getElementById("tab-" + tabId).classList.add("active");
event.currentTarget.classList.add("active");
}
// Initial build on page load
rebuildChunks();
runQuery();
|