"""Build compact, paper-aligned replay data for the GraphRAG demo.""" import csv import json import shutil from pathlib import Path ROOT = Path(__file__).resolve().parent PROJECT = ROOT.parents[1] PUBLIC_DEMO = PROJECT / "huggingface_demo" / "FinRAG-Equinor-Public-Demo" INPUTS = { "hybrid": PROJECT / "results" / "07_reranker" / "hybrid_rrf_bm25_year__intfloat__e5-large-v2_year__rerank_topk.jsonl", "graph": PROJECT / "results" / "20_path_graphrag" / "e5_plus_graph_topk.jsonl", "paths": PROJECT / "results" / "20_path_graphrag" / "e5_plus_path_topk.jsonl", "both": PROJECT / "results" / "20_path_graphrag" / "e5_plus_graph_path_topk.jsonl", } METRICS = { "hybrid": {"object_r10": 0.838, "page_r10": 0.908, "mrr": 0.730}, "graph": {"object_r10": 0.858, "page_r10": 0.905, "mrr": 0.737}, "paths": {"object_r10": 0.850, "page_r10": 0.914, "mrr": 0.733}, "both": {"object_r10": 0.859, "page_r10": 0.917, "mrr": 0.736}, } def rows(path): with path.open(encoding="utf-8") as handle: for line in handle: if line.strip(): yield json.loads(line) def compact_candidate(candidate): reasons = candidate.get("path_reasons") or candidate.get("graph_reasons") or [] return { "rank": candidate.get("rank"), "object_id": candidate.get("object_id"), "score": candidate.get("reranker_score", candidate.get("score")), "page": candidate.get("page"), "type": candidate.get("object_type"), "reasons": reasons, } def main(): output = {"metrics": METRICS, "results": {}} for method, path in INPUTS.items(): if not path.exists(): raise FileNotFoundError(path) for row in rows(path): qid = row["question_id"] output["results"].setdefault(qid, {})[method] = { "trace": row.get("path_trace", {}), "top_k": [compact_candidate(item) for item in row["top_k"][:10]], } (ROOT / "graph_replay.json").write_text( json.dumps(output, ensure_ascii=False, separators=(",", ":")), encoding="utf-8" ) for name in ("finrag_equinor_v1.0.jsonl", "source_manifest.csv"): shutil.copy2(PUBLIC_DEMO / name, ROOT / name) target = ROOT / "corpus" target.mkdir(exist_ok=True) for source in (PUBLIC_DEMO / "corpus").glob("eval_corpus_*.jsonl"): shutil.copy2(source, target / source.name) with (ROOT / "demo_build_summary.csv").open("w", encoding="utf-8", newline="") as handle: writer = csv.writer(handle) writer.writerow(["questions", "methods", "result_file_bytes"]) writer.writerow([len(output["results"]), len(INPUTS), (ROOT / "graph_replay.json").stat().st_size]) if __name__ == "__main__": main()