File size: 10,299 Bytes
d30d906
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
"""Compare ranking controls without rebuilding indexes or calling an LLM."""

from __future__ import annotations

import argparse
import json
import sys
from datetime import datetime, timezone
from pathlib import Path

sys.path.insert(0, str(Path(__file__).parent.parent))

from config import DATA_DIR, DEFAULT_APP_VERSION
import core.search_engine as search_module
from core.search_engine import MenuSearchEngine
from core.version_paths import raw_menus_path


def _answers(item: dict) -> set[str]:
    values = item.get("expected_ids")
    if values is None and item.get("menu_id"):
        values = [item["menu_id"]]
    return {str(value).strip() for value in (values or []) if str(value).strip()}


def _rank(predicted: list[str], expected: set[str]) -> int | None:
    return next(
        (
            index
            for index, menu_id in enumerate(predicted, 1)
            if menu_id in expected
        ),
        None,
    )


def _metrics(ranks: list[int | None]) -> dict[str, float]:
    total = len(ranks)
    return {
        "n": total,
        "acc1": sum(rank is not None and rank <= 1 for rank in ranks) / total,
        "acc3": sum(rank is not None and rank <= 3 for rank in ranks) / total,
        "acc5": sum(rank is not None and rank <= 5 for rank in ranks) / total,
        "mrr5": sum(1.0 / rank for rank in ranks if rank) / total,
    }


def _stage1_matrix() -> list[dict]:
    return [
        {
            "name": "baseline",
            "query_expansion": True,
            "popularity": 0.005,
            "query_top_k": 50,
            "query_threshold": 0.85,
            "query_weight": 0.4,
        },
        {
            "name": "expansion_off",
            "query_expansion": False,
            "popularity": 0.005,
            "query_top_k": 50,
            "query_threshold": 0.85,
            "query_weight": 0.4,
        },
        {
            "name": "popularity_off",
            "query_expansion": True,
            "popularity": 0.0,
            "query_top_k": 50,
            "query_threshold": 0.85,
            "query_weight": 0.4,
        },
        {
            "name": "expansion_popularity_off",
            "query_expansion": False,
            "popularity": 0.0,
            "query_top_k": 50,
            "query_threshold": 0.85,
            "query_weight": 0.4,
        },
    ]


def _query_index_matrix(
    *,
    query_expansion: bool,
    popularity: float,
) -> list[dict]:
    common = {
        "query_expansion": query_expansion,
        "popularity": popularity,
    }
    return [
        {
            **common,
            "name": "qidx_control",
            "query_top_k": 50,
            "query_threshold": 0.85,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top100",
            "query_top_k": 100,
            "query_threshold": 0.85,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top200",
            "query_top_k": 200,
            "query_threshold": 0.85,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top50_threshold082",
            "query_top_k": 50,
            "query_threshold": 0.82,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top100_threshold082",
            "query_top_k": 100,
            "query_threshold": 0.82,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top200_threshold082",
            "query_top_k": 200,
            "query_threshold": 0.82,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top50_threshold080",
            "query_top_k": 50,
            "query_threshold": 0.80,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top100_threshold080",
            "query_top_k": 100,
            "query_threshold": 0.80,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top200_threshold080",
            "query_top_k": 200,
            "query_threshold": 0.80,
            "query_weight": 0.4,
        },
        {
            **common,
            "name": "qidx_top200_weight06",
            "query_top_k": 200,
            "query_threshold": 0.85,
            "query_weight": 0.6,
        },
    ]


def _popularity_matrix(*, query_expansion: bool) -> list[dict]:
    return [
        {
            "name": f"popularity_{weight:g}",
            "query_expansion": query_expansion,
            "popularity": weight,
            "query_top_k": 50,
            "query_threshold": 0.85,
            "query_weight": 0.4,
        }
        for weight in (0.005, 0.004, 0.003, 0.002, 0.001, 0.0)
    ]


