Skip to content

Hoder-zyf/openhands-magic

Repository files navigation

Fine-tuning Pipeline

自动化 LLM 微调流程:数据清洗 → 训练 → 评测 → 迭代优化

安装

环境准备

cp init.sh ../ && bash init.sh

快速开始

cd finetune/finetune_pipeline
python main.py --config configs/default.yaml

命令行参数

参数 说明 示例
--config 配置文件路径 --config configs/default.yaml
--dataset 数据集名称 --dataset math
--max-iterations 最大迭代次数 --max-iterations 3
--timeout 超时时间(小时) --timeout 6.0
--resume 恢复之前的运行 --resume 20260117_120000
--gpu-ids 指定使用的 GPU --gpu-ids 0,1
--max-samples 最大训练样本数 --max-samples 1000

配置文件参考

完整配置示例 (configs/default.yaml):

# LLM 配置 - Agent 对话使用的模型
# 也可通过环境变量设置: LLM_API_KEY, LLM_BASE_URL, LLM_MODEL
llm:
  api_key: "sk-xxxx"              # LLM API 密钥
  base_url: "http://localhost:4001/v1"  # API 端点 (可选)
  model: "gpt-4o"                 # Agent 使用的模型

# 模型池 - 数据清洗脚本中可调用的模型
# 用于 CoT 生成、数据增强、质量评估等
model_pool:
  strong_models:                  # 复杂任务使用
    - "gpt-4o"
  weak_models:                    # 简单/快速任务使用
    - "gpt-4o-mini"
  api_base: "http://localhost:4001/v1"
  api_key: "sk-xxxx"
  max_workers: 80                 # 并发 API 调用数
  timeout: 120                    # API 超时时间(秒)

# HuggingFace 配置
# 访问 gated 数据集需要 (如 OpenMol/ChemCoTBench)
# 也可通过 HF_TOKEN 环境变量设置
huggingface:
  hf_token: "hf_xxxx"

# Pipeline 配置
pipeline:
  max_iterations: 10              # 最大迭代次数
  timeout_hours: 12.0             # 全局超时(小时), 0 表示不限时
  results_summary_path: results/final_summary.json  # 结果汇总保存路径

# 数据配置
data:
  dataset: chemcot_mol_und        # 数据集名称 (见 datasets.json)
  datasets_config: configs/datasets.json  # 数据集映射文件路径
  max_samples: 2000               # 最大清洗样本数

# 训练配置
training:
  base_model: Qwen/Qwen2.5-7B-Instruct  # 基座模型
  num_gpus: 1                     # GPU 数量
  gpu_ids: []                     # 指定 GPU ID, 空列表表示自动选择

# 评测配置
evaluation:
  benchmarks:                     # 评测基准
    - chemcotbench_mol_und
  validation_range: "[:min(100, len(index_list)//2)]"  # 验证集范围
  test_range: "[-min(100, len(index_list)//2):]"       # 测试集范围
  # LLM Judge 配置 (用于需要 LLM 评分的基准)
  judge_model: Qwen/Qwen2.5-32B-Instruct
  judge_api_base: "http://localhost:4001/v1"
  judge_api_key: "sk-xxxx"

# 工作目录
workspace: .

可用数据集

名称 说明
chemcot_mol_und 化学分子理解 (默认)
chemcot_mol_edit 化学分子编辑
chemcot_mol_opt 化学分子优化
chemcot_rxn 化学反应
math 数学推理 (DeepScaler)
financeiq 金融问答
panorama_noc4pc Panorama NOC4PC
panorama_par4pc Panorama PAR4PC
panorama_pi4pc Panorama PI4PC
tableinstruct_data_analysis 表格数据分析
tableinstruct_fact_checking 表格事实核查
tableinstruct_numerical_reasoning 表格数值推理
tableinstruct_visualization 表格可视化
bioprobench_gen 生物编程 - 代码生成
bioprobench_ord 生物编程 - 代码排序
bioprobench_err 生物编程 - 错误检测
bioprobench_pqa 生物编程 - 问答

