Redirect Custom Model Fine-tuning — MAF Fundamentals
← Back to Tutorials

11. Custom Model Fine-tuning

Why Fine-tune?

Base LLMs are general-purpose. Fine-tuning adapts a model to your domain — your terminology, tone, data formats, and business logic. This leads to higher accuracy, fewer hallucinations, and more consistent outputs for domain-specific tasks.

When to Fine-tune vs. Prompt Engineer

ApproachWhen to UseEffort
Prompt EngineeringSimple tasks, few examples, rapid iterationLow
RAG (Retrieval Augmented)Dynamic knowledge, large corporaMedium
Fine-tuningConsistent style/schema, specialized domain, cost reductionHigh (dataset prep)
Pre-trainingNew language or knowledge from scratchVery high

Fine-tuning Methods

Dataset Preparation

Fine-tuning data typically follows a chat format. For instruction tuning, use this structure:

[
  {
    "messages": [
      { "role": "system", "content": "You are a medical coding assistant." },
      { "role": "user", "content": "Diagnosis: Type 2 diabetes, initial encounter" },
      { "role": "assistant", "content": "E11.9" }
    ]
  },
  {
    "messages": [
      { "role": "system", "content": "You are a medical coding assistant." },
      { "role": "user", "content": "Diagnosis: Essential hypertension" },
      { "role": "assistant", "content": "I10" }
    ]
  }
]

Data Quality Checklist

Fine-tuning with Azure AI Foundry

Azure AI Foundry provides a managed fine-tuning service for models like GPT-4o, Phi-3, and Llama families:

# Azure CLI — start a fine-tuning job
az ml job create --file fine-tuning-job.yml

# fine-tuning-job.yml
$schema: https://azuremlschemas.azureedge.net/latest/commandJob.schema.json
type: command
code: .
command: >-
  python finetune.py
  --model gpt-4o
  --training-file azureml://datasets/train.jsonl
  --validation-file azureml://datasets/val.jsonl
environment: azureml:openai-finetune-env:latest
compute: azureml:gpu-cluster
resources:
  instance_count: 1
  instance_type: Standard_ND40rs_v2

Evaluating a Fine-tuned Model

After fine-tuning, evaluate using:

Deploying a Fine-tuned Model

Once validated, deploy the fine-tuned model as a serverless endpoint or managed compute:

from openai import AzureOpenAI

client = AzureOpenAI(
    azure_endpoint="https://your-endpoint.openai.azure.com/",
    api_version="2024-08-01-preview",
)

response = client.chat.completions.create(
    model="gpt-4o-finetuned",  # your fine-tuned deployment name
    messages=[{"role": "user", "content": "Classify this support ticket: ..."}]
)

Using Fine-tuned Models with MAF Agents

In MAF, you can configure an agent to use any model by specifying the deployment in the model config:

const agent = new Agent({
  name: "MedicalCoder",
  instructions: "Classify diagnoses into ICD-10 codes.",
  model: {
    provider: "azure-openai",
    deployment: "gpt-4o-finetuned",
    endpoint: process.env.AZURE_OPENAI_ENDPOINT
  }
});
💡 Key Insight: Fine-tuning doesn't replace RAG — they complement each other. Use fine-tuning for behavior/style and RAG for factual knowledge.
✏️ Exercise: Create a small dataset of 20 examples for a "tone converter" that rewrites technical jargon into plain English. Use the chat format shown above and note how you would split train/validation/test sets.