Skip to content

Commit 54fb0c0

Browse files
authored
Issue 248: optimized the DeprecationWarning (#397)
* del: actual deprecation implemented
1 parent a2a400b commit 54fb0c0

9 files changed

Lines changed: 189 additions & 195 deletions

File tree

src/rank_llm/rerank/listwise/rank_gemini.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import time
2-
from importlib.resources import files
32
from typing import Any
43

54
from tqdm import tqdm
@@ -32,9 +31,6 @@ def populate_generation_config(**kwargs) -> dict[str, Any]:
3231
return generation_config
3332

3433

35-
TEMPLATES = files("rank_llm.rerank.prompt_templates")
36-
37-
3834
class SafeGenai(ListwiseRankLLM):
3935
def __init__(
4036
self,
@@ -52,15 +48,6 @@ def __init__(
5248
max_passage_words: int = 300,
5349
**kwargs,
5450
):
55-
if not prompt_template_path:
56-
if prompt_mode == PromptMode.RANK_GPT_APEER:
57-
prompt_template_path = TEMPLATES / "rank_gpt_apeer_template.yaml"
58-
elif prompt_mode == PromptMode.RANK_GPT:
59-
prompt_template_path = TEMPLATES / "rank_zephyr_template.yaml"
60-
else:
61-
raise ValueError(
62-
"Either `prompt_mode` or `prompt_template_path` must be specified."
63-
)
6451
super().__init__(
6552
model=model,
6653
context_size=context_size,
@@ -82,13 +69,6 @@ def __init__(
8269
keys = [keys]
8370
if not keys:
8471
raise ValueError("Please provide Genai API Keys.")
85-
if prompt_mode and prompt_mode not in [
86-
PromptMode.RANK_GPT_APEER,
87-
PromptMode.RANK_GPT,
88-
]:
89-
raise ValueError(
90-
f"unsupported prompt mode for GEMINI models: {prompt_mode}, expected {PromptMode.RANK_GPT_APEER} or {PromptMode.RANK_GPT}."
91-
)
9272
self._output_token_estimate = None
9373
self._keys = keys
9474
self._cur_key_id = key_start_id or 0

src/rank_llm/rerank/listwise/rank_gpt.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
import time
33
from concurrent.futures import ThreadPoolExecutor, as_completed
4-
from importlib.resources import files
54
from typing import Any
65

76
from tqdm import tqdm
@@ -22,8 +21,6 @@
2221
except ImportError:
2322
tiktoken = None
2423

25-
TEMPLATES = files("rank_llm.rerank.prompt_templates")
26-
2724

2825
class SafeOpenai(ListwiseRankLLM):
2926
def __init__(
@@ -54,8 +51,8 @@ def __init__(
5451
Parameters:
5552
- model (str): The model identifier for the LLM (model identifier information can be found via OpenAI's model lists).
5653
- context_size (int): The maximum number of tokens that the model can handle in a single request.
57-
- prompt_mode (PromptMode, optional): Specifies the mode of prompt generation, with the default set to RANK_GPT,
58-
indicating that this class is designed primarily for listwise ranking tasks following the RANK_GPT methodology.
54+
- prompt_mode (PromptMode, optional): Deprecated and ignored. Prompt behavior is driven by the YAML
55+
template at prompt_template_path; passing prompt_mode only emits a deprecation warning.
5956
- num_few_shot_examples (int, optional): Number of few-shot learning examples to include in the prompt, allowing for
6057
the integration of example-based learning to improve model performance. Defaults to 0, indicating no few-shot examples
6158
by default.
@@ -70,7 +67,7 @@ def __init__(
7067
- api_version (str, optional): The API version, necessary for Azure AI integration.
7168
7269
Raises:
73-
- ValueError: If an unsupported prompt mode is provided or if no OpenAI API keys / invalid OpenAI API keys are supplied.
70+
- ValueError: If no prompt_template_path is provided or if no OpenAI API keys / invalid OpenAI API keys are supplied.
7471
7572
Note:
7673
- This class supports cycling between multiple OpenAI API keys to distribute quota usage or handle rate limiting.
@@ -86,26 +83,6 @@ def __init__(
8683
if not keys:
8784
raise ValueError("Please provide OpenAI Keys.")
8885

89-
if prompt_mode and prompt_mode not in [
90-
PromptMode.RANK_GPT,
91-
PromptMode.RANK_GPT_APEER,
92-
PromptMode.LRL,
93-
]:
94-
raise ValueError(
95-
f"unsupported prompt mode for GPT models: {prompt_mode}, expected {PromptMode.RANK_GPT}, {PromptMode.RANK_GPT_APEER} or {PromptMode.LRL}."
96-
)
97-
98-
if prompt_template_path is None:
99-
if prompt_mode == PromptMode.RANK_GPT:
100-
prompt_template_path = TEMPLATES / "rank_gpt_template.yaml"
101-
elif prompt_mode == PromptMode.RANK_GPT_APEER:
102-
prompt_template_path = TEMPLATES / "rank_gpt_apeer_template.yaml"
103-
elif prompt_mode == PromptMode.LRL:
104-
prompt_template_path = TEMPLATES / "rank_lrl_template.yaml"
105-
else:
106-
raise ValueError(
107-
"Either `prompt_mode` or `prompt_template_path` must be specified."
108-
)
10986
super().__init__(
11087
model=model,
11188
context_size=context_size,

src/rank_llm/rerank/listwise/rank_listwise_os_llm.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ def __init__(
8080
Parameters:
8181
- model (str): Identifier for the language model to be used for ranking tasks.
8282
- context_size (int, optional): Maximum number of tokens that can be handled in a single prompt. Defaults to 4096.
83-
- prompt_mode (PromptMode, optional): Specifies the mode of prompt generation, with the default set to RANK_GPT,
84-
indicating that this class is designed primarily for listwise ranking tasks following the RANK_GPT methodology.
83+
- prompt_mode (PromptMode, optional): Deprecated and ignored. Prompt behavior is driven by the YAML
84+
template at prompt_template_path; passing prompt_mode only emits a deprecation warning.
8585
- num_few_shot_examples (int, optional): Number of few-shot learning examples to include in the prompt, allowing for
8686
the integration of example-based learning to improve model performance. Defaults to 0, indicating no few-shot examples
8787
by default.
@@ -102,7 +102,7 @@ def __init__(
102102
103103
Raises:
104104
- AssertionError: If CUDA is specified as the device but is not available on the system.
105-
- ValueError: If an unsupported prompt mode is provided.
105+
- ValueError: If no prompt_template_path is provided and no default applies.
106106
- ValueError: If num_few_shot_examples > 0 but no valid file path is provided
107107
108108
Note:
@@ -151,10 +151,6 @@ def __init__(
151151
"Open-source listwise reranking with local backends requires PyTorch.",
152152
)
153153
assert torch.cuda.is_available() and torch.cuda.device_count() >= num_gpus
154-
if prompt_mode and prompt_mode != PromptMode.RANK_GPT:
155-
raise ValueError(
156-
f"Unsupported prompt mode: {prompt_mode}. The only prompt mode currently supported is a slight variation of {PromptMode.RANK_GPT} prompt."
157-
)
158154

159155
if sglang_batched:
160156
if Engine is None:

src/rank_llm/rerank/listwise/rank_litellm.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
from concurrent.futures import ThreadPoolExecutor, as_completed
3-
from importlib.resources import files
43
from typing import Any
54

65
from tqdm import tqdm
@@ -16,8 +15,6 @@
1615
except ImportError:
1716
litellm = None
1817

19-
TEMPLATES = files("rank_llm.rerank.prompt_templates")
20-
2118

2219
class SafeLiteLLM(ListwiseRankLLM):
2320
"""Listwise reranker using LiteLLM for 100+ LLM providers.
@@ -55,28 +52,6 @@ def __init__(
5552
"The LiteLLM reranker requires the litellm package.",
5653
)
5754

58-
if prompt_mode and prompt_mode not in [
59-
PromptMode.RANK_GPT,
60-
PromptMode.RANK_GPT_APEER,
61-
PromptMode.LRL,
62-
]:
63-
raise ValueError(
64-
f"unsupported prompt mode for LiteLLM: {prompt_mode}, "
65-
f"expected {PromptMode.RANK_GPT}, {PromptMode.RANK_GPT_APEER} or {PromptMode.LRL}."
66-
)
67-
68-
if prompt_template_path is None:
69-
if prompt_mode == PromptMode.RANK_GPT:
70-
prompt_template_path = TEMPLATES / "rank_gpt_template.yaml"
71-
elif prompt_mode == PromptMode.RANK_GPT_APEER:
72-
prompt_template_path = TEMPLATES / "rank_gpt_apeer_template.yaml"
73-
elif prompt_mode == PromptMode.LRL:
74-
prompt_template_path = TEMPLATES / "rank_lrl_template.yaml"
75-
else:
76-
raise ValueError(
77-
"Either `prompt_mode` or `prompt_template_path` must be specified."
78-
)
79-
8055
super().__init__(
8156
model=model,
8257
context_size=context_size,

src/rank_llm/rerank/rankllm.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import logging
44
import os
5+
import warnings
56
from abc import ABC, abstractmethod
67
from enum import Enum
78
from typing import Any
@@ -45,8 +46,21 @@ def __init__(
4546
self._few_shot_file = few_shot_file
4647

4748
if prompt_mode:
48-
print(
49-
"PromptMode is deprecated and will be removed in v0.30.0. Please use the prompt_template_path argument with a valid template file instead."
49+
warnings.warn(
50+
"PromptMode is deprecated and will be removed in v0.30.0. "
51+
"It is ignored: prompt behavior is now driven by YAML templates. "
52+
"Please use the prompt_template_path argument with a valid YAML "
53+
"template instead.",
54+
DeprecationWarning,
55+
stacklevel=2,
56+
)
57+
58+
if not prompt_template_path:
59+
raise ValueError(
60+
"A YAML prompt template is required: pass prompt_template_path "
61+
"(CLI: --prompt-template-path) pointing to a template under "
62+
"src/rank_llm/rerank/prompt_templates/. The legacy prompt_mode "
63+
"argument no longer selects a template."
5064
)
5165

5266
try:

src/rank_llm/scripts/run_rank_llm.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import argparse
44
import os
5+
import warnings
56
from collections.abc import Sequence
67

78
from rank_llm.cli.legacy import namespace_to_legacy_argv, translate_legacy_argv
@@ -10,18 +11,48 @@
1011
# Force spawn method to avoid "Cannot re-initialize CUDA in forked subprocess" error.
1112
os.environ["VLLM_WORKER_MULTIPROC_METHOD"] = "spawn"
1213

14+
# Flags accepted for backwards compatibility but not forwarded to the new CLI.
1315
_DROP_FLAGS = {"prompt_mode"}
1416

17+
_PROMPT_MODE_DEPRECATION = (
18+
"The --prompt_mode CLI flag is deprecated and will be removed in v0.30.0. "
19+
"It is now ignored: prompt behavior is driven by YAML templates. "
20+
"Pass --prompt-template-path pointing to a template under "
21+
"src/rank_llm/rerank/prompt_templates/ instead "
22+
"(see `rank-llm prompt --help` to list the available templates)."
23+
)
24+
25+
26+
def _warn_if_prompt_mode_present(args: argparse.Namespace | Sequence[str]) -> None:
27+
"""Emit a deprecation warning if the legacy --prompt_mode flag was supplied.
28+
29+
The flag is still accepted so old invocations do not break, but it no longer
30+
affects behavior; YAML prompt templates replace it.
31+
"""
32+
if isinstance(args, argparse.Namespace):
33+
present = getattr(args, "prompt_mode", None) is not None
34+
else:
35+
present = any(
36+
token in ("--prompt_mode", "--prompt-mode")
37+
or token.startswith(("--prompt_mode=", "--prompt-mode="))
38+
for token in args
39+
)
40+
if present:
41+
warnings.warn(_PROMPT_MODE_DEPRECATION, DeprecationWarning, stacklevel=3)
42+
1543

1644
def main(args: argparse.Namespace | Sequence[str] | None = None) -> int:
1745
if isinstance(args, argparse.Namespace):
46+
_warn_if_prompt_mode_present(args)
1847
argv = namespace_to_legacy_argv(args, drop_flags=_DROP_FLAGS)
1948
elif args is None:
2049
import sys
2150

2251
argv = sys.argv[1:]
52+
_warn_if_prompt_mode_present(argv)
2353
else:
2454
argv = list(args)
55+
_warn_if_prompt_mode_present(argv)
2556

2657
translated = translate_legacy_argv(argv, drop_flags=_DROP_FLAGS)
2758
return cli_main(["rerank", *translated])

0 commit comments

Comments
 (0)