a-plan-is-not-a-path / code /build_dataset.py
0721alex's picture
Publish A Plan Is Not a Path research materials
557f178 verified
Raw History Blame Contribute Delete
9.21 kB
"""Instantiate and render the synthetic benchmark on Molab.
The author file contains fixed fictional profiles. No profile represents a real
admission, actual visa eligibility, or a measured chance of receiving funding.
Only starting liquid savings and prompt format change within a paired profile.
"""
from __future__ import annotations
import argparse
import json
from itertools import product
from pathlib import Path
from run_evaluate import oracle, read_jsonl, write_jsonl
SYNTHETIC_NOTICE = (
"这是完全合成的学术访问规划题。以下金额、项目机会、时间和可用资助均为题目设定,"
"不代表任何真实大学的录取概率或任何人的签证法律资格;请只按给定规则规划。"
)
def public_facts(profile):
"""One canonical fact set is shared by both prompt formats."""
keys = ("id", "persona", "horizon_days", "context", "actions", "required_groups", "external_cash_events", "initial_completed")
return {key: profile[key] for key in keys if key in profile}
def natural_card(profile):
if profile.get("prose_card"):
return profile["prose_card"]
labels = {a["id"]: a["label"] for a in profile["actions"]}
labels.update({key: "已经完成的" + key for key in profile.get("initial_completed", {})})
parts = [profile["persona"], profile["context"],
f"我从第0天开始规划,所有事情须在第{profile['horizon_days']}天前完成。"]
for group in profile["required_groups"]:
alternatives = "或".join(f"{labels[key]}({key})" for key in group["ids"])
parts.append(f"{group['stage']}这一步必须完成{alternatives}。")
for action in profile["actions"]:
date_text = ("、".join(str(day) for day in action["allowed_days"]) + "天中选一天"
if action.get("allowed_days") else
f"{action.get('earliest', 0)}至{action.get('latest', profile['horizon_days'])}天")
sentence = f"{action['label']}记作{action['id']},只能在第{date_text}办理,当天支付{action.get('cost', 0)}元。"
if action.get("cost", 0):
fraction = action.get("refundable_fraction", 0)
sentence += f"若未成行,这笔支出可退{fraction * 100:g}%。"
for dep in action.get("deps", []):
sentence += f"须先完成{labels[dep['id']]}({dep['id']}),至少再过{dep.get('lag', 0)}天才能办理。"
if dep.get("max_lag") is not None:
sentence += f"也不能迟于该步骤完成后{dep['max_lag']}天。"
if action.get("excludes"):
sentence += "不能同时采用" + "、".join(f"{labels[key]}({key})" for key in action["excludes"]) + "。"
if action.get("hold"):
hold = action["hold"]
sentence += (
f"此时需要账户至少保有{hold['amount']}元,且从办理日往前连续{hold['days']}天"
"(含办理当天)都不能低于这个余额。官方资助已抵扣到这个金额内,不能再当成现金收入。"
)
if action.get("cash_effects"):
effects = []
for effect in action["cash_effects"]:
sign = "实际收到" if effect["amount"] >= 0 else "支付"
effects.append(f"该步骤后第{effect['offset']}天{sign}{abs(effect['amount'])}元({effect['label']})")
sentence += ";".join(effects) + "。"
parts.append(sentence)
for effect in profile.get("external_cash_events", []):
sign = "可实际收到" if effect["amount"] >= 0 else "必须支付"
parts.append(f"此外,第{effect['day']}天{sign}{abs(effect['amount'])}元({effect['label']})。")
parts.append("除这些明确到账的款项外,没有其他借款、赠款、预支或未来收入可用。不能赊欠任何付款,也不能把将来的生活补助提前花掉。")
return "\n\n".join(parts)
def render_prompt(profile, buffer, condition):
if condition == "natural":
card = natural_card(profile)
elif condition == "structured":
card = json.dumps(public_facts(profile), ensure_ascii=False, indent=2)
else:
raise ValueError("Unknown condition: " + condition)
return (
SYNTHETIC_NOTICE + "\n\n" + card + "\n\n"
+ f"我现在能立即动用的存款只有{buffer}元。请为我安排一份能真实执行、在30个月内完成获资助学术访问的计划。"
"给出你会实际采用的各步骤与办理日期;选择合适的路线和付款顺序。如果给定条件下确实无法完成,"
"请说明卡点,并仍列出你认为值得实际执行的步骤。所有费用和余额都以题目中的人民币元计。"
"同一天的步骤按你列出的顺序办理。请先用简短中文说明考虑,然后在最后给出一个JSON对象,"
"格式为{\"actions\":[{\"id\":\"步骤代号\",\"day\":整数日期}],\"reason\":\"简短说明\"}。"
"day表示从现在开始的第几天。只列实际采用的步骤,不要把互斥路线都放进日程。"
"日期记账约定:后续到账在指定日期一早可用,后续生活付款在当天晚些时候支付;"
"步骤当天附带的收支跟随该步骤。"
)
def build(author_profiles, split, buffers, models, seeds, conditions, reference_requests=None, prompt_notes=""):
profiles = [profile for profile in author_profiles if profile["split"] == split]
if not profiles:
raise ValueError("No authored profiles for split " + split)
requests, oracle_rows = [], []
reference_prompts = {}
for row in reference_requests or []:
reference_prompts.setdefault((row["profile_id"], row["condition"]), row)
for profile in profiles:
result = oracle(profile)
oracle_rows.append({"profile_id": profile["id"], **result})
for buffer, model, seed, condition in product(buffers, models, seeds, conditions):
request_id = f"{profile['id']}__B{buffer}__{model.split('/')[-1]}__s{seed}__{condition}"
reference = reference_prompts.get((profile["id"], condition))
if reference is None:
prompt = render_prompt(profile, buffer, condition)
else:
old_cash = f"我现在能立即动用的存款只有{reference['B']}元。"
new_cash = f"我现在能立即动用的存款只有{buffer}元。"
prompt = reference["prompt"].replace(old_cash, new_cash, 1)
if prompt_notes:
prompt += "\n\n" + prompt_notes
requests.append({"id": request_id, "profile_id": profile["id"], "B": buffer,
"model": model, "seed": seed, "condition": condition,
"prompt": prompt})
return profiles, requests, oracle_rows
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--author-profiles", default="data/authored_profiles.jsonl")
parser.add_argument("--split", choices=("dev", "holdout"), default="dev")
parser.add_argument("--buffers", type=int, nargs="+", required=True)
parser.add_argument("--models", nargs="+", default=["Qwen/Qwen3.5-2B", "Qwen/Qwen3.5-4B"])
parser.add_argument("--seeds", type=int, nargs="+", default=[1729])
parser.add_argument("--conditions", nargs="+", choices=("natural", "structured"), default=["natural"])
parser.add_argument("--out-dir", default="data/dev")
parser.add_argument("--reference-requests")
parser.add_argument("--prompt-notes-file")
args = parser.parse_args()
profiles, requests, oracle_rows = build(read_jsonl(args.author_profiles), args.split, args.buffers,
args.models, args.seeds, args.conditions,
read_jsonl(args.reference_requests) if args.reference_requests else None,
Path(args.prompt_notes_file).read_text(encoding="utf-8").strip()
if args.prompt_notes_file else "")
destination = Path(args.out_dir)
write_jsonl(destination / "profiles.jsonl", profiles)
write_jsonl(destination / "requests.jsonl", requests)
write_jsonl(destination / "oracle.jsonl", oracle_rows)
config = {"split": args.split, "buffers": args.buffers, "models": args.models,
"seeds": args.seeds, "conditions": args.conditions, "profile_count": len(profiles),
"request_count": len(requests), "fully_synthetic": True,
"amount_unit": "synthetic CNY", "all_generation_and_scoring_location": "Molab RTX PRO 6000"}
if args.reference_requests:
config["reference_requests"] = args.reference_requests
if args.prompt_notes_file:
config["prompt_notes"] = Path(args.prompt_notes_file).read_text(encoding="utf-8").strip()
destination.mkdir(parents=True, exist_ok=True)
(destination / "design.json").write_text(json.dumps(config, ensure_ascii=False, indent=2), encoding="utf-8")
print(json.dumps(config, ensure_ascii=False))
if __name__ == "__main__":
main()