Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
1685 lines (1611 loc) · 58.1 KB
/
Copy pathmain.py
File metadata and controls
1685 lines (1611 loc) · 58.1 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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
from __future__ import annotations
import argparse
import json
import sys
from collections import Counter
from pathlib import Path
from typing import TYPE_CHECKING
from reranking.failure_driven import (
build_reranker_training_contexts_from_failures,
build_stratified_rerank_pool,
classify_failure,
inject_bm25_tail_candidates,
mmr_select_candidates,
prefilter_rerank_candidates,
rrf_fuse_doc_ids,
source_miss_type,
train_reranker_from_contexts_jsonl,
)
from utils.cli_config import (
apply_config_defaults,
load_cli_defaults,
validate_required_command_params,
)
from utils.common import min_max_normalize
from utils.logger import configure_runtime_logger
from utils.query_manipulation import (
build_query_variants_with_debug,
llm_structured_query_expansion_batch,
)
if TYPE_CHECKING:
pass
DEFAULT_EMBEDDING_MODEL = "intfloat/e5-base-v2"
DEFAULT_LLM_CONFIG_PATH = "llm.config.json"
DEFAULT_CLI_PARAMS_CONFIG = "cli.defaults.json"
REQUIRED_COMMAND_PARAMS: dict[str, tuple[str, ...]] = {
"run_rag": ("question",),
"run_experiments": ("question",),
}
def cmd_build_parser(args: argparse.Namespace) -> None:
# Command-scoped import avoids loading parser stack for unrelated commands.
from parser.pipeline import run_pipeline
stats = run_pipeline(
output_path=args.output,
min_tokens=args.min_tokens,
max_tokens=args.max_tokens,
overlap_ratio=args.overlap_ratio,
min_output_chunk_tokens=args.min_output_chunk_tokens,
max_output_chunk_tokens=args.max_output_chunk_tokens,
max_chunks_per_url=args.max_chunks_per_url,
max_chunks_per_category=args.max_chunks_per_category,
sources_config=args.sources_config,
chunker_mode=args.chunker_mode,
near_duplicate_jaccard=args.near_duplicate_jaccard,
log_level=args.log_level,
log_path=args.log_path,
log_json=args.log_json,
)
stats["embedding_model"] = args.embedding_model
print(json.dumps(stats, indent=2))
def cmd_demo_retrieval(args: argparse.Namespace) -> None:
# Command-scoped import keeps startup lightweight.
from generation.demo_retrieval import run_demo
run_demo(
query=args.query,
top_k=args.top_k,
model_name=args.model,
dataset_path=args.dataset,
faiss_path=args.faiss_path,
index_name=args.index,
rerank=args.rerank,
reranker_model=args.reranker_model,
rerank_candidates=args.rerank_candidates,
)
def cmd_evaluation_runner(args: argparse.Namespace) -> None:
# Command-scoped imports keep module import side effects localized.
from evaluation.dataset import load_eval_samples
from evaluation.metrics import RetrievalResult, evaluate_retrieval
from evaluation.runner import QueryRun, build_retriever, parse_k_values
from ingestion.loaders import load_bm25_documents_from_dataset
from retrieval.semantic import search_semantic
from utils.embedding_format import format_query_for_embedding
logger = configure_runtime_logger(
"rag.evaluation_runner",
level=args.log_level,
log_path=args.log_path,
json_logs=args.log_json,
)
logger.info("starting evaluation runner")
samples = load_eval_samples(Path(args.dataset))
if not samples:
logger.error("no evaluation samples found: %s", args.dataset)
raise ValueError(f"No samples found in dataset: {args.dataset}")
total_samples_before_filter = len(samples)
logger.info("loaded evaluation samples: count=%s", total_samples_before_filter)
if args.require_evidence:
samples = [sample for sample in samples if sample.relevant_docs]
filtered_out_samples = total_samples_before_filter - len(samples)
if filtered_out_samples > 0:
logger.warning("filtered out samples without evidence: count=%s", filtered_out_samples)
if not samples:
logger.error("no samples left after filtering")
raise ValueError(
"No samples left after filtering. "
"Try running without --require-evidence or regenerate dataset with more evidence links."
)
k_values = parse_k_values(args.k_values)
max_k = max(k_values)
logger.info(
"building retriever: mode=%s max_k=%s retrieval_cache_enabled=%s",
args.retriever,
max_k,
args.retrieval_cache_enabled,
)
retriever = build_retriever(
args.retriever,
rag_dataset_path=args.rag_dataset,
faiss_path=args.faiss_path,
index_name=args.index,
embedding_model=args.embedding_model,
alpha=args.alpha,
hybrid_candidate_multiplier=args.hybrid_candidate_multiplier,
hybrid_max_per_group=args.hybrid_max_per_group,
hybrid_rrf_k=args.hybrid_rrf_k,
cache_enabled=args.retrieval_cache_enabled,
cache_capacity=args.retrieval_cache_capacity,
cache_ttl_seconds=args.retrieval_cache_ttl_seconds,
)
semantic_embedding_map: dict[str, list[float]] = {}
if args.retriever == "hybrid":
semantic_obj = getattr(retriever, "semantic", None)
documents = getattr(semantic_obj, "documents", None)
if documents:
semantic_embedding_map = {doc.doc_id: doc.embedding for doc in documents}
doc_text_map = {
item["id"]: item["text"] for item in load_bm25_documents_from_dataset(args.rag_dataset)
}
logger.info("loaded bm25 text map: count=%s", len(doc_text_map))
reranker = None
rerank_candidate_cls = None
if args.rerank:
# Heavy reranker deps are loaded only when reranking is enabled.
from reranking.cross_encoder import CrossEncoderReranker, RerankCandidate
logger.info("initializing reranker: model=%s", args.reranker_model)
reranker = CrossEncoderReranker(model_name=args.reranker_model)
rerank_candidate_cls = RerankCandidate
query_runs: list[QueryRun] = []
metric_inputs: list[RetrievalResult] = []
llm_expansion_stats = {
"requested_queries": 0,
"generated_queries": 0,
"fallback_queries": 0,
}
llm_expansion_examples: list[dict[str, object]] = []
failure_records: list[dict[str, object]] = []
miss_type_counts: Counter[str] = Counter()
failure_bucket_source_counts: dict[str, Counter[str]] = {}
llm_expansion_cache: dict[str, tuple[list[str], list[str], list[str]]] = {}
if args.multi_query and args.multi_query_llm_expansion:
llm_expansion_cache = llm_structured_query_expansion_batch(
[sample.query for sample in samples],
provider=args.multi_query_llm_provider,
model=args.multi_query_llm_model,
api_base=args.multi_query_llm_api_base,
api_key=args.multi_query_llm_api_key,
timeout_seconds=args.multi_query_llm_timeout_seconds,
retries=args.multi_query_llm_retries,
llm_config_path=args.llm_config_path,
cache_enabled=args.llm_cache_enabled,
cache_capacity=args.llm_cache_capacity,
cache_ttl_seconds=args.llm_cache_ttl_seconds,
)
from tqdm import tqdm
total_samples = len(samples)
sample_iter = tqdm(
samples,
total=total_samples,
desc="evaluation_runner",
unit="sample",
file=sys.stdout,
)
logger.info("tqdm progress bar enabled for evaluation loop")
progress_log_step = max(1, total_samples // 20) # ~5% increments
for sample_idx, sample in enumerate(sample_iter, start=1):
if sample_idx == 1 or sample_idx % progress_log_step == 0 or sample_idx == total_samples:
logger.info("evaluation progress: %s/%s", sample_idx, total_samples)
retrieve_k = max(max_k, args.rerank_candidates) if args.rerank else max_k
semantic_branch_doc_ids: list[str] | None = None
bm25_branch_doc_ids: list[str] | None = None
semantic_score_map: dict[str, float] = {}
bm25_score_map: dict[str, float] = {}
query_embedding_for_mmr: list[float] | None = None
if args.retriever == "hybrid":
source_probe_k = max(retrieve_k, args.soft_recall_rescue_bm25_depth)
semantic_obj = getattr(retriever, "semantic", None)
bm25_obj = getattr(retriever, "bm25", None)
if (
semantic_obj is not None
and hasattr(semantic_obj, "search")
and hasattr(semantic_obj, "embedder")
):
query_embedding = semantic_obj.embedder.encode(
[format_query_for_embedding(sample.query, semantic_obj.embedding_model)],
normalize_embeddings=True,
show_progress_bar=False,
)[0].tolist()
query_embedding_for_mmr = query_embedding
semantic_hits = search_semantic(
query_embedding, semantic_obj.documents, top_k=source_probe_k
)
semantic_branch_doc_ids = [item.doc_id for item in semantic_hits]
semantic_score_map = {item.doc_id: float(item.score) for item in semantic_hits}
if bm25_obj is not None and hasattr(bm25_obj, "index"):
bm25_hits = bm25_obj.index.search(sample.query, top_k=source_probe_k)
bm25_branch_doc_ids = [item.doc_id for item in bm25_hits]
bm25_score_map = {item.doc_id: float(item.score) for item in bm25_hits}
if args.multi_query:
query_variants, llm_debug = build_query_variants_with_debug(
query=sample.query,
max_variants=args.multi_query_variants,
use_llm_structured_expansion=args.multi_query_llm_expansion,
llm_provider=args.multi_query_llm_provider,
llm_model=args.multi_query_llm_model,
llm_api_base=args.multi_query_llm_api_base,
llm_api_key=args.multi_query_llm_api_key,
llm_timeout_seconds=args.multi_query_llm_timeout_seconds,
llm_retries=args.multi_query_llm_retries,
llm_config_path=args.llm_config_path,
llm_cache_enabled=args.llm_cache_enabled,
llm_cache_capacity=args.llm_cache_capacity,
llm_cache_ttl_seconds=args.llm_cache_ttl_seconds,
llm_precomputed=llm_expansion_cache.get(sample.query),
)
if args.multi_query_llm_debug and llm_debug.get("llm_requested"):
llm_expansion_stats["requested_queries"] += 1
if int(llm_debug.get("llm_generated_count", 0)) > 0:
llm_expansion_stats["generated_queries"] += 1
if len(llm_expansion_examples) < 5:
llm_expansion_examples.append(
{
"query": sample.query,
"generated_preview": llm_debug.get("llm_generated_preview", []),
}
)
if bool(llm_debug.get("fallback_used", False)):
llm_expansion_stats["fallback_queries"] += 1
per_query_results = [
retriever.search(query, top_k=retrieve_k) for query in query_variants if query
]
retrieved = rrf_fuse_doc_ids(
per_query_results, top_k=retrieve_k, rrf_k=args.multi_query_rrf_k
)
else:
retrieved = retriever.search(sample.query, top_k=retrieve_k)
if reranker is not None:
if args.stratified_rerank_pool and args.retriever == "hybrid":
retrieved = build_stratified_rerank_pool(
hybrid_doc_ids=retrieved,
semantic_doc_ids=semantic_branch_doc_ids,
bm25_doc_ids=bm25_branch_doc_ids,
limit=retrieve_k,
)
if args.soft_recall_rescue and args.retriever == "hybrid":
retrieved = inject_bm25_tail_candidates(
query=sample.query,
merged_doc_ids=retrieved,
retriever=retriever,
bm25_search_depth=args.soft_recall_rescue_bm25_depth,
rescue_tail_k=args.soft_recall_rescue_tail_k,
)
if (
args.mmr_before_rerank
and query_embedding_for_mmr is not None
and semantic_embedding_map
and args.retriever == "hybrid"
):
mmr_k = args.mmr_k if args.mmr_k > 0 else retrieve_k
mmr_k = min(mmr_k, retrieve_k)
diversity_threshold = (
args.mmr_diversity_threshold if args.mmr_diversity_threshold > 0 else None
)
retrieved = mmr_select_candidates(
candidate_doc_ids=retrieved,
query_embedding=query_embedding_for_mmr,
doc_embeddings=semantic_embedding_map,
lambda_=args.mmr_lambda,
max_k=mmr_k,
diversity_threshold=diversity_threshold,
)
sem_norm = min_max_normalize(semantic_score_map)
bm25_norm = min_max_normalize(bm25_score_map)
rerank_input = [
rerank_candidate_cls(
doc_id=doc_id,
text=doc_text_map.get(doc_id, ""),
score=(
(args.rerank_semantic_weight * sem_norm.get(doc_id, 0.0))
+ (args.rerank_bm25_weight * bm25_norm.get(doc_id, 0.0))
),
metadata={
"semantic_norm": sem_norm.get(doc_id, 0.0),
"bm25_norm": bm25_norm.get(doc_id, 0.0),
},
)
for rank, doc_id in enumerate(retrieved, start=1)
if doc_text_map.get(doc_id, "")
]
if args.hard_negative_semantic_floor > 0.0:
rerank_input = [
candidate
for candidate in rerank_input
if candidate.metadata.get("semantic_norm", 0.0)
>= args.hard_negative_semantic_floor
]
if args.two_stage_rerank:
rerank_input = prefilter_rerank_candidates(
sample.query,
rerank_input,
keep_top_n=args.prefilter_candidates,
)
reranked = reranker.rerank(
sample.query,
rerank_input,
top_k=retrieve_k,
alpha=args.rerank_alpha,
ce_calibration=args.ce_calibration,
ce_temperature=args.ce_temperature,
top1_margin_lambda=args.rerank_top1_margin_lambda,
)
retrieved = [item.doc_id for item in reranked]
else:
retrieved = retrieved[:retrieve_k]
retrieved_for_metrics = retrieved[:max_k]
query_runs.append(
QueryRun(
query=sample.query,
relevant_doc_ids=sample.relevant_docs,
retrieved_doc_ids=retrieved_for_metrics,
)
)
metric_inputs.append(
RetrievalResult(
query=sample.query,
retrieved_doc_ids=retrieved_for_metrics,
relevant_doc_ids=sample.relevant_docs,
)
)
if sample.relevant_docs and not set(retrieved_for_metrics).intersection(
sample.relevant_docs
):
miss_type = source_miss_type(
relevant_doc_ids=sample.relevant_docs,
semantic_doc_ids=semantic_branch_doc_ids,
bm25_doc_ids=bm25_branch_doc_ids,
)
miss_type_counts[miss_type] += 1
failure = classify_failure(
query=sample.query,
gt_doc_ids=sample.relevant_docs,
top_k_doc_ids=retrieved_for_metrics,
all_ranked_doc_ids=retrieved,
doc_text_map=doc_text_map,
near_miss_threshold=args.failure_near_miss_threshold,
top_k=max_k,
)
bucket = failure.bucket
reasons = failure.reasons
if bucket not in failure_bucket_source_counts:
failure_bucket_source_counts[bucket] = Counter()
failure_bucket_source_counts[bucket][miss_type] += 1
failure_records.append(
{
"query": sample.query,
"bucket": bucket,
"relevant_doc_ids": sample.relevant_docs,
"retrieved_top_k_doc_ids": retrieved_for_metrics,
"retrieved_full_doc_ids": retrieved,
"bm25_branch_doc_ids": (bm25_branch_doc_ids or [])[
: args.soft_recall_rescue_bm25_depth
],
"source_miss_type": miss_type,
"reasons": reasons,
}
)
metrics = evaluate_retrieval(metric_inputs, k_values)
top1_doc_ids = [run.retrieved_doc_ids[0] for run in query_runs if run.retrieved_doc_ids]
top1_counts = Counter(top1_doc_ids)
top1_total = sum(top1_counts.values())
top1_distribution = (
{doc_id: count / top1_total for doc_id, count in top1_counts.most_common()}
if top1_total
else {}
)
max_top1_doc, max_top1_count = top1_counts.most_common(1)[0] if top1_counts else (None, 0)
failure_bucket_counts = Counter(item["bucket"] for item in failure_records)
failure_bucket_source_counts_json = {
bucket: dict(counter) for bucket, counter in failure_bucket_source_counts.items()
}
manual_inspection_failed_queries = failure_records[: args.failure_sample_size]
report = {
"dataset": args.dataset,
"retriever": args.retriever,
"rerank_enabled": args.rerank,
"rerank_alpha": args.rerank_alpha if args.rerank else None,
"rerank_top1_margin_lambda": args.rerank_top1_margin_lambda if args.rerank else None,
"two_stage_rerank_enabled": args.two_stage_rerank,
"reranker_model": args.reranker_model if args.rerank else None,
"stratified_rerank_pool_enabled": args.stratified_rerank_pool,
"hard_negative_semantic_floor": args.hard_negative_semantic_floor,
"rerank_prior_weights": {
"semantic": args.rerank_semantic_weight,
"bm25": args.rerank_bm25_weight,
},
"ce_calibration": args.ce_calibration if args.rerank else None,
"ce_temperature": args.ce_temperature if args.rerank else None,
"soft_recall_rescue_enabled": args.soft_recall_rescue,
"soft_recall_rescue_tail_k": (
args.soft_recall_rescue_tail_k if args.soft_recall_rescue else 0
),
"soft_recall_rescue_bm25_depth": (
args.soft_recall_rescue_bm25_depth if args.soft_recall_rescue else 0
),
"multi_query_enabled": args.multi_query,
"cache": {
"retrieval_enabled": args.retrieval_cache_enabled,
"retrieval_capacity": args.retrieval_cache_capacity,
"retrieval_ttl_seconds": args.retrieval_cache_ttl_seconds,
"llm_enabled": args.llm_cache_enabled,
"llm_capacity": args.llm_cache_capacity,
"llm_ttl_seconds": args.llm_cache_ttl_seconds,
},
"mmr_before_rerank": args.mmr_before_rerank,
"mmr_lambda": args.mmr_lambda if args.mmr_before_rerank else None,
"mmr_k": args.mmr_k if args.mmr_before_rerank else None,
"mmr_diversity_threshold": (
args.mmr_diversity_threshold
if (args.mmr_before_rerank and args.mmr_diversity_threshold > 0)
else None
),
"require_evidence": args.require_evidence,
"k_values": k_values,
"samples_total": len(samples),
"samples_total_before_filter": total_samples_before_filter,
"samples_filtered_out": filtered_out_samples,
"samples_with_ground_truth": sum(1 for s in samples if s.relevant_docs),
"metrics": metrics,
"evaluation": {
"failed_queries_for_manual_inspection": manual_inspection_failed_queries,
"failed_queries_for_manual_inspection_count": len(manual_inspection_failed_queries),
},
"diagnostics": {
"top1_unique_docs": len(top1_counts),
"top1_total_queries": top1_total,
"top1_most_common_doc_id": max_top1_doc,
"top1_most_common_doc_fraction": (max_top1_count / top1_total) if top1_total else 0.0,
"top1_distribution": top1_distribution,
"failure_analysis": {
"enabled": True,
"failure_count": len(failure_records),
"failure_bucket_counts": dict(failure_bucket_counts),
"failure_source_miss_counts": dict(miss_type_counts),
"failure_bucket_source_miss_counts": failure_bucket_source_counts_json,
"near_miss_threshold": args.failure_near_miss_threshold,
"manual_inspection_samples": manual_inspection_failed_queries,
},
},
"runs": [run.__dict__ for run in query_runs],
}
reranker_dataset_export: dict[str, object] | None = None
reranker_training_result: dict[str, object] | None = None
if args.export_reranker_train_jsonl or args.train_reranker:
out_jsonl = Path(args.export_reranker_train_jsonl or "data/reranker_train.jsonl")
context_build_result = build_reranker_training_contexts_from_failures(
failure_records=failure_records,
doc_text_map=doc_text_map,
max_negative_rank=max(1, args.reranker_train_max_negative_rank),
max_negatives=max(1, args.reranker_train_max_negatives),
ranking_cutoff_weight=max(0.1, args.reranker_train_weight_ranking_cutoff),
true_recall_weight=max(0.1, args.reranker_train_weight_true_recall),
default_weight=max(0.1, args.reranker_train_weight_default),
)
contexts = context_build_result.contexts
pair_stats = context_build_result.stats
out_jsonl.parent.mkdir(parents=True, exist_ok=True)
with out_jsonl.open("w", encoding="utf-8") as fp:
for row in contexts:
fp.write(json.dumps(row.model_dump(), ensure_ascii=False) + "\n")
reranker_dataset_export = {
"path": str(out_jsonl),
"contexts": len(contexts),
"stats": pair_stats.model_dump(),
"schema_version": "reranker_context_v1",
}
report["reranker_dataset_export"] = reranker_dataset_export
print(f"- reranker_dataset_export: {out_jsonl} ({len(contexts)} contexts)")
if args.train_reranker:
reranker_training_result = train_reranker_from_contexts_jsonl(
train_jsonl=out_jsonl,
doc_text_map=doc_text_map,
model_name=args.train_reranker_model,
out_dir=Path(args.train_reranker_out_dir),
epochs=max(1, args.train_reranker_epochs),
batch_size=max(1, args.train_reranker_batch_size),
warmup_steps=max(0, args.train_reranker_warmup_steps),
val_ratio=args.train_reranker_val_ratio,
seed=args.train_reranker_seed,
)
report["reranker_training"] = reranker_training_result.model_dump()
print(f"- reranker_training_out: {reranker_training_result.out_dir}")
print("Retrieval benchmark report")
print(f"- dataset: {args.dataset}")
print(f"- retriever: {args.retriever}")
print(f"- samples: {len(samples)}")
if args.require_evidence:
print(f"- require_evidence: true (filtered_out={filtered_out_samples})")
for key in sorted(metrics):
print(f"- {key}: {metrics[key]:.4f}")
if failure_records:
print("- failure_buckets:")
for bucket in (
"near_miss",
"fragmentation",
"ranking_cutoff_failure",
"true_recall_failure",
):
print(f" - {bucket}: {failure_bucket_counts.get(bucket, 0)}")
print("- failure_source_miss:")
for key in ("embedding_miss", "bm25_miss", "both_miss", "both_hit"):
print(f" - {key}: {miss_type_counts.get(key, 0)}")
print("- failure_bucket_source_miss:")
for bucket in (
"near_miss",
"fragmentation",
"ranking_cutoff_failure",
"true_recall_failure",
):
per_bucket = failure_bucket_source_counts.get(bucket, Counter())
print(
" - "
f"{bucket}: embedding_miss={per_bucket.get('embedding_miss', 0)}, "
f"bm25_miss={per_bucket.get('bm25_miss', 0)}, "
f"both_miss={per_bucket.get('both_miss', 0)}, "
f"both_hit={per_bucket.get('both_hit', 0)}"
)
print(
f"- failed_queries_for_manual_inspection: {min(len(failure_records), args.failure_sample_size)}"
)
if args.multi_query_llm_debug and args.multi_query_llm_expansion:
print(
"- multi_query_llm_debug: "
f"requested={llm_expansion_stats['requested_queries']}, "
f"generated={llm_expansion_stats['generated_queries']}, "
f"fallback={llm_expansion_stats['fallback_queries']}"
)
for item in llm_expansion_examples:
print(f" - llm_example_query: {item['query']}")
print(f" generated_preview: {item['generated_preview']}")
if args.out_json:
out_path = Path(args.out_json)
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
print(f"Saved JSON report to {out_path}")
logger.info("saved json report: %s", out_path)
logger.info("evaluation runner completed successfully")
def cmd_run_rag(args: argparse.Namespace) -> None:
# Command-scoped import keeps retrieval-only commands lean.
from generation.run_rag import run_rag
run_rag(
question=args.question,
provider=args.provider,
model=args.model,
top_k=args.top_k,
max_context_tokens=args.max_context_tokens,
faiss_path=args.faiss_path,
index_name=args.index,
embedding_model=args.embedding_model,
stream=args.stream,
max_tokens=args.max_tokens,
temperature=args.temperature,
top_p=args.top_p,
rerank=args.rerank,
reranker_model=args.reranker_model,
rerank_candidates=args.rerank_candidates,
llm_cache_enabled=args.llm_cache_enabled,
llm_cache_capacity=args.llm_cache_capacity,
llm_cache_ttl_seconds=args.llm_cache_ttl_seconds,
log_level=args.log_level,
log_path=args.log_path,
log_json=args.log_json,
)
def cmd_cleanup_faiss(args: argparse.Namespace) -> None:
# Command-scoped import keeps cleanup deps isolated.
from ingestion.cleaner import cleanup_faiss_db
result = cleanup_faiss_db(
persist_directory=args.faiss_path,
index_name=args.index,
drop_persist_directory=args.drop_persist_directory,
)
print(json.dumps(result, indent=2))
def cmd_build_faiss(args: argparse.Namespace) -> None:
# Command-scoped import keeps embedding deps isolated.
from embeddings.embedder import build_faiss_index, prepare_embedding_input
input_jsonl = args.input_jsonl
prepared_rows = 0
if args.prepare_input:
prepared_rows = prepare_embedding_input(
input_jsonl=args.rag_dataset,
output_jsonl=args.input_jsonl,
)
input_jsonl = args.input_jsonl
indexed_rows = build_faiss_index(
input_jsonl=input_jsonl,
persist_directory=args.faiss_path,
index_name=args.index,
model_name=args.embedding_model,
)
print(
json.dumps(
{
"prepare_input": args.prepare_input,
"rag_dataset": args.rag_dataset if args.prepare_input else None,
"input_jsonl": input_jsonl,
"prepared_rows": prepared_rows,
"indexed_rows": indexed_rows,
"faiss_path": args.faiss_path,
"index_name": args.index,
"embedding_model": args.embedding_model,
},
indent=2,
)
)
def cmd_reranker_pipeline(args: argparse.Namespace) -> None:
eval_args = argparse.Namespace(
# Core IO
dataset=args.dataset,
rag_dataset=args.rag_dataset,
faiss_path=args.faiss_path,
index=args.index,
out_json=args.out_json,
# Retrieval stack defaults (current tuned setup)
retriever="hybrid",
k_values=args.k_values,
embedding_model=args.embedding_model,
alpha=0.65,
hybrid_candidate_multiplier=80,
hybrid_max_per_group=1,
hybrid_rrf_k=80.0,
# Reranker stack defaults
rerank=True,
reranker_model=args.reranker_model,
rerank_candidates=40,
rerank_alpha=0.45,
rerank_top1_margin_lambda=args.rerank_top1_margin_lambda,
ce_calibration="zscore",
ce_temperature=1.0,
stratified_rerank_pool=True,
hard_negative_semantic_floor=0.12,
rerank_semantic_weight=0.55,
rerank_bm25_weight=0.45,
two_stage_rerank=True,
prefilter_candidates=40,
# Multi-query + rescue + MMR defaults
multi_query=True,
multi_query_variants=3,
multi_query_rrf_k=60,
multi_query_llm_expansion=args.multi_query_llm_expansion,
multi_query_llm_provider=args.multi_query_llm_provider,
multi_query_llm_model=args.multi_query_llm_model,
multi_query_llm_api_base=args.multi_query_llm_api_base,
multi_query_llm_api_key=args.multi_query_llm_api_key,
multi_query_llm_timeout_seconds=args.multi_query_llm_timeout_seconds,
multi_query_llm_retries=args.multi_query_llm_retries,
multi_query_llm_debug=args.multi_query_llm_debug,
llm_config_path=args.llm_config_path,
retrieval_cache_enabled=args.retrieval_cache_enabled,
retrieval_cache_capacity=args.retrieval_cache_capacity,
retrieval_cache_ttl_seconds=args.retrieval_cache_ttl_seconds,
llm_cache_enabled=args.llm_cache_enabled,
llm_cache_capacity=args.llm_cache_capacity,
llm_cache_ttl_seconds=args.llm_cache_ttl_seconds,
log_level=args.log_level,
log_path=args.log_path,
log_json=args.log_json,
soft_recall_rescue=True,
soft_recall_rescue_tail_k=20,
soft_recall_rescue_bm25_depth=200,
mmr_before_rerank=True,
mmr_lambda=0.82,
mmr_k=30,
mmr_diversity_threshold=0.0,
# Failure analysis defaults
require_evidence=True,
failure_near_miss_threshold=0.80,
failure_sample_size=20,
# Integrated reranker dataset + train toggles
export_reranker_train_jsonl=args.export_reranker_train_jsonl,
reranker_train_max_negative_rank=20,
reranker_train_max_negatives=16,
reranker_train_weight_ranking_cutoff=2.0,
reranker_train_weight_true_recall=0.3,
reranker_train_weight_default=1.0,
train_reranker=args.train_reranker,
train_reranker_model=args.train_reranker_model,
train_reranker_out_dir=args.train_reranker_out_dir,
train_reranker_epochs=args.train_reranker_epochs,
train_reranker_batch_size=args.train_reranker_batch_size,
train_reranker_warmup_steps=args.train_reranker_warmup_steps,
train_reranker_val_ratio=args.train_reranker_val_ratio,
train_reranker_seed=args.train_reranker_seed,
)
cmd_evaluation_runner(eval_args)
def cmd_build_evaluation_dataset(args: argparse.Namespace) -> None:
# Command-scoped import keeps evaluation dataset builder optional.
from evaluation.dataset import build_evaluation_dataset
count, stats = build_evaluation_dataset(
rag_path=Path(args.rag),
eval_json_path=Path(args.eval),
out_path=Path(args.out),
fuzzy_ratio=args.fuzzy_ratio,
lexical_min_hits=args.lexical_min_hits,
max_chunk_ids=args.max_chunk_ids,
semantic_fallback=not args.no_semantic_fallback,
semantic_model=args.semantic_model,
semantic_min_score=args.semantic_min_score,
max_gt_url_share=args.max_gt_url_share,
target_multi_gt_share=args.target_multi_gt_share,
keep_max_ids_for_multi=args.keep_max_ids_for_multi,
excerpt_max=args.excerpt_max,
)
print(f"Wrote {args.out} ({count} records).")
print("Stats:", json.dumps(stats, indent=2))
def cmd_dataset_audit(args: argparse.Namespace) -> None:
# Command-scoped import keeps audit utilities optional.
from commands.dataset_audit import audit
report = audit(Path(args.rag), Path(args.eval))
print(json.dumps(report, ensure_ascii=False, indent=2))
if args.out:
out_path = Path(args.out)
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
def cmd_build_reranker_dataset(args: argparse.Namespace) -> None:
# Command-scoped import keeps reranker dataset utilities optional.
from commands.build_reranker_dataset import build_contexts, load_chunk_texts
report = json.loads(Path(args.eval_report).read_text(encoding="utf-8"))
chunk_texts = load_chunk_texts(Path(args.rag_dataset))
contexts, stats = build_contexts(
report=report,
chunk_texts=chunk_texts,
max_negative_rank=max(1, args.max_negative_rank),
max_negatives=max(1, args.max_negatives),
ranking_cutoff_weight=max(0.1, args.ranking_cutoff_weight),
true_recall_weight=max(0.1, args.true_recall_weight),
default_weight=max(0.1, args.default_weight),
)
out_path = Path(args.out)
out_path.parent.mkdir(parents=True, exist_ok=True)
with out_path.open("w", encoding="utf-8") as fp:
for row in contexts:
fp.write(json.dumps(row, ensure_ascii=False) + "\n")
print(f"Wrote {out_path} ({len(contexts)} contexts).")
print("Stats:", json.dumps(stats, indent=2))
def cmd_train_reranker(args: argparse.Namespace) -> None:
# Heavy training deps are imported lazily for non-training commands.
from sentence_transformers.cross_encoder import CrossEncoder
from sentence_transformers.cross_encoder.evaluation import CEBinaryClassificationEvaluator
from torch.utils.data import DataLoader
from commands.train_reranker import load_pairwise_samples
from ingestion.loaders import load_chunk_texts
chunk_texts = load_chunk_texts(Path(args.rag_dataset))
train_examples, val_examples = load_pairwise_samples(
Path(args.train_jsonl),
seed=args.seed,
val_ratio=args.val_ratio,
chunk_texts=chunk_texts,
)
if not train_examples:
raise ValueError("No training examples loaded. Check --train-jsonl contents.")
model = CrossEncoder(args.model, num_labels=1, max_length=512)
train_loader = DataLoader(train_examples, shuffle=True, batch_size=args.batch_size)
evaluator = None
if val_examples:
evaluator = CEBinaryClassificationEvaluator.from_input_examples(
val_examples,
name="failure-driven-val",
)
out_dir = Path(args.out_dir)
out_dir.mkdir(parents=True, exist_ok=True)
model.fit(
train_dataloader=train_loader,
evaluator=evaluator,
epochs=args.epochs,
warmup_steps=args.warmup_steps,
output_path=str(out_dir),
show_progress_bar=True,
)
model.save(str(out_dir))
print(
json.dumps(
{
"out_dir": str(out_dir),
"train_examples": len(train_examples),
"val_examples": len(val_examples),
"model": args.model,
},
indent=2,
)
)
def cmd_run_experiments(args: argparse.Namespace) -> None:
# Command-scoped import keeps experiment stack optional.
from experiments.run_experiments import run_experiments
models = [x.strip() for x in args.models.split(",") if x.strip()]
run_experiments(
question=args.question,
models=models,
top_k=args.top_k,
max_context_tokens=args.max_context_tokens,
faiss_path=args.faiss_path,
index_name=args.index,
embedding_model=args.embedding_model,
log_path=args.log_path,
llm_config_path=args.llm_config_path,
)
def build_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description="Single entrypoint for project workflows.")
parser.add_argument(
"--config",
help="Path to CLI defaults JSON (default: project root cli.defaults.json).",
)
subparsers = parser.add_subparsers(dest="command", required=True)
build_parser_cmd = subparsers.add_parser(
"build_parser",
aliases=["build-parser"],
help="Run parser pipeline and build rag_dataset.jsonl",
)
build_parser_cmd.add_argument(
"--output",
)
build_parser_cmd.add_argument(
"--min-tokens",
type=int,
)
build_parser_cmd.add_argument(
"--max-tokens",
type=int,
)
build_parser_cmd.add_argument(
"--overlap-ratio",
type=float,
)
build_parser_cmd.add_argument(
"--min-output-chunk-tokens",
type=int,
)
build_parser_cmd.add_argument(
"--max-output-chunk-tokens",
type=int,
)
build_parser_cmd.add_argument(
"--max-chunks-per-url",
type=int,
)
build_parser_cmd.add_argument(
"--max-chunks-per-category",
type=int,
)
build_parser_cmd.add_argument(
"--sources-config",
)
build_parser_cmd.add_argument(
"--chunker-mode",
choices=("token", "semantic_dynamic"),
)
build_parser_cmd.add_argument(
"--near-duplicate-jaccard",
type=float,
help="Skip near-duplicate chunks from the same URL when similarity >= threshold (0 disables).",
)
build_parser_cmd.add_argument(
"--embedding-model",
)
build_parser_cmd.add_argument("--log-level", choices=("DEBUG", "INFO", "WARNING", "ERROR"))
build_parser_cmd.add_argument(
"--log-path",
)
build_parser_cmd.add_argument("--log-json", action="store_true")
build_parser_cmd.set_defaults(handler=cmd_build_parser)
demo_cmd = subparsers.add_parser(
"demo_retrieval", help="Run BM25/semantic/hybrid retrieval demo."
)
demo_cmd.add_argument(
"--query",
"-q",
)
demo_cmd.add_argument(
"--top-k",
"-k",
type=int,
)
demo_cmd.add_argument(
"--model",
"-m",
)
demo_cmd.add_argument(
"--dataset",
)
demo_cmd.add_argument(
"--faiss-path",
)
demo_cmd.add_argument(
"--index",
)
demo_cmd.add_argument("--rerank", action="store_true")
demo_cmd.add_argument(
"--reranker-model",
)
demo_cmd.add_argument(
"--rerank-candidates",
type=int,
)
demo_cmd.set_defaults(handler=cmd_demo_retrieval)
eval_cmd = subparsers.add_parser(
"evaluation_runner",
aliases=["evaluate"],
help="Run retrieval benchmark over eval dataset.",
)
eval_cmd.add_argument(
"--dataset",
)
eval_cmd.add_argument(
"--retriever",
choices=("semantic", "bm25", "hybrid"),
)
eval_cmd.add_argument(
"--k-values",
)
eval_cmd.add_argument(
"--rag-dataset",
)
eval_cmd.add_argument(
"--faiss-path",