67 lines
2.6 KiB
Python
67 lines
2.6 KiB
Python
"""确定性长上下文语料生成器(dim3 专用)。
|
||
|
||
用法:
|
||
python gen_longctx.py # 打印语料(stdout)
|
||
python gen_longctx.py --write # 写入同目录 longctx-corpus.md
|
||
|
||
特性:
|
||
- 确定性:同样参数永远生成同样内容(无随机、无时间)
|
||
- 约 50KB:由固定填充段落重复拼接而成
|
||
- 位置锚定:3 处针(needle)分别落于约 6% / 50% / 92% 处
|
||
- 3 处诱饵(decoy)用于抗干扰测试
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import sys
|
||
from pathlib import Path
|
||
|
||
FILLER = (
|
||
"运维知识库条目:例行巡检包含磁盘水位、服务端口存活、日志轮转三项检查,"
|
||
"巡检结果登记到值班表。若发现异常,按照应急预案升级处理,并在复盘文档中记录。"
|
||
"本条为通用条目,不包含具体系统参数。\n"
|
||
)
|
||
|
||
NEEDLES = {
|
||
# (目标位置比例, 文本)
|
||
0.06: "【运维备注】内部代号 XN-7742 的灰度发布窗口固定为每周三 02:00–04:00(UTC+8),其余时段禁止对该代号执行发布操作。\n",
|
||
0.50: "【缓存记录】缓存 TTL 演进:初版为 900 秒;2026-03 缓存穿透事故复盘后,下调为 300 秒。\n",
|
||
0.92: "【指标改名】原“北极星指标”经评审更名为 NSM-7,定义保持不变:周活跃答题用户的次日留存率。\n",
|
||
}
|
||
|
||
DECOYS = {
|
||
0.15: "【旧报表注】历史报表中出现的 NSM-3 指“七日留存率”,与 NSM-7 无对应关系,勿混用。\n",
|
||
0.20: "【已取消】早期排期表曾计划 XN-7742 于周四 01:00 执行,后因值班人力调整取消,以最新窗口为准。\n",
|
||
0.35: "【CDN 配置】静态资源 CDN TTL 为 86400 秒,属于边缘缓存策略,不受应用层缓存调整影响。\n",
|
||
}
|
||
|
||
TARGET_BYTES = 50 * 1024
|
||
|
||
|
||
def build() -> str:
|
||
total_chars = TARGET_BYTES # 中文按 UTF-8 3 字节,这里以字符近似;约 50KB 量级
|
||
filler_len = len(FILLER)
|
||
n = max(1, total_chars // filler_len)
|
||
parts: list[str] = []
|
||
for i in range(n):
|
||
parts.append(FILLER)
|
||
pos = (i + 1) / n
|
||
for ratio, text in {**NEEDLES, **DECOYS}.items():
|
||
if pos >= ratio and not any(t in "".join(parts) for t in [text]):
|
||
parts.append(text)
|
||
return "".join(parts)
|
||
|
||
|
||
def main() -> None:
|
||
corpus = build()
|
||
if "--write" in sys.argv:
|
||
out = Path(__file__).with_name("longctx-corpus.md")
|
||
out.write_text(corpus, encoding="utf-8")
|
||
print(f"written: {out} ({len(corpus.encode('utf-8'))} bytes)")
|
||
else:
|
||
sys.stdout.write(corpus)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|