File size: 12,039 Bytes
e2c9d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/env python3
"""Convert the official RAGTruth release into our TokenEvalRow format.

RAGTruth ships character-span annotations on response text. This pipeline needs
one label per *model* token, so the conversion re-tokenises each response with
the scoring model's own tokenizer and maps spans onto tokens by character
overlap. Their word-level `generated_tokens` are deliberately ignored: the only
durable part of a span annotation is its character offsets.

Two things make this replay faithful rather than approximate:

* **Model matching.** Only responses generated by `--generator-model` are kept,
  so the model whose internals we later read is the model that wrote the text.
  Scoring another model's output would measure surprise at someone else's
  continuation, not self-detection. LLM-Check (NeurIPS 2024) filters RAGTruth
  the same way in `utils_ragtruth.py`.
* **Prompt matching.** RAGTruth's README states its generation wrapper exactly:
  "For Llama and Mistral models, we used `<s>[INST] {prompt} [/INST]`". We
  rebuild that string, so the hidden state at each prediction row is the state
  the model was actually in when it emitted the next token.

Label semantics follow this repo's convention: 0 = grounded, 1 = hallucinated,
2 = uncertain. RAGTruth has no uncertain class, so 2 is never assigned -- every
token outside a gold span is grounded.

Usage:
    python scripts/convert_ragtruth.py \
        --ragtruth-dir ~/Desktop/datasets/hallucinations/ragtruth_official \
        --generator-model llama-2-7b-chat \
        --tokenizer NousResearch/Llama-2-7b-chat-hf \
        --output outputs/ragtruth_llama2_7b_chat.jsonl
"""

from __future__ import annotations

import argparse
import collections
import hashlib
import json
from pathlib import Path
import sys

REPO_ROOT = Path(__file__).resolve().parents[1]
if str(REPO_ROOT) not in sys.path:
    sys.path.insert(0, str(REPO_ROOT))

# RAGTruth README: "For Llama and Mistral models, we used `<s>[INST] {prompt} [/INST]`".
# The BOS token is added by the tokenizer, so it is not written literally here.
LLAMA_INSTRUCT_WRAPPER = "[INST] {prompt} [/INST]"


def parse_args(argv=None):
    p = argparse.ArgumentParser(description=__doc__.splitlines()[0])
    p.add_argument("--ragtruth-dir", type=Path, required=True)
    p.add_argument("--output", type=Path, required=True)
    p.add_argument("--report", type=Path, default=None, help="Defaults to <output>.report.json")
    p.add_argument(
        "--generator-model",
        default="llama-2-7b-chat",
        help="Keep only responses from this generator; it must match the scoring model.",
    )
    p.add_argument(
        "--tokenizer",
        default="NousResearch/Llama-2-7b-chat-hf",
        help=(
            "Tokenizer of the scoring model. The official meta-llama repo is "
            "gated; NousResearch mirrors it byte-for-byte."
        ),
    )
    p.add_argument(
        "--scoring-model-id",
        default=None,
        help=(
            "HuggingFace id of the model that will score these rows. Written to "
            "metadata.generator_model so the replay guard can verify that the "
            "scoring model IS the generating model. Defaults to --tokenizer. "
            "RAGTruth's own label is preserved in extra.ragtruth_generator."
        ),
    )
    p.add_argument(
        "--splits",
        nargs="*",
        default=["train", "test"],
        help="RAGTruth splits to convert.",
    )
    p.add_argument(
        "--cross-check",
        type=Path,
        default=None,
        help="Optional teammate-normalised JSONL to verify our spans against.",
    )
    return p.parse_args(argv)


