File size: 3,099 Bytes
2a06dbe 58debbc 2a06dbe 58debbc 2a06dbe | 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 | #!/usr/bin/env python3
"""
์๋๋ฆฌ์ค ์์ฑ๊ธฐ ๊ณตํต ํฌํผ (๋ ์ด์ด ํ์ผ ์ถ๋ ฅ).
build_<scenario>_scenario.py๊ฐ TAXโbuild_intents/actions/behavior_intents๋ก ๋ง๋ ์ฐ์ถ์
engine ๋ ์ด์ด ํ์ผ์ ๊ธฐ๋กํ๋ค. L0๋ ์ ์ฒด ๊ต์ฒด, L3/L2๋ ํด๋น ํค๋ง ๊ต์ฒด(ํ์ ์น์
๋ณด์กด).
"""
from __future__ import annotations
import json
from pathlib import Path
def load_json(path: Path) -> dict:
return json.load(open(path, encoding="utf-8")) if path.exists() else {}
# ํ ๋
ธ๋๋ฅผ ์ธ๋ผ์ธ(ํ ์ค)์ผ๋ก ์ ์งํ ์ต๋ ํญ. ์ด๊ณผํ๋ฉด ์์๋ณ๋ก ํผ์น๋ค.
# worker-v3/engine/L1_feature.json์ "ํญ ๋จ์ ์ธ๋ผ์ธ" ์๋ง์ ์๋ ์ฌํํ๋ ๊ธฐ์ค.
_INLINE_WIDTH = 100
def compact_json(data, *, indent: int = 2, width: int = _INLINE_WIDTH) -> str:
"""
"ํญ(leaf) ๋จ์ ์ธ๋ผ์ธ" JSON ์ง๋ ฌํ.
ํ์ค indent=2๋ ์์ ๋ฐฐ์ดยทleaf ๊ฐ์ฒด๊น์ง ํ ์์์ฉ ํผ์ณ ์์ ํ์
์ด ์ด๋ ต๋ค.
์ด ํฌ๋งคํฐ๋ ๋
ธ๋๋ฅผ ๋จผ์ ํ ์ค(json.dumps)๋ก ๋ง๋ค์ด๋ณด๊ณ , width ์ดํ๋ฉด ์ธ๋ผ์ธ ์ ์ง,
์ด๊ณผํ๋ฉด dict/list์ ์์๋ณ๋ก ์ค๋ฐ๊ฟ ํผ์น๋ค. โ terms/steps/rule ๋งต ๊ฐ์ ๊ตฌ์กฐ๋
ํญ๋น ํ ์ค, linear[1,0]ยท์์ leaf ๊ฐ์ฒด๋ ํ ์ค๋ก ๋ชจ์ธ๋ค. (๊ฐ์ ํ์ค๊ณผ 1:1 ๋์ผ)
"""
def fmt(node, level: int) -> str:
pad = " " * (indent * level)
child_pad = " " * (indent * (level + 1))
# ์ธ๋ผ์ธ ํ๋ณด: ํ ์ค๋ก ํฉ์ณค์ ๋ ํญ ์ดํ๋ฉด ๊ทธ๋๋ก ์ฌ์ฉ (์ค์นผ๋ผ๋ ํญ์ ์ธ๋ผ์ธ)
inline = json.dumps(node, ensure_ascii=False, separators=(", ", ": "))
if not isinstance(node, (dict, list)) or len(pad) + len(inline) <= width:
return inline
if isinstance(node, dict):
if not node:
return "{}"
items = [f'{child_pad}{json.dumps(k, ensure_ascii=False)}: {fmt(v, level + 1)}'
for k, v in node.items()]
return "{\n" + ",\n".join(items) + "\n" + pad + "}"
# list
if not node:
return "[]"
items = [child_pad + fmt(v, level + 1) for v in node]
return "[\n" + ",\n".join(items) + "\n" + pad + "]"
return fmt(data, 0)
def dump_json(path: Path, data: dict) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
path.write_text(compact_json(data) + "\n", encoding="utf-8")
print(f"Wrote {path}")
def emit_layers(eng_dir: Path, *, taxonomy: dict, context_library: dict, behavior_signals: dict) -> None:
"""
L0_taxonomy.json ์ ์ฒด ๊ต์ฒด + L3_serving.json์ context_library + L2_inference.json์
ranker.behavior_signals ํค๋ง ๊ต์ฒด(context_manager/action_signal/calibrator ๋ฑ ํ์ ๋ณด์กด).
"""
dump_json(eng_dir / "L0_taxonomy.json", taxonomy)
l3 = load_json(eng_dir / "L3_serving.json")
l3["context_library"] = context_library
dump_json(eng_dir / "L3_serving.json", l3)
l2 = load_json(eng_dir / "L2_inference.json")
l2.setdefault("ranker", {})["behavior_signals"] = behavior_signals
dump_json(eng_dir / "L2_inference.json", l2)
|