11import asyncio
22import time
33from concurrent .futures import ThreadPoolExecutor , as_completed
4- from importlib .resources import files
54from typing import Any
65
76from tqdm import tqdm
2221except ImportError :
2322 tiktoken = None
2423
25- TEMPLATES = files ("rank_llm.rerank.prompt_templates" )
26-
2724
2825class 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 ,
0 commit comments