← Back to Blog

Fine-Tuning Open-Source LLMs on Private Enterprise Codebases

How I built a reliable WhatsApp AI shopping assistant for Clickmothercare that survives hallucinated products, silent save failures, and multi-agent handoff bugs.

Anas Rhimi
Anas Rhimi August 2026 • 8 min read

The Enterprise Data Dilemma: Privacy vs. Performance

Off-the-shelf LLMs excel at generic programming tasks but struggle with proprietary frameworks, internal design patterns, and domain-specific microservices. While RAG (Retrieval-Augmented Generation) helps, it cannot fundamentally alter the model's syntax intuition. Fine-tuning an open-source model (like Llama-3 or Mistral) on your private codebase bridges this gap, but standard fine-tuning requires massive VRAM. The solution is Parameter-Efficient Fine-Tuning (PEFT) using QLoRA (Quantized Low-Rank Adaptation).

Implementing QLoRA for Code-Specific Adaptation

Below is a highly optimized Python training script utilizing HuggingFace's transformers, trl (Transformer Reinforcement Learning), and peft libraries. This configuration quantizes the base model to 4-bit precision to fit on a single enterprise GPU (like an NVIDIA A10G or L4) while training the adapter weights to master your internal API schemas.

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
from trl import SFTTrainer

# Line 1: Configure 4-bit quantization to drastically reduce memory footprint
bnb_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_use_double_quant=True,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_compute_dtype=torch.bfloat16
)

# Line 2: Load the base model with the quantization configuration
model_id = "meta-llama/Meta-Llama-3-8B-Instruct"
model = AutoModelForCausalLM.from_pretrained(model_id, quantization_config=bnb_config, device_map="auto")
model = prepare_model_for_kbit_training(model)

# Line 3: Define the LoRA configuration targeting attention modules
peft_config = LoraConfig(
    r=16, 
    lora_alpha=32, 
    target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
    lora_dropout=0.05,
    bias="none",
    task_type="CAUSAL_LM"
)
model = get_peft_model(model, peft_config)

# Line 4: Initialize the Supervised Fine-Tuning (SFT) Trainer
trainer = SFTTrainer(
    model=model,
    train_dataset=internal_codebase_dataset, # Pre-formatted Dataset object
    dataset_text_field="text",
    max_seq_length=2048,
    args=TrainingArguments(
        per_device_train_batch_size=4,
        gradient_accumulation_steps=4,
        warmup_steps=100,
        learning_rate=2e-4,
        fp16=True,
        output_dir="outputs/enterprise-coder-v1"
    ),
    peft_config=peft_config,
)

# Line 5: Execute training and save the adapter weights
trainer.train()
trainer.model.save_pretrained("enterprise-coder-v1-adapter")

Deep Dive into the Architecture

  • Line 1: bnb_4bit_quant_type="nf4" (Normal Float 4) is theoretically optimal for normally distributed weights in LLMs. We pair this with bfloat16 compute dtype, ensuring that while weights are stored in 4-bit, the actual matrix multiplications during the forward/backward pass occur in 16-bit to preserve gradient precision.
  • Line 2: prepare_model_for_kbit_training is a critical utility. It casts layer norms and the final LM head to fp32 to prevent numerical instability (loss spikes) during training, a common issue when fine-tuning quantized models.
  • Line 3: The LoRA configuration. We target q_proj, k_proj, v_proj, and o_proj. Focusing solely on attention matrices (rather than MLP layers) provides the best balance between training speed and the model's ability to learn new coding syntax without catastrophic forgetting.
  • Line 4 & 5: We use gradient accumulation (batch_size=4 * acc_steps=4 = effective batch size of 16). This simulates a larger batch size on limited VRAM hardware. Finally, we save only the adapter weights (~100MB), not the entire base model, ensuring a lightweight and secure deployment artifact.

By applying QLoRA, engineering teams can inject deep proprietary knowledge into open-source models, keeping all sensitive code behind the corporate firewall while achieving performance that rivals proprietary APIs.

Is your AI agent's infrastructure secure and reliable?

Book a Free 15-Min Technical Audit
Hire Me