#!/usr/bin/env python3 """③ 평균 가격 협상률 — ① 측정 로그에서 독립 집계. 협상률 = (기존 공급가 − 최종 합의가) / 기존 공급가 × 100. 협상완료(합의) 건의 **단순 평균**(건별 비율의 평균). 목표 3%. ①의 detail_*.jsonl에 건별 reduction_pct가 남아 있어 LLM 재실행 없이 산출한다. 사용: python aggregate.py --source <① 실행 원본 폴더> (원본은 저장소 밖 NEGO_DOCS/_archive_20260909_실행원본/01_win_rate/ 에 보관) python aggregate.py --source <폴더> --out /tmp/x 정본을 건드리지 않고 그 폴더에 산출 출력: (기본 이 스크립트 폴더 — --out 으로 옮길 수 있다) 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" # 저장소 밖으로 보관 이동 — --source 로 지정한다 FORMULA = "건별 가격 협상률 = (기존 공급가 − 최종 합의가) ÷ 기존 공급가 × 100, 협상완료 건의 단순 평균" 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) ap.add_argument("--out", type=Path, default=HERE, help="산출 위치 (기본: 이 스크립트 폴더 = 정본). 시험 실행은 다른 경로를 준다") 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) args.out.mkdir(parents=True, exist_ok=True) out = args.out / "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", } (args.out / "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()