forked from Ikalus1988/MisakaNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_bm25_weights.py
More file actions
209 lines (177 loc) · 7.33 KB
/
Copy pathbenchmark_bm25_weights.py
File metadata and controls
209 lines (177 loc) · 7.33 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
"""BM25 Field Weighting Benchmark — Issue #311.
Compares old vs new metadata weights using regression queries.
Measures Precision@K and Mean Reciprocal Rank (MRR).
Usage:
python3 scripts/benchmark_bm25_weights.py
python3 scripts/benchmark_bm25_weights.py --json
"""
import json
import sys
import time
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(REPO))
from misakanet.search.engine import (
LESSONS,
WEIGHT_DOMAIN_MATCH,
WEIGHT_HAS_REF,
WEIGHT_STATUS,
WEIGHT_TITLE_EXACT,
WEIGHT_TITLE_PARTIAL,
_load_docs_cached,
_rank_docs_impl,
_tokenize,
)
# ── Weight configurations ──
WEIGHTS_OLD = {
"domain_match": 0.3,
"title_exact": 0.5,
"title_partial": 0.2,
"status_published": 0.2,
"has_reference": 0.08,
}
# Proposed: boost title match (strongest signal), reduce domain noise,
# remove status weight (published is the default, adds no info).
WEIGHTS_NEW = {
"domain_match": 0.25,
"title_exact": 0.80,
"title_partial": 0.40,
"status_published": 0.0, # removed: default state, no signal
"has_reference": 0.12,
}
# ── Query fixtures ──
QUERIES = [
{"query": "DCO", "expected": ["dco-auto-fix-workflow"]},
{"query": "GitHub token", "expected": ["github-api-pr-issue-management"]},
{"query": "pip timeout", "expected": ["pip-install-timeout-ssl"]},
{"query": "database locked", "expected": ["agent-state-database-lock-issues-cleanup-protocol"]},
{"query": "feishu", "expected": ["feishu-block-api-false-success"]},
{"query": "FANUC", "expected": ["fanuc-io-marker-m-instruction"]},
{"query": "PROFINET", "expected": ["fanuc-profinet-32bit-real-value-transfer"]},
{"query": "secret scan", "expected": ["codeql-alert-dismissal-false-positive"]},
{"query": "Windows Unicode", "expected": ["python-gbk-encoding-error"]},
{"query": "WSL permission", "expected": ["wsl-permission-ntfs-fix"]},
{"query": "SSL certificate", "expected": []}, # open-ended
{"query": "proxy configuration", "expected": []},
{"query": "git rebase conflict", "expected": []},
{"query": "Docker build failed", "expected": []},
{"query": "API rate limit", "expected": []},
{"query": "JSON parsing error", "expected": []},
{"query": "memory leak", "expected": []},
{"query": "timeout connection", "expected": []},
{"query": "permission denied", "expected": []},
{"query": "module not found", "expected": []},
# Edge cases: domain match vs title relevance
{"query": "FANUC alarm", "expected": []}, # should rank FANUC domain high
{"query": "agent lock database", "expected": []}, # multi-keyword
{"query": "python encoding error", "expected": []},
{"query": "git push rejected", "expected": []},
{"query": "docker compose failed", "expected": []},
{"query": "npm install ERESOLVE", "expected": []},
{"query": "ssh connection refused", "expected": []},
{"query": "cron not running", "expected": []},
]
def _apply_weights(weights: dict):
"""Monkey-patch engine weights for benchmarking."""
import misakanet.search.engine as eng
eng.WEIGHT_DOMAIN_MATCH = weights["domain_match"]
eng.WEIGHT_TITLE_EXACT = weights["title_exact"]
eng.WEIGHT_TITLE_PARTIAL = weights["title_partial"]
eng.WEIGHT_STATUS = {"published": weights["status_published"], "active": 0.1, "draft": 0.0}
eng.WEIGHT_HAS_REF = weights["has_reference"]
def _run_benchmark(docs, weights, top_k=5):
"""Run all queries with given weights, return metrics."""
_apply_weights(weights)
precisions = []
mrrs = []
results_detail = []
for q in QUERIES:
ranked = _rank_docs_impl(q["query"], docs, titles_only=False, broad_only=False)
top_results = ranked[:top_k]
top_filenames = [d.filename.replace(".md", "") for _, d in top_results]
expected = set(q["expected"])
if expected:
hits = sum(1 for f in top_filenames if f in expected)
precision = hits / min(top_k, len(expected))
precisions.append(precision)
# MRR
rr = 0.0
for i, f in enumerate(top_filenames):
if f in expected:
rr = 1.0 / (i + 1)
break
mrrs.append(rr)
results_detail.append({
"query": q["query"],
"expected": q["expected"],
"top_hits": top_filenames[:3],
"precision_at_5": hits / min(top_k, len(expected)) if expected else None,
"rr": rr if expected else None,
})
return {
"mean_precision_at_5": sum(precisions) / len(precisions) if precisions else 0,
"mean_mrr": sum(mrrs) / len(mrrs) if mrrs else 0,
"queries_with_expected": len(precisions),
"details": results_detail,
}
def main():
json_mode = "--json" in sys.argv
docs = _load_docs_cached(LESSONS, is_lesson=True)
print(f"Loaded {len(docs)} lessons\n")
print("=" * 60)
print("BM25 Field Weighting Benchmark — Issue #311")
print("=" * 60)
# Old weights
print("\n--- OLD weights ---")
for k, v in WEIGHTS_OLD.items():
print(f" {k}: {v}")
t0 = time.time()
old_result = _run_benchmark(docs, WEIGHTS_OLD)
old_time = time.time() - t0
print(f"\n Precision@5: {old_result['mean_precision_at_5']:.3f}")
print(f" MRR: {old_result['mean_mrr']:.3f}")
print(f" Time: {old_time*1000:.0f}ms")
# New weights
print("\n--- NEW weights ---")
for k, v in WEIGHTS_NEW.items():
print(f" {k}: {v}")
t0 = time.time()
new_result = _run_benchmark(docs, WEIGHTS_NEW)
new_time = time.time() - t0
print(f"\n Precision@5: {new_result['mean_precision_at_5']:.3f}")
print(f" MRR: {new_result['mean_mrr']:.3f}")
print(f" Time: {new_time*1000:.0f}ms")
# Delta
p_delta = new_result['mean_precision_at_5'] - old_result['mean_precision_at_5']
m_delta = new_result['mean_mrr'] - old_result['mean_mrr']
print(f"\n--- DELTA ---")
print(f" Precision@5: {p_delta:+.3f}")
print(f" MRR: {m_delta:+.3f}")
# Per-query comparison
print(f"\n--- PER-QUERY COMPARISON (queries with expected results) ---")
print(f" {'Query':<25} {'Old P@5':>8} {'New P@5':>8} {'Delta':>8}")
print(f" {'-'*25} {'-'*8} {'-'*8} {'-'*8}")
for i, q in enumerate(QUERIES):
if q["expected"]:
old_p = old_result['details'][i]['precision_at_5']
new_p = new_result['details'][i]['precision_at_5']
delta = (new_p or 0) - (old_p or 0)
marker = "✅" if delta > 0 else ("❌" if delta < 0 else " ")
print(f" {q['query']:<25} {old_p:>8.3f} {new_p:>8.3f} {delta:>+8.3f} {marker}")
report = {
"old_weights": WEIGHTS_OLD,
"new_weights": WEIGHTS_NEW,
"old_result": {k: v for k, v in old_result.items() if k != 'details'},
"new_result": {k: v for k, v in new_result.items() if k != 'details'},
"delta_precision": p_delta,
"delta_mrr": m_delta,
}
if json_mode:
print(json.dumps(report, indent=2))
else:
report_path = REPO / "data" / "bm25_weight_benchmark.json"
report_path.write_text(json.dumps(report, indent=2, ensure_ascii=False))
print(f"\nReport saved to: {report_path}")
if __name__ == "__main__":
main()