File size: 2,791 Bytes
41b7006
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""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()