RankFiD
rankify.models.rank_fid
Request
dataclass
Result
dataclass
ListwiseRankLLM
Bases: RankLLM, ABC
All children of ListwiseRankLLM must implement these functions
- rerank_batched
- run_llm_batched
- run_llm
- create_prompt_batched
- create_prompt
- get_num_tokens
- cost_per_1k_token
- num_output_tokens
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
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 | |
max_tokens()
Returns the maximum number of tokens for a given model
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The maximum token count. |
permutation_pipeline_batched(results, rank_start, rank_end, logging=False, populate_exec_summary=False, use_logits=False, use_alpha=False)
Runs the permutation pipeline on a batch of result objects within the passed in rank range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
results
|
List[Result]
|
The list of result objects to process. |
required |
rank_start
|
int
|
The start index for ranking. |
required |
rank_end
|
int
|
The end index for ranking. |
required |
logging
|
bool
|
Flag to enable logging of operations. Defaults to False. |
False
|
Returns:
| Type | Description |
|---|---|
List[Result]
|
List[Result]: The list of processed result objects after applying permutation. |
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
permutation_pipeline(result, rank_start, rank_end, logging=False, populate_exec_summary=True, use_logits=False, use_alpha=False)
Runs the permutation pipeline on the passed in result set within the passed in rank range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
The result object to process. |
required |
rank_start
|
int
|
The start index for ranking. |
required |
rank_end
|
int
|
The end index for ranking. |
required |
logging
|
bool
|
Flag to enable logging of operations. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
The processed result object after applying permutation. |
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
shuffle_and_rescore(rerank_results, rank_start, rank_end)
Shuffles candidates between rank_start and rank_end, and rescales scores based on new rank.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rerank_results
|
List[Result]
|
List of Result objects to process. |
required |
rank_start
|
int
|
Start index for ranking. |
required |
rank_end
|
int
|
End index for ranking. |
required |
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
sliding_windows_batched(requests, rank_start, rank_end, window_size, step, shuffle_candidates=False, logging=False, populate_exec_summary=False, use_logits=False, use_alpha=False)
Applies the sliding window algorithm to the reranking process for a batch of result objects. Args: requests (List[Request]): The list of request objects to process. rank_start (int): The start index for ranking. rank_end (int): The end index for ranking. window_size (int): The size of each sliding window. step (int): The step size for moving the window. shuffle_candidates (bool, optional): Flag to shuffle candidates before processing. Defaults to False. logging (bool, optional): Flag to enable logging of operations. Defaults to False. Returns: List[Result]: The list of result objects after applying the sliding window technique.
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
sliding_windows(request, rank_start, rank_end, window_size, step, shuffle_candidates=False, logging=False, populate_exec_summary=True, use_logits=False, use_alpha=False)
Applies the sliding window algorithm to the reranking process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The request object to process. |
required |
rank_start
|
int
|
The start index for ranking. |
required |
rank_end
|
int
|
The end index for ranking. |
required |
window_size
|
int
|
The size of each sliding window. |
required |
step
|
int
|
The step size for moving the window. |
required |
shuffle_candidates
|
bool
|
Flag to shuffle candidates before processing. Defaults to False. |
False
|
logging
|
bool
|
Flag to enable logging of operations. Defaults to False. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
The result object after applying the sliding window technique. |
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
get_ranking_cost_upperbound(num_q, rank_start, rank_end, window_size, step)
Calculates the upper bound of the ranking cost for a given set of parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
num_q
|
int
|
The number of queries. |
required |
rank_start
|
int
|
The start index for ranking. |
required |
rank_end
|
int
|
The end index for ranking. |
required |
window_size
|
int
|
The size of each sliding window. |
required |
step
|
int
|
The step size for moving the window. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, int]
|
Tuple[float, int]: A tuple object containing the cost and the total number of tokens used (input tokens + output tokens). |
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
get_ranking_cost(retrieved_results, rank_start, rank_end, window_size, step)
Calculates the ranking cost based on actual token counts from generated prompts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
retrieved_results
|
List[Request]
|
A list of retrieved results for processing. |
required |
rank_start
|
int
|
The start index for ranking. |
required |
rank_end
|
int
|
The end index for ranking. |
required |
window_size
|
int
|
The size of each sliding window. |
required |
step
|
int
|
The step size for moving the window. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[float, int]
|
Tuple[float, int]: A tuple object containing the calculated cost and the total number of tokens used (input tokens + output tokens). |
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
receive_permutation(result, permutation, rank_start, rank_end)
Processes and applies a permutation to the ranking results.
This function takes a permutation string, representing the new order of items, and applies it to a subset of the ranking results. It adjusts the ranks and scores in the 'result' object based on this permutation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
The result object containing the initial ranking results. |
required |
permutation
|
str
|
A string representing the new order of items. Each item in the string should correspond to a rank in the results. |
required |
rank_start
|
int
|
The starting index of the range in the results to which the permutation is applied. |
required |
rank_end
|
int
|
The ending index of the range in the results to which the permutation is applied. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Result |
Result
|
The updated result object with the new ranking order applied. |
Note
This function assumes that the permutation string is a sequence of integers separated by spaces. Each integer in the permutation string corresponds to a 1-based index in the ranking results. The function first normalizes these to 0-based indices, removes duplicates, and then reorders the items in the specified range of the 'result.candidates' list according to the permutation. Items not mentioned in the permutation string remain in their original sequence but are moved after the permuted items.
Source code in rankify/utils/models/rank_llm/rerank/listwise/listwise_rankllm.py
FiD
Bases: T5ForConditionalGeneration
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
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 | |
set_checkpoint(use_checkpoint)
Enable or disable checkpointing in the encoder. See https://pytorch.org/docs/stable/checkpoint.html
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
reset_score_storage()
Reset score storage, only used when cross-attention scores are saved to train a retriever.
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
get_crossattention_scores(n_passages, mask, ids, mask_query=None, output_sequence_lengths=[])
Cross-attention scores are aggregated to obtain a single scalar per passage. This scalar can be seen as a similarity score between the question and the input passage. It is obtained by averaging the cross-attention scores obtained on the first decoded token over heads, layers, and tokens of the input passage.
More details in Distilling Knowledge from Reader to Retriever: https://arxiv.org/abs/2012.04584.
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
overwrite_forward_crossattention()
Replace cross-attention forward function, only used to save cross-attention scores.
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
FiDCrossAttentionScore
Bases: T5ForConditionalGeneration
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
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 | |
set_checkpoint(use_checkpoint)
Enable or disable checkpointing in the encoder. See https://pytorch.org/docs/stable/checkpoint.html
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
reset_score_storage()
Reset score storage, only used when cross-attention scores are saved to train a retriever.
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
get_crossattention_scores(n_passages, mask, ids, mask_query=None, output_sequence_lengths=[])
Cross-attention scores are aggregated to obtain a single scalar per passage. This scalar can be seen as a similarity score between the question and the input passage. It is obtained by averaging the cross-attention scores obtained on the first decoded token over heads, layers, and tokens of the input passage.
More details in Distilling Knowledge from Reader to Retriever: https://arxiv.org/abs/2012.04584.
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
overwrite_forward_crossattention()
Replace cross-attention forward function, only used to save cross-attention scores.
Source code in rankify/utils/models/rank_llm/rerank/listwise/lit5/model.py
PromptMode
RankFiDDistill
Bases: ListwiseRankLLM
Implements RankFiDDistill, a listwise ranking approach leveraging Fusion-in-Decoder (FiD) for effective retriever-reader knowledge distillation.
RankFiDDistill utilizes Fusion-in-Decoder (FiD) for reranking retrieved passages using multi-document cross-attention. The model is optimized for ranking efficiency and distilling knowledge from reader-based models.
References
- Izacard, G. & Grave, E. (2020): Distilling Knowledge from Reader to Retriever for Question Answering. Paper
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
The name or path of the pre-trained RankFiDDistill model. |
context_size |
int
|
The maximum number of passages used for ranking. |
prompt_mode |
PromptMode
|
Defines the prompt template for FiD. |
num_few_shot_examples |
int
|
Number of few-shot examples for ranking. |
window_size |
int
|
The window size for ranking multiple documents at a time. |
step_size |
int
|
The step size for sliding window ranking. |
precision |
str
|
Precision mode ( |
device |
str
|
The device to use ( |
batched |
bool
|
Whether to enable batch processing. |
Example
from rankify.dataset.dataset import Document, Question, Context
from rankify.models.reranking import Reranking
# Define a query and contexts
question = Question("What are the effects of climate change?")
contexts = [
Context(text="Climate change leads to rising sea levels.", id=0),
Context(text="Artificial intelligence is transforming industries.", id=1),
Context(text="Global temperatures are increasing due to CO2 emissions.", id=2),
]
document = Document(question=question, contexts=contexts)
# Initialize RankFiDDistill Reranker
model = Reranking(method='lit5dist', model_name='LiT5-Distill-base')
model.rank([document])
# Print reordered contexts
print("Reordered Contexts:")
for context in document.reorder_contexts:
print(context.text)
Source code in rankify/models/rank_fid.py
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 | |
__init__(model, context_size=150, prompt_mode=PromptMode.LiT5, num_few_shot_examples=0, window_size=20, step_size=10, precision='bfloat16', device='cuda', batched=False)
Initializes RankFiDDistill for reranking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Path or name of the RankFiDDistill model. |
required |
context_size
|
int
|
Number of passages used for ranking. |
150
|
prompt_mode
|
PromptMode
|
Defines the FiD prompt mode. |
LiT5
|
num_few_shot_examples
|
int
|
Number of few-shot examples used for ranking. |
0
|
window_size
|
int
|
Defines the window size for ranking. |
20
|
step_size
|
int
|
Defines the step size for sliding window ranking. |
10
|
precision
|
str
|
Precision format ( |
'bfloat16'
|
device
|
str
|
The device for computation ( |
'cuda'
|
batched
|
bool
|
Whether to use batch processing. |
False
|
Source code in rankify/models/rank_fid.py
rerank_batch(requests, rank_start=0, rank_end=100, shuffle_candidates=False, logging=False, use_logits=False, use_alpha=False, **kwargs)
Reranks documents in batch using RankFiDDistill.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requests
|
List[Request]
|
List of query and candidate passages for ranking. |
required |
rank_start
|
int
|
The starting rank index. |
0
|
rank_end
|
int
|
The ending rank index. |
100
|
shuffle_candidates
|
bool
|
Whether to shuffle candidates before ranking. |
False
|
logging
|
bool
|
Enable logging for debugging. |
False
|
Returns:
| Type | Description |
|---|---|
List[Result]
|
List[Result]: Ranked list of documents. |
Source code in rankify/models/rank_fid.py
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 | |
run_llm(prompts, **kwargs)
Runs RankFiDDistill to generate ranking predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
List[Dict[str, str]]
|
List of query-context pairs formatted for FiD ranking. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, int]
|
Tuple[str, int]: Ranked list of passages. |
Source code in rankify/models/rank_fid.py
create_prompt(result, rank_start, rank_end, use_alpha=False)
Create a prompt based on the result and given ranking range.
Source code in rankify/models/rank_fid.py
get_num_tokens(prompt)
Abstract method to calculate the number of tokens contained in the given prompt.
Source code in rankify/models/rank_fid.py
RankFiDScore
Bases: ListwiseRankLLM
Implements RankFiDScore [18]_, a listwise ranking approach leveraging
Fusion-in-Decoder (FiD) with cross-attention scoring for accurate ranking.
.. _[18]: https://arxiv.org/abs/2012.04584
RankFiDScore utilizes Fusion-in-Decoder (FiD) models optimized for zero-shot listwise ranking by leveraging cross-attention weights for precise passage relevance estimation.
References
- Izacard, G. & Grave, E. (2020): Distilling Knowledge from Reader to Retriever for Question Answering. Paper
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
The name or path of the pre-trained RankFiDScore model. |
context_size |
int
|
The maximum number of passages used for ranking. |
prompt_mode |
PromptMode
|
Defines the prompt template for FiD. |
num_few_shot_examples |
int
|
Number of few-shot examples for ranking. |
window_size |
int
|
The window size for ranking multiple documents at a time. |
step_size |
int
|
The step size for sliding window ranking. |
precision |
str
|
Precision mode ( |
device |
str
|
The device to use ( |
Examples:
Basic Usage:
from rankify.dataset.dataset import Document, Question, Context
from rankify.models.reranking import Reranking
# Define a query and contexts
question = Question("What are the effects of climate change?")
contexts = [
Context(text="Climate change leads to rising sea levels.", id=0),
Context(text="Artificial intelligence is transforming industries.", id=1),
Context(text="Global temperatures are increasing due to CO2 emissions.", id=2),
]
document = Document(question=question, contexts=contexts)
# Initialize RankFiDScore Reranker
model = Reranking(method='lit5score', model_name='LiT5-Score-base')
model.rank([document])
# Print reordered contexts
print("Reordered Contexts:")
for context in document.reorder_contexts:
print(context.text)
Source code in rankify/models/rank_fid.py
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 | |
__init__(model, context_size=150, prompt_mode=PromptMode.LiT5, num_few_shot_examples=0, window_size=20, step_size=10, precision='bfloat16', device='cuda', batched=False)
Initializes RankFiDScore for reranking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
Path or name of the RankFiDScore model. |
required |
context_size
|
int
|
Number of passages to use for ranking. |
150
|
prompt_mode
|
PromptMode
|
Defines the FiD prompt mode. |
LiT5
|
num_few_shot_examples
|
int
|
Number of few-shot examples used for ranking. |
0
|
window_size
|
int
|
Defines the window size for ranking. |
20
|
step_size
|
int
|
Defines the step size for sliding window ranking. |
10
|
precision
|
str
|
Precision format ( |
'bfloat16'
|
device
|
str
|
The device for computation ( |
'cuda'
|
batched
|
bool
|
Whether to use batch processing. |
False
|
Source code in rankify/models/rank_fid.py
rerank_batch(requests, rank_start=0, rank_end=100, shuffle_candidates=False, logging=False, use_logits=False, use_alpha=False, **kwargs)
Reranks documents in batch using RankFiDScore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
requests
|
List[Request]
|
List of requests containing queries and candidate passages. |
required |
rank_start
|
int
|
The starting rank index. |
0
|
rank_end
|
int
|
The ending rank index. |
100
|
shuffle_candidates
|
bool
|
Whether to shuffle candidate passages before ranking. |
False
|
logging
|
bool
|
Enable logging for debugging. |
False
|
Returns:
| Type | Description |
|---|---|
List[Result]
|
List[Result]: The reranked documents. |
Source code in rankify/models/rank_fid.py
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 | |
run_llm(prompts, **kwargs)
Runs RankFiDScore to generate ranking predictions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompts
|
List[Dict[str, str]]
|
List of query-context pairs formatted for FiD ranking. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[str, int]
|
Tuple[str, int]: Ranked list of passages. |
Source code in rankify/models/rank_fid.py
create_prompt(result, rank_start, rank_end, use_alpha=False)
Creates a prompt based on the result and the specified ranking range.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
The result object containing query and candidate passages. |
required |
rank_start
|
int
|
The starting rank index. |
required |
rank_end
|
int
|
The ending rank index. |
required |
use_alpha
|
bool
|
Whether to apply alpha weighting. |
False
|
Returns:
| Type | Description |
|---|---|
Tuple[List[Dict[str, str]], int]
|
Tuple[List[Dict[str, str]], int]: A list of formatted prompts and their token count. |
Source code in rankify/models/rank_fid.py
get_num_tokens(prompt)
Computes the number of tokens in a given prompt string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str
|
The input prompt text. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of tokens in the prompt. |
Source code in rankify/models/rank_fid.py
cost_per_1k_token(input_token)
Returns the estimated cost per 1,000 tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_token
|
bool
|
Whether to compute for input tokens. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The cost per 1,000 tokens. |
Source code in rankify/models/rank_fid.py
num_output_tokens(current_window_size=None)
Computes the number of output tokens for the current window size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_window_size
|
Optional[int]
|
The size of the current ranking window. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The estimated output token count. |