πŸ“¦ mneedham / LearnDataWithMark

πŸ“„ eval.ipynb Β· 1013 lines
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{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "93e325b8",
   "metadata": {},
   "source": [
    "# Evaluating retrieval in a RAG pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "732d3c4a-ff93-4140-88e9-347bc8cf6af0",
   "metadata": {
    "collapsed": true,
    "execution": {
     "iopub.execute_input": "2024-08-04T09:34:07.914423Z",
     "iopub.status.busy": "2024-08-04T09:34:07.913986Z",
     "iopub.status.idle": "2024-08-04T09:34:08.921591Z",
     "shell.execute_reply": "2024-08-04T09:34:08.921207Z",
     "shell.execute_reply.started": "2024-08-04T09:34:07.914408Z"
    },
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: ranx in ./.venv/lib/python3.12/site-packages (0.3.20)\n",
      "Requirement already satisfied: duckdb in ./.venv/lib/python3.12/site-packages (1.0.0)\n",
      "Requirement already satisfied: rich in ./.venv/lib/python3.12/site-packages (13.7.1)\n",
      "Requirement already satisfied: numpy in ./.venv/lib/python3.12/site-packages (from ranx) (2.0.1)\n",
      "Requirement already satisfied: numba>=0.54.1 in ./.venv/lib/python3.12/site-packages (from ranx) (0.60.0)\n",
      "Requirement already satisfied: pandas in ./.venv/lib/python3.12/site-packages (from ranx) (2.2.2)\n",
      "Requirement already satisfied: tabulate in ./.venv/lib/python3.12/site-packages (from ranx) (0.9.0)\n",
      "Requirement already satisfied: tqdm in ./.venv/lib/python3.12/site-packages (from ranx) (4.66.4)\n",
      "Requirement already satisfied: scipy>=1.8.0 in ./.venv/lib/python3.12/site-packages (from ranx) (1.14.0)\n",
      "Requirement already satisfied: ir-datasets in ./.venv/lib/python3.12/site-packages (from ranx) (0.5.8)\n",
      "Requirement already satisfied: orjson in ./.venv/lib/python3.12/site-packages (from ranx) (3.10.6)\n",
      "Requirement already satisfied: lz4 in ./.venv/lib/python3.12/site-packages (from ranx) (4.3.3)\n",
      "Requirement already satisfied: cbor2 in ./.venv/lib/python3.12/site-packages (from ranx) (5.6.4)\n",
      "Requirement already satisfied: seaborn in ./.venv/lib/python3.12/site-packages (from ranx) (0.13.2)\n",
      "Requirement already satisfied: fastparquet in ./.venv/lib/python3.12/site-packages (from ranx) (2024.5.0)\n",
      "Requirement already satisfied: markdown-it-py>=2.2.0 in ./.venv/lib/python3.12/site-packages (from rich) (3.0.0)\n",
      "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in ./.venv/lib/python3.12/site-packages (from rich) (2.18.0)\n",
      "Requirement already satisfied: mdurl~=0.1 in ./.venv/lib/python3.12/site-packages (from markdown-it-py>=2.2.0->rich) (0.1.2)\n",
      "Requirement already satisfied: llvmlite<0.44,>=0.43.0dev0 in ./.venv/lib/python3.12/site-packages (from numba>=0.54.1->ranx) (0.43.0)\n",
      "Requirement already satisfied: cramjam>=2.3 in ./.venv/lib/python3.12/site-packages (from fastparquet->ranx) (2.8.3)\n",
      "Requirement already satisfied: fsspec in ./.venv/lib/python3.12/site-packages (from fastparquet->ranx) (2024.6.1)\n",
      "Requirement already satisfied: packaging in ./.venv/lib/python3.12/site-packages (from fastparquet->ranx) (24.1)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in ./.venv/lib/python3.12/site-packages (from pandas->ranx) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in ./.venv/lib/python3.12/site-packages (from pandas->ranx) (2024.1)\n",
      "Requirement already satisfied: tzdata>=2022.7 in ./.venv/lib/python3.12/site-packages (from pandas->ranx) (2024.1)\n",
      "Requirement already satisfied: beautifulsoup4>=4.4.1 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (4.12.3)\n",
      "Requirement already satisfied: inscriptis>=2.2.0 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (2.5.0)\n",
      "Requirement already satisfied: lxml>=4.5.2 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (5.2.2)\n",
      "Requirement already satisfied: pyyaml>=5.3.1 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (6.0.1)\n",
      "Requirement already satisfied: requests>=2.22.0 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (2.32.3)\n",
      "Requirement already satisfied: trec-car-tools>=2.5.4 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (2.6)\n",
      "Requirement already satisfied: warc3-wet>=0.2.3 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (0.2.5)\n",
      "Requirement already satisfied: warc3-wet-clueweb09>=0.2.5 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (0.2.5)\n",
      "Requirement already satisfied: zlib-state>=0.1.3 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (0.1.6)\n",
      "Requirement already satisfied: ijson>=3.1.3 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (3.3.0)\n",
      "Requirement already satisfied: unlzw3>=0.2.1 in ./.venv/lib/python3.12/site-packages (from ir-datasets->ranx) (0.2.2)\n",
      "Requirement already satisfied: matplotlib!=3.6.1,>=3.4 in ./.venv/lib/python3.12/site-packages (from seaborn->ranx) (3.9.1)\n",
      "Requirement already satisfied: soupsieve>1.2 in ./.venv/lib/python3.12/site-packages (from beautifulsoup4>=4.4.1->ir-datasets->ranx) (2.5)\n",
      "Requirement already satisfied: contourpy>=1.0.1 in ./.venv/lib/python3.12/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn->ranx) (1.2.1)\n",
      "Requirement already satisfied: cycler>=0.10 in ./.venv/lib/python3.12/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn->ranx) (0.12.1)\n",
      "Requirement already satisfied: fonttools>=4.22.0 in ./.venv/lib/python3.12/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn->ranx) (4.53.1)\n",
      "Requirement already satisfied: kiwisolver>=1.3.1 in ./.venv/lib/python3.12/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn->ranx) (1.4.5)\n",
      "Requirement already satisfied: pillow>=8 in ./.venv/lib/python3.12/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn->ranx) (10.4.0)\n",
      "Requirement already satisfied: pyparsing>=2.3.1 in ./.venv/lib/python3.12/site-packages (from matplotlib!=3.6.1,>=3.4->seaborn->ranx) (3.1.2)\n",
      "Requirement already satisfied: six>=1.5 in ./.venv/lib/python3.12/site-packages (from python-dateutil>=2.8.2->pandas->ranx) (1.16.0)\n",
      "Requirement already satisfied: charset-normalizer<4,>=2 in ./.venv/lib/python3.12/site-packages (from requests>=2.22.0->ir-datasets->ranx) (3.3.2)\n",
      "Requirement already satisfied: idna<4,>=2.5 in ./.venv/lib/python3.12/site-packages (from requests>=2.22.0->ir-datasets->ranx) (3.7)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in ./.venv/lib/python3.12/site-packages (from requests>=2.22.0->ir-datasets->ranx) (2.2.2)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in ./.venv/lib/python3.12/site-packages (from requests>=2.22.0->ir-datasets->ranx) (2024.7.4)\n",
      "Requirement already satisfied: cbor>=1.0.0 in ./.venv/lib/python3.12/site-packages (from trec-car-tools>=2.5.4->ir-datasets->ranx) (1.0.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install ranx duckdb rich"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "90855bc0",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:33.258028Z",
     "iopub.status.busy": "2024-08-04T09:32:33.257316Z",
     "iopub.status.idle": "2024-08-04T09:32:35.468073Z",
     "shell.execute_reply": "2024-08-04T09:32:35.467649Z",
     "shell.execute_reply.started": "2024-08-04T09:32:33.257986Z"
    }
   },
   "outputs": [],
   "source": [
    "from ranx import Qrels, Run, evaluate, compare"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3a377f4b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:35.469589Z",
     "iopub.status.busy": "2024-08-04T09:32:35.469323Z",
     "iopub.status.idle": "2024-08-04T09:32:35.471891Z",
     "shell.execute_reply": "2024-08-04T09:32:35.471634Z",
     "shell.execute_reply.started": "2024-08-04T09:32:35.469572Z"
    }
   },
   "outputs": [],
   "source": [
    "qrels_dict = {\n",
    "  \"q_1\": { \"d_12\": 1 },\n",
    "  \"q_2\": { \"d_11\": 1 },\n",
    "  \"q_3\": { \"d_10\": 1 }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2e49c4de",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:35.472535Z",
     "iopub.status.busy": "2024-08-04T09:32:35.472417Z",
     "iopub.status.idle": "2024-08-04T09:32:36.461525Z",
     "shell.execute_reply": "2024-08-04T09:32:36.461053Z",
     "shell.execute_reply.started": "2024-08-04T09:32:35.472526Z"
    }
   },
   "outputs": [],
   "source": [
    "qrels = Qrels(qrels_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9476d750",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:36.462478Z",
     "iopub.status.busy": "2024-08-04T09:32:36.462279Z",
     "iopub.status.idle": "2024-08-04T09:32:36.465379Z",
     "shell.execute_reply": "2024-08-04T09:32:36.464640Z",
     "shell.execute_reply.started": "2024-08-04T09:32:36.462463Z"
    }
   },
   "outputs": [],
   "source": [
    "run1_dict = {\n",
    "  \"q_1\": { \"d_12\": 0.9, \"d_23\": 0.8, \"d_25\": 0.7 },\n",
    "  \"q_2\": { \"d_12\": 0.9, \"d_25\": 0.7, \"d_36\": 0.6},\n",
    "  \"q_3\": { \"d_10\": 1.0}\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ffda3756",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:36.467313Z",
     "iopub.status.busy": "2024-08-04T09:32:36.467106Z",
     "iopub.status.idle": "2024-08-04T09:32:36.631047Z",
     "shell.execute_reply": "2024-08-04T09:32:36.626035Z",
     "shell.execute_reply.started": "2024-08-04T09:32:36.467301Z"
    }
   },
   "outputs": [],
   "source": [
    "run1 = Run(run1_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e72024ec",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:36.631760Z",
     "iopub.status.busy": "2024-08-04T09:32:36.631643Z",
     "iopub.status.idle": "2024-08-04T09:32:37.995353Z",
     "shell.execute_reply": "2024-08-04T09:32:37.995046Z",
     "shell.execute_reply.started": "2024-08-04T09:32:36.631750Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\u001b[1;35mnp.float64\u001b[0m\u001b[1m(\u001b[0m\u001b[1;36m0.6666666666666666\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluate(qrels, run1, [\"hit_rate\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "12e88a4c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:37.996135Z",
     "iopub.status.busy": "2024-08-04T09:32:37.995865Z",
     "iopub.status.idle": "2024-08-04T09:32:37.998648Z",
     "shell.execute_reply": "2024-08-04T09:32:37.998161Z",
     "shell.execute_reply.started": "2024-08-04T09:32:37.996115Z"
    }
   },
   "outputs": [],
   "source": [
    "run2_dict = {\n",
    "  \"q_1\": { \"d_32\": 0.5, \"d_35\": 0.4},\n",
    "  \"q_2\": { \"d_12\": 0.9, \"d_11\": 0.8, \"d_35\": 0.4},\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "dd4eca7b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:37.999422Z",
     "iopub.status.busy": "2024-08-04T09:32:37.999257Z",
     "iopub.status.idle": "2024-08-04T09:32:38.001872Z",
     "shell.execute_reply": "2024-08-04T09:32:38.001512Z",
     "shell.execute_reply.started": "2024-08-04T09:32:37.999412Z"
    }
   },
   "outputs": [],
   "source": [
    "run2 = Run(run2_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "806dedda",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:38.002479Z",
     "iopub.status.busy": "2024-08-04T09:32:38.002328Z",
     "iopub.status.idle": "2024-08-04T09:32:38.183676Z",
     "shell.execute_reply": "2024-08-04T09:32:38.182004Z",
     "shell.execute_reply.started": "2024-08-04T09:32:38.002467Z"
    }
   },
   "outputs": [
    {
     "ename": "AssertionError",
     "evalue": "Qrels and Run query ids do not match. Pass `make_comparable=True` to add empty results for queries missing from the run and remove those not appearing in qrels.",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAssertionError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[9], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mevaluate\u001b[49m\u001b[43m(\u001b[49m\u001b[43mqrels\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun2\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43mhit_rate\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
      "File \u001b[0;32m~/projects/learndatawithmark/evaluate-rag/.venv/lib/python3.12/site-packages/ranx/meta/evaluate.py:135\u001b[0m, in \u001b[0;36mevaluate\u001b[0;34m(qrels, run, metrics, return_mean, return_std, threads, save_results_in_run, make_comparable)\u001b[0m\n\u001b[1;32m    132\u001b[0m     run \u001b[38;5;241m=\u001b[39m run\u001b[38;5;241m.\u001b[39mmake_comparable(qrels)\n\u001b[1;32m    134\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(qrels) \u001b[38;5;129;01min\u001b[39;00m [Qrels, \u001b[38;5;28mdict\u001b[39m] \u001b[38;5;129;01mand\u001b[39;00m \u001b[38;5;28mtype\u001b[39m(run) \u001b[38;5;129;01min\u001b[39;00m [Run, \u001b[38;5;28mdict\u001b[39m]:\n\u001b[0;32m--> 135\u001b[0m     \u001b[43mcheck_keys\u001b[49m\u001b[43m(\u001b[49m\u001b[43mqrels\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mrun\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m    137\u001b[0m _qrels \u001b[38;5;241m=\u001b[39m convert_qrels(qrels)\n\u001b[1;32m    138\u001b[0m _run \u001b[38;5;241m=\u001b[39m convert_run(run)\n",
      "File \u001b[0;32m~/projects/learndatawithmark/evaluate-rag/.venv/lib/python3.12/site-packages/ranx/meta/evaluate.py:60\u001b[0m, in \u001b[0;36mcheck_keys\u001b[0;34m(qrels, run)\u001b[0m\n\u001b[1;32m     59\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mcheck_keys\u001b[39m(qrels, run):\n\u001b[0;32m---> 60\u001b[0m     \u001b[38;5;28;01massert\u001b[39;00m (\n\u001b[1;32m     61\u001b[0m         qrels\u001b[38;5;241m.\u001b[39mkeys() \u001b[38;5;241m==\u001b[39m run\u001b[38;5;241m.\u001b[39mkeys()\n\u001b[1;32m     62\u001b[0m     ), \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mQrels and Run query ids do not match. Pass `make_comparable=True` to add empty results for queries missing from the run and remove those not appearing in qrels.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n",
      "\u001b[0;31mAssertionError\u001b[0m: Qrels and Run query ids do not match. Pass `make_comparable=True` to add empty results for queries missing from the run and remove those not appearing in qrels."
     ]
    }
   ],
   "source": [
    "evaluate(qrels, run2, [\"hit_rate\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "da436bbe",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:42.783537Z",
     "iopub.status.busy": "2024-08-04T09:32:42.782625Z",
     "iopub.status.idle": "2024-08-04T09:32:43.047268Z",
     "shell.execute_reply": "2024-08-04T09:32:43.046779Z",
     "shell.execute_reply.started": "2024-08-04T09:32:42.783511Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\u001b[1;35mnp.float64\u001b[0m\u001b[1m(\u001b[0m\u001b[1;36m0.3333333333333333\u001b[0m\u001b[1m)\u001b[0m"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "evaluate(qrels, run2, [\"hit_rate\"], make_comparable=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cee355db",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:50.864509Z",
     "iopub.status.busy": "2024-08-04T09:32:50.864024Z",
     "iopub.status.idle": "2024-08-04T09:32:50.890873Z",
     "shell.execute_reply": "2024-08-04T09:32:50.890331Z",
     "shell.execute_reply.started": "2024-08-04T09:32:50.864487Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "#    Model      Hit Rate\n",
       "---  -------  ----------\n",
       "a    run_1         \u001b[1;36m0.667\u001b[0m\n",
       "b    run_2         \u001b[1;36m0.333\u001b[0m"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare(\n",
    "    qrels,\n",
    "    runs=[run1, run2],\n",
    "    metrics=[\"hit_rate\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "da9eb04f",
   "metadata": {},
   "source": [
    "## Importing DuckDB database"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "52461f29",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:50.892356Z",
     "iopub.status.busy": "2024-08-04T09:32:50.892143Z",
     "iopub.status.idle": "2024-08-04T09:32:51.026023Z",
     "shell.execute_reply": "2024-08-04T09:32:51.025635Z",
     "shell.execute_reply.started": "2024-08-04T09:32:50.892345Z"
    }
   },
   "outputs": [],
   "source": [
    "import duckdb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "53bf996b",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:51.026640Z",
     "iopub.status.busy": "2024-08-04T09:32:51.026547Z",
     "iopub.status.idle": "2024-08-04T09:32:51.034342Z",
     "shell.execute_reply": "2024-08-04T09:32:51.033692Z",
     "shell.execute_reply.started": "2024-08-04T09:32:51.026632Z"
    }
   },
   "outputs": [],
   "source": [
    "con = duckdb.connect(\"evaluate_rag.duckdb\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "bb037cd8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:51.035363Z",
     "iopub.status.busy": "2024-08-04T09:32:51.035176Z",
     "iopub.status.idle": "2024-08-04T09:32:51.084865Z",
     "shell.execute_reply": "2024-08-04T09:32:51.084303Z",
     "shell.execute_reply.started": "2024-08-04T09:32:51.035350Z"
    }
   },
   "outputs": [],
   "source": [
    "con.sql(\"INSTALL httpfs\")\n",
    "con.sql(\"LOAD httpfs\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "70de973a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:51.087027Z",
     "iopub.status.busy": "2024-08-04T09:32:51.086799Z",
     "iopub.status.idle": "2024-08-04T09:32:52.289854Z",
     "shell.execute_reply": "2024-08-04T09:32:52.289023Z",
     "shell.execute_reply.started": "2024-08-04T09:32:51.087016Z"
    }
   },
   "outputs": [],
   "source": [
    "con.sql(\"\"\"\n",
    "ATTACH IF NOT EXISTS\n",
    "'https://raw.githubusercontent.com/mneedham/LearnDataWithMark/main/evaluate-rag/olympics.duckdb' AS olympics\n",
    "\"\"\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c40e9000",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:52.293206Z",
     "iopub.status.busy": "2024-08-04T09:32:52.292743Z",
     "iopub.status.idle": "2024-08-04T09:32:52.312224Z",
     "shell.execute_reply": "2024-08-04T09:32:52.311252Z",
     "shell.execute_reply.started": "2024-08-04T09:32:52.293185Z"
    }
   },
   "outputs": [],
   "source": [
    "con.sql(\"USE olympics\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "bd7c3a92",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:52.314278Z",
     "iopub.status.busy": "2024-08-04T09:32:52.313892Z",
     "iopub.status.idle": "2024-08-04T09:32:52.339608Z",
     "shell.execute_reply": "2024-08-04T09:32:52.339260Z",
     "shell.execute_reply.started": "2024-08-04T09:32:52.314255Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”\n",
       "β”‚ column_name β”‚ column_type β”‚  null   β”‚   key   β”‚ default β”‚  extra  β”‚\n",
       "β”‚   varchar   β”‚   varchar   β”‚ varchar β”‚ varchar β”‚ varchar β”‚ varchar β”‚\n",
       "β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€\n",
       "β”‚ index       β”‚ BIGINT      β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚\n",
       "β”‚ embeddings  β”‚ FLOAT\u001b[1m[\u001b[0m\u001b[1;36m1024\u001b[0m\u001b[1m]\u001b[0m β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚\n",
       "β”‚ text        β”‚ VARCHAR     β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚\n",
       "β”‚ url         β”‚ VARCHAR     β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚\n",
       "β”‚ title       β”‚ VARCHAR     β”‚ YES     β”‚ NULL    β”‚ NULL    β”‚ NULL    β”‚\n",
       "β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.sql(\"DESCRIBE olympics\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1dea5f5d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:52.340304Z",
     "iopub.status.busy": "2024-08-04T09:32:52.340198Z",
     "iopub.status.idle": "2024-08-04T09:32:52.407493Z",
     "shell.execute_reply": "2024-08-04T09:32:52.407142Z",
     "shell.execute_reply.started": "2024-08-04T09:32:52.340294Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "β”Œβ”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”\n",
       "β”‚ index β”‚                                                     text                                                     β”‚\n",
       "β”‚ int64 β”‚                                                   varchar                                                    β”‚\n",
       "β”œβ”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€\n",
       "β”‚     \u001b[1;36m0\u001b[0m β”‚ The \u001b[1;36m2024\u001b[0m Olympics opened in Paris in spectacular style with thousands of athletes sailing along the River …  β”‚\n",
       "β”‚     \u001b[1;36m1\u001b[0m β”‚ Swapping a stadium for a waterway for the first time to open the \u001b[32m\"greatest show on Earth\"\u001b[0m, the near four-h…  β”‚\n",
       "β”‚     \u001b[1;36m2\u001b[0m β”‚ Blue, white and red fireworks had raised the Tricolore above Austerlitz Bridge before \u001b[1;36m6\u001b[0m,\u001b[1;36m800\u001b[0m athletes from …  β”‚\n",
       "β”‚     \u001b[1;36m3\u001b[0m β”‚ There were surprise performances through the ceremony, including a cabaret number from US singer-songwrite…  β”‚\n",
       "β”‚     \u001b[1;36m4\u001b[0m β”‚ The day had started with major disruption when the French train network was hit by arson attacks and heavy…  β”‚\n",
       "β”‚     \u001b[1;36m5\u001b[0m β”‚ The lashing rain may have forced athletes to add rain ponchos and umbrellas to their planned outfits but i…  β”‚\n",
       "β”‚     \u001b[1;36m6\u001b[0m β”‚ The last two boats to parade - first the US as the next hosts for Los Angeles \u001b[1;36m2028\u001b[0m and then France - had t…  β”‚\n",
       "β”‚     \u001b[1;36m7\u001b[0m β”‚ Rower Helen Glover and diver Tom Daley were Great Britain's flagbearers in Paris, which is hosting the sum…  β”‚\n",
       "β”‚     \u001b[1;36m8\u001b[0m β”‚ In opening the 33rd summer Olympics, which are taking part against a difficult international and domestic …  β”‚\n",
       "β”‚     \u001b[1;36m9\u001b[0m β”‚ More than \u001b[1;36m10\u001b[0m,\u001b[1;36m500\u001b[0m athletes will compete across \u001b[1;36m32\u001b[0m sports at the Games, which will close on \u001b[1;36m11\u001b[0m August.         β”‚\n",
       "β”œβ”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€\n",
       "β”‚ \u001b[1;36m10\u001b[0m rows                                                                                                    \u001b[1;36m2\u001b[0m columns β”‚\n",
       "β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "con.sql(\"SELECT index, text FROM olympics LIMIT 10\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0461b9b5",
   "metadata": {},
   "source": [
    "## Evaluating against real questions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "df582978",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:52.408595Z",
     "iopub.status.busy": "2024-08-04T09:32:52.408321Z",
     "iopub.status.idle": "2024-08-04T09:32:53.468368Z",
     "shell.execute_reply": "2024-08-04T09:32:53.467857Z",
     "shell.execute_reply.started": "2024-08-04T09:32:52.408583Z"
    }
   },
   "outputs": [],
   "source": [
    "qrels = Qrels.from_file(\"data/questions.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "dcc41ea3",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:53.469612Z",
     "iopub.status.busy": "2024-08-04T09:32:53.469452Z",
     "iopub.status.idle": "2024-08-04T09:32:53.581670Z",
     "shell.execute_reply": "2024-08-04T09:32:53.581144Z",
     "shell.execute_reply.started": "2024-08-04T09:32:53.469601Z"
    }
   },
   "outputs": [],
   "source": [
    "from search import Search\n",
    "s = Search(con)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "46af986c",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:53.582813Z",
     "iopub.status.busy": "2024-08-04T09:32:53.582484Z",
     "iopub.status.idle": "2024-08-04T09:32:53.585705Z",
     "shell.execute_reply": "2024-08-04T09:32:53.585028Z",
     "shell.execute_reply.started": "2024-08-04T09:32:53.582801Z"
    }
   },
   "outputs": [],
   "source": [
    "functions = [\n",
    "  (s.fts, \"Full-Text\"), \n",
    "  (s.vector_search, \"Vector\"), \n",
    "  (s.hybrid, \"Hybrid\")\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "dc44ca9a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:53.586898Z",
     "iopub.status.busy": "2024-08-04T09:32:53.586555Z",
     "iopub.status.idle": "2024-08-04T09:32:53.589969Z",
     "shell.execute_reply": "2024-08-04T09:32:53.589536Z",
     "shell.execute_reply.started": "2024-08-04T09:32:53.586887Z"
    }
   },
   "outputs": [],
   "source": [
    "def create_run(qrels, retrieval_fn, name):\n",
    "  run_dict = {\n",
    "    question: {\n",
    "      str(index): score\n",
    "      for index, score in (retrieval_fn(question)\n",
    "                            .select(\"index, score\")\n",
    "                            .fetchall()\n",
    "                          )\n",
    "    }\n",
    "    for question in qrels.to_dict()\n",
    "  }\n",
    "  return Run(run_dict, name=name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "3cd2f834",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:32:53.590837Z",
     "iopub.status.busy": "2024-08-04T09:32:53.590606Z",
     "iopub.status.idle": "2024-08-04T09:33:00.896105Z",
     "shell.execute_reply": "2024-08-04T09:33:00.891632Z",
     "shell.execute_reply.started": "2024-08-04T09:32:53.590826Z"
    }
   },
   "outputs": [],
   "source": [
    "runs = [\n",
    "   create_run(qrels, fn, name)\n",
    "   for fn, name in functions\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "78e78548",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:33:00.898793Z",
     "iopub.status.busy": "2024-08-04T09:33:00.898603Z",
     "iopub.status.idle": "2024-08-04T09:33:01.278889Z",
     "shell.execute_reply": "2024-08-04T09:33:01.278569Z",
     "shell.execute_reply.started": "2024-08-04T09:33:00.898778Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "\n",
       "#    Model        Hit Rate\n",
       "---  ---------  ----------\n",
       "a    Full-Text        \u001b[1;36m0.7\u001b[0m\n",
       "b    Vector           \u001b[1;36m0.75\u001b[0m\n",
       "c    Hybrid           \u001b[1;36m0.9\u001b[0m"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compare(\n",
    "    qrels,\n",
    "    runs=runs,\n",
    "    metrics=[\"hit_rate\"],\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "ac0094eb",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:33:01.279434Z",
     "iopub.status.busy": "2024-08-04T09:33:01.279349Z",
     "iopub.status.idle": "2024-08-04T09:33:01.281437Z",
     "shell.execute_reply": "2024-08-04T09:33:01.281121Z",
     "shell.execute_reply.started": "2024-08-04T09:33:01.279425Z"
    }
   },
   "outputs": [],
   "source": [
    "from rich.table import Table\n",
    "from rich.console import Console\n",
    "c = Console()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbd83afc",
   "metadata": {},
   "source": [
    "## Which ones did we get wrong?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "b4179c75",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:33:01.282368Z",
     "iopub.status.busy": "2024-08-04T09:33:01.282220Z",
     "iopub.status.idle": "2024-08-04T09:33:01.286622Z",
     "shell.execute_reply": "2024-08-04T09:33:01.285869Z",
     "shell.execute_reply.started": "2024-08-04T09:33:01.282356Z"
    }
   },
   "outputs": [],
   "source": [
    "table = Table(title=\"Comparing Retrieval Techniques\")\n",
    "table.add_column(\"Question\")\n",
    "for run in runs:\n",
    "  table.add_column(run.name, justify=\"center\")\n",
    "\n",
    "cols = [col.header for col in table.columns][1:]\n",
    "for question in qrels.to_dict():\n",
    "  row = [question]\n",
    "  for col in cols:\n",
    "    selected_run = [r for r in runs if r.name == col][0]\n",
    "    score = selected_run.scores['hit_rate'][question]\n",
    "    row.append(\"βœ…\" if score == 1.0 else \"❌\")\n",
    "  table.add_row(*row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2b5b380d",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:33:01.287346Z",
     "iopub.status.busy": "2024-08-04T09:33:01.287161Z",
     "iopub.status.idle": "2024-08-04T09:33:01.296474Z",
     "shell.execute_reply": "2024-08-04T09:33:01.296047Z",
     "shell.execute_reply.started": "2024-08-04T09:33:01.287336Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[3m                            Comparing Retrieval Techniques                            \u001b[0m\n",
      "┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━┓\n",
      "┃\u001b[1m \u001b[0m\u001b[1mQuestion                                            \u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mFull-Text\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mVector\u001b[0m\u001b[1m \u001b[0m┃\u001b[1m \u001b[0m\u001b[1mHybrid\u001b[0m\u001b[1m \u001b[0m┃\n",
      "┑━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━┩\n",
      "β”‚ How many competitors are there?                      β”‚    ❌     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ How many medals will be won on Saturday?             β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ How many times has Paris hosted the Olympic games?   β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ What colors were the fireworks during the ceremony?  β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ What started the day of the opening ceremony?        β”‚    βœ…     β”‚   ❌   β”‚   ❌   β”‚\n",
      "β”‚ What things went wrong?                              β”‚    ❌     β”‚   ❌   β”‚   ❌   β”‚\n",
      "β”‚ What was Serena's role?                              β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ What was the weather like?                           β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ What was unique about the location for the ceremony? β”‚    ❌     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ Where are opening ceremonies usually held?           β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ Where are they playing the tennis?                   β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ Where did the opening ceremony end?                  β”‚    βœ…     β”‚   ❌   β”‚   βœ…   β”‚\n",
      "β”‚ Where did the opening ceremony take place?           β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ Where was the last games held?                       β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ Which number Olympics is this?                       β”‚    ❌     β”‚   ❌   β”‚   βœ…   β”‚\n",
      "β”‚ Who did Zidane hand the flame to?                    β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ Who is banned from the Olympics?                     β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ Who received significant applause?                   β”‚    ❌     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β”‚ Who was the oldest athlete?                          β”‚    ❌     β”‚   ❌   β”‚   βœ…   β”‚\n",
      "β”‚ Why were there concerns about the Seine?             β”‚    βœ…     β”‚   βœ…   β”‚   βœ…   β”‚\n",
      "β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”˜\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "with c.pager(styles=True):\n",
    "  c.print(table)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d4b023de",
   "metadata": {},
   "source": [
    "##Β What wrong answers did we return?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "bc32f40a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:33:01.297477Z",
     "iopub.status.busy": "2024-08-04T09:33:01.297254Z",
     "iopub.status.idle": "2024-08-04T09:33:01.300773Z",
     "shell.execute_reply": "2024-08-04T09:33:01.299991Z",
     "shell.execute_reply.started": "2024-08-04T09:33:01.297462Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_text(ids):\n",
    "  result = con.sql(\"\"\"SELECT text\n",
    "      FROM olympics \n",
    "      WHERE list_contains($ids::BIGINT[], index)\n",
    "      \"\"\", \n",
    "      params={\"ids\": ids}\n",
    "  )\n",
    "  return [row[0] for row in result.fetchall()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "8635c132",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-08-04T09:33:01.301767Z",
     "iopub.status.busy": "2024-08-04T09:33:01.301574Z",
     "iopub.status.idle": "2024-08-04T09:33:01.698206Z",
     "shell.execute_reply": "2024-08-04T09:33:01.697915Z",
     "shell.execute_reply.started": "2024-08-04T09:33:01.301753Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[1mWhat started the day of the opening ceremony?\u001b[0m\n",
      "\u001b[3;32mCorrect Answer\u001b[0m\n",
      "\u001b[32mThe day had started with major disruption when the French train network was hit by arson attacks and heavy rain in \u001b[0m\n",
      "\u001b[32mthe evening put paid to the original plan by artistic director Thomas Jolly to use the Parisian sun to \u001b[0m\u001b[32m\"make the \u001b[0m\n",
      "\u001b[32mwater sparkle\"\u001b[0m\u001b[32m. \u001b[0m\n",
      "\u001b[3;33mRun Answer\u001b[0m\n",
      "\u001b[33mThe \u001b[0m\u001b[1;36m2024\u001b[0m\u001b[33m Olympics opened in Paris in spectacular style with thousands of athletes sailing along the River Seine \u001b[0m\n",
      "\u001b[33mpast lively performers on bridges, banks and rooftops in an ambitious take on an opening ceremony.   \u001b[0m\n",
      "\u001b[33mGiven the miserable weather after what had been a sunny week in Paris until now, it seemed fitting that the \u001b[0m\n",
      "\u001b[33mstoryline at the start of the ceremony was about the arrival of the Olympic flame in Paris not going according to \u001b[0m\n",
      "\u001b[33mplan.\u001b[0m\n",
      "\u001b[33mThe peace anthem, part of all Olympic opening ceremonies, is aligned with the message of unity and tolerance \u001b[0m\n",
      "\u001b[33mconveyed by the Games. \u001b[0m\n",
      "\n",
      "\u001b[1mWhat things went wrong?\u001b[0m\n",
      "\u001b[3;32mCorrect Answer\u001b[0m\n",
      "\u001b[32mThe torchbearer did not get the memo about it not being in the Stade de France, and then Zinedine Zidane's metro \u001b[0m\n",
      "\u001b[32mtrain broke down while he was transporting the torch.\u001b[0m\n",
      "\u001b[3;33mRun Answer\u001b[0m\n",
      "\u001b[33mAt times it was bizarre - one moment Lady Gaga surrounded by pink and black feathers was singing in French, the \u001b[0m\n",
      "\u001b[33mnext Bangladesh's athletes were being introduced on their boat. \u001b[0m\n",
      "\u001b[33mA lot of the time it was brilliantly frenetic and occasionally emotional. \u001b[0m\n",
      "\u001b[33mGiven the miserable weather after what had been a sunny week in Paris until now, it seemed fitting that the \u001b[0m\n",
      "\u001b[33mstoryline at the start of the ceremony was about the arrival of the Olympic flame in Paris not going according to \u001b[0m\n",
      "\u001b[33mplan.\u001b[0m\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"></pre>\n"
      ],
      "text/plain": []
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "run = [r for r in runs if r.name == \"Hybrid\"][0]\n",
    "with c.pager(styles=True):\n",
    "  for question, score in run.scores['hit_rate'].items():\n",
    "    if score == 0.0:\n",
    "      c.print(question, style=\"bold\")\n",
    "\n",
    "      correct_ids = list(qrels.to_dict()[question])\n",
    "      c.print(\"Correct Answer\", style=\"Green italic\")\n",
    "      c.print(\"\\n\".join(get_text(correct_ids)), style=\"Green\")\n",
    "\n",
    "      answer_ids = list(run.to_dict()[question])      \n",
    "      c.print(\"Run Answer\", style=\"Yellow italic\")\n",
    "      c.print(\"\\n\".join(get_text(answer_ids)) or \"None\", style=\"Yellow\")\n",
    "\n",
    "      c.print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  },
  "widgets": {
   "application/vnd.jupyter.widget-state+json": {
    "state": {},
    "version_major": 2,
    "version_minor": 0
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}