def token_labels_from_spans(offsets, spans, answer_len):
    """One label per token; a token is H if it overlaps any gold span.

    Overlap rule ``max(tok_start, span_start) < min(tok_end, span_end)``, the
    same half-open convention RAGTruth's own annotation tooling uses. Zero-width
    tokens (some tokenizers emit them for specials) can never overlap and stay
    grounded.
    """
    labels = [0] * len(offsets)
    covered = []
    for span in spans:
        s, e = int(span["start"]), int(span["end"])
        if s >= e or s < 0 or e > answer_len:
            covered.append(0)
            continue
        hit = 0
        for index, (ts, te) in enumerate(offsets):
            if ts >= te:
                continue
            if max(ts, s) < min(te, e):
                labels[index] = 1
                hit += 1
        covered.append(hit)
    return labels, covered


def build_row(record, source, tokenizer, generator_model, scoring_model_id):
    answer = record["response"]
    encoded = tokenizer(answer, add_special_tokens=False, return_offsets_mapping=True)
    token_ids = list(encoded["input_ids"])
    offsets = [tuple(o) for o in encoded["offset_mapping"]]
    if not token_ids:
        return None, "empty_answer"

    spans = [
        {"start": int(s["start"]), "end": int(s["end"]), "text": s.get("text")}
        for s in (record.get("labels") or [])
    ]
    # Drop RAGTruth's `meta`/`label_type` from the span itself (our schema has no
    # slot for it) but keep the taxonomy in metadata so the error types survive.
    span_types = [s.get("label_type") for s in (record.get("labels") or [])]

    labels, covered = token_labels_from_spans(offsets, spans, len(answer))
    t_star = next((i for i, v in enumerate(labels) if v == 1), None)

    prompt = LLAMA_INSTRUCT_WRAPPER.format(prompt=source["prompt"])
    row = {
        "id": f"ragtruth_{record['id']}",
        "prompt": prompt,
        "answer": answer,
        "labels": labels,
        "hallucinated_spans": spans,
        "metadata": {
            "source": "ragtruth",
            "dataset": "ParticleMedia/RAGTruth",
            "split": record["split"],
            # The id the replay guard checks. RAGTruth generated these with
            # Meta's llama-2-7b-chat; we score with the same weights, so this
            # records the repo we actually load rather than RAGTruth's label.
            "generator_model": scoring_model_id,
            "generated_token_ids": token_ids,
            "generated_tokens": tokenizer.convert_ids_to_tokens(token_ids),
            "t_star_index": t_star,
            "extra": {
                "ragtruth_generator": generator_model,
                "ragtruth_response_id": record["id"],
                "ragtruth_source_id": record["source_id"],
                "task_type": source["task_type"],
                "upstream_source": source.get("source"),
                "temperature": record.get("temperature"),
                "quality": record.get("quality"),
                "hallucination_types": span_types,
                "raw_prompt": source["prompt"],
                "prompt_wrapper": "llama_inst_v1",
                "split_group": f"ragtruth_source_{record['source_id']}",
            },
        },
    }
    return row, {"covered": covered, "n_spans": len(spans), "n_tokens": len(token_ids)}