Pipeline 工作流

  1. Data Cleaning Agent: 分析原始数据,编写并执行数据清洗脚本,生成 cleaned_data.jsonl
  2. Training Agent: 使用 LlamaFactory 进行 LoRA 微调,自动决定训练参数
  3. Evaluation Agent: 使用 OpenCompass 在验证集上评测模型性能
  4. Checkpoint 选择: 每 3 轮迭代后,Agent 分析历史结果,决定是继续迭代还是回退到最佳 checkpoint
  5. 最终模型选择: 迭代完成后,综合分析所有迭代结果,选择最优模型
  6. 测试集评测: 对选定的最优模型在测试集上进行最终评测

输出目录结构

outputs/{benchmark}/{timestamp}/
├── iteration_1/
│   ├── cleaned_data.jsonl        # 清洗后的训练数据
│   ├── clean_data.py             # 数据清洗脚本
│   ├── clean_data.log            # 清洗日志
│   ├── model/                    # 训练得到的 LoRA 模型
│   └── eval_validation/          # 验证集评测结果
├── iteration_2/
│   └── ...
├── iteration_3/
│   └── ...
├── checkpoint_iter3_conversation.json   # 第3轮后 Checkpoint 选择对话
├── checkpoint_iter6_conversation.json   # 第6轮后 Checkpoint 选择对话
├── ...
├── model_selection_conversation.json    # 最终模型选择对话
├── final_test/                          # 测试集评测结果
├── pipeline_results.json                # 完整 Pipeline 结果
└── latest -> iteration_N/               # 指向最新迭代的软链接

pipeline_results.json 结构

{
  "iterations": [
    {
      "iteration": 1,
      "data_cleaning": {"status": "success", "samples": 1500},
      "training": {"status": "success", "model_path": "..."},
      "evaluation": {
        "status": "success",
        "scores": {"chemcotbench_mol_und": 0.75},
        "metrics": {"chemcotbench_mol_und": "accuracy"}
      }
    }
  ],
  "config": {
    "max_iterations": 10,
    "dataset": "chemcot_mol_und"
  },
  "best_iteration_so_far": 5,
  "selected_iteration": 5,
  "best_validation_score": 0.85,
  "best_validation_result": {
    "scores": {"chemcotbench_mol_und": 0.85},
    "metrics": {"chemcotbench_mol_und": "accuracy"}
  },
  "test_score": 0.83,
  "test_result": {
    "scores": {"chemcotbench_mol_und": 0.83},
    "metrics": {"chemcotbench_mol_und": "accuracy"}
  },
  "best_model": "/path/to/outputs/benchmark/timestamp/iteration_5/model",
  "final_test_path": "/path/to/outputs/benchmark/timestamp/final_test",
  "status": "completed"
}

关键字段说明

字段 说明
iterations 所有迭代的详细记录
best_iteration_so_far 验证集得分最高的迭代轮次
selected_iteration Agent 最终选择的迭代轮次 (可能与 best 不同)
best_validation_score 最佳验证集得分
best_validation_result 最佳验证集的详细评测结果
test_score 测试集得分
test_result 测试集的详细评测结果
best_model 最终选定模型的路径

使用示例

快速测试 (1轮迭代)

python main.py --config configs/default.yaml --max-iterations 1 --timeout 2.0

指定数据集

python main.py --config configs/default.yaml --dataset math

指定 GPU

python main.py --config configs/default.yaml --gpu-ids 0,1

限制样本数

python main.py --config configs/default.yaml --max-samples 500

恢复之前的运行

python main.py --config configs/default.yaml --resume 20260117_120000

完整示例

python main.py \
  --config configs/default.yaml \
  --dataset chemcot_mol_und \
  --max-iterations 10 \
  --timeout 24.0 \
  --gpu-ids 0,1,2,3 \
  --max-samples 2000

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors