为什么需要微调?
凌晨1点45分,我看着Agent 80%的调用都花在通用模型上。明明只有3种任务类型,为什么要让Agent"学所有知识"?
通用模型像大学的通识教育——什么都知道一点,但什么都不精。微调就像专业深造——让Agent成为某个领域的专家。
📊 微调提升数据:
- Task-specific准确率:通用模型78% → 微调后94%(+16%)
- Token消耗:减少40%(不需要写详细的system prompt了)
- 推理延迟:降低25%(模型更专注,不需要"思考该怎么做")
- 成本:微调一次$50-200,但长期使用每天省$20-50
微调方法对比
| 方法 | 成本 | 数据需求 | 硬件 | 效果 |
|---|---|---|---|---|
| LoRA | $50-100 | 100-1000条 | 1×A100 | ⭐⭐⭐⭐ |
| QLoRA | $30-60 | 100-1000条 | 1×RTX 4090 | ⭐⭐⭐ |
| 全量微调 | $200-500 | 1000-10000条 | 8×A100 | ⭐⭐⭐⭐⭐ |
| Prompt优化 | $0(纯人工) | 几十条 | 不需要 | ⭐⭐(有限) |
OpenClaw 微调工作流
微调Pipeline:
收集数据 → 格式化 → 微调训练 → 评估 → 导出 → OpenClaw集成
↓ ↓ ↓ ↓ ↓ ↓
Agent日志 JSONL格式 LoRA训练 验证集测试 Adapters 挂载到OpenClaw
收集数据 → 格式化 → 微调训练 → 评估 → 导出 → OpenClaw集成
↓ ↓ ↓ ↓ ↓ ↓
Agent日志 JSONL格式 LoRA训练 验证集测试 Adapters 挂载到OpenClaw
步骤1:准备训练数据
# 从Agent日志导出微调数据
openclaw agent export-training-data \
--agent-id "my-agent" \
--output "./ft-data/" \
--format "jsonl" \
--min-samples 100 \
--date-range "2026-04-01:2026-05-20"
# 导出的数据格式(每条记录)
# {"messages": [
# {"role": "system", "content": "你是一个财务分析Agent..."},
# {"role": "user", "content": "分析上季度收入"},
# {"role": "assistant", "content": "上季度总收入$1,234,567..."}
# ]}
# 数据增强:如果你有几百条,可以用合成数据扩展
openclaw ft augment-data \
--input "./ft-data/raw.jsonl" \
--output "./ft-data/augmented.jsonl" \
--factor 3 # 扩展到3倍
步骤2:启动LoRA微调
# OpenClaw 微调配置
openclaw ft init \
--base-model "meta-llama/Llama-3.1-8B" \
--method "lora" \
--output-dir "./fine-tuned-model/"
# 微调配置
# ft-config.yaml
base_model: "meta-llama/Llama-3.1-8B"
method: "lora"
data:
train: "./ft-data/augmented.jsonl"
validation: "./ft-data/validation.jsonl"
lora:
r: 16 # LoRA秩
alpha: 32 # 缩放参数
dropout: 0.1 # 防止过拟合
target_modules:
- "q_proj"
- "v_proj"
- "k_proj"
- "o_proj"
training:
batch_size: 4
epochs: 3
learning_rate: 2e-4
warmup_steps: 100
max_seq_length: 4096
save_steps: 500
# 使用Unsloth加速微调
use_unsloth: true
bfloat16: true
# 启动微调
openclaw ft train --config ft-config.yaml \
--gpus 1 \
--project-name "miaoquai-finance-agent" \
--wandb # 用wandb追踪训练过程
步骤3:评估微调结果
# 评估微调后的模型
openclaw ft evaluate \
--model-dir "./fine-tuned-model/checkpoint-500" \
--test-set "./ft-data/test.jsonl" \
--metrics "accuracy,f1,latency,cost"
# 评估结果示例
# ======== 评估报告 ========
# 数据集: 200条测试样本
#
# 指标 | 基线模型 | 微调模型 | 提升
# 准确率 | 78.5% | 93.2% | +14.7%
# F1 Score | 0.761 | 0.928 | +0.167
# 平均延迟 | 1.32s | 0.89s | -32.6%
# Token消耗/请求 | 856 | 512 | -40.2%
# 幻觉率 | 6.2% | 2.1% | -66.1%
#
# ✅ 微调成功!性能提升显著。
步骤4:导出并集成到OpenClaw
# 导出LoRA适配器(只有几十MB)
openclaw ft export \
--model-dir "./fine-tuned-model/checkpoint-500" \
--format "openclaw-lora" \
--output "./exports/miaoquai-finance-lora/"
# 集成到OpenClaw配置
# ~/.openclaw/config.yaml
models:
# 基础模型
- id: "llama-3.1-8b-base"
provider: "ollama"
model: "llama3.1:8b"
endpoint: "http://localhost:11434"
# 微调后的模型(挂载LoRA)
- id: "miaoquai-finance-agent"
provider: "ollama"
model: "llama3.1:8b"
endpoint: "http://localhost:11434"
adapters:
finance-lora:
path: "/path/to/miaoquai-finance-lora/"
weight: 1.0
# 自动路由Agent请求到此模型
tags:
- "finance"
- "domestic"
# 测试微调模型
openclaw chat --model miaoquai-finance-agent \
--message "分析上季度的收入趋势,给出异常点"
Agent模板微调:最佳实践
🎯 微调数据配方:
好的微调数据比模型选择更重要:
- 30% 正常样本:Agent最常见的使用场景
- 20% 边界案例:容易出错的边缘情况
- 20% 拒绝样本:用户请求超出Agent能力时如何优雅拒绝
- 15% 多轮对话:有上下文的对话场景
- 15% 工具调用:包含工具调用格式的数据
工具调用格式微调
如果Agent经常使用自定义工具,微调工具调用格式能大幅提升准确率:
# 微调数据中的工具调用格式
{"messages": [
{"role": "system", "content": "你是一个数据查询Agent,可以使用以下工具:query_database"},
{"role": "user", "content": "查询上个月销售额"},
{"role": "assistant", "content": "让我查一下数据库。",
"tool_calls": [{
"name": "query_database",
"arguments": "{\"query\": \"SELECT SUM(amount) FROM sales WHERE date >= '2026-04-01' AND date < '2026-05-01'\", \"database\": \"prod_sales\"}"
}]},
{"role": "tool", "content": "{\"result\": 1234567}"},
{"role": "assistant", "content": "上个月的总销售额为$1,234,567..."}
]}
持续改进循环
# 1. 持续收集Agent失败案例
openclaw agent collect-failures \
--agent-id "finance-agent" \
--output "./failure-cases/" \
--criteria "user_feedback=negative OR tool_call_error=true"
# 2. 重新训练
openclaw ft train \
--config ft-config-v2.yaml \
--data "./failure-cases/" \
--resume "./fine-tuned-model/"
# 3. A/B测试新旧模型
openclaw ft ab-test \
--model-a "llama-3.1-8b-base" \
--model-b "miaoquai-finance-agent" \
--traffic-split "50:50" \
--duration "2h" \
--metrics "accuracy,latency,user_satisfaction"
⚠️ 避坑指南:
- 微调不等于万能——数据质量比数据量重要
- 别过多微调——模型会"遗忘"通用知识(灾难性遗忘)
- 微调后测试覆盖所有场景,别只验证训练过的
- 生产环境不要直接替换,先灰度测试
- LoRA只是适配器,基础模型版本升级后需要重新微调
🎯 妙趣金句:
"微调Agent就像给员工做在职培训——不是重新培养一个人,而是让他在自己岗位上变得更专业。培训资料(训练数据)的质量,决定了培训效果的上限。"