Reach out

Command Palette

Search for a command to run...

[Capabilities]

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

In various domains and enterprises, classification models play a crucial role in enhancing efficiency, improving user experience, and ensuring compliance. These models serve diverse purposes, including but not limited to:

  • Moderation: Classification models are essential for moderating services and classifying unwanted content. For instance, our moderation service helps in identifying and filtering inappropriate or harmful content in real-time, ensuring a safe and respectful environment for users.
  • Intent Detection: These models help in understanding user intent and behavior. By analyzing user interactions, they can predict the user's next actions or needs, enabling personalized recommendations and improved customer support.
  • Sentiment Analysis: Emotion and sentiment detection models analyze text data to determine the emotional tone behind words. This is particularly useful in social media monitoring, customer feedback analysis, and market research, where understanding public sentiment can drive strategic decisions.
  • Data Clustering: Classification models can group similar data points together, aiding in data organization and pattern recognition. This is beneficial in market segmentation, where businesses can identify distinct customer groups for targeted marketing campaigns.
  • Fraud Detection: In the financial sector, classification models help in identifying fraudulent transactions by analyzing patterns and anomalies in transaction data. This ensures the security and integrity of financial systems.
  • Spam Filtering: Email services use classification models to filter out spam emails, ensuring that users receive only relevant and safe communications.
  • Recommendation Systems: Classification models power recommendation engines by categorizing user preferences and suggesting relevant products, movies, or content based on past behavior and preferences.

By leveraging classification models, organizations can make data-driven decisions, improve operational efficiency, and deliver better products and services to their customers.

For this reason, we designed a friendly and easy way to make your own classifiers. Leveraging our small but highly efficient models and training methods, the Classifier Factory is both available directly in la plateforme and our API.

Dataset Format

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

We provide two endpoints:

  • v1/classifications: To classify raw text.
  • v1/chat/classifications: To classify chats and multi-turn interactions.

There are 2 main kinds of classification models:

  • Single Target
  • Multi-Target

1. Single Target

For single label classification, data must have the label name and the value for that corresponding label. Example:

1{
2  "text": "I love this product!",
3  "labels": {
4    "sentiment": "positive" // positive/neutral/negative
5  }
6}

For multiple labels, you can provide a list.

1{
2  "text": "I love this product!",
3  "labels": {
4    "sentiment": ["positive", "neutral"]
5  }
6}
1{
2  "messages": [{ "role": "user", "content": "I love this product!" }],
3  "labels": {
4    "sentiment": "positive" // positive/neutral/negative
5  }
6}

For multiple labels, you can provide a list.

1{
2  "messages": [{ "role": "user", "content": "I love this product!" }],
3  "labels": {
4    "sentiment": ["positive", "neutral"]
5  }
6}

When using the result model, you will be able to retrieve the scores for the corresponding label and value.

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.

1{"text": "I love this product!", "labels": {"sentiment": "positive"}}
2{"text": "The game was amazing.", "labels": {"sentiment": "positive"}}
3{"text": "The new policy is controversial.", "labels": {"sentiment": "neutral"}}
4{"text": "I don't like the new design.", "labels": {"sentiment": "negative"}}
5{"text": "The team won the championship.", "labels": {"sentiment": "positive"}}
6{"text": "The economy is in a bad shape.", "labels": {"sentiment": "negative"}}
7...
  • Label data must be a dictionary with the label name as the key and the label value as the value.

2. Multi-Target

You can also have multiple targets and not only a single one. This is useful if you want to classify different aspects of the same content independently. Example:

1{
2  "text": "I love this product!",
3  "labels": {
4    "sentiment": "positive", // positive/neutral/negative
5    "is-english": "yes" // yes/no, boolean
6  }
7}
1{
2  "messages": [{ "role": "user", "content": "I love this product!" }],
3  "labels": {
4    "sentiment": "positive", // positive/neutral/negative
5    "is-english": "yes" // yes/no, boolean
6  }
7}
  • Each target is independent of each other, meaning the scores for each label will also be independent.

