Fine-tuning LLMs with Custom Data Using LoRA and QLoRA
Complete guide to fine-tuning language models with efficient parameter adaptation techniques.
Fine-tune a large language model for a specific domain, minimizing computational costs with PEFT (Parameter-Efficient Fine-Tuning) techniques.
At a glance
Access
Free prompt
Open to copy without upgrading.
Prompt objective
Fine-tune a large language model for a specific domain, minimizing computational costs with PEFT (Parameter-Efficient Fine-Tuning) techniques.
Real use case
Minas Gerais legaltech JurisAI wants to fine-tune an LLM to answer questions about Brazilian labor legislation. They have 15,000 lawyer-reviewed question-answer pairs and a budget of R$ 5,000 (~$1,000 USD) for compute. They need a model that outperforms GPT-4 in this specific domain.
Customize these fields first
Replace the placeholders with your own context before you run the prompt. That usually improves the first output more than adding more instructions later.
Prompt
Create a complete fine-tuning pipeline for [BASE MODEL NAME: Llama 3/Mistral/Gemma] focused on [DOMAIN] using [LoRA/QLoRA].\\\\\\\\n\\\\\\\\n**Context:**\\\\\\\\n- Base model: [NAME AND SIZE: e.g. Llama 3.1 8B]\\\\\\\\n- Dataset: [NUMBER] examples of [TYPE: QA/instruction/chat/classification]\\\\\\\\n- Available hardware: [GPU: A100/L4/T4/RTX 4090] with [VRAM] GB\\\\\\\\n- Compute budget: R$ [AMOUNT]\\\\\\\\n- Goal: [DESCRIBE WHAT THE MODEL SHOULD DO BETTER]\\\\\\\\n\\\\\\\\n**1) Data Preparation:**\\\\\\\\n- Dataset format (Alpaca, ShareGPT, chat template)\\\\\\\\n- Cleaning: duplicate removal, normalization, quality validation\\\\\\\\n- Split: train (80%) / validation (10%) / test (10%)\\\\\\\\n- Tokenization and length distribution analysis\\\\\\\\n- Augmentation strategies (if small dataset < 5,000)\\\\\\\\n- Consistent prompt template with base model\\\\\\\\n- Example of 3 correctly formatted records\\\\\\\\n\\\\\\\\n**2) LoRA/QLoRA Configuration:**\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\`\\\\\\\\\\\\\\\\\\\\\\\`\\\\\\\\\\\\\\\\\\\\\\\`python\\\\\\\\n# Recommended configuration\\\\\\\\n\\\\\\\\\\\\\\\\\\\\\\\`\\\\\\\\\\\\\\\\\\\\\\\`\\\\\\\\\\\\\\\\\\\\\\\`\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\`r\\\\\\\\\\\\\\\\\\\\\\\` (rank): [4/8/16/32] — quality vs. memory trade-off\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\`lora_alpha\\\\\\\\\\\\\\\\\\\\\\\`: [16/32] — scaling factor\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\`target_modules\\\\\\\\\\\\\\\\\\\\\\\`: which layers to adapt (q_proj, v_proj, k_proj, o_proj, gate_proj, up_proj, down_proj)\\\\\\\\n- \\\\\\\\\\\\\\\\\\\\\\\`lora_dropout\\\\\\\\\\\\\\\\\\\\\\\`: [0.05/0.1]\\\\\\\\n- Quantization: 4-bit (QLoRA) with nf4 + double quantization\\\\\\\\n- Estimated trainable vs. total parameters\\\\\\\\n\\\\\\\\n**3) Training Loop:**\\\\\\\\n- Framework: Hugging Face TRL (SFTTrainer)\\\\\\\\n- Hyperparameters:\\\\\\\\n - Learning rate: [1e-4/2e-4/5e-5] with cosine scheduler\\\\\\\\n - Effective batch size: [VALUE] (gradient accumulation)\\\\\\\\n - Epochs: [1-5] (monitor for overfitting)\\\\\\\\n - Max sequence length: [512/1024/2048/4096]\\\\\\\\n - Warmup steps: [VALUE]\\\\\\\\n- Mixed precision: bf16 (if A100) or fp16 (if T4/RTX)\\\\\\\\n- Gradient checkpointing for memory savings\\\\\\\\n- WandB/MLflow for experiment tracking\\\\\\\\n\\\\\\\\n**4) Evaluation:**\\\\\\\\n- Automatic metrics: perplexity, BLEU, ROUGE, BERTScore\\\\\\\\n- Human evaluation: quality rubric (1-5) with 100 test set examples\\\\\\\\n- Comparison: base model vs. fine-tuned vs. GPT-4 (baseline)\\\\\\\\n- Hallucination and factuality assessment in domain\\\\\\\\n- Portuguese benchmark (if available for domain)\\\\\\\\n\\\\\\\\n**5) Merge and Deploy:**\\\\\\\\n- Merge LoRA weights into base model\\\\\\\\n- Quantization for inference: GGUF (llama.cpp) or GPTQ\\\\\\\\n- Deploy: vLLM/TGI for high-performance API\\\\\\\\n- Estimated inference cost per 1K tokens\\\\\\\\n\\\\\\\\n**6) Cost Estimate:**\\\\\\\\n- Training cost (GPU-hours × price)\\\\\\\\n- Inference cost (per 1K tokens)\\\\\\\\n- Comparison with using API (OpenAI/Anthropic) for same volume\\\\\\\\n\\\\\\\\nProvide the complete Python script for the pipeline, from data prep to deploy.
Open directly in an AI — the text is pre-filled:
How to use this prompt
- 1Replace the key placeholders first: BASE MODEL NAME: Llama 3/Mistral/Gemma, DOMAIN, LoRA/QLoRA, NAME AND SIZE: e.g. Llama 3.1 8B.
- 2Replace any bracketed placeholders like [this] with your own context.
- 3Add extra background information when you want more tailored results.
- 4Combine multiple prompts in one conversation when you need a richer output.
- 5Save your best-performing prompts so they are easy to reuse later.
Next best step
Open the guide first, then branch only if you still need more.
A guide for technical builders choosing between prompts, coding workflows, and agent-based implementation.
If this prompt is close but not quite right, generate variants next. If the job is recurring, move into the course library after the guide.
Related prompts
View allRAG Pipeline (Retrieval-Augmented Generation) with Embeddings and Vector Database
Complete RAG implementation covering chunking, embeddings, semantic search, and augmented generation.
Best for
Build a document-based Q&A system that reduces hallucinations and delivers answers grounded in your actual data.
Advanced Prompt Engineering with Chain-of-Thought and Function Calling
Advanced prompt engineering techniques to maximize LLM performance in production environments.
Best for
Master prompt engineering techniques that improve the quality, consistency, and reliability of LLM responses in production applications.
Complete MLOps Pipeline with Model Training, Versioning, and Deployment
MLOps infrastructure to manage the full lifecycle of ML models in production.
Best for
Implement an MLOps pipeline that automates model training, evaluation, versioning, and deployment with continuous monitoring.
Data Preprocessing Pipeline for Brazilian Portuguese NLP Model Training
A robust pipeline for collecting, cleaning, and preparing Brazilian Portuguese text data for NLP model training.
Best for
Build a reproducible data pipeline that transforms raw data into high-quality datasets ready for Brazilian Portuguese NLP model training.
Explore other prompt categories
Move sideways into adjacent libraries when the current category is not the full answer.
Free browsing stays open. Premium prompts unlock the reusable workflow layer.
Use the guides and role paths to validate the job first. Upgrade when you want the full prompt text, editable premium prompts, and the surrounding course paths in one place.
Free access
- Browse guides, role paths, and category pages.
- Preview prompts before you decide to upgrade.
- Find the right starting point without friction.
Membership access
- Unlock premium prompts and the full copy text.
- See more workflow paths and course connections.
- Keep the reusable templates in one place.