File size: 250,090 Bytes
40645d0
2015a51
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
85842db
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d69d5b
2015a51
8d69d5b
 
 
 
 
 
 
 
 
2015a51
 
 
 
 
 
8d69d5b
 
 
40645d0
 
 
 
8d69d5b
 
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cd23a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d69d5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d69d5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04751a7
 
 
 
 
 
 
 
 
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d69d5b
2015a51
8d69d5b
 
 
 
 
 
 
 
 
2015a51
 
8d69d5b
 
40645d0
 
8d69d5b
 
40645d0
 
 
 
 
 
 
 
 
2015a51
40645d0
 
 
 
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04751a7
 
 
 
 
 
 
 
 
2015a51
8d69d5b
2015a51
 
 
 
 
 
8d69d5b
2015a51
 
8d69d5b
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04751a7
1cd23a6
04751a7
 
 
 
 
1cd23a6
04751a7
1cd23a6
04751a7
 
1cd23a6
 
2015a51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
85842db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
e10efac
40645d0
 
 
 
 
 
e10efac
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
9110364
40645d0
 
9110364
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
9110364
40645d0
 
9110364
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
9110364
40645d0
 
9110364
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
9110364
40645d0
 
9110364
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
 
 
 
 
 
 
 
 
 
 
 
9110364
 
40645d0
 
 
9110364
40645d0
 
 
 
1cd23a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
 
1cd23a6
 
 
2015a51
 
1cd23a6
 
 
 
 
 
 
 
2015a51
1cd23a6
 
 
2015a51
1cd23a6
 
 
 
 
2015a51
1cd23a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2015a51
1cd23a6
 
 
2015a51
 
 
1cd23a6
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
 
1cd23a6
 
 
 
 
 
 
 
 
 
 
 
2015a51
 
1cd23a6
 
 
 
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
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
// AI Engineer Masterclass — Module Data (Updated for AI Engineering Guidebook 2025 Edition)
const modules = [
    { id: 'llm-fundamentals', icon: '🧠', title: 'LLM Fundamentals', desc: 'Decoding strategies (SLED, Contrastive), 3 distillation types, 4 local setups, 7 generation params', category: 'Foundation', catClass: 'cat-foundation' },
    { id: 'transformers', icon: '⚡', title: 'Transformer Architecture', desc: 'KV Cache, GQA vs MQA, FlashAttention-3, RoPE, MoE routing patterns', category: 'Foundation', catClass: 'cat-foundation' },
    { id: 'huggingface', icon: '🤗', title: 'Hugging Face Ecosystem', desc: 'Transformers, PEFT, TRL, Safetensors, Hub API, programmatic deployment', category: 'Core Tools', catClass: 'cat-core' },
    { id: 'finetuning', icon: '🎯', title: 'Fine-Tuning & PEFT', desc: 'LoRA from scratch, QLoRA, SFT vs RFT, GRPO/DeepSeek-R1 logic, IFT generation', category: 'Core', catClass: 'cat-core' },
    { id: 'rag', icon: '🔍', title: 'RAG Pipelines', desc: '8 architectures (HyDE, REFRAG, CAG), 5 chunking types, RAGAS, context recall', category: 'Core', catClass: 'cat-core' },
    { id: 'vectordb', icon: '🗄️', title: 'Vector Databases', desc: 'HNSW levels, IVF-PQ, DiskANN, distance metrics, metadata filtering patterns', category: 'Core', catClass: 'cat-core' },
    { id: 'context-engineering', icon: '🧩', title: 'Context Engineering', desc: '6 context types, context window management, Claude skills, dynamic assembly', category: 'Core', catClass: 'cat-core' },
    { id: 'agents', icon: '🤖', title: 'AI Agents & Frameworks', desc: 'ReAct loops, 5 levels of autonomy, 4 layers of architecture, 8 multi-agent patterns', category: 'Agentic', catClass: 'cat-agent' },
    { id: 'agentic-patterns', icon: '🔮', title: 'Agentic Design Patterns', desc: 'Reflection, Tool-use, Planning, Multi-agent, Memory, ReAct from scratch', category: 'Agentic', catClass: 'cat-agent' },
    { id: 'multiagent', icon: '🕸️', title: 'Multi-Agent Systems', desc: '7 Patterns (Parallel, Router, Sequential, Hierarchical), swarm logic, orchestration', category: 'Agentic', catClass: 'cat-agent' },
    { id: 'agent-protocols', icon: '📡', title: 'Agent Protocol Landscape', desc: 'Standardizing Agent-to-Tool (MCP), Agent-to-Agent (A2A), and Agent-to-User (AG-UI)', category: 'Agentic', catClass: 'cat-agent' },
    { id: 'tools', icon: '🔧', title: 'Function Calling & Tools', desc: 'Tool schemas, JSON prompting, Verbalized Sampling, few-shot tool usage', category: 'Agentic', catClass: 'cat-agent' },
    { id: 'evaluation', icon: '📊', title: 'Evaluation & Benchmarks', desc: 'LLM-as-a-judge, DeepEval, DeepSeek benchmarks, quality vs latency tradeoffs', category: 'Production', catClass: 'cat-production' },
    { id: 'guardrails', icon: '🛡️', title: 'Guardrails & Safety', desc: 'Hallucination detection, LlamaGuard, NeMo, input/output filtering', category: 'Production', catClass: 'cat-production' },
    { id: 'deployment', icon: '🚀', title: 'Deployment & Serving', desc: 'vLLM, TGI, PagedAttention, quantization (GGUF/EXL2), serving at scale', category: 'Production', catClass: 'cat-production' },
    { id: 'production', icon: '⚙️', title: 'Production Patterns', desc: 'Observability, tracing, rate limiting, token cost optimization, caching', category: 'Production', catClass: 'cat-production' }
];


