Bench harness scripts (rebuttal regeneration tooling; kept off the code repository)
Browse files
evaluation/bench_100/dump_evidence.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python
|
| 2 |
+
"""证据落盘重跑:验证 MEDICAL→EasyIO 是否为社区检索错引。
|
| 3 |
+
|
| 4 |
+
取 bench_100 MEDICAL 里被判为 BUILDING_AUTOMATION/EasyIO 的误判 IP,
|
| 5 |
+
只重跑这几个 IP 的决策, 并把 retrieval 证据 (local/community/reasoning)
|
| 6 |
+
完整落盘到 /tmp/evdump/<ip>.json 供离线分析。
|
| 7 |
+
"""
|
| 8 |
+
import json
|
| 9 |
+
import os
|
| 10 |
+
import sys
|
| 11 |
+
|
| 12 |
+
REPO = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 13 |
+
sys.path.insert(0, os.path.join(REPO, "agent"))
|
| 14 |
+
sys.path.insert(0, REPO)
|
| 15 |
+
|
| 16 |
+
BENCH = os.path.join(REPO, "evaluation", "bench_100")
|
| 17 |
+
VALID = os.path.join(REPO, "evaluation", "validation")
|
| 18 |
+
DUMP = "/tmp/evdump"
|
| 19 |
+
os.makedirs(DUMP, exist_ok=True)
|
| 20 |
+
|
| 21 |
+
TARGET_IPS = ["3.71.188.253", "13.39.163.24", "16.112.58.170", "13.56.11.122"]
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def main():
|
| 25 |
+
import pandas as pd
|
| 26 |
+
|
| 27 |
+
# 1) 构造 4-IP 子集并换入 test csv (bench 同款机制)
|
| 28 |
+
src = os.path.join(BENCH, "sample_MEDICAL_100.csv")
|
| 29 |
+
bak = os.path.join(VALID, "test_MEDICAL_1.csv.bench_bak")
|
| 30 |
+
tst = os.path.join(VALID, "test_MEDICAL_1.csv")
|
| 31 |
+
if os.path.exists(tst) and not os.path.exists(bak):
|
| 32 |
+
import shutil
|
| 33 |
+
shutil.copy2(tst, bak)
|
| 34 |
+
df = pd.read_csv(src, low_memory=False)
|
| 35 |
+
sub = df[df["ip"].astype(str).isin(TARGET_IPS)]
|
| 36 |
+
sub.to_csv(tst, index=False)
|
| 37 |
+
print(f"[dump] {len(sub)} IPs swapped into test_MEDICAL_1.csv")
|
| 38 |
+
|
| 39 |
+
# 2) patch RetrievalToolRuntime.run 落盘证据
|
| 40 |
+
import decision
|
| 41 |
+
|
| 42 |
+
_orig_run = decision.RetrievalToolRuntime.run
|
| 43 |
+
|
| 44 |
+
def dumping_run(self, ip):
|
| 45 |
+
combined = _orig_run(self, ip)
|
| 46 |
+
try:
|
| 47 |
+
with open(os.path.join(DUMP, f"{ip}.json"), "w") as fh:
|
| 48 |
+
json.dump(combined, fh, ensure_ascii=False, default=str)
|
| 49 |
+
except Exception as exc: # noqa: BLE001
|
| 50 |
+
print(f"[dump] write failed {ip}: {exc}")
|
| 51 |
+
return combined
|
| 52 |
+
|
| 53 |
+
decision.RetrievalToolRuntime.run = dumping_run
|
| 54 |
+
|
| 55 |
+
# ── token 用量计量: 拦截 ChatOpenAI.invoke(决策分支) 与 LLM.chat_with_llm(检索漏斗) ──
|
| 56 |
+
USAGE = {
|
| 57 |
+
"decision_prompt": 0, "decision_completion": 0, "decision_cache_read": 0,
|
| 58 |
+
"decision_calls": 0, "funnel_prompt": 0, "funnel_completion": 0,
|
| 59 |
+
"funnel_calls": 0,
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
from langchain_openai import ChatOpenAI as _CO
|
| 63 |
+
_orig_invoke = _CO.invoke
|
| 64 |
+
|
| 65 |
+
def _counting_invoke(self, input, *a, **kw):
|
| 66 |
+
r = _orig_invoke(self, input, *a, **kw)
|
| 67 |
+
try:
|
| 68 |
+
um = getattr(r, "usage_metadata", None) or {}
|
| 69 |
+
USAGE["decision_prompt"] += int(um.get("input_tokens") or 0)
|
| 70 |
+
USAGE["decision_completion"] += int(um.get("output_tokens") or 0)
|
| 71 |
+
det = um.get("input_token_details") or {}
|
| 72 |
+
USAGE["decision_cache_read"] += int(det.get("cache_read") or 0)
|
| 73 |
+
USAGE["decision_calls"] += 1
|
| 74 |
+
except Exception: # noqa: BLE001
|
| 75 |
+
pass
|
| 76 |
+
return r
|
| 77 |
+
|
| 78 |
+
_CO.invoke = _counting_invoke
|
| 79 |
+
|
| 80 |
+
import llm as _llm_mod
|
| 81 |
+
_orig_chat = _llm_mod.LLM.chat_with_llm
|
| 82 |
+
|
| 83 |
+
def _counting_chat(self, llm_name, messages, *a, **kw):
|
| 84 |
+
kw["return_usage"] = True
|
| 85 |
+
res = _orig_chat(self, llm_name, messages, *a, **kw)
|
| 86 |
+
try:
|
| 87 |
+
result, usage = res
|
| 88 |
+
except (TypeError, ValueError):
|
| 89 |
+
return res
|
| 90 |
+
if usage:
|
| 91 |
+
USAGE["funnel_prompt"] += int(usage.get("prompt_tokens") or 0)
|
| 92 |
+
USAGE["funnel_completion"] += int(usage.get("completion_tokens") or 0)
|
| 93 |
+
USAGE["funnel_calls"] += 1
|
| 94 |
+
return result
|
| 95 |
+
|
| 96 |
+
_llm_mod.LLM.chat_with_llm = _counting_chat
|
| 97 |
+
|
| 98 |
+
# 3) 跑决策 (强模型 deepseek 双分支)
|
| 99 |
+
from agent import IdentificationAgent
|
| 100 |
+
|
| 101 |
+
agent = IdentificationAgent(llm="DEEPSEEK", gpu=0)
|
| 102 |
+
try:
|
| 103 |
+
agent.run_decision(
|
| 104 |
+
devices=["MEDICAL"], whether_local=True, whether_community=True,
|
| 105 |
+
whether_reasoning=True, top_k=5, enable_first_stage=True,
|
| 106 |
+
unseen_adapter_path=os.path.join(
|
| 107 |
+
REPO, "evaluation/unseen/llama3/results_v2/final_model"),
|
| 108 |
+
unseen_load_in_4bit=True,
|
| 109 |
+
drift_model_dir=os.path.join(REPO, "drift_data/autoencoder_drift"),
|
| 110 |
+
gpu=0,
|
| 111 |
+
)
|
| 112 |
+
finally:
|
| 113 |
+
# 4) 还原 test csv
|
| 114 |
+
if os.path.exists(bak):
|
| 115 |
+
import shutil
|
| 116 |
+
shutil.move(bak, tst)
|
| 117 |
+
print("[dump] test_MEDICAL_1.csv restored")
|
| 118 |
+
|
| 119 |
+
print("[usage]", json.dumps(USAGE))
|
| 120 |
+
with open(os.path.join(DUMP, "usage.json"), "w") as fh:
|
| 121 |
+
json.dump(USAGE, fh, indent=2)
|
| 122 |
+
|
| 123 |
+
preds = json.load(open(os.path.join(
|
| 124 |
+
REPO, "evaluation", "predict", "MEDICAL_type_prediction.json")))
|
| 125 |
+
if isinstance(preds, list):
|
| 126 |
+
preds = {str(r.get("ip")): r for r in preds if isinstance(r, dict)}
|
| 127 |
+
for ip in TARGET_IPS:
|
| 128 |
+
r = preds.get(ip) or {}
|
| 129 |
+
print(f"{ip}: pred={r.get('predicted_device_type')} "
|
| 130 |
+
f"vendor={r.get('predicted_vendor')} "
|
| 131 |
+
f"conf={r.get('confidence')}")
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
if __name__ == "__main__":
|
| 135 |
+
main()
|