98 lines
3.6 KiB
Python
98 lines
3.6 KiB
Python
from __future__ import annotations
|
||
|
||
import json
|
||
|
||
from online_mining_common import (
|
||
emit_error,
|
||
emit_success,
|
||
load_json_payload,
|
||
resolve_portable_path,
|
||
)
|
||
|
||
|
||
def load_cases(payload: dict) -> list[dict]:
|
||
if isinstance(payload.get("cases"), list):
|
||
return [item for item in payload["cases"] if isinstance(item, dict)]
|
||
cases_path = payload.get("cases_path")
|
||
if not cases_path:
|
||
raise ValueError("cases or cases_path is required")
|
||
rows = []
|
||
text = resolve_portable_path(str(cases_path)).read_text(encoding="utf-8")
|
||
for line in text.splitlines():
|
||
if not line.strip():
|
||
continue
|
||
item = json.loads(line)
|
||
if isinstance(item, dict):
|
||
rows.append(item)
|
||
return rows
|
||
|
||
|
||
def case_value(case: dict, key: str):
|
||
if key in case:
|
||
return case.get(key)
|
||
main = case.get("main") if isinstance(case.get("main"), dict) else {}
|
||
pre = case.get("pre_processing") if isinstance(case.get("pre_processing"), dict) else {}
|
||
return case.get(key) or pre.get(key) or main.get(key)
|
||
|
||
|
||
def main() -> int:
|
||
try:
|
||
payload = load_json_payload()
|
||
dataset_label = str(payload.get("dataset_label") or "").strip()
|
||
target = str(payload.get("target") or "").strip()
|
||
if not dataset_label:
|
||
raise ValueError("dataset_label is required")
|
||
if not target:
|
||
raise ValueError("target is required")
|
||
complex_value = bool(payload.get("complex", False))
|
||
cases = load_cases(payload)
|
||
if not cases:
|
||
raise ValueError("cases must not be empty")
|
||
|
||
lines = [f"# dataset_label: {dataset_label}", ""]
|
||
for index, case in enumerate(cases, start=1):
|
||
query = str(case_value(case, "query") or "").strip()
|
||
if not query:
|
||
continue
|
||
request_id = str(case_value(case, "request_id") or "").strip()
|
||
timestamp = case_value(case, "timestamp")
|
||
tts = str(case_value(case, "tts") or "").strip()
|
||
planning_result = str(case_value(case, "planning_result") or "").strip()
|
||
notes = []
|
||
if planning_result:
|
||
notes.append(f"线上模型输出: {planning_result}")
|
||
main_domain = case_value(case, "domain") or case.get("main_domain")
|
||
if main_domain:
|
||
notes.append(f"线上 domain: {main_domain}")
|
||
lines.append(f"### case: online_{index:04d}")
|
||
lines.append(f"用户: {query}")
|
||
lines.append(f"complex: {'true' if complex_value else 'false'}")
|
||
lines.append(f"target: {target}")
|
||
if request_id:
|
||
lines.append(f"request_id: {request_id}")
|
||
if timestamp:
|
||
lines.append(f"timestamp: {timestamp}")
|
||
if tts:
|
||
lines.append(f"notes: tts={tts}" + (f";{';'.join(notes)}" if notes else ""))
|
||
elif notes:
|
||
lines.append(f"notes: {';'.join(notes)}")
|
||
lines.append("")
|
||
|
||
draft_text = "\n".join(lines).strip() + "\n"
|
||
output_path = None
|
||
if payload.get("output_path"):
|
||
output = resolve_portable_path(str(payload["output_path"]))
|
||
output.parent.mkdir(parents=True, exist_ok=True)
|
||
output.write_text(draft_text, encoding="utf-8")
|
||
output_path = str(output)
|
||
emit_success({"case_count": draft_text.count("### case:"), "draft_text": draft_text, "output_path": output_path})
|
||
return 0
|
||
except Exception as exc: # noqa: BLE001
|
||
emit_error(exc)
|
||
return 1
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|
||
|