const MODULE_CONTENT = {
    'llm-fundamentals': {
        concepts: `
            <div class="section">
                <h2>🧠 LLM Fundamentals — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ The Core Idea</div>
                    <div class="box-content">
                        A language model is a probability distribution over sequences of tokens: <strong>P(token_n | token_1, token_2, ..., token_n-1)</strong>. LLMs are trained to predict the next token. During inference, they sample repeatedly from this distribution to generate text. Everything — creativity, reasoning, hallucination — emerges from this single objective.
                    </div>
                </div>

                <h3>1. How Language Models Actually Work</h3>
                <p>An LLM is fundamentally a <strong>next-token predictor</strong>. Given a sequence of tokens, it outputs a probability distribution over the entire vocabulary (~32K-128K tokens). The training objective is to minimize <strong>cross-entropy loss</strong> between the predicted distribution and the actual next token across billions of text examples. The model learns grammar, facts, reasoning patterns, and even code — all as statistical regularities in token sequences.</p>
                <div class="callout insight">
                    <div class="callout-title">🔑 Key Insight: Emergent Abilities</div>
                    <p>Below ~10B parameters, models just predict tokens. Above ~50B, new abilities <strong>emerge</strong> that weren't explicitly trained: chain-of-thought reasoning, few-shot learning, code generation, translation between languages never paired in training data. This is why scale matters and is the foundation of the "scaling laws" (Chinchilla, Kaplan et al.).</p>
                </div>

                <h3>2. Tokenization — The Hidden Layer</h3>
                <p>Text is never fed directly to an LLM. It's first converted to <strong>tokens</strong> (sub-word units). Understanding tokenization is critical because:</p>
                <table>
                    <tr><th>Aspect</th><th>Why It Matters</th><th>Example</th></tr>
                    <tr><td>Cost</td><td>API pricing is per-token, not per-word</td><td>"unbelievable" = 3 tokens = 3x cost vs 1 word</td></tr>
                    <tr><td>Context limits</td><td>128K tokens ≠ 128K words (~75K words)</td><td>1 token ≈ 0.75 English words on average</td></tr>
                    <tr><td>Non-English penalty</td><td>Languages like Hindi/Chinese use 2-3x more tokens per word</td><td>"नमस्ते" might be 6 tokens vs "hello" = 1 token</td></tr>
                    <tr><td>Code tokenization</td><td>Whitespace and syntax consume tokens</td><td>4 spaces of indentation = 1 token wasted per line</td></tr>
                    <tr><td>Number handling</td><td>Numbers tokenize unpredictably</td><td>"1234567" might split as ["123", "45", "67"] — why LLMs are bad at math</td></tr>
                </table>
                <p><strong>Algorithms:</strong> BPE (GPT, LLaMA) — merges frequent byte pairs iteratively. WordPiece (BERT) — maximizes likelihood. SentencePiece/Unigram (T5) — statistical segmentation. Modern LLMs use vocabularies of 32K-128K tokens.</p>

                <h3>3. Inference Parameters — Controlling Output</h3>
                <p>Every generation from an LLM is shaped by parameters under the hood. Knowing how to tune these is critical for production AI engineering.</p>
                <table>
                    <tr><th>Parameter</th><th>What it controls</th><th>Book Insight / Use Case</th></tr>
                    <tr><td><strong>1. Max tokens</strong></td><td>Hard cap on generation length</td><td>Lower for speed/safety; Higher for summaries</td></tr>
                    <tr><td><strong>2. Temperature</strong></td><td>Randomness/Creativity</td><td>~0 for deterministic QA; 0.7-1.0 for brainstorming</td></tr>
                    <tr><td><strong>3. Top-k</strong></td><td>Limit sampling to top K tokens</td><td>K=5 forces the model to choose only from 5 most likely words</td></tr>
                    <tr><td><strong>4. Top-p (nucleus)</strong></td><td>Limit to smallest set covering p% mass</td><td>P=0.9 is adaptive; handles coherence vs diversity better than K</td></tr>
                    <tr><td><strong>5. Frequency penalty</strong></td><td>Discourage reusing frequent tokens</td><td>Set > 0 to stop the model from repeating itself in loops</td></tr>
                    <tr><td><strong>6. Presence penalty</strong></td><td>Encourage new topics/tokens</td><td>Set > 0 to push the model towards broad exploration</td></tr>
                    <tr><td><strong>7. Stop sequences</strong></td><td>Halts generation at specific tokens</td><td>Critical for structured JSON (stop at "}") or code blocks</td></tr>
                    <tr><td><strong>Bonus: Min-p</strong></td><td>Dynamic probability threshold</td><td>Only keeps tokens at least X% as likely as the top token. Most robust for coherence.</td></tr>
                </table>
                <div class="callout warning">
                    <div class="callout-title">⚠️ Common Mistake</div>
                    <p>Don't combine temperature=0 with top_p=0.1 — they interact. Use <strong>either</strong> temperature OR top-p for sampling control, not both. OpenAI recommends changing one and leaving the other at default.</p>
                </div>

                <h3>4. 4 LLM Text Generation Strategies</h3>
                <p>Decoding is the process of picking the next token. How we pick it determines the style of the output.</p>
                <ul>
                    <li><strong>Greedy Strategy:</strong> Navely chooses the word with the highest probability. <em>Issue:</em> Often leads to repetitive, low-quality loops.</li>
                    <li><strong>Multinomial Sampling:</strong> Sample from the probability distribution available (controlled by temperature). <em>Benefit:</em> Much more creative and human-like.</li>
                    <li><strong>Beam Search:</strong> Explores top-K partial sequences (beams) and keeps alternatives alive. <em>Best for:</em> Translation and code where sequence-level correctness matters most.</li>
                    <li><strong>Contrastive Search:</strong> Balances fluency with diversity by penalizing continuations too similar to previous tokens. <em>Best for:</em> Long stories or creative writing to avoid loops.</li>
                </ul>

                <div class="callout insight">
                    <div class="callout-title">🚀 Bonus: SLED (Self-Logits Evolution Decoding)</div>
                    <p>Normally, models use only the final layer's logits. <strong>SLED</strong> looks at how logits evolve across ALL layers. It measures consensus across internal representations, producing more <strong>grounded and factual</strong> outputs without retraining or extra data.</p>
                </div>

                <h3>5. 3 Techniques to Train an LLM using another LLM</h3>
                <p>LLMs don't just learn from raw text; they transfer knowledge between models (Distillation). Distillation happens at Pre-training (Llama 4 Scout) or Post-training (DeepSeek-R1 to Qwen).</p>
                <table>
                    <tr><th>Technique</th><th>How It Works</th><th>Advantage / Note</th></tr>
                    <tr><td><strong>1. Soft-label Distillation</strong></td><td>Student matches the Teacher's entire probability distribution (softmax).</td><td>Maximum reasoning transfer; requires access to logits.</td></tr>
                    <tr><td><strong>2. Hard-label Distillation</strong></td><td>Student matches only the final token choice (hard label) of the Teacher.</td><td>DeepSeek used this to distill R1 into smaller models.</td></tr>
                    <tr><td><strong>3. Co-distillation</strong></td><td>Train Teacher and Student together from scratch; both predict concurrently.</td><td>Gemma 3 used this during both pre and post-training.</td></tr>
                </table>

                <h3>6. 4 Ways to Run LLMs Locally</h3>
                <table>
                    <tr><th>Tool</th><th>Best For</th><th>Setup Complexity</th></tr>
                    <tr><td><strong>Ollama</strong></td><td>Simple CLI/Desktop usage; one-command 'run'.</td><td>Zero (Just install app)</td></tr>
                    <tr><td><strong>LM Studio</strong></td><td>GUI for chatting with GGUF models; eject/load easily.</td><td>Zero (Desktop app)</td></tr>
                    <tr><td><strong>vLLM</strong></td><td>High-performance serving and API hosting.</td><td>Low-Medium (pip install)</td></tr>
                    <tr><td><strong>llama.cpp</strong></td><td>Extreme portability (C++), runs on Mac/CPU/Android.</td><td>Medium (compile or download bins)</td></tr>
                </table>

                <h3>7. Context Window — The LLM's Working Memory</h3>
                <p>The context window determines how many tokens the model can process in a single call (input + output combined).</p>
                <table>
                    <tr><th>Model</th><th>Context Window</th><th>Approx. Pages</th></tr>
                    <tr><td>GPT-4o</td><td>128K tokens</td><td>~200 pages</td></tr>
                    <tr><td>Claude 3.5 Sonnet</td><td>200K tokens</td><td>~350 pages</td></tr>
                    <tr><td>Gemini 1.5 Pro</td><td>2M tokens</td><td>~3,000 pages</td></tr>
                    <tr><td>LLaMA 3.1</td><td>128K tokens</td><td>~200 pages</td></tr>
                    <tr><td>Mistral Large</td><td>128K tokens</td><td>~200 pages</td></tr>
                </table>
                <p><strong>"Lost in the Middle"</strong> (Liu et al., 2023): Performance degrades for information placed in the middle of very long contexts. Models attend most to the <strong>beginning and end</strong> of prompts. Strategy: put the most important content at the start or end; use retrieval to avoid stuffing the entire context.</p>

                <h3>5. Pre-training Pipeline</h3>
                <p>Training an LLM from scratch involves: (1) <strong>Data collection</strong> — crawl the web (Common Crawl, ~1 trillion tokens), books, code (GitHub), conversations. (2) <strong>Data cleaning</strong> — deduplication, quality filtering, toxicity removal, PII scrubbing. (3) <strong>Tokenizer training</strong> — build BPE vocabulary from the corpus. (4) <strong>Pre-training</strong> — next-token prediction on massive GPU clusters (thousands of A100s/H100s for weeks). Cost: $2M-$100M+ for frontier models.</p>

                <h3>6. Alignment: RLHF, DPO, and Constitutional AI</h3>
                <p>A base model predicts tokens but doesn't follow instructions or refuse harmful content. Alignment methods bridge this gap:</p>
                <table>
                    <tr><th>Method</th><th>How It Works</th><th>Used By</th></tr>
                    <tr><td><strong>SFT (Supervised Fine-Tuning)</strong></td><td>Train on (instruction, response) pairs from human annotators</td><td>All models (Step 1)</td></tr>
                    <tr><td><strong>RLHF</strong></td><td>Train a reward model on human preferences, then optimize policy via PPO</td><td>GPT-4, Claude (early)</td></tr>
                    <tr><td><strong>DPO (Direct Preference Optimization)</strong></td><td>Skip the reward model — directly optimize from preference pairs, simpler and more stable</td><td>LLaMA 3, Zephyr, Gemma</td></tr>
                    <tr><td><strong>Constitutional AI</strong></td><td>Model critiques and revises its own outputs against a set of principles</td><td>Claude (Anthropic)</td></tr>
                    <tr><td><strong>RLAIF</strong></td><td>Use an AI model (not humans) to generate preference data</td><td>Gemini, some open models</td></tr>
                </table>

                <h3>7. The Modern LLM Landscape (2024-2025)</h3>
                <table>
                    <tr><th>Provider</th><th>Flagship Model</th><th>Strengths</th><th>Best For</th></tr>
                    <tr><td>OpenAI</td><td>GPT-4o, o1, o3</td><td>Best all-around, strong coding, reasoning chains (o1/o3)</td><td>General purpose, production</td></tr>
                    <tr><td>Anthropic</td><td>Claude 3.5 Sonnet</td><td>Best for long documents, coding, safety-conscious</td><td>Enterprise, agents, analysis</td></tr>
                    <tr><td>Google</td><td>Gemini 1.5 Pro/2.0</td><td>Massive context (2M), multi-modal, grounding</td><td>Document processing, multi-modal</td></tr>
                    <tr><td>Meta</td><td>LLaMA 3.1/3.2</td><td>Best open-source, fine-tunable, commercially free</td><td>Self-hosting, fine-tuning</td></tr>
                    <tr><td>Mistral</td><td>Mistral Large, Mixtral</td><td>Strong open models, MoE efficiency</td><td>European market, cost-effective</td></tr>
                    <tr><td>DeepSeek</td><td>DeepSeek V3, R1</td><td>Exceptional reasoning, competitive with o1</td><td>Math, coding, research</td></tr>
                </table>

                <h3>8. Advanced Decoding Strategies</h3>
                <p>Decoding is the process of generating tokens from probability distributions. Advanced techniques improve coherence and reasoning:</p>
                <table>
                    <tr><th>Strategy</th><th>How It Works</th><th>Best For</th></tr>
                    <tr><td><strong>SLED</strong></td><td>Speculative LEvel Decoding — utilizes smaller "draft" models to verify tokens, speeding up generation.</td><td>Low-latency serving</td></tr>
                    <tr><td><strong>Contrastive Decoding</strong></td><td>Generated by subtracting the log-probabilities of an amateur model from an expert model.</td><td>Reducing hallucinations</td></tr>
                    <tr><td><strong>Beam Search</strong></td><td>Maintains multiple hypotheses (beams) and selects the sequence with highest overall probability.</td><td>Translation, summarization</td></tr>
                    <tr><td><strong>Top-P (Nucleus)</strong></td><td>Samples from the smallest set of tokens whose cumulative probability exceeds P.</td><td>Creative writing</td></tr>
                    <tr><td><strong>Typical Sampling</strong></td><td>Favors tokens whose log-probability is close to the average entropy.</td><td>Consistent high-quality text</td></tr>
                </table>

                <h3>9. Model Distillation — 3 Core Types</h3>
                <p>Transferring knowledge from a large teacher (e.g., GPT-4) to a small student model (e.g., LLaMA-1B):</p>
                <ul>
                    <li><strong>1. Black-box Distillation:</strong> Use teacher output (labels/text) only. Simplest but less precise.</li>
                    <li><strong>2. Multi-teacher Distillation:</strong> Average gradients or outputs from multiple experts to a single student.</li>
                    <li><strong>3. Self-distillation:</strong> Using a model to critique and improve its own smaller/earlier versions.</li>
                </ul>

                <h3>10. Local Development Setups</h3>
                <table>
                    <tr><th>Setup</th><th>Key Advantage</th><th>Best For</th></tr>
                    <tr><td><strong>llama.cpp</strong></td><td>Pure C++, zero dependencies, runs on CPU/M-series.</td><td>Maximum portability</td></tr>
                    <tr><td><strong>MLC-LLM</strong></td><td>Universal deployment via Vulcan/WebGPU/Metal.</td><td>Cross-platform apps</td></tr>
                    <tr><td><strong>GPT4All</strong></td><td>Desktop UI, easy installer, local RAG built-in.</td><td>Non-technical users</td></tr>
                    <tr><td><strong>LM Studio</strong></td><td>Professional UI, model discovery, local server API.</td><td>Developer productivity</td></tr>
                </table>

                <h3>11. Scaling Laws — Why Bigger Models Get Smarter</h3>
                <p><strong>Chinchilla Scaling Law</strong> (Hoffmann et al., 2022): For a compute-optimal model, training tokens should scale proportionally to model parameters. A 70B model should be trained on ~1.4 trillion tokens. Key insight: many earlier models were <strong>undertrained</strong> (too many params, not enough data). LLaMA showed smaller, well-trained models can match larger undertrained ones.</p>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 LLM Fundamentals — Comprehensive Code Examples</h2>

                <h3>1. OpenAI API — Complete Patterns</h3>
                <div class="code-block"><span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI

client = OpenAI()  <span class="comment"># Uses OPENAI_API_KEY env var</span>

<span class="comment"># ─── Basic Chat Completion ───</span>
response = client.chat.completions.create(
    model=<span class="string">"gpt-4o"</span>,
    messages=[
        {<span class="string">"role"</span>: <span class="string">"system"</span>, <span class="string">"content"</span>: <span class="string">"You are an expert data scientist."</span>},
        {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Explain attention in 3 sentences."</span>}
    ],
    temperature=<span class="number">0.7</span>,
    max_tokens=<span class="number">512</span>
)
<span class="function">print</span>(response.choices[<span class="number">0</span>].message.content)

<span class="comment"># ─── Multi-turn Conversation ───</span>
messages = [
    {<span class="string">"role"</span>: <span class="string">"system"</span>, <span class="string">"content"</span>: <span class="string">"You are a Python tutor."</span>},
    {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"What is a decorator?"</span>},
    {<span class="string">"role"</span>: <span class="string">"assistant"</span>, <span class="string">"content"</span>: <span class="string">"A decorator is a function that wraps another function..."</span>},
    {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Show me an example with arguments."</span>}
]
resp = client.chat.completions.create(model=<span class="string">"gpt-4o"</span>, messages=messages)</div>

                <h3>2. Streaming Responses</h3>
                <div class="code-block"><span class="comment"># Streaming for real-time output — essential for UX</span>
stream = client.chat.completions.create(
    model=<span class="string">"gpt-4o"</span>,
    messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Write a haiku about neural nets"</span>}],
    stream=<span class="keyword">True</span>
)

full_response = <span class="string">""</span>
<span class="keyword">for</span> chunk <span class="keyword">in</span> stream:
    token = chunk.choices[<span class="number">0</span>].delta.content <span class="keyword">or</span> <span class="string">""</span>
    full_response += token
    <span class="function">print</span>(token, end=<span class="string">""</span>, flush=<span class="keyword">True</span>)

<span class="comment"># Async streaming (for FastAPI/web apps)</span>
<span class="keyword">async def</span> <span class="function">stream_chat</span>(prompt):
    <span class="keyword">from</span> openai <span class="keyword">import</span> AsyncOpenAI
    aclient = AsyncOpenAI()
    stream = <span class="keyword">await</span> aclient.chat.completions.create(
        model=<span class="string">"gpt-4o"</span>, stream=<span class="keyword">True</span>,
        messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: prompt}]
    )
    <span class="keyword">async for</span> chunk <span class="keyword">in</span> stream:
        <span class="keyword">yield</span> chunk.choices[<span class="number">0</span>].delta.content <span class="keyword">or</span> <span class="string">""</span></div>

                <h3>3. Token Counting & Cost Estimation</h3>
                <div class="code-block"><span class="keyword">import</span> tiktoken

<span class="comment"># Count tokens for any model</span>
enc = tiktoken.encoding_for_model(<span class="string">"gpt-4o"</span>)
text = <span class="string">"The transformer architecture changed everything."</span>
tokens = enc.encode(text)
<span class="function">print</span>(<span class="string">f"Token count: {len(tokens)}"</span>)
<span class="function">print</span>(<span class="string">f"Tokens: {[enc.decode([t]) for t in tokens]}"</span>)

<span class="comment"># Cost estimation helper</span>
<span class="keyword">def</span> <span class="function">estimate_cost</span>(text, model=<span class="string">"gpt-4o"</span>):
    enc = tiktoken.encoding_for_model(model)
    token_count = <span class="function">len</span>(enc.encode(text))
    prices = {
        <span class="string">"gpt-4o"</span>: (<span class="number">2.50</span>, <span class="number">10.00</span>),        <span class="comment"># (input, output) per 1M tokens</span>
        <span class="string">"gpt-4o-mini"</span>: (<span class="number">0.15</span>, <span class="number">0.60</span>),
        <span class="string">"claude-3-5-sonnet"</span>: (<span class="number">3.00</span>, <span class="number">15.00</span>),
    }
    input_price = prices.get(model, (<span class="number">1</span>,<span class="number">1</span>))[<span class="number">0</span>]
    cost = (token_count / <span class="number">1_000_000</span>) * input_price
    <span class="keyword">return</span> <span class="string">f"{token_count} tokens = $\\{cost:.4f}"</span>

print(estimate_cost(<span class="string">"Explain AI in 500 words"</span>))</div>

                <h3>4. Structured Output (JSON Mode)</h3>
                <div class="code-block"><span class="comment"># Force JSON output — essential for pipelines</span>
response = client.chat.completions.create(
    model=<span class="string">"gpt-4o"</span>,
    response_format={<span class="string">"type"</span>: <span class="string">"json_object"</span>},
    messages=[{
        <span class="string">"role"</span>: <span class="string">"user"</span>,
        <span class="string">"content"</span>: <span class="string">"Extract entities from: 'Elon Musk founded SpaceX in 2002'. Return JSON with fields: persons, orgs, dates."</span>
    }]
)
<span class="keyword">import</span> json
data = json.loads(response.choices[<span class="number">0</span>].message.content)
<span class="function">print</span>(data)  <span class="comment"># {"persons": ["Elon Musk"], "orgs": ["SpaceX"], "dates": ["2002"]}</span>

<span class="comment"># Pydantic structured output (newest API)</span>
<span class="keyword">from</span> pydantic <span class="keyword">import</span> BaseModel

<span class="keyword">class</span> <span class="function">Entity</span>(BaseModel):
    persons: list[str]
    organizations: list[str]
    dates: list[str]

completion = client.beta.chat.completions.parse(
    model=<span class="string">"gpt-4o"</span>,
    messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Extract from: 'Google was founded in 1998'"</span>}],
    response_format=Entity
)
entity = completion.choices[<span class="number">0</span>].message.parsed  <span class="comment"># Typed Entity object!</span></div>

                <h3>5. Multi-Provider Pattern (Anthropic & Google)</h3>
                <div class="code-block"><span class="comment"># ─── Anthropic (Claude) ───</span>
<span class="keyword">import</span> anthropic

claude = anthropic.Anthropic()
msg = claude.messages.create(
    model=<span class="string">"claude-3-5-sonnet-20241022"</span>,
    max_tokens=<span class="number">1024</span>,
    system=<span class="string">"You are an expert ML engineer."</span>,
    messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Explain LoRA"</span>}]
)
<span class="function">print</span>(msg.content[<span class="number">0</span>].text)

<span class="comment"># ─── Google Gemini ───</span>
<span class="keyword">import</span> google.generativeai <span class="keyword">as</span> genai

genai.configure(api_key=<span class="string">"YOUR_KEY"</span>)
model = genai.GenerativeModel(<span class="string">"gemini-1.5-pro"</span>)
response = model.generate_content(<span class="string">"Explain transformers"</span>)
<span class="function">print</span>(response.text)</div>

                <h3>6. Comparing Models Programmatically</h3>
                <div class="code-block"><span class="keyword">import</span> time

<span class="keyword">def</span> <span class="function">benchmark_model</span>(model_name, prompt, client):
    start = time.time()
    resp = client.chat.completions.create(
        model=model_name,
        messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: prompt}],
        max_tokens=<span class="number">300</span>
    )
    elapsed = time.time() - start
    tokens = resp.usage.total_tokens
    <span class="keyword">return</span> {
        <span class="string">"model"</span>: model_name,
        <span class="string">"tokens"</span>: tokens,
        <span class="string">"latency"</span>: <span class="string">f"{elapsed:.2f}s"</span>,
        <span class="string">"tokens_per_sec"</span>: <span class="function">round</span>(resp.usage.completion_tokens / elapsed),
        <span class="string">"response"</span>: resp.choices[<span class="number">0</span>].message.content[:<span class="number">100</span>]
    }

<span class="comment"># Compare GPT-4o vs GPT-4o-mini</span>
prompt = <span class="string">"What is the capital of France? Explain its history in 3 sentences."</span>
<span class="keyword">for</span> model <span class="keyword">in</span> [<span class="string">"gpt-4o"</span>, <span class="string">"gpt-4o-mini"</span>]:
    result = benchmark_model(model, prompt, client)
    <span class="function">print</span>(result)</div>
            </div > `,
        interview: `
    < div class="section" >
                <h2>🎯 LLM Fundamentals — In-Depth Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What happens when temperature = 0?</strong><p><strong>Answer:</strong> The model becomes <strong>deterministic</strong>, always picking the highest-probability token (greedy decoding). Use for tasks requiring consistency (code generation, extraction, classification). Side effects: can get stuck in repetitive loops; technically temperature=0 is greedy, temperature=1 is the trained distribution, above 1 is "hotter" (more random). For near-deterministic with slight randomness, use temperature=0.1 with top_p=1.</p></div>
                <div class="interview-box"><strong>Q2: Why do LLMs hallucinate, and what are the solutions?</strong><p><strong>Answer:</strong> LLMs don't "know" facts — they model <strong>token probabilities</strong>. When asked about something rare or unseen, the model generates statistically plausible text rather than admitting ignorance. Solutions: (1) <strong>RAG</strong> — ground answers in retrieved documents; (2) <strong>Lower temperature</strong> — reduce sampling randomness; (3) <strong>Structured output forcing</strong> — constrain output to valid formats; (4) <strong>Self-consistency</strong> — sample N times, pick the majority answer; (5) <strong>Calibrated prompting</strong> — explicitly instruct "say I don't know if unsure."</p></div>
                <div class="interview-box"><strong>Q3: What's the difference between context window and memory?</strong><p><strong>Answer:</strong> Context window is the tokens the model can process in a <strong>single inference pass</strong> — it's completely stateless. There is no persistent memory between API calls. "Memory" in frameworks like LangChain is implemented externally by: (1) storing past conversation turns in a database, (2) summarizing old turns to compress history, (3) reinserting relevant history into the new prompt. Every call is independent — the model has zero recall of previous calls.</p></div>
                <div class="interview-box"><strong>Q4: What is RLHF vs DPO and which is better?</strong><p><strong>Answer:</strong> <strong>RLHF</strong>: Train a separate reward model on human preferences, then optimize the LLM policy via PPO (reinforcement learning). Complex, unstable, expensive. <strong>DPO</strong> (Direct Preference Optimization): Skip the reward model entirely — directly optimize from preference pairs using a closed-form solution. Simpler, more stable, cheaper. DPO is now preferred for open-source models (LLaMA 3, Gemma). RLHF is still used by OpenAI/Anthropic where they have massive human labeling infrastructure.</p></div>
                <div class="interview-box"><strong>Q5: What is the "Lost in the Middle" phenomenon?</strong><p><strong>Answer:</strong> Research by Liu et al. (2023) showed that LLMs perform significantly worse when relevant information is placed in the <strong>middle</strong> of long contexts compared to the beginning or end. The model's attention mechanism attends most to recent tokens (recency bias) and the very first tokens (primacy bias). Practical implication: place the most critical context at the <strong>start or end</strong> of your prompt, never buried in the middle of a long document.</p></div>
                <div class="interview-box"><strong>Q6: Explain the difference between GPT-4o and o1/o3 models.</strong><p><strong>Answer:</strong> GPT-4o is a standard auto-regressive LLM — generates tokens left-to-right in one pass. o1/o3 are <strong>reasoning models</strong> that use "chain-of-thought before answering" — they generate internal reasoning tokens (hidden from the user) before producing the final answer. This makes them dramatically better at math, logic, and coding, but 3-10x slower and more expensive. Use GPT-4o for speed-sensitive tasks (chat, extraction), o1/o3 for complex reasoning (math proofs, hard coding, multi-step analysis).</p></div>
            </div > `
    },
    'transformers': {
        concepts: `
            <div class="section">
                <h2>🔗 Transformer Architecture — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ "Attention Is All You Need" (Vaswani et al., 2017)</div>
                    <div class="box-content">The Transformer replaced RNNs with pure attention mechanisms. The key insight: instead of processing tokens sequentially, process all tokens <strong>in parallel</strong>, computing relevance scores between every pair. This enabled massive parallelization on GPUs and is the foundation of every modern LLM, from GPT-4 to LLaMA to Gemini.</div>
                </div>

                <h3>1. Self-Attention — The Core Mechanism</h3>
                <p>For each token, compute 3 vectors via learned linear projections: <strong>Query (Q)</strong>, <strong>Key (K)</strong>, <strong>Value (V)</strong>. The attention formula:</p>
                <div class="formula">Attention(Q, K, V) = softmax(QK<sup>T</sup> / √d<sub>k</sub>) × V</div>
                <table>
                    <tr><th>Component</th><th>Role</th><th>Analogy</th></tr>
                    <tr><td><strong>Query (Q)</strong></td><td>What this token is looking for</td><td>Search query on Google</td></tr>
                    <tr><td><strong>Key (K)</strong></td><td>What each token offers/advertises</td><td>Page titles in search index</td></tr>
                    <tr><td><strong>Value (V)</strong></td><td>Actual content to retrieve</td><td>Page content returned</td></tr>
                    <tr><td><strong>√d<sub>k</sub> scaling</strong></td><td>Prevents softmax saturation for large dims</td><td>Normalization for numerical stability</td></tr>
                </table>
                <p><strong>Why it works:</strong> Self-attention lets every token attend to every other token in O(1) hops (vs O(n) for RNNs). "The cat sat on the <strong>mat</strong>" — the word "mat" can directly attend to "cat" and "sat" to understand context, without information passing through intermediate words.</p>

                <h3>2. Multi-Head Attention (MHA)</h3>
                <p>Run <strong>h independent attention heads</strong> in parallel, each learning different relationship types. Concatenate outputs and project. GPT-4 likely uses ~96 heads. Each head specializes: head 1 may track subject-verb agreement, head 2 may track pronoun coreference, head 3 may track positional patterns.</p>
                <div class="formula">MultiHead(Q,K,V) = Concat(head<sub>1</sub>, ..., head<sub>h</sub>) × W<sub>O</sub><br>where head<sub>i</sub> = Attention(QW<sub>i</sub><sup>Q</sup>, KW<sub>i</sub><sup>K</sup>, VW<sub>i</sub><sup>V</sup>)</div>

                <h3>3. Modern Attention Variants</h3>
                <table>
                    <tr><th>Variant</th><th>Key Idea</th><th>Used By</th><th>KV Cache Savings</th></tr>
                    <tr><td><strong>MHA</strong> (Multi-Head)</td><td>Separate Q, K, V per head</td><td>GPT-2, BERT</td><td>1x (baseline)</td></tr>
                    <tr><td><strong>GQA</strong> (Grouped Query)</td><td>Share K,V across groups of Q heads</td><td>LLaMA 3, Gemma, Mistral</td><td>4-8x smaller</td></tr>
                    <tr><td><strong>MQA</strong> (Multi-Query)</td><td>Single K,V shared across ALL Q heads</td><td>PaLM, Falcon</td><td>32-96x smaller</td></tr>
                    <tr><td><strong>Sliding Window</strong></td><td>Attend only to nearby tokens (window)</td><td>Mistral, Mixtral</td><td>Fixed memory regardless of length</td></tr>
                </table>

                <h3>4. Positional Encoding (RoPE)</h3>
                <p><strong>RoPE (Rotary Position Embedding)</strong> encodes position by rotating Q and K vectors in complex space. Advantages: (1) Relative position naturally emerges from dot products, (2) Enables length extrapolation beyond training length (with techniques like YaRN, NTK-aware scaling), (3) No additional parameters. Used by LLaMA, Mistral, Gemma, Qwen — virtually all modern open LLMs.</p>

                <h3>5. Transformer Block Architecture</h3>
                <p>Each transformer block has: (1) <strong>Multi-Head Attention</strong> → (2) <strong>Residual Connection + LayerNorm</strong> → (3) <strong>Feed-Forward Network (FFN)</strong> with hidden dim 4x model dim → (4) <strong>Residual Connection + LayerNorm</strong>. Modern models use <strong>Pre-LayerNorm</strong> (normalize before attention, not after) and <strong>SwiGLU</strong> activation in FFN instead of ReLU for better performance.</p>

                <h3>6. FlashAttention — Memory-Efficient Attention</h3>
                <p>Standard attention requires O(n²) memory for the attention matrix. <strong>FlashAttention</strong> (Dao et al., 2022) computes exact attention without materializing the full matrix by using tiling and kernel fusion. Result: 2-4x faster inference, ~20x less memory for long sequences. FlashAttention 2 adds further optimizations. Essential for context windows &gt;8K tokens.</p>

                <h3>7. Mixture-of-Experts (MoE)</h3>
                <p>Instead of one massive FFN, use <strong>N expert FFNs</strong> and a router that selects top-k experts per token. Only selected experts are activated (sparse computation). Mixtral 8x7B has 8 experts, activates 2 per token — 47B total params but only 13B active per token. Result: 3-4x more efficient than dense models of same quality.</p>

                <h3>8. Decoder-Only vs Encoder-Decoder</h3>
                <table>
                    <tr><th>Architecture</th><th>Attention Type</th><th>Best For</th><th>Examples</th></tr>
                    <tr><td><strong>Decoder-Only</strong></td><td>Causal (left-to-right only)</td><td>Text generation, chat, code</td><td>GPT-4, LLaMA, Gemma, Mistral</td></tr>
                    <tr><td><strong>Encoder-Only</strong></td><td>Bidirectional (sees all tokens)</td><td>Classification, NER, embeddings</td><td>BERT, RoBERTa, DeBERTa</td></tr>
                    <tr><td><strong>Encoder-Decoder</strong></td><td>Encoder bidirectional, decoder causal</td><td>Translation, summarization</td><td>T5, BART, mT5, Flan-T5</td></tr>
                </table>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Transformer Architecture — Code Examples</h2>

                <h3>1. Self-Attention from Scratch (NumPy)</h3>
                <div class="code-block"><span class="keyword">import</span> numpy <span class="keyword">as</span> np

<span class="keyword">def</span> <span class="function">scaled_dot_product_attention</span>(Q, K, V, mask=<span class="keyword">None</span>):
    d_k = Q.shape[-<span class="number">1</span>]
    scores = np.matmul(Q, K.transpose(-<span class="number">2</span>, -<span class="number">1</span>)) / np.sqrt(d_k)
    <span class="keyword">if</span> mask <span class="keyword">is not None</span>:
        scores = np.where(mask == <span class="number">0</span>, -<span class="number">1e9</span>, scores)
    weights = np.exp(scores) / np.sum(np.exp(scores), axis=-<span class="number">1</span>, keepdims=<span class="keyword">True</span>)
    <span class="keyword">return</span> np.matmul(weights, V), weights

<span class="comment"># Example: 3 tokens, d_model=4</span>
Q = np.random.randn(<span class="number">3</span>, <span class="number">4</span>)
K = np.random.randn(<span class="number">3</span>, <span class="number">4</span>)
V = np.random.randn(<span class="number">3</span>, <span class="number">4</span>)
output, attn_weights = scaled_dot_product_attention(Q, K, V)
<span class="function">print</span>(<span class="string">"Attention weights (each row sums to 1):"</span>)
<span class="function">print</span>(attn_weights)</div>

                <h3>2. PyTorch Multi-Head Attention</h3>
                <div class="code-block"><span class="keyword">import</span> torch
<span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn

<span class="keyword">class</span> <span class="function">MultiHeadAttention</span>(nn.Module):
    <span class="keyword">def</span> <span class="function">__init__</span>(self, d_model=<span class="number">512</span>, n_heads=<span class="number">8</span>):
        <span class="keyword">super</span>().__init__()
        self.n_heads = n_heads
        self.d_k = d_model // n_heads
        self.W_q = nn.Linear(d_model, d_model)
        self.W_k = nn.Linear(d_model, d_model)
        self.W_v = nn.Linear(d_model, d_model)
        self.W_o = nn.Linear(d_model, d_model)

    <span class="keyword">def</span> <span class="function">forward</span>(self, x, mask=<span class="keyword">None</span>):
        B, T, C = x.shape
        Q = self.W_q(x).view(B, T, self.n_heads, self.d_k).transpose(<span class="number">1</span>, <span class="number">2</span>)
        K = self.W_k(x).view(B, T, self.n_heads, self.d_k).transpose(<span class="number">1</span>, <span class="number">2</span>)
        V = self.W_v(x).view(B, T, self.n_heads, self.d_k).transpose(<span class="number">1</span>, <span class="number">2</span>)

        scores = (Q @ K.transpose(-<span class="number">2</span>, -<span class="number">1</span>)) / (self.d_k ** <span class="number">0.5</span>)
        <span class="keyword">if</span> mask <span class="keyword">is not None</span>:
            scores = scores.masked_fill(mask == <span class="number">0</span>, -<span class="number">1e9</span>)
        attn = torch.softmax(scores, dim=-<span class="number">1</span>)
        out = (attn @ V).transpose(<span class="number">1</span>, <span class="number">2</span>).contiguous().view(B, T, C)
        <span class="keyword">return</span> self.W_o(out)

<span class="comment"># Usage</span>
mha = MultiHeadAttention(d_model=<span class="number">512</span>, n_heads=<span class="number">8</span>)
x = torch.randn(<span class="number">2</span>, <span class="number">10</span>, <span class="number">512</span>)  <span class="comment"># batch=2, seq=10, dim=512</span>
output = mha(x)  <span class="comment"># (2, 10, 512)</span></div>

                <h3>3. Inspecting Attention Patterns</h3>
                <div class="code-block"><span class="keyword">from</span> transformers <span class="keyword">import</span> AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained(<span class="string">"gpt2"</span>, output_attentions=<span class="keyword">True</span>)
tokenizer = AutoTokenizer.from_pretrained(<span class="string">"gpt2"</span>)

inputs = tokenizer(<span class="string">"The cat sat on the"</span>, return_tensors=<span class="string">"pt"</span>)
outputs = model(**inputs)

<span class="comment"># outputs.attentions: tuple of (batch, heads, seq, seq) per layer</span>
attn = outputs.attentions[<span class="number">0</span>]  <span class="comment"># Layer 0: shape (1, 12, 6, 6)</span>
<span class="function">print</span>(<span class="string">f"Layers: {len(outputs.attentions)}, Heads: {attn.shape[1]}"</span>)
<span class="function">print</span>(<span class="string">f"Token 'the' attends most to: {attn[0, 0, -1].argmax()}"</span>)</div>

                <h3>4. FlashAttention Usage</h3>
                <div class="code-block"><span class="keyword">from</span> transformers <span class="keyword">import</span> AutoModelForCausalLM
<span class="keyword">import</span> torch

<span class="comment"># Enable FlashAttention 2 (requires compatible GPU)</span>
model = AutoModelForCausalLM.from_pretrained(
    <span class="string">"meta-llama/Llama-3.1-8B-Instruct"</span>,
    torch_dtype=torch.bfloat16,
    attn_implementation=<span class="string">"flash_attention_2"</span>,  <span class="comment"># 2-4x faster!</span>
    device_map=<span class="string">"auto"</span>
)

<span class="comment"># Or use SDPA (PyTorch native, works everywhere)</span>
model = AutoModelForCausalLM.from_pretrained(
    <span class="string">"meta-llama/Llama-3.1-8B-Instruct"</span>,
    attn_implementation=<span class="string">"sdpa"</span>,  <span class="comment"># Scaled Dot Product Attention</span>
    device_map=<span class="string">"auto"</span>
)</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Transformer Architecture — In-Depth Interview Questions</h2>
                <div class="interview-box"><strong>Q1: Why divide by √d_k in attention?</strong><p><strong>Answer:</strong> For large d_k, dot products grow large in magnitude (variance ≈ d_k), pushing softmax into regions with extremely small gradients (saturated). Dividing by √d_k normalizes variance to 1, keeping gradients healthy. Without it, training becomes unstable — same principle as Xavier/He weight initialization.</p></div>
                <div class="interview-box"><strong>Q2: What is KV Cache and why is it critical for inference?</strong><p><strong>Answer:</strong> During autoregressive generation, Key and Value matrices for past tokens are <strong>cached in GPU memory</strong> so they don't need recomputation on each new token. Without KV cache: generating token n requires reprocessing all n-1 previous tokens — O(n²) total work. With KV cache: each new token only computes its own Q, K, V and attends to cached K, V — O(n) total. A 7B model with 8K context uses ~4GB just for KV cache. This is why <strong>GPU memory</strong> (not compute) is the real bottleneck for long-context inference.</p></div>
                <div class="interview-box"><strong>Q3: What's the difference between MHA, GQA, and MQA?</strong><p><strong>Answer:</strong> <strong>MHA</strong> (Multi-Head Attention): Separate K,V per head — maximum expressivity but largest KV cache. <strong>GQA</strong> (Grouped Query Attention): K,V shared across groups of Q heads (e.g., 8 Q heads share 1 KV pair). 4-8x smaller KV cache with minimal quality loss. Used by LLaMA-3, Mistral, Gemma. <strong>MQA</strong> (Multi-Query Attention): ALL Q heads share a SINGLE K,V pair. Maximum KV cache savings (32-96x) but slightly lower quality. Used by PaLM, Falcon. Industry has settled on GQA as the best tradeoff.</p></div>
                <div class="interview-box"><strong>Q4: What is RoPE and why replaced sinusoidal encoding?</strong><p><strong>Answer:</strong> RoPE (Rotary Position Embedding) encodes position by <strong>rotating</strong> Q and K vectors in 2D complex planes. Key advantages: (1) Relative position naturally emerges from dot products — Attention(q_m, k_n) depends only on m-n, not absolute positions. (2) No additional learned parameters. (3) Better length generalization — techniques like YaRN and NTK-aware scaling allow extending context beyond training length. Sinusoidal encoding struggled with extrapolation and required absolute position awareness.</p></div>
                <div class="interview-box"><strong>Q5: What is FlashAttention and how does it achieve speedup?</strong><p><strong>Answer:</strong> Standard attention materializes the full N×N attention matrix in GPU HBM (High Bandwidth Memory). FlashAttention uses <strong>tiling</strong> — it breaks Q, K, V into blocks, computes attention within SRAM (fast on-chip memory), and never writes the full attention matrix to HBM. This reduces memory IO from O(N²) to O(N²/M) where M is SRAM size. Result: exact same output, but 2-4x faster and uses O(N) memory instead of O(N²). It's a pure systems optimization — no approximation.</p></div>
                <div class="interview-box"><strong>Q6: Explain Mixture-of-Experts (MoE) and its tradeoffs.</strong><p><strong>Answer:</strong> MoE replaces the single FFN in each transformer block with N parallel expert FFNs plus a learned router. For each token, the router selects top-k experts (usually k=2). Only selected experts are activated — rest are skipped. <strong>Benefits:</strong> Train a model with 8x more parameters at ~2x the compute cost of a dense model. <strong>Tradeoffs:</strong> (1) All parameters must fit in memory even though only k are active. (2) Load balancing — if router always picks the same experts, others waste space. Solved with auxiliary loss. (3) Harder to fine-tune — expert specialization can be disrupted. Example: Mixtral 8x7B = 47B params but only 13B active per token.</p></div>
            </div>`
    },
    'huggingface': {
        concepts: `
    < div class="section" >
                <h2>🤗 Hugging Face Deep Dive — The Complete Ecosystem</h2>
                <div class="info-box">
                    <div class="box-title">⚡ The GitHub of AI</div>
                    <div class="box-content">Hugging Face (HF) is the central hub for the ML community. With <strong>700,000+ models</strong>, <strong>150,000+ datasets</strong>, and 15+ libraries, it's the standard toolchain for modern AI — from experimentation to production deployment. Understanding HF deeply is essential for any GenAI practitioner.</div>
                </div>

                <h3>1. Transformers Library — The Core Engine</h3>
                <p>The <code>transformers</code> library provides a unified API to load, run, and fine-tune any model architecture. It wraps 200+ architectures (GPT, LLaMA, Mistral, Gemma, T5, BERT, ViT, Whisper, etc.) behind <strong>Auto Classes</strong> that detect architecture automatically from <code>config.json</code>.</p>
                <table>
                    <tr><th>AutoClass</th><th>Use Case</th><th>Example Models</th></tr>
                    <tr><td>AutoModelForCausalLM</td><td>Text generation (decoder-only)</td><td>LLaMA, GPT-2, Mistral, Gemma</td></tr>
                    <tr><td>AutoModelForSeq2SeqLM</td><td>Translation, summarization</td><td>T5, BART, mT5, Flan-T5</td></tr>
                    <tr><td>AutoModelForSequenceClassification</td><td>Text classification, sentiment</td><td>BERT, RoBERTa, DeBERTa</td></tr>
                    <tr><td>AutoModelForTokenClassification</td><td>NER, POS tagging</td><td>BERT-NER, SpanBERT</td></tr>
                    <tr><td>AutoModelForQuestionAnswering</td><td>Extractive QA</td><td>BERT-QA, RoBERTa-QA</td></tr>
                    <tr><td>AutoModel (base)</td><td>Embeddings, custom heads</td><td>Any backbone</td></tr>
                </table>
                <div class="callout tip">
                    <div class="callout-title">💡 Key from_pretrained() Arguments</div>
                    <p><code>torch_dtype=torch.bfloat16</code> — half precision, saves 50% memory<br>
                    <code>device_map="auto"</code> — auto-shard across GPUs/CPU/disk<br>
                    <code>load_in_4bit=True</code> — 4-bit quantization via BitsAndBytes<br>
                    <code>attn_implementation="flash_attention_2"</code> — use FlashAttention for 2-4x faster inference<br>
                    <code>trust_remote_code=True</code> — needed for custom architectures with code on the Hub</p>
                </div>

                <h3>2. Pipelines — 20+ Tasks in One Line</h3>
                <p>The <code>pipeline()</code> function wraps tokenization + model + post-processing into a single call. Under the hood: tokenize → model forward pass → decode/format output. Supports these key tasks:</p>
                <table>
                    <tr><th>Task</th><th>Pipeline Name</th><th>Default Model</th></tr>
                    <tr><td>Text Generation</td><td><code>"text-generation"</code></td><td>gpt2</td></tr>
                    <tr><td>Sentiment Analysis</td><td><code>"sentiment-analysis"</code></td><td>distilbert-sst2</td></tr>
                    <tr><td>Named Entity Recognition</td><td><code>"ner"</code></td><td>dbmdz/bert-large-NER</td></tr>
                    <tr><td>Summarization</td><td><code>"summarization"</code></td><td>sshleifer/distilbart-cnn</td></tr>
                    <tr><td>Translation</td><td><code>"translation_en_to_fr"</code></td><td>Helsinki-NLP/opus-mt</td></tr>
                    <tr><td>Zero-Shot Classification</td><td><code>"zero-shot-classification"</code></td><td>facebook/bart-large-mnli</td></tr>
                    <tr><td>Feature Extraction (Embeddings)</td><td><code>"feature-extraction"</code></td><td>sentence-transformers</td></tr>
                    <tr><td>Image Classification</td><td><code>"image-classification"</code></td><td>google/vit-base</td></tr>
                    <tr><td>Speech Recognition</td><td><code>"automatic-speech-recognition"</code></td><td>openai/whisper-base</td></tr>
                    <tr><td>Text-to-Image</td><td><code>"text-to-image"</code></td><td>stabilityai/sdxl</td></tr>
                </table>

                <h3>3. Tokenizers Library — Rust-Powered Speed</h3>
                <p>The <code>tokenizers</code> library is written in Rust with Python bindings. It's 10-100x faster than pure-Python tokenizers. Key tokenization algorithms used by modern LLMs:</p>
                <table>
                    <tr><th>Algorithm</th><th>Used By</th><th>How It Works</th></tr>
                    <tr><td>BPE (Byte-Pair Encoding)</td><td>GPT-2, GPT-4, LLaMA</td><td>Repeatedly merges most frequent byte pairs. "unbelievable" → ["un", "believ", "able"]</td></tr>
                    <tr><td>SentencePiece (Unigram)</td><td>T5, ALBERT, XLNet</td><td>Statistical model that finds optimal subword segmentation probabilistically</td></tr>
                    <tr><td>WordPiece</td><td>BERT, DistilBERT</td><td>Greedy algorithm; splits by maximizing likelihood. Uses "##" prefix for sub-tokens</td></tr>
                </table>
                <div class="callout warning">
                    <div class="callout-title">⚠️ Tokenizer Gotchas</div>
                    <p>• Numbers tokenize unpredictably: "1234" might be 1-4 tokens depending on the model<br>
                    • Whitespace matters: " hello" and "hello" produce different tokens in GPT<br>
                    • Non-English languages use more tokens per word (higher cost per concept)<br>
                    • Always use the model's own tokenizer — never mix tokenizers between models</p>
                </div>

                <h3>4. Datasets Library — Apache Arrow Under the Hood</h3>
                <p><code>datasets</code> uses <strong>Apache Arrow</strong> for columnar, memory-mapped storage. A 100GB dataset can be iterated without loading into RAM. Key features:</p>
                <p><strong>Memory Mapping:</strong> Data stays on disk; only accessed rows are loaded into memory. <strong>Streaming:</strong> <code>load_dataset(..., streaming=True)</code> returns an iterable — process terabytes with constant memory. <strong>Map/Filter:</strong> Apply transformations with automatic caching and multiprocessing. <strong>Hub Integration:</strong> 150,000+ datasets available via <code>load_dataset("dataset_name")</code>.</p>

                <h3>5. Trainer API — High-Level Training Loop</h3>
                <p>The <code>Trainer</code> class handles: training loop, evaluation, checkpointing, logging (to TensorBoard/W&B), mixed precision, gradient accumulation, distributed training, and early stopping. You just provide model + dataset + TrainingArguments. For instruction-tuning LLMs, use <strong>TRL's SFTTrainer</strong> (built on top of Trainer) which handles chat templates and packing automatically.</p>

                <h3>6. Accelerate — Distributed Training Made Easy</h3>
                <p><code>accelerate</code> abstracts away multi-GPU, TPU, and mixed-precision complexity. Write your training loop once; run on 1 GPU or 64 GPUs with zero code changes. Key feature: <code>Accelerator</code> class wraps your model, optimizer, and dataloader. It handles data sharding, gradient synchronization, and device placement automatically.</p>

                <h3>7. Model Hub — Everything Is a Git Repo</h3>
                <p>Every model on HF Hub is a <strong>Git LFS repo</strong> containing: <code>config.json</code> (architecture), <code>model.safetensors</code> (weights), <code>tokenizer.json</code>, and a <code>README.md</code> (model card). You can push your own models with <code>model.push_to_hub()</code>. The Hub supports: model versioning (Git branches/tags), automatic model cards, gated access (license agreements), and API inference endpoints.</p>

                <h3>8. Additional HF Libraries</h3>
                <table>
                    <tr><th>Library</th><th>Purpose</th><th>Key Feature</th></tr>
                    <tr><td><code>peft</code></td><td>Parameter-efficient fine-tuning</td><td>LoRA, QLoRA, Adapters, Prompt Tuning</td></tr>
                    <tr><td><code>trl</code></td><td>RLHF and alignment training</td><td>SFTTrainer, DPOTrainer, PPOTrainer, RewardTrainer</td></tr>
                    <tr><td><code>diffusers</code></td><td>Image/video generation</td><td>Stable Diffusion, SDXL, ControlNet, IP-Adapter</td></tr>
                    <tr><td><code>evaluate</code></td><td>Metrics computation</td><td>BLEU, ROUGE, accuracy, perplexity, and 100+ metrics</td></tr>
                    <tr><td><code>gradio</code></td><td>Build ML demos</td><td>Web UI for any model in 5 lines of code</td></tr>
                    <tr><td><code>smolagents</code></td><td>Lightweight AI agents</td><td>Code-based tool calling, HF model integration</td></tr>
                    <tr><td><code>safetensors</code></td><td>Safe model format</td><td>Fast, safe, and efficient tensor serialization (replaces pickle)</td></tr>
                    <tr><td><code>huggingface_hub</code></td><td>Hub API client</td><td>Download files, push models, create repos, manage spaces</td></tr>
                </table>

                <h3>9. Spaces — Deploy ML Apps Free</h3>
                <p>HF Spaces lets you deploy <strong>Gradio</strong> or <strong>Streamlit</strong> apps on managed infrastructure. Free CPU tier for demos; upgrade to T4 ($0.60/hr) or A100 ($3.09/hr) for GPU workloads. Spaces support Docker, static HTML, and custom environments. They auto-build from a Git repo with a simple <code>requirements.txt</code>. Ideal for: model demos, portfolio projects, internal tools, and quick prototypes.</p>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Hugging Face — Comprehensive Code Examples</h2>

                <h3>1. Pipelines — Every Task</h3>
                <div class="code-block"><span class="keyword">from</span> transformers <span class="keyword">import</span> pipeline

<span class="comment"># ─── Text Generation ───</span>
gen = pipeline(<span class="string">"text-generation"</span>, model=<span class="string">"meta-llama/Llama-3.2-1B-Instruct"</span>)
result = gen(<span class="string">"Explain RAG in one paragraph:"</span>, max_new_tokens=<span class="number">200</span>)
<span class="function">print</span>(result[<span class="number">0</span>][<span class="string">"generated_text"</span>])

<span class="comment"># ─── Sentiment Analysis ───</span>
sa = pipeline(<span class="string">"sentiment-analysis"</span>)
<span class="function">print</span>(sa(<span class="string">"Hugging Face is amazing!"</span>))
<span class="comment"># [{'label': 'POSITIVE', 'score': 0.9998}]</span>

<span class="comment"># ─── Named Entity Recognition ───</span>
ner = pipeline(<span class="string">"ner"</span>, grouped_entities=<span class="keyword">True</span>)
<span class="function">print</span>(ner(<span class="string">"Elon Musk founded SpaceX in California"</span>))
<span class="comment"># [{'entity_group': 'PER', 'word': 'Elon Musk'}, ...]</span>

<span class="comment"># ─── Zero-Shot Classification (no training needed!) ───</span>
zsc = pipeline(<span class="string">"zero-shot-classification"</span>)
result = zsc(<span class="string">"I need to fix a bug in my Python code"</span>,
    candidate_labels=[<span class="string">"programming"</span>, <span class="string">"cooking"</span>, <span class="string">"sports"</span>])
<span class="function">print</span>(result[<span class="string">"labels"</span>][<span class="number">0</span>])  <span class="comment"># "programming"</span>

<span class="comment"># ─── Speech Recognition (Whisper) ───</span>
asr = pipeline(<span class="string">"automatic-speech-recognition"</span>, model=<span class="string">"openai/whisper-large-v3"</span>)
<span class="function">print</span>(asr(<span class="string">"audio.mp3"</span>)[<span class="string">"text"</span>])</div>

                <h3>2. Tokenizers Deep Dive</h3>
                <div class="code-block"><span class="keyword">from</span> transformers <span class="keyword">import</span> AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained(<span class="string">"meta-llama/Llama-3.1-8B-Instruct"</span>)

<span class="comment"># Basic tokenization</span>
text = <span class="string">"Hugging Face transformers are powerful!"</span>
tokens = tokenizer.tokenize(text)
ids = tokenizer.encode(text)
<span class="function">print</span>(<span class="string">f"Tokens: {tokens}"</span>)
<span class="function">print</span>(<span class="string">f"IDs: {ids}"</span>)
<span class="function">print</span>(<span class="string">f"Decoded: {tokenizer.decode(ids)}"</span>)

<span class="comment"># Chat template (critical for instruction models)</span>
messages = [
    {<span class="string">"role"</span>: <span class="string">"system"</span>, <span class="string">"content"</span>: <span class="string">"You are a helpful assistant."</span>},
    {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"What is LoRA?"</span>}
]
formatted = tokenizer.apply_chat_template(messages, tokenize=<span class="keyword">False</span>)
<span class="function">print</span>(formatted)  <span class="comment"># Proper &lt;|start_header|&gt; format for Llama</span>

<span class="comment"># Batch tokenization with padding</span>
batch = tokenizer(
    [<span class="string">"short"</span>, <span class="string">"a much longer sentence here"</span>],
    padding=<span class="keyword">True</span>,
    truncation=<span class="keyword">True</span>,
    max_length=<span class="number">512</span>,
    return_tensors=<span class="string">"pt"</span>  <span class="comment"># Returns PyTorch tensors</span>
)
<span class="function">print</span>(batch.keys())  <span class="comment"># input_ids, attention_mask</span></div>

                <h3>3. Datasets Library — Load, Process, Stream</h3>
                <div class="code-block"><span class="keyword">from</span> datasets <span class="keyword">import</span> load_dataset, Dataset

<span class="comment"># Load from Hub</span>
ds = load_dataset(<span class="string">"imdb"</span>)
<span class="function">print</span>(ds)  <span class="comment"># DatasetDict with 'train' and 'test' splits</span>
<span class="function">print</span>(ds[<span class="string">"train"</span>][<span class="number">0</span>])  <span class="comment"># First example</span>

<span class="comment"># Streaming (constant memory for huge datasets)</span>
stream = load_dataset(<span class="string">"allenai/c4"</span>, split=<span class="string">"train"</span>, streaming=<span class="keyword">True</span>)
<span class="keyword">for</span> i, example <span class="keyword">in</span> enumerate(stream):
    <span class="keyword">if</span> i >= <span class="number">5</span>: <span class="keyword">break</span>
    <span class="function">print</span>(example[<span class="string">"text"</span>][:<span class="number">100</span>])

<span class="comment"># Map with parallel processing</span>
<span class="keyword">def</span> <span class="function">tokenize_fn</span>(examples):
    <span class="keyword">return</span> tokenizer(examples[<span class="string">"text"</span>], truncation=<span class="keyword">True</span>, max_length=<span class="number">512</span>)

tokenized = ds[<span class="string">"train"</span>].map(tokenize_fn, batched=<span class="keyword">True</span>, num_proc=<span class="number">4</span>)

<span class="comment"># Create custom dataset from dict/pandas</span>
my_data = Dataset.from_dict({
    <span class="string">"text"</span>: [<span class="string">"Hello world"</span>, <span class="string">"AI is great"</span>],
    <span class="string">"label"</span>: [<span class="number">1</span>, <span class="number">0</span>]
})

<span class="comment"># Push your dataset to Hub</span>
my_data.push_to_hub(<span class="string">"your-username/my-dataset"</span>)</div>

                <h3>4. Model Loading — From Basic to Production</h3>
                <div class="code-block"><span class="keyword">from</span> transformers <span class="keyword">import</span> AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
<span class="keyword">import</span> torch

<span class="comment"># ─── Basic Loading (full precision) ───</span>
model = AutoModelForCausalLM.from_pretrained(<span class="string">"gpt2"</span>)

<span class="comment"># ─── Half Precision (saves 50% VRAM) ───</span>
model = AutoModelForCausalLM.from_pretrained(
    <span class="string">"meta-llama/Llama-3.1-8B-Instruct"</span>,
    torch_dtype=torch.bfloat16,
    device_map=<span class="string">"auto"</span>
)

<span class="comment"># ─── 4-bit Quantization (QLoRA-ready) ───</span>
bnb_config = BitsAndBytesConfig(
    load_in_4bit=<span class="keyword">True</span>,
    bnb_4bit_quant_type=<span class="string">"nf4"</span>,        <span class="comment"># NormalFloat4 — better than uniform int4</span>
    bnb_4bit_compute_dtype=torch.bfloat16,
    bnb_4bit_use_double_quant=<span class="keyword">True</span>    <span class="comment"># Quantize the quantization constants too</span>
)
model = AutoModelForCausalLM.from_pretrained(
    <span class="string">"meta-llama/Llama-3.1-8B-Instruct"</span>,
    quantization_config=bnb_config,
    device_map=<span class="string">"auto"</span>
)

<span class="comment"># ─── Flash Attention 2 (2-4x faster) ───</span>
model = AutoModelForCausalLM.from_pretrained(
    <span class="string">"meta-llama/Llama-3.1-8B-Instruct"</span>,
    torch_dtype=torch.bfloat16,
    attn_implementation=<span class="string">"flash_attention_2"</span>,
    device_map=<span class="string">"auto"</span>
)</div>

                <h3>5. Trainer API — Full Training Loop</h3>
                <div class="code-block"><span class="keyword">from</span> transformers <span class="keyword">import</span> AutoModelForSequenceClassification, Trainer, TrainingArguments
<span class="keyword">from</span> datasets <span class="keyword">import</span> load_dataset

<span class="comment"># Load model and dataset</span>
model = AutoModelForSequenceClassification.from_pretrained(<span class="string">"bert-base-uncased"</span>, num_labels=<span class="number">2</span>)
ds = load_dataset(<span class="string">"imdb"</span>)
tokenized = ds.map(<span class="keyword">lambda</span> x: tokenizer(x[<span class="string">"text"</span>], truncation=<span class="keyword">True</span>, max_length=<span class="number">512</span>), batched=<span class="keyword">True</span>)

<span class="comment"># Configure training</span>
args = TrainingArguments(
    output_dir=<span class="string">"./results"</span>,
    num_train_epochs=<span class="number">3</span>,
    per_device_train_batch_size=<span class="number">16</span>,
    per_device_eval_batch_size=<span class="number">64</span>,
    learning_rate=<span class="number">2e-5</span>,
    weight_decay=<span class="number">0.01</span>,
    eval_strategy=<span class="string">"epoch"</span>,
    save_strategy=<span class="string">"epoch"</span>,
    load_best_model_at_end=<span class="keyword">True</span>,
    fp16=<span class="keyword">True</span>,            <span class="comment"># Mixed precision</span>
    gradient_accumulation_steps=<span class="number">4</span>,
    logging_steps=<span class="number">100</span>,
    report_to=<span class="string">"wandb"</span>,   <span class="comment"># Log to Weights & Biases</span>
)

trainer = Trainer(model=model, args=args, train_dataset=tokenized[<span class="string">"train"</span>], eval_dataset=tokenized[<span class="string">"test"</span>])
trainer.train()
trainer.push_to_hub()  <span class="comment"># Push trained model directly</span></div>

                <h3>6. Gradio — Build a Demo in 5 Lines</h3>
                <div class="code-block"><span class="keyword">import</span> gradio <span class="keyword">as</span> gr
<span class="keyword">from</span> transformers <span class="keyword">import</span> pipeline

pipe = pipeline(<span class="string">"sentiment-analysis"</span>)

<span class="keyword">def</span> <span class="function">analyze</span>(text):
    result = pipe(text)[<span class="number">0</span>]
    <span class="keyword">return</span> <span class="string">f"{result['label']} ({result['score']:.2%})"</span>

gr.Interface(fn=analyze, inputs=<span class="string">"text"</span>, outputs=<span class="string">"text"</span>,
    title=<span class="string">"Sentiment Analyzer"</span>).launch()
<span class="comment"># Runs at http://localhost:7860 — deploy to HF Spaces for free!</span></div>

                <h3>7. Hub API — Programmatic Access</h3>
                <div class="code-block"><span class="keyword">from</span> huggingface_hub <span class="keyword">import</span> HfApi, hf_hub_download, login

<span class="comment"># Login</span>
login(token=<span class="string">"hf_your_token"</span>)  <span class="comment"># or: huggingface-cli login</span>

api = HfApi()

<span class="comment"># List models by task</span>
models = api.list_models(filter=<span class="string">"text-generation"</span>, sort=<span class="string">"downloads"</span>, limit=<span class="number">5</span>)
<span class="keyword">for</span> m <span class="keyword">in</span> models:
    <span class="function">print</span>(<span class="string">f"{m.id}: {m.downloads} downloads"</span>)

<span class="comment"># Download specific file</span>
path = hf_hub_download(repo_id=<span class="string">"meta-llama/Llama-3.1-8B"</span>, filename=<span class="string">"config.json"</span>)

<span class="comment"># Push model to Hub</span>
model.push_to_hub(<span class="string">"your-username/my-model"</span>)
tokenizer.push_to_hub(<span class="string">"your-username/my-model"</span>)

<span class="comment"># Create a new Space</span>
api.create_repo(<span class="string">"your-username/my-demo"</span>, repo_type=<span class="string">"space"</span>, space_sdk=<span class="string">"gradio"</span>)</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Hugging Face — In-Depth Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What's the difference between <code>from_pretrained</code> and <code>pipeline</code>?</strong><p><strong>Answer:</strong> <code>pipeline()</code> is a high-level convenience wrapper — it auto-detects the task, loads both model + tokenizer, handles tokenization/decoding, and returns human-readable output. <code>from_pretrained()</code> gives raw access to model weights for: custom inference loops, fine-tuning, extracting embeddings, modifying the model architecture, or anything beyond standard inference. Rule: prototyping → pipeline, production/training → from_pretrained.</p></div>
                <div class="interview-box"><strong>Q2: What is <code>device_map="auto"</code> and how does model sharding work?</strong><p><strong>Answer:</strong> It uses the <code>accelerate</code> library to automatically distribute model layers across available hardware. The algorithm: (1) Measure available memory on each GPU, CPU, and disk; (2) Place layers sequentially, filling GPU first, spilling to CPU, then disk. For a 70B model on two 24GB GPUs: layers 0-40 on GPU 0, layers 41-80 on GPU 1. CPU/disk offloading adds latency but enables running models that don't fit in GPU memory at all. Use <code>max_memory</code> param to control allocation.</p></div>
                <div class="interview-box"><strong>Q3: Why use HF Datasets over pandas, and how does Apache Arrow help?</strong><p><strong>Answer:</strong> Datasets uses <strong>Apache Arrow</strong> — a columnar, memory-mapped format. Key advantages: (1) <strong>Memory mapping:</strong> A 100GB dataset uses near-zero RAM — data stays on disk but accessed at near-RAM speed via OS page cache. (2) <strong>Zero-copy:</strong> Slicing doesn't duplicate data. (3) <strong>Streaming:</strong> Process datasets larger than disk with <code>streaming=True</code>. (4) <strong>Parallel map:</strong> <code>num_proc=N</code> for multi-core preprocessing. (5) <strong>Caching:</strong> Processed results are automatically cached to disk. Pandas loads everything into RAM — impossible for large-scale ML datasets.</p></div>
                <div class="interview-box"><strong>Q4: What is a chat template and why does it matter?</strong><p><strong>Answer:</strong> Each instruction-tuned model is trained with a specific format for system/user/assistant messages. Llama uses <code>&lt;|begin_of_text|&gt;&lt;|start_header_id|&gt;system&lt;|end_header_id|&gt;</code>, while ChatML uses <code>&lt;|im_start|&gt;system</code>. If you format input incorrectly, the model behaves like a base model (no instruction following). <code>tokenizer.apply_chat_template()</code> auto-formats messages correctly for any model. This is the #1 mistake beginners make — using raw text instead of the chat template.</p></div>
                <div class="interview-box"><strong>Q5: How do you handle gated models (Llama, Gemma) in production?</strong><p><strong>Answer:</strong> (1) Accept the model license on the Hub model page. (2) Create a read token at hf.co/settings/tokens. (3) For local: <code>huggingface-cli login</code>. (4) In CI/CD: set <code>HF_TOKEN</code> environment variable. (5) In code: pass <code>token="hf_xxx"</code> to <code>from_pretrained()</code>. For Docker: bake the token as a secret, never in the image. For Kubernetes: use a Secret mounted as an env var. The token is only needed for download — once cached locally, no token is needed for inference.</p></div>
                <div class="interview-box"><strong>Q6: What is safetensors and why replace pickle?</strong><p><strong>Answer:</strong> Traditional PyTorch models use Python's <code>pickle</code> format, which can execute arbitrary code during loading — a <strong>security vulnerability</strong>. A malicious model file could run code on your machine when loaded. <code>safetensors</code> is a safe, fast tensor format that: (1) Cannot execute code (pure data), (2) Supports zero-copy loading (memory-mapped), (3) Is 2-5x faster to load than pickle, (4) Supports lazy loading (load only specific tensors). It's now the default format on HF Hub.</p></div>
            </div>`
    },
    'finetuning': {
        concepts: `
            <div class="section">
                <h2>Fine-Tuning & PEFT — Adapting LLMs Efficiently</h2>
                <div class="info-box">
                    <div class="box-title">⚡ Why Not Full Fine-Tuning?</div>
                    <div class="box-content">A 7B model has 7 billion parameters. Full fine-tuning requires storing the model weights, gradients, optimizer states (Adam keeps 2 momentum terms per param), and activations — roughly <strong>4x the model size in VRAM</strong>. A 7B model needs ~112GB GPU RAM for full fine-tuning. PEFT methods reduce this to &lt;16GB.</div>
                </div>
                <h3>LoRA — Low-Rank Adaptation</h3>
                <p>Instead of modifying the full weight matrix W (d × k), LoRA trains two small matrices: <strong>A (d × r) and B (r × k)</strong> where rank r &lt;&lt; min(d,k). The adapted weight is W + αBA. During inference, BA is merged into W — zero latency overhead. Only 0.1-1% of parameters are trained.</p>
                <table>
                    <tr><th>Method</th><th>Trainable Params</th><th>GPU Needed (7B)</th><th>Quality</th></tr>
                    <tr><td>Full Fine-Tuning</td><td>100%</td><td>~80GB</td><td>Best</td></tr>
                    <tr><td>LoRA (r=16)</td><td>~0.5%</td><td>~16GB</td><td>Very Good</td></tr>
                    <tr><td>QLoRA (4-bit + LoRA)</td><td>~0.5%</td><td>~8GB</td><td>Good</td></tr>
                    <tr><td>Prompt Tuning</td><td>&lt;0.01%</td><td>~6GB</td><td>Task specific</td></tr>
                </table>
                <h3>QLoRA — The Game Changer</h3>
                <p>QLoRA (Dettmers et al., 2023) combines: (1) <strong>4-bit NF4 quantization</strong> of the base model, (2) <strong>double quantization</strong> to compress quantization constants, (3) <strong>paged optimizers</strong> to handle gradient spikes. Fine-tune a 65B model on a single 48GB GPU — impossible before QLoRA.</p>
                 <h3>7 LLM Fine-tuning Techniques</h3>
                <table>
                    <tr><th>Technique</th><th>How It Works</th><th>Use Case</th></tr>
                    <tr><td><strong>1. SFT</strong></td><td>Supervised Fine-Tuning on (Q, A) pairs</td><td>Instruction following</td></tr>
                    <tr><td><strong>2. RLHF</strong></td><td>Reward model + PPO optimization</td><td>Human value alignment</td></tr>
                    <tr><td><strong>3. DPO</strong></td><td>Directly optimize from preferences</td><td>Popular, stable alternative to RLHF</td></tr>
                    <tr><td><strong>4. LoRA / QLoRA</strong></td><td>Low-rank adaptation</td><td>Efficient training on consumer GPUs</td></tr>
                    <tr><td><strong>5. RFT</strong></td><td>Rejection Fine-Tuning</td><td>Self-improving by filtering best outputs</td></tr>
                    <tr><td><strong>6. GRPO</strong></td><td>Group Relative Policy Optimization</td><td>The core of DeepSeek-R1; trains reasoning models without a separate value model</td></tr>
                    <tr><td><strong>7. IFT</strong></td><td>Instruction Fine-Tuning</td><td>Bridge gap between base model and agent</td></tr>
                </table>

                <div class="callout insight">
                    <div class="callout-title">📖 Book Insight: SFT vs RFT</div>
                    <p><strong>SFT (Supervised Fine-Tuning)</strong> trains on fixed human-written data. <strong>RFT (Rejection Fine-Tuning)</strong> uses the model itself to generate multiple responses, filters them for correctness (e.g., using a code compiler or calculator), and then fine-tunes only on those verified correct samples. This allows models to self-improve beyond human capabilities in certain domains.</p>
                </div>

                <h3>Building a Reasoning LLM (GRPO)</h3>
                <p>DeepSeek's <strong>GRPO</strong> (Group Relative Policy Optimization) is the new frontier. Instead of a separate reward model, you generate a group of outputs and rank them relative to each other (e.g., based on mathematical correctness or formatting). This forces the model to learn long chains of thought and "thinking" behavior.</p>

                <h3>When to Fine-Tune vs RAG</h3>
                <div class="comparison">
                    <div class="comparison-bad"><strong>Use RAG when:</strong> Knowledge changes frequently, facts need to be cited, domain data is large/dynamic. Lower cost, easier updates.</div>
                    <div class="comparison-good"><strong>Use Fine-tuning when:</strong> Teaching a specific <em>style or format</em>, specialized vocabulary, or task-specific instructions that are hard to prompt-engineer.</div>
                </div>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Fine-Tuning Code Examples</h2>
                <h3>QLoRA Fine-Tuning with TRL + PEFT</h3>
                <div class="code-block"><span class="keyword">from</span> transformers <span class="keyword">import</span> AutoModelForCausalLM, BitsAndBytesConfig
<span class="keyword">from</span> peft <span class="keyword">import</span> LoraConfig, get_peft_model
<span class="keyword">from</span> trl <span class="keyword">import</span> SFTTrainer, SFTConfig
<span class="keyword">from</span> datasets <span class="keyword">import</span> load_dataset

<span class="comment"># 1. Load base model in 4-bit</span>
bnb = BitsAndBytesConfig(load_in_4bit=<span class="keyword">True</span>, bnb_4bit_quant_type=<span class="string">"nf4"</span>)
model = AutoModelForCausalLM.from_pretrained(<span class="string">"meta-llama/Llama-3.1-8B"</span>, quantization_config=bnb)

<span class="comment"># 2. Configure LoRA</span>
lora_config = LoraConfig(
    r=<span class="number">16</span>,           <span class="comment"># Rank — higher = more capacity, more memory</span>
    lora_alpha=<span class="number">32</span>,  <span class="comment"># Scaling factor (alpha/r = effective learning rate)</span>
    target_modules=[<span class="string">"q_proj"</span>, <span class="string">"v_proj"</span>, <span class="string">"k_proj"</span>, <span class="string">"o_proj"</span>],
    lora_dropout=<span class="number">0.05</span>,
    bias=<span class="string">"none"</span>,
    task_type=<span class="string">"CAUSAL_LM"</span>
)

<span class="comment"># 3. Get PEFT model</span>
peft_model = get_peft_model(model, lora_config)
peft_model.print_trainable_parameters()  <span class="comment"># ~0.5% trainable</span>

<span class="comment"># 4. Train with TRL's SFTTrainer</span>
dataset = load_dataset(<span class="string">"tatsu-lab/alpaca"</span>, split=<span class="string">"train"</span>)
trainer = SFTTrainer(
    model=peft_model,
    train_dataset=dataset,
    args=SFTConfig(output_dir=<span class="string">"./llama-finetuned"</span>, num_train_epochs=<span class="number">2</span>)
)
trainer.train()</div>

                <h3>Implementing LoRA From Scratch (Conceptual)</h3>
                <div class="code-block"><span class="keyword">import</span> torch
<span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn

<span class="keyword">class</span> <span class="function">LoRALayer</span>(nn.Module):
    <span class="keyword">def</span> <span class="function">__init__</span>(self, W, rank=<span class="number">8</span>, alpha=<span class="number">16</span>):
        super().__init__()
        self.W = W  <span class="comment"># Original frozen weights (d x k)</span>
        d, k = W.shape
        <span class="comment"># Low-rank matrices A and B</span>
        self.A = nn.Parameter(torch.randn(d, rank) / (rank**<span class="number">0.5</span>))
        self.B = nn.Parameter(torch.zeros(rank, k))
        self.scaling = alpha / rank

    <span class="keyword">def</span> <span class="function">forward</span>(self, x):
        <span class="comment"># Output = xW + (xAB) * scaling</span>
        base_out = x @ self.W
        lora_out = (x @ self.A @ self.B) * self.scaling
        <span class="keyword">return</span> base_out + lora_out

<span class="comment"># DeepSeek-R1 style GRPO loop (simplified)</span>
<span class="keyword">def</span> <span class="function">grpo_step</span>(model, query, num_samples=<span class="number">8</span>):
    outputs = model.generate(query, n=num_samples)
    rewards = [compute_reward(o) <span class="keyword">for</span> o <span class="keyword">in</span> outputs]
    <span class="comment"># Normalize rewards within the group</span>
    adv = [(r - mean(rewards)) / std(rewards) <span class="keyword">for</span> r <span class="keyword">in</span> rewards]
    loss = compute_ppo_loss(outputs, adv)
    loss.backward()</div>
                <h3>Merge LoRA Weights for Deployment</h3>
                <div class="code-block"><span class="keyword">from</span> peft <span class="keyword">import</span> PeftModel

<span class="comment"># Load base + adapter, then merge for zero-latency inference</span>
base = AutoModelForCausalLM.from_pretrained(<span class="string">"meta-llama/Llama-3.1-8B"</span>)
peft = PeftModel.from_pretrained(base, <span class="string">"./llama-finetuned"</span>)
merged = peft.merge_and_unload()  <span class="comment"># BA merged into W, adapter removed</span>
merged.save_pretrained(<span class="string">"./llama-merged"</span>)</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Fine-Tuning Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What does "rank" mean in LoRA and how to choose it?</strong><p><strong>Answer:</strong> Rank r controls the expressiveness of the adaptation. Lower r (4-8): minimal params, fast, good for narrow tasks. Higher r (16-64): more capacity, better for complex tasks. Rule of thumb: start with r=16. If quality is poor, try r=64. If memory is tight, try r=4 with higher alpha. lora_alpha/r acts as the effective learning rate scaling.</p></div>
                <div class="interview-box"><strong>Q2: Which layers should LoRA target?</strong><p><strong>Answer:</strong> Target the attention projection layers: <strong>q_proj, k_proj, v_proj, o_proj</strong>. Optionally add MLP layers (gate_proj, up_proj, down_proj). Targeting more modules increases quality but also memory. Research shows q_proj + v_proj alone gives 80% of the benefit. Use <code>target_modules="all-linear"</code> in recent PEFT versions for maximum coverage.</p></div>
                <div class="interview-box"><strong>Q3: What is catastrophic forgetting and how is LoRA different?</strong><p><strong>Answer:</strong> In full fine-tuning, training on new data overwrites old weights, causing the model to "forget" general capabilities. LoRA <strong>freezes the original weights</strong> — the base model is untouched. Only the low-rank adapter is trained. This means the base knowledge is preserved, and you can merge/unmerge adapters to switch tasks.</p></div>
            </div>`
    }
};


