Skip to main content
Article
openaifine-tuninggpt-3.5-turbomodel-customizationpythonapinlpllm

Fine-Tune GPT-3.5 Turbo with the OpenAI API

Customize GPT-3.5 Turbo on your own data using the OpenAI Fine-tuning API. This improves model performance for specific tasks like maintaining a consistent style or following complex instructions. This guide walks you through the process with Python.

intermediate30 min5 steps
The play
  1. Prepare Your Training Data
    Create a JSONL file where each line is a JSON object representing a single training example. Each object must contain a `messages` key with a list of `role` and `content` pairs, simulating a conversation that teaches the model your desired behavior.
  2. Upload Data to OpenAI
    Use the OpenAI Python client to upload your prepared `.jsonl` file. You must specify the `purpose` as 'fine-tune' to make it available for training jobs. This returns a File ID.
  3. Start the Fine-Tuning Job
    Create a new fine-tuning job using the File ID from the upload step. Specify the base model you want to customize, such as `gpt-3.5-turbo`, and the uploaded training file ID.
  4. Monitor Job Status
    OpenAI Fine-tuning jobs are asynchronous. Periodically retrieve the job's status using its ID. The job is complete when the status is `succeeded`. The response object will contain your new custom model ID in the `fine_tuned_model` field.
  5. Use Your Custom Model
    Once training is complete, use your new model ID in chat completion requests just like any other OpenAI model. It will now exhibit the specialized behavior learned from your training data.
Starter code
import os
import time
import json
from openai import OpenAI

# Make sure your OPENAI_API_KEY is set as an environment variable
client = OpenAI()

# 1. Prepare training data
# We'll create a dummy file for this example.
# In a real-world scenario, you would have a pre-existing JSONL file.
training_data = [
    {"messages": [{"role": "system", "content": "You are a helpful assistant that provides short, concise answers."}, {"role": "user", "content": "What is the capital of Japan?"}, {"role": "assistant", "content": "Tokyo."}]},
    {"messages": [{"role": "system", "content": "You are a helpful assistant that provides short, concise answers."}, {"role": "user", "content": "What is 2+2?"}, {"role": "assistant", "content": "4."}]}
]

file_path = "training_data.jsonl"
with open(file_path, "w") as f:
    for entry in training_data:
        f.write(json.dumps(entry) + "\n")

print(f"Training data created at {file_path}")

# 2. Upload the file
try:
    training_file = client.files.create(
        file=open(file_path, "rb"),
        purpose="fine-tune"
    )
    print(f"File uploaded with ID: {training_file.id}")

    # 3. Create a fine-tuning job
    job = client.fine_tuning.jobs.create(
        training_file=training_file.id,
        model="gpt-3.5-turbo",
        hyperparameters={
            "n_epochs": 3
        }
    )
    print(f"Fine-tuning job created with ID: {job.id}")

    # 4. Monitor the job
    print("Waiting for fine-tuning to complete... (This may take 10-30 minutes)")
    while True:
        job_status = client.fine_tuning.jobs.retrieve(job.id)
        print(f"Job status: {job_status.status}")
        if job_status.status == "succeeded":
            fine_tuned_model_id = job_status.fine_tuned_model
            print(f"Fine-tuning succeeded! Model ID: {fine_tuned_model_id}")
            break
        elif job_status.status in ["failed", "cancelled"]:
            print(f"Fine-tuning failed with status: {job_status.status}")
            break
        time.sleep(60) # Wait for a minute before checking again

    # 5. Use the fine-tuned model
    if 'fine_tuned_model_id' in locals():
        print("\n--- Using the fine-tuned model ---")
        completion = client.chat.completions.create(
            model=fine_tuned_model_id,
            messages=[
                {"role": "system", "content": "You are a helpful assistant that provides short, concise answers."},
                {"role": "user", "content": "What is the largest planet in our solar system?"}
            ]
        )
        print(f"Response: {completion.choices[0].message.content}")

finally:
    # Clean up the created file
    if os.path.exists(file_path):
        os.remove(file_path)
        print(f"\nCleaned up {file_path}")
Fine-Tune GPT-3.5 Turbo with the OpenAI API — Action Pack