const METHODS = { hybrid: { label: "Hybrid E5 + rerank", color: "#4f46e5" }, graph: { label: "Retained E5 + selected graph", color: "#0f766e" }, paths: { label: "Retained E5 + graph paths", color: "#c25b16" }, both: { label: "Retained E5 + graph + paths", color: "#9a6700" } }; const state = { benchmark: [], byId: {}, replay: null, sources: {}, corpus: {}, filtered: [] }; const $ = id => document.getElementById(id); const esc = value => String(value ?? "").replace(/[&<>"']/g, c => ({"&":"&","<":"<",">":">",'"':""","'":"'"}[c])); const parseJsonl = text => text.split(/\r?\n/).filter(Boolean).map(line => JSON.parse(line)); function parseCsv(text) { const lines = text.trim().split(/\r?\n/); const headers = lines.shift().split(","); return lines.map(line => { const cells = line.match(/("(?:[^"]|"")*"|[^,]*)(?:,|$)/g).map(v => v.replace(/,$/, "").replace(/^"|"$/g, "").replace(/""/g, '"')); return Object.fromEntries(headers.map((h, i) => [h, cells[i] || ""])); }); } async function loadCorpus(year) { if (state.corpus[year]) return state.corpus[year]; const response = await fetch(`corpus/eval_corpus_${year}.jsonl`); if (!response.ok) throw new Error(`Could not load the ${year} evidence corpus.`); const rows = parseJsonl(await response.text()); state.corpus[year] = Object.fromEntries(rows.map(row => [row.object_id, row])); return state.corpus[year]; } function goldStatus(question, candidate) { const evidence = question.evidence || []; if (evidence.some(e => e.object_id === candidate.object_id)) return { css: "gold", label: "Exact gold evidence" }; if (evidence.some(e => String(e.page) === String(candidate.page))) return { css: "page", label: "Gold page, different object" }; return { css: "miss", label: "Not annotated gold" }; } function provenance(reasons = []) { const labels = []; for (const reason of reasons) { if (reason === "base" || reason.startsWith("source:hybrid")) labels.push("Hybrid"); else if (reason.startsWith("source:without_adjacent_page")) labels.push("Selected graph"); else if (reason.startsWith("query_metric:")) labels.push(`Query metric: ${reason.split(":")[1]}`); else if (reason.startsWith("query_entity:")) labels.push(`Query entity: ${reason.split(":")[1]}`); else if (reason.startsWith("path:same_page:")) labels.push("Same page"); else if (reason.startsWith("path:same_metric:")) labels.push(`Same metric: ${reason.split(":").at(-1)}`); else if (reason.startsWith("path:same_entity:")) labels.push(`Same entity: ${reason.split(":").at(-1)}`); } return [...new Set(labels.length ? labels : ["Saved ranking"])]; } function applyFilters() { const year = $("year-filter").value; const type = $("type-filter").value; const keyword = $("keyword-filter").value.trim().toLowerCase(); state.filtered = state.benchmark.filter(q => state.replay.results[q.question_id]) .filter(q => year === "All years" || String(q.report_year) === year) .filter(q => type === "All types" || q.question_type === type) .filter(q => !keyword || q.question.toLowerCase().includes(keyword)); $("question-count").textContent = `${state.filtered.length} questions`; $("question-select").innerHTML = state.filtered.map(q => ``).join(""); render(); } async function render() { const qid = $("question-select").value; if (!qid) { $("question-summary").innerHTML = "

No questions match these filters.

"; $("comparison").innerHTML = ""; $("ranking").innerHTML = ""; return; } const question = state.byId[qid]; $("question-summary").innerHTML = `

${esc(question.question)}

Gold answer: ${esc(question.answer || "No annotated answer")}

${esc(question.report_year)} annual report · ${esc(question.question_type.replaceAll("_", " "))}

`; $("comparison").innerHTML = Object.entries(METHODS).map(([key, method]) => { const top = state.replay.results[qid][key].top_k; const exact = top.filter(c => goldStatus(question, c).css === "gold"); const page = top.filter(c => ["gold", "page"].includes(goldStatus(question, c).css)); const first = exact.length ? exact[0].rank : "-"; return `

${esc(method.label)}

Exact objects${exact.length}
Gold-page objects${page.length}
First exact rank${first}
`; }).join(""); await renderRanking(question); } async function renderRanking(question) { const key = $("method-select").value || "both"; const method = METHODS[key]; const result = state.replay.results[question.question_id][key]; $("ranking").innerHTML = '
Loading evidence text...
'; try { const corpus = await loadCorpus(question.report_year); const trace = result.trace || {}; const cues = []; if (trace.years?.length) cues.push(`Year: ${trace.years.join(", ")}`); if (trace.entities?.length) cues.push(`Entities: ${trace.entities.join(", ")}`); if (trace.metrics?.length) cues.push(`Metrics: ${trace.metrics.join(", ")}`); const allLabels = [...new Set(result.top_k.flatMap(c => provenance(c.reasons)))]; $("route-summary").style.setProperty("--accent", method.color); $("route-summary").innerHTML = `${esc(method.label)}${esc(cues.join(" · ") || "Saved ranking without path cues")}
${allLabels.map(x => `${esc(x)}`).join("")}
`; renderGraph(question, key, result, corpus); $("ranking").innerHTML = result.top_k.map(candidate => { const obj = corpus[candidate.object_id] || {}; const status = goldStatus(question, candidate); const base = state.sources[question.document_id] || ""; const link = base ? `${base}#page=${candidate.page}` : "#"; return `
#${candidate.rank}
${esc(status.label)}
${provenance(candidate.reasons).map(x => `${esc(x)}`).join("")}
Page ${esc(candidate.page)}
${esc(candidate.type || obj.object_type || "")}
${esc(candidate.object_id)}

${esc((obj.text || "Evidence text unavailable").replace(/\s+/g, " ").slice(0, 520))}

PDF
`; }).join(""); } catch (error) { $("graph-view").innerHTML = `
${esc(error.message)}
`; $("ranking").innerHTML = `
${esc(error.message)}
`; } } function renderGraph(question, key, result, corpus) { const trace = result.trace || {}; const years = trace.years?.length ? trace.years : [question.report_year]; const entities = trace.entities || []; const metrics = trace.metrics || []; const cueNodes = [ ...years.map(year => `
Year${esc(year)}
`), ...entities.map(entity => `
Entity${esc(entity)}
`), ...metrics.map(metric => `
Metric${esc(metric)}
`) ].join(""); const graphRows = result.top_k.slice(0, 5).map(candidate => { const obj = corpus[candidate.object_id] || {}; const status = goldStatus(question, candidate); const reasons = candidate.reasons || []; let source = "BM25 + E5"; let sourceClass = "hybrid"; let relation = "retrieves"; let edgeClass = "hybrid"; if (reasons.some(reason => reason.startsWith("path:same_metric:"))) { source = `Metric: ${metrics[0] || "query metric"}`; sourceClass = "metric"; relation = "same-metric"; edgeClass = "metric"; } else if (reasons.some(reason => reason.startsWith("path:same_entity:"))) { source = `Entity: ${entities[0] || "query entity"}`; sourceClass = "entity"; relation = "same-entity"; edgeClass = "entity"; } else if (reasons.some(reason => reason.startsWith("path:same_page:"))) { source = "Page-linked object"; sourceClass = "page"; relation = "same-page"; edgeClass = "page"; } else if (reasons.some(reason => reason.startsWith("source:without_adjacent_page"))) { source = "Hybrid seed object"; sourceClass = "graph"; relation = "selected-graph expansion"; edgeClass = "graph"; } const text = (obj.text || candidate.object_id).replace(/\s+/g, " ").slice(0, 115); return `
${esc(source)}
${esc(relation)}
#${candidate.rank} · Page ${esc(candidate.page)}${esc(text)}
`; }).join(""); $("graph-view").style.setProperty("--accent", METHODS[key].color); $("graph-view").innerHTML = `
Question${esc(question.question)}
${cueNodes || `
Year${esc(question.report_year)}
`}
Active node-to-node routes
${graphRows}
Hybrid edgeGraph expansion edgeTyped graph edgeGreen node = exact gold · Amber node = gold page
`; } async function init() { try { const [benchmarkRes, replayRes, sourceRes] = await Promise.all([ fetch("finrag_equinor_v1.0.jsonl"), fetch("graph_replay.json"), fetch("source_manifest.csv") ]); if (!benchmarkRes.ok || !replayRes.ok || !sourceRes.ok) throw new Error("One or more demo data files could not be loaded."); state.benchmark = parseJsonl(await benchmarkRes.text()).filter(q => q.is_answerable); state.byId = Object.fromEntries(state.benchmark.map(q => [q.question_id, q])); state.replay = await replayRes.json(); state.sources = Object.fromEntries(parseCsv(await sourceRes.text()).map(row => [row.document_id, row.download_url])); const years = [...new Set(state.benchmark.map(q => q.report_year))].sort(); $("year-filter").innerHTML += years.map(y => ``).join(""); const types = [...new Set(state.benchmark.map(q => q.question_type))].sort(); $("type-filter").innerHTML += types.map(t => ``).join(""); $("method-select").innerHTML = Object.entries(METHODS).map(([k, v]) => ``).join(""); ["year-filter", "type-filter"].forEach(id => $(id).addEventListener("change", applyFilters)); $("keyword-filter").addEventListener("input", applyFilters); $("question-select").addEventListener("change", render); $("method-select").addEventListener("change", () => renderRanking(state.byId[$("question-select").value])); applyFilters(); } catch (error) { $("question-count").textContent = "Load failed"; $("question-summary").innerHTML = `
${esc(error.message)}
`; } } init();