TRL 文档
评判器
并获得增强的文档体验
开始使用
评判器
TRL 评判器是一个实验性 API,随时可能更改。
TRL 提供评判器以便轻松比较两个补全。
请确保通过运行以下命令安装了所需的依赖项
pip install trl[judges]
使用提供的评判器
TRL 提供了几个开箱即用的评判器。例如,您可以使用 HfPairwiseJudge
通过 Hugging Face 模型中心预训练的模型来比较两个补全
from trl import HfPairwiseJudge
judge = HfPairwiseJudge()
judge.judge(
prompts=["What is the capital of France?", "What is the biggest planet in the solar system?"],
completions=[["Paris", "Lyon"], ["Saturn", "Jupiter"]],
) # Outputs: [0, 1]
定义您自己的评判器
要定义您自己的评判器,我们提供了几个您可以子类化的基类。对于基于排名的评判器,您需要子类化 BaseRankJudge 并实现 BaseRankJudge.judge() 方法。对于成对评判器,您需要子类化 BasePairJudge
并实现 BasePairJudge.judge
方法。如果您想定义一个不属于这些类别的评判器,您需要子类化 BaseJudge 并实现 BaseJudge.judge()
方法。
例如,让我们定义一个偏好较短补全的成对评判器
from trl import BasePairwiseJudge
class PrefersShorterJudge(BasePairwiseJudge):
def judge(self, prompts, completions, shuffle_order=False):
return [0 if len(completion[0]) > len(completion[1]) else 1 for completion in completions]
然后您可以按如下方式使用此评判器
judge = PrefersShorterJudge()
judge.judge(
prompts=["What is the capital of France?", "What is the biggest planet in the solar system?"],
completions=[["Paris", "The capital of France is Paris."], ["Jupiter is the biggest planet in the solar system.", "Jupiter"]],
) # Outputs: [0, 1]
提供的评判器
PairRMJudge
基于 AllenAI 的 PairRM 模型的 LLM 评判器。
此评判器使用 PairRM 模型对给定提示的补全对进行排名。它专为语言模型输出的成对比较而设计。PairRM 模型使用 llm-blender 库加载并在默认 Accelerator 设备上运行。
属性:
blender (llm_blender.Blender
): llm-blender 中 Blender 类的实例。
示例:
>>> pairrm_judge = PairRMJudge()
>>> prompts = ["Translate 'hello' to French", "What's the capital of Japan?"]
>>> completions = [["Bonjour", "Salut"], ["Kyoto", "Tokyo"]]
>>> results = pairrm_judge.judge(prompts, completions)
>>> print(results) # [0, 1] (indicating the first completion is preferred for the first prompt and the second)
此类需要安装 llm-blender 库。使用以下命令安装:pip install llm-blender
。
judge
< source >( prompts: list completions: list shuffle_order: bool = True return_scores: bool = False temperature: float = 1.0 ) → Union[list[int, float]]
参数
- prompts (
list[str]
) — 要评判的提示列表。 - completions (
list[list[str]]
) — 每个提示的补全对列表。 - shuffle_order (
bool
, 可选, 默认为True
) — 是否打乱补全顺序以避免位置偏差。 - return_scores (
bool
, 可选, 默认为False
) — 如果为True
,则返回第一个补全的概率分数,而不是排名(即软评判器)。 - temperature (
float
, 可选, 默认为1.0
) — 如果return_scores
为 True,则用于缩放 logits 的温度。
返回
Union[list[int, float]]
如果 return_scores
为 False
,则返回每个提示的排名列表(0
或 1
),指示首选哪个补全。如果 return_scores
为 True
,则返回第一个补全的 softmax 概率。
引发
ValueError
ValueError
— 如果每个提示的补全数不正好为 2。
使用 PairRM 模型评判给定提示的补全对。
注意:与 llm-blender 不同,排名是 0 索引的(0
表示首选第一个补全)。
HfPairwiseJudge
class trl.HfPairwiseJudge
< source >( model = 'meta-llama/Meta-Llama-3-70B-Instruct' token: typing.Optional[str] = None system_prompt: typing.Optional[str] = None )
参数
- model (
str
, 可选, 默认为"meta-llama/Meta-Llama-3-70B-Instruct"
) — 用于评判的模型。 - token (
str
, 可选) — 用于 huggingface_hub.InferenceClient 的 Hugging Face API 令牌。 - system_prompt (
str
或None
, 可选, 默认为None
) — 用于评判的系统提示。如果未提供,则使用默认提示。请注意,系统提示应包含以下占位符:{prompt}
、{response0}
和{response1}
。此外,推理调用时max_tokens=1
,因此系统提示应要求单 token 响应。
基于 Hugging Face API 和聊天补全的成对评判器。
此评判器与评估聊天模型的质量有关,其中补全是对给定提示的响应。
OpenAIPairwiseJudge
类 trl.OpenAIPairwiseJudge
< 源代码 >( model = 'gpt-4-turbo-preview' system_prompt: typing.Optional[str] = None max_requests: typing.Optional[int] = 1000 )
参数
- model (
str
, 可选, 默认为"gpt-4-turbo-preview"
) — 用于判断的模型。 - system_prompt (
str
或None
, 可选, 默认为None
) — 用于判断的系统提示。如果未提供,则使用默认提示。请注意,系统提示应包含以下占位符:{prompt}
、{response0}
和{response1}
。此外,推理调用时max_tokens=1
,因此系统提示应要求返回单个 token 的响应。 - max_requests (
int
或None
, 可选, 默认为1000
) — 向 OpenAI API 发出的最大请求数。如果设置为None
,则没有限制。
基于 OpenAI API 的判断器。
此评判器与评估聊天模型的质量有关,其中补全是对给定提示的响应。
AllTrueJudge
类 trl.AllTrueJudge
< 源代码 >( judges: list )
参数
- judges (
list[BaseBinaryJudge]
) — BaseBinaryJudge 实例的列表,其决策将被统一。
统一多个 BaseBinaryJudge 实例的决策。
仅当所有内部二元判断器都返回 1
时,才返回 1
。如果任何判断器返回 0
,则返回 0
。如果任何判断器返回 -1
,表示其过程失败,则此判断器也将返回 -1
。
实现 CGPO 论文 中描述的 Judge 混合。
基础类
BaseJudge
判断器的基类。此类的子类应实现 judge
方法。
BaseBinaryJudge
二元判断器的基类。
judge
< 源代码 >( prompts: list completions: list gold_completions: typing.Optional[list[str]] = None shuffle_order: bool = True ) → list[int]
判断给定提示的完成情况。用于评估完成是否满足约束。
此基类应用于实现二元评估,如 CGPO 论文 的 4.1.4 节中所述。它与评估提示完成对是否满足特定约束相关。
注意:如果判断器为任何提示返回 -1,则表示用于计算偏好的内部过程失败。例如,如果底层语言模型或基于规则的约束返回无效答案,则可能会发生这种情况。在这种情况下,调用者应适当处理这些无效索引,可能通过实施回退逻辑或错误处理。
BaseRankJudge
LLM 排名判断器的基类。
示例:
class MyRankJudge(BaseRankJudge):
def judge(self, prompts, completions, shuffle_order=True):
return ... # Your ranking logic here
judge = MyRankJudge()
judge.judge(
prompts=["The capital of France is", "The capital of Germany is"],
completions=[[" Paris", " Marseille", "Lyon"], [" Munich", " Berlin"]]
) # [[0, 1, 2], [1, 0]]
judge
< 源代码 >( prompts: list completions: list shuffle_order: bool = True ) → list[list[int]]
判断给定提示的完成情况,并返回每个完成的排名。
BasePairwiseJudge
成对判断器的基类。
judge
< 源代码 >( prompts: list completions: list shuffle_order: bool = True ) → list[int]
判断给定提示的完成项对。
注意:如果 judge 对任何提示返回 -1
,则表示用于计算偏好的内部过程失败。例如,如果底层语言模型返回了无效答案,则可能发生这种情况。在这种情况下,调用者应适当处理这些无效索引,可能通过实现回退逻辑或错误处理。