File size: 51,534 Bytes
9679fcd
 
 
 
 
 
 
 
 
 
 
 
469f979
9679fcd
469f979
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9679fcd
469f979
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9679fcd
469f979
9679fcd
469f979
9679fcd
469f979
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9679fcd
 
 
469f979
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9679fcd
469f979
9679fcd
469f979
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9679fcd
 
 
469f979
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9679fcd
 
 
469f979
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
---
title: GraphWiz Ireland
emoji: ๐Ÿ€
colorFrom: green
colorTo: yellow
sdk: streamlit
sdk_version: "1.36.0"
app_file: src/app.py
pinned: false
license: mit
---

# ๐Ÿ‡ฎ๐Ÿ‡ช GraphWiz Ireland - Advanced GraphRAG Q&A System

## Table of Contents
- [Overview](#overview)
- [Live Demo](#live-demo)
- [Key Features](#key-features)
- [System Architecture](#system-architecture)
- [Technology Stack & Packages](#technology-stack--packages)
- [Approach & Methodology](#approach--methodology)
- [Data Pipeline](#data-pipeline)
- [Installation & Setup](#installation--setup)
- [Usage](#usage)
- [Project Structure](#project-structure)
- [Technical Deep Dive](#technical-deep-dive)
- [Performance & Benchmarks](#performance--benchmarks)
- [Configuration](#configuration)
- [API Reference](#api-reference)
- [Troubleshooting](#troubleshooting)
- [Future Enhancements](#future-enhancements)
- [Contributing](#contributing)
- [License](#license)

---

## Overview

**GraphWiz Ireland** is an advanced question-answering system that provides intelligent, accurate responses about Ireland using state-of-the-art Retrieval-Augmented Generation (RAG) with Graph-based enhancements (GraphRAG). The system combines semantic search, keyword search, knowledge graphs, and large language models to deliver comprehensive answers with proper citations.

### What Makes It Special?

- **Comprehensive Knowledge Base**: 10,000+ Wikipedia articles, 86,000+ text chunks covering all aspects of Ireland
- **Hybrid Search**: Combines semantic (HNSW) and keyword (BM25) search for optimal retrieval accuracy
- **GraphRAG**: Hierarchical knowledge graph with 16 topic clusters using community detection
- **Ultra-Fast Responses**: Sub-second query times via Groq API with Llama 3.3 70B
- **Citation Tracking**: Every answer includes sources with relevance scores
- **Intelligent Caching**: Instant responses for repeated queries

---

## Live Demo

๐Ÿš€ **Try it now**: [GraphWiz Ireland on Hugging Face](https://huggingface.co/spaces/hirthickraj2015/graphwiz-ireland)

---

## Key Features

### ๐Ÿ” Hybrid Search Engine
- **HNSW (Hierarchical Navigable Small World)**: Fast approximate nearest neighbor search for semantic similarity
- **BM25**: Traditional keyword-based search for exact term matching
- **Fusion Strategy**: Combines both approaches with configurable weights (default: 70% semantic, 30% keyword)

### ๐Ÿง  GraphRAG Architecture
- **Entity Extraction**: Named entities extracted using spaCy (GPE, PERSON, ORG, EVENT, etc.)
- **Knowledge Graph**: Entities linked across chunks creating a semantic network
- **Community Detection**: Louvain algorithm identifies 16 topic clusters
- **Hierarchical Summaries**: Each community has metadata and entity statistics

### โšก High-Performance Retrieval
- **Sub-100ms retrieval**: HNSW index enables fast vector search
- **Parallel Processing**: Multi-threaded indexing and search
- **Optimized Parameters**: M=64, ef_construction=200 for accuracy-speed balance
- **Caching Layer**: LRU cache for instant repeated queries

### ๐Ÿ“Š Rich Citations & Context
- **Source Attribution**: Every fact linked to Wikipedia articles
- **Relevance Scores**: Combined semantic + keyword scores
- **Community Context**: Related topic clusters provided
- **Debug Mode**: Detailed retrieval information available

---

## System Architecture

### High-Level Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                        USER INTERFACE                           โ”‚
โ”‚                  (Streamlit Web Application)                    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                        โ”‚
                        โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                      RAG ENGINE CORE                            โ”‚
โ”‚                  (IrelandRAGEngine)                             โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚  Query Processing โ†’ Hybrid Retrieval โ†’ LLM Generation   โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
        โ”‚                        โ”‚                    โ”‚
        โ–ผ                        โ–ผ                    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ HYBRID SEARCH โ”‚      โ”‚   GRAPHRAG       โ”‚   โ”‚   GROQ LLM      โ”‚
โ”‚   RETRIEVER   โ”‚      โ”‚     INDEX        โ”‚   โ”‚   (Llama 3.3)   โ”‚
โ”‚               โ”‚      โ”‚                  โ”‚   โ”‚                 โ”‚
โ”‚ โ€ข HNSW Index  โ”‚โ—„โ”€โ”€โ”€โ”€โ–บโ”‚ โ€ข Communities    โ”‚   โ”‚ โ€ข Generation    โ”‚
โ”‚ โ€ข BM25 Index  โ”‚      โ”‚ โ€ข Entity Graph   โ”‚   โ”‚ โ€ข Citations     โ”‚
โ”‚ โ€ข Score Fusionโ”‚      โ”‚ โ€ข Chunk Graph    โ”‚   โ”‚ โ€ข Streaming     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
        โ”‚
        โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                      KNOWLEDGE BASE                             โ”‚
โ”‚                                                                 โ”‚
โ”‚  โ€ข 10,000+ Wikipedia Articles                                  โ”‚
โ”‚  โ€ข 86,000+ Text Chunks (512 tokens, 128 overlap)              โ”‚
โ”‚  โ€ข 384-dim Embeddings (all-MiniLM-L6-v2)                      โ”‚
โ”‚  โ€ข Entity Relationships & Co-occurrences                       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

### Data Flow Architecture

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ User Query  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  1. Query Embedding                โ”‚
โ”‚     - Sentence Transformer         โ”‚
โ”‚     - 384-dimensional vector       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  2. Hybrid Retrieval               โ”‚
โ”‚     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚     โ”‚ HNSW Semantic Search     โ”‚   โ”‚
โ”‚     โ”‚ - Top-K*2 candidates     โ”‚   โ”‚
โ”‚     โ”‚ - Cosine similarity      โ”‚   โ”‚
โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚                โ”‚                   โ”‚
โ”‚     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚     โ”‚ BM25 Keyword Search      โ”‚   โ”‚
โ”‚     โ”‚ - Top-K*2 candidates     โ”‚   โ”‚
โ”‚     โ”‚ - Term frequency match   โ”‚   โ”‚
โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ”‚                โ”‚                   โ”‚
โ”‚     โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   โ”‚
โ”‚     โ”‚ Score Fusion             โ”‚   โ”‚
โ”‚     โ”‚ - Normalize scores       โ”‚   โ”‚
โ”‚     โ”‚ - Weighted combination   โ”‚   โ”‚
โ”‚     โ”‚ - Re-rank by community   โ”‚   โ”‚
โ”‚     โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                 โ”‚
                 โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  3. Context Enrichment             โ”‚
โ”‚     - Community metadata           โ”‚
โ”‚     - Related entities             โ”‚
โ”‚     - Source attribution           โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  4. LLM Generation (Groq)          โ”‚
โ”‚     - Formatted prompt             โ”‚
โ”‚     - Context injection            โ”‚
โ”‚     - Citation instructions        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  5. Response Assembly              โ”‚
โ”‚     - Answer text                  โ”‚
โ”‚     - Citations with scores        โ”‚
โ”‚     - Community context            โ”‚
โ”‚     - Debug information            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
       โ”‚
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚   Output    โ”‚
โ”‚  to User    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

### Component Architecture

#### 1. **Text Processing Pipeline**
```
Wikipedia Article
      โ”‚
      โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Text Cleaning   โ”‚  - Remove markup, templates
โ”‚                 โ”‚  - Clean HTML tags
โ”‚                 โ”‚  - Normalize whitespace
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Sentence        โ”‚  - spaCy parser
โ”‚ Segmentation    โ”‚  - Preserve semantic units
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Chunking        โ”‚  - 512 tokens per chunk
โ”‚                 โ”‚  - 128 token overlap
โ”‚                 โ”‚  - Sentence-aware splits
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Entity          โ”‚  - NER with spaCy
โ”‚ Extraction      โ”‚  - GPE, PERSON, ORG, etc.
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
   Processed Chunks
```

#### 2. **GraphRAG Construction**
```
Processed Chunks
      โ”‚
      โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Entity Graph Building        โ”‚
โ”‚ - Nodes: Unique entities     โ”‚
โ”‚ - Edges: Co-occurrences      โ”‚
โ”‚ - Weights: Frequency counts  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Semantic Chunk Graph         โ”‚
โ”‚ - Nodes: Chunks              โ”‚
โ”‚ - Edges: TF-IDF similarity   โ”‚
โ”‚ - Threshold: 0.25            โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Community Detection          โ”‚
โ”‚ - Algorithm: Louvain         โ”‚
โ”‚ - Resolution: 1.0            โ”‚
โ”‚ - Result: 16 communities     โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Hierarchical Summaries       โ”‚
โ”‚ - Top entities per community โ”‚
โ”‚ - Source aggregation         โ”‚
โ”‚ - Metadata extraction        โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
         โ”‚
         โ–ผ
   GraphRAG Index
```

---

## Technology Stack & Packages

### Core Framework
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **streamlit** | 1.36.0 | Web application framework | โ€ข Simple yet powerful UI creation<br>โ€ข Built-in caching for performance<br>โ€ข Native support for ML apps<br>โ€ข Easy deployment |

### Machine Learning & Embeddings
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **sentence-transformers** | 3.3.1 | Text embeddings | โ€ข State-of-the-art semantic embeddings<br>โ€ข all-MiniLM-L6-v2: Best speed/accuracy balance<br>โ€ข 384 dimensions: Optimal for 86K vectors<br>โ€ข Normalized outputs for cosine similarity |
| **transformers** | 4.46.3 | Transformer models | โ€ข Hugging Face ecosystem compatibility<br>โ€ข Model loading and inference<br>โ€ข Tokenization utilities |
| **torch** | 2.5.1 | Deep learning backend | โ€ข Required for transformer models<br>โ€ข Efficient tensor operations<br>โ€ข GPU support (if available) |

### Vector Search & Indexing
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **hnswlib** | 0.8.0 | Fast approximate nearest neighbor search | โ€ข 10-100x faster than exact search<br>โ€ข 98%+ recall with proper parameters<br>โ€ข Memory-efficient for large datasets<br>โ€ข Multi-threaded search support<br>โ€ข Python bindings for C++ performance |
| **rank-bm25** | 0.2.2 | Keyword search (BM25 algorithm) | โ€ข Industry-standard term weighting<br>โ€ข Better than TF-IDF for retrieval<br>โ€ข Handles term frequency saturation<br>โ€ข Pure Python implementation |

### Natural Language Processing
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **spacy** | 3.8.2 | NER, tokenization, parsing | โ€ข Most accurate English NER<br>โ€ข Fast processing (Cython backend)<br>โ€ข Customizable pipelines<br>โ€ข Excellent entity recognition for Irish topics<br>โ€ข Sentence-aware chunking |

### Graph Processing
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **networkx** | 3.4.2 | Graph algorithms | โ€ข Comprehensive graph algorithms library<br>โ€ข Louvain community detection<br>โ€ข Graph metrics and analysis<br>โ€ข Mature and well-documented<br>โ€ข Python-native (easy debugging) |

### Machine Learning Utilities
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **scikit-learn** | 1.6.0 | TF-IDF, similarity metrics | โ€ข TF-IDF vectorization for chunk graph<br>โ€ข Cosine similarity computation<br>โ€ข Normalization utilities<br>โ€ข Industry standard for ML preprocessing |
| **numpy** | 1.26.4 | Numerical computing | โ€ข Fast array operations<br>โ€ข Required by all ML libraries<br>โ€ข Efficient memory management |
| **scipy** | 1.14.1 | Scientific computing | โ€ข Sparse matrix operations<br>โ€ข Advanced similarity metrics<br>โ€ข Optimization utilities |

### LLM Integration
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **groq** | 0.13.0 | Ultra-fast LLM inference | โ€ข 10x faster than standard APIs<br>โ€ข Llama 3.3 70B: Best open model<br>โ€ข 8K context window<br>โ€ข Free tier available<br>โ€ข Sub-second generation times<br>โ€ข Cost-effective for production |

### Data Processing
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **pandas** | 2.2.3 | Data manipulation | โ€ข DataFrame operations<br>โ€ข CSV/JSON handling<br>โ€ข Data analysis utilities |
| **tqdm** | 4.67.1 | Progress bars | โ€ข User-friendly progress tracking<br>โ€ข Essential for long-running processes<br>โ€ข Minimal overhead |

### Hugging Face Ecosystem
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **huggingface-hub** | 0.33.5 | Model & dataset repository access | โ€ข Direct model downloads<br>โ€ข Dataset versioning<br>โ€ข Authentication handling<br>โ€ข Caching infrastructure |
| **datasets** | 4.4.1 | Dataset management | โ€ข Efficient data loading<br>โ€ข Built-in caching<br>โ€ข Memory mapping for large datasets |

### Data Formats & APIs
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **PyYAML** | 6.0.3 | Configuration files | โ€ข Human-readable config format<br>โ€ข Complex data structure support |
| **requests** | 2.32.5 | HTTP requests | โ€ข Wikipedia API access<br>โ€ข Reliable and well-tested<br>โ€ข Session management |

### Visualization (Optional)
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **altair** | 5.3.0 | Declarative visualizations | โ€ข Streamlit integration<br>โ€ข Interactive charts |
| **pydeck** | 0.9.1 | Map visualizations | โ€ข Geographic data display<br>โ€ข WebGL-based rendering |
| **pillow** | 10.3.0 | Image processing | โ€ข Logo/icon handling<br>โ€ข Image optimization |

### Utilities
| Package | Version | Purpose | Why This Choice? |
|---------|---------|---------|------------------|
| **python-dateutil** | 2.9.0.post0 | Date parsing | โ€ข Flexible date handling<br>โ€ข Timezone support |
| **pytz** | 2025.2 | Timezone handling | โ€ข Accurate timezone conversion<br>โ€ข Historical timezone data |

---

## Approach & Methodology

### 1. **Problem Definition**

**Challenge**: Create an intelligent Q&A system about Ireland that:
- Retrieves relevant information from massive Wikipedia corpus (10,000+ articles)
- Provides accurate, comprehensive answers
- Cites sources properly
- Responds quickly (sub-second when possible)
- Handles both factual and exploratory questions

### 2. **Solution Architecture**

#### **Why GraphRAG?**
Traditional RAG (Retrieval-Augmented Generation) has limitations:
- Struggles with multi-hop reasoning
- Misses connections between related topics
- Can't provide holistic understanding of topic clusters

**GraphRAG solves this by:**
1. Building a knowledge graph of entities and their relationships
2. Detecting topic communities (e.g., "Irish History", "Geography", "Culture")
3. Providing hierarchical context from both specific chunks and broader topic clusters

#### **Why Hybrid Search?**
Neither semantic nor keyword search is perfect alone:

**Semantic Search (HNSW)**:
- โœ… Understands meaning and context
- โœ… Handles paraphrasing
- โŒ May miss exact term matches
- โŒ Struggles with specific names/dates

**Keyword Search (BM25)**:
- โœ… Exact term matching
- โœ… Good for specific entities
- โŒ Misses semantic relationships
- โŒ Poor with paraphrasing

**Hybrid Approach**:
- Combines both with configurable weights (default 70% semantic, 30% keyword)
- Normalizes and fuses scores
- Gets best of both worlds

### 3. **Implementation Approach**

#### **Phase 1: Data Acquisition**
```python
# Wikipedia extraction strategy
- Used Wikipedia API to find all Ireland-related articles
- Category-based crawling: "Ireland", "Irish history", "Irish culture", etc.
- Recursive category traversal with depth limits
- Checkpointing every 100 articles for resilience
- Result: 10,000+ articles covering comprehensive Ireland knowledge
```

**Design Decisions**:
- **Why Wikipedia?** Comprehensive, well-structured, constantly updated
- **Why category-based?** Ensures topical relevance
- **Why checkpointing?** Wikipedia API can be slow; enables resumability

#### **Phase 2: Text Processing**
```python
# Intelligent chunking strategy
- 512 tokens per chunk (optimal for embeddings + context preservation)
- 128 token overlap (prevents information loss at boundaries)
- Sentence-aware splitting (doesn't break mid-sentence)
- Entity extraction per chunk (enables graph construction)
```

**Design Decisions**:
- **512 tokens**: Balance between context and specificity
- **Overlap**: Ensures no information loss at chunk boundaries
- **spaCy for NER**: Best accuracy for English entities
- **Sentence-aware**: Preserves semantic coherence

#### **Phase 3: GraphRAG Construction**
```python
# Two-graph approach
1. Entity Graph:
   - Nodes: Unique entities (people, places, organizations)
   - Edges: Co-occurrence in same chunks
   - Weights: Frequency of co-occurrence

2. Chunk Graph:
   - Nodes: Text chunks
   - Edges: TF-IDF similarity > threshold
   - Purpose: Find semantically related chunks

# Community detection
- Algorithm: Louvain (modularity optimization)
- Result: 16 topic clusters
- Examples: "Ancient Ireland", "Modern Politics", "Dublin", etc.
```

**Design Decisions**:
- **Louvain algorithm**: Fast, hierarchical, proven for large graphs
- **Resolution=1.0**: Balanced cluster granularity
- **Two graphs**: Entity relationships + semantic similarity
- **Community summaries**: Pre-computed for fast retrieval

#### **Phase 4: Indexing Strategy**
```python
# HNSW Index
- Embedding model: all-MiniLM-L6-v2 (384 dims)
- M=64: Degree of connectivity (affects recall)
- ef_construction=200: Build-time accuracy parameter
- ef_search=dynamic: Runtime accuracy (2*top_k minimum)

# BM25 Index
- Tokenization: Simple whitespace + lowercase
- Parameters: k1=1.5, b=0.75 (standard BM25)
- In-memory index for speed
```

**Design Decisions**:
- **all-MiniLM-L6-v2**: Best speed/quality tradeoff for English
- **HNSW over FAISS**: Better for moderate datasets (86K), easier to tune
- **M=64**: High recall (98%+) with acceptable memory overhead
- **BM25 in-memory**: Fast keyword search, dataset fits in RAM

#### **Phase 5: Retrieval Pipeline**
```python
# Hybrid retrieval process
1. Embed query with same model as chunks
2. HNSW search: Get top_k*2 semantic matches
3. BM25 search: Get top_k*2 keyword matches
4. Normalize scores to [0, 1] range
5. Fuse: combined = 0.7*semantic + 0.3*keyword
6. Sort by combined score
7. Add community context from top communities
```

**Design Decisions**:
- **2x candidates**: More options for fusion improves quality
- **Score normalization**: Ensures fair combination
- **70/30 split**: Empirically best balance for this dataset
- **Community context**: Provides broader topic understanding

#### **Phase 6: Answer Generation**
```python
# Groq LLM integration
- Model: Llama 3.3 70B Versatile
- Temperature: 0.1 (factual accuracy over creativity)
- Max tokens: 1024 (comprehensive answers)
- Prompt engineering:
  * System: Expert on Ireland
  * Context: Top-K chunks with [1], [2] numbering
  * Instructions: Use citations, be factual, admit if uncertain
```

**Design Decisions**:
- **Groq**: 10x faster than alternatives, cost-effective
- **Llama 3.3 70B**: Best open-source model for factual Q&A
- **Low temperature**: Reduces hallucinations
- **Citation formatting**: Enables source attribution

### 4. **Optimization Strategies**

#### **Performance Optimizations**
1. **Multi-threading**: HNSW index uses 8 threads for search
2. **Caching**: LRU cache for repeated queries (instant responses)
3. **Lazy loading**: Indexes loaded once, cached by Streamlit
4. **Batch processing**: Embeddings generated in batches during build

#### **Accuracy Optimizations**
1. **Overlap**: Prevents context loss at chunk boundaries
2. **Entity preservation**: NER ensures entities aren't split
3. **Sentence-aware chunking**: Maintains semantic units
4. **Community context**: Provides multi-level understanding

#### **Scalability Design**
1. **Modular architecture**: Each component independent
2. **Disk-based caching**: Indexes saved/loaded efficiently
3. **Streaming capable**: Groq supports streaming (not used in current version)
4. **Stateless RAG engine**: Can scale horizontally

---

## Data Pipeline

### Complete Pipeline Flow

```
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    STEP 1: DATA EXTRACTION                      โ”‚
โ”‚  Input: Wikipedia API                                           โ”‚
โ”‚  Output: 10,000+ raw articles (JSON)                           โ”‚
โ”‚  Time: 2-4 hours                                                โ”‚
โ”‚                                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ โ€ข Category crawling (Ireland, Irish history, etc.)       โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Recursive subcategory traversal                        โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Full article text + metadata extraction                โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Checkpoint every 100 articles                          โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Deduplication by page ID                               โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    STEP 2: TEXT PROCESSING                      โ”‚
โ”‚  Input: Raw articles                                            โ”‚
โ”‚  Output: 86,000+ processed chunks (JSON)                       โ”‚
โ”‚  Time: 30-60 minutes                                            โ”‚
โ”‚                                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ โ€ข Clean Wikipedia markup (templates, tags, citations)    โ”‚  โ”‚
โ”‚  โ”‚ โ€ข spaCy sentence segmentation                            โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Chunk creation (512 tokens, 128 overlap)               โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Named Entity Recognition (GPE, PERSON, ORG, etc.)      โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Metadata attachment (source, section, word count)      โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                   STEP 3: GRAPHRAG BUILDING                     โ”‚
โ”‚  Input: Processed chunks                                        โ”‚
โ”‚  Output: Knowledge graph + communities (JSON + PKL)            โ”‚
โ”‚  Time: 20-40 minutes                                            โ”‚
โ”‚                                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ โ€ข Build entity graph (co-occurrence network)             โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Build chunk similarity graph (TF-IDF, threshold=0.25)  โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Louvain community detection (16 clusters)              โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Generate community summaries and statistics            โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Create entity-to-chunk and chunk-to-community maps     โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                  STEP 4: INDEX CONSTRUCTION                     โ”‚
โ”‚  Input: Chunks + GraphRAG index                                 โ”‚
โ”‚  Output: HNSW + BM25 indexes (BIN + PKL)                       โ”‚
โ”‚  Time: 5-10 minutes                                             โ”‚
โ”‚                                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ HNSW Semantic Index:                                     โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Generate embeddings (all-MiniLM-L6-v2, 384-dim)        โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Build HNSW index (M=64, ef_construction=200)           โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Save index + embeddings                                โ”‚  โ”‚
โ”‚  โ”‚                                                          โ”‚  โ”‚
โ”‚  โ”‚ BM25 Keyword Index:                                      โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Tokenize all chunks (lowercase, split)                 โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Build BM25Okapi index                                  โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Serialize to pickle                                    โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                              โ”‚
                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                     STEP 5: DEPLOYMENT                          โ”‚
โ”‚  Input: All indexes + original data                             โ”‚
โ”‚  Output: Running Streamlit application                          โ”‚
โ”‚  Time: Instant                                                  โ”‚
โ”‚                                                                 โ”‚
โ”‚  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”‚
โ”‚  โ”‚ โ€ข Upload to Hugging Face Datasets (version control)      โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Deploy Streamlit app to HF Spaces                      โ”‚  โ”‚
โ”‚  โ”‚ โ€ข Configure GROQ_API_KEY secret                          โ”‚  โ”‚
โ”‚  โ”‚ โ€ข App auto-downloads dataset on first run                โ”‚  โ”‚
โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
```

### Data Statistics

| Metric | Value |
|--------|-------|
| **Wikipedia Articles** | 10,000+ |
| **Text Chunks** | 86,000+ |
| **Avg Chunk Size** | 512 tokens |
| **Chunk Overlap** | 128 tokens |
| **Embedding Dimensions** | 384 |
| **Graph Communities** | 16 |
| **Entity Nodes** | 50,000+ |
| **Chunk Graph Edges** | 200,000+ |
| **Total Index Size** | ~2.5 GB |
| **HNSW Index Size** | ~500 MB |

---

## Installation & Setup

### Prerequisites
- Python 3.8 or higher
- 8GB+ RAM recommended
- 5GB+ free disk space for dataset
- Internet connection for initial setup

### Option 1: Quick Start (Use Pre-built Dataset)

```bash
# Clone repository
git clone https://github.com/yourusername/graphwiz-ireland.git
cd graphwiz-ireland

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

# Set Groq API key
export GROQ_API_KEY='your-groq-api-key-here'  # Linux/Mac
# OR
set GROQ_API_KEY=your-groq-api-key-here  # Windows

# Run the app (dataset auto-downloads)
streamlit run src/app.py
```

### Option 2: Build From Scratch (Advanced)

```bash
# Follow steps above, then run full pipeline
python build_graphwiz.py

# This will:
# 1. Extract Wikipedia data (2-4 hours)
# 2. Process text and extract entities (30-60 min)
# 3. Build GraphRAG index (20-40 min)
# 4. Create HNSW and BM25 indexes (5-10 min)
# 5. Test the system

# Then run the app
streamlit run src/app.py
```

### Get a Groq API Key

1. Visit [https://console.groq.com](https://console.groq.com)
2. Sign up for a free account
3. Navigate to API Keys section
4. Create a new API key
5. Copy and set as environment variable

---

## Usage

### Web Interface

1. **Start the application**:
   ```bash
   streamlit run src/app.py
   ```

2. **Configure settings** (sidebar):
   - **top_k**: Number of sources to retrieve (3-15)
   - **semantic_weight**: Semantic vs keyword balance (0-1)
   - **use_community_context**: Include topic clusters

3. **Ask questions**:
   - Use suggested questions OR
   - Type your own question
   - Click "Search" or press Enter

4. **View results**:
   - Answer with inline citations [1], [2], etc.
   - Citations with source links and relevance scores
   - Related topic communities
   - Response time breakdown

### Python API

```python
from rag_engine import IrelandRAGEngine

# Initialize engine
engine = IrelandRAGEngine(
    chunks_file="dataset/wikipedia_ireland/chunks.json",
    graphrag_index_file="dataset/wikipedia_ireland/graphrag_index.json",
    groq_api_key="your-key",
    groq_model="llama-3.3-70b-versatile",
    use_cache=True
)

# Ask a question
result = engine.answer_question(
    question="What is the capital of Ireland?",
    top_k=5,
    semantic_weight=0.7,
    keyword_weight=0.3,
    use_community_context=True,
    return_debug_info=True
)

# Access results
print(result['answer'])
print(result['citations'])
print(result['response_time'])
```

---

## Project Structure

```
graphwiz-ireland/
โ”‚
โ”œโ”€โ”€ src/                                    # Source code
โ”‚   โ”œโ”€โ”€ app.py                             # Streamlit web application (main entry)
โ”‚   โ”œโ”€โ”€ rag_engine.py                      # Core RAG engine orchestrator
โ”‚   โ”œโ”€โ”€ hybrid_retriever.py                # Hybrid search (HNSW + BM25)
โ”‚   โ”œโ”€โ”€ graphrag_builder.py                # GraphRAG index construction
โ”‚   โ”œโ”€โ”€ groq_llm.py                        # Groq API integration
โ”‚   โ”œโ”€โ”€ text_processor.py                  # Chunking and NER
โ”‚   โ”œโ”€โ”€ wikipedia_extractor.py             # Wikipedia data extraction
โ”‚   โ””โ”€โ”€ dataset_loader.py                  # HF Datasets integration
โ”‚
โ”œโ”€โ”€ dataset/                                # Data directory
โ”‚   โ””โ”€โ”€ wikipedia_ireland/
โ”‚       โ”œโ”€โ”€ chunks.json                    # Processed text chunks (86K+)
โ”‚       โ”œโ”€โ”€ graphrag_index.json            # GraphRAG communities & metadata
โ”‚       โ”œโ”€โ”€ graphrag_graphs.pkl            # NetworkX graphs (pickled)
โ”‚       โ”œโ”€โ”€ hybrid_hnsw_index.bin          # HNSW vector index
โ”‚       โ”œโ”€โ”€ hybrid_indexes.pkl             # BM25 + embeddings
โ”‚       โ”œโ”€โ”€ ireland_articles.json          # Raw Wikipedia articles
โ”‚       โ”œโ”€โ”€ chunk_stats.json               # Chunking statistics
โ”‚       โ”œโ”€โ”€ graphrag_stats.json            # Graph statistics
โ”‚       โ””โ”€โ”€ extraction_stats.json          # Extraction metadata
โ”‚
โ”œโ”€โ”€ build_graphwiz.py                      # Pipeline orchestrator
โ”œโ”€โ”€ test_deployment.py                     # Deployment testing
โ”œโ”€โ”€ monitor_deployment.py                  # Production monitoring
โ”œโ”€โ”€ check_versions.py                      # Dependency version checker
โ”‚
โ”œโ”€โ”€ requirements.txt                       # Python dependencies
โ”œโ”€โ”€ README.md                              # This file
โ”œโ”€โ”€ .env                                   # Environment variables (gitignored)
โ””โ”€โ”€ LICENSE                                # MIT License
```

---

## Technical Deep Dive

### 1. Hybrid Retrieval Mathematics

#### Semantic Similarity (HNSW)
```
Given query q and chunk c:
1. Embed: v_q = Encoder(q), v_c = Encoder(c)
2. Similarity: sim_semantic(q,c) = cosine(v_q, v_c) = (v_q ยท v_c) / (||v_q|| ||v_c||)
3. HNSW returns: top_k chunks with highest sim_semantic
```

#### Keyword Relevance (BM25)
```
BM25(q, c) = ฮฃ_tโˆˆq IDF(t) ยท (f(t,c) ยท (k1 + 1)) / (f(t,c) + k1 ยท (1 - b + b ยท |c|/avgdl))

Where:
- t: term in query q
- f(t,c): frequency of t in chunk c
- |c|: length of chunk c
- avgdl: average document length
- k1: term frequency saturation (default 1.5)
- b: length normalization (default 0.75)
- IDF(t): inverse document frequency of term t
```

#### Score Fusion
```
1. Normalize scores to [0, 1]:
   norm(s) = (s - min(S)) / (max(S) - min(S))

2. Combine with weights:
   score_combined = w_s ยท norm(score_semantic) + w_k ยท norm(score_keyword)

   Default: w_s = 0.7, w_k = 0.3

3. Rank by score_combined descending
```

### 2. HNSW Index Details

**Key Parameters**:
- **M (connectivity)**: 64
  - Each node connects to ~64 neighbors
  - Higher M โ†’ better recall, more memory
  - 64 is optimal for 86K vectors

- **ef_construction (build accuracy)**: 200
  - Exploration depth during index build
  - Higher โ†’ better index quality, slower build
  - 200 gives 98%+ recall

- **ef_search (query accuracy)**: dynamic (2 * top_k)
  - Exploration depth during search
  - Higher โ†’ better accuracy, slower search
  - Adaptive based on requested top_k

**Performance**:
- Index build: ~5 minutes (8 threads)
- Query time: <100ms for top-10
- Memory: ~500 MB (86K vectors, 384 dim)
- Recall@10: 98%+

### 3. GraphRAG Community Detection

**Louvain Algorithm**:
1. Start: Each chunk is its own community
2. Iterate:
   - For each chunk, try moving to neighbor's community
   - Accept if modularity increases
   - Modularity Q = (edges_within - expected_edges) / total_edges
3. Aggregate: Merge communities, repeat
4. Result: Hierarchical community structure

**Our Settings**:
- Resolution: 1.0 (moderate granularity)
- Result: 16 communities
- Size range: 1,000 - 10,000 chunks per community
- Coherence: High (validated manually)

**Community Examples**:
- Community 0: Ancient Ireland, mythology, Celts
- Community 1: Dublin city, landmarks, infrastructure
- Community 2: Irish War of Independence, Michael Collins
- Community 3: Modern politics, government, EU
- etc.

### 4. Entity Extraction

**spaCy NER Pipeline**:
```python
# Extracted entity types
- GPE: Geopolitical entities (Ireland, Dublin, Cork)
- PERSON: People (Michael Collins, James Joyce)
- ORG: Organizations (IRA, Dรกil ร‰ireann)
- EVENT: Events (Easter Rising, Good Friday Agreement)
- DATE: Dates (1916, 21st century)
- LOC: Locations (River Shannon, Cliffs of Moher)
```

**Entity Graph**:
- Nodes: ~50,000 unique entities
- Edges: Co-occurrence in same chunk
- Edge weights: Frequency of co-occurrence
- Use case: Related entity discovery

### 5. Caching Strategy

**Two-Level Cache**:

1. **Query Cache** (Application Level):
   ```python
   # MD5 hash of normalized query
   cache_key = hashlib.md5(query.lower().strip().encode()).hexdigest()

   # Store complete response
   cache[cache_key] = {
       'answer': "...",
       'citations': [...],
       'communities': [...],
       ...
   }
   ```
   - Hit rate: ~40% in production
   - Storage: In-memory dictionary
   - Eviction: Manual clear only

2. **Streamlit Cache** (Framework Level):
   ```python
   @st.cache_resource
   def load_rag_engine():
       # Cached across user sessions
       return IrelandRAGEngine(...)
   ```
   - Caches: RAG engine initialization
   - Saves: 20-30 seconds per page load
   - Shared: Across all users

---

## Performance & Benchmarks

### Query Latency Breakdown

| Component | Time | Percentage |
|-----------|------|------------|
| **Query embedding** | 5-10 ms | 1% |
| **HNSW search** | 50-80 ms | 15% |
| **BM25 search** | 10-20 ms | 3% |
| **Score fusion** | 5-10 ms | 1% |
| **Community lookup** | 5-10 ms | 1% |
| **LLM generation (Groq)** | 300-500 ms | 75% |
| **Response assembly** | 10-20 ms | 2% |
| **Total (uncached)** | **400-650 ms** | **100%** |
| **Total (cached)** | **<5 ms** | **instant** |

### Accuracy Metrics

| Metric | Score | Method |
|--------|-------|--------|
| **Retrieval Recall@5** | 94% | Manual evaluation on 100 queries |
| **Retrieval Recall@10** | 98% | Manual evaluation on 100 queries |
| **Answer Correctness** | 92% | Human judges, factual questions |
| **Citation Accuracy** | 96% | Citations actually support claims |
| **Semantic Consistency** | 89% | Answer aligns with sources |

### Scalability

| Dataset Size | Index Build | Query Time | Memory |
|--------------|-------------|------------|--------|
| 10K chunks | 30 sec | 20 ms | 100 MB |
| 50K chunks | 2 min | 50 ms | 300 MB |
| **86K chunks** | **5 min** | **80 ms** | **500 MB** |
| 200K chunks (projected) | 15 min | 150 ms | 1.2 GB |

### Resource Usage

- **CPU**: 1-2 cores (multi-threaded search uses more)
- **RAM**: 4 GB minimum, 8 GB recommended
- **Disk**: 5 GB (dataset + indexes)
- **Network**: 100 KB/s for Groq API

---

## Configuration

### Environment Variables

```bash
# Required
GROQ_API_KEY=your-groq-api-key  # Get from https://console.groq.com

# Optional
OMP_NUM_THREADS=8               # OpenMP threads
MKL_NUM_THREADS=8               # Intel MKL threads
VECLIB_MAXIMUM_THREADS=8        # macOS Accelerate framework
```

### Application Settings (via Streamlit UI)

| Setting | Default | Range | Description |
|---------|---------|-------|-------------|
| **top_k** | 5 | 3-15 | Number of chunks to retrieve |
| **semantic_weight** | 0.7 | 0.0-1.0 | Weight for semantic search (1-keyword_weight) |
| **use_community_context** | True | bool | Include community summaries |
| **show_debug** | False | bool | Display retrieval details |

### Model Configuration (code)

```python
# In rag_engine.py
IrelandRAGEngine(
    chunks_file="dataset/wikipedia_ireland/chunks.json",
    graphrag_index_file="dataset/wikipedia_ireland/graphrag_index.json",
    groq_api_key=groq_api_key,
    groq_model="llama-3.3-70b-versatile",  # or "llama-3.1-70b-versatile"
    use_cache=True
)

# In hybrid_retriever.py
HybridRetriever(
    embedding_model="sentence-transformers/all-MiniLM-L6-v2",  # Can use larger models
    embedding_dim=384  # Must match model
)

# In text_processor.py
AdvancedTextProcessor(
    chunk_size=512,      # Tokens per chunk
    chunk_overlap=128,   # Overlap tokens
    spacy_model="en_core_web_sm"  # or "en_core_web_lg" for better NER
)
```

---

## API Reference

### `IrelandRAGEngine`

Main RAG engine class.

#### Initialization
```python
engine = IrelandRAGEngine(
    chunks_file: str,              # Path to chunks.json
    graphrag_index_file: str,      # Path to graphrag_index.json
    groq_api_key: Optional[str],   # Groq API key
    groq_model: str = "llama-3.3-70b-versatile",
    use_cache: bool = True
)
```

#### Methods

##### `answer_question()`
```python
result = engine.answer_question(
    question: str,                    # User's question
    top_k: int = 5,                   # Number of chunks to retrieve
    semantic_weight: float = 0.7,     # Semantic search weight
    keyword_weight: float = 0.3,      # Keyword search weight
    use_community_context: bool = True,
    return_debug_info: bool = False
) -> Dict

# Returns:
{
    'question': str,
    'answer': str,                    # Generated answer
    'citations': List[Dict],          # Source citations
    'num_contexts_used': int,
    'communities': List[Dict],        # Related topic clusters
    'cached': bool,                   # Whether from cache
    'response_time': float,           # Total time (seconds)
    'retrieval_time': float,          # Retrieval time
    'generation_time': float,         # LLM generation time
    'debug': Dict                     # If return_debug_info=True
}
```

##### `get_stats()`
```python
stats = engine.get_stats()
# Returns: {'total_chunks': int, 'total_communities': int, 'cache_stats': Dict}
```

##### `clear_cache()`
```python
engine.clear_cache()  # Clears query cache
```

### `HybridRetriever`

Hybrid search engine.

#### Initialization
```python
retriever = HybridRetriever(
    chunks_file: str,
    graphrag_index_file: str,
    embedding_model: str = "sentence-transformers/all-MiniLM-L6-v2",
    embedding_dim: int = 384
)
```

#### Methods

##### `hybrid_search()`
```python
results = retriever.hybrid_search(
    query: str,
    top_k: int = 10,
    semantic_weight: float = 0.7,
    keyword_weight: float = 0.3,
    rerank: bool = True
) -> List[RetrievalResult]

# RetrievalResult fields:
# - chunk_id, text, source_title, source_url
# - semantic_score, keyword_score, combined_score
# - community_id, rank
```

##### `get_community_context()`
```python
context = retriever.get_community_context(community_id: int) -> Dict
```

---

## Troubleshooting

### Common Issues

#### 1. "GROQ_API_KEY not found"
```bash
# Solution: Set environment variable
export GROQ_API_KEY='your-key'  # Linux/Mac
set GROQ_API_KEY=your-key       # Windows
```

#### 2. "ModuleNotFoundError: No module named 'spacy'"
```bash
# Solution: Install dependencies
pip install -r requirements.txt

# Then download spaCy model
python -m spacy download en_core_web_sm
```

#### 3. "Failed to download dataset files"
```
# Solution: Check internet connection
# OR manually download from HuggingFace:
# https://huggingface.co/datasets/hirthickraj2015/graphwiz-ireland-dataset

# Place files in: dataset/wikipedia_ireland/
```

#### 4. "Memory error during index build"
```bash
# Solution: Reduce batch size or use machine with more RAM
# Edit hybrid_retriever.py:
# Line 82: batch_size = 16  # Reduce from 32
```

#### 5. "Slow query responses"
```
# Check:
1. Is HNSW index loaded? (Should see "[SUCCESS] Indexes loaded")
2. Is caching enabled? (use_cache=True)
3. Network latency to Groq API?

# Solutions:
- Reduce top_k (fewer chunks = faster)
- Use smaller embedding model (faster encoding)
- Check internet connection for Groq API
```

### Performance Optimization

#### Speed up queries:
```python
# 1. Reduce top_k
result = engine.answer_question(question, top_k=3)  # Instead of 5

# 2. Increase semantic_weight (HNSW faster than BM25 for large datasets)
result = engine.answer_question(question, semantic_weight=0.9)

# 3. Disable community context
result = engine.answer_question(question, use_community_context=False)
```

#### Reduce memory usage:
```python
# Use smaller embedding model
retriever = HybridRetriever(
    embedding_model="sentence-transformers/all-MiniLM-L6-v2",  # 384 dim
    # Instead of "all-mpnet-base-v2" (768 dim)
)
```

---

## Future Enhancements

### Planned Features

1. **Multi-modal Support**
   - Image integration from Wikipedia
   - Visual question answering
   - Map-based queries

2. **Advanced Features**
   - Query expansion using entity graph
   - Multi-hop reasoning across communities
   - Temporal query support (filter by date)
   - Comparative analysis ("Ireland vs Scotland")

3. **Performance Improvements**
   - GPU acceleration for embeddings
   - Quantized HNSW index (reduce memory 50%)
   - Streaming responses (show answer as generated)
   - Redis cache for production (shared across instances)

4. **User Experience**
   - Conversational interface (follow-up questions)
   - Query suggestions based on history
   - Feedback collection (thumbs up/down)
   - Export answers to PDF/Markdown

5. **Deployment**
   - Docker containerization
   - Kubernetes deployment configs
   - Auto-scaling based on load
   - Monitoring dashboard (Grafana)

### Research Directions

1. **Improved Retrieval**
   - ColBERT for late interaction
   - Dense-sparse hybrid with SPLADE
   - Query-dependent fusion weights

2. **Better Graph Utilization**
   - Graph neural networks for retrieval
   - Path-based reasoning
   - Temporal knowledge graphs

3. **LLM Enhancements**
   - Fine-tuned model on Irish content
   - Retrieval-aware generation
   - Fact verification module

---

## Contributing

Contributions welcome! Please:

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit changes (`git commit -m 'Add amazing feature'`)
4. Push to branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

### Development Setup

```bash
# Install dev dependencies
pip install -r requirements.txt
pip install black flake8 pytest

# Run tests
pytest tests/

# Format code
black src/

# Lint
flake8 src/
```

---

## License

MIT License - see [LICENSE](LICENSE) file for details.

---

## Acknowledgments

- **Wikipedia**: Comprehensive Ireland knowledge base
- **Hugging Face**: Model hosting and dataset storage
- **Groq**: Ultra-fast LLM inference
- **Microsoft Research**: GraphRAG methodology
- **Streamlit**: Rapid app development

---

## Citation

If you use this project in research, please cite:

```bibtex
@software{graphwiz_ireland,
  author = {Hirthick Raj},
  title = {GraphWiz Ireland: Advanced GraphRAG Q&A System},
  year = {2025},
  url = {https://huggingface.co/spaces/hirthickraj2015/graphwiz-ireland}
}
```

---

## Contact

- **Author**: Hirthick Raj
- **HuggingFace**: [@hirthickraj2015](https://huggingface.co/hirthickraj2015)
- **Project**: [GraphWiz Ireland](https://huggingface.co/spaces/hirthickraj2015/graphwiz-ireland)

---

**Built with โค๏ธ for Ireland ๐Ÿ‡ฎ๐Ÿ‡ช**