// Modules 5-9 — Expanded Deep Dive
Object.assign(MODULE_CONTENT, {
    'rag': {
        concepts: `
            <div class="section">
                <h2>📚 RAG Pipelines — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ Why RAG?</div>
                    <div class="box-content">LLMs have a knowledge cutoff and hallucinate facts. RAG (Retrieval-Augmented Generation) solves this by fetching <strong>relevant documents at query time</strong> and injecting them into the context. The LLM generates answers grounded in real, up-to-date data rather than parametric memory. RAG is the #1 production-deployed LLM architecture after basic chat.</div>
                </div>

                <h3>1. The RAG Pipeline (End-to-End)</h3>
                <p><strong>Indexing phase (offline):</strong> (1) Load documents (PDF, HTML, Markdown, DB), (2) Chunk into segments (~500 tokens), (3) Embed each chunk with an embedding model, (4) Store vectors + metadata in a vector database.</p>
                <p><strong>Query phase (online):</strong> (1) Embed user query with same model, (2) Retrieve top-k similar chunks via ANN search, (3) Inject chunks into LLM prompt as context, (4) LLM generates a grounded response with citations.</p>

                <h3>2. The Evolution of RAG</h3>
                <div class="comparison">
                    <div class="box-bad"><strong>Stage 1: Naive RAG</strong> (Retreive > Stuff > Gen). Works for simple docs. Fails when query and doc use different words (vocabulary mismatch).</div>
                    <div class="box-good"><strong>Stage 2: Advanced RAG</strong> (Rewrite > Retrieve > Re-rank). Uses HyDE to rewrite queries and a Re-ranker model to sort chunks by actual relevance before feeding LLM.</div>
                </div>
                <div class="callout insight">
                    <div class="callout-title">💡 The "Library Librarian" Analogy</div>
                    <p>Naive RAG is like a librarian who only looks at the <strong>index</strong> of a book. Advanced RAG is a librarian who <strong>skims the chapters</strong> to make sure they actually answer your question before handing you the book.</p>
                </div>
                <div class="callout tip">
                    <div class="callout-title">💡 Chunk Size Sweet Spot</div>
                    <p>256-512 tokens for OpenAI embeddings, 128-256 for smaller models. Use 50-100 token overlap. Test with your actual queries — measure retrieval recall, not just generation quality.</p>
                </div>

                <h3>3. Embedding Models</h3>
                <table>
                    <tr><th>Model</th><th>Dims</th><th>Max Tokens</th><th>Quality</th><th>Cost</th></tr>
                    <tr><td>OpenAI text-embedding-3-large</td><td>3072</td><td>8191</td><td>Top tier</td><td>$0.13/1M</td></tr>
                    <tr><td>OpenAI text-embedding-3-small</td><td>1536</td><td>8191</td><td>Very good</td><td>$0.02/1M</td></tr>
                    <tr><td>Cohere embed-v3</td><td>1024</td><td>512</td><td>Top tier</td><td>$0.10/1M</td></tr>
                    <tr><td>BGE-large-en-v1.5</td><td>1024</td><td>512</td><td>Great (open)</td><td>Free</td></tr>
                    <tr><td>all-MiniLM-L6-v2</td><td>384</td><td>256</td><td>Good</td><td>Free</td></tr>
                </table>

                <h3>4. 8 Modern RAG Architectures</h3>
                <table>
                    <tr><th>Architecture</th><th>How It Works</th><th>Advantage</th></tr>
                    <tr><td><strong>Naive RAG</strong></td><td>Retrieve top-K, generate</td><td>Simplest, 70% accuracy</td></tr>
                    <tr><td><strong>Advanced RAG</strong></td><td>Pre-retrieval (HyDE) + Post (Re-ranking)</td><td>Better precision, 85% accuracy</td></tr>
                    <tr><td><strong>Self-RAG</strong></td><td>Agent decides <em>if</em> retrieval is needed</td><td>Reduces token cost / hallucinations</td></tr>
                    <tr><td><strong>REFRAG</strong></td><td>Retrieve first, then Refine query and Retrieve again</td><td>Critical for multi-hop reasoning</td></tr>
                    <tr><td><strong>CAG</strong> (Context Augmented Gen)</td><td>Pre-process docs into model KV cache</td><td>Ultra-low latency for fixed datasets</td></tr>
                    <tr><td><strong>HyDE</strong></td><td>Embed hypothetical answer, not query</td><td>Handles vocabulary mismatch</td></tr>
                    <tr><td><strong>Agentic RAG</strong></td><td>Agent uses search tool loop as needed</td><td>Most flexible but slowest</td></tr>
                    <tr><td><strong>Knowledge Graph RAG</strong></td><td>Retrieve triple relations (GraphRAG)</td><td>Excellent for complex connections</td></tr>
                </table>

                <h3>5. Traditional RAG vs HyDE</h3>
                <div class="comparison">
                    <div class="box-bad"><strong>Naive:</strong> Embed "How is company X doing?". Vector search searches for fragments of that query.</div>
                    <div class="box-good"><strong>HyDE:</strong> LLM writes a <em>hypothetical</em> investor report for company X. We embed THAT report. Vector search finds similar *actual* reports.</div>
                </div>

                <h3>6. 5 Chunking Strategies for RAG</h3>
                <table>
                    <tr><th>Strategy</th><th>Description</th><th>Best For</th></tr>
                    <tr><td><strong>1. Fixed-size</strong></td><td>Chunking by character/token count</td><td>Simple text, general usage</td></tr>
                    <tr><td><strong>2. Recursive</strong></td><td>Split by paragraph, then sentence</td><td>Most documents (LangChain default)</td></tr>
                    <tr><td><strong>3. Semantic</strong></td><td>Split where embedding distance jumps</td><td>Long books or unified narratives</td></tr>
                    <tr><td><strong>4. Structural</strong></td><td>Split by HTML headings or PDF sections</td><td>Technical docs, manuals</td></tr>
                    <tr><td><strong>5. Agentic</strong></td><td>LLM analyzes text and groups it</td><td>Highest accuracy, highest cost</td></tr>
                </table>

                <h3>7. RAG vs Agentic RAG and AI Memory</h3>
                <p>Standard RAG is a <strong>one-shot</strong> process. <strong>Agentic RAG</strong> allows an agent to decide how to search, what to search, and when to stop. Combined with <strong>AI Memory</strong> (persisting relevant facts across sessions), this creates systems that grow smarter over time.</p>

                <h3>8. Evaluating RAG (RAGAS)</h3>
                <table>
                    <tr><th>Metric</th><th>Measures</th><th>Target</th></tr>
                    <tr><td><strong>Faithfulness</strong></td><td>Are claims supported by context?</td><td>0.9+</td></tr>
                    <tr><td><strong>Answer Relevance</strong></td><td>Does answer address the question?</td><td>0.85+</td></tr>
                    <tr><td><strong>Context Recall</strong></td><td>Did retriever find all needed info?</td><td>0.8+</td></tr>
                    <tr><td><strong>Context Precision</strong></td><td>Is retrieved context relevant?</td><td>0.8+</td></tr>
                </table>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 RAG Pipeline — Code Examples</h2>

                <h3>1. End-to-End RAG with LangChain</h3>
                <div class="code-block"><span class="keyword">from</span> langchain_community.document_loaders <span class="keyword">import</span> PyPDFLoader
<span class="keyword">from</span> langchain.text_splitter <span class="keyword">import</span> RecursiveCharacterTextSplitter
<span class="keyword">from</span> langchain_community.vectorstores <span class="keyword">import</span> FAISS
<span class="keyword">from</span> langchain_openai <span class="keyword">import</span> OpenAIEmbeddings, ChatOpenAI
<span class="keyword">from</span> langchain.chains <span class="keyword">import</span> RetrievalQA

<span class="comment"># 1. Load & split</span>
loader = PyPDFLoader(<span class="string">"your-document.pdf"</span>)
docs = loader.load()
splitter = RecursiveCharacterTextSplitter(chunk_size=<span class="number">500</span>, chunk_overlap=<span class="number">50</span>)
chunks = splitter.split_documents(docs)

<span class="comment"># 2. Embed & index</span>
embeddings = OpenAIEmbeddings(model=<span class="string">"text-embedding-3-small"</span>)
vectorstore = FAISS.from_documents(chunks, embeddings)

<span class="comment"># 3. Query</span>
retriever = vectorstore.as_retriever(search_kwargs={<span class="string">"k"</span>: <span class="number">5</span>})
llm = ChatOpenAI(model=<span class="string">"gpt-4o"</span>)
qa = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
<span class="function">print</span>(qa.run(<span class="string">"What are the main findings?"</span>))</div>

                <h3>2. Hybrid Search (BM25 + Vector)</h3>
                <div class="code-block"><span class="keyword">from</span> langchain.retrievers <span class="keyword">import</span> EnsembleRetriever
<span class="keyword">from</span> langchain_community.retrievers <span class="keyword">import</span> BM25Retriever

bm25 = BM25Retriever.from_documents(chunks, k=<span class="number">10</span>)
vector_ret = vectorstore.as_retriever(search_kwargs={<span class="string">"k"</span>: <span class="number">10</span>})

<span class="comment"># Reciprocal Rank Fusion</span>
hybrid = EnsembleRetriever(
    retrievers=[bm25, vector_ret],
    weights=[<span class="number">0.4</span>, <span class="number">0.6</span>]
)
results = hybrid.invoke(<span class="string">"performance benchmarks"</span>)</div>

                <h3>3. Re-ranking with Cohere</h3>
                <div class="code-block"><span class="keyword">from</span> langchain.retrievers <span class="keyword">import</span> ContextualCompressionRetriever
<span class="keyword">from</span> langchain_cohere <span class="keyword">import</span> CohereRerank

base = vectorstore.as_retriever(search_kwargs={<span class="string">"k"</span>: <span class="number">20</span>})
reranker = CohereRerank(top_n=<span class="number">5</span>)
retriever = ContextualCompressionRetriever(
    base_compressor=reranker, base_retriever=base
)</div>

                <h3>4. RAGAS Evaluation</h3>
                <div class="code-block"><span class="keyword">from</span> ragas <span class="keyword">import</span> evaluate
<span class="keyword">from</span> ragas.metrics <span class="keyword">import</span> faithfulness, answer_relevancy, context_recall
<span class="keyword">from</span> datasets <span class="keyword">import</span> Dataset

eval_data = {
    <span class="string">"question"</span>: [<span class="string">"What is RAG?"</span>],
    <span class="string">"answer"</span>: [<span class="string">"RAG combines retrieval with generation."</span>],
    <span class="string">"contexts"</span>: [[<span class="string">"RAG stands for Retrieval-Augmented Generation..."</span>]],
    <span class="string">"ground_truth"</span>: [<span class="string">"RAG is retrieval-augmented generation."</span>]
}
result = evaluate(Dataset.from_dict(eval_data),
    metrics=[faithfulness, answer_relevancy, context_recall])
<span class="function">print</span>(result.to_pandas())</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 RAG — In-Depth Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What is chunk overlap and why is it critical?</strong><p><strong>Answer:</strong> Overlap ensures sentences spanning chunk boundaries aren't lost. A 50-token overlap on 500-token chunks means each chunk shares context with neighbors. Without it, a key sentence split across chunks is invisible during retrieval. Tradeoff: more overlap = more storage + slightly redundant results.</p></div>
                <div class="interview-box"><strong>Q2: How do you evaluate RAG end-to-end?</strong><p><strong>Answer:</strong> Use <strong>RAGAS</strong>: (1) <strong>Faithfulness</strong> — answer uses only retrieved context? (2) <strong>Answer Relevance</strong> — addresses the question? (3) <strong>Context Recall</strong> — retriever found all needed info? (4) <strong>Context Precision</strong> — retrieved context relevant? Build a golden dataset of (question, ground_truth, expected_contexts). Use LLM-as-judge for scale.</p></div>
                <div class="interview-box"><strong>Q3: When to use HyDE vs query decomposition?</strong><p><strong>Answer:</strong> <strong>HyDE:</strong> Short queries that don't match document vocabulary. LLM generates hypothetical answer, embed that instead. <strong>Query decomposition:</strong> Complex multi-part questions. Break into sub-queries, retrieve for each, combine. Use HyDE for vocabulary mismatch, decomposition for complexity.</p></div>
                <div class="interview-box"><strong>Q4: What's the difference between naive and advanced RAG?</strong><p><strong>Answer:</strong> <strong>Naive:</strong> embed query -> top-k -> stuff in prompt -> generate. Problems: poor recall, irrelevant chunks. <strong>Advanced:</strong> adds query transformation, hybrid search, re-ranking, parent-child chunks, metadata filtering. Advanced RAG achieves 20-40% higher faithfulness in production.</p></div>
                <div class="interview-box"><strong>Q5: How do you handle tables and images in RAG?</strong><p><strong>Answer:</strong> (1) <strong>Extract and caption</strong> — tables to markdown, images captioned via GPT-4o, embed text. (2) <strong>Multi-modal embeddings</strong> — CLIP for images alongside text. (3) <strong>ColPali</strong> — embed entire PDF page screenshots without OCR. Approach 1 is most production-ready; approach 3 is newest.</p></div>
            </div>`
    },
    'vectordb': {
        concepts: `
            <div class="section">
                <h2>🗄️ Vector Databases — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ Why Not PostgreSQL?</div>
                    <div class="box-content">Regular databases find exact matches. Vector databases find <strong>approximate nearest neighbors (ANN)</strong> in high-dimensional space. A brute-force search over 10M 1536-d vectors would take ~2 seconds. HNSW reduces this to ~5 milliseconds at 99% recall.</div>
                </div>

                <h3>1. Distance Metrics</h3>
                <table>
                    <tr><th>Metric</th><th>Best For</th><th>Range</th></tr>
                    <tr><td><strong>Cosine Similarity</strong></td><td>Text embeddings (normalized)</td><td>-1 to 1 (1 = identical)</td></tr>
                    <tr><td><strong>Dot Product</strong></td><td>When magnitude matters</td><td>-inf to inf</td></tr>
                    <tr><td><strong>L2 (Euclidean)</strong></td><td>Image embeddings</td><td>0 to inf (0 = identical)</td></tr>
                </table>
                <p><strong>Critical rule:</strong> Use the same metric your embedding model was trained with. OpenAI = cosine. Wrong metric can drop recall by 20%+.</p>

                <h3>2. ANN Algorithms</h3>
                <table>
                    <tr><th>Algorithm</th><th>How It Works</th><th>Speed</th><th>Memory</th><th>Best For</th></tr>
                    <tr><td><strong>HNSW</strong></td><td>Hierarchical graph navigation</td><td>Very fast</td><td>High</td><td>Low-latency (&lt;10ms)</td></tr>
                    <tr><td><strong>IVF</strong></td><td>Cluster vectors, search nearby</td><td>Fast</td><td>Medium</td><td>Large datasets, GPU</td></tr>
                    <tr><td><strong>PQ</strong></td><td>Compress vectors into codes</td><td>Medium</td><td>Very low</td><td>Billions of vectors</td></tr>
                    <tr><td><strong>ScaNN</strong></td><td>Google's anisotropic hashing</td><td>Very fast</td><td>Medium</td><td>Extreme scale</td></tr>
                    <tr><td><strong>DiskANN</strong></td><td>SSD-based graph search</td><td>Fast</td><td>Very low</td><td>Billion-scale on commodity HW</td></tr>
                </table>

                <h3>3. HNSW — How It Works</h3>
                <p>Multi-layered graph. Top layers are sparse "highways" with long-range connections. Bottom layers are dense with local neighbors. Search starts at top, greedily navigates toward query, drops to lower layers for precision. <strong>Params:</strong> M (connections per node, 16-64), efConstruction (build quality, 100-200), efSearch (query quality, 50-200).</p>

                <h3>4. Database Comparison</h3>
                <table>
                    <tr><th>Database</th><th>Type</th><th>Best Use</th><th>Hosting</th><th>Scale</th></tr>
                    <tr><td><strong>FAISS</strong></td><td>Library</td><td>Research, in-process</td><td>In-process</td><td>Billions (GPU)</td></tr>
                    <tr><td><strong>ChromaDB</strong></td><td>Embedded</td><td>Prototyping</td><td>Local/Cloud</td><td>Millions</td></tr>
                    <tr><td><strong>Pinecone</strong></td><td>Managed SaaS</td><td>Production, zero-ops</td><td>Fully managed</td><td>Billions</td></tr>
                    <tr><td><strong>Weaviate</strong></td><td>Full DB</td><td>Hybrid search</td><td>Cloud/Self-host</td><td>Billions</td></tr>
                    <tr><td><strong>Qdrant</strong></td><td>Full DB</td><td>Filtering + vectors</td><td>Cloud/Self-host</td><td>Billions</td></tr>
                    <tr><td><strong>pgvector</strong></td><td>Extension</td><td>Already using Postgres</td><td>In PostgreSQL</td><td>Millions</td></tr>
                    <tr><td><strong>Milvus</strong></td><td>Distributed</td><td>Enterprise</td><td>Cloud/Self-host</td><td>Billions+</td></tr>
                </table>

                <h3>5. Embedding Model Selection</h3>
                <p>The embedding model matters more than the vector DB. Check <strong>MTEB leaderboard</strong>. Key factors: (1) <strong>Dimension</strong> — 384 (fast) to 3072 (accurate). (2) <strong>Max tokens</strong> — must be >= chunk size. (3) <strong>Multilingual</strong> — require for non-English. (4) <strong>Domain</strong> — fine-tuned models beat general-purpose.</p>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Vector Database — Code Examples</h2>

                <h3>1. ChromaDB — Local Quick Start</h3>
                <div class="code-block"><span class="keyword">import</span> chromadb
<span class="keyword">from</span> chromadb.utils <span class="keyword">import</span> embedding_functions

ef = embedding_functions.OpenAIEmbeddingFunction(model_name=<span class="string">"text-embedding-3-small"</span>)
client = chromadb.PersistentClient(path=<span class="string">"./chroma_db"</span>)
collection = client.get_or_create_collection(<span class="string">"my_docs"</span>, embedding_function=ef)

<span class="comment"># Add documents (auto-embedded)</span>
collection.add(
    documents=[<span class="string">"RAG uses vector similarity"</span>, <span class="string">"HNSW is fast"</span>, <span class="string">"Transformers changed NLP"</span>],
    metadatas=[{<span class="string">"source"</span>: <span class="string">"paper"</span>}, {<span class="string">"source"</span>: <span class="string">"blog"</span>}, {<span class="string">"source"</span>: <span class="string">"paper"</span>}],
    ids=[<span class="string">"d1"</span>, <span class="string">"d2"</span>, <span class="string">"d3"</span>]
)
results = collection.query(query_texts=[<span class="string">"how does retrieval work?"</span>], n_results=<span class="number">2</span>,
    where={<span class="string">"source"</span>: <span class="string">"paper"</span>})
<span class="function">print</span>(results[<span class="string">'documents'</span>])</div>

                <h3>2. FAISS — High-Performance Local</h3>
                <div class="code-block"><span class="keyword">import</span> faiss
<span class="keyword">import</span> numpy <span class="keyword">as</span> np

d = <span class="number">1536</span>  <span class="comment"># OpenAI embedding dimension</span>
index = faiss.IndexHNSWFlat(d, <span class="number">32</span>)  <span class="comment"># M=32</span>
index.hnsw.efConstruction = <span class="number">200</span>
index.hnsw.efSearch = <span class="number">100</span>

vectors = np.random.randn(<span class="number">10000</span>, d).astype(<span class="string">'float32'</span>)
index.add(vectors)

query = np.random.randn(<span class="number">1</span>, d).astype(<span class="string">'float32'</span>)
distances, indices = index.search(query, k=<span class="number">5</span>)
<span class="function">print</span>(<span class="string">f"Top 5 neighbors: {indices[0]}"</span>)

faiss.write_index(index, <span class="string">"my_index.faiss"</span>)</div>

                <h3>3. Pinecone — Production</h3>
                <div class="code-block"><span class="keyword">from</span> pinecone <span class="keyword">import</span> Pinecone, ServerlessSpec

pc = Pinecone(api_key=<span class="string">"your-key"</span>)
pc.create_index(<span class="string">"rag-index"</span>, dimension=<span class="number">1536</span>, metric=<span class="string">"cosine"</span>,
    spec=ServerlessSpec(cloud=<span class="string">"aws"</span>, region=<span class="string">"us-east-1"</span>))
index = pc.Index(<span class="string">"rag-index"</span>)

index.upsert(vectors=[
    (<span class="string">"doc-1"</span>, embedding_1, {<span class="string">"source"</span>: <span class="string">"policy.pdf"</span>, <span class="string">"page"</span>: <span class="number">3</span>}),
])
res = index.query(vector=query_emb, top_k=<span class="number">10</span>,
    filter={<span class="string">"source"</span>: {<span class="string">"$eq"</span>: <span class="string">"policy.pdf"</span>}},
    include_metadata=<span class="keyword">True</span>)</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Vector DB — In-Depth Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What is ANN and why not exact search?</strong><p><strong>Answer:</strong> ANN trades &lt;1% recall for massive speedups. Exact NN is O(n*d) — 10M vectors at 1536 dims = 15 billion ops per query. HNSW achieves ~99% recall at 100x+ speedup by navigating a graph, visiting only ~log(n) nodes.</p></div>
                <div class="interview-box"><strong>Q2: What similarity metric should you use?</strong><p><strong>Answer:</strong> <strong>Cosine</strong> for text (angle-based, normalized). <strong>Dot product</strong> when magnitude matters. <strong>L2</strong> for image/audio. Always match the metric your embedding model was trained with — wrong metric drops recall 20%+.</p></div>
                <div class="interview-box"><strong>Q3: How to handle metadata filtering efficiently?</strong><p><strong>Answer:</strong> (1) <strong>Pre-filter</strong>: metadata first, ANN on subset — accurate but slow. (2) <strong>Post-filter</strong>: ANN first, filter after — fast but may return &lt;k. (3) <strong>Filtered ANN</strong> (Pinecone, Qdrant): filtering during graph traversal — best of both. Always prefer filtered ANN in production.</p></div>
                <div class="interview-box"><strong>Q4: FAISS vs ChromaDB vs Pinecone — how to choose?</strong><p><strong>Answer:</strong> <strong>FAISS</strong>: Research, benchmarks, in-process — no persistence or filtering. <strong>ChromaDB</strong>: Prototyping &lt;1M vectors — good DX, local-first. <strong>Pinecone</strong>: Production, zero-ops, auto-scales, filtered ANN — 10x more expensive. Use FAISS to validate, Chroma to prototype, Pinecone/Qdrant to ship.</p></div>
            </div>`
    },
    'agents': {
        concepts: `
            <div class="section">
                <h2>🤖 AI Agents — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ What Makes an Agent?</div>
                    <div class="box-content">An agent is an LLM + a <strong>reasoning loop</strong> + <strong>tools</strong>. It doesn't just respond — it plans, calls tools, observes results, and iterates. The agent paradigm turns LLMs from answer machines into action machines.</div>
                </div>

                <h3>1. ReAct: The Brain's Operating System</h3>
                <p>ReAct (Yao 2022) is the fundamental "Thinking Loop". It forces the LLM to output its internal state before action: <strong>Thought</strong> (Reasoning) > <strong>Action</strong> (Tool Call) > <strong>Observation</strong> (Result). This prevents "reflexive" hallucinations where the model answers before it has any data.</p>
                
                <div class="info-box">
                    <div class="box-title">🔌 MCP: The USB Port for AI</div>
                    <div class="box-content">
                        <strong>Model Context Protocol (MCP)</strong> is the industry's attempt to standardize how agents talk to tools. Instead of rewriting tool-calling code for every model, MCP creates a universal "plug-and-play" interface where any model can use any database, API, or local file system seamlessly.
                    </div>
                </div>

                <h3>2. Agent Architectures & Patterns</h3>
                <table>
                    <tr><th>Architecture</th><th>How It Works</th><th>Best For</th></tr>
                    <tr><td><strong>ReAct Loop</strong></td><td>Fixed think-act-observe cycle</td><td>Simple tool-using tasks</td></tr>
                    <tr><td><strong>Plan-and-Execute</strong></td><td>Full plan first, then execute steps</td><td>Multi-step structured tasks</td></tr>
                    <tr><td><strong>State Machine (LangGraph)</strong></td><td>Directed graph with conditional edges</td><td>Complex workflows, branching</td></tr>
                    <tr><td><strong>Reflection</strong></td><td>Agent evaluates own output, retries</td><td>Quality-critical tasks</td></tr>
                    <tr><td><strong>Self-Correction</strong></td><td>Agent detects syntax/logic errors via tools</td><td>Code generation agents</td></tr>
                </table>

                <h3>3. Advanced Prompting for Agents</h3>
                <ul>
                    <li><strong>JSON Prompting:</strong> Forcing the model to output *only* valid JSON. Essential for reliable tool calling and downstream processing. Strategy: Provide a schema and a "Stop Sequence" at "}".</li>
                    <li><strong>Verbalized Sampling:</strong> Forcing the agent to "think out loud" before choosing a tool. Similar to Chain of Thought but explicitly for tool selection.</li>
                    <li><strong>Few-shot Tooling:</strong> Providing 2-3 examples of correct tool usage in the prompt block. 10x more effective than instructions alone.</li>
                </ul>

                <div class="callout insight">
                    <div class="callout-title">📖 Book Insight: 30 Must-Know Agentic Terms</div>
                    <p>Key terms: <strong>Handoff</strong> (passing task to sub-agent), <strong>Orchestrator</strong> (supervisor agent), <strong>Part</strong> (typed data in A2A), <strong>Interrupt</strong> (Human-in-the-loop wait), <strong>Grounding</strong> (connecting to RAG/Tools), <strong>Hallucination guardrails</strong> (output filtering).</p>
                </div>

                <h3>3. Framework Comparison</h3>
                <table>
                    <tr><th>Framework</th><th>Paradigm</th><th>Strengths</th><th>Best For</th></tr>
                    <tr><td><strong>LangGraph</strong></td><td>Stateful graph</td><td>Persistence, human-in-loop, cycles</td><td>Production agents</td></tr>
                    <tr><td><strong>CrewAI</strong></td><td>Role-based multi-agent</td><td>Easy setup, role/goal/backstory</td><td>Business workflows</td></tr>
                    <tr><td><strong>AutoGen</strong></td><td>Conversational multi-agent</td><td>Code execution, group chat</td><td>Research automation</td></tr>
                    <tr><td><strong>Smolagents (HF)</strong></td><td>Code-based tool calling</td><td>Simple, open-source</td><td>Lightweight agents</td></tr>
                    <tr><td><strong>OpenAI Assistants</strong></td><td>Hosted agent</td><td>Zero setup, code interpreter</td><td>Quick prototypes</td></tr>
                </table>

                <h3>4. Agent Memory</h3>
                <table>
                    <tr><th>Type</th><th>Purpose</th><th>Implementation</th></tr>
                    <tr><td><strong>Short-term</strong></td><td>Current conversation</td><td>Message history in prompt</td></tr>
                    <tr><td><strong>Long-term</strong></td><td>Past conversations, facts</td><td>Vector DB + retrieval</td></tr>
                    <tr><td><strong>Working memory</strong></td><td>Intermediate state</td><td>LangGraph State object</td></tr>
                    <tr><td><strong>Episodic</strong></td><td>Past task outcomes</td><td>Structured DB of (task, result)</td></tr>
                </table>

                <h3>5. Tool Design Principles</h3>
                <p>(1) <strong>Single responsibility</strong> — one tool, one job. (2) <strong>Idempotent</strong> — safe to retry. (3) <strong>Fast</strong> — slow tools stall the loop. (4) <strong>Rich descriptions</strong> — the LLM decides based on the description string.</p>

                <h3>6. Agent Safety</h3>
                <p>Agents take <strong>real actions</strong>. Safety measures: (1) Human-in-the-loop for destructive actions. (2) Sandboxing code execution. (3) Permission models. (4) Max iterations. (5) Cost/budget limits.</p>

                <h3>7. The LangChain Ecosystem</h3>
                <p>LangChain has evolved from a simple wrapper into a comprehensive suite for production AI:</p>
                <table>
                    <tr><th>Component</th><th>Purpose</th><th>When to Use</th></tr>
                    <tr><td><strong>LangChain Core</strong></td><td>Standard interface for LLMs, prompts, tools, and retrievers</td><td>Building the basic components of your app</td></tr>
                    <tr><td><strong>LangGraph</strong></td><td>Orchestration framework for stateful, multi-actor applications</td><td>Building complex, cyclic agents and workflows</td></tr>
                    <tr><td><strong>LangSmith</strong></td><td>Observability, tracing, and evaluation platform</td><td>Debugging, testing, and monitoring in production</td></tr>
                    <tr><td><strong>LangServe</strong></td><td>REST API deployment using FastAPI</td><td>Serving chains/agents as production endpoints</td></tr>
                </table>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 AI Agents — Code Examples</h2>

                <h3>1. LangGraph ReAct Agent</h3>
                <div class="code-block"><span class="keyword">from</span> langgraph.prebuilt <span class="keyword">import</span> create_react_agent
<span class="keyword">from</span> langchain_openai <span class="keyword">import</span> ChatOpenAI
<span class="keyword">from</span> langchain_core.tools <span class="keyword">import</span> tool

<span class="preprocessor">@tool</span>
<span class="keyword">def</span> <span class="function">search_web</span>(query: str) -> str:
    <span class="string">"""Search the web for current information."""</span>
    <span class="keyword">return</span> <span class="string">f"Results for: {query}"</span>

<span class="preprocessor">@tool</span>
<span class="keyword">def</span> <span class="function">calculate</span>(expression: str) -> str:
    <span class="string">"""Evaluate a math expression safely."""</span>
    <span class="keyword">return</span> <span class="function">str</span>(<span class="function">eval</span>(expression))

llm = ChatOpenAI(model=<span class="string">"gpt-4o"</span>)
agent = create_react_agent(llm, tools=[search_web, calculate])

result = agent.invoke({
    <span class="string">"messages"</span>: [{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"What is 137 * 42?"</span>}]
})
<span class="function">print</span>(result[<span class="string">"messages"</span>][-<span class="number">1</span>].content)</div>

                <h3>2. LangGraph Custom State Machine</h3>
                <div class="code-block"><span class="keyword">from</span> langgraph.graph <span class="keyword">import</span> StateGraph, START, END
<span class="keyword">from</span> typing <span class="keyword">import</span> TypedDict, Annotated
<span class="keyword">import</span> operator

<span class="keyword">class</span> <span class="function">AgentState</span>(TypedDict):
    messages: Annotated[list, operator.add]
    plan: str
    step: int

<span class="keyword">def</span> <span class="function">planner</span>(state):
    <span class="keyword">return</span> {<span class="string">"plan"</span>: <span class="string">"Step 1: search, Step 2: analyze"</span>}

<span class="keyword">def</span> <span class="function">executor</span>(state):
    <span class="keyword">return</span> {<span class="string">"step"</span>: state[<span class="string">"step"</span>] + <span class="number">1</span>}

<span class="keyword">def</span> <span class="function">should_continue</span>(state):
    <span class="keyword">return</span> <span class="string">"executor"</span> <span class="keyword">if</span> state[<span class="string">"step"</span>] < <span class="number">3</span> <span class="keyword">else</span> END

graph = StateGraph(AgentState)
graph.add_node(<span class="string">"planner"</span>, planner)
graph.add_node(<span class="string">"executor"</span>, executor)
graph.add_edge(START, <span class="string">"planner"</span>)
graph.add_edge(<span class="string">"planner"</span>, <span class="string">"executor"</span>)
graph.add_conditional_edges(<span class="string">"executor"</span>, should_continue)
app = graph.compile()</div>

                <h3>3. CrewAI — Role-Based Agents</h3>
                <div class="code-block"><span class="keyword">from</span> crewai <span class="keyword">import</span> Agent, Task, Crew, Process

researcher = Agent(
    role=<span class="string">"Research Analyst"</span>,
    goal=<span class="string">"Find accurate information on any topic"</span>,
    backstory=<span class="string">"Expert researcher with 10 years experience"</span>,
    tools=[search_tool], llm=<span class="string">"gpt-4o"</span>
)

writer = Agent(
    role=<span class="string">"Technical Writer"</span>,
    goal=<span class="string">"Transform research into clear reports"</span>,
    backstory=<span class="string">"Professional writer specializing in AI"</span>,
    llm=<span class="string">"gpt-4o"</span>
)

research_task = Task(description=<span class="string">"Research LLM agents"</span>, agent=researcher)
write_task = Task(description=<span class="string">"Write a 500-word report"</span>, agent=writer,
    context=[research_task])

crew = Crew(agents=[researcher, writer],
    tasks=[research_task, write_task],
    process=Process.sequential)
result = crew.kickoff()</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 AI Agents — In-Depth Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What are the failure modes of agents?</strong><p><strong>Answer:</strong> (1) <strong>Infinite loops</strong> — stuck in reasoning cycle. Fix: max_iterations + loop detection. (2) <strong>Tool hallucination</strong> — calls nonexistent tools. Fix: strict function calling schemas. (3) <strong>Context overflow</strong> — long loops fill context. Fix: summarize observations. (4) <strong>Cascading errors</strong> — bad step corrupts downstream. Fix: validation at each step. (5) <strong>Cost explosion</strong> — long GPT-4o chains cost $1+/task. Fix: budget limits.</p></div>
                <div class="interview-box"><strong>Q2: LangChain vs LangGraph?</strong><p><strong>Answer:</strong> LangChain AgentExecutor is a <strong>fixed linear loop</strong>. LangGraph models workflows as a <strong>directed graph with persistent state</strong> — branching, cycles, human-in-the-loop, session persistence. LangGraph is the production evolution; AgentExecutor is legacy.</p></div>
                <div class="interview-box"><strong>Q3: How to make agents production-reliable?</strong><p><strong>Answer:</strong> (1) <strong>Structured output</strong> via function calling (not text parsing). (2) <strong>Checkpointing</strong> — resume after failures. (3) <strong>Observability</strong> — trace every LLM call (LangSmith, Langfuse). (4) <strong>Guardrails</strong> on tool inputs/outputs. (5) <strong>Fallbacks</strong> — gracefully degrade after N failures.</p></div>
                <div class="interview-box"><strong>Q4: What is Plan-and-Execute?</strong><p><strong>Answer:</strong> Separates <strong>planning</strong> (create full multi-step plan) from <strong>execution</strong> (carry out each step independently). Planner has full task context; executor focuses on one step with clean context. After each step, planner can revise. Better than ReAct for complex multi-step tasks where the agent loses track of the goal.</p></div>
                <div class="interview-box"><strong>Q5: How do multi-agent systems communicate?</strong><p><strong>Answer:</strong> Two patterns: (1) <strong>Shared state</strong> — agents read/write a common state object (LangGraph). (2) <strong>Message passing</strong> — structured messages between agents (AutoGen). Shared state is simpler; message passing is better for dynamic collaboration. Always pass structured data, not LLM summaries, to prevent error compounding.</p></div>
            </div>`
    },
    'multiagent': {
        concepts: `
            <div class="section">
                <h2>👥 Multi-Agent Systems — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ Why Multiple Agents?</div>
                    <div class="box-content">Single agents degrade with complex tasks — context fills with irrelevant history, and the model loses track. Multi-agent systems decompose tasks into <strong>specialized sub-agents</strong>, each with a focused context window and clear role. Think: a software team vs one person doing everything.</div>
                </div>

                <h3>1. Multi-Agent Architectures</h3>
                <table>
                    <tr><th>Pattern</th><th>Structure</th><th>Best For</th><th>Example</th></tr>
                    <tr><td><strong>Supervisor</strong></td><td>Orchestrator delegates to workers</td><td>Clear task decomposition</td><td>Manager + researcher + writer</td></tr>
                    <tr><td><strong>Peer-to-Peer</strong></td><td>Agents communicate directly</td><td>Collaborative reasoning, debate</td><td>Two reviewers debating quality</td></tr>
                    <tr><td><strong>Hierarchical</strong></td><td>Nested supervisors (teams of teams)</td><td>Enterprise workflows</td><td>Department heads coordinating</td></tr>
                    <tr><td><strong>Swarm</strong></td><td>Dynamic hand-off between agents</td><td>Customer service routing</td><td>OpenAI Swarm pattern</td></tr>
                </table>

                <h3>2. Coordination Patterns</h3>
                <p><strong>Sequential:</strong> Agent A finishes before Agent B starts. Simple but no parallelism. <strong>Parallel:</strong> Independent tasks run simultaneously. <strong>Debate:</strong> Multiple agents propose solutions, a judge picks the best. <strong>Voting:</strong> Run the same task on N agents, majority wins (increases reliability).</p>

                <h3>3. CrewAI — Role-Based Design</h3>
                <p>CrewAI uses <strong>role-based agents</strong> with defined goals and backstories. A "Research Analyst" has different system prompts and tools than a "Report Writer." They collaborate via a crew's shared task queue. This mirrors real organizations.</p>

                <h3>4. OpenAI Swarm Pattern</h3>
                <p>Lightweight multi-agent pattern: each agent is a function with instructions + tools. Agents can <strong>hand off</strong> to each other by returning a different agent. No orchestrator — control flows through hand-offs. Great for customer service routing (sales agent -> support agent -> billing agent).</p>

                <h3>5. Common Pitfalls</h3>
                <p>(1) <strong>Over-engineering</strong> — single ReAct agent often outperforms poorly designed multi-agent. (2) <strong>Context leakage</strong> — agents sharing too much irrelevant context. (3) <strong>Error compounding</strong> — LLM summaries between agents introduce errors. Always pass structured data. (4) <strong>Cost explosion</strong> — N agents = N times the API calls.</p>

                <h3>6. Agent-to-Agent (A2A) Protocols</h3>
                <p>For agents to collaborate at scale, they need standardized communication protocols (A2A). Unlike human-to-agent chat, A2A relies on strict payloads.</p>
                <table>
                    <tr><th>Protocol / Concept</th><th>Description</th></tr>
                    <tr><td><strong>FIPA ACL</strong></td><td>Legacy agent communication language specifying performatives (e.g., REQUEST, INFORM, PROPOSE). Still conceptually relevant for modern A2A state machines.</td></tr>
                    <tr><td><strong>Agent Protocol (AI Engineer Foundation)</strong></td><td>A single open-source OpenAPI specification for agent interaction. Standardizes how to list tasks, execute steps, and upload artifacts.</td></tr>
                    <tr><td><strong>Message Passing Interfaces</strong></td><td>Modern frameworks use structured JSON schema validation for passing state. Agent A emits a JSON payload that perfectly matches the input schema expected by Agent B.</td></tr>
                    <tr><td><strong>Pub/Sub Eventing</strong></td><td>Agents publish events to a message broker (like Kafka or Redis) and other agents subscribe, enabling asynchronous, decoupled A2A swarms.</td></tr>
                </table>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Multi-Agent — Code Examples</h2>

                <h3>1. CrewAI — Role-Based Team</h3>
                <div class="code-block"><span class="keyword">from</span> crewai <span class="keyword">import</span> Agent, Task, Crew, Process

researcher = Agent(
    role=<span class="string">"Research Analyst"</span>,
    goal=<span class="string">"Find accurate, up-to-date information"</span>,
    backstory=<span class="string">"Expert researcher with data analysis experience"</span>,
    tools=[search_tool], llm=<span class="string">"gpt-4o"</span>
)

writer = Agent(
    role=<span class="string">"Technical Writer"</span>,
    goal=<span class="string">"Transform research into clear, engaging reports"</span>,
    backstory=<span class="string">"Professional writer specializing in AI"</span>,
    llm=<span class="string">"gpt-4o"</span>
)

research_task = Task(description=<span class="string">"Research the latest LLM agent developments"</span>,
    agent=researcher, expected_output=<span class="string">"Comprehensive research notes"</span>)
write_task = Task(description=<span class="string">"Write a 500-word report from the research"</span>,
    agent=writer, context=[research_task])

crew = Crew(agents=[researcher, writer],
    tasks=[research_task, write_task],
    process=Process.sequential, verbose=<span class="keyword">True</span>)
result = crew.kickoff()</div>

                <h3>2. LangGraph Supervisor Pattern</h3>
                <div class="code-block"><span class="keyword">from</span> langgraph.graph <span class="keyword">import</span> StateGraph, START, END

<span class="keyword">def</span> <span class="function">supervisor</span>(state):
    <span class="comment"># Route to appropriate worker based on task</span>
    <span class="keyword">if</span> <span class="string">"research"</span> <span class="keyword">in</span> state[<span class="string">"task"</span>]:
        <span class="keyword">return</span> {<span class="string">"next"</span>: <span class="string">"researcher"</span>}
    <span class="keyword">return</span> {<span class="string">"next"</span>: <span class="string">"writer"</span>}

<span class="keyword">def</span> <span class="function">researcher</span>(state):
    <span class="comment"># Research agent with search tools</span>
    <span class="keyword">return</span> {<span class="string">"findings"</span>: <span class="string">"Research results..."</span>}

<span class="keyword">def</span> <span class="function">writer</span>(state):
    <span class="comment"># Writer agent generates final output</span>
    <span class="keyword">return</span> {<span class="string">"report"</span>: <span class="string">"Final report based on findings..."</span>}

graph = StateGraph(dict)
graph.add_node(<span class="string">"supervisor"</span>, supervisor)
graph.add_node(<span class="string">"researcher"</span>, researcher)
graph.add_node(<span class="string">"writer"</span>, writer)
graph.add_edge(START, <span class="string">"supervisor"</span>)
graph.add_conditional_edges(<span class="string">"supervisor"</span>,
    <span class="keyword">lambda</span> s: s[<span class="string">"next"</span>],
    {<span class="string">"researcher"</span>: <span class="string">"researcher"</span>, <span class="string">"writer"</span>: <span class="string">"writer"</span>})
graph.add_edge(<span class="string">"researcher"</span>, <span class="string">"supervisor"</span>)  <span class="comment"># Cycle back</span>
graph.add_edge(<span class="string">"writer"</span>, END)</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Multi-Agent — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: How do agents communicate?</strong><p><strong>Answer:</strong> (1) <strong>Shared state</strong> — agents read/write a common state object (LangGraph). Simple for pipelines. (2) <strong>Message passing</strong> — agents send structured messages (AutoGen). Better for dynamic collaboration. Always pass structured data, not LLM-generated summaries, to prevent error compounding.</p></div>
                <div class="interview-box"><strong>Q2: When should you NOT use multi-agent?</strong><p><strong>Answer:</strong> When a single agent with good tools can handle the task. Multi-agent adds latency (N serial LLM calls), cost (N times more tokens), and complexity (coordination bugs). Start with a single ReAct agent; only split into multi-agent when context window overflow or specialization clearly helps.</p></div>
                <div class="interview-box"><strong>Q3: How to prevent error compounding?</strong><p><strong>Answer:</strong> Each agent in a chain can introduce errors. Mitigations: (1) Pass raw tool outputs between agents, not summaries. (2) Add validation nodes between agents. (3) Use structured output formats. (4) Implement self-correction loops where agents verify their own outputs. (5) Hierarchical review — a supervisor validates worker outputs before proceeding.</p></div>
                <div class="interview-box"><strong>Q4: Supervisor vs Swarm — when to use each?</strong><p><strong>Answer:</strong> <strong>Supervisor:</strong> When you know the workflow structure upfront — orchestrator routes to specialized workers. Good for content pipelines, data processing. <strong>Swarm:</strong> When routing is dynamic and agents decide who handles what. Each agent can hand off to another. Better for customer service, conversational flows. Swarm is simpler but less controllable.</p></div>
            </div>`
    },
    'tools': {
        concepts: `
            <div class="section">
                <h2>🔧 Function Calling & Tool Use — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ Structured Output from LLMs</div>
                    <div class="box-content">Function calling allows LLMs to output <strong>structured JSON</strong> conforming to your schema, instead of free text. You define tool schemas; the model fills them with values. This is how agents reliably call tools without fragile text parsing.</div>
                </div>

                <h3>1. How Function Calling Works</h3>
                <p>(1) You define tool schemas (name, description, parameters as JSON Schema). (2) Send schemas + user message to the LLM. (3) The model outputs a <strong>tool call</strong> with function name + arguments as JSON. (4) You execute the function locally. (5) Send results back to the model for final response.</p>

                <h3>2. Tool Schema Design Principles</h3>
                <p><strong>Description matters most</strong> — the LLM decides which tool to call based entirely on the description. "Search the web" is bad. "Search the web for real-time news. Do NOT use for stable facts like math or history" is good. Include: what it does, when to use it, when NOT to use it, and expected input format.</p>

                <h3>3. Parallel vs Sequential Calling</h3>
                <p><strong>Parallel:</strong> Model outputs multiple independent tool calls in one response. Execute all simultaneously. GPT-4o supports this natively. <strong>Sequential:</strong> One call at a time, each depending on previous results. Parallel is 3-5x faster for independent operations.</p>

                <h3>4. Model Context Protocol (MCP)</h3>
                <p><strong>Model Context Protocol (MCP)</strong> (Anthropic, 2024) is an open-source standard solving the "N-to-M integration problem" between AI assistants and data sources. Instead of writing custom integrations, developers build reusable MCP servers.</p>
                <table>
                    <tr><th>MCP Component</th><th>Role</th><th>Web Analogy</th></tr>
                    <tr><td><strong>MCP Host</strong></td><td>The AI application (e.g., Claude Desktop, Cursor)</td><td>Web Browser</td></tr>
                    <tr><td><strong>MCP Client</strong></td><td>Runs inside the Host, manages server connections</td><td>HTTP Client</td></tr>
                    <tr><td><strong>MCP Server</strong></td><td>Secure, lightweight program giving access to data/tools</td><td>Web Server</td></tr>
                </table>
                <p>Servers expose three primitives to the LLM: <strong>Resources</strong> (read-only data like files or DB schemas giving context), <strong>Tools</strong> (executable functions like "run query"), and <strong>Prompts</strong> (reusable system instruction templates). This separates the reasoning engine from the data layer, allowing immense scalability.</p>

                <h3>5. Provider Comparison</h3>
                <table>
                    <tr><th>Provider</th><th>Feature Name</th><th>Parallel Calls</th><th>Strict Mode</th></tr>
                    <tr><td>OpenAI</td><td>Function Calling</td><td>Yes</td><td>Yes (strict JSON)</td></tr>
                    <tr><td>Anthropic</td><td>Tool Use</td><td>Yes</td><td>Yes</td></tr>
                    <tr><td>Google</td><td>Function Calling</td><td>Yes</td><td>Yes</td></tr>
                    <tr><td>Open models</td><td>Varies (Hermes format)</td><td>Some</td><td>Via constrained decoding</td></tr>
                </table>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Function Calling — Code Examples</h2>

                <h3>1. OpenAI Parallel Function Calling</h3>
                <div class="code-block"><span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI
<span class="keyword">import</span> json

client = OpenAI()

tools = [{
    <span class="string">"type"</span>: <span class="string">"function"</span>,
    <span class="string">"function"</span>: {
        <span class="string">"name"</span>: <span class="string">"get_weather"</span>,
        <span class="string">"description"</span>: <span class="string">"Get current weather for a city. Use for weather queries only."</span>,
        <span class="string">"parameters"</span>: {
            <span class="string">"type"</span>: <span class="string">"object"</span>,
            <span class="string">"properties"</span>: {
                <span class="string">"city"</span>: {<span class="string">"type"</span>: <span class="string">"string"</span>, <span class="string">"description"</span>: <span class="string">"City name"</span>},
                <span class="string">"unit"</span>: {<span class="string">"type"</span>: <span class="string">"string"</span>, <span class="string">"enum"</span>: [<span class="string">"celsius"</span>, <span class="string">"fahrenheit"</span>]}
            },
            <span class="string">"required"</span>: [<span class="string">"city"</span>]
        }
    }
}]

response = client.chat.completions.create(
    model=<span class="string">"gpt-4o"</span>,
    messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Weather in Delhi and London?"</span>}],
    tools=tools, tool_choice=<span class="string">"auto"</span>
)

<span class="comment"># Model returns 2 parallel tool calls!</span>
<span class="keyword">for</span> tc <span class="keyword">in</span> response.choices[<span class="number">0</span>].message.tool_calls:
    args = json.loads(tc.function.arguments)
    <span class="function">print</span>(<span class="string">f"Call: {tc.function.name}({args})"</span>)</div>

                <h3>2. Complete Tool Calling Loop</h3>
                <div class="code-block"><span class="keyword">def</span> <span class="function">get_weather</span>(city, unit=<span class="string">"celsius"</span>):
    <span class="keyword">return</span> <span class="string">f"25C in {city}"</span>

<span class="comment"># Step 1: Get tool calls from model</span>
messages = [{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Weather in Paris?"</span>}]
resp = client.chat.completions.create(
    model=<span class="string">"gpt-4o"</span>, messages=messages, tools=tools)

<span class="comment"># Step 2: Execute tools and send results back</span>
msg = resp.choices[<span class="number">0</span>].message
messages.append(msg)

<span class="keyword">for</span> tc <span class="keyword">in</span> msg.tool_calls:
    args = json.loads(tc.function.arguments)
    result = get_weather(**args)
    messages.append({
        <span class="string">"role"</span>: <span class="string">"tool"</span>,
        <span class="string">"tool_call_id"</span>: tc.id,
        <span class="string">"content"</span>: result
    })

<span class="comment"># Step 3: Get final response</span>
final = client.chat.completions.create(
    model=<span class="string">"gpt-4o"</span>, messages=messages)
<span class="function">print</span>(final.choices[<span class="number">0</span>].message.content)</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Function Calling — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: Function calling vs parsing JSON from text?</strong><p><strong>Answer:</strong> Function calling uses <strong>constrained decoding</strong> — the model can only output valid JSON matching your schema. Text parsing relies on the model following instructions, which fails ~10-20% of the time. Function calling gives near-100% format reliability. It also enables parallel tool calls and strict type validation.</p></div>
                <div class="interview-box"><strong>Q2: What is parallel function calling?</strong><p><strong>Answer:</strong> When the LLM determines multiple tools can be called independently, it outputs ALL tool calls in a single response. You execute them in parallel and return all results. GPT-4o supports this natively. A query like "Weather in 3 cities" produces 3 parallel calls instead of 3 sequential LLM rounds. 3-5x faster.</p></div>
                <div class="interview-box"><strong>Q3: What is MCP and why does it matter?</strong><p><strong>Answer:</strong> Model Context Protocol standardizes how AI assistants connect to external tools. Instead of building custom integrations for each LLM provider, you build one MCP server that works everywhere. It defines: tools (functions), resources (data), and prompts (templates). Similar to how HTTP standardized web communication — MCP standardizes AI-tool communication.</p></div>
                <div class="interview-box"><strong>Q4: How to handle tool calling errors gracefully?</strong><p><strong>Answer:</strong> (1) <strong>Validate inputs</strong> before execution — check types, ranges, permissions. (2) <strong>Catch exceptions</strong> and return descriptive error messages to the model. (3) <strong>Let the model retry</strong> — include error details so it can fix its call. (4) <strong>Max retries</strong> — after 3 failed attempts, return a user-friendly error. Never let tool errors crash the agent loop.</p></div>
            </div>`
    }
});

