Dressage白盒Agent实战:ALFWorld与HotpotQA两个文本智能体强化学习训练完整教程

原创2026-10-26 10:52:171,814 阅读

Dressage白盒Agent实战:ALFWorld与HotpotQA两个文本智能体强化学习训练完整教程

Dressage 是一个可扩展的智能体强化学习(Agentic RL)训练框架,它让 LLM 智能体在使用真实工具(检索、文本环境交互等)时也能获得完整的 RL 梯度流。本文将带你用 Dressage 训练两个开箱即用的白盒 Agent(Whitebox Agent):在文本家居环境中完成家务任务的 ALFWorld 导航智能体,以及基于本地 FAISS+BGE 检索的多跳问答智能体 HotpotQA。无需修改框架源码,只需准备好数据和模型,即可跑通完整的 RL 训练流水线。

Dressage白盒Agent训练架构:推理代理记录轨迹并驱动智能体强化学习

什么是白盒Agent?

Dressage 支持两类智能体训练范式:

  • 白盒(Whitebox):Agent 逻辑用 Python 编写,直接调用 self.chat() 经过 Dressage 推理代理,再调用自己的 Python 工具函数。适合检索、API 调用、轻量文本环境等场景,易调试、延迟低。
  • 黑盒(Blackbox):委托给 opencode、claude_code 等 HTTP 智能体框架,适合真实编码场景。

白盒 Agent 的核心结构非常清晰——继承 whitebox_agent.py 中的 WhiteboxAgent 基类:

你的 Python Agent
  └── self.chat(messages)          → Proxy → SGLang(每个 token 都被记录)
  └── 工具函数(Python)            → 返回观察/检索结果
  └── 循环直到任务完成              → 轨迹转为训练样本

两次训练脚本分别提供 Sync(同步) 与 Async(全异步) 两种调度模式:同步模式简单、便于调试;异步模式让 GPU 利用率更高。

环境准备:模型与仓库

1. 克隆仓库并初始化

git clone --recurse-submodules https://gitcode.com/gh_mirrors/dr/Dressage
cd Dressage

--recurse-submodules 会自动拉取底层训练框架 slime。若未拉取,可执行 git submodule update --init --recursive。

2. 下载并转换 Qwen3.5-4B 模型

pip install -U huggingface_hub

hf download Qwen/Qwen3.5-4B --local-dir /root/Qwen3.5-4B

# 转换为 Slime 训练所需的 torch-dist 格式
source slime/scripts/models/qwen3.5-4B.sh
torchrun --nproc_per_node 8 slime/tools/convert_hf_to_torch_dist.py \
  --hf-checkpoint /root/Qwen3.5-4B \
  --save /root/Qwen3.5-4B_torch_dist \
  "${MODEL_ARGS[@]}"

# 初始化可训练检查点目录
cp -a /root/Qwen3.5-4B_torch_dist /root/Qwen3.5-4B_slime

完成后的目录结构应为:

/root/
  Qwen3.5-4B/
  Qwen3.5-4B_torch_dist/
  Qwen3.5-4B_slime/

ALFWorld 文本家居智能体实战

任务是什么?

ALFWorld 是构建在 TextWorld 引擎上的文本交互环境。Agent 收到自然语言指令(如"把一个干净的杯子放到台面上"),通过发送文本命令在模拟家庭中导航完成任务。

任务:"Put a clean cup on the counter"
  1. "go to kitchen"        → 看到厨房、橱柜和台面
  2. "open cabinet 1"      → 看到一个脏杯子
  3. "take cup from cabinet 1" → 拿起杯子
  4. "clean cup with sink"   → 清洗杯子
  5. "put cup on counter"    → ✅ 任务完成!

Agent 工作原理

ALFWorld 智能体(agent_whitebox.py)遵循简洁有效的循环:让 LLM 给出下一个动作 → 在环境执行 → 把观察喂回对话 → 重复,直到任务完成或达到 ALFWORLD_MAX_STEPS=30 上限。

  • 工具:env_step 函数把文本命令发给 ALFWorld 环境,返回观察和 done 信号
  • 奖励(reward.py):纯二元信号——任务成功得 1.0,否则 0.0。刻意不做格式/长度塑形,以避免 reward hacking

三步跑通训练

① 安装依赖并下载游戏数据

python3 -m pip install -U pip setuptools wheel
python3 -m pip install alfworld textworld gymnasium pyyaml

mkdir -p examples/data/alfworld/alfworld_data
ALFWORLD_DATA="$(pwd)/examples/data/alfworld/alfworld_data" alfworld-download

② 构建训练数据集

python3 examples/data/alfworld/prepare_alfworld.py \
  --alfworld-data examples/data/alfworld/alfworld_data \
  --output-dir examples/data/alfworld \
  --split train

该脚本会扫描 ALFWorld train 分区并生成 examples/data/alfworld/train.jsonl,其中保存仓库相对路径的 game_file,供 rollout 时加载环境。

③ 启动训练

# 同步 rollout —— 简单,适合调试
bash examples/scripts/run_alfworld_whitebox_agent_qwen3.5_4b.sh

# 全异步 rollout —— GPU 利用率更高
bash examples/scripts/run_alfworld_whitebox_agent_qwen3.5_4b_async.sh

训练曲线展示了 ALFWorld 白盒智能体的原始奖励均值(rollout/raw_reward_trajectory_mean)随 200 个训练步的变化,同步与异步两种模式均稳定上升至 0.95 以上:

