liveBook · Manning

Fine-tuned model

As explained in Data Analysis with LLMs

A fine-tuned model is a model obtained by adapting an existing model through additional training on task-specific data.

A fine-tuned model is a variant of an existing model adapted with task-specific training data. For example, training on reviews paired with sentiment labels can produce a model specialized in classifying new reviews as positive or negative. It may outperform the original model on that task, but the improvement is not guaranteed. section 1.5.2

Specialization can change what you need to put in a prompt. A general-purpose model needs instructions explaining that a review should receive a class label. A model trained to map reviews to labels can instead receive just the review text. Give that same bare review to the original model, and it may write an analysis rather than return a label. section 9.10 section 9.7

To create and use one through OpenAI:

  • Prepare task-specific training data, such as reviews with their intended classifications, and choose a base model. section 1.5.2 section 9.9
  • Upload the training file, start a fine-tuning job, and use its job ID to check progress. Once the job succeeds, retrieve the new model ID. section 9.9
  • Send the review as a user message through the OpenAI library, specifying the fine-tuned model ID instead of a standard model name. section 9.10
import openai
client = openai.OpenAI()

response = client.chat.completions.create(
    model='[Fine-tuned model ID]',
    messages=[
        {'role':'user', 'content':prompt}
        ]
    )

The request uses the fine-tuned model ID; the prompt variable holds the text to classify. section 9.10

Check the trade-off

In one review-classification run, the fine-tuned model got 7 of 10 labels right without task instructions or examples in each prompt. That was 70% accuracy versus 60% for the earlier version, with about 200 fewer tokens used. Compare results on your own task before committing: fine-tuning takes time and money, and using the resulting model can cost more per token than using its base model. section 9.10 section 9.7

How Data Analysis with LLMs covers it

How deep it goes
Explains the training-job and inference workflow, plus cost trade-offs, without describing model internals.
Examples
Uses labeled reviews, a Python OpenAI request, and a ten-review comparison against ground-truth labels.
What it assumes
Introduces specialization in plain terms; assumes prepared training data for the job and Python with the OpenAI library for the examples.
What you can do after
Operate/configure it by retrieving a completed job’s model ID and using that ID to classify reviews with the OpenAI library.
How it connects
Connects fine-tuning to sentiment classification, prompt length, model accuracy, and token costs.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.