Introduction to OpenAI

Vision

Fine Tuning

OpenAI fine-tuning lets you customize base models to your domain, improve response quality, and optimize for latency. In this guide, we'll walk through preparing data, launching jobs, monitoring progress, and deploying your fine-tuned model.

The image shows a webpage from OpenAI's platform documentation, specifically focusing on fine-tuning models. It outlines the benefits and steps of fine-tuning, with a sidebar menu listing related topics.

High-level workflow:

  1. Prepare and upload training data.
  2. Create a fine-tuning job.
  3. Evaluate the fine-tuned model.
  4. Deploy your custom model in production.

1. Preparing Training Data

High-quality JSONL data is critical for reliable GPT fine-tuning.

1.1 Text-Only Tasks

Each JSONL line contains a prompt and a completion. Example:

{"prompt":"Paris, as if everyone doesn't know that already.","completion":""}
{"prompt":"Oh, just some guy named William Shakespeare. Ever heard of him?","completion":""}
{"prompt":"Around 384,400 kilometers. Give or take a few, like that really matters.","completion":""}

Note

Ensure each completion begins with a space or newline if you want the model to include that prefix.

1.2 Vision or Chat Tasks

For visual or chat-based fine-tuning, wrap exchanges in a messages array. Example—image classification:

{
  "messages": [
    {"role":"system","content":"You are an assistant that identifies uncategorized images."},
    {"role":"user","content":"What is this cheese?"},
    {
      "role":"user",
      "content":[
        {
          "type":"image_url",
          "image_url":{"url":"https://upload.wikimedia.org/wikipedia/commons/3/36/Danbo.jpg"}
        }
      ]
    },
    {"role":"assistant","content":"Danbo"}
  ]
}

2. Uploading Data & Creating a Fine-Tuning Job

First, upload your JSONL as a file resource:

from openai import OpenAI
client = OpenAI()

file = client.files.create(
    file=open("training-data.jsonl","rb"),
    purpose="fine-tune"
)

Then launch the fine-tune job:

client.fine_tuning.jobs.create(
    training_file=file.id,
    model="gpt-4o-mini-2024-07-18"
)

3. Managing Fine-Tuning Jobs

Quick reference for common operations:

ActionAPI Method
List jobsclient.fine_tuning.jobs.list(limit=10)
Retrieve job statusclient.fine_tuning.jobs.retrieve("ftjob-abc123")
Cancel a jobclient.fine_tuning.jobs.cancel("ftjob-abc123")
List job eventsclient.fine_tuning.jobs.list_events(job_id, limit=10)
Delete a fine-tuned model<sup>1</sup>client.models.delete("ft:gpt-3.5-turbo:...")

<sup>1</sup> You must be the organization owner to delete a model.


4. Fine-Tuning Examples

4.1 Style & Tone: Sarcastic “Marv” Chatbot

Upload and start a job for a witty assistant:

file = client.files.create(file=open("marv.jsonl","rb"), purpose="fine-tune")
client.fine_tuning.jobs.create(training_file=file.id, model="gpt-4o-mini-2024-07-18")

4.2 Structured JSON Outputs

For tasks requiring strict JSON responses (e.g., sports stats):

file = client.files.create(file=open("sports-context.jsonl","rb"), purpose="fine-tune")
client.fine_tuning.jobs.create(training_file=file.id, model="gpt-4o-mini-2024-07-18")

5. Function-Calling Integration

Fine-tuning can teach the model to invoke your functions. Example payload:

{
  "messages":[
    {"role":"user","content":"What is the weather in San Francisco?"},
    {"role":"assistant","function_call":{"name":"get_current_weather","arguments":"{\"location\":\"San Francisco, USA\"}"}},
    {"role":"function","name":"get_current_weather","content":"{\"temperature\":21,\"unit\":\"celsius\"}"},
    {"role":"assistant","content":"It is 21 degrees celsius in San Francisco."}
  ],
  "functions":[
    {
      "name":"get_current_weather",
      "description":"Get the current weather.",
      "parameters":{"type":"object","properties":{"location":{"type":"string"}},"required":["location"]}
    }
  ]
}

6. Advanced Options & Monitoring

Integrate with Weights & Biases, include validation sets, and track metrics:

curl -X POST https://api.openai.com/v1/fine_tuning/jobs \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
        "model":"gpt-4o-mini-2024-07-18",
        "training_file":"file-ABC123",
        "validation_file":"file-DEF456",
        "integrations":[{"type":"wandb","wandb":{"project":"my-project","tags":["lineage","version1"]}}]
      }'

7. Best Practices & Considerations

  • Start with representative, high-quality data.
  • Monitor for biases, guardrails, and compliance in regulated industries.
  • Track token usage and costs to avoid surprises.
  • Retrain periodically as requirements evolve.

Watch Video

Watch video content

Previous
Project 2 Image Captioning