Token导航 LogoToken导航TokenDH.com
开发需要联网github未标认证来源可访问许可证需确认审计通过

ai-fine-tuningAI 微调

Agent Skill

ai-fine-tuning 用于处理 GitHub 仓库、Issue、Pull Request 和代码协作信息,适合在 Codex、Claude、Cursor、Gemini CLI 中需要围绕仓库状态、代码变更或协作事项进行整理时使用。可结合来源仓库、安装命令和原始 README 继续核验具体用法。安装前建议确认权限范围、维护状态,以及是否会触发联网、命令执行或文件读写。

总安装

380

周安装

16

GitHub Stars

3

下载量

133
CodexClaudeCursorGemini CLI

安装说明

本站只整理中文说明和来源信息,不托管安装包,也不代用户安装。

GitHub

来源数

2

许可证

unknown

最后核验

2026-05-01

来源状态

来源可访问

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:ai-fine-tuning(AI 微调)
来源仓库:https://github.com/lebsral/dspy-programming-not-prompting-lms-skills
仓库路径:skills/ai-fine-tuning
安装命令:
npx skills add https://github.com/lebsral/dspy-programming-not-prompting-lms-skills --skill ai-fine-tuning
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。该命令会通过 npx skills 从第三方来源获取 Skill;本站只展示命令,不托管安装包,也不自动执行。

skills.shnpx skills
npx skills add https://github.com/lebsral/dspy-programming-not-prompting-lms-skills --skill ai-fine-tuning

简介

AI 微调技能引导用户判断是否需要微调、准备数据并完成部署。

  • 适用于已有基础模型但需针对特定领域优化的开发场景。
  • 通过 npx 命令安装并使用,建议结合原始 README 核验具体用法。
  • 安装前需确认权限范围、维护状态及是否触发联网或文件读写操作。
  • ai-fine-tuning 属于开发类 Skill,可作为该场景下的辅助能力补充。

SKILL.md

Fine-Tune Models on Your Data

Guide the user through deciding whether to fine-tune, preparing data, running fine-tuning with DSPy, distilling to cheaper models, and deploying. Fine-tuning is powerful but expensive — always confirm prerequisites first.

Should you fine-tune?

Before writing any code, walk through these questions with the user:

  1. Have you optimized prompts first? If not, use /ai-improving-accuracy — prompt optimization is 10x cheaper and often sufficient.
  2. Do you have 500+ labeled examples? Fine-tuning with less data usually overfits. Collect more data first.
  3. Is your baseline accuracy above 50%? If your prompt-optimized program is below 50%, your task definition or data has problems. Fix those first.
  4. What's the goal — quality or cost?

- Quality: You've maxed out prompt optimization and need more accuracy - Cost: You want a small cheap model to match an expensive one

When to fine-tune

  • You've already optimized prompts with MIPROv2 and hit a ceiling
  • You have 500+ labeled examples (1000+ is better)
  • Your baseline is >50% and you need to push higher
  • You want to distill an expensive model into a cheaper one (10-50x cost savings)
  • Your domain has specialized vocabulary or patterns the base model doesn't know
  • You need faster inference (smaller fine-tuned models are faster)

When NOT to fine-tune

  • You haven't tried prompt optimization yet — start with /ai-improving-accuracy
  • You have fewer than 500 examples — need more data? Use /ai-generating-data to bootstrap synthetic examples, or use BootstrapFewShot or MIPROv2 instead
  • Your baseline is below 50% — your data or task definition needs work
  • You're still iterating on what the task is — fine-tuning locks you in
  • You don't have a clear metric — you can't evaluate fine-tuning without one
  • Your use case changes frequently — fine-tuned models don't adapt to new instructions easily

Prerequisites checklist

Before starting, confirm:

  • Data: 500+ labeled examples (1000+ recommended), split 80/10/10 (train/dev/test)
  • Baseline: Prompt-optimized program with measured accuracy (use /ai-improving-accuracy)
  • Metric: Clear, automated metric that scores predictions
  • Compute: API access (OpenAI fine-tuning API) or local GPUs (for open-source models)
  • Budget: OpenAI fine-tuning costs ~$0.008/1K tokens for GPT-4o-mini; local needs 1+ GPU

Step 1: Prepare your data and baseline

Build a strong baseline first

Always compare fine-tuning against a prompt-optimized baseline:

import dspy

lm = dspy.LM("openai/gpt-4o")
dspy.configure(lm=lm)

