Agentic OPD 必须自定义 Rollout(自行控制生成过程,如多轮交互、传入 tools 参数产生结构化 tool_call),Reward 可选。本文介绍不启用 Reward 与启用 Reward 两种模式的函数开发。
tools 参数产生结构化 tool_call(工具调用)等),Reward 可选。由三部分组成:工具定义 + Rollout(采样产轨迹)+ Reward(评分,可选)。训练概述见 在线策略蒸馏训练概述。
开发概览
Agentic OPD 与 Model OPD 的关键区别在是否涉及工具调用、多轮或自定义交互:
- Model OPD 用于纯文本任务,学生模型直接生成文本输出。
- Agentic OPD 用于需工具调用或多轮交互的任务,通过传入
tools参数由框架把工具信息直接传给模型,模型返回结构化的tool_calls字段(原生工具调用)。
RolloutFunctionComponent,训练信号以教师模型蒸馏为主。
两种模式差异如下。
维度 | 无 Reward(只 Rollout) | 有 Reward(Rollout + Reward) |
|---|---|---|
开发工作 | 编写 Rollout 函数(自定义生成过程) | 额外编写 Reward 评分函数 |
训练信号 | 教师模型蒸馏为唯一监督 | 教师蒸馏 + 任务奖励 |
适用场景 | 只需工具调用形态对齐、教师模型已是上限 | 需按业务目标优化工具选择与参数 |
数据格式
每行一条 JSONL,含 messages(对话)与 rollout_extra(附加信息)两个字段。
messages:对话列表,按 user/assistant 交替排列,首轮可为 user 或 system。rollout_extra:附加信息,可存 sample_id、category、solution 等。solution是 JSON 字符串,存标准答案供 Reward 评分;不启用 Reward 时仅作标记不被消费。
query_status、delete_item)在下方工具定义节定义,Rollout 通过传入 tools 参数注入工具见Rollout 函数结构节:
- 单轮 · tool_call
- 多轮 · tool_call
- 单轮 · 无工具调用
query_status 工具查询:Rollout 函数开发
Agentic OPD 必须自定义 Rollout(自行控制生成过程)。本节给出 Rollout 函数开发:工具定义、Rollout 函数结构、Tracing 接入。
工具定义
工具定义是 Rollout 的前置依赖。先定义 tool_spec,再实现 Rollout 处理器,二者同属 functions/ 子包。目录结构如下(__init__.py 使目录成为可导入的 Python 包,缺则 classpath 解析失败):
classpath 指向 functions.rollout.my_rollout.MyRolloutProcessor(即 functions/rollout/my_rollout.py 内的 MyRolloutProcessor 类),Reward 类同理指向 functions.reward.my_reward.MyRewardProcessor。
工具定义含三个部分:
- 系统提示词:约束何时调用工具、何时回复文本前缀
- 工具定义列表:JSON Schema 列表,供 Rollout 内传入
tools参数直接使用,框架自动渲染工具信息给模型;格式与 OpenAI function calling 一致 - 高危操作集合:标记破坏性工具名称,供 Reward 判断模型是否误调危险工具
tools 参数由框架传工具产生原生 tool_calls。
Rollout 函数结构
继承 AbstractRolloutProcessor,实现 async process(input: RolloutInput) -> RolloutOutput。@observe_processor 装饰 process(自动接入 Tracing)。关键步骤:
- 取模型资源:从
RolloutInput取model_resource(api_key/base_url/model_name)与sampling_params - 构造并包装 LLM:构造
ChatOpenAI,调trace_client包装 - 采样:传入
tools参数注入工具定义,ainvoke采样(单轮、不执行工具) - 构造输出:构造
AgentOutput(含原生tool_calls、rollout_metrics),置于RolloutOutput.agent_output - 异常兜底:返回
ROLLOUT_ERROR兜底文本,状态置 FAILED
RolloutInput → AgentOutput → RolloutOutput。
字段 | 来源 | 含义 |
|---|---|---|
| RolloutInput | 含 api_key/base_url/model_name,构造 ChatOpenAI |
| RolloutInput | temperature/max_tokens/timeout 等采样参数 |
| RolloutInput | 输入对话列表,含 system/user/assistant |
| RolloutInput | 透传到 AgentOutput,供 Reward 读取 |
| AgentOutput | 输出对话,末轮 assistant 含 tool_calls |
| AgentOutput | 上报 latency(秒)与 tool_call_count(float) |
| AgentOutput | 占位 0.0,由 Reward 覆写 |
| RolloutOutput | SUCCESS 或 FAILED |
| RolloutOutput | 异常类型名,正常时为 None |
Tracing 接入
Rollout 自构造 LLM 客户端,与 Reward(@observe_processor 隐式注入)不同,需显式调用 trace_client 包装客户端。接入步骤:
- 从
RolloutInput取model_resource(含 api_key/base_url/model_name)与sampling_params,构造 LLM 客户端(如ChatOpenAI) - 调用
trace_client(llm)包装——之后该客户端的所有 LLM 请求自动产生 LLM Span(记录模型名、请求内容、Token 用量、延迟) - 包装后再传入
tools参数并ainvoke采样
ChatOpenAI(推荐,封装 OpenAI 兼容接口);也可直接用 openai SDK,替换 ChatOpenAI 及消息类即可。
不调用 trace_client 则 LLM 请求不产生 Span,轨迹页看不到工具调用链(训练不受影响)。完整 Tracing 配置(装饰器、Span 类型、指标路径)见可观测配置。
无 Reward 开发参考
不启用 Reward 时 functions 列表只含 Rollout 组件,教师模型蒸馏为唯一监督信号。用 AgenticRL 客户端提交,functions 列表只含 RolloutFunctionComponent,不传 RewardFunctionComponent。构造 function_components 指定工作目录与 classpath,配置 teacher_model(教师模型)与学生模型、数据集路径、资源规格。teacher_model 字段触发蒸馏,超参配置见训练配置。
启用 Reward 开发
Reward 为可选组件,启用后在 functions 列表中追加 RewardFunctionComponent。Agentic Reward 比 Model OPD 更简洁:经由 chat-completions 接口,解析器(parser)生效,输出内容(content)干净无需剥离思考文本。
Reward 函数结构
继承 AbstractRewardProcessor,实现 async process(input: RewardInput) -> RewardOutput,@observe_processor 装饰 process(自动接入 Tracing)。process 做五件事:
- 跳过失败样本:Rollout 失败时(末轮 content 以
ROLLOUT_ERROR:开头)直接返回 0 分,不评分 - 取标准答案:
ground_truth(评分基准,优先从RewardInput取,缺失时从数据的rollout_extra.solution读) - 取学生输出:从末轮 assistant 消息取
tool_calls(学生模型产生的工具调用) - 评分:对比标准答案与学生输出,算出
reward_score(主分)与reward_metrics(各维度分) - 构造返回:
reward_score单独放Reward.reward_score,其余维度进reward_metrics,包装成RewardOutput
agent_output(学生输出)由 Rollout 产出,Reward 只读不写。完整 Tracing 配置见可观测配置。
字段 | 来源 | 含义 |
|---|---|---|
| RewardInput | 评分基准,可为 None |
| RewardInput | Rollout 产出,Reward 只读 |
| agent_output.messages | 含 tool_calls 的对话轨迹 |
| agent_output.rollout_extra | 透传的附加信息,可取 solution |
| Reward | 主分,单独上报,不进 metrics |
| Reward | 其余维度 dict,每值 float |
| RewardOutput | SUCCESS 或 FAILED |
| RewardOutput | 异常信息,正常时为 None |
评分逻辑
reward_score 为主奖励标量,reward_metrics 为附加维度字典(每值 float)。评分函数实现见上方Reward 函数结构节。关键约定:
- 主分单独上报:
reward_score单独赋给Reward.reward_score,不重复进reward_metrics(metrics.pop("reward_score")) - 维度自定:维度与权重由任务定,按业务自行实现
- 参数比对:argument 比对用整体序列化相等:
json.dumps(args, sort_keys=True, separators=(",", ":"))后字符串比较 - 聚焦业务约束:教师模型蒸馏已提供稠密的分布级监督,Reward 不必面面俱到——把精力放在教师模型难以覆盖的业务约束上(如
safe_action是否误调破坏性工具、format_valid格式是否合规)
提交启用 Reward 的训练任务
写好 MyRewardProcessor 后,在 functions 列表中同时注册 RolloutFunctionComponent 与 RewardFunctionComponent 两个组件。run() 一步完成注册 → 上传数据 → 提交任务。
RolloutFunctionComponent 与无 Reward 模式一致,RewardFunctionComponent 指向 MyRewardProcessor 所在模块。完整字段说明见训练配置。
函数测试
提交训练前先本地验证评分逻辑与 Rollout 输出结构,避免到训练任务里才发现错误(浪费算力)。
对 your_scoring_function 输入样例 messages + ground_truth,断言 reward_score 与各维度值。Rollout 可构造 mock RolloutInput 本地调用 process 验证输出结构。本地单测只验证函数逻辑,不验证远程训练效果。
测试用 pytest 运行(未装先 pip install pytest):
RolloutInput 调用 Rollout process,需填充三个字段:model_resource(api_key/base_url/model_name)、sampling_params(采样参数)、messages(对话列表)。示例:
后续步骤
- 训练参数与提交配置 → 在线策略蒸馏训练配置
- 可观测配置见 在线策略蒸馏可观测配置
- 回顾训练概述 → 在线策略蒸馏训练概述