ARTICLE - AI

AI Model Fine-Tuning Tutorial: From Dataset Preparation to LoRA Training

This article introduces AI model fine-tuning from dataset preparation and training environment setup to LoRA/QLoRA practice, using a customer-service bot case to build the core skills for customizing general models.

2026-07-18 16IDC

AI Model Fine-Tuning Tutorial: From Dataset Preparation to LoRA Training

Let's start with a real scenario: you are building a customer-service bot for your company. With GPT-4o or Claude directly, generic answers are fine, but the bot fumbles your own product terms and its tone always sounds vaguely corporate. You can keep stacking prompts, or you can fine-tune a small model on a few hundred real support conversations, run it in-house with faster responses and no per-token billing. This article is about the second path: from cleaning up a dataset to getting a model trained with LoRA.

General-purpose large models often underperform in specific domains. Fine-tuning allows developers to customize general models with their own data, significantly improving performance in specific scenarios.

First, Get the Choice Right: Fine-Tuning / Prompt Engineering / RAG

Method Principle Use Case Cost
Prompt Engineering Carefully crafted prompts Simple tasks, one-off needs Very Low
RAG External knowledge retrieval Real-time / private knowledge Low
Fine-Tuning Continue training on specific data Fixed style / domain / format High

To decide, ask three questions: is there a stable "style or format requirement" on the output? Does the knowledge change frequently? Is there enough data (at least a few hundred items)? Only when the answer is "fixed style, stable knowledge, enough data" does fine-tuning pay off. If knowledge changes often, RAG fits better; if you only need occasional rephrasing, prompt engineering suffices.

Dataset Preparation: The Step That Decides Everything

The ceiling of fine-tuning is set by your data. The most common format is the conversation/instruction format:

{
  "messages": [
    {"role": "system", "content": "You are after-sales support for XX Appliances. Answer concisely: conclusion first, then steps."},
    {"role": "user", "content": "My air conditioner remote is not responding. What should I do?"},
    {"role": "assistant", "content": "First check whether the batteries are reversed. Replace them, press the reset button, and if it still does not respond, call the 400 hotline."}
  ]
}

On quantity: more is not always better. A few hundred high-quality conversations covering the main branches often beat tens of thousands of low-quality records. Check four things carefully:

  • Accuracy: review every record by hand; wrong answers get "learned in" and amplified;
  • Consistency: keep system prompts, tone, and format uniform across the whole set;
  • Diversity: cover common cases, edge cases, and the "fallback wording" for refusing to answer;
  • Balance: don't let one category take up 80%, or the model will be skewed.

If data is scarce, don't give up immediately: rewrite existing conversations by hand (paraphrase, add steps), or clean question-answer pairs out of old support tickets. Round-trip back-translation (e.g., EN to ZH and back) is also a common augmentation trick.

Full Fine-Tuning / LoRA / QLoRA: Which Route to Take

Method Principle GPU Memory (7B) Best For
Full fine-tuning Updates all weights 4×A100 80GB Ample budget, maximum quality
LoRA Freezes weights, trains low-rank matrices Single 24GB GPU Most scenarios
QLoRA Quantization + LoRA Single 12GB GPU Individuals / experiments

LoRA's core idea is to decompose the weight update into low-rank matrices:

W' = W + BA

W is frozen and only BA is trained, usually 0.1%–1% of the full parameter count. That is why a consumer 24GB GPU can train a 7B model, and why training time shrinks from "days" to "hours".

Hands-On: Fine-Tuning Llama 3 with LoRA

Using meta-llama/Llama-3.1-8B as an example. First install the dependencies:

pip install torch transformers datasets peft accelerate bitsandbytes

Load the model and configure LoRA:

from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import get_peft_model, LoraConfig, TaskType

model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B")
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B")

lora_config = LoraConfig(
    r=16,
    lora_alpha=32,
    target_modules=["q_proj", "v_proj"],
    lora_dropout=0.1,
    task_type=TaskType.CAUSAL_LM,
)
model = get_peft_model(model, lora_config)
model.print_trainable_parameters()  # usually 0.x% of parameters are trainable

Useful starting values for hyperparameters: learning rate around 2e-4, r between 8 and 16, lora_alpha usually twice r, and 1–3 epochs. If the batch is too small, raise gradient_accumulation_steps; if you run out of memory, enable fp16 or bf16 first:

