SuperAPI AI 工具集

02-rl-reward

智能体与模型

查看项目

更新于 10-25 08:46

技能介绍

本技能文档说明如何在使用 openjudge 库时,为基于人类反馈的强化学习(RLHF)和基于 AI 反馈的强化学习(RLAIF)构建奖励信号。

用途

本技能适用于以下场景:

  • 为 GRPO / REINFORCE 的 rollout 评分构建标量奖励
  • 为 DPO / IPO 生成 (chosen, rejected) 偏好对
  • Best-of-N 候选选择
  • 多维奖励塑造(正确性 + 安全性 + 格式)
  • 用 LLM-as-judge 替换或引导奖励模型

主要能力

奖励策略选择

文档提供决策树辅助选择奖励策略:

  • GRPO / REINFORCE — 可验证任务(数学、代码、结构化输出):使用 POINTWISE(FunctionGrader,精确评分,零 LLM 成本)。
  • GRPO / REINFORCE — 主观任务(指令遵循、对话、摘要):使用 PAIRWISE TOURNAMENT(将每个 rollout 与组内其他 rollout 比较,奖励为组内净胜率)。
  • DPO / IPO / SLiC — 需要 (chosen, rejected) 对:使用 PAIRWISE(双向比较,返回赢家/输家)。
  • Best-of-N / 重排序 — 对 N 个候选排名:使用 LISTWISE(一次调用对全部 N 个排名)。

成本约束下的建议:

  • 低预算:FunctionGrader(免费)→ pointwise;或使用小评判模型的 pairwise。
  • 中等预算:Pointwise 使用 2–3 个 LLM 评分器 + WeightedSumAggregator;Pairwise 锦标赛使用 1 个 LLM 评判,每组 N*(N-1)/2 次比较。
  • 高质量/无成本限制:Pointwise 投票(3–5 次调用)或使用强评判模型 + 去偏的 pairwise。

策略对比

  • Pointwise:输出每个响应的标量,奖励信号为直接奖励 r(x, y),用于可验证任务的 GRPO、过滤,成本低–中。
  • Pairwise Tournament:输出每个响应的净胜率,奖励信号为组内相对奖励,用于主观任务的 GRPO,成本中等(N²/2 次调用)。
  • Pairwise:输出赢家/输家对,奖励信号为隐式偏好 y+ > y-,用于 DPO、IPO、RLAIF 偏好数据,成本中等。
  • Listwise:输出对 N 个响应的排名,奖励信号为序数奖励/重排序,用于 Best-of-N、重排序,成本中–高。

分数归一化

所有评分器返回的分数尺度不同,输入 RL 前必须归一化。文档提供归一化函数示例(代码内容保留):

def normalize(score: float, min_score: float, max_score: float) -> float: """Map [min_score, max_score] → [0.0, 1.0].""" if max_score == min_score: return 0.0 return (score - min_score) / (max_score - min_score)

LLM graders (common/*) return 1–5 → normalize to 0–1

reward = normalize(result.score, min_score=1, max_score=5)

FunctionGrader / text graders already return 0–1 → no normalization needed

评估策略

评估策略控制评分器的调用次数和结果聚合方式,独立于评分器本身。

选择决策:

  • 确定性评分器(FunctionGrader、StringMatch、CodeExecution 等):使用 Direct(零方差,无需聚合)。
  • LLM 评分器 — Pointwise 评分:
    • 预算有限/速度关键:使用 Direct(接受方差,1× 成本)。
    • 离散分数(1–5 整数、通过/失败、二元):使用 Voting(多数投票,对异常值稳健,N× 成本)。
    • 连续/细粒度分数(需要精确排名):使用 Average(均值,保留信号,N× 成本)。
  • LLM 评分器 — Pairwise GRPO 锦标赛:使用 GRPOTournament(全对比较,净胜率)。

策略对比:

  • DirectEvaluationStrategy:无聚合,适用于确定性评分器、低预算,1× 成本。
  • VotingEvaluationStrategy:多数投票,适用于离散/整数 LLM 分数,N× 成本。
  • AverageEvaluationStrategy:均值,适用于连续 LLM 分数,N× 成本。
  • GRPOTournamentEvaluationStrategy:净胜率,适用于主观任务的 Pairwise GRPO,N²/2× 成本。

所有策略从 openjudge.evaluation_strategy 导入。

示例代码

Pointwise 降低噪声(投票/平均):

from openjudge.evaluation_strategy import VotingEvaluationStrategy

grader = CorrectnessGrader( model=model, strategy=VotingEvaluationStrategy(num_votes=3, tie_breaker="closest_to_mean"), )

Now each call internally runs 3 LLM evaluations and returns the most common score

使用奇数 num_votes(3、5)避免平局。

Pairwise — GRPO 锦标赛:

from openjudge.evaluation_strategy import GRPOTournamentEvaluationStrategy

strategy = GRPOTournamentEvaluationStrategy(debiased=False) results = await strategy.execute( pairwise_grader.aevaluate, query="Write a haiku about the ocean.", responses=["rollout_1", "rollout_2", "rollout_3", "rollout_4"], ) rewards = [r.score for r in results] # net win rates in [-1.0, 1.0]

设置 debiased=True 会以两种顺序运行每对比较,只统计一致结果(LLM 调用翻倍但缓解位置偏差)。

使用方式

安装命令:

pip install py-openjudge

编写代码前应先阅读相关子文档:

  • pointwise.md:逐点多维奖励(GRPO 可验证任务、多维评分)
  • pairwise.md:成对奖励(GRPO 主观任务锦标赛、DPO/RLAIF 偏好对)

适用场景

  • 需要为强化学习构建奖励信号的开发者和研究者
  • 使用 GRPO、REINFORCE、DPO、IPO、SLiC 等算法进行训练
  • 需要多维奖励塑造或 Best-of-N 候选选择
  • 希望用 LLM-as-judge 替代或引导奖励模型

限制

  • 使用前需安装 py-openjudge 库。
  • 不同评分器输出分数尺度不同,必须进行归一化。
  • Pairwise 锦标赛计算成本为 N²/2 次调用,需考虑成本限制。
  • 未提供具体价格、平台支持或性能基准。
  • 文档未详细说明各评分器的具体实现细节,需参考子文档。