[Capabilities]

Text & Vision Fine-tuning

Fine-tuning allows you to tailor a pre-trained language model to your specific needs by training it on your dataset. This guide explains how to fine-tune text and vision models, from preparing your data to training, whether you aim to improve domain-specific understanding or adapt to a unique conversational style.

You can both finetune directly in the AI Studio or via our API.

Before You Start

Before You Start

Dataset

To fine-tune a model, you need to provide a dataset that contains the data you want to train on, it is also recommended to have a validation dataset and a test dataset.

The dataset must be in a specific format, and you can upload it to the Mistral Cloud before launching the fine-tuning job.

Data must be stored in JSON Lines (.jsonl) files, which allow storing multiple JSON objects, each on a new line.

SFT Datasets should follow an instruction-following format representing a user-assistant conversation. Each JSON data sample should either consist of only user and assistant messages or include function-calling logic.

Conversational text only data between user and assistant, which can be one-turn or multi-turn.

{
  "messages": [
    {
      "role": "user",
      "content": "User interaction n°1"
    },
    {
      "role": "assistant",
      "content": "Bot interaction n°1"
    },
    {
      "role": "user",
      "content": "User interaction n°2"
    },
    {
      "role": "assistant",
      "content": "Bot interaction n°2"
    }
  ]
}
  • Conversational data must be stored under the "messages" key as a list.
  • Each list item is a dictionary containing the "content" and "role" keys. "role" is a string: "system", "user", "assistant" or "tool".
  • Loss computation is performed only on tokens corresponding to assistant messages ("role" == "assistant").

While text-only fine-tuning covers multiple use cases, you can also fine-tune the vision capabilities of our models. This allows you to create models that can understand and generate responses based on both text and image inputs.

Note that the files must be in JSONL format, meaning every JSON object must be flattened into a single line, and each JSON object is on a new line.

Raw .jsonl file example:

{"messages": [{"role": "user","content": "..."},{"role": "assistant","content": "..."},...]}
{"messages": [{"role": "user","content": "..."},{"role": "assistant","content": "..."},...]}
{"messages": [{"role": "user","content": "..."},{"role": "assistant","content": "..."},...]}
{"messages": [{"role": "user","content": "..."},{"role": "assistant","content": "..."},...]}
...
Jobs Management

Jobs Management

Create and Manage Fine-tuning Jobs

To create your custom model, you need to create a fine-tuning job. You can fully manage jobs via our API, from creation, to starting, monitoring and cancellation.

A fine-tuning job corresponds to a single training run. You can create a fine-tuning job with the following parameters:

Create a fine-tuning job

Create a fine-tuning job

  • model: the specific model you would like to fine-tune. The choices are:
    • Text Only:
      • open-mistral-7b
      • mistral-small-latest
      • codestral-latest
      • open-mistral-nemo
      • mistral-large-latest
      • ministral-8b-latest
      • ministral-3b-latest
    • Vision:
      • pixtral-12b-latest
  • training_files: a collection of training file IDs, which can consist of a single file or multiple files
  • validation_files: a collection of validation file IDs, which can consist of a single file or multiple files
  • hyperparameters: two adjustable hyperparameters, "training_steps" and "learning_rate", that users can modify.
  • auto_start:
    • auto_start=True: Your job will be launched immediately after validation.
    • auto_start=False (default): You can manually start the training after validation by sending a POST request to /fine_tuning/jobs/<uuid>/start.
  • integrations: external integrations we support such as Weights and Biases for metrics tracking during training.
# create a fine-tuning job
created_jobs = client.fine_tuning.jobs.create(
    model="open-mistral-7b",
    training_files=[{"file_id": training_data.id, "weight": 1}],
    validation_files=[validation_data.id],
    hyperparameters={
        "training_steps": 10,
        "learning_rate":0.0001
    },
    auto_start=False,
#   integrations=[
#       {
#           "project": "finetuning",
#           "api_key": "WANDB_KEY",
#       }
#   ]
)
Job Status

Job Status

After creating a fine-tuning job, you can check the job status using:

client.fine_tuning.jobs.get(job_id = created_jobs.id)
Start a fine-tuning job

Start a fine-tuning job

Initially, the job status will be "QUEUED". After a brief period, the status will update to "VALIDATED". At this point, you can proceed to start the fine-tuning job:

# start a fine-tuning job
client.fine_tuning.jobs.start(job_id = created_jobs.id)

created_jobs
Fine-tuned Model

Fine-tuned Model

Use and Delete Fine-tuned Models

Once your fine-tuning job is done, you can use your fine-tuned custom model in your applications.

Use a fine-tuned model

Use a fine-tuned model

Below is an example of how to use a fine-tuned model to classify your data.

# You will be able to see the fine-tuned model name via `retrieved_job.fine_tuned_model`
chat_response = client.chat.complete(
    model=retrieved_job.fine_tuned_model,
    messages = [{"role":'user', "content":'What is the best French cheese?'}]
)
Delete a fine-tuned model

Delete a fine-tuned model

You can delete a fine-tuned model if you no longer need it.

client.models.delete(model_id=retrieved_job.fine_tuned_model)
FAQ

FAQ