// Modules 10-13 — Expanded Deep Dive
Object.assign(MODULE_CONTENT, {
    'evaluation': {
        concepts: `
            <div class="section">
                <h2>📊 Evaluation & Benchmarks — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ "You can't improve what you can't measure"</div>
                    <div class="box-content">LLM evaluation is hard because outputs are open-ended. Rule-based metrics (BLEU, ROUGE) fail for generative tasks. The field has shifted toward <strong>LLM-as-a-Judge</strong> — using a powerful LLM to evaluate another LLM's outputs against human-defined criteria.</div>
                </div>

                <h3>1. Evaluation Approaches</h3>
                <table>
                    <tr><th>Type</th><th>Method</th><th>When to Use</th></tr>
                    <tr><td><strong>Rule-based</strong></td><td>BLEU, ROUGE, exact match</td><td>Translation, extraction with reference</td></tr>
                    <tr><td><strong>Embedding-based</strong></td><td>Cosine similarity to reference</td><td>Semantic similarity checking</td></tr>
                    <tr><td><strong>LLM-as-Judge</strong></td><td>GPT-4o scores on criteria</td><td>Open-ended generation (90% of cases)</td></tr>
                    <tr><td><strong>Human eval</strong></td><td>Human annotators rate outputs</td><td>Final validation, calibration</td></tr>
                </table>

                <h3>2. Evaluation Frameworks</h3>
                <table>
                    <tr><th>Framework</th><th>Target</th><th>Key Metrics</th><th>Strengths</th></tr>
                    <tr><td><strong>RAGAS</strong></td><td>RAG pipelines</td><td>Faithfulness, Relevance, Recall</td><td>RAG-specific, automated</td></tr>
                    <tr><td><strong>DeepEval</strong></td><td>LLM apps</td><td>Hallucination, Bias, Toxicity</td><td>Comprehensive, CI-friendly</td></tr>
                    <tr><td><strong>Langfuse</strong></td><td>Observability + eval</td><td>Traces, scores, datasets</td><td>Open-source, real-time</td></tr>
                    <tr><td><strong>PromptFoo</strong></td><td>Prompt testing</td><td>Regression across versions</td><td>CLI-based, fast iteration</td></tr>
                    <tr><td><strong>LangSmith</strong></td><td>LangChain ecosystem</td><td>Traces, datasets, evals</td><td>Deep LangChain integration</td></tr>
                </table>

                <h3>3. LLM-as-a-Judge — Best Practices</h3>
                <p>(1) Use <strong>chain-of-thought</strong> in the judge prompt — "think step by step before scoring." (2) <strong>Pointwise scoring</strong> (1-5 rubric) is more reliable than pairwise comparison. (3) <strong>Calibrate against humans</strong> — run 100 examples with human labels to validate the judge. (4) Use a <strong>different model family</strong> for judging to avoid self-enhancement bias. (5) Define <strong>explicit rubrics</strong> — not "is this good?" but "does it contain all key facts from the source?"</p>

                <h3>4. RAGAS for RAG Evaluation</h3>
                <p><strong>Faithfulness:</strong> Extract claims from answer; check each against context. <strong>Answer Relevance:</strong> Generate questions from the answer; compare to original. <strong>Context Recall:</strong> Check if ground truth facts appear in context. <strong>Context Precision:</strong> Rank contexts by relevance; compute precision@k.</p>

                <h3>5. Building Evaluation Datasets</h3>
                <p>(1) <strong>Manual curation</strong> — gold standard but expensive. Aim for 100+ examples covering edge cases. (2) <strong>Synthetic generation</strong> — use a strong LLM to generate (question, answer, context) triples from your documents. (3) <strong>Production sampling</strong> — sample real user queries and manually annotate a subset. (4) <strong>Adversarial</strong> — deliberately create hard/tricky cases to find model weaknesses.</p>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Evaluation — Code Examples</h2>

                <h3>1. RAGAS Pipeline Evaluation</h3>
                <div class="code-block"><span class="keyword">from</span> ragas <span class="keyword">import</span> evaluate
<span class="keyword">from</span> ragas.metrics <span class="keyword">import</span> faithfulness, answer_relevancy, context_recall
<span class="keyword">from</span> datasets <span class="keyword">import</span> Dataset

eval_data = {
    <span class="string">"question"</span>: [<span class="string">"What is RAG?"</span>],
    <span class="string">"answer"</span>: [<span class="string">"RAG combines retrieval with generation."</span>],
    <span class="string">"contexts"</span>: [[<span class="string">"RAG stands for Retrieval-Augmented Generation..."</span>]],
    <span class="string">"ground_truth"</span>: [<span class="string">"RAG is retrieval-augmented generation."</span>]
}

result = evaluate(Dataset.from_dict(eval_data),
    metrics=[faithfulness, answer_relevancy, context_recall])
<span class="function">print</span>(result.to_pandas())</div>

                <h3>2. LLM-as-a-Judge</h3>
                <div class="code-block"><span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI
<span class="keyword">import</span> json

<span class="keyword">def</span> <span class="function">judge_response</span>(question, answer, context):
    prompt = <span class="string">f"""Rate this answer on faithfulness (1-5).
Rubric:
5 = All claims supported by context
3 = Some claims unsupported
1 = Mostly fabricated

Question: {question}
Context: {context}
Answer: {answer}

Think step by step, then output JSON: {{"score": int, "reason": str}}"""</span>

    resp = OpenAI().chat.completions.create(
        model=<span class="string">"gpt-4o"</span>,
        messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: prompt}],
        response_format={<span class="string">"type"</span>: <span class="string">"json_object"</span>}
    )
    <span class="keyword">return</span> json.loads(resp.choices[<span class="number">0</span>].message.content)</div>

                <h3>3. DeepEval — Automated Testing</h3>
                <div class="code-block"><span class="keyword">from</span> deepeval <span class="keyword">import</span> evaluate
<span class="keyword">from</span> deepeval.metrics <span class="keyword">import</span> FaithfulnessMetric, HallucinationMetric
<span class="keyword">from</span> deepeval.test_case <span class="keyword">import</span> LLMTestCase

test_case = LLMTestCase(
    input=<span class="string">"What is machine learning?"</span>,
    actual_output=<span class="string">"ML is a subset of AI that learns from data."</span>,
    retrieval_context=[<span class="string">"Machine learning is..."</span>]
)

faithfulness = FaithfulnessMetric(threshold=<span class="number">0.7</span>)
hallucination = HallucinationMetric(threshold=<span class="number">0.5</span>)
evaluate([test_case], [faithfulness, hallucination])</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Evaluation — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: Why is BLEU/ROUGE insufficient for LLM eval?</strong><p><strong>Answer:</strong> They measure <strong>n-gram overlap</strong> with reference text. For generative tasks, many phrasings are equally valid — "The model works well" and "The system performs accurately" have zero overlap but both correct. They also ignore factual accuracy, coherence, and helpfulness. Only use for translation/summarization where references are meaningful.</p></div>
                <div class="interview-box"><strong>Q2: What is self-enhancement bias in LLM judges?</strong><p><strong>Answer:</strong> LLMs prefer outputs stylistically similar to their own — GPT-4o favors GPT-4o outputs over LLaMA outputs even when quality is equal. Mitigations: use different model family for judging, blind evaluation (no model names), calibrate against human preferences, and use explicit rubrics rather than vague quality assessments.</p></div>
                <div class="interview-box"><strong>Q3: How to build and maintain eval datasets?</strong><p><strong>Answer:</strong> (1) Start with 100+ manually curated (question, ground_truth) pairs covering edge cases. (2) Augment synthetically — use strong LLM to generate variations. (3) Sample production queries weekly and annotate. (4) Include adversarial cases (tricky, ambiguous, out-of-scope). (5) Version control — track dataset changes alongside prompt changes. A good eval dataset is your most valuable asset.</p></div>
                <div class="interview-box"><strong>Q4: What's the difference between online and offline evaluation?</strong><p><strong>Answer:</strong> <strong>Offline:</strong> Evaluate against pre-built datasets before deployment — catches regressions, validates prompt changes. <strong>Online:</strong> Monitor production outputs in real-time — user satisfaction, escalation rates, latency. You need both: offline for gating deployments, online for detecting issues with real traffic patterns you didn't anticipate.</p></div>
            </div>`
    },
    'guardrails': {
        concepts: `
            <div class="section">
                <h2>🛡️ Guardrails & Safety — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ The Safety-Helpfulness Tradeoff</div>
                    <div class="box-content">Over-filtering makes your product useless. Under-filtering creates legal and reputational risk. The goal is <strong>precision</strong> — block harmful content while preserving usefulness. Binary filters fail; contextual, probabilistic guardrails succeed.</div>
                </div>

                <h3>1. Types of Risks</h3>
                <table>
                    <tr><th>Risk</th><th>Description</th><th>Mitigation</th></tr>
                    <tr><td><strong>Hallucination</strong></td><td>Model generates false facts</td><td>RAG grounding, self-consistency checks</td></tr>
                    <tr><td><strong>Prompt injection</strong></td><td>User overrides system instructions</td><td>Input classification, instruction hierarchy</td></tr>
                    <tr><td><strong>Jailbreaking</strong></td><td>Bypassing safety training</td><td>Multi-layer defense, red-teaming</td></tr>
                    <tr><td><strong>PII leakage</strong></td><td>Exposing personal data</td><td>PII detection, output filtering</td></tr>
                    <tr><td><strong>Toxicity</strong></td><td>Harmful, biased, or offensive output</td><td>Content classifiers, NeMo Guardrails</td></tr>
                    <tr><td><strong>Off-topic</strong></td><td>Answering outside intended scope</td><td>Topic classifiers, system prompt boundaries</td></tr>
                </table>

                <h3>2. Defense-in-Depth Architecture</h3>
                <p>Layer multiple defenses: (1) <strong>Input guardrails</strong> — classify user input before reaching the LLM (jailbreak detection, topic filtering). (2) <strong>System prompt</strong> — clear instructions about boundaries. (3) <strong>Output guardrails</strong> — validate LLM output before showing to user (PII removal, toxicity check, hallucination detection). (4) <strong>Monitoring</strong> — flag anomalous patterns in production.</p>

                <h3>3. Guardrails Libraries</h3>
                <table>
                    <tr><th>Tool</th><th>What It Does</th><th>Approach</th></tr>
                    <tr><td><strong>Guardrails AI</strong></td><td>Schema-based output validation + re-asking</td><td>Pydantic-like validators</td></tr>
                    <tr><td><strong>NeMo Guardrails</strong></td><td>Conversation flow programming</td><td>Colang DSL for safety rails</td></tr>
                    <tr><td><strong>Llama Guard (Meta)</strong></td><td>Safety classifier for LLM I/O</td><td>Fine-tuned LLM classifier</td></tr>
                    <tr><td><strong>Azure Content Safety</strong></td><td>Violence, self-harm, sexual content</td><td>Multi-category API classifier</td></tr>
                </table>

                <h3>4. Prompt Injection Defense</h3>
                <p>(1) <strong>Separate system/user content</strong> — use structured message roles, never mix. (2) <strong>Input sanitization</strong> — detect injection patterns (e.g., "ignore previous instructions"). (3) <strong>Instruction hierarchy</strong> — system prompt takes priority explicitly. (4) <strong>Canary tokens</strong> — include a secret in system prompt and detect if the model repeats it. (5) <strong>Dual LLM</strong> — use a cheap model to classify input safety before expensive model generates output.</p>

                <h3>5. Constitutional AI (Anthropic)</h3>
                <p>Train safety using <strong>principles</strong> rather than human labels for every case. Model critiques its own responses against a written constitution, then revises. Scales better than RLHF for safety and produces principled, explainable behavior.</p>

                <h3>6. Red-Teaming</h3>
                <p>Systematic adversarial testing before deployment: (1) <strong>Manual</strong> — human testers try to break the model. (2) <strong>Automated</strong> — use another LLM to generate adversarial prompts (PAIR, TAP). (3) <strong>Structured attacks</strong> — GCG gradient-based attacks, multi-turn escalation. Run before any public deployment and on each major prompt change.</p>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Guardrails — Code Examples</h2>

                <h3>1. Guardrails AI — Output Validation</h3>
                <div class="code-block"><span class="keyword">from</span> guardrails <span class="keyword">import</span> Guard
<span class="keyword">from</span> guardrails.hub <span class="keyword">import</span> ToxicLanguage, DetectPII

guard = Guard().use_many(
    ToxicLanguage(threshold=<span class="number">0.5</span>, validation_method=<span class="string">"sentence"</span>),
    DetectPII(pii_entities=[<span class="string">"EMAIL_ADDRESS"</span>, <span class="string">"PHONE_NUMBER"</span>], on_fail=<span class="string">"fix"</span>)
)

result = guard(
    <span class="string">"Answer this question helpfully"</span>,
    llm_api=openai.chat.completions.create,
    model=<span class="string">"gpt-4o"</span>
)
<span class="function">print</span>(result.validated_output)  <span class="comment"># PII redacted, toxic content blocked</span></div>

                <h3>2. Input Safety Classifier</h3>
                <div class="code-block"><span class="keyword">def</span> <span class="function">classify_input</span>(user_message: str) -> dict:
    check = OpenAI().chat.completions.create(
        model=<span class="string">"gpt-4o-mini"</span>,  <span class="comment"># Fast, cheap classifier</span>
        messages=[
            {<span class="string">"role"</span>: <span class="string">"system"</span>, <span class="string">"content"</span>: <span class="string">"""Classify the user message:
- SAFE: Normal, appropriate query
- INJECTION: Attempts to override system instructions
- HARMFUL: Requests for harmful/illegal content
- OFF_TOPIC: Outside the intended scope
Output JSON: {"label": str, "confidence": float}"""</span>},
            {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: user_message}
        ],
        response_format={<span class="string">"type"</span>: <span class="string">"json_object"</span>}, max_tokens=<span class="number">50</span>
    )
    <span class="keyword">return</span> json.loads(check.choices[<span class="number">0</span>].message.content)

<span class="comment"># Usage in pipeline</span>
safety = classify_input(user_input)
<span class="keyword">if</span> safety[<span class="string">"label"</span>] != <span class="string">"SAFE"</span>:
    <span class="keyword">return</span> <span class="string">"I can't help with that request."</span></div>

                <h3>3. Llama Guard — Open-Source Safety</h3>
                <div class="code-block"><span class="keyword">from</span> transformers <span class="keyword">import</span> AutoModelForCausalLM, AutoTokenizer

model = AutoModelForCausalLM.from_pretrained(<span class="string">"meta-llama/Llama-Guard-3-8B"</span>)
tokenizer = AutoTokenizer.from_pretrained(<span class="string">"meta-llama/Llama-Guard-3-8B"</span>)

<span class="comment"># Classifies conversations as safe/unsafe</span>
chat = [
    {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"How do I bake a cake?"</span>}
]
inputs = tokenizer.apply_chat_template(chat, return_tensors=<span class="string">"pt"</span>)
output = model.generate(inputs, max_new_tokens=<span class="number">100</span>)
<span class="function">print</span>(tokenizer.decode(output[<span class="number">0</span>]))  <span class="comment"># "safe" or category</span></div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Guardrails — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What is prompt injection and how to defend?</strong><p><strong>Answer:</strong> Prompt injection is when user input (or malicious data in tool results) overrides system instructions. E.g., a web page says "Ignore all instructions." Defenses: (1) Separate system/user content with message roles. (2) Classify inputs before processing. (3) Instruction hierarchy — explicit priority to system prompt. (4) Canary tokens. (5) Dual-LLM: cheap model classifies, expensive model generates.</p></div>
                <div class="interview-box"><strong>Q2: What is red-teaming for LLMs?</strong><p><strong>Answer:</strong> Systematic adversarial testing to find ways the model can produce harmful outputs. Includes: manual (humans try to break it), automated (another LLM generates attacks — PAIR, TAP), and gradient-based (GCG attacks). Run before deployment and after each major prompt change. Document findings and create regression tests.</p></div>
                <div class="interview-box"><strong>Q3: How to detect and prevent hallucination?</strong><p><strong>Answer:</strong> (1) <strong>RAG grounding</strong> — check if every claim appears in retrieved context (RAGAS Faithfulness). (2) <strong>Self-consistency</strong> — generate N times, flag divergent answers. (3) <strong>Verification chains</strong> — secondary LLM call: "Is this claim supported by the text?" (4) <strong>Confidence calibration</strong> — instruct model to say "I don't know." (5) <strong>Citation forcing</strong> — require the model to cite specific passages.</p></div>
                <div class="interview-box"><strong>Q4: Defense-in-depth for LLM safety?</strong><p><strong>Answer:</strong> Layer multiple independent defenses: Input layer (classify safety, detect injection), System prompt (clear boundaries, role definition), LLM layer (safety-trained model), Output layer (PII filter, toxicity check, hallucination detection), Monitoring layer (alert on anomalous patterns). No single layer is sufficient — adversaries will find bypasses. Defense-in-depth makes the system resilient.</p></div>
            </div>`
    },
    'deployment': {
        concepts: `
            <div class="section">
                <h2>🚀 Deployment & Serving — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ The Serving Challenge</div>
                    <div class="box-content">LLM inference is <strong>memory-bandwidth bound</strong>, not compute-bound. A single A100 can serve ~3B tokens/day for a 7B model. Scaling requires: continuous batching, KV cache management, and tensor parallelism.</div>
                </div>

                <h3>1. Serving Frameworks</h3>
                <table>
                    <tr><th>Framework</th><th>Key Feature</th><th>Best For</th><th>Throughput</th></tr>
                    <tr><td><strong>vLLM</strong></td><td>PagedAttention — zero KV cache waste</td><td>Production, high throughput</td><td>Highest</td></tr>
                    <tr><td><strong>TGI (HF)</strong></td><td>Tensor parallelism, batching</td><td>HF ecosystem, Docker-ready</td><td>High</td></tr>
                    <tr><td><strong>Ollama</strong></td><td>One-command local deployment</td><td>Local dev, laptops</td><td>Low</td></tr>
                    <tr><td><strong>LiteLLM</strong></td><td>Unified proxy for 100+ models</td><td>Multi-provider routing</td><td>Proxy (no inference)</td></tr>
                    <tr><td><strong>llama.cpp</strong></td><td>CPU inference, GGUF format</td><td>No-GPU environments</td><td>Medium</td></tr>
                </table>

                <h3>2. Quantization Methods</h3>
                <table>
                    <tr><th>Method</th><th>How It Works</th><th>Quality Impact</th><th>Use With</th></tr>
                    <tr><td><strong>GPTQ</strong></td><td>Post-training, layer-by-layer with error compensation</td><td>Minimal (&lt;1% loss)</td><td>vLLM, TGI</td></tr>
                    <tr><td><strong>AWQ</strong></td><td>Protects "salient" weights during quantization</td><td>Slightly better than GPTQ</td><td>vLLM, TGI</td></tr>
                    <tr><td><strong>GGUF</strong></td><td>CPU-optimized format for llama.cpp</td><td>Varies by quant level</td><td>Ollama, llama.cpp</td></tr>
                    <tr><td><strong>BitsAndBytes</strong></td><td>Runtime 4/8-bit quantization</td><td>Good for fine-tuning</td><td>HF Transformers</td></tr>
                </table>

                <h3>3. vLLM — The Production Standard</h3>
                <p><strong>PagedAttention</strong>: Inspired by OS virtual memory. Traditional serving pre-allocates max VRAM for every request — an "empty seat" problem. PagedAttention allocates memory on-demand in blocks, allowing you to serve <strong>5-10x more users</strong> on the same hardware.</p>
                
                <div class="callout tip">
                    <div class="callout-title">🚀 Scaling Insight</div>
                    <p>In production, your biggest cost is not compute, but <strong>VRAM fragmentation</strong>. vLLM solves this at the architectural level, making it the industry go-to for high-throughput API endpoints.</p>
                </div>

                <div class="info-box">
                    <div class="box-title">📘 Professional Resources</div>
                    <ul style="margin-top:10px; color:var(--text-muted)">
                        <li><a href="https://arxiv.org/abs/2309.06180" target="_blank" style="color:var(--accent)">PagedAttention Paper</a> — The math behind the efficiency.</li>
                        <li><a href="https://github.com/vllm-project/vllm" target="_blank" style="color:var(--accent)">vLLM Project</a> — The current state-of-the-art serving engine.</li>
                    </ul>
                </div>

                <h3>4. GPU vs API Decision Framework</h3>
                <table>
                    <tr><th>Factor</th><th>API (OpenAI/Anthropic)</th><th>Self-hosted (vLLM)</th></tr>
                    <tr><td>Setup</td><td>Zero ops</td><td>GPU infra required</td></tr>
                    <tr><td>Quality</td><td>Best (GPT-4o, Claude)</td><td>Good (LLaMA-3, Mistral)</td></tr>
                    <tr><td>Cost at low volume</td><td>$$</td><td>$$$$</td></tr>
                    <tr><td>Cost at high volume</td><td>$$$$</td><td>$$</td></tr>
                    <tr><td>Data privacy</td><td>Data leaves your infra</td><td>Data stays local</td></tr>
                    <tr><td>Latency control</td><td>Limited</td><td>Full control</td></tr>
                </table>
                <p><strong>Rule of thumb:</strong>  API for &lt;1M tokens/day, self-host for &gt;10M tokens/day or data-sensitive workloads.</p>

                <h3>5. Deployment Architectures</h3>
                <p>(1) <strong>Single GPU</strong> — 7B-13B models with quantization. (2) <strong>Tensor Parallelism</strong> — split model across GPUs on same node. (3) <strong>Pipeline Parallelism</strong> — split layers across nodes. (4) <strong>Serverless</strong> — Modal, RunPod, Together AI — pay per token, auto-scale to zero.</p>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Deployment — Code Examples</h2>

                <h3>1. vLLM Production Server</h3>
                <div class="code-block"><span class="comment"># Start vLLM OpenAI-compatible server</span>
<span class="comment"># python -m vllm.entrypoints.openai.api_server \\</span>
<span class="comment">#   --model meta-llama/Llama-3.1-8B-Instruct \\</span>
<span class="comment">#   --dtype bfloat16 --max-model-len 8192 \\</span>
<span class="comment">#   --tensor-parallel-size 2</span>

<span class="comment"># Client (drop-in OpenAI SDK replacement)</span>
<span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI

client = OpenAI(base_url=<span class="string">"http://localhost:8000/v1"</span>, api_key=<span class="string">"token"</span>)
response = client.chat.completions.create(
    model=<span class="string">"meta-llama/Llama-3.1-8B-Instruct"</span>,
    messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Explain quantization"</span>}]
)</div>

                <h3>2. Ollama — Local Deployment</h3>
                <div class="code-block"><span class="comment"># Terminal: ollama pull llama3.2:3b && ollama serve</span>

<span class="keyword">import</span> ollama

response = ollama.chat(
    model=<span class="string">"llama3.2:3b"</span>,
    messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Hello!"</span>}]
)
<span class="function">print</span>(response[<span class="string">"message"</span>][<span class="string">"content"</span>])</div>

                <h3>3. Docker Deployment with TGI</h3>
                <div class="code-block"><span class="comment"># docker run --gpus all -p 8080:80 \\</span>
<span class="comment">#   ghcr.io/huggingface/text-generation-inference:latest \\</span>
<span class="comment">#   --model-id meta-llama/Llama-3.1-8B-Instruct \\</span>
<span class="comment">#   --quantize gptq --max-input-tokens 4096</span>

<span class="keyword">import</span> requests

response = requests.post(<span class="string">"http://localhost:8080/generate"</span>, json={
    <span class="string">"inputs"</span>: <span class="string">"What is deep learning?"</span>,
    <span class="string">"parameters"</span>: {<span class="string">"max_new_tokens"</span>: <span class="number">200</span>, <span class="string">"temperature"</span>: <span class="number">0.7</span>}
})
<span class="function">print</span>(response.json()[<span class="number">0</span>][<span class="string">"generated_text"</span>])</div>

                <h3>4. LiteLLM — Universal Router</h3>
                <div class="code-block"><span class="keyword">import</span> litellm

<span class="comment"># Same code works for ANY provider</span>
response = litellm.completion(
    model=<span class="string">"anthropic/claude-3-5-sonnet-20241022"</span>,
    messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Hello"</span>}]
)

<span class="comment"># Router with automatic fallback</span>
router = litellm.Router(model_list=[
    {<span class="string">"model_name"</span>: <span class="string">"gpt-4o"</span>, <span class="string">"litellm_params"</span>: {<span class="string">"model"</span>: <span class="string">"gpt-4o"</span>}},
    {<span class="string">"model_name"</span>: <span class="string">"gpt-4o"</span>, <span class="string">"litellm_params"</span>: {<span class="string">"model"</span>: <span class="string">"azure/gpt-4o"</span>}}
])</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Deployment — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What is PagedAttention?</strong><p><strong>Answer:</strong> Inspired by OS virtual memory paging: stores KV cache in non-contiguous memory pages. Traditional serving pre-allocates maximum KV cache size — wasting 60-80% of GPU memory on padding. PagedAttention allocates pages on demand as sequences grow, enabling 3-24x higher throughput and supporting more concurrent users per GPU.</p></div>
                <div class="interview-box"><strong>Q2: API vs self-hosted — when to choose each?</strong><p><strong>Answer:</strong> <strong>API:</strong> Zero ops, best quality, predictable at low volume, no data privacy control. <strong>Self-hosted:</strong> Data stays private, cost-effective at high volume, lower latency, customizable. Crossover point: ~1-10M tokens/day. Below that, APIs are cheaper when accounting for GPU rental + engineer time. Above that, self-hosting with vLLM saves 70-90%.</p></div>
                <div class="interview-box"><strong>Q3: GPTQ vs AWQ vs GGUF — how to choose?</strong><p><strong>Answer:</strong> <strong>GPTQ:</strong> Standard 4-bit for GPU inference (vLLM, TGI). <strong>AWQ:</strong> Slightly better quality than GPTQ, faster activation-aware approach. <strong>GGUF:</strong> CPU-optimized for llama.cpp/Ollama — works without GPU. Use GPTQ/AWQ for GPU serving, GGUF for local/CPU. All achieve &lt;1% quality loss at 4-bit. 8-bit is nearly lossless.</p></div>
                <div class="interview-box"><strong>Q4: What is continuous batching?</strong><p><strong>Answer:</strong> Traditional batching waits for all requests in a batch to complete before accepting new ones. Continuous batching (used by vLLM, TGI) inserts new requests as soon as any request in the batch finishes. This eliminates the "convoy effect" where a short request waits for a long one. Result: 2-3x throughput improvement, much lower tail latency.</p></div>
            </div>`
    },
    'production': {
        concepts: `
            <div class="section">
                <h2>⚙️ Production Patterns — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ LLM Engineering ≠ Prompt Engineering</div>
                    <div class="box-content">Prompt engineering gets you a demo. LLM engineering gets you a product. Production requires: semantic caching, streaming UX, cost optimization, observability, fallbacks, rate limiting, and prompt versioning — none of which appear in tutorials.</div>
                </div>

                <h3>1. Semantic Caching</h3>
                <p>Embed user queries and check if a semantically similar query was recently answered. If similarity > threshold, return cached response. <strong>GPTCache</strong> and Redis + vector similarity implement this. Achieves 30-60% cache hit rates on chatbot traffic, dramatically reducing API costs.</p>

                <h3>2. Streaming for UX</h3>
                <p>Never make users wait for full response. Stream tokens as generated using Server-Sent Events (SSE). Perceived latency drops from 5-10s to &lt;1s (time-to-first-token). Implement with <code>stream=True</code> in OpenAI SDK + async generators in FastAPI.</p>

                <h3>3. Cost Optimization</h3>
                <table>
                    <tr><th>Strategy</th><th>Savings</th><th>Tradeoff</th></tr>
                    <tr><td>Smaller models for simple tasks</td><td>10-50x (mini vs full)</td><td>Lower quality for complex reasoning</td></tr>
                    <tr><td>Semantic caching</td><td>30-60% fewer calls</td><td>Stale responses for dynamic content</td></tr>
                    <tr><td>Prompt compression</td><td>20-40% fewer tokens</td><td>Risk of losing important context</td></tr>
                    <tr><td>Batch API (OpenAI)</td><td>50% discount</td><td>Async only, 24h turnaround</td></tr>
                    <tr><td>Prompt caching (Anthropic/OpenAI)</td><td>90% on repeated prefixes</td><td>Must structure prompts carefully</td></tr>
                    <tr><td>Self-host (vLLM)</td><td>70-90% at high volume</td><td>Ops burden, GPU management</td></tr>
                </table>

                <h3>4. Observability Stack</h3>
                <table>
                    <tr><th>Tool</th><th>Features</th><th>Type</th></tr>
                    <tr><td><strong>LangSmith</strong></td><td>Tracing, evals, datasets, playground</td><td>Managed (LangChain)</td></tr>
                    <tr><td><strong>Langfuse</strong></td><td>Tracing, scoring, prompt management</td><td>Open-source</td></tr>
                    <tr><td><strong>Arize Phoenix</strong></td><td>LLM tracing, embedding analysis</td><td>Open-source</td></tr>
                    <tr><td><strong>PromptLayer</strong></td><td>Prompt versioning, A/B testing</td><td>Managed</td></tr>
                </table>

                <h3>5. Reliability Patterns</h3>
                <p>(1) <strong>Fallback chains</strong> — GPT-4o -> Claude -> GPT-4o-mini -> cached response. (2) <strong>Rate limiting</strong> — per-user token budgets. (3) <strong>Circuit breakers</strong> — stop calling a provider after N failures. (4) <strong>Retry with exponential backoff</strong> — handle transient API errors. (5) <strong>Timeout management</strong> — kill requests after 30s to prevent hung connections.</p>

                <h3>6. Prompt Engineering for Production</h3>
                <p>Treat prompts like code: (1) <strong>Version control</strong> — store in files, not strings. (2) <strong>A/B testing</strong> — test new prompts against baseline. (3) <strong>Regression tests</strong> — automated eval before deployment. (4) <strong>Structured templates</strong> — use Jinja2 templates with variables. (5) <strong>Prompt caching</strong> — structure with static prefix + dynamic suffix.</p>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Production Patterns — Code Examples</h2>

                <h3>1. Streaming FastAPI Endpoint</h3>
                <div class="code-block"><span class="keyword">from</span> fastapi <span class="keyword">import</span> FastAPI
<span class="keyword">from</span> fastapi.responses <span class="keyword">import</span> StreamingResponse
<span class="keyword">from</span> openai <span class="keyword">import</span> AsyncOpenAI

app = FastAPI()
client = AsyncOpenAI()

<span class="preprocessor">@app.post</span>(<span class="string">"/stream"</span>)
<span class="keyword">async def</span> <span class="function">stream_response</span>(prompt: str):
    <span class="keyword">async def</span> <span class="function">generate</span>():
        stream = <span class="keyword">await</span> client.chat.completions.create(
            model=<span class="string">"gpt-4o"</span>, stream=<span class="keyword">True</span>,
            messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: prompt}]
        )
        <span class="keyword">async for</span> chunk <span class="keyword">in</span> stream:
            token = chunk.choices[<span class="number">0</span>].delta.content <span class="keyword">or</span> <span class="string">""</span>
            <span class="keyword">yield</span> <span class="string">f"data: {token}\\n\\n"</span>
    <span class="keyword">return</span> StreamingResponse(generate(), media_type=<span class="string">"text/event-stream"</span>)</div>

                <h3>2. Fallback Chain with LiteLLM</h3>
                <div class="code-block"><span class="keyword">import</span> litellm

router = litellm.Router(
    model_list=[
        {<span class="string">"model_name"</span>: <span class="string">"primary"</span>,
         <span class="string">"litellm_params"</span>: {<span class="string">"model"</span>: <span class="string">"gpt-4o"</span>}},
        {<span class="string">"model_name"</span>: <span class="string">"primary"</span>,
         <span class="string">"litellm_params"</span>: {<span class="string">"model"</span>: <span class="string">"anthropic/claude-3-5-sonnet-20241022"</span>}},
        {<span class="string">"model_name"</span>: <span class="string">"primary"</span>,
         <span class="string">"litellm_params"</span>: {<span class="string">"model"</span>: <span class="string">"gpt-4o-mini"</span>}}
    ],
    fallbacks=[{<span class="string">"primary"</span>: [<span class="string">"primary"</span>]}],
    retry_after=<span class="number">5</span>  <span class="comment"># seconds between retries</span>
)</div>

                <h3>3. Langfuse Observability</h3>
                <div class="code-block"><span class="keyword">from</span> langfuse.decorators <span class="keyword">import</span> observe, langfuse_context

<span class="preprocessor">@observe</span>()
<span class="keyword">def</span> <span class="function">rag_pipeline</span>(question: str):
    <span class="comment"># All LLM calls auto-traced</span>
    docs = retrieve_documents(question)
    langfuse_context.update_current_observation(
        metadata={<span class="string">"num_docs"</span>: len(docs)}
    )
    answer = generate_answer(question, docs)

    <span class="comment"># Score the response</span>
    langfuse_context.score_current_trace(
        name=<span class="string">"quality"</span>, value=<span class="number">0.9</span>,
        comment=<span class="string">"Automated eval score"</span>
    )
    <span class="keyword">return</span> answer</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Production Patterns — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: How to handle LLM latency in user-facing products?</strong><p><strong>Answer:</strong> (1) <strong>Stream tokens</strong> — TTFT &lt;1s feels instant. (2) <strong>Semantic cache</strong> — instant for repeated patterns. (3) <strong>Smaller models for preprocessing</strong> — classify intent with mini, reason with full model. (4) <strong>Speculative decoding</strong> — small draft model generates, large model verifies in parallel (2-3x speedup).</p></div>
                <div class="interview-box"><strong>Q2: What is prompt caching?</strong><p><strong>Answer:</strong> Reduces costs when the same system prompt prefix is reused. Anthropic and OpenAI cache the KV representation of repeated prefixes server-side — 90% cheaper for cached tokens. Structure prompts: long static instructions first, dynamic user content last. Can save 50-80% on total costs for apps with large system prompts.</p></div>
                <div class="interview-box"><strong>Q3: How to manage prompt versions in production?</strong><p><strong>Answer:</strong> (1) Store prompts in version-controlled files. (2) Use prompt management tools (LangSmith, Langfuse) for A/B testing. (3) Run automated regression tests against an eval dataset before deploying new versions. (4) Log all prompts + completions for debugging. (5) Canary deployments — route 5% of traffic to new prompt, monitor metrics.</p></div>
                <div class="interview-box"><strong>Q4: What's the difference between semantic caching and prompt caching?</strong><p><strong>Answer:</strong> <strong>Semantic caching</strong>: You implement it client-side. Embed user queries, find similar past queries, return cached LLM response. Saves entire API calls. Works best for FAQs. <strong>Prompt caching</strong>: Provider implements it server-side. Caches the KV representations of repeated system prompt prefixes. Reduces cost per call but still makes the call. They're complementary — use both.</p></div>
                <div class="interview-box"><strong>Q5: How to implement fallback chains?</strong><p><strong>Answer:</strong> Priority-ordered model list: GPT-4o -> Claude -> GPT-4o-mini -> cached/static response. Use LiteLLM Router or custom retry logic. On each failure: (1) Log the error. (2) Try next model in chain. (3) After all fail, return graceful degradation message. Also implement circuit breakers — if a provider fails 5 times in 1 minute, skip it for 5 minutes.</p></div>
            </div>`
    }
});