Dressage ALFWorld白盒智能体同步模式强化学习训练奖励曲线

Dressage ALFWorld白盒智能体异步模式强化学习训练奖励曲线

HotpotQA 多跳检索智能体实战

任务是什么?

HotpotQA 是需要跨多个维基段落做多跳推理的问答数据集。Dressage 的 HotpotQA 智能体(agent_whitebox.py)配备本地 FAISS + BGE 检索工具,采用"检索→推理→再检索→作答"的循环:

问题:"What is the birthplace of the director of Inception?"
  1. 搜索 "director of Inception" → "Inception is directed by Christopher Nolan"
  2. 搜索 "Christopher Nolan birthplace" → "born in London, England"
  3. 作答:<answer>London, England</answer> ✅

奖励函数(reward.py)采用纯精确匹配(Exact Match):取最后一个 <answer>...</answer> 块,归一化后与标准答案比对,完全一致得 1.0,否则 0.0。不提供 F1 部分分或格式加分——实验表明这些会让策略坍缩到"跳过检索直接猜"的捷径上。

三步跑通训练

① 安装检索依赖并下载 BGE 嵌入模型

python3 -m pip install -U datasets sentence-transformers faiss-cpu huggingface_hub

hf download BAAI/bge-large-en-v1.5 --local-dir /root/bge-large-en-v1.5

② 构建数据集与检索索引

# 从 hotpot_qa 数据集(distractor 配置)构建 train/dev JSONL
python3 examples/data/hotpotqa/prepare_hotpotqa.py \
  --out-dir examples/data/hotpotqa

# 抽取段落语料并用 BGE 构建 FAISS 索引
python3 examples/data/hotpotqa/build_corpus.py \
  --out-dir examples/data/hotpotqa/corpus

python3 examples/data/hotpotqa/build_index.py \
  --corpus examples/data/hotpotqa/corpus/hpqa_corpus.jsonl \
  --out-dir examples/data/hotpotqa/corpus \
  --embedding-model /root/bge-large-en-v1.5 \
  --devices cuda:0,cuda:1,cuda:2,cuda:3,cuda:4,cuda:5,cuda:6,cuda:7 \
  --batch-size 2048

索引构建后,corpus/ 目录包含 hpqa_corpus.jsonl、hpqa_corpus.npy 和 index.bin,供本地检索使用。GPU 显存不足时可改用 --devices cpu。

③ 启动训练

# 同步 rollout
bash examples/scripts/run_hotpotqa_whitebox_agent_qwen3.5_4b.sh

# 全异步 rollout
bash examples/scripts/run_hotpotqa_whitebox_agent_qwen3.5_4b_async.sh

HotpotQA 的奖励曲线同样记录 200 步训练的 rollout/raw_reward_trajectory_mean,异步模式在训练后段稳定在 0.8 附近:

Dressage HotpotQA白盒智能体同步模式强化学习训练奖励曲线

Dressage HotpotQA白盒智能体异步模式强化学习训练奖励曲线

关键配置速查

两个 recipe 脚本内部已自动设置好核心环境变量,理解它们有助于你自定义 Agent:

变量 值 说明
DRESSAGE_PADDOCK_MODE whitebox 白盒交互范式
DRESSAGE_SANDBOX_PROVIDER local_bwrap 沙箱后端(ALFWorld 使用 command_only 池模式)
--custom-generate-function-path dressage.recipes.alfworld.agent_whitebox.generate 指向你的 Agent 生成函数
--custom-rm-path dressage.reward.custom_rm.custom_rm 奖励入口
DRESSAGE_REWARD_MODULES dressage.recipes.alfworld.reward 奖励模块注册路径
ALFWORLD_MAX_STEPS 30 单回合最大步数

脚本支持通过环境变量覆盖数据根目录和模型目录:

DATA_ROOT=/some/data/root \
BASE_FOLDER=/root \
HOTPOTQA_EMBEDDING_DEVICE=cuda:0 \
bash examples/scripts/run_hotpotqa_whitebox_agent_qwen3.5_4b.sh

完整参数说明见 quickstart.md,两个 recipe 的数据准备细节见 whitebox-agent-quickstart.md。

常见问题排查

问题 解决方案
零奖励(所有轨迹 reward=0) 检查奖励注册:确保 DRESSAGE_REWARD_MODULES 包含你的模块,且使用了 @register_reward 装饰器
模式不匹配错误 DRESSAGE_PADDOCK_MODE 必须与池模式对应:whitebox ↔ command_only
TITO 失败警告 检查模型兼容性,失败时框架会自动回退到安全的分段边界
调试轨迹数据 设置 DRESSAGE_TRAJECTORY_PAYLOAD_LOG_DIR=/tmp/dressage_logs 落盘完整轨迹

总结与延伸阅读

本文用 Dressage 跑通了两个白盒文本智能体的完整 RL 训练:

  • ALFWorld:TextWorld 导航 + env_step 工具 + 二元任务奖励,200 步后成功率奖励稳定在 0.95+
  • HotpostQA:FAISS+BGE 多跳检索 + 纯 EM 奖励,异步训练后稳定在 0.8 附近

两个 recipe 都是绝佳的模板:子集 WhiteboxAgent → 实现 rollout() → make_generate 导出 → 注册奖励 → 编写训练脚本。想构建自己的 Agent,可参考 recipes.md 中的"Building Your Own Recipe"章节;想深入理解轨迹记录、多段训练与 TITO 机制,建议继续阅读 proxy.md 和 training.md。

登录后查看全文
Dressage