-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvectorless_rag_guide.html
More file actions
1924 lines (1673 loc) · 96.3 KB
/
Copy pathvectorless_rag_guide.html
File metadata and controls
1924 lines (1673 loc) · 96.3 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vectorless RAG — Complete Engineering Guide</title>
<style>
:root {
--bg: #0d0f12;
--bg2: #151820;
--bg3: #1c2030;
--surface: #1e2235;
--border: rgba(255,255,255,0.08);
--border2: rgba(255,255,255,0.14);
--text: #e2e5f0;
--muted: #8892a8;
--accent: #4f8ef7;
--accent2: #7c6af7;
--green: #3ecf8e;
--amber: #f7a94f;
--red: #f76f6f;
--teal: #3ecfc8;
--pink: #f76fa8;
--mono: 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
--sans: 'Inter', system-ui, sans-serif;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
background: var(--bg);
color: var(--text);
font-family: var(--sans);
font-size: 15px;
line-height: 1.7;
display: flex;
}
/* NAV SIDEBAR */
#nav {
width: 260px;
min-height: 100vh;
background: var(--bg2);
border-right: 1px solid var(--border);
padding: 24px 0;
position: fixed;
top: 0; left: 0;
overflow-y: auto;
z-index: 100;
}
#nav-logo {
padding: 0 20px 24px;
border-bottom: 1px solid var(--border);
margin-bottom: 16px;
}
#nav-logo .title { font-size: 13px; font-weight: 600; color: var(--accent); letter-spacing: 0.05em; text-transform: uppercase; }
#nav-logo .sub { font-size: 11px; color: var(--muted); margin-top: 3px; }
#nav a {
display: block;
padding: 6px 20px;
font-size: 13px;
color: var(--muted);
text-decoration: none;
transition: color 0.15s, background 0.15s;
border-left: 2px solid transparent;
}
#nav a:hover { color: var(--text); background: rgba(255,255,255,0.04); }
#nav a.active { color: var(--accent); border-left-color: var(--accent); background: rgba(79,142,247,0.08); }
#nav .nav-section {
font-size: 10px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
color: rgba(255,255,255,0.25);
padding: 16px 20px 4px;
}
/* MAIN CONTENT */
#main {
margin-left: 260px;
flex: 1;
max-width: 900px;
padding: 48px 52px 120px;
}
/* TYPOGRAPHY */
h1 { font-size: 32px; font-weight: 700; line-height: 1.2; margin-bottom: 12px; }
h2 { font-size: 22px; font-weight: 600; margin: 56px 0 16px; padding-top: 16px; color: var(--text); }
h3 { font-size: 16px; font-weight: 600; margin: 32px 0 10px; color: var(--accent); }
h4 { font-size: 14px; font-weight: 600; margin: 20px 0 8px; color: var(--amber); }
p { margin-bottom: 14px; color: var(--text); }
ul, ol { padding-left: 20px; margin-bottom: 14px; }
li { margin-bottom: 6px; }
strong { color: #fff; font-weight: 600; }
em { color: var(--amber); font-style: normal; font-weight: 500; }
.lead {
font-size: 17px;
color: var(--muted);
line-height: 1.8;
margin-bottom: 32px;
border-left: 3px solid var(--accent);
padding-left: 16px;
}
/* SECTION ANCHORS */
section { scroll-margin-top: 24px; }
/* BADGES */
.badge {
display: inline-block;
font-size: 11px;
font-weight: 600;
padding: 2px 8px;
border-radius: 4px;
letter-spacing: 0.04em;
text-transform: uppercase;
margin-right: 6px;
}
.badge-blue { background: rgba(79,142,247,0.15); color: var(--accent); }
.badge-green { background: rgba(62,207,142,0.15); color: var(--green); }
.badge-red { background: rgba(247,111,111,0.15); color: var(--red); }
.badge-amber { background: rgba(247,169,79,0.15); color: var(--amber); }
.badge-teal { background: rgba(62,207,200,0.15); color: var(--teal); }
.badge-purple { background: rgba(124,106,247,0.15); color: var(--accent2); }
/* CALLOUT BOXES */
.callout {
border-radius: 8px;
padding: 16px 20px;
margin: 20px 0;
border-left: 3px solid;
font-size: 14px;
}
.callout-info { background: rgba(79,142,247,0.08); border-color: var(--accent); }
.callout-warn { background: rgba(247,169,79,0.08); border-color: var(--amber); }
.callout-green { background: rgba(62,207,142,0.08); border-color: var(--green); }
.callout-danger { background: rgba(247,111,111,0.08); border-color: var(--red); }
.callout p { margin-bottom: 0; }
.callout-title { font-weight: 700; font-size: 12px; letter-spacing: 0.06em; text-transform: uppercase; margin-bottom: 6px; }
/* CODE BLOCKS */
pre {
background: #0a0c10;
border: 1px solid var(--border2);
border-radius: 8px;
padding: 20px;
overflow-x: auto;
margin: 16px 0 24px;
font-family: var(--mono);
font-size: 13px;
line-height: 1.65;
}
code {
font-family: var(--mono);
font-size: 13px;
background: rgba(255,255,255,0.06);
padding: 2px 6px;
border-radius: 3px;
color: var(--teal);
}
pre code { background: none; padding: 0; color: inherit; }
/* Syntax coloring in pre */
.kw { color: #c792ea; } /* keyword */
.fn { color: #82aaff; } /* function */
.st { color: #c3e88d; } /* string */
.cm { color: #546e7a; font-style: italic; } /* comment */
.nu { color: #f78c6c; } /* number */
.cl { color: var(--teal); } /* class */
.de { color: var(--amber); } /* decorator */
/* TABLES */
table {
width: 100%;
border-collapse: collapse;
margin: 16px 0 24px;
font-size: 13px;
}
th {
background: var(--bg3);
color: var(--muted);
font-weight: 600;
font-size: 11px;
text-transform: uppercase;
letter-spacing: 0.06em;
padding: 10px 14px;
text-align: left;
border-bottom: 1px solid var(--border2);
}
td {
padding: 10px 14px;
border-bottom: 1px solid var(--border);
vertical-align: top;
line-height: 1.5;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: rgba(255,255,255,0.02); }
/* COMPARISON TABLE */
.cmp-yes { color: var(--green); font-weight: 600; }
.cmp-no { color: var(--red); font-weight: 600; }
.cmp-par { color: var(--amber); font-weight: 600; }
/* ARCH DIAGRAM */
.arch-diagram {
background: var(--bg3);
border: 1px solid var(--border2);
border-radius: 10px;
padding: 28px;
margin: 24px 0;
font-family: var(--mono);
font-size: 13px;
line-height: 1.8;
overflow-x: auto;
}
.arch-diagram .box { color: var(--accent); }
.arch-diagram .arrow { color: var(--muted); }
.arch-diagram .label { color: var(--amber); }
.arch-diagram .note { color: var(--green); }
/* FOLDER TREE */
.tree {
background: #0a0c10;
border: 1px solid var(--border2);
border-radius: 8px;
padding: 20px;
margin: 16px 0 24px;
font-family: var(--mono);
font-size: 13px;
line-height: 1.9;
}
.tree .dir { color: var(--accent); }
.tree .file { color: var(--text); }
.tree .desc { color: var(--muted); }
/* PIPELINE STEPS */
.pipeline {
display: flex;
flex-direction: column;
gap: 0;
margin: 20px 0 28px;
}
.pipe-step {
display: flex;
gap: 16px;
position: relative;
}
.pipe-step::before {
content: '';
position: absolute;
left: 19px;
top: 44px;
bottom: -1px;
width: 2px;
background: var(--border2);
}
.pipe-step:last-child::before { display: none; }
.pipe-num {
width: 40px;
height: 40px;
border-radius: 50%;
background: var(--bg3);
border: 1px solid var(--border2);
display: flex;
align-items: center;
justify-content: center;
font-size: 13px;
font-weight: 700;
color: var(--accent);
flex-shrink: 0;
margin-top: 2px;
}
.pipe-body { padding-bottom: 28px; }
.pipe-title { font-weight: 600; font-size: 15px; margin-bottom: 4px; }
.pipe-desc { font-size: 13px; color: var(--muted); }
/* COMPARISON GRID */
.cmp-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 20px;
margin: 20px 0 28px;
}
.cmp-card {
background: var(--bg3);
border: 1px solid var(--border);
border-radius: 10px;
padding: 20px;
}
.cmp-card h4 { margin-top: 0; }
.cmp-card ul { margin-bottom: 0; }
/* PACKAGE LIST */
.pkg-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 12px;
margin: 16px 0 24px;
}
.pkg-item {
background: var(--bg3);
border: 1px solid var(--border);
border-radius: 8px;
padding: 12px 14px;
}
.pkg-name { font-family: var(--mono); font-size: 13px; color: var(--teal); font-weight: 600; }
.pkg-desc { font-size: 12px; color: var(--muted); margin-top: 4px; }
.pkg-badge { margin-top: 6px; }
/* DIFF TABLE */
.diff-add { color: var(--green); }
.diff-rem { color: var(--red); text-decoration: line-through; opacity: 0.7; }
.diff-mod { color: var(--amber); }
/* HR */
hr { border: none; border-top: 1px solid var(--border); margin: 48px 0; }
/* HERO */
.hero {
background: linear-gradient(135deg, rgba(79,142,247,0.08) 0%, rgba(124,106,247,0.05) 100%);
border: 1px solid rgba(79,142,247,0.2);
border-radius: 12px;
padding: 36px 40px;
margin-bottom: 40px;
}
.hero-tag {
font-size: 11px;
font-weight: 700;
letter-spacing: 0.1em;
text-transform: uppercase;
color: var(--accent);
margin-bottom: 12px;
}
.hero h1 { font-size: 28px; }
.hero p { color: var(--muted); margin-top: 12px; margin-bottom: 0; }
/* TOC pills at top */
.toc-pills {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin: 24px 0 40px;
}
.toc-pill {
background: var(--bg3);
border: 1px solid var(--border2);
border-radius: 20px;
padding: 6px 14px;
font-size: 12px;
color: var(--muted);
text-decoration: none;
transition: all 0.15s;
}
.toc-pill:hover { background: var(--surface); color: var(--text); border-color: var(--accent); }
/* SCROLLBAR */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 3px; }
@media (max-width: 900px) {
#nav { display: none; }
#main { margin-left: 0; padding: 24px 20px 80px; }
}
</style>
</head>
<body>
<!-- ========== SIDEBAR ========== -->
<nav id="nav">
<div id="nav-logo">
<div class="title">Vectorless RAG</div>
<div class="sub">Complete Engineering Guide</div>
</div>
<div class="nav-section">Overview</div>
<a href="#what-is">What is Vectorless RAG?</a>
<a href="#arch-diff">Architecture Differences</a>
<a href="#tradeoffs">Tradeoffs & When to Use</a>
<div class="nav-section">Analysis</div>
<a href="#current-analysis">Your Current System</a>
<a href="#what-stays">What Stays / Changes</a>
<div class="nav-section">Design</div>
<a href="#architecture">Complete Architecture</a>
<a href="#folder">Folder Structure</a>
<a href="#pipelines">Pipeline Designs</a>
<div class="nav-section">Stack</div>
<a href="#packages">Packages & Libraries</a>
<a href="#models">Models & Hardware</a>
<div class="nav-section">Implementation</div>
<a href="#workflow">Coding Workflow</a>
<a href="#code-config">config/settings.py</a>
<a href="#code-loader">ingestion/loader.py</a>
<a href="#code-chunker">ingestion/chunker.py</a>
<a href="#code-indexer">retrieval/indexer.py</a>
<a href="#code-search">retrieval/search.py</a>
<a href="#code-reranker">retrieval/reranker.py</a>
<a href="#code-cache">retrieval/cache.py</a>
<a href="#code-llm">generation/llm.py</a>
<a href="#code-main">main.py (FastAPI)</a>
<div class="nav-section">Comparison</div>
<a href="#what-changed">What Changed</a>
<a href="#latency">Latency Analysis</a>
<div class="nav-section">Advanced</div>
<a href="#optimization">Optimization</a>
<a href="#enterprise">Enterprise Best Practices</a>
<a href="#evaluation">Evaluation Methods</a>
<a href="#future">Future Upgrades</a>
</nav>
<!-- ========== MAIN ========== -->
<main id="main">
<!-- HERO -->
<div class="hero">
<div class="hero-tag">Senior Engineering Guide</div>
<h1>Vectorless RAG — Complete Build Reference</h1>
<p>A production-grade, senior-engineer-level guide to designing, building, and optimizing a Vectorless RAG system from scratch — based on your existing Hybrid RAG (BM25 + FAISS + RRF) codebase.</p>
</div>
<div class="toc-pills">
<a class="toc-pill" href="#what-is">1. What is Vectorless RAG</a>
<a class="toc-pill" href="#current-analysis">2. Your System Analysis</a>
<a class="toc-pill" href="#architecture">3. Architecture Design</a>
<a class="toc-pill" href="#packages">4. Full Stack</a>
<a class="toc-pill" href="#workflow">5. Coding Workflow</a>
<a class="toc-pill" href="#code-config">6. Complete Codebase</a>
<a class="toc-pill" href="#what-changed">7. What Changed</a>
<a class="toc-pill" href="#optimization">8. Advanced Topics</a>
</div>
<!-- ====================================================== -->
<section id="what-is">
<h2>1. What Is Vectorless RAG?</h2>
<p class="lead">Vectorless RAG is a retrieval-augmented generation architecture that <strong>completely eliminates dense vector embeddings and approximate nearest-neighbor (ANN) search</strong> from the retrieval pipeline, replacing them with sparse lexical retrieval (BM25 or TF-IDF), structured metadata filtering, and cross-encoder reranking.</p>
<h3>Core Architecture</h3>
<p>The retrieval chain is:</p>
<div class="arch-diagram">
<span class="label">Query</span>
<span class="arrow">──►</span> <span class="box">[Query Preprocessing]</span> <span class="cm">← normalize, spell-correct, expand</span>
<span class="arrow">──►</span> <span class="box">[BM25 Index]</span> <span class="cm">← in-memory or Elasticsearch</span>
<span class="arrow">──►</span> <span class="box">[Metadata Filter]</span> <span class="cm">← file, date, section type</span>
<span class="arrow">──►</span> <span class="box">[Top-K Candidates]</span> <span class="cm">← e.g. top-50 BM25 hits</span>
<span class="arrow">──►</span> <span class="box">[Cross-Encoder Reranker]</span> <span class="cm">← ms-marco MiniLM or similar</span>
<span class="arrow">──►</span> <span class="box">[Top-N Passages]</span> <span class="cm">← e.g. top-5 reranked</span>
<span class="arrow">──►</span> <span class="box">[LLM Generation]</span> <span class="cm">← Ollama / Claude / OpenAI</span>
<span class="arrow">──►</span> <span class="label">Answer + Citations</span>
</div>
<div class="callout callout-info">
<div class="callout-title">Key insight</div>
<p>The cross-encoder reranker is what makes this competitive with vector search. It uses a small transformer that reads <em>query + passage together</em> as a pair, giving you semantic understanding at the reranking stage — without needing embeddings at retrieval time.</p>
</div>
<h3>How It Differs from Traditional Vector RAG</h3>
<table>
<thead><tr><th>Dimension</th><th>Traditional Vector RAG</th><th>Vectorless RAG</th></tr></thead>
<tbody>
<tr><td>Retrieval mechanism</td><td>Dense embedding → ANN (FAISS, Chroma, Pinecone)</td><td>BM25 lexical scoring → inverted index</td></tr>
<tr><td>Query representation</td><td>Float vector (768–3072 dims)</td><td>Token frequency bag-of-words</td></tr>
<tr><td>Index size</td><td>Scales with vector dims × chunk count (large)</td><td>Inverted index (compact)</td></tr>
<tr><td>Infrastructure</td><td>GPU for embedding, ANN index service</td><td>CPU only, plain Python or Elasticsearch</td></tr>
<tr><td>Semantic understanding</td><td>At retrieval (embedding model)</td><td>At reranking (cross-encoder)</td></tr>
<tr><td>Latency profile</td><td>Fast at scale, embedding is bottleneck</td><td>BM25 is <5ms, reranker is main cost</td></tr>
<tr><td>Explainability</td><td>Black-box similarity score</td><td>Token-level TF-IDF scores (auditable)</td></tr>
<tr><td>Cold start</td><td>Must embed all docs (slow)</td><td>BM25 indexing is fast, no GPU needed</td></tr>
</tbody>
</table>
<h3>How It Differs from Your Hybrid RAG (BM25 + FAISS + RRF)</h3>
<div class="cmp-grid">
<div class="cmp-card">
<h4>Your Hybrid RAG</h4>
<ul>
<li>Two retrieval signals fused: FAISS (dense) + BM25 (sparse)</li>
<li>RRF fusion requires both to run simultaneously</li>
<li>sentence-transformers GPU embedding at query time</li>
<li>FAISS index loaded in RAM (~100–500MB for medium corpora)</li>
<li>ChromaDB + FAISS dual storage</li>
<li>Complexity: ~5 retrieval stages (embed → ANN → BM25 → RRF → filter)</li>
</ul>
</div>
<div class="cmp-card">
<h4>Vectorless RAG</h4>
<ul>
<li>Single retrieval signal: BM25 only</li>
<li>No fusion step — just BM25 then rerank</li>
<li>No query-time GPU embedding at all</li>
<li>BM25 index fits in RAM as a Python dict</li>
<li>One store: JSON file or Elasticsearch</li>
<li>Complexity: ~3 retrieval stages (BM25 → filter → rerank)</li>
</ul>
</div>
</div>
<h3>Core Retrieval Concepts Explained</h3>
<h4>BM25 (Best Match 25)</h4>
<p>BM25 is an extension of TF-IDF with term saturation and document length normalization. The score for a document D given query Q is:</p>
<pre><code>score(D, Q) = Σ IDF(qi) × [ f(qi,D) × (k1+1) ] / [ f(qi,D) + k1×(1 - b + b×|D|/avgdl) ]
k1 = 1.2 (term frequency saturation)
b = 0.75 (document length normalization)
f(qi,D) = frequency of term qi in document D
avgdl = average document length in corpus</code></pre>
<p>Key property: doubling a term's count does NOT double the score (saturation). This makes it robust against keyword stuffing and verbosity.</p>
<h4>Sparse vs Dense Retrieval</h4>
<table>
<thead><tr><th>Property</th><th>Sparse (BM25)</th><th>Dense (Embeddings)</th></tr></thead>
<tbody>
<tr><td>Representation</td><td>High-dimensional sparse vector (vocab size)</td><td>Low-dim dense vector (768–3072)</td></tr>
<tr><td>Matching</td><td>Exact token overlap</td><td>Semantic similarity</td></tr>
<tr><td>Acronyms / codes</td><td class="cmp-yes">Excellent</td><td class="cmp-no">Poor</td></tr>
<tr><td>Cross-lingual</td><td class="cmp-no">Poor</td><td class="cmp-yes">Excellent</td></tr>
<tr><td>Domain adaptation</td><td class="cmp-yes">Immediate (no retraining)</td><td class="cmp-no">Requires fine-tuning</td></tr>
<tr><td>OOV terms</td><td class="cmp-yes">Handles naturally</td><td class="cmp-no">Maps to noise</td></tr>
<tr><td>Compute at index time</td><td class="cmp-yes">Very fast (counting)</td><td class="cmp-no">Heavy (embedding forward pass)</td></tr>
<tr><td>Compute at query time</td><td class="cmp-yes">Microseconds</td><td class="cmp-par">GPU: ~5ms / CPU: ~50ms</td></tr>
</tbody>
</table>
<h4>Cross-Encoder Reranking</h4>
<p>A cross-encoder is a transformer fine-tuned to score relevance of a (query, passage) pair by processing both as a single input sequence:</p>
<pre><code>[CLS] query tokens [SEP] passage tokens [SEP] → scalar relevance score</code></pre>
<p>Unlike bi-encoders (used for embeddings), cross-encoders attend across the full pair — making them significantly more accurate. They are too slow for first-stage retrieval (you can't run them on all 10,000 chunks), but perfect for reranking top-50 BM25 candidates down to top-5.</p>
<h4>Why Enterprises Are Moving Toward Vectorless/Sparse-Heavy Architectures</h4>
<ul>
<li><strong>Auditability:</strong> Compliance teams can inspect why a document was retrieved (token scores) vs. "the cosine similarity was 0.87."</li>
<li><strong>Infrastructure cost:</strong> No GPU inference servers, no vector DB SaaS ($$$), no embedding re-indexing on model updates.</li>
<li><strong>Acronym/code precision:</strong> Legal, finance, medical, and government docs are full of exact identifiers — BM25 matches <code>TPDS</code>, <code>Section 12(3)(b)</code>, or <code>IDD-2024-Q3</code> exactly.</li>
<li><strong>Staleness immunity:</strong> BM25 doesn't drift — there's no "embedding model updated, must re-index 500k docs" event.</li>
<li><strong>Operational simplicity:</strong> One index, one retrieval stage, one system to monitor.</li>
</ul>
<h3>Advantages & Disadvantages</h3>
<div class="cmp-grid">
<div class="cmp-card">
<h4 style="color:var(--green)">✓ Advantages</h4>
<ul>
<li>No GPU required in production</li>
<li>Sub-millisecond BM25 retrieval</li>
<li>Handles rare tokens, codes, names perfectly</li>
<li>No re-indexing when models update</li>
<li>Explainable token-level scores</li>
<li>Simpler infrastructure (half the services)</li>
<li>Lower memory footprint (no embedding matrix)</li>
<li>Scales to large corpora with Elasticsearch</li>
<li>Deterministic — same query → same results</li>
</ul>
</div>
<div class="cmp-card">
<h4 style="color:var(--red)">✗ Disadvantages</h4>
<ul>
<li>Vocabulary gap: "automobile" ≠ "car" (unless expanded)</li>
<li>Cannot handle paraphrase retrieval natively</li>
<li>Multilingual queries harder (BM25 is language-specific)</li>
<li>Reranker adds 50–200ms latency for large candidate sets</li>
<li>No zero-shot semantic search across entirely new domains</li>
<li>Query expansion must be explicitly engineered</li>
</ul>
</div>
</div>
<h3>When Vectorless RAG Performs Better</h3>
<ul>
<li>Domain-specific corpora with dense jargon, codes, identifiers</li>
<li>Legal, financial, government document retrieval</li>
<li>Low-latency requirements where GPU cold start is unacceptable</li>
<li>Edge / on-prem deployments with no GPU budget</li>
<li>Compliance environments requiring retrieval explainability</li>
<li>Corpora that update frequently (re-indexing BM25 is instant)</li>
</ul>
<h3>When Vector RAG Still Wins</h3>
<ul>
<li>Semantic paraphrase queries: "What did the report say about economic growth?" (no overlap with "GDP expansion")</li>
<li>Cross-lingual retrieval</li>
<li>Image/multimodal retrieval</li>
<li>Very open-domain consumer Q&A</li>
</ul>
</section>
<hr>
<!-- ====================================================== -->
<section id="current-analysis">
<h2>2. Analysis of Your Current Hybrid RAG Architecture</h2>
<div class="callout callout-warn">
<div class="callout-title">Key finding</div>
<p>Your existing system is already BM25-dominant (BM25_WEIGHT = 0.60, FAISS_WEIGHT = 0.40), meaning the vector component contributes less than half the retrieval signal. This makes it an excellent candidate for dropping FAISS entirely.</p>
</div>
<h3 id="what-stays">Component Disposition</h3>
<table>
<thead><tr><th>Component</th><th>File</th><th>Disposition</th><th>Reason</th></tr></thead>
<tbody>
<tr><td>PDF loader (pdfplumber + OCR)</td><td>loader.py</td><td><span class="badge badge-green">Keep</span></td><td>Solid extraction, table-aware</td></tr>
<tr><td>Table extraction</td><td>loader.py</td><td><span class="badge badge-green">Keep</span></td><td>Still needed for structured data</td></tr>
<tr><td>OCR pipeline</td><td>ocr.py</td><td><span class="badge badge-green">Keep</span></td><td>Required for scanned docs</td></tr>
<tr><td>Recursive chunker</td><td>chunker.py</td><td><span class="badge badge-green">Keep</span></td><td>Language-agnostic, well-tuned</td></tr>
<tr><td>BM25 tokenizer</td><td>search.py</td><td><span class="badge badge-green">Keep</span></td><td>Preserve email/code tokenization</td></tr>
<tr><td>Query normalization</td><td>search.py</td><td><span class="badge badge-green">Keep</span></td><td>Spelling map is domain-valuable</td></tr>
<tr><td>Query variant generation</td><td>search.py</td><td><span class="badge badge-amber">Redesign</span></td><td>Simplify — no need for 3 variants when BM25 dominates</td></tr>
<tr><td>Metadata filter logic</td><td>search.py</td><td><span class="badge badge-green">Keep</span></td><td>File-based routing is correct</td></tr>
<tr><td>Language detection</td><td>search.py</td><td><span class="badge badge-green">Keep</span></td><td>Gujarati-English mixed docs still need it</td></tr>
<tr><td>LLM generation</td><td>llm.py</td><td><span class="badge badge-green">Keep</span></td><td>Generation is model-agnostic</td></tr>
<tr><td>FastAPI backend</td><td>main.py</td><td><span class="badge badge-green">Keep</span></td><td>Clean API contract</td></tr>
<tr><td>SentenceTransformer embedding</td><td>embedder.py</td><td><span class="badge badge-red">Remove</span></td><td>No more dense retrieval</td></tr>
<tr><td>FAISS index</td><td>vector_store.py</td><td><span class="badge badge-red">Remove</span></td><td>Replaced by pure BM25</td></tr>
<tr><td>ChromaDB</td><td>vector_store.py</td><td><span class="badge badge-red">Remove</span></td><td>JSON store replaces it</td></tr>
<tr><td>RRF fusion</td><td>search.py</td><td><span class="badge badge-red">Remove</span></td><td>Nothing to fuse without FAISS</td></tr>
<tr><td>Numpy/scipy ANN</td><td>search.py</td><td><span class="badge badge-red">Remove</span></td><td>Not needed</td></tr>
<tr><td>— (new) Cross-encoder reranker</td><td>reranker.py</td><td><span class="badge badge-blue">Add</span></td><td>Semantic understanding at rerank stage</td></tr>
<tr><td>— (new) Query expansion</td><td>search.py</td><td><span class="badge badge-blue">Add</span></td><td>Synonym/abbreviation expansion</td></tr>
<tr><td>— (new) Response cache</td><td>cache.py</td><td><span class="badge badge-blue">Add</span></td><td>Redis or in-memory TTL cache</td></tr>
</tbody>
</table>
<h3>What Changes in the Retrieval Pipeline</h3>
<p>Old: <code>embed(query) → FAISS.search() + BM25.score() → RRF_fusion() → metadata_filter() → LLM</code></p>
<p>New: <code>expand(query) → BM25.search() → metadata_filter() → cross_encoder.rerank() → LLM</code></p>
<h3>Infrastructure Simplification</h3>
<table>
<thead><tr><th>Before</th><th>After</th><th>Savings</th></tr></thead>
<tbody>
<tr><td>ChromaDB (vector DB service)</td><td>JSON file / SQLite</td><td>0 infra cost, no port conflicts</td></tr>
<tr><td>FAISS index (RAM)</td><td>BM25 object (RAM)</td><td>~70% smaller index footprint</td></tr>
<tr><td>sentence-transformers (500MB model)</td><td>Cross-encoder (50–130MB)</td><td>Smaller, faster, no GPU needed</td></tr>
<tr><td>numpy ANN operations</td><td>Rank-based BM25 scoring</td><td>Simpler code path</td></tr>
<tr><td>2 parallel index lookups</td><td>1 sequential BM25 lookup</td><td>Simpler debugging, deterministic</td></tr>
</tbody>
</table>
</section>
<hr>
<!-- ====================================================== -->
<section id="architecture">
<h2>3. Complete Vectorless RAG Architecture</h2>
<h3 id="folder">Folder Structure</h3>
<div class="tree">
<span class="dir">vectorless_rag/</span>
├── <span class="dir">config/</span>
│ └── <span class="file">settings.py</span> <span class="desc"># All tuneable constants in one place</span>
│
├── <span class="dir">ingestion/</span> <span class="desc"># Document loading & preprocessing</span>
│ ├── <span class="file">loader.py</span> <span class="desc"># PDF extraction (pdfplumber + OCR fallback)</span>
│ ├── <span class="file">ocr.py</span> <span class="desc"># pytesseract wrapper for scanned PDFs</span>
│ ├── <span class="file">chunker.py</span> <span class="desc"># Recursive text splitter + table routing</span>
│ └── <span class="file">preprocessor.py</span> <span class="desc"># Text cleaning, normalization, dedup</span>
│
├── <span class="dir">retrieval/</span> <span class="desc"># The entire retrieval pipeline</span>
│ ├── <span class="file">indexer.py</span> <span class="desc"># Build & persist BM25 index + JSON store</span>
│ ├── <span class="file">search.py</span> <span class="desc"># Query expansion, BM25 search, metadata filter</span>
│ ├── <span class="file">reranker.py</span> <span class="desc"># Cross-encoder reranking (ms-marco)</span>
│ └── <span class="file">cache.py</span> <span class="desc"># In-memory + optional Redis query cache</span>
│
├── <span class="dir">generation/</span> <span class="desc"># LLM answer generation</span>
│ ├── <span class="file">llm.py</span> <span class="desc"># Prompt templates + LLM calls (Ollama/OpenAI)</span>
│ └── <span class="file">citations.py</span> <span class="desc"># Citation extraction from passages</span>
│
├── <span class="dir">data/</span> <span class="desc"># Drop your PDF files here</span>
├── <span class="dir">index_store/</span> <span class="desc"># Persisted BM25 index + corpus JSON</span>
│ ├── <span class="file">bm25_index.pkl</span> <span class="desc"># Serialized BM25Okapi object</span>
│ └── <span class="file">corpus.json</span> <span class="desc"># All chunks with text + metadata</span>
│
├── <span class="dir">logs/</span>
│ └── <span class="file">rag_audit.log</span>
│
├── <span class="file">main.py</span> <span class="desc"># FastAPI app</span>
├── <span class="file">chat.py</span> <span class="desc"># CLI chatbot</span>
├── <span class="file">requirements.txt</span>
└── <span class="file">README.md</span>
</div>
<h3 id="pipelines">Pipeline Designs</h3>
<h4>Ingestion Pipeline</h4>
<div class="pipeline">
<div class="pipe-step">
<div class="pipe-num">1</div>
<div class="pipe-body">
<div class="pipe-title">PDF Detection & Routing</div>
<div class="pipe-desc">pdfplumber reads each file. If less than 100 chars of native text, route to OCR (pytesseract with eng+guj). Table pages are extracted separately with pdfplumber's table extractor.</div>
</div>
</div>
<div class="pipe-step">
<div class="pipe-num">2</div>
<div class="pipe-body">
<div class="pipe-title">Text Preprocessing</div>
<div class="pipe-desc">Unicode normalization (NFKC), whitespace compression, header/footer removal heuristics, ligature expansion. Detect and flag Gujarati script chunks for language metadata.</div>
</div>
</div>
<div class="pipe-step">
<div class="pipe-num">3</div>
<div class="pipe-body">
<div class="pipe-title">Chunking</div>
<div class="pipe-desc">RecursiveCharacterTextSplitter (chunk_size=600, overlap=100). Tables are kept as single chunks regardless of size. Each chunk tagged with: source, chunk_id, page_range, type (text/table), language.</div>
</div>
</div>
<div class="pipe-step">
<div class="pipe-num">4</div>
<div class="pipe-body">
<div class="pipe-title">BM25 Indexing</div>
<div class="pipe-desc">Tokenize all chunks using the domain-aware tokenizer (preserves emails, codes, numbers). Build BM25Okapi. Serialize with pickle. Write full corpus as corpus.json with all metadata.</div>
</div>
</div>
</div>
<h4>Retrieval Pipeline</h4>
<div class="pipeline">
<div class="pipe-step">
<div class="pipe-num">1</div>
<div class="pipe-body">
<div class="pipe-title">Cache Lookup</div>
<div class="pipe-desc">Check in-memory LRU cache keyed on normalized query string. Cache hit returns immediately — ~0ms. TTL = 1 hour by default.</div>
</div>
</div>
<div class="pipe-step">
<div class="pipe-num">2</div>
<div class="pipe-body">
<div class="pipe-title">Query Normalization & Expansion</div>
<div class="pipe-desc">Spell correction from domain map. Synonym expansion (Gujarat → Guj, TPDS → targeted public distribution system). File routing detection. Language detection.</div>
</div>
</div>
<div class="pipe-step">
<div class="pipe-num">3</div>
<div class="pipe-body">
<div class="pipe-title">BM25 Retrieval</div>
<div class="pipe-desc">BM25Okapi.get_scores() on expanded query tokens. Returns top-K candidates (K=50 for complex queries, K=30 for simple). <5ms for 100k chunks in RAM.</div>
</div>
</div>
<div class="pipe-step">
<div class="pipe-num">4</div>
<div class="pipe-body">
<div class="pipe-title">Metadata Filtering</div>
<div class="pipe-desc">Apply file filter if query targets specific document. Filter non-English chunks if query is English (with score threshold). Apply type filter for explicit table requests.</div>
</div>
</div>
<div class="pipe-step">
<div class="pipe-num">5</div>
<div class="pipe-body">
<div class="pipe-title">Cross-Encoder Reranking</div>
<div class="pipe-desc">ms-marco-MiniLM-L-6-v2 cross-encoder scores each (query, passage) pair. Top-50 BM25 → top-5 reranked. Pure CPU inference ~50–150ms depending on passage length and candidate count.</div>
</div>
</div>
<div class="pipe-step">
<div class="pipe-num">6</div>
<div class="pipe-body">
<div class="pipe-title">Threshold Filtering</div>
<div class="pipe-desc">Drop reranked passages below cross-encoder score threshold (e.g. >0.1). Prevents hallucination on out-of-corpus queries. Return NO_RELEVANT_CONTEXT if nothing passes.</div>
</div>
</div>
</div>
</section>
<hr>
<!-- ====================================================== -->
<section id="packages">
<h2>4. Packages, Libraries & Models</h2>
<h3>Core Python Packages</h3>
<div class="pkg-grid">
<div class="pkg-item">
<div class="pkg-name">rank_bm25</div>
<div class="pkg-desc">BM25Okapi implementation. Pure Python, fast enough for 500k chunks in RAM.</div>
<div class="pkg-badge"><span class="badge badge-green">Core</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">sentence-transformers</div>
<div class="pkg-desc">Cross-encoder reranking via CrossEncoder class. Also hosts ms-marco models.</div>
<div class="pkg-badge"><span class="badge badge-green">Core</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">pdfplumber</div>
<div class="pkg-desc">PDF text and table extraction. Handles hyperlinks and inline URLs correctly.</div>
<div class="pkg-badge"><span class="badge badge-green">Core</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">pypdf</div>
<div class="pkg-desc">Fallback PDF reader when pdfplumber returns empty.</div>
<div class="pkg-badge"><span class="badge badge-amber">Fallback</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">pdf2image + pytesseract</div>
<div class="pkg-desc">OCR pipeline for scanned PDFs. Supports eng+guj (Gujarati).</div>
<div class="pkg-badge"><span class="badge badge-amber">OCR</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">langchain-text-splitters</div>
<div class="pkg-desc">RecursiveCharacterTextSplitter — well-tested chunking.</div>
<div class="pkg-badge"><span class="badge badge-green">Core</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">fastapi + uvicorn</div>
<div class="pkg-desc">Async API backend with auto OpenAPI docs.</div>
<div class="pkg-badge"><span class="badge badge-green">Core</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">cachetools</div>
<div class="pkg-desc">TTLCache and LRUCache — in-memory query caching.</div>
<div class="pkg-badge"><span class="badge badge-blue">Cache</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">ollama (python SDK)</div>
<div class="pkg-desc">Local LLM calls via Ollama. Falls back to OpenAI if configured.</div>
<div class="pkg-badge"><span class="badge badge-purple">LLM</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">redis (optional)</div>
<div class="pkg-desc">Distributed cache for multi-instance deployments.</div>
<div class="pkg-badge"><span class="badge badge-amber">Optional</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">elasticsearch (optional)</div>
<div class="pkg-desc">Production-grade BM25 at scale (>1M chunks). Replace rank_bm25 with ES.</div>
<div class="pkg-badge"><span class="badge badge-amber">Scale</span></div>
</div>
<div class="pkg-item">
<div class="pkg-name">unicodedata, re</div>
<div class="pkg-desc">Stdlib. Unicode normalization and regex tokenization.</div>
<div class="pkg-badge"><span class="badge badge-teal">Stdlib</span></div>
</div>
</div>
<pre><code><span class="cm"># requirements.txt</span>
rank-bm25==0.2.2
sentence-transformers==2.7.0
pdfplumber==0.11.0
pypdf==4.3.1
pdf2image==1.17.0
pytesseract==0.3.10
langchain-text-splitters==0.3.0
fastapi==0.111.0
uvicorn[standard]==0.30.1
cachetools==5.3.3
ollama==0.2.1
pydantic==2.7.0
<span class="cm"># Optional scale / cache</span>
redis==5.0.4
elasticsearch==8.13.0</code></pre>
<h3 id="models">Models</h3>
<h4>Cross-Encoder Reranker Models (download locally)</h4>
<table>
<thead><tr><th>Model</th><th>Size</th><th>Latency/query</th><th>Quality</th><th>Recommendation</th></tr></thead>
<tbody>
<tr><td><code>cross-encoder/ms-marco-MiniLM-L-6-v2</code></td><td>22MB</td><td>~50ms (CPU)</td><td>Good</td><td><span class="badge badge-green">Default choice</span></td></tr>
<tr><td><code>cross-encoder/ms-marco-MiniLM-L-12-v2</code></td><td>34MB</td><td>~90ms (CPU)</td><td>Better</td><td>If you have extra latency budget</td></tr>
<tr><td><code>cross-encoder/ms-marco-electra-base</code></td><td>420MB</td><td>~250ms (CPU)</td><td>Best</td><td>GPU-only production with SLA >500ms</td></tr>
<tr><td><code>BAAI/bge-reranker-base</code></td><td>280MB</td><td>~180ms (CPU)</td><td>Excellent</td><td>Best quality/latency on GPU</td></tr>
</tbody>
</table>
<h4>LLM Models (via Ollama — local)</h4>
<table>
<thead><tr><th>Model</th><th>RAM</th><th>Quality</th><th>Use case</th></tr></thead>
<tbody>
<tr><td><code>llama3.1:8b</code></td><td>~5GB</td><td>Good</td><td>Development, low-resource</td></tr>
<tr><td><code>qwen2.5:14b</code></td><td>~9GB</td><td>Very good</td><td>Balanced production choice</td></tr>
<tr><td><code>mistral:7b</code></td><td>~5GB</td><td>Good</td><td>Fast inference, good at citations</td></tr>
<tr><td><code>llama3.1:70b</code></td><td>~40GB</td><td>Excellent</td><td>High-stakes enterprise answers</td></tr>
</tbody>
</table>
<h4>Hardware Considerations</h4>
<table>
<thead><tr><th>Setup</th><th>BM25 (50k chunks)</th><th>Reranker</th><th>LLM (7B)</th><th>Total P50 latency</th></tr></thead>
<tbody>
<tr><td>CPU-only (8 cores)</td><td>~3ms</td><td>~120ms</td><td>~4s (Ollama)</td><td>~4.5s</td></tr>
<tr><td>CPU + GPU (RTX 3080)</td><td>~3ms</td><td>~15ms</td><td>~0.8s</td><td>~1s</td></tr>
<tr><td>Claude/OpenAI API</td><td>~3ms</td><td>~15ms</td><td>~500ms (API)</td><td>~550ms</td></tr>
</tbody>
</table>
</section>
<hr>
<!-- ====================================================== -->
<section id="workflow">
<h2>5. Complete Coding Workflow</h2>
<h3>Project Setup</h3>
<pre><code><span class="cm"># 1. Create project directory</span>
mkdir vectorless_rag && cd vectorless_rag
<span class="cm"># 2. Create virtual environment</span>
python3 -m venv .venv
source .venv/bin/activate <span class="cm"># Windows: .venv\Scripts\activate</span>
<span class="cm"># 3. Create folder structure</span>
mkdir -p config ingestion retrieval generation data index_store logs
touch config/__init__.py config/settings.py
touch ingestion/__init__.py ingestion/loader.py ingestion/chunker.py
touch ingestion/ocr.py ingestion/preprocessor.py
touch retrieval/__init__.py retrieval/indexer.py retrieval/search.py
touch retrieval/reranker.py retrieval/cache.py
touch generation/__init__.py generation/llm.py generation/citations.py
touch main.py chat.py requirements.txt
<span class="cm"># 4. Install dependencies</span>
pip install -r requirements.txt
<span class="cm"># 5. Install Ollama and pull a model</span>
<span class="cm"># Visit https://ollama.com to install Ollama, then:</span>
ollama pull llama3.1:8b
<span class="cm"># 6. Drop your PDFs into data/</span>
cp /your/pdfs/*.pdf data/
<span class="cm"># 7. Run ingestion (builds BM25 index)</span>
python -m retrieval.indexer
<span class="cm"># 8. Start the API</span>
uvicorn main:app --reload --port 8000
<span class="cm"># 9. Test with curl</span>
curl -X POST http://localhost:8000/query \
-H "Content-Type: application/json" \
-d '{"question": "What is the TPDS policy?"}'</code></pre>
</section>
<hr>
<!-- ====================================================== -->
<section id="code-config">
<h2>6. Complete Codebase</h2>
<h3>config/settings.py</h3>
<pre><code><span class="cm">"""
Central configuration. Change these values; everything imports from here.
Do NOT scatter magic numbers across the codebase.
"""</span>
<span class="kw">import</span> os
<span class="cm"># ── Paths ────────────────────────────────────────────────────</span>
DATA_FOLDER = <span class="st">"data"</span>
INDEX_STORE = <span class="st">"index_store"</span>
BM25_INDEX_PATH = os.path.join(INDEX_STORE, <span class="st">"bm25_index.pkl"</span>)
CORPUS_PATH = os.path.join(INDEX_STORE, <span class="st">"corpus.json"</span>)
LOG_PATH = <span class="st">"logs/rag_audit.log"</span>
<span class="cm"># ── Chunking ─────────────────────────────────────────────────</span>
CHUNK_SIZE = <span class="nu">600</span> <span class="cm"># characters (smaller → more precise retrieval)</span>
CHUNK_OVERLAP = <span class="nu">80</span> <span class="cm"># overlap for context continuity</span>
<span class="cm"># ── Retrieval ────────────────────────────────────────────────</span>
BM25_TOP_K = <span class="nu">50</span> <span class="cm"># candidates to retrieve from BM25 before reranking</span>
RERANK_TOP_N = <span class="nu">5</span> <span class="cm"># final passages after reranking</span>
RERANK_THRESHOLD = <span class="nu">0.05</span> <span class="cm"># cross-encoder score floor (drop noise)</span>
SIMILARITY_THRESHOLD = <span class="nu">0.5</span> <span class="cm"># BM25 score floor (fraction of max score)</span>
<span class="cm"># ── Models ───────────────────────────────────────────────────</span>
RERANKER_MODEL = <span class="st">"cross-encoder/ms-marco-MiniLM-L-6-v2"</span>
RERANKER_DEVICE = <span class="st">"cpu"</span> <span class="cm"># "cuda" if GPU available</span>
RERANKER_CACHE = <span class="st">"./models"</span>
LLM_PROVIDER = <span class="st">"ollama"</span> <span class="cm"># "ollama" | "openai" | "anthropic"</span>
OLLAMA_MODEL = <span class="st">"llama3.1:8b"</span>
OLLAMA_BASE_URL = <span class="st">"http://localhost:11434"</span>
OPENAI_MODEL = <span class="st">"gpt-4o-mini"</span>
<span class="cm"># ── Cache ────────────────────────────────────────────────────</span>
CACHE_MAX_SIZE = <span class="nu">256</span> <span class="cm"># max cached queries</span>
CACHE_TTL_SEC = <span class="nu">3600</span> <span class="cm"># 1 hour</span>
<span class="cm"># ── Domain Spelling Map ───────────────────────────────────────</span>
SPELLING_MAP = {
<span class="st">"gujrat"</span>: <span class="st">"gujarat"</span>, <span class="st">"gujrati"</span>: <span class="st">"gujarati"</span>,
<span class="st">"ahemadabad"</span>: <span class="st">"ahmedabad"</span>, <span class="st">"baroda"</span>: <span class="st">"vadodara"</span>,
<span class="st">"tpsd"</span>: <span class="st">"tpds"</span>, <span class="st">"nfas"</span>: <span class="st">"nfsa"</span>,
<span class="st">"aadhar"</span>: <span class="st">"aadhaar"</span>, <span class="st">"adhaar"</span>: <span class="st">"aadhaar"</span>,
<span class="st">"beneficery"</span>: <span class="st">"beneficiary"</span>,
}</code></pre>
</section>
<section id="code-loader">
<h3>ingestion/loader.py</h3>
<pre><code><span class="kw">import</span> os
<span class="kw">from</span> pypdf <span class="kw">import</span> PdfReader