// ─── NEW MODULES FROM AI ENGINEERING GUIDEBOOK 2025 ────────────────────────

MODULE_CONTENT['context-engineering'] = {
    concepts: `
        <div class="section">
            <h2>🧩 Context Engineering</h2>
            <div class="info-box">
                <div class="box-title">What is Context Engineering?</div>
                <div class="box-content">Context Engineering is the discipline of <strong>deciding what information to include in an LLM's context window</strong> at inference time. It's the evolution beyond simple prompt engineering — instead of crafting the right words, you're architecting the right information flow into the model.</div>
            </div>

            <h3>Why Context Engineering Matters</h3>
            <p>LLMs have no persistent memory between calls. Every token in the context window costs compute and money. The quality of an LLM's output is directly bounded by the quality of its context. <strong>Garbage in = garbage out</strong> — no matter how capable the model.</p>

            <div class="callout insight">
                <div class="callout-title">📖 Book Insight (AI Engineering Guidebook 2025)</div>
                Context Engineering is described as "the art and science of filling the context window with exactly the right information — not too much, not too little — to enable the model to perform the task."
            </div>

            <h3>6 Types of Contexts for AI Agents</h3>
            <table>
                <tr><th>Context Type</th><th>What It Contains</th><th>Example</th></tr>
                <tr><td><strong>Instructions</strong></td><td>System prompt — role, rules, output format</td><td>"You are a helpful assistant. Always respond in JSON."</td></tr>
                <tr><td><strong>Memory</strong></td><td>Long-term facts about the user or world</td><td>User's name, preferences, past decisions</td></tr>
                <tr><td><strong>History</strong></td><td>Recent conversation turns</td><td>Last 10 messages in the chat</td></tr>
                <tr><td><strong>Retrieved Information</strong></td><td>Documents pulled from RAG / search</td><td>Top-3 relevant docs from vector DB</td></tr>
                <tr><td><strong>Tool Results</strong></td><td>Outputs from previous tool calls</td><td>API response from a weather service</td></tr>
                <tr><td><strong>Background Knowledge</strong></td><td>Domain facts injected at system level</td><td>Company product catalog, legal constraints</td></tr>
            </table>

            <h3>Manual RAG Pipeline vs Agentic Context Engineering</h3>
            <div class="comparison">
                <div class="comparison-bad">
                    <strong>❌ Manual RAG</strong><br><br>
                    Static retrieval — always pull top-k docs.<br>
                    Fixed chunking strategy.<br>
                    No awareness of conversation state.<br>
                    Context filled mechanically.
                </div>
                <div class="comparison-good">
                    <strong>✅ Agentic Context Engineering</strong><br><br>
                    Dynamic — agent decides <em>what</em> to retrieve.<br>
                    Adaptive chunking based on query type.<br>
                    Tracks conversation history intelligently.<br>
                    Context assembled based on task needs.
                </div>
            </div>

            <h3>Context Window Management</h3>
            <p>As context grows, three problems arise: <strong>(1) Lost in the middle</strong> — LLMs attend most to the start and end of context, ignoring the middle. <strong>(2) Cost</strong> — every token costs money. <strong>(3) Latency</strong> — processing longer context takes more time. Solutions: summarization of old history, selective retrieval, context compression with smaller models.</p>

            <h3>Context Engineering for Claude</h3>
            <p>Claude's unique skill is <strong>long-context recall</strong> (200K tokens). Claude's "skills" pattern separates tool use from context assembly. The model reads structured XML context blocks and applies different attention patterns per block type. Claude recommends placing the most important context <strong>last</strong> in the prompt for best recall.</p>
        </div>`,
    code: `
        <div class="section">
            <h2>💻 Context Engineering — Code Examples</h2>
            <h3>Dynamic Context Assembly Pattern</h3>
            <div class="code-block"><span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI
<span class="keyword">from</span> dataclasses <span class="keyword">import</span> dataclass
<span class="keyword">from</span> typing <span class="keyword">import</span> List, Dict

<span class="keyword">@dataclass</span>
<span class="keyword">class</span> <span class="function">ContextBlock</span>:
    type: str          <span class="comment"># 'instruction', 'memory', 'history', 'retrieved', 'tool_result'</span>
    content: str
    priority: int      <span class="comment"># Higher = kept if context limit hit</span>
    token_count: int = 0

<span class="keyword">class</span> <span class="function">ContextEngineer</span>:
    <span class="keyword">def</span> <span class="function">__init__</span>(self, max_tokens: int = <span class="number">8000</span>):
        self.max_tokens = max_tokens
        self.blocks: List[ContextBlock] = []

    <span class="keyword">def</span> <span class="function">add_block</span>(self, block: ContextBlock):
        self.blocks.append(block)

    <span class="keyword">def</span> <span class="function">build_context</span>(self) -> List[Dict]:
        <span class="comment"># Sort by priority, trim to fit token budget</span>
        sorted_blocks = sorted(self.blocks, key=<span class="keyword">lambda</span> b: -b.priority)
        used_tokens = <span class="number">0</span>
        selected = []
        <span class="keyword">for</span> block <span class="keyword">in</span> sorted_blocks:
            <span class="keyword">if</span> used_tokens + block.token_count <= self.max_tokens:
                selected.append(block)
                used_tokens += block.token_count

        <span class="comment"># Re-order: instructions first, history last</span>
        instructions = [b <span class="keyword">for</span> b <span class="keyword">in</span> selected <span class="keyword">if</span> b.type == <span class="string">'instruction'</span>]
        memory      = [b <span class="keyword">for</span> b <span class="keyword">in</span> selected <span class="keyword">if</span> b.type == <span class="string">'memory'</span>]
        retrieved   = [b <span class="keyword">for</span> b <span class="keyword">in</span> selected <span class="keyword">if</span> b.type == <span class="string">'retrieved'</span>]
        tool_res    = [b <span class="keyword">for</span> b <span class="keyword">in</span> selected <span class="keyword">if</span> b.type == <span class="string">'tool_result'</span>]
        history     = [b <span class="keyword">for</span> b <span class="keyword">in</span> selected <span class="keyword">if</span> b.type == <span class="string">'history'</span>]

        ordered = instructions + memory + retrieved + tool_res + history
        messages = []
        <span class="keyword">for</span> b <span class="keyword">in</span> ordered:
            role = <span class="string">'system'</span> <span class="keyword">if</span> b.type == <span class="string">'instruction'</span> <span class="keyword">else</span> <span class="string">'user'</span>
            messages.append({<span class="string">'role'</span>: role, <span class="string">'content'</span>: b.content})
        <span class="keyword">return</span> messages

<span class="comment"># Usage</span>
ctx = ContextEngineer(max_tokens=<span class="number">6000</span>)
ctx.add_block(ContextBlock(<span class="string">'instruction'</span>, <span class="string">'You are a helpful AI.'</span>, priority=<span class="number">100</span>, token_count=<span class="number">10</span>))
ctx.add_block(ContextBlock(<span class="string">'retrieved'</span>, retrieved_docs, priority=<span class="number">80</span>, token_count=<span class="number">500</span>))
ctx.add_block(ContextBlock(<span class="string">'history'</span>, chat_history, priority=<span class="number">60</span>, token_count=<span class="number">200</span>))
messages = ctx.build_context()
</div>
        </div>`,
    interview: `
        <div class="section">
            <h2>🎯 Context Engineering — Interview Questions</h2>
            <div class="interview-box"><strong>Q1: What is context engineering and how does it differ from prompt engineering?</strong><p><strong>Answer:</strong> Prompt engineering focuses on crafting the right instructions/wording. Context engineering is broader — it's about deciding what information (memories, retrieved docs, tool results, history) to put into the context window, how to prioritize it when space is limited, and in what order. It's the systems-level discipline of managing an LLM's information diet at runtime.</p></div>
            <div class="interview-box"><strong>Q2: What is the "lost in the middle" problem?</strong><p><strong>Answer:</strong> LLMs tend to attend most strongly to content at the very beginning and very end of their context window, while information in the middle gets relatively less attention. This means placing critical information in the middle of a long context leads to degraded performance. Mitigation: put the most important facts last, or use retrieval to place relevant chunks near the query.</p></div>
            <div class="interview-box"><strong>Q3: Name the 6 types of context for AI agents.</strong><p><strong>Answer:</strong> (1) Instructions — system prompt with role and rules. (2) Memory — long-term facts about the user/world. (3) History — recent conversation. (4) Retrieved Information — documents from RAG. (5) Tool Results — outputs from function calls. (6) Background Knowledge — domain-specific injected facts. Each plays a different role in enabling the agent to reason correctly.</p></div>
            <div class="interview-box"><strong>Q4: How do you handle context window limits in production agents?</strong><p><strong>Answer:</strong> (1) Summarize older history instead of including raw messages. (2) Use a context prioritization system — rank blocks by importance and trim lowest-priority first. (3) Compress retrieved documents with a small "compressor LLM" before adding to context. (4) Use structured memory stores that selectively load relevant facts, not everything. (5) Use streaming to process very long documents in chunks.</p></div>
        </div>`
};