Upload a file

Once you have the data file with the right format, you can upload the data file to the Mistral Client, making them available for use in fine-tuning jobs.

1from mistralai import Mistral
2import os
3
4api_key = os.environ["MISTRAL_API_KEY"]
5
6client = Mistral(api_key=api_key)
7
8training_data = client.files.upload(
9    file={
10        "file_name": "training_file.jsonl",
11        "content": open("training_file.jsonl", "rb"),
12    }
13)
14
15validation_data = client.files.upload(
16    file={
17        "file_name": "validation_file.jsonl",
18        "content": open("validation_file.jsonl", "rb"),
19    }
20)
1import { Mistral } from '@mistralai/mistralai';
2import fs from 'fs';
3
4const apiKey = process.env.MISTRAL_API_KEY;
5
6const client = new Mistral({ apiKey: apiKey });
7
8const training_file = fs.readFileSync('training_file.jsonl');
9const training_data = await client.files.upload({
10  file: {
11    fileName: 'training_file.jsonl',
12    content: training_file,
13  },
14});
15
16const validation_file = fs.readFileSync('validation_file.jsonl');
17const validation_data = await client.files.upload({
18  file: {
19    fileName: 'validation_file.jsonl',
20    content: validation_file,
21  },
22});
1curl https://api.mistral.ai/v1/files \
2  -H "Authorization: Bearer $MISTRAL_API_KEY" \
3  -F purpose="fine-tune" \
4  -F file="@training_file.jsonl"
5
6curl https://api.mistral.ai/v1/files \
7  -H "Authorization: Bearer $MISTRAL_API_KEY" \
8  -F purpose="fine-tune" \
9  -F file="@validation_file.jsonl"

Create a fine-tuning job

The next step is to create a fine-tuning job.

  • model: the specific model you would like to fine-tune. The choice is ministral-3b-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.
1# create a fine-tuning job
2created_jobs = client.fine_tuning.jobs.create(
3    model="ministral-3b-latest",
4    job_type="classifier",
5    training_files=[{"file_id": training_data.id, "weight": 1}],
6    validation_files=[validation_data.id],
7    hyperparameters={
8        "training_steps": 10,
9        "learning_rate":0.0001
10    },
11    auto_start=False,
12#   integrations=[
13#       {
14#           "project": "finetuning",
15#           "api_key": "WANDB_KEY",
16#       }
17#   ]
18)

After creating a fine-tuning job, you can check the job status using client.fine_tuning.jobs.get(job_id = created_jobs.id).

1const createdJob = await client.fineTuning.jobs.create({
2  model: 'ministral-3b-latest',
3  jobType: 'classifier',
4  trainingFiles: [{ fileId: training_data.id, weight: 1 }],
5  validationFiles: [validation_data.id],
6  hyperparameters: {
7    trainingSteps: 10,
8    learningRate: 0.0001,
9  },
10  autoStart: false,
11  //  integrations:[
12  //      {
13  //          project: "finetuning",
14  //          apiKey: "WANDB_KEY",
15  //      }
16  //  ],
17});

After creating a fine-tuning job, you can check the job status using client.fineTuning.jobs.get({ jobId: createdJob.id }).

1curl https://api.mistral.ai/v1/fine_tuning/jobs \
2--header "Authorization: Bearer $MISTRAL_API_KEY" \
3--header 'Content-Type: application/json' \
4--header 'Accept: application/json' \
5--data '{
6  "model": "ministral-3b-latest",
7  "job_type": "classifier",
8  "training_files": [
9    "<uuid>"
10  ],
11  "validation_files": [
12    "<uuid>"
13  ],
14  "hyperparameters": {
15    "training_steps": 10,
16    "learning_rate": 0.0001
17  },
18  "auto_start": false
19}'

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

1curl https://api.mistral.ai/v1/fine_tuning/jobs/<jobid> \
2--header "Authorization: Bearer $MISTRAL_API_KEY"

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:

