-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcorpus_batch_qa.py
More file actions
240 lines (215 loc) · 8.32 KB
/
Copy pathcorpus_batch_qa.py
File metadata and controls
240 lines (215 loc) · 8.32 KB
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
"""
Multi-sample single-QA run with a **larger policy corpus**, **checkpoints**, and **JSONL export**.
This script is the place for “production-ish” ergonomics that we keep out of
``minimal_pipeline.py``: resume from disk, optional Hub push, concurrent sample
generation with incremental ``data/train.jsonl`` writes (crash-safe append).
Layout under ``--output-dir``::
<output-dir>/
opensimula/ # manifest + taxonomy + strategy + typed run_config (OpenSimulaRunConfig)
data/
train.jsonl # one JSON object per line (accepted DataPointRecord only)
Requires: GEMINI_API_KEY. Optional: HF_TOKEN + ``--push-hf`` for upload after save.
"""
from __future__ import annotations
import argparse
import asyncio
import os
import random
import sys
from pathlib import Path
from tqdm.auto import tqdm
from afterimage.providers import InMemoryDocumentProvider, LLMFactory
from afterimage.simula import (
Checkpointer,
OpenSimula,
OpenSimulaRunConfig,
append_datapoints_jsonl,
configure_example_console,
load_checkpoint,
)
configure_example_console()
INSTRUCTION_Y = """\
You are generating synthetic **security and compliance Q&A** for employees.
Ground every answer in the policy excerpts: cite concrete procedures, channels, or
classification rules that appear in the text. Do not invent statutes, vendors, or
tools not implied by the excerpts. Question ≤140 words; answer ≤200 words; neutral
professional tone.\
"""
# Slightly larger synthetic “corpus” (still static strings—swap for files if you like).
CORPUS_EXCERPTS = [
"""**Acceptable use.** Company devices and accounts may be monitored. Users must not
disable EDR, must use MFA for remote access, and must report suspected phishing within
one hour to the security mailbox. Customer PII must not be stored on personal cloud drives.\
""",
"""**Classification.** Restricted data includes credentials, live customer PII, and
unreleased financials. Restricted data must use approved encrypted channels only.
Managers must complete annual ransomware tabletops.\
""",
"""**Access lifecycle.** Contractors receive least-privilege roles; access is revoked
within 24 hours of offboarding. Shared mailboxes require documented owners and quarterly
access reviews.\
""",
"""**Incidents.** P1/P2 incidents page the SOC on-call immediately; P3/P4 follow the
next-business-day queue. Evidence preservation steps must not tip off suspected insiders.\
""",
"""**Third parties.** Vendors with access to customer data must sign the standard DPA
and provide SOC2 or equivalent annually. Exceptions require CISO approval with compensating
controls documented.\
""",
"""**Secure development.** Production secrets never live in git. CI must run SAST on
default branches; critical findings block release until waived by security engineering with
an expiry date.\
""",
]
MODEL_NAME = "gemini-2.5-flash"
OPEN_SIMULA_TEMPERATURE = 0.4
TARGET_DEPTH_D = 2
PROPOSAL_N = 3
META_PROMPT_K = 6
COMPLEXIFY_C = 0.28
MAX_FACTORS = 4
MAX_CHILDREN_PER_NODE = 8
MAX_FRONTIER_PER_DEPTH = 12
def _parse_args() -> argparse.Namespace:
p = argparse.ArgumentParser(description=__doc__)
p.add_argument(
"--output-dir",
type=Path,
default=Path("outputs/simula_corpus_batch"),
help="Run directory (opensimula/ + data/train.jsonl).",
)
p.add_argument(
"--num-samples",
type=int,
default=4,
help="Number of independent (mix, meta, generate) draws.",
)
p.add_argument(
"--resume",
action="store_true",
help="Load opensimula/ from output-dir; skip taxonomy and strategy inference.",
)
p.add_argument(
"--max-concurrency",
type=int,
default=2,
help="Max concurrent sample pipelines (each does mix + meta + critic loop).",
)
p.add_argument("--seed", type=int, default=42, help="RNG seed for mix/meta subsampling.")
p.add_argument(
"--push-hf",
default=None,
metavar="REPO_ID",
help="After writing opensimula/, upload to this Hub dataset repo (needs HF_TOKEN).",
)
return p.parse_args()
async def main() -> None:
args = _parse_args()
out = args.output_dir.resolve()
data_dir = out / "data"
jsonl_path = data_dir / "train.jsonl"
api_key = os.environ.get("GEMINI_API_KEY")
if not api_key:
print("Set GEMINI_API_KEY to run this example.", file=sys.stderr)
sys.exit(1)
llm = LLMFactory.create(
provider="gemini",
model_name=MODEL_NAME,
api_key=api_key,
)
docs = InMemoryDocumentProvider(CORPUS_EXCERPTS)
sim = OpenSimula(llm, temperature=OPEN_SIMULA_TEMPERATURE)
rng = random.Random(args.seed)
if args.resume:
print(f"Resume: loading checkpoint from {out / 'opensimula'}\n", flush=True)
ckpt = load_checkpoint(out)
bundle = ckpt.bundle
spec = ckpt.sampling_strategy
if spec is None:
print("No sampling_strategy.json; inferring strategies…", flush=True)
spec = await sim.infer_strategies(bundle)
else:
out.mkdir(parents=True, exist_ok=True)
data_dir.mkdir(parents=True, exist_ok=True)
print("Building taxonomy (multi-document corpus)…\n", flush=True)
bundle = await sim.build_taxonomy(
INSTRUCTION_Y,
document_provider=docs,
target_depth_D=TARGET_DEPTH_D,
proposal_N=PROPOSAL_N,
max_factors=MAX_FACTORS,
max_children_per_node=MAX_CHILDREN_PER_NODE,
max_frontier_per_depth=MAX_FRONTIER_PER_DEPTH,
show_progress=True,
)
print()
OpenSimula.validate_taxonomy_bundle(bundle)
spec = await sim.infer_strategies(bundle)
run_cfg = OpenSimulaRunConfig(
name="corpus_batch_qa",
description="Multi-sample single-QA with policy corpus (examples/simula).",
model=MODEL_NAME,
temperature=OPEN_SIMULA_TEMPERATURE,
target_depth_D=TARGET_DEPTH_D,
proposal_N=PROPOSAL_N,
meta_prompt_K=META_PROMPT_K,
complexify_c=COMPLEXIFY_C,
max_factors=MAX_FACTORS,
max_children_per_node=MAX_CHILDREN_PER_NODE,
max_frontier_per_depth=MAX_FRONTIER_PER_DEPTH,
num_samples=args.num_samples,
max_concurrency=args.max_concurrency,
seed=args.seed,
data_jsonl=str(jsonl_path.relative_to(out)),
corpus_excerpt_count=len(CORPUS_EXCERPTS),
)
with Checkpointer(out) as cp:
bundle.save(cp)
spec.save(cp)
cp.write_run_config(run_cfg)
assert cp.manifest is not None
print(
f"Checkpoint written ({cp.manifest.format} {cp.manifest.format_version}) → {out / 'opensimula'}\n",
flush=True,
)
if args.push_hf:
url = cp.push_to_hub(args.push_hf)
print(f"Pushed to Hub: {url}\n", flush=True)
if args.num_samples <= 0:
print("Nothing to generate (--num-samples <= 0).", flush=True)
return
accepted = 0
pbar = tqdm(
total=args.num_samples,
desc="Generating samples",
unit="sample",
dynamic_ncols=True,
)
async for _idx, rec in sim.aiter_single_qa_samples(
instruction_y=bundle.instruction_y,
bundle=bundle,
spec=spec,
n=args.num_samples,
K=META_PROMPT_K,
complexify_c=COMPLEXIFY_C,
sequential=False,
max_concurrency=args.max_concurrency,
rng=rng,
):
if rec is not None:
append_datapoints_jsonl(jsonl_path, [rec])
accepted += 1
pbar.set_postfix_str(f"accepted={accepted}")
pbar.update(1)
pbar.close()
print(
f"\nDone: {accepted}/{args.num_samples} accepted rows appended to {jsonl_path}",
flush=True,
)
if not args.resume and args.push_hf is None:
print(
"Tip: re-run with --resume to skip taxonomy, or --push-hf org/repo after a save.",
flush=True,
)
if __name__ == "__main__":
asyncio.run(main())