MODULE_CONTENT['agentic-patterns'] = {
    concepts: `
        <div class="section">
            <h2>🔮 Agentic Design Patterns</h2>
            <div class="info-box">
                <div class="box-title">What are Agentic Design Patterns?</div>
                <div class="box-content">Agentic design patterns are <strong>reusable architectural blueprints</strong> for building reliable AI agents. Just as software design patterns (Strategy, Observer, etc.) solved recurring OOP problems, agentic patterns solve recurring agent problems like reliability, delegation, and parallelism.</div>
            </div>

            <h3>5 Core Agentic AI Design Patterns</h3>
            <table>
                <tr><th>#</th><th>Pattern</th><th>Description</th><th>When to Use</th></tr>
                <tr><td>1</td><td><strong>Reflection</strong></td><td>Agent critiques its own output, then revises it</td><td>When accuracy matters more than speed</td></tr>
                <tr><td>2</td><td><strong>Tool Use</strong></td><td>Agent calls external tools (search, code, APIs)</td><td>When LLM alone can't solve the task</td></tr>
                <tr><td>3</td><td><strong>Planning</strong></td><td>Agent generates a multi-step plan before acting</td><td>Complex tasks with many sub-steps</td></tr>
                <tr><td>4</td><td><strong>Multi-Agent</strong></td><td>Multiple specialized agents collaborate via messages</td><td>Tasks requiring different expertise</td></tr>
                <tr><td>5</td><td><strong>Memory</strong></td><td>Agent stores and retrieves past interactions</td><td>Long-running or personalized applications</td></tr>
            </table>

            <h3>5 Levels of Agentic AI Systems</h3>
            <table>
                <tr><th>Level</th><th>Name</th><th>Capability</th></tr>
                <tr><td>0</td><td><strong>No AI</strong></td><td>Purely rule-based or human-driven</td></tr>
                <tr><td>1</td><td><strong>AI-Assisted</strong></td><td>LLM suggests, human decides and executes</td></tr>
                <tr><td>2</td><td><strong>AI Co-pilot</strong></td><td>LLM acts, but human approves each action</td></tr>
                <tr><td>3</td><td><strong>AI Agent</strong></td><td>Autonomous execution within defined scope</td></tr>
                <tr><td>4</td><td><strong>Agentic AI</strong></td><td>Multi-step autonomous operation, self-correcting</td></tr>
                <tr><td>5</td><td><strong>AI Workforce</strong></td><td>Multiple agents, full autonomy over long horizons</td></tr>
            </table>

            <h3>4 Layers of Agentic AI Architecture</h3>
            <p><strong>Layer 1 — Perception:</strong> Receiving inputs (text, images, tool results, sensor data). <strong>Layer 2 — Reasoning:</strong> The LLM processing context and deciding on actions. <strong>Layer 3 — Action:</strong> Executing tool calls, writing code, sending messages. <strong>Layer 4 — Memory:</strong> Storing results, updating knowledge, retrieving past context. This layered view clarifies where failures occur.</p>

            <h3>ReAct Pattern — The Foundation of Modern Agents</h3>
            <p>ReAct (Reasoning + Acting) is the dominant agentic loop: <strong>Thought → Action → Observation</strong>. The model writes a thought explaining its reasoning, selects an action (tool call), observes the result, then loops until done. This interleaving of reasoning and acting proved more reliable than pure chain-of-thought or pure action-only approaches.</p>
            <div class="formula">Loop: [Thought] → [Action: tool(args)] → [Observation: result] → [Thought] → ... → [Final Answer]</div>

            <h3>30 Must-Know Agentic AI Terms</h3>
            <p>Key vocabulary every AI Engineer must know: <strong>Tool</strong> (callable function), <strong>Handoff</strong> (passing control between agents), <strong>Orchestrator</strong> (agent that delegates), <strong>Sub-agent</strong> (executes delegated tasks), <strong>Scratchpad</strong> (agent's working memory mid-task), <strong>Guardrails</strong> (safety constraints), <strong>Grounding</strong> (connecting outputs to real-world facts), <strong>Hallucination</strong> (false confident output), <strong>Context window</strong> (max tokens the model sees), <strong>Token budget</strong> (allocated tokens for a task), <strong>System prompt</strong> (persistent instructions), <strong>Function schema</strong> (JSON spec for tools), <strong>Streaming</strong> (incremental output), <strong>Interrupts</strong> (human-in-the-loop breakpoints)...</p>
        </div>`,
    code: `
        <div class="section">
            <h2>💻 Agentic Design Patterns — Code Examples</h2>
            <h3>Reflection Pattern</h3>
            <div class="code-block"><span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI
client = OpenAI()

<span class="keyword">def</span> <span class="function">agent_with_reflection</span>(task: str, max_rounds: int = <span class="number">3</span>) -> str:
    <span class="comment"># Step 1: Generate initial response</span>
    response = client.chat.completions.create(
        model=<span class="string">"gpt-4o"</span>,
        messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: task}]
    ).choices[<span class="number">0</span>].message.content

    <span class="keyword">for</span> _ <span class="keyword">in</span> range(max_rounds):
        <span class="comment"># Step 2: Self-critique</span>
        critique = client.chat.completions.create(
            model=<span class="string">"gpt-4o"</span>,
            messages=[
                {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: task},
                {<span class="string">"role"</span>: <span class="string">"assistant"</span>, <span class="string">"content"</span>: response},
                {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Review your answer. What's wrong or missing? Be specific."</span>}
            ]
        ).choices[<span class="number">0</span>].message.content

        <span class="keyword">if</span> <span class="string">"looks correct"</span> <span class="keyword">in</span> critique.lower() <span class="keyword">or</span> <span class="string">"no issues"</span> <span class="keyword">in</span> critique.lower():
            <span class="keyword">break</span>

        <span class="comment"># Step 3: Revise</span>
        response = client.chat.completions.create(
            model=<span class="string">"gpt-4o"</span>,
            messages=[
                {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: task},
                {<span class="string">"role"</span>: <span class="string">"assistant"</span>, <span class="string">"content"</span>: response},
                {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: f<span class="string">"Fix these issues: {critique}"</span>}
            ]
        ).choices[<span class="number">0</span>].message.content

    <span class="keyword">return</span> response
</div>
            <h3>ReAct Pattern from Scratch</h3>
            <div class="code-block"><span class="keyword">import</span> json, re
<span class="keyword">from</span> openai <span class="keyword">import</span> OpenAI

client = OpenAI()

TOOLS = {
    <span class="string">"search"</span>: <span class="keyword">lambda</span> q: f<span class="string">"[Search result for '{q}': Paris is the capital of France]"</span>,
    <span class="string">"calculate"</span>: <span class="keyword">lambda</span> expr: str(eval(expr))
}

SYSTEM = <span class="string">"""You are a ReAct agent. At each step output ONLY:
Thought: your reasoning
Action: {"tool": "search"|"calculate", "input": "..."}
Or when done:
Thought: I have the answer
Final Answer: <your answer>"""</span>

<span class="keyword">def</span> <span class="function">react_agent</span>(question: str, max_steps: int = <span class="number">5</span>) -> str:
    messages = [
        {<span class="string">"role"</span>: <span class="string">"system"</span>, <span class="string">"content"</span>: SYSTEM},
        {<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: question}
    ]
    <span class="keyword">for</span> step <span class="keyword">in</span> range(max_steps):
        response = client.chat.completions.create(
            model=<span class="string">"gpt-4o"</span>, messages=messages
        ).choices[<span class="number">0</span>].message.content
        messages.append({<span class="string">"role"</span>: <span class="string">"assistant"</span>, <span class="string">"content"</span>: response})

        <span class="keyword">if</span> <span class="string">"Final Answer:"</span> <span class="keyword">in</span> response:
            <span class="keyword">return</span> response.split(<span class="string">"Final Answer:"</span>)[<span class="number">1</span>].strip()

        action_match = re.search(r<span class="string">'Action:\s*(\{.*?\})'</span>, response, re.DOTALL)
        <span class="keyword">if</span> action_match:
            action = json.loads(action_match.group(<span class="number">1</span>))
            observation = TOOLS[action[<span class="string">"tool"</span>]](action[<span class="string">"input"</span>])
            messages.append({<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: f<span class="string">"Observation: {observation}"</span>})
    <span class="keyword">return</span> <span class="string">"Max steps reached"</span>

print(react_agent(<span class="string">"What is the capital of France and what is 15 * 23?"</span>))
</div>
        </div>`,
    interview: `
        <div class="section">
            <h2>🎯 Agentic Design Patterns — Interview Questions</h2>
            <div class="interview-box"><strong>Q1: What are the 5 agentic AI design patterns?</strong><p><strong>Answer:</strong> (1) Reflection — agent critiques and revises its own output. (2) Tool Use — agent calls external functions/APIs. (3) Planning — agent creates a multi-step plan before execution. (4) Multi-Agent — multiple agents collaborate, each specializing in different sub-tasks. (5) Memory — agent persists and retrieves information across interactions. These patterns are often combined: e.g., a planning agent that uses tools and reflects on results.</p></div>
            <div class="interview-box"><strong>Q2: Explain the ReAct loop.</strong><p><strong>Answer:</strong> ReAct (Reasoning + Acting) interleaves thinking with tool use: (1) Thought — model reasons about what to do next. (2) Action — model calls a tool with specific arguments. (3) Observation — tool result is appended to context. This loop repeats until the model outputs "Final Answer." The key insight is that grounding reasoning in real tool observations prevents hallucination chains and enables multi-step problem solving.</p></div>
            <div class="interview-box"><strong>Q3: What are the 5 levels of agentic AI systems?</strong><p><strong>Answer:</strong> Level 0: No AI. Level 1: AI-Assisted (suggests, human acts). Level 2: Co-pilot (AI acts, human approves). Level 3: AI Agent (autonomous in defined scope). Level 4: Agentic AI (multi-step autonomous, self-correcting). Level 5: AI Workforce (swarm of agents, long-horizon autonomy). Most production systems today are Level 2-3; Level 4-5 is emerging.</p></div>
        </div>`
};