1# start a fine-tuning job
2client.fine_tuning.jobs.start(job_id = created_jobs.id)
3
4created_jobs
1await client.fineTuning.jobs.start({ jobId: createdJob.id });
1curl -X POST https://api.mistral.ai/v1/fine_tuning/jobs/<jobid>/start \
2--header "Authorization: Bearer $MISTRAL_API_KEY"

List/retrieve/cancel jobs

You can also list jobs, retrieve a job, or cancel a job.

You can filter and view a list of jobs using various parameters such as page, page_size, model, created_after, created_by_me, status, wandb_project, wandb_name, and suffix. Check out our API specs for details.

1# List jobs
2jobs = client.fine_tuning.jobs.list()
3print(jobs)
4
5# Retrieve a jobs
6retrieved_jobs = client.fine_tuning.jobs.get(job_id = created_jobs.id)
7print(retrieved_jobs)
8
9# Cancel a jobs
10canceled_jobs = client.fine_tuning.jobs.cancel(job_id = created_jobs.id)
11print(canceled_jobs)
1// List jobs
2const jobs = await client.fineTuning.jobs.list();
3
4// Retrieve a job
5const retrievedJob = await client.fineTuning.jobs.get({ jobId: createdJob.id });
6
7// Cancel a job
8const canceledJob = await client.fineTuning.jobs.cancel({
9  jobId: createdJob.id,
10});
1# List jobs
2curl https://api.mistral.ai/v1/fine_tuning/jobs \
3--header "Authorization: Bearer $MISTRAL_API_KEY"
4
5# Retrieve a job
6curl https://api.mistral.ai/v1/fine_tuning/jobs/<jobid> \
7--header "Authorization: Bearer $MISTRAL_API_KEY"
8
9# Cancel a job
10curl -X POST https://api.mistral.ai/v1/fine_tuning/jobs/<jobid>/cancel \
11--header "Authorization: Bearer $MISTRAL_API_KEY"

Use a fine-tuned model

When a fine-tuned job is finished, you will be able to see the fine-tuned model name via retrieved_jobs.fine_tuned_model.

1classifier_response = client.classifiers.classify(
2    model=retrieved_job.fine_tuned_model,
3    inputs=["It's nice", "It's terrible", "Why not"],
4)

Use classify_chat to classify chats and multiturn interactions.

1const classifierResponse = await client.classifiers.classify({
2  model: retrievedJob.fine_tuned_model,
3  inputs: ["It's nice", "It's terrible", 'Why not'],
4});

Use classifyChat to classify chats and multiturn interactions.

1curl "https://api.mistral.ai/v1/classifications" \
2     --header 'Content-Type: application/json' \
3     --header 'Accept: application/json' \
4     --header "Authorization: Bearer $MISTRAL_API_KEY" \
5     --data '{
6    "model": "ft:classifier:ministral-3b-latest:XXX:20250401:XXX",
7    "input": ["It's nice", "It's terrible", "Why not"]
8  }'

Delete a fine-tuned model

1client.models.delete(model_id=retrieved_job.fine_tuned_model)
1await client.models.delete({ modelId: retrieved_job.fine_tuned_model });
1curl --location --request DELETE 'https://api.mistral.ai/v1/models/ft:classifier:ministral-3b-latest:XXX:20250401:XXX' \
2     --header 'Accept: application/json' \
3     --header "Authorization: Bearer $MISTRAL_API_KEY"

Cookbooks

Explore our guides and cookbooks leveraging the Classifier Factory:

  • Intent Classification: Creating a single-target, single-label, intent classification model to predict user actions and improve customer interactions.
  • Moderation Classifier: Build a single-target, multi-label, simple moderation model to label public comments.
  • Product Classification: Create a multi-target, single-label and multi-label, food classification model to categorize dishes and their country of origin and compare to classic LLM solutions, enhancing recipe recommendations and dietary planning.

FAQ

Q: Which models can we fine-tune to create our own classifiers? A: Currently, the classifier factory supports ministral-3b.

Q: Where can I find the pricing? A: You can find it on our pricing page in the "fine-tunable models" section of our API Pricing.