Notebooks
C
Cohere
Fine Tuning For Classify

Fine Tuning For Classify

llmunotebookscohere-developer-experience

Fine-Tuning for Classify

Cohere's large language models (LLMs) have been pre-trained with a vast amount of training data, allowing them to capture how words are being used and how their meaning changes depending on the context.

However, there are many cases in which getting the best model performance requires performing an additional round of training on custom user data. Creating a custom model using this process is called fine-tuning.

Fine-tuning allows you to alter the model itself and customize it to excel at your specific task. In this notebook, you'll learn how to fine-tune a model for classification.

Overview

We'll do the following steps:

  • Step 1: Prepare the Dataset - Download the dataset, select a subset, and prepare it for the Classify endpoint.
  • Step 2: Fine-Tune the Model - Kick off a fine-tuning job, and confirm when the model has completed training.
  • Step 3: Use/Evaluate the Fine-Tuned Model - Evaluate the fine-tuned model's performance on the test dataset.

Setup

We'll start by installing the tools we'll need and then importing them.

[1]
[2]

Fill in your Cohere API key in the next cell. To do this, begin by signing up to Cohere (for free!) if you haven't yet. Then get your API key here.

[3]
[3]

Step 1: Prepare the Dataset

We'll use the Airline Travel Information System (ATIS) intent classification dataset [source]. For demonstration purposes, we’ll take just a small portion of the dataset: 1,000 data points in total.

[4]
Number of entries: 1000

The first thing we need is to create a training dataset, to be used for building the classifier, and a test dataset, to be used for testing the classifier performance. We will use 800 and 200 data points for these datasets, respectively.

[5]

Our goal is to train the classifier so it can predict the class of a new customer inquiry out of eight classes, as follows:

[6]
['atis_flight', 'atis_airfare', 'atis_ground_service', 'atis_flight_time', 'atis_airline', 'atis_quantity', 'atis_abbreviation', 'atis_aircraft']

We transform the data to JSONL format to match the style expected by the Classification endpoint (documentation).

[7]
Creating jsonl file ...
Done

The JSONL file has a row for each example. Each example has "text" and "label" fields.

[8]
{"text": " what flights go from newark to boston after 5 pm", "label": "atis_flight"}
{"text": " can you show me economy class one way fares for flights from oakland to dallas on december sixteenth", "label": "atis_airfare"}
{"text": " show me the latest flight from salt lake city to phoenix", "label": "atis_flight"}
{"text": " pittsburgh to denver", "label": "atis_flight"}
{"text": " show me all flights from san francisco to atlanta", "label": "atis_flight"}

Step 2: Fine-Tune the Model

We kick off a fine-tuning job by navigating to the fine-tuning tab of the Dashboard. Under "Classify", click on "Create a Classify model".

Next, upload the .jsonl file you just created as the training set by clicking on the "TRAINING SET" button. When ready, click on "Review data" to proceed to the next step.

Then, you'll see a preview of how the model will ingest your data. If anything is wrong with the data, the page will also provide suggested changes to fix the training file. Otherwise, if everything looks good, you can proceed to the next step.

Finally, you'll see a page where you'll provide a nickname to your model. We used atis-classify-ft as the nickname for our model. Under "BASE MODEL", ensure "english" is selected.

Then click on "Start training" to kick off the fine-tuning process. This will navigate you to a page where you can monitor the status of the model. A model that has finished fine-tuning will show the status as READY.

Step 3: Use/Evaluate the Fine-Tuned Model

Once the model has finished fine-tuning, it’s time to evaluate its performance. Navigate to the API tab of the fine-tuned model. There, you'll see the model ID that you should use when calling co.classify().

We fill in the model ID to generate test predictions.

[15]

Next, we calculate the model's test accuracy and F1 score.

[16]
Accuracy: 98.00
F1-score: 98.05