# Define your program
class Classify(dspy.Signature):
    """Classify the support ticket."""
    text: str = dspy.InputField()
    category: str = dspy.OutputField()

program = dspy.ChainOfThought(Classify)

# Prepare data
import json
with open("labeled_data.json") as f:
    data = json.load(f)

examples = [dspy.Example(text=x["text"], category=x["category"]).with_inputs("text") for x in data]

# Split: 80% train, 10% dev, 10% test
n = len(examples)
trainset = examples[:int(n * 0.8)]
devset = examples[int(n * 0.8):int(n * 0.9)]
testset = examples[int(n * 0.9):]

# Measure baseline
def metric(example, prediction, trace=None):
    return prediction.category.lower() == example.category.lower()

from dspy.evaluate import Evaluate
evaluator = Evaluate(devset=devset, metric=metric, num_threads=4, display_progress=True)
baseline_score = evaluator(program)
print(f"Baseline: {baseline_score:.1f}%")

Optimize prompts first (your comparison point)

optimizer = dspy.MIPROv2(metric=metric, auto="medium")
prompt_optimized = optimizer.compile(program, trainset=trainset)
prompt_score = evaluator(prompt_optimized)
print(f"Prompt-optimized: {prompt_score:.1f}%")

If prompt optimization gets you to your quality goal, stop here. Fine-tuning is only worth it if you need to go further.

Step 2: BootstrapFinetune (core fine-tuning)

The main fine-tuning workflow in DSPy. It bootstraps successful reasoning traces from your training data, filters them by your metric, and fine-tunes the model weights.

optimizer = dspy.BootstrapFinetune(metric=metric, num_threads=24)
finetuned = optimizer.compile(program, trainset=trainset)

# Evaluate the fine-tuned model
finetuned_score = evaluator(finetuned)
print(f"Baseline:         {baseline_score:.1f}%")
print(f"Prompt-optimized: {prompt_score:.1f}%")
print(f"Fine-tuned:       {finetuned_score:.1f}%")

How it works

  1. Bootstrap traces: Runs your program on each training example, keeping traces where the metric passes
  2. Filter by metric: Only successful traces become training data
  3. Fine-tune weights: Sends traces to the model provider's fine-tuning API
  4. Return optimized program: The program now uses the fine-tuned model

Requirements

  • A fine-tunable model (OpenAI gpt-4o-mini, gpt-4o; or local open-source models)
  • 500+ training examples (more traces bootstrapped = better fine-tuning)
  • A metric that reliably identifies good outputs

Step 3: Model distillation (expensive to cheap)

Train a small, cheap model to mimic an expensive model. This is the biggest cost saver — 10-50x reduction with 85-95% quality retention.

Teacher-student pattern

# Step 1: Teacher — expensive model, high quality
teacher_lm = dspy.LM("openai/gpt-4o")
dspy.configure(lm=teacher_lm)

# Build and optimize the teacher
teacher = dspy.ChainOfThought(Classify)
optimizer = dspy.MIPROv2(metric=metric, auto="medium")
teacher_optimized = optimizer.compile(teacher, trainset=trainset)

teacher_score = evaluator(teacher_optimized)
print(f"Teacher (GPT-4o): {teacher_score:.1f}%")

# Step 2: Student — fine-tune cheap model on teacher's outputs
student_lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=student_lm)

student = dspy.ChainOfThought(Classify)
ft_optimizer = dspy.BootstrapFinetune(metric=metric, num_threads=24)
student_finetuned = ft_optimizer.compile(student, trainset=trainset, teacher=teacher_optimized)

student_score = evaluator(student_finetuned)
print(f"Student (GPT-4o-mini, fine-tuned): {student_score:.1f}%")

Typical results

ModelQualityCost per 1M tokens
GPT-4o (teacher)85%~$5.00
GPT-4o-mini (no tuning)70%~$0.15
GPT-4o-mini (fine-tuned)81%~$0.15

The fine-tuned student costs 33x less and retains ~95% of teacher quality.

Step 4: BetterTogether (maximum quality)

BetterTogether alternates between prompt optimization and weight optimization, getting more out of both. Based on the BetterTogether paper (arXiv 2407.10930v2), this approach yields 5-78% gains over either technique alone.

optimizer = dspy.BetterTogether(
    metric=metric,
    prompt_optimizer=dspy.MIPROv2,
    weight_optimizer=dspy.BootstrapFinetune,
)
best = optimizer.compile(program, trainset=trainset)

