Akahsizrr commited on
Commit
ac0bfca
·
verified ·
1 Parent(s): 38bc822

Upload alea/gen_jb3.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. alea/gen_jb3.py +305 -0
alea/gen_jb3.py ADDED
@@ -0,0 +1,305 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """gen_jb3: LONG-document reasoning — multi-section policies with nested
2
+ exceptions, judge-style verdicts, multi-constraint chains.
3
+
4
+ Hard tier needs 1-4k token docs where the answer depends on composing
5
+ several sections in order (base rule -> endorsement override ->
6
+ exception-to-endorsement -> global condition). These generators build
7
+ that structure deterministically so the correct answer is exact.
8
+ """
9
+ import argparse
10
+ import json
11
+ import random
12
+
13
+ rng = random.Random(31337)
14
+
15
+
16
+ def choice(state, instr, crit, correct_key, family):
17
+ keys = list(crit.keys())
18
+ tgt = [0.0] * len(keys)
19
+ tgt[keys.index(correct_key)] = 1.0
20
+ return {"state": state,
21
+ "questions": {"q0": {"type": "choice", "instructions": instr,
22
+ "criteria": crit}},
23
+ "targets": {"q0": tgt}, "family": family}
24
+
25
+
26
+ def noul(state, instr, val, family):
27
+ return {"state": state,
28
+ "questions": {"q0": {"type": "noul", "instructions": instr}},
29
+ "targets": {"q0": float(val)}, "family": family}
30
+
31
+
32
+ # ---------- long insurance/loan/policy docs ----------
33
+
34
+ def gen_longpolicy(n):
35
+ """Multi-section policy with exception chains -> adjudication."""
36
+ out = []
37
+ for _ in range(n):
38
+ # --- document skeleton ---
39
+ cov_window = rng.choice([30, 60, 90])
40
+ late_days = rng.choice([30, 45])
41
+ ded = rng.choice([250, 500])
42
+ cap = rng.choice([2000, 5000, 10000])
43
+ region_ok = rng.choice([True, False])
44
+ secs = [
45
+ "MASTER SERVICES AND PROTECTION AGREEMENT",
46
+ "Document ref: MPA-%d" % rng.randint(1000, 9999),
47
+ "In force: %s through %s." % (
48
+ rng.choice(["01 Jan 2024", "01 Feb 2024"]),
49
+ rng.choice(["31 Dec 2025", "31 Dec 2026"])),
50
+ "",
51
+ "1. DEFINITIONS",
52
+ " 1.1 'Covered Event' means an event listed in Section 3.",
53
+ " 1.2 'Territory' means the coverage area in Schedule A.",
54
+ " 1.3 'Prompt Notice' means notice within %d days." % late_days,
55
+ "",
56
+ "2. SCOPE",
57
+ " 2.1 This agreement applies to claims arising during the "
58
+ "in-force period.",
59
+ " 2.2 Claims arising outside the Territory are excluded "
60
+ "absent a Territory Endorsement.",
61
+ "",
62
+ "3. COVERED EVENTS",
63
+ " 3.1 Accidental damage reported within %d days." % cov_window,
64
+ " 3.2 Theft with a filed police report.",
65
+ " 3.3 Mechanical failure not caused by misuse.",
66
+ "",
67
+ "4. EXCLUSIONS",
68
+ " 4.1 Cosmetic damage is excluded.",
69
+ " 4.2 Events arising from unauthorized modification are "
70
+ "excluded.",
71
+ " 4.3 Events occurring before the in-force date are excluded.",
72
+ "",
73
+ "5. CONDITIONS PRECEDENT",
74
+ " 5.1 Prompt Notice is a condition precedent to coverage.",
75
+ " 5.2 A deductible of $%d applies per claim." % ded,
76
+ "",
77
+ "6. ENDORSEMENTS",
78
+ ]
79
+ # endorsements create the exception chains
80
+ e_theft_no_police = rng.random() < 0.4
81
+ e_territory = rng.random() < 0.5
82
+ e_mod = rng.random() < 0.35
83
+ if e_theft_no_police:
84
+ secs.append(" E-T1: theft claims without a police report are "
85
+ "covered up to $%d if reported with two forms of "
86
+ "proof of ownership." % cap)
87
+ if e_territory:
88
+ secs.append(" E-TR: the Territory exclusion in 2.2 is waived "
89
+ "for losses occurring within 50 km of the Territory "
90
+ "boundary.")
91
+ if e_mod:
92
+ secs.append(" E-M1: Section 4.2 does not apply where the "
93
+ "modification was performed by an authorized "
94
+ "service center.")
95
+ secs += ["",
96
+ "7. CLAIMS PROCEDURE",
97
+ " 7.1 All claims must be filed via the portal.",
98
+ " 7.2 The insurer responds within 15 business days.",
99
+ "",
100
+ "8. GENERAL",
101
+ " 8.1 This agreement is governed by the laws of the "
102
+ "issuing jurisdiction.",
103
+ " 8.2 Nothing herein limits statutory rights."]
104
+
105
+ # --- the claim ---
106
+ event = rng.choice(["accident", "theft", "mechanical"])
107
+ reported = rng.choice(["prompt", "late"])
108
+ outside = rng.random() < 0.4
109
+ police = rng.random() < 0.6
110
+ modded = rng.random() < 0.35
111
+ auth_mod = modded and (rng.random() < 0.5)
112
+
113
+ facts = ["Loss date: %s 2025 (in-force period)." %
114
+ rng.choice(["Apr 3", "Jul 19", "Oct 8"]),
115
+ "Event type: %s." % event]
116
+ if event == "accident":
117
+ facts.append("Damage is structural, not cosmetic.")
118
+ facts.append("Reported %d days after the event." %
119
+ (rng.randint(1, cov_window - 3) if reported == "prompt"
120
+ else cov_window + rng.randint(3, 20)))
121
+ if event == "theft":
122
+ facts.append("Police report %s." %
123
+ ("filed same day" if police else "was NOT filed"))
124
+ if e_theft_no_police and not police:
125
+ facts.append("Claimant supplied %s." %
126
+ rng.choice(["two proofs of ownership",
127
+ "one proof of ownership"]))
128
+ if event == "mechanical":
129
+ facts.append("Unit %s." %
130
+ ("was modified by an authorized service center"
131
+ if auth_mod else
132
+ "has an unauthorized third-party modification"
133
+ if modded else "is unmodified"))
134
+ facts.append("Reported %d days after the event." %
135
+ rng.randint(1, cov_window))
136
+ if outside:
137
+ facts.append("Loss occurred %d km outside the Territory "
138
+ "boundary." % rng.choice([20, 80]))
139
+ if reported == "late" and event != "accident":
140
+ facts.append("Claim filed %d days after the loss." %
141
+ (late_days + rng.randint(1, 15)))
142
+ elif event != "accident":
143
+ facts.append("Claim filed %d days after the loss." %
144
+ rng.randint(1, late_days - 2))
145
+
146
+ # --- adjudicate ---
147
+ if event == "accident" and reported == "late":
148
+ correct = "deny_late"
149
+ elif event == "accident":
150
+ correct = "pay"
151
+ elif event == "theft":
152
+ if police:
153
+ correct = "pay"
154
+ elif e_theft_no_police:
155
+ two_proofs = any("two proofs" in f for f in facts)
156
+ correct = "pay_capped" if two_proofs else "deny_condition"
157
+ else:
158
+ correct = "deny_condition"
159
+ else: # mechanical
160
+ if modded and auth_mod and e_mod:
161
+ correct = "pay"
162
+ elif modded and auth_mod and not e_mod:
163
+ correct = "deny_excluded"
164
+ elif modded and not auth_mod:
165
+ correct = "deny_excluded"
166
+ else:
167
+ correct = "pay"
168
+ if correct == "pay" and outside:
169
+ near = "20 km" in " ".join(facts)
170
+ correct = "pay" if (e_territory and near) else "deny_excluded"
171
+ elif outside and correct.startswith("pay"):
172
+ near = "20 km" in " ".join(facts)
173
+ correct = "pay" if (e_territory and near) else "deny_excluded"
174
+ if reported == "late" and event != "accident" and correct == "pay":
175
+ correct = "deny_late"
176
+
177
+ crit = {
178
+ "pay": "Pay the claim less the deductible",
179
+ "pay_capped": "Pay subject to the endorsement cap",
180
+ "deny_late": "Deny: notice condition not met",
181
+ "deny_excluded": "Deny: exclusion applies",
182
+ "deny_condition": "Deny: a coverage condition was not met",
183
+ }
184
+ st = "\n".join(secs) + "\n\nCLAIM:\n" + "\n".join(facts)
185
+ out.append(choice(st, "Adjudicate the claim. Apply sections in "
186
+ "order; endorsements modify earlier sections; "
187
+ "conditions precedent bind first.", crit,
188
+ correct, "jb3_longpolicy"))
189
+ return out
190
+
191
+
192
+ # ---------- judge: verify a verdict ----------
193
+
194
+ def gen_judge(n):
195
+ """Doc + candidate verdict -> agree/disagree (judge_hard style)."""
196
+ out = []
197
+ for _ in range(n):
198
+ a = rng.randint(100, 900)
199
+ b = rng.randint(100, 900)
200
+ c = rng.randint(100, 900)
201
+ col = rng.choice(["revenue", "cost", "units"])
202
+ rec = {"revenue": a, "cost": b, "units": c}
203
+ rec["margin"] = round((a - b) / a, 3)
204
+ st = ("Record:\n revenue: %d\n cost: %d\n units: %d\n margin: "
205
+ "%.3f\n region: %s\n quarter: Q%d" %
206
+ (a, b, c, rec["margin"], rng.choice(["EU", "NA", "APAC"]),
207
+ rng.randint(1, 4)))
208
+ # candidate verdict — correct half the time
209
+ kind = rng.choice(["margin", "sum", "max"])
210
+ if kind == "margin":
211
+ true_v = rec["margin"]
212
+ wrong_v = round((a - b) / b, 3)
213
+ claim = "the margin is %.3f" % (true_v if rng.random() < 0.5
214
+ else wrong_v)
215
+ ok = ("%.3f" % true_v) in claim
216
+ elif kind == "sum":
217
+ true_v = a + b
218
+ wrong_v = a + b + rng.choice([-1, 1]) * rng.randint(5, 50)
219
+ claim = "revenue plus cost equals %d" % (true_v
220
+ if rng.random() < 0.5 else wrong_v)
221
+ ok = str(true_v) in claim
222
+ else:
223
+ big = max(rec.values())
224
+ wrong = min(rec.values())
225
+ claim = "the largest field is %d" % (big if rng.random() < 0.5
226
+ else wrong)
227
+ ok = str(big) in claim
228
+ st += "\n\nAnalyst verdict: \"%s\"." % claim
229
+ out.append(choice(st, "Judge the verdict: does it follow from the "
230
+ "record?",
231
+ {"correct": "Verdict is correct",
232
+ "incorrect": "Verdict is incorrect"},
233
+ "correct" if ok else "incorrect", "jb3_judge"))
234
+ return out
235
+
236
+
237
+ # ---------- multi-hop temporal chains ----------
238
+
239
+ def gen_temporal_chain(n):
240
+ """3-4 step date/shift chains — multi_hop style."""
241
+ import datetime as dt
242
+ out = []
243
+ for _ in range(n):
244
+ y = rng.choice([2024, 2025])
245
+ m, d = rng.randint(1, 12), rng.randint(1, 28)
246
+ try:
247
+ base = dt.date(y, m, d)
248
+ except ValueError:
249
+ continue
250
+ hops = [rng.randint(3, 14) for _ in range(3)]
251
+ wds = [h * rng.choice([1, 1]) for h in hops]
252
+ steps_txt = []
253
+ cur = base
254
+ for i, h in enumerate(hops):
255
+ steps_txt.append("Step %d: add %d days." % (i + 1, h))
256
+ cur += dt.timedelta(days=h)
257
+ # ask for final weekday or date
258
+ ask = rng.choice(["weekday", "date"])
259
+ st = ("Start date: %s.\n%s\n" % (base.strftime("%A, %B %d, %Y"),
260
+ "\n".join(steps_txt)))
261
+ if ask == "weekday":
262
+ opts = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
263
+ "Saturday", "Sunday"]
264
+ rng.shuffle(opts)
265
+ crit = {o: "final day is %s" % o for o in opts}
266
+ out.append(choice(st, "Apply each step in order and choose the "
267
+ "final weekday.", crit,
268
+ cur.strftime("%A"), "jb3_temporal_chain"))
269
+ else:
270
+ opts = [cur.strftime("%B %d")]
271
+ for _ in range(3):
272
+ w = cur + dt.timedelta(days=rng.choice([-3, -2, -1, 1, 2, 3]))
273
+ opts.append(w.strftime("%B %d"))
274
+ order = list(range(4)); rng.shuffle(order)
275
+ shuffled = [opts[j] for j in order]
276
+ crit = {"d%d" % i: shuffled[i] for i in range(4)}
277
+ out.append(choice(st, "Apply each step in order and choose the "
278
+ "final date.", crit,
279
+ "d%d" % order.index(0), "jb3_temporal_chain"))
280
+ return out
281
+
282
+
283
+ GENS = {"longpolicy": gen_longpolicy, "judge": gen_judge,
284
+ "temporal_chain": gen_temporal_chain}
285
+
286
+
287
+ def main():
288
+ ap = argparse.ArgumentParser()
289
+ ap.add_argument("--out", default="data/jb3.jsonl")
290
+ ap.add_argument("--per", type=int, default=8000)
291
+ args = ap.parse_args()
292
+ rows = []
293
+ for name, fn in GENS.items():
294
+ got = fn(args.per)
295
+ print(name, len(got))
296
+ rows += got
297
+ rng.shuffle(rows)
298
+ with open(args.out, "w") as f:
299
+ for r in rows:
300
+ f.write(json.dumps(r, ensure_ascii=False) + "\n")
301
+ print("TOTAL", len(rows), "->", args.out)
302
+
303
+
304
+ if __name__ == "__main__":
305
+ main()