training_args = TrainingArguments(
    output_dir="./fine-tuned-model",
    num_train_epochs=3,
    per_device_train_batch_size=4,
    gradient_accumulation_steps=4,
    learning_rate=2e-4,
    fp16=True,
    save_steps=500,
    logging_steps=50,
)
trainer = Trainer(model=model, args=training_args, train_dataset=dataset)
trainer.train()

The trained LoRA weights take only a few hundred MB, so you can save them separately and merge at deployment time:

from peft import PeftModel
model = PeftModel.from_pretrained(base_model, "./lora-checkpoint")
merged_model = model.merge_and_unload()
merged_model.save_pretrained("./final-model")

A Complete Case: The Support-Bot Fine-Tuning Path

Back to the customer-service bot from the introduction. Suppose you fine-tune Qwen2.5-7B-Instruct with QLoRA on a single 12GB GPU:

  1. Clean 800 question-answer pairs out of historical tickets, balanced across "product inquiry / troubleshooting / after-sales policy / cannot answer";
  2. Train for 2 epochs with the QLoRA config above, roughly 2–3 hours;
  3. Score a held-out 50-example test set by hand, watching whether answers follow the system prompt's format and whether the model fabricates policy;
  4. Once quality is good enough, merge the weights and deploy in-house with vLLM or Ollama.

In practice, for this kind of "fixed phrasing + stable domain" use case, a fine-tuned 7B model often approaches a general flagship model while costing maybe a tenth as much per inference. That is why many companies would rather spend a few days fine-tuning than pay per call forever.

Evaluation and Iteration

Don't rush to production after training. Go through at least this checklist:

Dimension Evaluation Method
Output quality Human scoring + comparison with a baseline
Instruction following Automated test set (covering format requirements)
Domain accuracy Expert review of key answers
Safety / hallucination Red-team testing + fallback for unknown questions

For a more systematic methodology, see the AI model evaluation guide; to run it cheaper, check the local deployment with Ollama guide.

Common Pitfalls

  • Overfitting: too little data or too many epochs; the symptom is "parroting answers" — it fails as soon as the question is rephrased;
  • Catastrophic forgetting: the model loses general capabilities learned in pretraining; fewer epochs and a small mix of general data help;
  • Format inconsistency: if system prompts differ across training data, the model behaves inconsistently at inference;
  • Treating fine-tuning as a silver bullet: when knowledge changes often or data is thin, use RAG and prompt engineering first.

Reference: Hugging Face PEFT docs https://huggingface.co/docs/peft/; LoRA paper https://arxiv.org/abs/2106.09685; QLoRA paper https://arxiv.org/abs/2305.14314

Integration & Code Examples

Example 1: Write the training data as conversations first

{"messages": [
  {"role": "system", "content": "You are the 16IDC support assistant and answer only cloud server questions."},
  {"role": "user", "content": "I only have a 2 vCPU / 4 GB box. How large a model can it run?"},
  {"role": "assistant", "content": "A quantized 7B model usually fits, but concurrency is limited; validate latency with one request before moving to a GPU instance."}
]}
{"messages": [
  {"role": "system", "content": "You are the 16IDC support assistant and answer only cloud server questions."},
  {"role": "user", "content": "How long does ICP filing take?"},
  {"role": "assistant", "content": "It depends on the local authority and is counted in working days; I cannot promise a fixed duration, only the current review status."}
]}

One sample per JSONL line, with a stable system prompt: varying system text during training teaches the model to blur "role" and "task".

Example 2: The few lines that matter for LoRA training

# 4-bit quantization plus LoRA: a 7B model is trainable on a single GPU
import os
from datasets import load_dataset
from peft import LoraConfig
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from trl import SFTConfig, SFTTrainer

MODEL = os.environ.get("BASE_MODEL", "Qwen/Qwen2.5-7B-Instruct")
tok = AutoTokenizer.from_pretrained(MODEL)
model = AutoModelForCausalLM.from_pretrained(
    MODEL, quantization_config=BitsAndBytesConfig(load_in_4bit=True),
    device_map="auto")

