-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenRerank.py
More file actions
914 lines (736 loc) · 19.8 KB
/
Copy pathOpenRerank.py
File metadata and controls
914 lines (736 loc) · 19.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
# If the customer is committed to OpenAI embeddings,
# position the Voyage reranker as a second-stage accuracy improvement.
import math
import time
import openai
from voyageai import Client as VoyageClient
from rich import box
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
console = Console()
# ============================================================
# Configuration
# ============================================================
VOYAGE_API_KEY = ""
OPENAI_API_KEY = ""
OPENAI_EMBEDDING_MODEL = "text-embedding-3-large"
RERANK_MODEL = "rerank-2.5"
# Retrieve at least 10 candidates so nDCG@10 can be calculated.
# This demo contains 12 documents, so retrieve all 12.
RETRIEVAL_LIMIT = 12
# Return the top 10 after reranking.
FINAL_RESULTS = 10
# Evaluation cutoff.
NDCG_K = 10
# ============================================================
# Setup
# ============================================================
# Preserves the pre-1.0 OpenAI Python SDK syntax
# used by the original script.
openai.api_key = OPENAI_API_KEY
voyage = VoyageClient(
api_key=VOYAGE_API_KEY,
)
# ============================================================
# Inline Example Documents
# ============================================================
DOCUMENTS = [
{
"id": "DOC-001",
"text": (
"A heart-healthy diet should emphasize vegetables, fruits, "
"whole grains, beans, nuts, fish, and foods low in saturated fat."
),
},
{
"id": "DOC-002",
"text": (
"Oats contain soluble fiber, which can help reduce LDL cholesterol "
"and support cardiovascular health."
),
},
{
"id": "DOC-003",
"text": (
"Regular aerobic exercise such as brisk walking, cycling, "
"and swimming can improve cardiovascular fitness and overall "
"heart health."
),
},
{
"id": "DOC-004",
"text": (
"Salmon and other fatty fish contain omega-3 fatty acids "
"that may help support heart health and reduce triglycerides."
),
},
{
"id": "DOC-005",
"text": (
"Avocados contain monounsaturated fats and can be part "
"of a balanced diet focused on cardiovascular health."
),
},
{
"id": "DOC-006",
"text": (
"Stress management techniques such as breathing exercises "
"and meditation can improve overall mental wellbeing."
),
},
{
"id": "DOC-007",
"text": (
"Foods high in saturated fat, sodium, and added sugar "
"should generally be limited when following a heart-healthy "
"eating pattern."
),
},
{
"id": "DOC-008",
"text": (
"Walnuts, almonds, and other nuts provide unsaturated fats, "
"fiber, and other nutrients associated with cardiovascular health."
),
},
{
"id": "DOC-009",
"text": (
"People with diabetes should monitor blood glucose levels "
"and follow their individualized treatment plan."
),
},
{
"id": "DOC-010",
"text": (
"Leafy green vegetables such as spinach and kale provide "
"vitamins, minerals, fiber, and dietary nitrates."
),
},
{
"id": "DOC-011",
"text": (
"Whole grains such as oatmeal, brown rice, and whole-wheat "
"products provide fiber and can support a heart-conscious diet."
),
},
{
"id": "DOC-012",
"text": (
"Processed meats, fried foods, and foods containing large "
"amounts of saturated fat may work against cardiovascular "
"health goals."
),
},
]
# ============================================================
# Human Relevance Judgments
# ============================================================
# These judgments are specific to this question:
#
# "Which foods can help lower cholesterol and improve heart health?"
#
# 3 = highly relevant
# 2 = relevant
# 1 = somewhat relevant
# 0 or missing = not relevant
#
# These labels should be created by a human reviewer.
# For a real benchmark, create separate judgments for each query.
RELEVANCE_JUDGMENTS = {
"DOC-002": 3, # Oats and LDL cholesterol
"DOC-004": 2, # Fatty fish and heart health
"DOC-008": 2, # Nuts and cardiovascular health
"DOC-011": 2, # Whole grains and fiber
"DOC-001": 1, # General heart-healthy diet
"DOC-005": 1, # Avocados and unsaturated fats
"DOC-010": 1, # Leafy green vegetables
}
# ============================================================
# OpenAI Embeddings
# ============================================================
def embed_texts(texts):
"""
Generate embeddings using OpenAI.
Uses the pre-1.0 OpenAI Python SDK syntax from
the original demo.
"""
response = openai.Embedding.create(
model=OPENAI_EMBEDDING_MODEL,
input=texts,
)
return [
item["embedding"]
for item in response["data"]
]
# ============================================================
# Local Vector Search
# ============================================================
def cosine_similarity(a, b):
"""
Calculate cosine similarity between two vectors.
"""
dot_product = sum(
x * y
for x, y in zip(a, b)
)
magnitude_a = math.sqrt(
sum(x * x for x in a)
)
magnitude_b = math.sqrt(
sum(x * x for x in b)
)
if magnitude_a == 0 or magnitude_b == 0:
return 0.0
return dot_product / (
magnitude_a * magnitude_b
)
def retrieve(
query_embedding,
embedded_documents,
k=RETRIEVAL_LIMIT,
):
"""
First-stage semantic retrieval using OpenAI embeddings.
This simulates vector search locally without requiring
a database or vector store.
"""
results = []
for doc in embedded_documents:
score = cosine_similarity(
query_embedding,
doc["embedding"],
)
results.append(
{
"id": doc["id"],
"text": doc["text"],
"vector_score": score,
}
)
results.sort(
key=lambda item: item["vector_score"],
reverse=True,
)
return results[:k]
# ============================================================
# Voyage Reranking
# ============================================================
def rerank(
query,
candidates,
):
"""
Second-stage reranking using Voyage rerank-2.5.
Voyage does not receive the OpenAI vectors.
Voyage receives:
1. The original query text
2. The candidate document text
OpenAI remains the embedding model for the entire
first-stage retrieval pipeline.
"""
start = time.time()
response = voyage.rerank(
query=query,
documents=[
doc["text"]
for doc in candidates
],
model=RERANK_MODEL,
top_k=min(
FINAL_RESULTS,
len(candidates),
),
)
elapsed_ms = (
time.time() - start
) * 1000
results = []
for new_rank, result in enumerate(
response.results,
start=1,
):
original = candidates[result.index]
results.append(
{
"new_rank": new_rank,
"original_rank": result.index + 1,
"id": original["id"],
"text": original["text"],
"vector_score": original["vector_score"],
"rerank_score": result.relevance_score,
}
)
return results, elapsed_ms
# ============================================================
# nDCG@K Evaluation
# ============================================================
def dcg_at_k(
ranked_ids,
relevance_judgments,
k=10,
):
"""
Calculate Discounted Cumulative Gain at K.
Uses graded relevance with exponential gain:
gain = 2^relevance - 1
"""
score = 0.0
for rank, doc_id in enumerate(
ranked_ids[:k],
start=1,
):
relevance = relevance_judgments.get(
doc_id,
0,
)
gain = (
2 ** relevance
) - 1
discount = math.log2(
rank + 1
)
score += gain / discount
return score
def ndcg_at_k(
ranked_ids,
relevance_judgments,
k=10,
):
"""
Calculate Normalized Discounted Cumulative Gain at K.
"""
actual_dcg = dcg_at_k(
ranked_ids,
relevance_judgments,
k,
)
ideal_relevances = sorted(
relevance_judgments.values(),
reverse=True,
)
ideal_dcg = sum(
(
(2 ** relevance) - 1
) / math.log2(rank + 1)
for rank, relevance in enumerate(
ideal_relevances[:k],
start=1,
)
)
if ideal_dcg == 0:
return 0.0
return actual_dcg / ideal_dcg
# ============================================================
# Display: Vector Results
# ============================================================
def show_vector_results(results):
table = Table(
title="1️⃣ OpenAI Embedding Retrieval",
box=box.ROUNDED,
title_style="bold cyan",
)
table.add_column(
"Rank",
width=6,
)
table.add_column(
"ID",
width=10,
)
table.add_column(
"Vector Score",
width=14,
)
table.add_column(
"Human Relevance",
width=16,
)
table.add_column(
"Document",
style="white",
)
for rank, doc in enumerate(
results,
start=1,
):
relevance = RELEVANCE_JUDGMENTS.get(
doc["id"],
0,
)
table.add_row(
f"#{rank}",
doc["id"],
f"{doc['vector_score']:.4f}",
str(relevance),
doc["text"],
)
console.print(table)
# ============================================================
# Display: Reranked Results
# ============================================================
def show_reranked_results(results):
table = Table(
title="2️⃣ OpenAI Retrieval + Voyage rerank-2.5",
box=box.ROUNDED,
title_style="bold green",
)
table.add_column(
"New Rank",
width=10,
)
table.add_column(
"Old Rank",
width=12,
)
table.add_column(
"ID",
width=10,
)
table.add_column(
"Rerank Score",
width=14,
)
table.add_column(
"Vector Score",
width=14,
)
table.add_column(
"Human Relevance",
width=16,
)
table.add_column(
"Document",
style="white",
)
for doc in results:
movement = (
doc["original_rank"]
- doc["new_rank"]
)
if movement > 0:
movement_text = (
f"#{doc['original_rank']} "
f"[green]↑{movement}[/green]"
)
elif movement < 0:
movement_text = (
f"#{doc['original_rank']} "
f"[red]↓{abs(movement)}[/red]"
)
else:
movement_text = (
f"#{doc['original_rank']}"
)
relevance = RELEVANCE_JUDGMENTS.get(
doc["id"],
0,
)
table.add_row(
f"#{doc['new_rank']}",
movement_text,
doc["id"],
f"{doc['rerank_score']:.4f}",
f"{doc['vector_score']:.4f}",
str(relevance),
doc["text"],
)
console.print(table)
# ============================================================
# Display: nDCG Comparison
# ============================================================
def show_ndcg_comparison(
embedding_ndcg,
reranked_ndcg,
k=NDCG_K,
):
absolute_lift = (
reranked_ndcg
- embedding_ndcg
)
if embedding_ndcg > 0:
relative_lift = (
absolute_lift
/ embedding_ndcg
) * 100
relative_lift_text = (
f"{relative_lift:+.2f}%"
)
else:
relative_lift_text = "N/A"
console.print(
Panel(
f"[cyan]"
f"Embeddings nDCG@{k}:"
f"[/cyan] "
f"[bold]"
f"{embedding_ndcg:.4f}"
f"[/bold]\n\n"
f"[green]"
f"Embeddings + Rerank nDCG@{k}:"
f"[/green] "
f"[bold]"
f"{reranked_ndcg:.4f}"
f"[/bold]\n\n"
f"[yellow]"
f"Absolute lift:"
f"[/yellow] "
f"[bold]"
f"{absolute_lift:+.4f}"
f"[/bold]\n"
f"[yellow]"
f"Relative lift:"
f"[/yellow] "
f"[bold]"
f"{relative_lift_text}"
f"[/bold]",
title="📊 Ranking Quality",
border_style="green",
box=box.DOUBLE,
)
)
# ============================================================
# Demo
# ============================================================
def run_demo(question):
console.print()
console.print(
Panel.fit(
"[bold cyan]"
"OpenAI Embeddings + Voyage Reranking"
"[/bold cyan]\n\n"
f"[yellow]Embedding Model:[/yellow] "
f"{OPENAI_EMBEDDING_MODEL}\n"
f"[yellow]Reranker:[/yellow] "
f"{RERANK_MODEL}\n"
f"[yellow]Evaluation Metric:[/yellow] "
f"nDCG@{NDCG_K}\n\n"
f"[bold]Question:[/bold] "
f"{question}",
border_style="cyan",
)
)
console.print()
# --------------------------------------------------------
# STEP 1
# Embed all documents using OpenAI
# --------------------------------------------------------
console.print(
f"[dim]"
f"Embedding {len(DOCUMENTS)} documents with "
f"{OPENAI_EMBEDDING_MODEL}..."
f"[/dim]"
)
start = time.time()
document_embeddings = embed_texts(
[
doc["text"]
for doc in DOCUMENTS
]
)
document_embed_ms = (
time.time() - start
) * 1000
embedded_documents = []
for doc, embedding in zip(
DOCUMENTS,
document_embeddings,
):
embedded_documents.append(
{
**doc,
"embedding": embedding,
}
)
console.print(
f"[green]✓[/green] "
f"Documents embedded with OpenAI in "
f"[yellow]"
f"{document_embed_ms:.1f} ms"
f"[/yellow]"
)
# --------------------------------------------------------
# STEP 2
# Embed the query using the same OpenAI model
# --------------------------------------------------------
console.print(
f"[dim]"
f"Embedding query with "
f"{OPENAI_EMBEDDING_MODEL}..."
f"[/dim]"
)
start = time.time()
query_embedding = embed_texts(
[question]
)[0]
query_embed_ms = (
time.time() - start
) * 1000
console.print(
f"[green]✓[/green] "
f"Query embedded with OpenAI in "
f"[yellow]"
f"{query_embed_ms:.1f} ms"
f"[/yellow]"
)
# --------------------------------------------------------
# STEP 3
# Vector retrieval
# --------------------------------------------------------
console.print(
"[dim]"
"Running cosine similarity retrieval..."
"[/dim]"
)
start = time.time()
candidates = retrieve(
query_embedding,
embedded_documents,
k=RETRIEVAL_LIMIT,
)
retrieval_ms = (
time.time() - start
) * 1000
console.print(
f"[green]✓[/green] "
f"Retrieved "
f"[bold]{len(candidates)}[/bold] "
f"candidates in "
f"[yellow]"
f"{retrieval_ms:.1f} ms"
f"[/yellow]"
)
console.print()
show_vector_results(
candidates
)
# --------------------------------------------------------
# STEP 4
# Voyage reranking
# --------------------------------------------------------
console.print()
console.print(
f"[dim]"
f"Sending the same "
f"{len(candidates)} candidates "
f"to Voyage {RERANK_MODEL}..."
f"[/dim]"
)
reranked, rerank_ms = rerank(
question,
candidates,
)
console.print(
f"[green]✓[/green] "
f"Voyage {RERANK_MODEL} completed in "
f"[yellow]"
f"{rerank_ms:.1f} ms"
f"[/yellow]"
)
console.print()
show_reranked_results(
reranked
)
# --------------------------------------------------------
# STEP 5
# Calculate nDCG@10 for both rankings
# --------------------------------------------------------
vector_ranked_ids = [
doc["id"]
for doc in candidates
]
reranked_ids = [
doc["id"]
for doc in reranked
]
embedding_ndcg = ndcg_at_k(
vector_ranked_ids,
RELEVANCE_JUDGMENTS,
k=NDCG_K,
)
reranked_ndcg = ndcg_at_k(
reranked_ids,
RELEVANCE_JUDGMENTS,
k=NDCG_K,
)
console.print()
show_ndcg_comparison(
embedding_ndcg,
reranked_ndcg,
k=NDCG_K,
)
# --------------------------------------------------------
# STEP 6
# Calculate ranking movement
# --------------------------------------------------------
reordered = sum(
1
for doc in reranked
if doc["original_rank"]
!= doc["new_rank"]
)
promoted = [
doc
for doc in reranked
if doc["original_rank"]
> doc["new_rank"]
]
biggest_promotion = max(
(
doc["original_rank"]
- doc["new_rank"]
for doc in promoted
),
default=0,
)
# --------------------------------------------------------
# STEP 7
# Show final summary
# --------------------------------------------------------
console.print()
console.print(
Panel(
"[bold green]"
"What Changed?"
"[/bold green]\n\n"
"OpenAI created every document embedding.\n"
"OpenAI created the query embedding.\n"
"Cosine similarity produced the initial ranking.\n\n"
f"Voyage {RERANK_MODEL} then evaluated "
"the original query against the candidate text "
"and refined the ranking.\n\n"
f"[yellow]"
f"{reordered}/{len(reranked)}"
f"[/yellow] "
f"final results changed position.\n"
f"Largest promotion: "
f"[yellow]"
f"{biggest_promotion} positions"
f"[/yellow]\n\n"
f"Embedding-only nDCG@{NDCG_K}: "
f"[cyan]"
f"{embedding_ndcg:.4f}"
f"[/cyan]\n"
f"Embedding + rerank nDCG@{NDCG_K}: "
f"[green]"
f"{reranked_ndcg:.4f}"
f"[/green]\n\n"
"[bold white]"
"The embedding model never changed. "
"Voyage was added only as a second-stage reranker."
"[/bold white]",
title="✨ RESULT ✨",
border_style="green",
box=box.DOUBLE,
)
)
# ============================================================
# Main
# ============================================================
if __name__ == "__main__":
question = (
"Which foods can help lower cholesterol "
"and improve heart health?"
)
run_demo(question)