MODULE_CONTENT['agent-protocols'] = {
    concepts: `
        <div class="section">
            <h2>📡 Agent Protocol Landscape</h2>
            <div class="info-box">
                <div class="box-title">Why Do Agent Protocols Matter?</div>
                <div class="box-content">As AI agents move from single-LLM apps to interconnected systems, they need <strong>standardized communication protocols</strong>. Just like HTTP standardized web communication, agent protocols standardize how agents discover capabilities, delegate tasks, share context, and return results — enabling interoperability across vendors and frameworks.</div>
            </div>

            <h3>The 4 Key Protocols You Must Know</h3>
            <table>
                <tr><th>Protocol</th><th>Created By</th><th>Purpose</th><th>Layer</th></tr>
                <tr><td><strong>MCP</strong> (Model Context Protocol)</td><td>Anthropic</td><td>Standardize how LLMs connect to tools & data sources</td><td>Tool/Data Layer</td></tr>
                <tr><td><strong>A2A</strong> (Agent-to-Agent)</td><td>Google</td><td>Standardize how agents discover and delegate to other agents</td><td>Agent Communication Layer</td></tr>
                <tr><td><strong>AG-UI</strong> (Agent-User Interaction)</td><td>CopilotKit</td><td>Standardize how agents communicate state to frontends</td><td>Presentation Layer</td></tr>
                <tr><td><strong>Agent Protocol</strong></td><td>AI Engineer Foundation</td><td>OpenAPI spec for agent task management</td><td>Task Management Layer</td></tr>
            </table>

            <h3>MCP — Model Context Protocol (Deep Dive)</h3>
            <p>MCP solves the N×M integration problem. Before MCP: each LLM app needed custom integrations to each tool/database. With MCP: each tool builds one MCP Server; each LLM app builds one MCP Client. They interoperate automatically.</p>
            <table>
                <tr><th>MCP Component</th><th>Role</th><th>Analogy</th></tr>
                <tr><td><strong>MCP Host</strong></td><td>App running the LLM (Claude Desktop, Cursor, your app)</td><td>Web Browser</td></tr>
                <tr><td><strong>MCP Client</strong></td><td>Protocol client inside the host</td><td>HTTP Client</td></tr>
                <tr><td><strong>MCP Server</strong></td><td>Exposes tools, resources, prompts</td><td>Web Server</td></tr>
            </table>
            <p>MCP Servers expose 3 primitives: <strong>Tools</strong> (executable functions), <strong>Resources</strong> (read-only data), <strong>Prompts</strong> (reusable templates). Transport: stdio (local) or HTTP+SSE (remote).</p>

            <h3>A2A — Agent-to-Agent Protocol (Google)</h3>
            <p>A2A allows agents built by different vendors to collaborate. An agent publishes an <strong>Agent Card</strong> (JSON) describing its capabilities. Other agents discover it and delegate tasks using standardized request/response schemas. Key concepts: <strong>Task</strong> (unit of work), <strong>Artifact</strong> (output produced), <strong>Part</strong> (typed content: text, file, data). A2A is transport-agnostic (HTTP, gRPC, etc.).</p>

            <h3>AG-UI — Agent-User Interaction Protocol</h3>
            <p>AG-UI standardizes the real-time event stream between agents and frontend UIs. Instead of building custom WebSocket logic for every agent app, AG-UI defines: <strong>streaming text events</strong>, <strong>tool call events</strong>, <strong>state sync events</strong>, and <strong>lifecycle events</strong> (run_start, run_end). This means any AG-UI-compatible agent can power any AG-UI-compatible frontend.</p>

            <h3>When to Use Which Protocol?</h3>
            <div class="comparison">
                <div class="comparison-good">
                    <strong>Use MCP when:</strong><br>
                    Connecting LLM to external tools, files, APIs, or databases. Building reusable tool servers that work with multiple LLM apps.
                </div>
                <div class="comparison-good">
                    <strong>Use A2A when:</strong><br>
                    Orchestrating multiple specialized agents from different teams/vendors. Delegating sub-tasks from one agent to another.
                </div>
            </div>
        </div>`,
    code: `
        <div class="section">
            <h2>💻 Agent Protocols — Code Examples</h2>
            <h3>Building a Simple MCP Server (Python SDK)</h3>
            <div class="code-block"><span class="preprocessor"># pip install mcp</span>
<span class="keyword">from</span> mcp.server.fastmcp <span class="keyword">import</span> FastMCP
<span class="keyword">import</span> httpx

<span class="comment"># Create an MCP server</span>
mcp = FastMCP(<span class="string">"Weather Server"</span>)

<span class="keyword">@mcp.tool()</span>
<span class="keyword">async def</span> <span class="function">get_weather</span>(city: str) -> str:
    <span class="string">"""Get current weather for a city."""</span>
    <span class="keyword">async with</span> httpx.AsyncClient() <span class="keyword">as</span> client:
        resp = <span class="keyword">await</span> client.get(
            f<span class="string">"https://wttr.in/{city}?format=3"</span>
        )
        <span class="keyword">return</span> resp.text

<span class="keyword">@mcp.resource(</span><span class="string">"weather://cities"</span><span class="keyword">)</span>
<span class="keyword">def</span> <span class="function">list_cities</span>() -> str:
    <span class="string">"""List of supported cities."""</span>
    <span class="keyword">return</span> <span class="string">"London, Paris, Tokyo, New York, Sydney"</span>

<span class="keyword">@mcp.prompt()</span>
<span class="keyword">def</span> <span class="function">weather_report_prompt</span>(city: str) -> str:
    <span class="string">"""Generate a professional weather report."""</span>
    <span class="keyword">return</span> f<span class="string">"Write a professional weather report for {city} in 3 sentences."</span>

<span class="comment"># Run the server (stdio transport for local use)</span>
<span class="keyword">if</span> __name__ == <span class="string">"__main__"</span>:
    mcp.run()  <span class="comment"># Connects with Claude Desktop, Cursor, etc.</span>
</div>
            <h3>A2A Agent Card Example</h3>
            <div class="code-block"><span class="comment"># Agent Card: JSON descriptor other agents discover</span>
agent_card = {
    <span class="string">"name"</span>: <span class="string">"CodeReviewAgent"</span>,
    <span class="string">"description"</span>: <span class="string">"Reviews Python code for bugs, style, and security"</span>,
    <span class="string">"version"</span>: <span class="string">"1.0"</span>,
    <span class="string">"capabilities"</span>: {
        <span class="string">"streaming"</span>: <span class="keyword">True</span>,
        <span class="string">"pushNotifications"</span>: <span class="keyword">False</span>,
    },
    <span class="string">"skills"</span>: [
        {
            <span class="string">"id"</span>: <span class="string">"review_code"</span>,
            <span class="string">"name"</span>: <span class="string">"Review Code"</span>,
            <span class="string">"description"</span>: <span class="string">"Review Python code and return issues"</span>,
            <span class="string">"inputModes"</span>: [<span class="string">"text"</span>],
            <span class="string">"outputModes"</span>: [<span class="string">"text"</span>]
        }
    ],
    <span class="string">"url"</span>: <span class="string">"https://myagent.example.com/a2a"</span>
}
</div>
        </div>`,
    interview: `
        <div class="section">
            <h2>🎯 Agent Protocol Landscape — Interview Questions</h2>
            <div class="interview-box"><strong>Q1: What problem does MCP solve?</strong><p><strong>Answer:</strong> MCP solves the N×M integration problem. Before MCP, if you had N LLM applications and M tools/data sources, you needed N×M custom integrations. With MCP, each tool builds one MCP Server and each app builds one MCP Client — reducing to N+M integrations. It standardizes what tools expose (functions, data, templates) and how LLMs request them.</p></div>
            <div class="interview-box"><strong>Q2: How does A2A differ from MCP?</strong><p><strong>Answer:</strong> MCP is about an LLM connecting to tools and data sources — it's a vertical protocol (LLM → tools). A2A is about agents communicating with other agents — it's a horizontal protocol (agent ↔ agent). You'd use MCP to give your agent access to a database, and A2A to have your orchestrator agent delegate a coding task to a specialized code-writing agent.</p></div>
            <div class="interview-box"><strong>Q3: What is AG-UI and why does it matter?</strong><p><strong>Answer:</strong> AG-UI (Agent-User Interaction protocol) standardizes the streaming event protocol between AI agents and their frontend UIs. Without AG-UI, every agent app needs custom WebSocket or SSE logic for showing streaming responses, tool call status, and agent state in the UI. AG-UI defines a standard event schema so any compliant agent can power any compliant frontend — enabling a plugin-like ecosystem of agent UIs.</p></div>
            <div class="interview-box"><strong>Q4: What are the 3 primitives exposed by an MCP Server?</strong><p><strong>Answer:</strong> (1) <strong>Tools</strong> — executable functions the LLM can call (like "search_web", "run_sql"). (2) <strong>Resources</strong> — read-only data the LLM can reference for context (like database schemas, file contents). (3) <strong>Prompts</strong> — reusable prompt templates the host app can offer to users. Each primitive serves a different role in the LLM's workflow.</p></div>
        </div>`
};