trainer = SFTTrainer(
    model=model, tokenizer=tok,
    train_dataset=load_dataset("json", data_files="train.jsonl", split="train"),
    peft_config=LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05, task_type="CAUSAL_LM"),
    args=SFTConfig(output_dir="out/lora", num_train_epochs=2, learning_rate=1e-4,
                   per_device_train_batch_size=1, gradient_accumulation_steps=8,
                   bf16=True, logging_steps=10, save_strategy="epoch"))
trainer.train(); trainer.save_model("out/lora-final")

r and learning_rate are the first two knobs to tune; watching validation loss across one or two epochs catches overfitting far better than a single ten-epoch run.

The Four-Step Sequence in the Support-Bot Case

The support-bot scenario at the top of the article runs all the way to deployment: clean 800 Q&A pairs from historical tickets, train 2 epochs with QLoRA, score a 50-item held-out set by hand, then merge the weights and serve in-house with vLLM or Ollama. The order cannot be reshuffled — acceptance comes before merging and deployment.

The table pairs each step with its checks, all taken from the configuration and criteria in this article.

Step Action Checks
Data preparation Clean 800 Q&A pairs from historical tickets, balanced across four categories Share of product questions, troubleshooting, after-sales policy, unanswerable
Training QLoRA for 2 epochs, roughly 2-3 hours Learning rate 2e-4, r between 8 and 16, lora_alpha twice r
Acceptance Manual scoring on a 50-item held-out set Follows the system-prompt format, invents no policy
Deployment Merge weights, serve in-house with vLLM or Ollama LoRA weights are only a few hundred MB, merged before saving

The step most often skipped is the time scale in step two: 2 epochs of a 7B model on a single 12GB card takes about 2-3 hours, which makes the data-adjustment loop fast. Data work gets the effort because redoing it costs far more than another training run.

Three Questions Before Deciding to Fine-Tune

Three questions in order: does the output need a stable style or format, does the knowledge change often, and are there at least a few hundred examples. The first two pick the route; the third decides whether the route is open.

The three answers map onto three routes: fixed style, stable knowledge and enough data make fine-tuning worthwhile; frequently changing knowledge belongs to RAG; the occasional wording tweak is a prompt-engineering job.

When data is short, expand before deciding: rewrite existing conversations (paraphrase, add missing steps), extract and clean Q&A pairs from historical tickets, and back-translate between languages. If expansion still cannot reach a few hundred examples, RAG plus prompt engineering fits the current stage better than forcing a fine-tune.

Limitations

  • This article reflects public information as of 2026-07-18; terms, prices and quotas follow the provider's current published terms.
  • Third-party capability and limit statements follow their official documentation; no further commitment is implied here.
  • Licensing, filing and data-residency decisions must be verified against the regulations of your operating region.

FAQ

Fine-tuning or RAG: which is the alternative?

Use fine-tuning only when style and format are fixed, knowledge is stable, and a few hundred examples exist. Use RAG when knowledge changes often and prompting when you only tweak wording. Choosing wrong buys no quality and freezes 1 set of stale knowledge into the weights.

What GPU memory and cost does LoRA training need?

Three tiers. A 7B model needs 1 24GB card for LoRA, 1 12GB card for QLoRA, and 4 A100 80GB GPUs for full fine-tuning. LoRA trains low-rank matrices at roughly 0.1%-1% of parameters, saves weights of a few hundred MB, and cuts training from days to hours.

When should a team avoid fine-tuning?

Avoid fine-tuning when knowledge changes frequently, only a few dozen examples exist, or you just want different wording. Start with RAG and prompting. If you do fine-tune, keep epochs at 1-3 and mix in general data to limit overfitting and catastrophic forgetting. Validating on held-out questions catches both early.

Related Providers

  • Hugging Face: when datasets live in Hub format and base weights are pulled straight from the Hub.
  • Ollama: when the tuned model must run quantized on one machine behind a single command.
  • vLLM: when concurrent serving needs PagedAttention plus an OpenAI-compatible HTTP endpoint.
  • RunPod: when training runs on rented GPU hours instead of self-built hardware.

Community & Resources

  • AI: other coverage in this category.
    Official docs and communities

  • PEFT Docs — LoRA, QLoRA and adapter merging

  • TRL Docs — parameters for SFT, DPO and related trainers

  • Transformers Docs — model loading and quantization configs

  • Hugging Face Datasets — public datasets and format conventions

Learning resources and tools

Original source: 16IDC ↗