Fine-Tuning Assistant by jmsktm/claude-settings
npx skills add https://github.com/jmsktm/claude-settings --skill 'Fine-Tuning Assistant'微调助手技能将引导您完成将预训练模型适配到特定用例的过程。微调可以显著提升模型在专业任务上的性能,教会模型您偏好的风格,并添加仅靠提示无法实现的能力。
本技能涵盖何时选择微调而非提示工程、准备训练数据、选择基础模型、配置训练参数、评估结果以及部署微调模型。它应用了包括 LoRA、QLoRA 和指令调优在内的现代技术,使微调变得实用且经济高效。
无论您是通过 API 微调 GPT 模型、使用开源模型进行本地训练,还是使用 Hugging Face 等平台,本技能都能确保您以战略性和有效的方式进行微调。
收集训练示例:
格式化用于训练:
{"messages": [ {"role": "system", "content": "You are a helpful assistant..."}, {"role": "user", "content": "User input here"}, {"role": "assistant", "content": "Ideal response here"} ]}
:
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
分割训练/验证/测试集
验证数据集格式
选择基础模型:
配置训练:
training_config = {
"model": "gpt-4o-mini-2024-07-18",
"training_file": "file-xxx",
"hyperparameters": {
"n_epochs": 3,
"batch_size": "auto",
"learning_rate_multiplier": "auto"
}
}
# LoRA fine-tuning (local)
lora_config = {
"r": 16, # Rank
"lora_alpha": 32,
"lora_dropout": 0.05,
"target_modules": ["q_proj", "v_proj"]
}
3. 监控训练: * 观察损失曲线 * 检查是否过拟合 * 在保留集上进行验证 4. 评估结果: * 与基线模型比较 * 在多样化输入上测试 * 检查是否有能力退化
| 操作 | 命令/触发 |
|---|---|
| 决定方法 | "Should I fine-tune for [task]" |
| 准备数据 | "Format data for fine-tuning" |
| 选择模型 | "Which model to fine-tune for [task]" |
| 配置训练 | "Fine-tuning parameters for [goal]" |
| 评估结果 | "Evaluate fine-tuned model" |
| 调试训练 | "Fine-tuning loss not decreasing" |
大型模型的高效微调:
from peft import LoraConfig, get_peft_model
lora_config = LoraConfig(
r=16, # Rank of update matrices
lora_alpha=32, # Scaling factor
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM"
)
# Apply LoRA to base model
model = get_peft_model(base_model, lora_config)
# Only ~0.1% of parameters are trainable
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
在消费级硬件上微调大型模型:
from transformers import BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True
)
# Load model in 4-bit
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
quantization_config=bnb_config
)
# Apply LoRA on top
model = get_peft_model(model, lora_config)
将原始数据转换为指令格式:
def create_instruction_example(raw_data):
return {
"messages": [
{
"role": "system",
"content": "You are a customer service agent for TechCorp..."
},
{
"role": "user",
"content": f"Customer inquiry: {raw_data['inquiry']}"
},
{
"role": "assistant",
"content": raw_data['ideal_response']
}
]
}
# Apply to dataset
instruction_dataset = [create_instruction_example(d) for d in raw_dataset]
对微调模型进行全面评估:
def evaluate_fine_tuned_model(model, test_set, baseline_model=None):
results = {
"task_accuracy": [],
"format_compliance": [],
"style_match": [],
"regression_check": []
}
for example in test_set:
output = model.generate(example.input)
# Task-specific accuracy
results["task_accuracy"].append(
check_correctness(output, example.expected)
)
# Format compliance
results["format_compliance"].append(
matches_expected_format(output)
)
# Style matching (for style transfer tasks)
results["style_match"].append(
style_similarity(output, example.expected)
)
# Regression on general capabilities
if baseline_model:
results["regression_check"].append(
compare_general_capability(model, baseline_model, example)
)
return {k: np.mean(v) for k, v in results.items()}
按难度排序训练数据:
def create_curriculum(dataset):
# Score examples by complexity
scored = [(score_complexity(ex), ex) for ex in dataset]
scored.sort(key=lambda x: x[0])
# Create epochs with increasing difficulty
n = len(scored)
curriculum = {
"epoch_1": [ex for _, ex in scored[:n//3]], # Easy
"epoch_2": [ex for _, ex in scored[:2*n//3]], # Easy + Medium
"epoch_3": [ex for _, ex in scored], # All
}
return curriculum
每周安装次数
–
代码仓库
GitHub 星标数
2
首次出现时间
–
安全审计
The Fine-Tuning Assistant skill guides you through the process of adapting pre-trained models to your specific use case. Fine-tuning can dramatically improve model performance on specialized tasks, teach models your preferred style, and add capabilities that prompting alone cannot achieve.
This skill covers when to fine-tune versus prompt engineer, preparing training data, selecting base models, configuring training parameters, evaluating results, and deploying fine-tuned models. It applies modern techniques including LoRA, QLoRA, and instruction tuning to make fine-tuning practical and cost-effective.
Whether you are fine-tuning GPT models via API, running local training with open-source models, or using platforms like Hugging Face, this skill ensures you approach fine-tuning strategically and effectively.
Collect training examples:
Format for training:
{"messages": [ {"role": "system", "content": "You are a helpful assistant..."}, {"role": "user", "content": "User input here"}, {"role": "assistant", "content": "Ideal response here"} ]}
Quality assurance :
Split train/validation/test sets
Validate dataset format
Select base model:
Configure training:
training_config = {
"model": "gpt-4o-mini-2024-07-18",
"training_file": "file-xxx",
"hyperparameters": {
"n_epochs": 3,
"batch_size": "auto",
"learning_rate_multiplier": "auto"
}
}
# LoRA fine-tuning (local)
lora_config = {
"r": 16, # Rank
"lora_alpha": 32,
"lora_dropout": 0.05,
"target_modules": ["q_proj", "v_proj"]
}
3. Monitor training: * Watch loss curves * Check for overfitting * Validate on held-out set 4. Evaluate results: * Compare to baseline model * Test on diverse inputs * Check for regressions
| Action | Command/Trigger |
|---|---|
| Decide approach | "Should I fine-tune for [task]" |
| Prepare data | "Format data for fine-tuning" |
| Choose model | "Which model to fine-tune for [task]" |
| Configure training | "Fine-tuning parameters for [goal]" |
| Evaluate results | "Evaluate fine-tuned model" |
| Debug training | "Fine-tuning loss not decreasing" |
Start with Prompting : Fine-tuning is expensive; exhaust cheaper options first
Quality Over Quantity : 100 excellent examples beat 10,000 mediocre ones
Match Format to Use Case : Training examples should mirror real usage
Don't Over-Train : More epochs isn't always better
Evaluate Properly : Training loss isn't the goal
Version Everything : Fine-tuning is iterative
Efficient fine-tuning for large models:
from peft import LoraConfig, get_peft_model
lora_config = LoraConfig(
r=16, # Rank of update matrices
lora_alpha=32, # Scaling factor
target_modules=["q_proj", "v_proj", "k_proj", "o_proj"],
lora_dropout=0.05,
bias="none",
task_type="CAUSAL_LM"
)
# Apply LoRA to base model
model = get_peft_model(base_model, lora_config)
# Only ~0.1% of parameters are trainable
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
Fine-tune large models on consumer hardware:
from transformers import BitsAndBytesConfig
bnb_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=True
)
# Load model in 4-bit
model = AutoModelForCausalLM.from_pretrained(
"meta-llama/Llama-2-7b-hf",
quantization_config=bnb_config
)
# Apply LoRA on top
model = get_peft_model(model, lora_config)
Convert raw data to instruction format:
def create_instruction_example(raw_data):
return {
"messages": [
{
"role": "system",
"content": "You are a customer service agent for TechCorp..."
},
{
"role": "user",
"content": f"Customer inquiry: {raw_data['inquiry']}"
},
{
"role": "assistant",
"content": raw_data['ideal_response']
}
]
}
# Apply to dataset
instruction_dataset = [create_instruction_example(d) for d in raw_dataset]
Comprehensive assessment of fine-tuned models:
def evaluate_fine_tuned_model(model, test_set, baseline_model=None):
results = {
"task_accuracy": [],
"format_compliance": [],
"style_match": [],
"regression_check": []
}
for example in test_set:
output = model.generate(example.input)
# Task-specific accuracy
results["task_accuracy"].append(
check_correctness(output, example.expected)
)
# Format compliance
results["format_compliance"].append(
matches_expected_format(output)
)
# Style matching (for style transfer tasks)
results["style_match"].append(
style_similarity(output, example.expected)
)
# Regression on general capabilities
if baseline_model:
results["regression_check"].append(
compare_general_capability(model, baseline_model, example)
)
return {k: np.mean(v) for k, v in results.items()}
Order training data by difficulty:
def create_curriculum(dataset):
# Score examples by complexity
scored = [(score_complexity(ex), ex) for ex in dataset]
scored.sort(key=lambda x: x[0])
# Create epochs with increasing difficulty
n = len(scored)
curriculum = {
"epoch_1": [ex for _, ex in scored[:n//3]], # Easy
"epoch_2": [ex for _, ex in scored[:2*n//3]], # Easy + Medium
"epoch_3": [ex for _, ex in scored], # All
}
return curriculum
Weekly Installs
–
Repository
GitHub Stars
2
First Seen
–
Security Audits
AI Elements:基于shadcn/ui的AI原生应用组件库,快速构建对话界面
60,400 周安装