微调:优化大语言模型 - Openclaw Skills

作者:互联网

2026-03-30

AI教程

什么是 微调?

Openclaw Skills 的微调技能为模型优化提供了一套结构化方法,允许开发者突破少样本提示(few-shot prompting)的限制。它解决了训练生命周期中的技术复杂性,包括基于投资回报率(ROI)、风格一致性需求或推理量来决定是否进行微调的关键决策。通过使用 Openclaw Skills 资源,团队可以系统地改进模型行为,同时保持对成本和数据隐私的控制。

该技能对于格式一致性至关重要,或大规模推理量使得小型微调模型比大型通用 API 更具经济性的场景特别有效。它整合了参数高效微调(PEFT)和 LoRA 等最佳实践,确保优化对于现代开发团队而言既易于获取又具有成本效益。

下载入口:https://github.com/openclaw/skills/tree/main/skills/ivangdavila/fine-tuning

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install fine-tuning

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 fine-tuning。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

微调 应用场景

  • 修复无法通过系统提示词解决的持久格式或风格不一致问题
  • 为每月处理超过 100,000 次请求的应用降低长期推理成本
  • 使模型适应仅靠 RAG(检索增强生成)不足的专业领域知识
  • 通过在本地或安全环境中训练模型来实现严格的隐私限制
  • 将复杂工作流从高延迟模型迁移到优化的特定任务版本
微调 工作原理
  1. 使用 Openclaw Skills 决策框架进行可行性分析,确定微调是否是特定用例的正确选择。
  2. 执行数据准备,将原始数据集转换为经过验证的 JSONL 格式,同时进行去重。
  3. 通过对比 OpenAI、Anthropic (via Bedrock)、Google 或开源备选方案,选择最合适的供应商。
  4. 运行成本估算模型,计算训练成本与持续推理节省之间的盈亏平衡点。
  5. 配置技术训练参数,包括学习率、训练轮数(epochs)和 LoRA 秩(ranks),以实现最佳收敛。
  6. 通过使用特定任务指标将微调模型与原始基线进行对比,执行严格的评估。

微调 配置指南

要在工作流中实施这些 Openclaw Skills,请确保您的环境已准备好进行数据处理和供应商通信:

  1. 安装数据验证和训练所需的依赖项:
pip install torch transformers peft datasets jsonlines
  1. 配置您的供应商凭据:
export OPENAI_API_KEY='your-key-here'
# 或用于 AWS Bedrock
export AWS_ACCESS_KEY_ID='your-id'
  1. 将训练数据结构化为 80/10/10 的拆分比例(训练、验证、测试),以确保评估指标有效。

微调 数据架构与分类体系

该技能在 Openclaw Skills 生态系统中将其技术文档和数据结构组织成清晰的分类:

主题 组件 描述
供应商 providers.md 价格、延迟和可用基础模型的对比。
数据准备 data-prep.md JSONL 模式、去重逻辑和 PII(个人身份信息)脱敏规则。
配置 training.md 针对 LoRA、SFT 和 RLHF 的超参数模板。
经济学 costs.md 投资回报率(ROI)计算器和推理成本对比表。
质量保证 evaluation.md 基线对比指标和损耗曲线分析指南。
安全 compliance.md 审计日志规范和数据驻留配置。
name: Fine-Tuning
slug: fine-tuning
description: Fine-tune LLMs with data preparation, provider selection, cost estimation, evaluation, and compliance checks.

When to Use

User wants to fine-tune a language model, evaluate if fine-tuning is worth it, or debug training issues.

Quick Reference

Topic File
Provider comparison & pricing providers.md
Data preparation & validation data-prep.md
Training configuration training.md
Evaluation & debugging evaluation.md
Cost estimation & ROI costs.md
Compliance & security compliance.md

Core Capabilities

  1. Decide fit — Analyze if fine-tuning beats prompting for the use case
  2. Prepare data — Convert raw data to JSONL, deduplicate, validate format
  3. Select provider — Compare OpenAI, Anthropic (Bedrock), Google, open source based on constraints
  4. Estimate costs — Calculate training cost, inference savings, break-even point
  5. Configure training — Set hyperparameters (learning rate, epochs, LoRA rank)
  6. Run evaluation — Compare fine-tuned vs base model on task-specific metrics
  7. Debug failures — Diagnose loss curves, overfitting, catastrophic forgetting
  8. Handle compliance — Scan for PII, configure on-premise training, generate audit logs

Decision Checklist

Before recommending fine-tuning, ask:

  • What's the failure mode with prompting? (format, style, knowledge, cost)
  • How many training examples available? (minimum 50-100)
  • Expected inference volume? (affects ROI calculation)
  • Privacy constraints? (determines provider options)
  • Budget for training + ongoing inference?

Fine-Tune vs Prompt Decision

Signal Recommendation
Format/style inconsistency Fine-tune ?
Missing domain knowledge RAG first, then fine-tune if needed
High inference volume (>100K/mo) Fine-tune for cost savings
Requirements change frequently Stick with prompting
<50 quality examples Prompting + few-shot

Critical Rules

  • Data quality > quantity — 100 great examples beat 1000 noisy ones
  • LoRA first — Never jump to full fine-tuning; LoRA is 10-100x cheaper
  • Hold out eval set — Always 80/10/10 split; never peek at test data
  • Same precision — Train and serve at identical precision (4-bit, 16-bit)
  • Baseline first — Run eval on base model before training to measure actual improvement
  • Expect iteration — First attempt rarely optimal; plan for 2-3 cycles

Common Pitfalls

Mistake Fix
Training on inconsistent data Manual review of 100+ samples before training
Learning rate too high Start with 2e-4 for SFT, 5e-6 for RLHF
Expecting new knowledge Fine-tuning adjusts behavior, not knowledge — use RAG
No baseline comparison Always test base model on same eval set
Ignoring forgetting Mix 20% general data to preserve capabilities