def _apply(config: dict) -> None:
    search_module.QUERY_EXPANSION_ENABLED = config["query_expansion"]
    search_module.SEARCH_COUNT_BOOST_WEIGHT = config["popularity"]
    search_module.QUERY_INDEX_TOP_K = config["query_top_k"]
    search_module.QUERY_IDX_THRESHOLD = config["query_threshold"]
    search_module.W_QUERY_IDX = config["query_weight"]


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("--app-version", default=DEFAULT_APP_VERSION)
    parser.add_argument("--eval-path", type=Path, default=DATA_DIR / "eval_queries_dev.json")
    parser.add_argument(
        "--matrix",
        choices=("stage1", "popularity", "qidx"),
        default="stage1",
    )
    parser.add_argument(
        "--expansion",
        choices=("on", "off"),
        default="on",
        help="base query-expansion setting for the qidx matrix",
    )
    parser.add_argument(
        "--popularity",
        type=float,
        default=0.005,
        help="base search-count boost for the qidx matrix",
    )
    parser.add_argument("--output", type=Path)
    args = parser.parse_args()

    rows = json.loads(args.eval_path.read_text(encoding="utf-8"))
    available = {
        item["menu_id"]
        for item in json.loads(
            raw_menus_path(args.app_version).read_text(encoding="utf-8")
        )
    }
    rows = [
        item
        for item in rows
        if not item.get("no_match", False) and _answers(item) & available
    ]
    if args.matrix == "stage1":
        matrix = _stage1_matrix()
    elif args.matrix == "popularity":
        matrix = _popularity_matrix(
            query_expansion=args.expansion == "on"
        )
    else:
        matrix = _query_index_matrix(
            query_expansion=args.expansion == "on",
            popularity=args.popularity,
        )

    engine = MenuSearchEngine.get_instance(args.app_version)
    original_embed = engine.embedder.embed_text
    embedding_cache: dict[str, list] = {}

    def cached_embed(text: str) -> list:
        if text not in embedding_cache:
            embedding_cache[text] = original_embed(text)
        return embedding_cache[text]

    engine.embedder.embed_text = cached_embed
    reports = []
    baseline_ranks: list[int | None] | None = None

    for config in matrix:
        _apply(config)
        ranks = []
        predictions = []
        by_source: dict[str, list[int | None]] = {}
        for item in rows:
            hits = engine.search(
                item["query"],
                top_n=5,
                threshold=0.0,
                use_hyde=False,
                use_reranker=False,
            )
            predicted = [hit.get("menu_id", "") for hit in hits]
            rank = _rank(predicted, _answers(item))
            ranks.append(rank)
            by_source.setdefault(
                str(item.get("source") or "unknown"), []
            ).append(rank)
            predictions.append(
                {
                    "query": item["query"],
                    "source": str(item.get("source") or "unknown"),
                    "expected_ids": sorted(_answers(item)),
                    "predicted_ids": predicted,
                    "rank": rank,
                }
            )

        if baseline_ranks is None:
            baseline_ranks = ranks
        improved_top3 = sum(
            (before is None or before > 3)
            and after is not None
            and after <= 3
            for before, after in zip(baseline_ranks, ranks)
        )
        regressed_top3 = sum(
            before is not None
            and before <= 3
            and (after is None or after > 3)
            for before, after in zip(baseline_ranks, ranks)
        )
        report = {
            "config": config,
            "metrics": _metrics(ranks),
            "metrics_by_source": {
                source: _metrics(source_ranks)
                for source, source_ranks in sorted(by_source.items())
            },
            "top3_improved_vs_control": improved_top3,
            "top3_regressed_vs_control": regressed_top3,
            "predictions": predictions,
        }
        reports.append(report)
        metrics = report["metrics"]
        print(
            f"{config['name']}: Acc@1={metrics['acc1']:.1%} "
            f"Acc@3={metrics['acc3']:.1%} Acc@5={metrics['acc5']:.1%} "
            f"MRR@5={metrics['mrr5']:.3f} "
            f"Top3 +{improved_top3}/-{regressed_top3}"
        )
        for source, source_metrics in report["metrics_by_source"].items():
            print(
                f"  {source}: Acc@1={source_metrics['acc1']:.1%} "
                f"Acc@3={source_metrics['acc3']:.1%} "
                f"Acc@5={source_metrics['acc5']:.1%}"
            )

    output = args.output or (
        DATA_DIR
        / "experiments"
        / f"ranking_{args.matrix}_{args.app_version.replace('.', '_')}.json"
    )
    output.parent.mkdir(parents=True, exist_ok=True)
    output.write_text(
        json.dumps(
            {
                "created_at": datetime.now(timezone.utc).isoformat(),
                "app_version": args.app_version,
                "eval_path": str(args.eval_path),
                "rows": len(rows),
                "reports": reports,
            },
            ensure_ascii=False,
            indent=2,
        )
        + "\n",
        encoding="utf-8",
    )
    print(f"output: {output}")


if __name__ == "__main__":
    main()