Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Deploy, evaluate, and manage AI agents end-to-end on Microsoft Azure AI Foundry
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
finetuning/workflows/quickstart.md
1# Quickstart: Fine-Tune Your First Model236 steps from zero to a fine-tuned model using SFT with synthetic data.45> **Time**: ~20 min active + 1-3 hours training.67## Prerequisites89- Azure AI Foundry project with a deployed model (e.g., `gpt-4.1-mini`)10- Python 3.10+ with `openai` installed11- Project endpoint URL and API key (Foundry portal → Project Settings)1213## Step 1: Connect to Your Project1415```bash16export OPENAI_BASE_URL="https://<your-resource>.services.ai.azure.com/api/projects/<your-project>/openai/v1/"17export AZURE_OPENAI_API_KEY="<your-key>"18```1920```python21from openai import OpenAI22import os2324client = OpenAI(base_url=os.environ["OPENAI_BASE_URL"], api_key=os.environ["AZURE_OPENAI_API_KEY"])25resp = client.chat.completions.create(model="gpt-4.1-mini", messages=[{"role": "user", "content": "Hello"}], max_tokens=10)26print(resp.choices[0].message.content)27```2829## Step 2: Generate Training Data3031```python32import json, re3334SYSTEM_PROMPT = "You are a concise technical support agent. Answer in 1-2 sentences."3536generation_prompt = """Generate 50 diverse technical support conversations.37Each should have a customer question and an ideal agent response (1-2 sentences).38Cover: password resets, billing, product setup, account changes, shipping, troubleshooting.39Return a JSON array where each element has "question" and "answer" fields."""4041resp = client.chat.completions.create(42model="gpt-4.1-mini", messages=[{"role": "user", "content": generation_prompt}],43max_tokens=8000, temperature=1.0,44)4546content = resp.choices[0].message.content47match = re.search(r'```(?:json)?\s*\n(.*?)\n```', content, re.DOTALL)48json_str = match.group(1) if match else content.strip().strip("`").replace("json\n", "")49examples = json.loads(json_str)5051for split, name, rng in [("train", "train.jsonl", examples[:40]), ("val", "val.jsonl", examples[40:])]:52with open(name, "w") as f:53for ex in rng:54f.write(json.dumps({"messages": [55{"role": "system", "content": SYSTEM_PROMPT},56{"role": "user", "content": ex["question"]},57{"role": "assistant", "content": ex["answer"]},58]}) + "\n")59```6061Validate: `python scripts/validate/validate_sft.py train.jsonl`6263## Step 3: Baseline the Base Model6465```python66with open("val.jsonl") as f:67test_examples = [json.loads(line) for line in f][:5]6869for ex in test_examples:70resp = client.chat.completions.create(71model="gpt-4.1-mini", messages=ex["messages"][:2], max_tokens=200)72print(f"Q: {ex['messages'][1]['content']}")73print(f"Expected: {ex['messages'][2]['content']}")74print(f"Base model: {resp.choices[0].message.content}\n")75```7677## Step 4: Upload Data and Submit Job7879```python80import time8182with open("train.jsonl", "rb") as f:83train = client.files.create(file=f, purpose="fine-tune")84with open("val.jsonl", "rb") as f:85val = client.files.create(file=f, purpose="fine-tune")8687for _ in range(30):88if client.files.retrieve(train.id).status == "processed" and client.files.retrieve(val.id).status == "processed":89break90time.sleep(10)9192job = client.fine_tuning.jobs.create(93model="gpt-4.1-mini", training_file=train.id, validation_file=val.id,94suffix="my-first-ft",95method={"type": "supervised"},96hyperparameters={"n_epochs": 2, "learning_rate_multiplier": 1.0},97)98print(f"Job submitted: {job.id}")99```100101Or via script:102```bash103python scripts/submit_training.py --model gpt-4.1-mini --training-file train.jsonl --validation-file val.jsonl --type sft --suffix my-first-ft --epochs 2104```105106## Step 5: Monitor107108```bash109python scripts/monitor_training.py --job-id <your-job-id>110```111112Or check [Azure AI Foundry portal](https://ai.azure.com) → Fine-tuning → Jobs.113114## Step 6: Deploy, Test, and Compare115116```bash117python scripts/deploy_model.py --model-id <fine-tuned-model-name> --name my-ft-deployment --capacity 50118```119120```python121for ex in test_examples:122base = client.chat.completions.create(model="gpt-4.1-mini", messages=ex["messages"][:2], max_tokens=200)123ft = client.chat.completions.create(model="my-ft-deployment", messages=ex["messages"][:2], max_tokens=200)124print(f"Q: {ex['messages'][1]['content']}")125print(f"Base: {base.choices[0].message.content}")126print(f"Fine-tuned: {ft.choices[0].message.content}\n")127```128129## What's Next130131- **Scale data**: 200-500 examples → `workflows/dataset-creation.md`132- **Try RFT**: For verifiable answers → `references/training-types.md`133- **Debug**: `workflows/diagnose-poor-results.md`134- **Full guide**: `workflows/full-pipeline.md`135