Object.assign(MODULE_CONTENT, {
    'prompt-engineering': {
        concepts: `
            <div class="section">
                <h2>✍️ Prompting for Agents — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ Why Specialized Prompting?</div>
                    <div class="box-content">Agentic AI requires different prompting strategies than simple chat. Instead of asking for a final answer, we prompt the model to <strong>reason, format data, and make decisions</strong>. The output must be perfectly parseable by our code.</div>
                </div>

                <h3>1. Three Prompting Techniques for Reasoning</h3>
                <p>To improve an LLM's logical accuracy and ability to plan, use these three structural techniques:</p>
                <table>
                    <tr><th>Technique</th><th>How It Works</th><th>Best For</th></tr>
                    <tr><td><strong>Chain of Thought (CoT)</strong></td><td>Adding "Think step by step" to the prompt, forcing the model to generate intermediate reasoning tokens before the final answer.</td><td>Math, logic, code, and agent planning.</td></tr>
                    <tr><td><strong>Tree of Thoughts (ToT)</strong></td><td>Prompting the model to generate multiple possible paths, evaluate them, and perform search (BFS/DFS) across paths to find the optimal solution.</td><td>Complex multi-step reasoning where backtracking is needed.</td></tr>
                    <tr><td><strong>Self-Consistency</strong></td><td>Prompting the model to generate N different CoT sequences, then taking the majority vote for the final answer.</td><td>Highly reliable fact extraction and math problems.</td></tr>
                </table>

                <h3>2. Verbalized Sampling (Agent "Thinking Out Loud")</h3>
                <p>When an agent selects a tool to call, it can easily make a mistake if it just outputs the tool name directly. <strong>Verbalized Sampling</strong> requires the agent to explicitly output its reasoning process <em>before</em> outputting the tool call or action.</p>
                <div class="callout tip">
                    <div class="callout-title">💡 Why Verbalized Sampling Works</div>
                    <p>Because LLMs generate tokens autoregressively (left-to-right), the tokens representing the "thought" become part of the context for generating the "tool call" tokens. The tool call intrinsically becomes more accurate because it's physically conditioned on a logical rationale.</p>
                </div>

                <h3>3. JSON Prompting for LLMs</h3>
                <p>Agents and orchestrators need structured data (JSON), not conversational text. If the JSON is malformed, the application crashes.</p>
                <ul>
                    <li><strong>Define a Schema:</strong> Always provide a strict schema in the system prompt. "Output JSON matching this typescript interface: { ... }"</li>
                    <li><strong>Use JSON Mode:</strong> Use <code>response_format: { type: "json_object" }</code> (OpenAI). The model is guaranteed to output valid JSON.</li>
                    <li><strong>Stop Sequences:</strong> For older or open models, using <code>}</code> as a stop sequence guarantees no trailing text.</li>
                    <li><strong>Pre-filling the Assistant:</strong> Append <code>{</code> to the end of your prompt so the model is forced to start generating JSON keys instantly without saying "Here is the JSON...".</li>
                </ul>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Prompting for Agents — Code Examples</h2>

                <h3>1. Verbalized Sampling Prompt Template</h3>
                <div class="code-block"><span class="keyword">const</span> system_prompt = <span class="string">\`You are a sophisticated AI agent with access to tools.
When given a task, you MUST use the following format:

Thought: Consider what you need to do, step by step. Which tool is needed?
Action: The name of the tool to use (e.g. "search_web", "calculate")
Action Input: The arguments for the tool in valid JSON.

You MUST articulate your Thought before your Action.\`</span></div>

                <h3>2. Forcing JSON on Open Models</h3>
                <div class="code-block"><span class="keyword">import</span> { pipeline } <span class="keyword">from</span> <span class="string">"@huggingface/transformers"</span>;

<span class="comment">// Provide schema and force it to start with {</span>
<span class="keyword">const</span> prompt = <span class="string">"Extract name and age. Return JSON: {\"name\": string, \"age\": number}\n\nText: John is 25.\nOutput:\n{"</span>;

<span class="keyword">const</span> generator = <span class="keyword">await</span> pipeline(<span class="string">"text-generation"</span>, <span class="string">"meta-llama/Llama-3.2-1B-Instruct"</span>);
<span class="keyword">const</span> out = <span class="keyword">await</span> generator(prompt, {
    max_new_tokens: <span class="number">50</span>,
    stop_strings: [<span class="string">"}"</span>] <span class="comment">// Stop generation exactly when JSON closes</span>
});
<span class="keyword">const</span> raw = <span class="string">"{"</span> + out[0].generated_text; <span class="comment">// Prepend the '{' that we forced</span>
<span class="keyword">const</span> json = JSON.parse(raw);</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Prompt Engineering — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: Why does Chain of Thought work?</strong><p><strong>Answer:</strong> It provides additional computational steps (tokens) for the model to process logic. Since an LLM spends a fixed amount of computation per token, forcing it to generate a 50-token thought process before answering allocates 50x more computation to solving the problem than just answering immediately.</p></div>
                <div class="interview-box"><strong>Q2: How is JSON Prompting different from OpenAI Function Calling?</strong><p><strong>Answer:</strong> JSON prompting is done via the text prompt and relies on the model's instruction following (good for open models). Function/Tool calling is a native API feature where the provider fine-tunes the model explicitly to output arguments matching a schema via constrained decoding, ensuring much higher reliability.</p></div>
            </div>`
    },
    'llm-optimization': {
        concepts: `
            <div class="section">
                <h2>🗜️ LLM Optimization — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ Why Optimization Matters</div>
                    <div class="box-content">LLMs are massively compute- and memory-bound. A 70B parameter model in FP16 takes 140GB of VRAM just to load. Optimizing how models are compressed and how inference runs determines whether an application is economically viable.</div>
                </div>

                <h3>1. Model Compression</h3>
                <p>Compression reduces the memory footprint and increases memory bandwidth, leading to faster token generation.</p>
                <table>
                    <tr><th>Technique</th><th>How It Works</th><th>Tradeoff</th></tr>
                    <tr><td><strong>Quantization (PTQ)</strong></td><td>Convert parameters from FP16 (16-bit) to INT8 or INT4. Ex: GGUF, AWQ, GPTQ.</td><td>Slight accuracy loss, massive speed/memory gains.</td></tr>
                    <tr><td><strong>Pruning</strong></td><td>Set near-zero weights to exactly zero, creating sparse matrices.</td><td>Requires specialized hardware for sparse acceleration.</td></tr>
                    <tr><td><strong>Knowledge Distillation</strong></td><td>Train a smaller model (student) to match the probability distribution of a large model (teacher).</td><td>High upfront compute cost to train the student.</td></tr>
                </table>

                <h3>2. Regular ML Inference vs LLM Inference</h3>
                <div class="comparison">
                    <div class="comparison-bad"><strong>Regular ML (e.g., ResNet):</strong> One input → One forward pass → One output. Compute-bound (matrix multiplication speed is the bottleneck). Highly batchable.</div>
                    <div class="comparison-good"><strong>LLM Inference:</strong> Autoregressive. One input → forward pass → 1 token → append token → forward pass → 1 token. Memory-bandwidth bound (reading huge weights from HBM to SRAM for every single token).</div>
                </div>

                <h3>3. KV Caching in LLMs</h3>
                <p>During generation, each new token needs to pay attention to all past tokens. Recomputing the Key (K) and Value (V) matrices for all past tokens for every new token generation is <strong>O(N²)</strong> and terribly slow.</p>
                <p><strong>KV Caching</strong> stores the K and V tensors for all past tokens in GPU memory. For the next step, only the Q, K, V for the <em>newest token</em> are computed, and attention is run against the cached past. This reduces computation to <strong>O(N)</strong> per step.</p>
                <div class="callout warning">
                    <div class="callout-title">⚠️ The KV Cache Bottleneck</div>
                    <p>While KV caching solves the compute problem, it introduces a memory problem. A 100K context window across high batch sizes can cause the KV cache to consume more GPU RAM than the model weights themselves! This is why techniques like <strong>PagedAttention</strong> (vLLM) and <strong>GQA (Grouped Query Attention)</strong> were invented.</p>
                </div>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 LLM Optimization — Code Examples</h2>

                <h3>1. GGUF Quantization via Llama.cpp</h3>
                <div class="code-block"><span class="comment"># Convert HF model to 4-bit GGUF using llama.cpp</span>
python llama.cpp/convert_hf_to_gguf.py models/Llama-3-8B \
  --outfile models/llama3-8b.gguf \
  --outtype q4_k_m

<span class="comment"># Run highly optimized inference locally in C++</span>
./llama.cpp/main -m models/llama3-8b.gguf -n 256 -p "Explain KV caching"</div>

                <h3>2. AWQ Quantization in Python</h3>
                <div class="code-block"><span class="keyword">from</span> awq <span class="keyword">import</span> AutoAWQForCausalLM
<span class="keyword">from</span> transformers <span class="keyword">import</span> AutoTokenizer

model_path = <span class="string">"meta-llama/Llama-3-8B"</span>
quant_path = <span class="string">"llama-3-8b-awq"</span>
quant_config = { <span class="string">"zero_point"</span>: True, <span class="string">"q_group_size"</span>: 128, <span class="string">"w_bit"</span>: 4 }

<span class="comment"># Quantize and save</span>
model = AutoAWQForCausalLM.from_pretrained(model_path)
tokenizer = AutoTokenizer.from_pretrained(model_path)
model.quantize(tokenizer, quant_config=quant_config)
model.save_quantized(quant_path)
tokenizer.save_pretrained(quant_path)</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 LLM Optimization — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What is the difference between compute-bound and memory-bandwidth bound?</strong><p><strong>Answer:</strong> Compute-bound means the GPU spends all its time doing math (matrix multiplications). Memory-bandwidth bound means the math is easy, but the GPU spends all its time waiting for weights to be copied from High Bandwidth Memory (HBM) to on-chip SRAM. LLM prefill (reading the prompt) is compute-bound, but decoding (generating tokens one by one) is memory-bandwidth bound.</p></div>
                <div class="interview-box"><strong>Q2: Assume you use vLLM. What is PagedAttention?</strong><p><strong>Answer:</strong> Normally, KV cache is pre-allocated continuously in GPU memory. Because output lengths are unknown, frameworks over-allocate memory, wasting up to 60%. PagedAttention divides the KV cache into small blocks (pages) and allocates them dynamically, like virtual memory in an OS. This allows near-zero waste and 2-4x higher concurrency (batching).</p></div>
            </div>`
    },
    'llm-observability': {
        concepts: `
            <div class="section">
                <h2>🔭 LLM Observability — Complete Deep Dive</h2>
                <div class="info-box">
                    <div class="box-title">⚡ Evaluation vs. Observability</div>
                    <div class="box-content"><strong>Evaluation</strong> happens offline or asynchronously to check if a model meets a standard (metrics, RAGAS scores). <strong>Observability</strong> is runtime monitoring of production systems to understand exactly what the model did, how long it took, what tools it called, and how much it cost.</div>
                </div>

                <h3>1. Key Observability Metrics</h3>
                <ul>
                    <li><strong>Latency:</strong> Time-to-First-Token (TTFT), Tokens-Per-Second (TPS), Total request duration.</li>
                    <li><strong>Cost & Usage:</strong> Token tracking (Prompt tokens vs Completion tokens), dollar cost calculation per provider.</li>
                    <li><strong>Trace Visualization:</strong> For agents and chains, tracking the exact tree of execution. (User Prompt -> Retriever -> Tool Call 1 -> Tool Call 2 -> Final LLM Answer).</li>
                    <li><strong>Quality/Feedback:</strong> Capturing user thumbs up/down, implicit feedback (copy-pasting the result).</li>
                </ul>

                <h3>2. Tracing Implementations</h3>
                <p>Standard software uses APM (Datadog, New Relic) for tracing. LLMs require specialized APMs (Langfuse, LangSmith, Helicone, Opik) because the payload sizes are huge and the dependencies (prompts, tool results) are non-standard.</p>
                <table>
                    <tr><th>Platform</th><th>Best For</th></tr>
                    <tr><td><strong>LangSmith</strong></td><td>Deep LangChain integration and easy local debugging.</td></tr>
                    <tr><td><strong>Langfuse</strong></td><td>Open-source, framework-agnostic tracing with a great UI.</td></tr>
                    <tr><td><strong>Helicone</strong></td><td>Proxy-based observability (just change the base URL, no SDK needed).</td></tr>
                    <tr><td><strong>Opik (by Comet)</strong></td><td>Agent optimization and evaluation natively integrated with traces.</td></tr>
                </table>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Observability — Code Examples</h2>

                <h3>1. Langfuse Tracing with OpenAI</h3>
                <div class="code-block"><span class="keyword">from</span> langfuse.openai <span class="keyword">import</span> OpenAI
<span class="comment"># Drop-in replacement! Automatically traces all API calls.</span>

client = OpenAI()

response = client.chat.completions.create(
    model=<span class="string">"gpt-4o"</span>,
    messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">"Write a Python script."</span>}],
    name=<span class="string">"script-generation"</span>, <span class="comment"># Optional trace grouping</span>
    metadata={<span class="string">"user_id"</span>: <span class="string">"123"</span>, <span class="string">"env"</span>: <span class="string">"production"</span>}
)</div>

                <h3>2. LangSmith Tracing via Decorators</h3>
                <div class="code-block"><span class="keyword">from</span> langsmith <span class="keyword">import</span> traceable
<span class="keyword">from</span> openai <span class="keyword">import</span> Client

client = Client()

<span class="preprocessor">@traceable</span>(name=<span class="string">"RAG Pipeline"</span>)
<span class="keyword">def</span> <span class="function">my_rag_agent</span>(query):
    <span class="comment"># All LLM calls inside this function get nested inside the "RAG Pipeline" trace segment</span>
    context = retrieve_docs(query)  <span class="comment"># Can add @traceable to this too</span>
    
    resp = client.chat.completions.create(
        model=<span class="string">"gpt-4o"</span>,
        messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: <span class="string">f"Context: {context}\nQuery: {query}"</span>}]
    )
    <span class="keyword">return</span> resp.choices[<span class="number">0</span>].message.content</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Observability — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: What is Time-To-First-Token (TTFT) and why does it matter?</strong><p><strong>Answer:</strong> TTFT measures the latency from the moment the user sends the request until the first token streams back to the client. In LLM applications, total end-to-end latency might be 5-10 seconds, which is unacceptable for UX. Streaming combined with low TTFT (&lt;1 second) creates the illusion of speed and keeps users engaged.</p></div>
                <div class="interview-box"><strong>Q2: Why use a proxy like Helicone over an SDK like LangSmith?</strong><p><strong>Answer:</strong> A proxy requires ZERO code changes — you simply change the API base URL from <code>api.openai.com</code> to <code>oai.hconeai.com</code> and pass your proxy key in the header. It automatically logs all prompts, responses, costs, and latencies. However, an SDK (like Langfuse/LangSmith) is required if you want deep, nested trace trees for complex agents (e.g., seeing exactly which step in a 10-step LangGraph flow failed).</p></div>
            </div>`
    },
    'multiagent': {
        concepts: `
            <div class="section">
                <h2>🕸️ Multi-Agent Systems (MAS)</h2>
                <div class="info-box">
                    <div class="box-title">Why Multiple Agents?</div>
                    <div class="box-content">Monolithic agents (one LLM with one giant prompt) are brittle. Specialized agents that collaborate reduce friction, improve accuracy, and enable parallel processing. MAS is about <strong>orchestration</strong> — how agents talk to each other to solve big tasks.</div>
                </div>

                <h3>7 Core Patterns of Multi-Agent Orchestration</h3>
                <table>
                    <tr><th>Pattern</th><th>How It Works</th><th>Best For</th></tr>
                    <tr><td><strong>1. Parallel</strong></td><td>Tasks run concurrently across specialists (e.g., searcher + coder).</td><td>Reducing latency in complex pipelines.</td></tr>
                    <tr><td><strong>2. Sequential</strong></td><td>Step-by-step handoff (e.g., Coder → Reviewer → Deployer).</td><td>ETL chains, code development, linear workflows.</td></tr>
                    <tr><td><strong>3. Loop</strong></td><td>Continuous refinement until quality threshold is met.</td><td>Report generation, creative writing, proofreading.</td></tr>
                    <tr><td><strong>4. Router</strong></td><td>Controller agent directs task to the right specialist.</td><td>Customer support (billing vs technical vs sales).</td></tr>
                    <tr><td><strong>5. Aggregator</strong></td><td>Many agents form opinions; one central agent merges them.</td><td>Consensus voting, RAG retrieval fusion.</td></tr>
                    <tr><td><strong>6. Network</strong></td><td>No hierarchy; agents communicate freely (peer-to-peer).</td><td>Simulations, games, collective brainstorming.</td></tr>
                    <tr><td><strong>7. Hierarchical</strong></td><td>Planner/Manager agent delegates to workers and tracks progress.</td><td>Large enterprise projects with many sub-tasks.</td></tr>
                </table>

                <h3>A2A: The Protocol for Teamwork</h3>
                <p><strong>Agent-to-Agent (A2A)</strong> protocol standardizes how these agents exchange context and instructions. Instead of sharing a global state, they exchange <strong>Agent Cards</strong> and <strong>Task Payloads</strong>. This allows an agent built in CrewAI to delegate a task to an agent built in LangGraph.</p>

                <div class="callout tip">
                    <div class="callout-title">💡 Minimizing Friction</div>
                    <p>When picking a pattern, prioritize minimizing communication overhead. 10 agents isn't better than 2 if they duplicate work. The system should feel smarter than its individual parts.</p>
                </div>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Multi-Agent — Code Examples</h2>
                <h3>Simple Router Pattern with LiteLLM</h3>
                <div class="code-block"><span class="keyword">from</span> litellm <span class="keyword">import</span> completion

<span class="keyword">def</span> <span class="function">router_agent</span>(query):
    <span class="comment"># Intent classification</span>
    intent = completion(
        model=<span class="string">"gpt-4o-mini"</span>,
        messages=[{<span class="string">"role"</span>: <span class="string">"user"</span>, <span class="string">"content"</span>: f<span class="string">"Classify: {query}. Labels: [CODING, FINANCE, GENERAL]"</span>}]
    ).choices[0].message.content

    <span class="keyword">if</span> <span class="string">"CODING"</span> <span class="keyword">in</span> intent:
        <span class="keyword">return</span> coding_specialist(query)
    <span class="keyword">elif</span> <span class="string">"FINANCE"</span> <span class="keyword">in</span> intent:
        <span class="keyword">return</span> finance_specialist(query)
    <span class="keyword">else</span>:
        <span class="keyword">return</span> general_agent(query)</div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Multi-Agent — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: Parallel vs Sequential orchestration?</strong><p><strong>Answer:</strong> Parallel is for independent tasks (data extraction + web search) to reduce latency. Sequential is for dependent tasks where step B needs output of step A (code writing then code review). Use parallel for scale, sequential for quality-controlled pipelines.</p></div>
                <div class="interview-box"><strong>Q2: What is the Hierarchical pattern?</strong><p><strong>Answer:</strong> It mimics a corporate structure: a Manager/Planner agent receives the high-level goal, breaks it into sub-tasks, and delegates them to specialized Worker agents. The Manager tracks state and makes the final quality check. Best for complex, ambiguous projects.</p></div>
            </div>`
    },
    'tools': {
        concepts: `
            <div class="section">
                <h2>🔧 Function Calling & Tools</h2>
                <div class="info-box">
                    <div class="box-title">Tools: The Hands of the LLM</div>
                    <div class="box-content">An LLM without tools is just a talker. Tools give LLMs <strong>agency</strong>. Function calling allows models to generate structured arguments for functions you've defined, enabling real-world actions like database queries, web searches, or code execution.</div>
                </div>

                <h3>1. The Function Calling Lifecycle</h3>
                <p>1. <strong>Definition:</strong> Send tool schemas (JSON) to LLM. 2. <strong>Selection:</strong> LLM realizes it needs a tool and outputs <code>tool_calls</code>. 3. <strong>Execution:</strong> Your code runs the tool locally. 4. <strong>Feedback:</strong> Result is sent back to LLM. 5. <strong>Final Output:</strong> LLM uses result to answer user.</p>

                <h3>2. JSON Prompting vs Native Tooling</h3>
                <p><strong>JSON Prompting:</strong> Manually instructing the model to output JSON (used for open models). <strong>Native Tooling:</strong> Using provider APIs (OpenAI/Claude) which use <strong>constrained decoding</strong> for 99.9% reliability.</p>

                <h3>3. Verbalized Sampling</h3>
                <p>Forcing the agent to generate a "Thought:" block before the "Action:" block. This conditions the tool selection on a logical premise, significantly reducing errors in choosing the wrong tool or arguments.</p>
            </div>`,
        code: `
            <div class="section">
                <h2>💻 Tools — Code Examples</h2>
                <h3>OpenAI Native Tool Call</h3>
                <div class="code-block"><span class="keyword">tools</span> = [{
    <span class="string">"type"</span>: <span class="string">"function"</span>,
    <span class="string">"function"</span>: {
        <span class="string">"name"</span>: <span class="string">"get_stock_price"</span>,
        <span class="string">"parameters"</span>: {
            <span class="string">"type"</span>: <span class="string">"object"</span>,
            <span class="string">"properties"</span>: {<span class="string">"symbol"</span>: {<span class="string">"type"</span>: <span class="string">"string"</span>}}
        }
    }
}]
<span class="comment"># Pass this to chat.completions.create(..., tools=tools)</span></div>
            </div>`,
        interview: `
            <div class="section">
                <h2>🎯 Tools — Interview Questions</h2>
                <div class="interview-box"><strong>Q1: Why use Verbalized Sampling in tool calling?</strong><p><strong>Answer:</strong> It forces the model to articulate a rationale *before* picking a tool. Since tokens are generated left-to-right, the tool selection becomes conditioned on the reasoning, which increases precision, especially when multiple similar tools exist.</p></div>
            </div>`
    }
});


// ─── Dashboard Orchestration ────────────────────────────────────────────────
let activeFilter = 'All';

function renderDashboard(query = '') {
    const grid = document.getElementById('moduleGrid');
    const noResults = document.getElementById('noResults');
    const q = query.toLowerCase().trim();

    const filtered = modules.filter(m => {
        const matchFilter = activeFilter === 'All' || m.category === activeFilter;
        const matchSearch = !q || m.title.toLowerCase().includes(q) || m.desc.toLowerCase().includes(q) || m.category.toLowerCase().includes(q);
        return matchFilter && matchSearch;
    });

    const statModules = document.getElementById('statModules');
    if (statModules) statModules.textContent = modules.length;

    if (!grid) return;

    if (filtered.length === 0) {
        grid.innerHTML = '';
        if (noResults) noResults.style.display = 'block';
        return;
    }
    if (noResults) noResults.style.display = 'none';

    grid.innerHTML = filtered.map((m, i) => `
        <div class="card ${m.catClass} stagger stagger-${(i % 8) + 1}" onclick="openModule('${m.id}')">
            <span class="card-badge">${m.category}</span>
            <div class="card-icon">${m.icon}</div>
            <h3>${m.title}</h3>
            <p>${m.desc}</p>
            <div class="card-arrow">→</div>
        </div>
    `).join('');
}

function setFilter(cat, btn) {
    activeFilter = cat;
    document.querySelectorAll('.filter-btn').forEach(b => b.classList.remove('active'));
    if (btn) btn.classList.add('active');
    
    const searchInput = document.getElementById('searchInput');
    renderDashboard(searchInput ? searchInput.value : '');
}

function filterModules(q) {
    renderDashboard(q);
}

function openModule(id) {
    const m = modules.find(x => x.id === id);
    const c = MODULE_CONTENT[id] || {};

    const dashboard = document.getElementById('dashboard');
    const container = document.getElementById('modulesContainer');
    
    if (dashboard) dashboard.classList.remove('active');
    if (container) {
        container.classList.add('active');
        container.innerHTML = `
            <div class="module active" id="module-${id}">
                <button class="btn-back" onclick="showDashboard()">← Back to Dashboard</button>
                <header>
                    <h1>${m.icon} ${m.title}</h1>
                    <p class="subtitle">${m.desc}</p>
                </header>

                <div class="tabs">
                    <button class="tab-btn active" onclick="switchTab(event,'concepts-${id}','${id}')">📖 Key Concepts</button>
                    <button class="tab-btn" onclick="switchTab(event,'code-${id}','${id}')">💻 Code Examples</button>
                    <button class="tab-btn" onclick="switchTab(event,'interview-${id}','${id}')">🎯 Interview Questions</button>
                </div>

                <div id="concepts-${id}" class="tab active">${c.concepts || '<p style="color:var(--text-muted);padding:40px 0">Technical concepts for this module are being finalized.</p>'}</div>
                <div id="code-${id}" class="tab">${c.code || '<p style="color:var(--text-muted);padding:40px 0">Implementation examples are being prepared.</p>'}</div>
                <div id="interview-${id}" class="tab">${c.interview || '<p style="color:var(--text-muted);padding:40px 0">Interview prep module is in development.</p>'}</div>
            </div>
        `;
    }
    window.scrollTo({ top: 0, behavior: 'smooth' });
}

function showDashboard() {
    const dashboard = document.getElementById('dashboard');
    const container = document.getElementById('modulesContainer');
    
    if (container) {
        container.classList.remove('active');
        container.innerHTML = '';
    }
    if (dashboard) dashboard.classList.add('active');
    
    // Refresh the dashboard state
    const searchInput = document.getElementById('searchInput');
    renderDashboard(searchInput ? searchInput.value : '');
}

function switchTab(event, tabId, moduleId) {
    const mod = document.getElementById('module-' + moduleId);
    if (!mod) return;
    
    mod.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));
    mod.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
    
    const targetTab = document.getElementById(tabId);
    if (targetTab) targetTab.classList.add('active');
    
    // Support clicking on icons inside buttons
    const btn = event.target.closest('.tab-btn');
    if (btn) btn.classList.add('active');
}

// Initial Render
document.addEventListener('DOMContentLoaded', () => {
    renderDashboard();
});