Introduction

You probably have labored with constructing textual content classification fashions, likelihood is you bought conversant in the same old drill: amassing a whole bunch to hundreds of labeled examples, cleansing the uncooked textual content, changing it to appropriate numerical representations for machine studying fashions — like embeddings or TF-IDF — and coaching your mannequin, similar to a logistic regression or random forest classifier.

Nonetheless, when massive language fashions (LLMs) got here alongside, they modified the principles of the sport — and that features conventional machine studying pipelines to some extent. Now it’s potential to categorise a textual content by merely asking an LLM to take action. Here is the catch: shifting from classical machine studying workflows to immediate engineering will be difficult. Scikit-LLM is a Python library that bridges the hole between each worlds. It wraps the highly effective capabilities of pre-trained LLMs behind the acquainted scikit-learn interface — particularly match() and predict().

This text exhibits use Scikit-LLM for one of many easiest but most sensible textual content classification duties within the trendy AI panorama: few-shot classification, that’s, classifying textual content based mostly on a small variety of labeled examples that information the LLM on what to do and the way.

Zero-Shot vs. Few-Shot Classification

Earlier than entering into our Python instance, it is value clarifying the distinction between few-shot classification and one other type of textual content classification that additionally gained reputation with the rise of LLMs: zero-shot classification.

  • Zero-shot classification: The mannequin is given a textual content along with an inventory of potential labels and requested to categorize the textual content underneath essentially the most appropriate one — all with none input-output examples. This paradigm depends completely on the LLM’s inner data gained throughout pre-training.
  • Few-shot classification: A small set of labeled examples, or “photographs” (texts with related class labels), is handed to the mannequin earlier than asking it to categorise a brand new, unlabeled textual content. These domain-specific examples complement the mannequin’s inner data and higher information its reasoning.

Few-shot classification is usually higher than its zero-shot counterpart for extremely domain-specific textual content classification duties the place texts comprise particular jargon, edge instances, or nuanced sentiments. Generally, offering simply 3 to five labeled examples could suffice to “train” a pre-trained LLM the additional guidelines it must carry out classification — all with out retraining the entire mannequin.

The excellent news: Scikit-LLM makes this course of remarkably simple. Here is the way it works.

Working Few-Shot Classification with Scikit-LLM, Step by Step

This instance will be run completely in a Google Colab pocket book. It makes use of Groq‘s LLM API, which supplies entry to free fashions. As a prerequisite, register on the Groq web site and generate your personal API key. As soon as generated, rigorously copy your API key and paste it as a brand new “Secret” variable in Google Colab — discovered on the key icon on the left-hand aspect of Colab. Identify the key variable GROQ_API_KEY.

After that, set up Scikit-LLM as follows:

!pip set up scikit-llm -q

Whereas Scikit-LLM is constructed to function with OpenAI-compatible LLM endpoints, Groq’s API is amongst them. The next code retrieves your Groq API key and applies the mandatory configuration to arrange entry to the endpoint.

from google.colab import userdata
from skllm.config import SKLLMConfig

# Retrieve the API key securely from Google Colab Secrets and techniques
groq_key = userdata.get('GROQ_API_KEY')

# Level scikit-LLM to Groq's OpenAI-compatible endpoint
SKLLMConfig.set_gpt_url("
SKLLMConfig.set_openai_key(groq_key)

Subsequent, we put together the dataset. To make use of few-shot classification, we do not want a big labeled dataset as we might for coaching a brand new mannequin from scratch. As a substitute, we’d like a small, consultant set of labeled “few-shot” examples and their related labels. Let’s assume we wish to classify IT assist tickets into three classes. Following the classical machine studying conference, we identify the few-shot examples and their labels X_train and y_train respectively. We additionally outline X_test, which incorporates three unlabeled texts we wish to classify:

# Our "Few-Shot" examples to show the mannequin the nuances
X_train = [
    "My laptop screen is flickering heavily.",
    "I need a new mouse, the scroll wheel is broken.",
    "Excel keeps crashing every time I open a macro.",
    "How do I update to the latest version of Photoshop?",
    "I forgot my VPN password.",
    "Can you grant me admin rights to the shared drive?"
]

y_train = [
    "Hardware",
    "Hardware",
    "Software",
    "Software",
    "Access",
    "Access"
]

# The unseen knowledge we wish to really predict
X_test = [
    "The keyboard types double letters randomly.",
    "I am locked out of my email account.",
    "Zoom freezes when I share my screen."
]

To make use of Scikit-LLM, we import the important thing class for our goal process: FewShotGPTClassifier. This class wraps a suitable mannequin of our selection — similar to Groq’s openai/gpt-oss-20b — inside a task-specific layer that lets us use it identical to a classical machine studying mannequin skilled with scikit-learn.

from skllm.fashions.gpt.classification.few_shot import FewShotGPTClassifier

# Use a confirmed 2026 free-tier mannequin on Groq, making certain the custom_url:: prefix is current
clf = FewShotGPTClassifier(mannequin="custom_url::openai/gpt-oss-20b")

# "Practice" the mannequin (constructs the few-shot immediate reminiscence)
clf.match(X_train, y_train)

Importantly, match() doesn’t carry out precise coaching (weight updating) on the LLM. It merely builds a few-shot immediate reminiscence for the mannequin, containing the labeled examples it would depend on for reasoning and producing its responses.

All it takes now could be calling predict() to acquire predictions on our unlabeled textual content examples:

# Generate predictions
predictions = clf.predict(X_test)

# Show the outcomes
for textual content, label in zip(X_test, predictions):
    print(f"Ticket: '{textual content}'nPredicted Class: {label}n")

The classification labels the mannequin assigns to those three assist tickets are precisely what we would count on — all three are appropriate:

Ticket: 'The keyboard sorts double letters randomly.'
Predicted Class: {Hardware}

Ticket: 'I'm locked out of my e mail account.'
Predicted Class: Entry

Ticket: 'Zoom freezes after I share my display.'
Predicted Class: Software program

Wrapping Up

This text has proven how Scikit-LLM’s elegant design allows you to flip pre-trained, open LLMs into machine studying engines able to performing domain-specific duties like few-shot classification. No heavy mannequin retraining is required, and no massive paid fashions are wanted — only a handful of labeled examples is sufficient for the mannequin to categorise new texts precisely.

 
 

Iván Palomares Carrascosa is a pacesetter, author, speaker, and adviser in AI, machine studying, deep studying & LLMs. He trains and guides others in harnessing AI in the actual world.