File size: 4,380 Bytes
c9faf30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Structured data emitted inside a chat turn.

    python tools/gen_structured.py --n 700

`pool/structured/repos.jsonl` holds real YAML, JSON, TOML and SQL as files.
That covers the tokens but not the situation the target model is actually in: an
agent asked for a config answers *inside* an assistant turn, so the structured
tokens sit next to chat special tokens rather than at the start of a document.

The structured payload in every conversation here is a real file from the pool,
byte for byte, keeping its upstream licence.  Only the request and the sentence
introducing it are templated.
"""

from __future__ import annotations

import argparse
import os
import random
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)

import poollib as P

SEED = 20260810
GENERATOR_VERSION = "gen_structured/1.0"

FENCE = {"yaml": "yaml", "json": "json", "toml": "toml", "sql": "sql"}

ASKS = [
    ("Show me the {lang} for `{path}` in {repo} — I want to copy it into a new project.",
     "Here is `{path}` as it stands in {repo}:"),
    ("What does `{path}` configure in {repo}? Paste it and walk me through it.",
     "`{path}` in {repo}, in full:"),
    ("I need the {lang} from `{path}`. Don't summarise it, I need the exact file.",
     "Verbatim, `{path}`:"),
    ("Reproduce `{path}` from {repo} so I can diff it against mine.",
     "`{path}` at the current revision:"),
]

TAILS = [
    "The parts most projects need to change are the paths and the version pins; "
    "everything else is defaults.",
    "Note the ordering matters here — entries later in the file override earlier ones.",
    "If you copy this, check the pinned versions first; they are the part that goes "
    "stale fastest.",
    "Nothing in here is project-specific except the names, so it ports cleanly.",
]


def main() -> int:
    ap = argparse.ArgumentParser()
    ap.add_argument("--n", type=int, default=700)
    ap.add_argument("--seed", type=int, default=SEED)
    ap.add_argument("--max-chars", type=int, default=9000,
                    help="skip payloads longer than this so one file cannot "
                         "swallow the whole category")
    args = ap.parse_args()

    rng = random.Random(args.seed)
    src = [d for d in P.load_pool(categories=("structured",))
           if d.get("render") == "text" and len(d["text"]) <= args.max_chars]
    src.sort(key=lambda d: d["id"])
    if not src:
        raise SystemExit("no structured source documents in the pool")
    print(f"  {len(src)} structured source files eligible")

    records = []
    for i in range(min(args.n, len(src))):
        d = src[(i * 4409) % len(src)]
        lang = d["lang"]
        ask, intro = ASKS[i % len(ASKS)]
        fields = {"lang": lang.upper() if lang in ("sql", "json") else lang,
                  "path": d["path"], "repo": d["source"]}
        body = f"```{FENCE.get(lang, lang)}\n{d['text']}\n```"
        msgs = [
            {"role": "user", "content": ask.format(**fields)},
            {"role": "assistant",
             "content": intro.format(**fields) + "\n\n" + body + "\n\n" +
                        TAILS[i % len(TAILS)]},
        ]
        records.append(P.Record(
            category="structured", domain="structured",
            source=f"synthetic/structured-in-chat:{d['source']}",
            license=d["license"], license_url=d.get("license_url", ""),
            path=f"structured/chat/{d['source']}/{d['path']}", lang=lang,
            origin="synth_structured_chat", messages=msgs,
            reasoning_strength="low", render="chat", synthetic=True,
            provenance={"generator": GENERATOR_VERSION, "seed": args.seed,
                        "payload_from": d["id"],
                        "payload_upstream": d.get("provenance", {}).get("upstream", ""),
                        "note": "the fenced payload is a real repository file, "
                                "reproduced byte for byte under its own licence; "
                                "only the request and framing sentences are templated"},
        ))

    n = P.write_jsonl(os.path.join(P.POOL_ROOT, "structured", "synthetic",
                                   "structured-in-chat.jsonl"), records)
    print(f"  pool/structured/synthetic/structured-in-chat.jsonl  {n} conversations")
    return 0


if __name__ == "__main__":
    sys.exit(main())