initial commit: retrieval + agent artifacts
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
"""Build evaluation tasks from BEIR qrels: real queries, real gold documents.
|
||||
|
||||
The required fact is a short verbatim span from the gold document, so the
|
||||
verifier can reject an answer that paraphrased it away or cited the wrong chunk.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import random
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def main() -> int:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--data", default="../rag-lab/data/scifact")
|
||||
parser.add_argument("--count", type=int, default=12)
|
||||
parser.add_argument("--out", default="data/tasks.jsonl")
|
||||
parser.add_argument("--seed", type=int, default=7)
|
||||
args = parser.parse_args()
|
||||
|
||||
root = Path(args.data)
|
||||
queries = {row["_id"]: row["text"] for row in map(json.loads, (root / "queries.jsonl").read_text(encoding="utf-8").splitlines()) if row}
|
||||
corpus = {row["_id"]: row for row in map(json.loads, (root / "corpus.jsonl").read_text(encoding="utf-8").splitlines()) if row}
|
||||
|
||||
pairs = []
|
||||
for line in (root / "qrels" / "test.tsv").read_text(encoding="utf-8").splitlines()[1:]:
|
||||
if not line.strip():
|
||||
continue
|
||||
qid, did, score = line.split("\t")
|
||||
if int(score) >= 1 and qid in queries and did in corpus:
|
||||
pairs.append((qid, did))
|
||||
|
||||
rng = random.Random(args.seed)
|
||||
chosen: list[tuple[str, str]] = []
|
||||
seen_docs: set[str] = set()
|
||||
for pair in rng.sample(pairs, len(pairs)):
|
||||
if pair[1] in seen_docs:
|
||||
continue
|
||||
chosen.append(pair)
|
||||
seen_docs.add(pair[1])
|
||||
if len(chosen) >= args.count:
|
||||
break
|
||||
|
||||
out = Path(args.out)
|
||||
out.parent.mkdir(parents=True, exist_ok=True)
|
||||
with out.open("w", encoding="utf-8") as handle:
|
||||
for index, (qid, did) in enumerate(chosen, start=1):
|
||||
doc = corpus[did]
|
||||
sentences = [s.strip().replace("\n", " ") for s in doc.get("text", ". ").split(".")
|
||||
if 40 < len(s.strip()) < 220]
|
||||
span = min(sentences, key=len) if sentences else ""
|
||||
handle.write(json.dumps({
|
||||
"id": f"t{index:03d}",
|
||||
"question": queries[qid],
|
||||
"gold_doc": did,
|
||||
"required_fact": span,
|
||||
"source": f"beir-scifact:{qid}",
|
||||
}, ensure_ascii=False) + "\n")
|
||||
print(f"tasks={len(chosen)} -> {out}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
sys.exit(main())
|
||||
Reference in New Issue
Block a user