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{
"cells": [
{
"cell_type": "markdown",
"id": "f0d5e472-1356-4ceb-b2a9-c4f15fc0aacf",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:29:42.362912Z",
"iopub.status.busy": "2024-09-28T06:29:42.362414Z",
"iopub.status.idle": "2024-09-28T06:29:42.388293Z",
"shell.execute_reply": "2024-09-28T06:29:42.387586Z",
"shell.execute_reply.started": "2024-09-28T06:29:42.362879Z"
}
},
"source": [
"# Rerankers\n",
"\n",
"This is a short notebook showing how to use Answer AI's rerankers library"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "1d9b2824-b188-4190-8bdf-039c607c19ce",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:30:13.468168Z",
"iopub.status.busy": "2024-09-28T06:30:13.467708Z",
"iopub.status.idle": "2024-09-28T06:30:15.905520Z",
"shell.execute_reply": "2024-09-28T06:30:15.905143Z",
"shell.execute_reply.started": "2024-09-28T06:30:13.468138Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting rerankers[transformers]\n",
" Obtaining dependency information for rerankers[transformers] from https://files.pythonhosted.org/packages/24/77/a0ea5b36221c89fbb7be9bf28075a57e829d89a72cbee0e69945b4524a7b/rerankers-0.5.3-py3-none-any.whl.metadata\n",
" Downloading rerankers-0.5.3-py3-none-any.whl.metadata (27 kB)\n",
"Requirement already satisfied: torch in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (2.4.1)\n",
"Requirement already satisfied: pydantic in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from rerankers[transformers]) (2.6.2)\n",
"Requirement already satisfied: tqdm in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from rerankers[transformers]) (4.66.1)\n",
"Requirement already satisfied: transformers in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from rerankers[transformers]) (4.44.2)\n",
"Requirement already satisfied: sentencepiece in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from rerankers[transformers]) (0.1.99)\n",
"Requirement already satisfied: protobuf in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from rerankers[transformers]) (5.27.3)\n",
"Requirement already satisfied: filelock in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from torch) (3.13.1)\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from torch) (4.9.0)\n",
"Requirement already satisfied: sympy in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from torch) (1.12)\n",
"Requirement already satisfied: networkx in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from torch) (3.2.1)\n",
"Requirement already satisfied: jinja2 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from torch) (3.1.3)\n",
"Requirement already satisfied: fsspec in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from torch) (2024.2.0)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from jinja2->torch) (2.1.5)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from pydantic->rerankers[transformers]) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.16.3 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from pydantic->rerankers[transformers]) (2.16.3)\n",
"Requirement already satisfied: mpmath>=0.19 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from sympy->torch) (1.3.0)\n",
"Requirement already satisfied: huggingface-hub<1.0,>=0.23.2 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from transformers->rerankers[transformers]) (0.24.6)\n",
"Requirement already satisfied: numpy>=1.17 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from transformers->rerankers[transformers]) (1.26.4)\n",
"Requirement already satisfied: packaging>=20.0 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from transformers->rerankers[transformers]) (24.1)\n",
"Requirement already satisfied: pyyaml>=5.1 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from transformers->rerankers[transformers]) (6.0.1)\n",
"Requirement already satisfied: regex!=2019.12.17 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from transformers->rerankers[transformers]) (2023.12.25)\n",
"Requirement already satisfied: requests in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from transformers->rerankers[transformers]) (2.31.0)\n",
"Requirement already satisfied: safetensors>=0.4.1 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from transformers->rerankers[transformers]) (0.4.2)\n",
"Requirement already satisfied: tokenizers<0.20,>=0.19 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from transformers->rerankers[transformers]) (0.19.1)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from requests->transformers->rerankers[transformers]) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from requests->transformers->rerankers[transformers]) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from requests->transformers->rerankers[transformers]) (2.0.4)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/markhneedham/miniforge3/lib/python3.10/site-packages (from requests->transformers->rerankers[transformers]) (2023.7.22)\n",
"Downloading rerankers-0.5.3-py3-none-any.whl (37 kB)\n",
"Installing collected packages: rerankers\n",
"Successfully installed rerankers-0.5.3\n",
"Note: you may need to restart the kernel to use updated packages.\n"
]
}
],
"source": [
"%pip install \"rerankers[transformers]\" torch"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3dc26ee0",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:30:18.179883Z",
"iopub.status.busy": "2024-09-28T06:30:18.179208Z",
"iopub.status.idle": "2024-09-28T06:30:23.568619Z",
"shell.execute_reply": "2024-09-28T06:30:23.568233Z",
"shell.execute_reply.started": "2024-09-28T06:30:18.179851Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/markhneedham/miniforge3/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import time\n",
"from utils import view\n",
"from rerankers import Reranker"
]
},
{
"cell_type": "markdown",
"id": "cd7aba53-7f26-4703-9607-dd71bf2afd36",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:30:46.777782Z",
"iopub.status.busy": "2024-09-28T06:30:46.776873Z",
"iopub.status.idle": "2024-09-28T06:30:46.783450Z",
"shell.execute_reply": "2024-09-28T06:30:46.782570Z",
"shell.execute_reply.started": "2024-09-28T06:30:46.777674Z"
}
},
"source": [
"##Β Initialising ReRanker"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "3d9da06e",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:30:28.966361Z",
"iopub.status.busy": "2024-09-28T06:30:28.965668Z",
"iopub.status.idle": "2024-09-28T06:30:31.813149Z",
"shell.execute_reply": "2024-09-28T06:30:31.812693Z",
"shell.execute_reply.started": "2024-09-28T06:30:28.966328Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading TransformerRanker model mixedbread-ai/mxbai-rerank-large-v1\n",
"No dtype set\n",
"Using dtype torch.float16\n",
"Loaded model mixedbread-ai/mxbai-rerank-large-v1\n",
"Using device mps.\n",
"Using dtype torch.float16.\n"
]
}
],
"source": [
"ranker = Reranker(\n",
" model_name='mixedbread-ai/mxbai-rerank-large-v1',\n",
" model_type='cross-encoder',\n",
" device='mps'\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c8b70532",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:30:34.224172Z",
"iopub.status.busy": "2024-09-28T06:30:34.223729Z",
"iopub.status.idle": "2024-09-28T06:30:38.583496Z",
"shell.execute_reply": "2024-09-28T06:30:38.583151Z",
"shell.execute_reply.started": "2024-09-28T06:30:34.224141Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 282 ms, sys: 184 ms, total: 466 ms\n",
"Wall time: 4.36 s\n"
]
}
],
"source": [
"%%time\n",
"sorted_rows = ranker.rank(\n",
" query=\"I love you\",\n",
" docs=[\"I hate you\", \"I really like you\", \"You're not too bad\"],\n",
" doc_ids=[0,1,2]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8f35c94d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:00.653785Z",
"iopub.status.busy": "2024-09-28T06:31:00.653302Z",
"iopub.status.idle": "2024-09-28T06:31:00.672516Z",
"shell.execute_reply": "2024-09-28T06:31:00.672180Z",
"shell.execute_reply.started": "2024-09-28T06:31:00.653753Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;35mRankedResults\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mresults\u001b[0m=\u001b[1m[\u001b[0m\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtext\u001b[0m=\u001b[32m'I really like you'\u001b[0m, \u001b[33mdoc_id\u001b[0m=\u001b[1;36m1\u001b[0m, \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\u001b[1m)\u001b[0m, \u001b[33mscore\u001b[0m=\u001b[1;36m-1.5400390625\u001b[0m, \u001b[33mrank\u001b[0m=\u001b[1;36m1\u001b[0m\u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtext\u001b[0m=\u001b[32m\"You\u001b[0m\u001b[32m're not too bad\"\u001b[0m, \u001b[33mdoc_id\u001b[0m=\u001b[1;36m2\u001b[0m, \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\u001b[1m)\u001b[0m, \u001b[33mscore\u001b[0m=\u001b[1;36m-2.8828125\u001b[0m, \u001b[33mrank\u001b[0m=\u001b[1;36m2\u001b[0m\u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\u001b[33mtext\u001b[0m=\u001b[32m'I hate you'\u001b[0m, \u001b[33mdoc_id\u001b[0m=\u001b[1;36m0\u001b[0m, \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\u001b[1m)\u001b[0m, \u001b[33mscore\u001b[0m=\u001b[1;36m-4.30859375\u001b[0m, \u001b[33mrank\u001b[0m=\u001b[1;36m3\u001b[0m\u001b[1m)\u001b[0m\n",
" \u001b[1m]\u001b[0m,\n",
" \u001b[33mquery\u001b[0m=\u001b[32m'I love you'\u001b[0m,\n",
" \u001b[33mhas_scores\u001b[0m=\u001b[3;92mTrue\u001b[0m\n",
"\u001b[1m)\u001b[0m\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": [
"view(sorted_rows)"
]
},
{
"cell_type": "markdown",
"id": "f67565a1-4766-4cac-ad76-86a7c5da6e68",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:15.895162Z",
"iopub.status.busy": "2024-09-28T06:31:15.894760Z",
"iopub.status.idle": "2024-09-28T06:31:15.900058Z",
"shell.execute_reply": "2024-09-28T06:31:15.898703Z",
"shell.execute_reply.started": "2024-09-28T06:31:15.895133Z"
}
},
"source": [
"## Reranking an Olympics dataset\n",
"\n",
"Let's see how well it works on a dataset about the Olympics opening ceremony."
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "73439db3",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:02.752426Z",
"iopub.status.busy": "2024-09-28T06:31:02.751985Z",
"iopub.status.idle": "2024-09-28T06:31:03.431931Z",
"shell.execute_reply": "2024-09-28T06:31:03.431594Z",
"shell.execute_reply.started": "2024-09-28T06:31:02.752396Z"
}
},
"outputs": [],
"source": [
"import duckdb\n",
"con = duckdb.connect(\"olympics.duckdb\")"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "aa6f0e3f",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:25.984666Z",
"iopub.status.busy": "2024-09-28T06:31:25.984257Z",
"iopub.status.idle": "2024-09-28T06:31:26.005565Z",
"shell.execute_reply": "2024-09-28T06:31:26.005053Z",
"shell.execute_reply.started": "2024-09-28T06:31:25.984636Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"βββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n",
"βββββ\n",
"β index β text \n",
"β\n",
"β int64 β varchar \n",
"β\n",
"βββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\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",
"β¦ β\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 \n",
"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",
"β¦ β\n",
"β \u001b[1;36m3\u001b[0m β There were surprise performances through the ceremony, including a cabaret number from US \n",
"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 \n",
"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 \n",
"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 \n",
"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 \n",
"sumβ¦ β\n",
"β \u001b[1;36m8\u001b[0m β In opening the 33rd summer Olympics, which are taking part against a difficult international and domestic\n",
"β¦ β\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",
"βββββββββ΄ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n",
"βββββ€\n",
"β \u001b[1;36m10\u001b[0m rows \u001b[1;36m2\u001b[0m \n",
"columns β\n",
"βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n",
"βββββ\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": [
"view(\n",
" con.query(\"FROM olympics SELECT index, text\").limit(10)\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "e3ed70ed",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:34.351037Z",
"iopub.status.busy": "2024-09-28T06:31:34.350335Z",
"iopub.status.idle": "2024-09-28T06:31:34.538258Z",
"shell.execute_reply": "2024-09-28T06:31:34.537934Z",
"shell.execute_reply.started": "2024-09-28T06:31:34.350976Z"
}
},
"outputs": [],
"source": [
"from search import Search\n",
"s = Search(con)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "b0c89dff",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:36.588113Z",
"iopub.status.busy": "2024-09-28T06:31:36.587458Z",
"iopub.status.idle": "2024-09-28T06:31:39.398514Z",
"shell.execute_reply": "2024-09-28T06:31:39.398175Z",
"shell.execute_reply.started": "2024-09-28T06:31:36.588080Z"
}
},
"outputs": [],
"source": [
"question = \"What things went wrong?\"\n",
"rows = s.vector_search(question, limit=10).fetchall()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "b2e13079",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:42.602192Z",
"iopub.status.busy": "2024-09-28T06:31:42.601586Z",
"iopub.status.idle": "2024-09-28T06:31:42.619240Z",
"shell.execute_reply": "2024-09-28T06:31:42.618403Z",
"shell.execute_reply.started": "2024-09-28T06:31:42.602162Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m[\u001b[0m\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m'Given the miserable weather after what had been a sunny week in Paris until now, it seemed fitting that \u001b[0m\n",
"\u001b[32mthe storyline at the start of the ceremony was about the arrival of the Olympic flame in Paris not going according \u001b[0m\n",
"\u001b[32mto plan.'\u001b[0m,\n",
" \u001b[1;36m15\u001b[0m,\n",
" \u001b[1;36m0.5197256803512573\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\u001b[32m'A lot of the time it was brilliantly frenetic and occasionally emotional. '\u001b[0m, \u001b[1;36m14\u001b[0m, \u001b[1;36m0.4570101201534271\u001b[0m\u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m\"At times it was bizarre - one moment Lady Gaga surrounded by pink and black feathers was singing in \u001b[0m\n",
"\u001b[32mFrench, the next Bangladesh's athletes were being introduced on their boat. \"\u001b[0m,\n",
" \u001b[1;36m13\u001b[0m,\n",
" \u001b[1;36m0.45612600445747375\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m\"The torchbearer did not get the memo about it not being in the Stade de France, and then Zinedine Zidane's\u001b[0m\n",
"\u001b[32mmetro train broke down while he was transporting the torch.\"\u001b[0m,\n",
" \u001b[1;36m16\u001b[0m,\n",
" \u001b[1;36m0.44767796993255615\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m'The day had started with major disruption when the French train network was hit by arson attacks and heavy\u001b[0m\n",
"\u001b[32mrain in the evening put paid to the original plan by artistic director Thomas Jolly to use the Parisian sun to \u001b[0m\n",
"\u001b[32m\"make the water sparkle\". '\u001b[0m,\n",
" \u001b[1;36m4\u001b[0m,\n",
" \u001b[1;36m0.4340539574623108\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m'Since the last Olympics - the Beijing 2022 Winter Games - wars have started in Ukraine and Gaza.'\u001b[0m,\n",
" \u001b[1;36m26\u001b[0m,\n",
" \u001b[1;36m0.4187776744365692\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m\"One segment focused on rebuilding Notre Dame, which was damaged in a fire in 2019. A large troupe of \u001b[0m\n",
"\u001b[32mdancers were accompanied by music composed using sounds captured from the iconic cathedral's reconstruction.\"\u001b[0m,\n",
" \u001b[1;36m19\u001b[0m,\n",
" \u001b[1;36m0.41769999265670776\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m'When organisers first revealed plans to hold the opening ceremony along the river in the heart of the \u001b[0m\n",
"\u001b[32mcity, rather than in a stadium as is usual, there were some raised eyebrows and questions over how they would \u001b[0m\n",
"\u001b[32mmanage such a huge security operation.'\u001b[0m,\n",
" \u001b[1;36m10\u001b[0m,\n",
" \u001b[1;36m0.40121984481811523\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m'More than 100 heads of state and government were in attendance, including Prime Minister Sir Keir Starmer \u001b[0m\n",
"\u001b[32mand French President Emmanuel Macron.'\u001b[0m,\n",
" \u001b[1;36m32\u001b[0m,\n",
" \u001b[1;36m0.3883416950702667\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1m(\u001b[0m\n",
" \u001b[32m'The impact of conflicts is being felt at these Olympics, with Russians and Belarusians banned following \u001b[0m\n",
"\u001b[32mthe Russian invasion of Ukraine. Just 15 Russian and 17 Belarusian athletes will be competing as Individual Neutral\u001b[0m\n",
"\u001b[32mAthletes \u001b[0m\u001b[32m(\u001b[0m\u001b[32mAIN\u001b[0m\u001b[32m)\u001b[0m\u001b[32m in Paris and they were not part of the parade at the opening ceremony.'\u001b[0m,\n",
" \u001b[1;36m30\u001b[0m,\n",
" \u001b[1;36m0.38072648644447327\u001b[0m\n",
" \u001b[1m)\u001b[0m\n",
"\u001b[1m]\u001b[0m\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": [
"view(rows)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "01bcfc62",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:44.951584Z",
"iopub.status.busy": "2024-09-28T06:31:44.951181Z",
"iopub.status.idle": "2024-09-28T06:31:45.741640Z",
"shell.execute_reply": "2024-09-28T06:31:45.741250Z",
"shell.execute_reply.started": "2024-09-28T06:31:44.951555Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 175 ms, sys: 84.1 ms, total: 259 ms\n",
"Wall time: 786 ms\n"
]
}
],
"source": [
"%%time\n",
"sorted_rows = ranker.rank(\n",
" query=question,\n",
" docs=[r[0] for r in rows],\n",
" doc_ids=[r[1] for r in rows]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "7fce874f",
"metadata": {
"execution": {
"iopub.execute_input": "2024-09-28T06:31:47.711260Z",
"iopub.status.busy": "2024-09-28T06:31:47.710854Z",
"iopub.status.idle": "2024-09-28T06:31:47.745835Z",
"shell.execute_reply": "2024-09-28T06:31:47.745436Z",
"shell.execute_reply.started": "2024-09-28T06:31:47.711231Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1;35mRankedResults\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mresults\u001b[0m=\u001b[1m[\u001b[0m\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m'The day had started with major disruption when the French train network was hit by arson \u001b[0m\n",
"\u001b[32mattacks and heavy rain in the evening put paid to the original plan by artistic director Thomas Jolly to use the \u001b[0m\n",
"\u001b[32mParisian sun to \"make the water sparkle\". '\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m4\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m0\u001b[0m\u001b[1;36m.0321044921875\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m1\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m\"The\u001b[0m\u001b[32m torchbearer did not get the memo about it not being in the Stade de France, and then \u001b[0m\n",
"\u001b[32mZinedine Zidane's metro train broke down while he was transporting the torch.\"\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m16\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-0.12066650390625\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m2\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m'Given the miserable weather after what had been a sunny week in Paris until now, it seemed \u001b[0m\n",
"\u001b[32mfitting that the storyline at the start of the ceremony was about the arrival of the Olympic flame in Paris not \u001b[0m\n",
"\u001b[32mgoing according to plan.'\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m15\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-0.9423828125\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m3\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m'Since the last Olympics - the Beijing 2022 Winter Games - wars have started in Ukraine and \u001b[0m\n",
"\u001b[32mGaza.'\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m26\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-1.322265625\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m4\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m'When organisers first revealed plans to hold the opening ceremony along the river in the \u001b[0m\n",
"\u001b[32mheart of the city, rather than in a stadium as is usual, there were some raised eyebrows and questions over how \u001b[0m\n",
"\u001b[32mthey would manage such a huge security operation.'\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m10\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-1.8583984375\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m5\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m'The impact of conflicts is being felt at these Olympics, with Russians and Belarusians banned\u001b[0m\n",
"\u001b[32mfollowing the Russian invasion of Ukraine. Just 15 Russian and 17 Belarusian athletes will be competing as \u001b[0m\n",
"\u001b[32mIndividual Neutral Athletes \u001b[0m\u001b[32m(\u001b[0m\u001b[32mAIN\u001b[0m\u001b[32m)\u001b[0m\u001b[32m in Paris and they were not part of the parade at the opening ceremony.'\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m30\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-1.96484375\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m6\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m\"One\u001b[0m\u001b[32m segment focused on rebuilding Notre Dame, which was damaged in a fire in 2019. A large \u001b[0m\n",
"\u001b[32mtroupe of dancers were accompanied by music composed using sounds captured from the iconic cathedral's \u001b[0m\n",
"\u001b[32mreconstruction.\"\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m19\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-3.298828125\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m7\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m'More than 100 heads of state and government were in attendance, including Prime Minister Sir \u001b[0m\n",
"\u001b[32mKeir Starmer and French President Emmanuel Macron.'\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m32\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-4.34765625\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m8\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m'A lot of the time it was brilliantly frenetic and occasionally emotional. '\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m14\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-4.81640625\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m9\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[1;35mResult\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mdocument\u001b[0m=\u001b[1;35mDocument\u001b[0m\u001b[1m(\u001b[0m\n",
" \u001b[33mtext\u001b[0m=\u001b[32m\"At\u001b[0m\u001b[32m times it was bizarre - one moment Lady Gaga surrounded by pink and black feathers was \u001b[0m\n",
"\u001b[32msinging in French, the next Bangladesh's athletes were being introduced on their boat. \"\u001b[0m,\n",
" \u001b[33mdoc_id\u001b[0m=\u001b[1;36m13\u001b[0m,\n",
" \u001b[33mmetadata\u001b[0m=\u001b[1m{\u001b[0m\u001b[1m}\u001b[0m\n",
" \u001b[1m)\u001b[0m,\n",
" \u001b[33mscore\u001b[0m=\u001b[1;36m-5.08984375\u001b[0m,\n",
" \u001b[33mrank\u001b[0m=\u001b[1;36m10\u001b[0m\n",
" \u001b[1m)\u001b[0m\n",
" \u001b[1m]\u001b[0m,\n",
" \u001b[33mquery\u001b[0m=\u001b[32m'What things went wrong?'\u001b[0m,\n",
" \u001b[33mhas_scores\u001b[0m=\u001b[3;92mTrue\u001b[0m\n",
"\u001b[1m)\u001b[0m\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": [
"view(sorted_rows)"
]
}
],
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}