best_score = evaluator(best)
print(f"Prompt-only:    {prompt_score:.1f}%")
print(f"Fine-tune-only: {finetuned_score:.1f}%")
print(f"BetterTogether: {best_score:.1f}%")

How it works

  1. Round 1: Optimize prompts (instructions + few-shot examples)
  2. Round 2: Fine-tune weights using the optimized prompts
  3. Round 3: Re-optimize prompts for the fine-tuned model
  4. Each round builds on the previous, creating synergy between prompt and weight optimization

When to use BetterTogether

  • You want the absolute best quality and have the compute budget
  • Fine-tuning alone didn't close the gap to your quality target
  • You have 500+ examples and a reliable metric

Step 5: Evaluate and deploy

Thorough evaluation

Always evaluate on the held-out test set (not dev set):

test_evaluator = Evaluate(devset=testset, metric=metric, num_threads=4, display_progress=True)

print(f"Test set results:")
print(f"  Baseline:         {test_evaluator(program):.1f}%")
print(f"  Prompt-optimized: {test_evaluator(prompt_optimized):.1f}%")
print(f"  Fine-tuned:       {test_evaluator(finetuned):.1f}%")

Save and load for production

# Save
finetuned.save("finetuned_program.json")

# Load later
from my_module import MyProgram
production = MyProgram()
production.load("finetuned_program.json")
result = production(text="New support ticket...")

When fine-tuning goes wrong

Can't bootstrap enough traces

If the base model fails on most training examples, there aren't enough successful traces to fine-tune on.

Fixes:

  • Use a stronger model for bootstrapping (GPT-4o instead of GPT-4o-mini)
  • Relax your metric during bootstrapping (accept partial credit)
  • Simplify your task (break multi-step into single steps)

Model overfits (high train accuracy, low test accuracy)

Fixes:

  • Add more training data
  • Reduce fine-tuning epochs (if provider allows)
  • Use a larger base model (less prone to overfitting)
  • Simplify your output format

Fine-tuning didn't improve over prompt optimization

Fixes:

  • Check that bootstrapping produced enough successful traces (need 200+)
  • Try BetterTogether instead of BootstrapFinetune alone
  • Verify your metric actually correlates with quality
  • Try a different base model

Infrastructure choices

OpenAI API (easiest)

Works with gpt-4o-mini and gpt-4o. DSPy handles the fine-tuning API calls automatically:

lm = dspy.LM("openai/gpt-4o-mini")  # fine-tunable via API
  • Pros: No GPU needed, simple setup, fast
  • Cons: Data sent to OpenAI, ongoing per-token costs, limited model choices

Local fine-tuning (own your model)

For open-source models (Llama, Mistral, etc.) using LoRA/QLoRA:

lm = dspy.LM("together_ai/meta-llama/Llama-3-70b-chat-hf")
  • Pros: Data stays private, no per-token costs after training, full control
  • Cons: Needs GPU(s), more setup, slower iteration

Cloud GPU platforms

AWS SageMaker, Google Cloud, Lambda Labs, or Together AI for training:

  • Pros: Scalable, no hardware to manage
  • Cons: Costs vary, setup per platform

Additional resources

  • For worked examples (classification, distillation, BetterTogether), see examples.md
  • Use /ai-improving-accuracy to build a strong baseline before fine-tuning
  • Use /ai-cutting-costs for other cost reduction strategies beyond distillation
  • Use /ai-fixing-errors if fine-tuning or evaluation errors occur
  • Not sure which skill to use next? Try /ai-do to get routed to the right one

适合场景

01

用户想查找某类 Agent Skill 时

02

需要根据任务场景推荐可安装能力包时

03

需要对比不同来源的安装命令和来源信息时

能力概览

能力 1

按任务关键词查找相关 Skills

能力 2

展示可复制的安装命令

能力 3

保留来源站点、仓库和原始说明,方便继续核验

能力 4

展示第三方安全扫描或审计结果

安装后应在对应宿主中按原始 README 的触发条件使用;具体调用方式请以来源页面和 README 为准。

平台分布

Codex

34.83%
按下载量换算46

Claude

29.91%
按下载量换算40

Cursor

16.05%
按下载量换算21

Gemini CLI

9.67%
按下载量换算13

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。当前只有一个来源,正式发布前建议补源仓库或其他目录站核验。

来源信息

继续浏览同类 Skills