def main(argv=None):
    args = parse_args(argv)
    from transformers import AutoTokenizer

    tokenizer = AutoTokenizer.from_pretrained(args.tokenizer)
    scoring_model_id = args.scoring_model_id or args.tokenizer
    if not tokenizer.is_fast:
        raise SystemExit("a fast tokenizer is required for character offsets")

    rt = args.ragtruth_dir
    sources = {}
    with (rt / "source_info.jsonl").open(encoding="utf-8") as f:
        for line in f:
            s = json.loads(line)
            sources[s["source_id"]] = s

    rows, stats = [], []
    skipped = collections.Counter()
    task_types = collections.Counter()
    splits = collections.Counter()
    with (rt / "response.jsonl").open(encoding="utf-8") as f:
        for line in f:
            rec = json.loads(line)
            if rec["model"] != args.generator_model:
                skipped["other_generator"] += 1
                continue
            if rec["split"] not in args.splits:
                skipped["other_split"] += 1
                continue
            source = sources.get(rec["source_id"])
            if source is None:
                skipped["missing_source"] += 1
                continue
            row, stat = build_row(
                rec, source, tokenizer, args.generator_model, scoring_model_id
            )
            if row is None:
                skipped[stat] += 1
                continue
            rows.append(row)
            stats.append(stat)
            task_types[source["task_type"]] += 1
            splits[rec["split"]] += 1

    if not rows:
        raise SystemExit(f"no rows matched generator {args.generator_model!r}")

    args.output.parent.mkdir(parents=True, exist_ok=True)
    with args.output.open("w", encoding="utf-8") as f:
        for row in rows:
            f.write(json.dumps(row, ensure_ascii=False) + "\n")

    total_tokens = sum(s["n_tokens"] for s in stats)
    h_tokens = sum(sum(1 for v in r["labels"] if v == 1) for r in rows)
    all_spans = sum(s["n_spans"] for s in stats)
    lost_spans = sum(1 for s in stats for c in s["covered"] if c == 0)
    rows_with_h = sum(1 for r in rows if any(v == 1 for v in r["labels"]))

    report = {
        "output": str(args.output),
        "output_sha256": hashlib.sha256(args.output.read_bytes()).hexdigest(),
        "generator_model": args.generator_model,
        "tokenizer": args.tokenizer,
        "scoring_model_id": scoring_model_id,
        "prompt_wrapper": LLAMA_INSTRUCT_WRAPPER,
        "rows": len(rows),
        "splits": dict(splits),
        "task_types": dict(task_types),
        "answer_tokens": total_tokens,
        "hallucinated_tokens": h_tokens,
        "hallucinated_token_rate": h_tokens / max(total_tokens, 1),
        "rows_with_hallucination": rows_with_h,
        "gold_spans": all_spans,
        "spans_covering_zero_tokens": lost_spans,
        "span_survival_rate": 1.0 - lost_spans / max(all_spans, 1),
        "skipped": dict(skipped),
    }

    if args.cross_check and args.cross_check.exists():
        report["cross_check"] = cross_check(args.cross_check, rows, args.generator_model)

    report_path = args.report or args.output.with_suffix(args.output.suffix + ".report.json")
    report_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")

    print(f"wrote {len(rows):,} rows -> {args.output}")
    print(f"  answer tokens {total_tokens:,} | H {h_tokens:,} ({report['hallucinated_token_rate']:.2%})")
    print(f"  rows with H {rows_with_h:,} | task types {dict(task_types)}")
    print(f"  gold spans {all_spans:,} | covering zero tokens {lost_spans} "
          f"(survival {report['span_survival_rate']:.4%})")
    if "cross_check" in report:
        print(f"  cross-check: {report['cross_check']}")
    print(f"wrote report -> {report_path}")
    return 0


def cross_check(path: Path, rows, generator_model):
    """Compare our character spans against a teammate's normalisation.

    Two independent normalisations agreeing on spans is real evidence the
    conversion is right; it is not a substitute for the coverage report above.
    """
    theirs = {}
    with path.open(encoding="utf-8") as f:
        for line in f:
            r = json.loads(line)
            if r.get("model") != generator_model:
                continue
            key = str(r.get("id"))
            theirs[key] = {(int(s["start"]), int(s["end"])) for s in (r.get("hallucinated_spans") or [])}
    matched = agreed = 0
    for row in rows:
        key = str(row["metadata"]["extra"]["ragtruth_response_id"])
        if key not in theirs:
            continue
        matched += 1
        ours = {(s["start"], s["end"]) for s in row["hallucinated_spans"]}
        if ours == theirs[key]:
            agreed += 1
    return {
        "rows_matched_by_id": matched,
        "rows_with_identical_spans": agreed,
        "agreement_rate": agreed / max(matched, 1),
        "rows_only_in_ours": len(rows) - matched,
    }


if __name__ == "__main__":
    raise SystemExit(main())