#!/usr/bin/env python3 """③ 평균 가격 협상률 — ① 측정 로그에서 독립 집계. 협상률 = (기존 공급가 − 최종 합의가) / 기존 공급가. 협상완료(합의) 건만 평균. 목표 3%. ①의 detail_*.jsonl에 건별 reduction_pct가 남아 있어 LLM 재실행 없이 산출한다. 사용: python aggregate.py [--source ../01_win_rate/본측정300_azure_20260901] [--target 3] 출력: data/reduction_all.csv 건별 원자료 (모델·기존공급가·합의가·협상률) data/reduction_all_meta.json 출처·산식·모델별·분포 result.json 건수 + 평균 협상률 + PASS/FAIL """ import argparse import csv import json import statistics import sys from pathlib import Path HERE = Path(__file__).parent DEFAULT_SOURCE = HERE.parent / "01_win_rate" / "본측정300_azure_20260901" FORMULA = "협상완료 합의 건의 (기존 공급가 − 최종 합의가) / 기존 공급가 평균" def main(): ap = argparse.ArgumentParser(description=__doc__.split("\n", 1)[0]) ap.add_argument("--source", type=Path, default=DEFAULT_SOURCE) ap.add_argument("--target", type=float, default=3.0) args = ap.parse_args() files = sorted(args.source.glob("detail_*.jsonl")) if not files: sys.exit(f"detail_*.jsonl 이 없습니다: {args.source}") cases = [(f.stem[len("detail_"):].replace("_", "."), json.loads(line)) for f in files for line in f.open(encoding="utf-8")] agreed = [(m, c) for m, c in cases if c.get("reduction_pct") is not None] if not agreed: sys.exit("합의(협상완료) 건이 없습니다") reds = [c["reduction_pct"] for _, c in agreed] avg = round(sum(reds) / len(reds), 3) out = HERE / "data" out.mkdir(exist_ok=True) with open(out / "reduction_all.csv", "w", encoding="utf-8-sig", newline="") as f: w = csv.writer(f) w.writerow(["모델", "case_id", "견적번호", "기존공급가", "최종합의가", "협상률_pct"]) for model, c in agreed: w.writerow([model, c["case_id"], c.get("quotation_number"), c["prev_price"], c["final_price"], c["reduction_pct"]]) by_model = {} for model, c in agreed: by_model.setdefault(model, []).append(c["reduction_pct"]) (out / "reduction_all_meta.json").write_text(json.dumps({ "출처": f"{args.source.name} — {len(files)}개 detail_*.jsonl, 전체 {len(cases)}건", "산식": FORMULA, "모델별": {m: {"합의건수": len(v), "avg_reduction_pct": round(sum(v) / len(v), 3)} for m, v in by_model.items()}, "분포_pct": {"최소": min(reds), "중앙값": round(statistics.median(reds), 3), "최대": max(reds), "표준편차": round(statistics.pstdev(reds), 3)}, }, ensure_ascii=False, indent=1), encoding="utf-8") result = { "전체 건수": len(cases), "합의 건수": len(agreed), "미합의 건수": len(cases) - len(agreed), "평균가격협상률_pct": avg, "산식": FORMULA, "목표_pct": args.target, "verdict": "PASS" if avg >= args.target else "FAIL", "상세": "data/reduction_all_meta.json", } (HERE / "result.json").write_text( json.dumps(result, ensure_ascii=False, indent=1), encoding="utf-8") print(json.dumps(result, ensure_ascii=False, indent=1)) if __name__ == "__main__": main()