Text Classification with LLMs: A Prompt Engineering Guide

Updated on Oct 09,2025

Table of Contents

Large Language Models (LLMs) have revolutionized Natural Language Processing (NLP), offering powerful tools for various text-related tasks. Among these, text classification stands out as a fundamental technique. This guide delves into using LLMs for text classification through prompt engineering, providing practical insights and code examples to get you started. Learn how to leverage free, open-source resources to create effective text classifiers without extensive custom training or fine-tuning.

Key Points

Understand the basics of text classification with LLMs.

Learn prompt engineering techniques to guide LLM behavior.

Utilize the Hugging Face Transformers library for free LLM access.

Classify movie reviews using a pre-trained LLM.

Evaluate the accuracy of your text classification model.

Explore prompt optimization strategies for better results.

Know when fine-tuning becomes necessary.

Getting Started with Text Classification Using LLMs

Understanding Text Classification

Text classification, at its core, is the task of assigning predefined categories to pieces of text. This has myriad applications, from sentiment analysis (identifying positive, negative, or neutral tones in text) to spam detection (classifying emails or messages as spam or not spam).

Traditional approaches to text classification involved feature engineering and training specialized models. However, LLMs offer a simpler, more versatile approach. Instead of relying on explicit feature engineering and complex machine learning pipelines, we can leverage the pre-trained knowledge of LLMs to perform classification tasks with minimal effort.

Large language models, pre-trained on vast amounts of text data, possess an intrinsic understanding of language nuances, semantics, and context. Therefore, they are naturally well-suited for text classification. Using prompt engineering, it can yield impressive outcomes without requiring extensive training datasets or lengthy fine-tuning procedures. This is especially useful when dealing with niche classifications with limited labeled data, a common scenario in many organizations.

Introduction to Prompt Engineering

Prompt engineering is the art of crafting effective instructions or prompts that guide an LLM to produce the desired output. Instead of relying on traditional training methods, we formulate prompts that elicit the model's pre-existing knowledge and steer it toward the target task. A carefully designed Prompt can significantly impact the accuracy and quality of the classification. It means carefully constructing input text to elicit the specific output behavior from the LLM.

For text classification, this involves creating prompts that provide context, instructions, and, potentially, examples to the LLM.

Here's why prompt engineering is critical:

  • Cost-Effectiveness: It eliminates the need for costly custom training, potentially saving businesses huge costs.
  • Time Efficiency: Prompt engineering is faster to implement, allowing businesses to adapt models quickly to changing business needs.
  • Accessibility: It lowers the barrier to entry by removing the need for specialized machine learning skills.

The main objective of prompt engineering is to minimize costs, save time, and reduce complexity while reaching the highest accuracy feasible for your business requirements.

Setting Up Your Environment

To follow this guide, you'll need a few tools:

  • Python Environment: Ensure you have Python 3.6 or later installed.
  • Hugging Face Transformers Library: This library provides easy access to a wide range of pre-trained LLMs. Install it using pip: pip install transformers
  • Pandas: For data manipulation and analysis: pip install pandas
  • Torch: For numerical computing, especially when GPU is available: pip install torch
  • TQDM: For neat progress bars while running classification: pip install tqdm

It is highly recommended to use a cloud-based environment like Google Colab. This will not only simplify dependency management but also provide access to free GPUs, which significantly accelerate LLM inference. If using Google Colab, remember to change the runtime type to include a GPU (Runtime > Change runtime type > Hardware accelerator > GPU). The best part of this is that you are getting a free, open-source, without any APIs solution for Large Language Models. If you can use this correctly, you will save lots of money and time by avoiding custom training or fine-tuning.

Loading Your Dataset: Movie Reviews

For this guide, we'll use the IMDb movie review dataset. This dataset contains movie reviews labeled with either 'positive' or 'negative' sentiment. You can load it directly using the Hugging Face datasets library.

import pandas as pd
from datasets import load_dataset

ds = load_dataset("imdb", split="test[:100]")
df = pd.DataFrame({
    "text": ds["text"],
    "label": ["Positive" if l == 1 else "Negative" for l in ds["label"]]
})

print(df.head())

This code snippet loads a small subset (100 reviews) of the IMDb dataset and converts it into a Pandas DataFrame, making it easier to work with. Feel free to use data sets that you want in an excel file.

Crafting Effective Prompts for Text Classification

The Build Prompt Function

Prompt design is the key to effective text classification with LLMs.

Here's a Python function to construct prompts tailored to your classification task:

def build_prompt(text):
    instruction = (
        f"You are a helpful assistant that classifies movie reviews.
"
        f"Choose one of the following categories: {', '.join(class_labels)}.
"
        f"Only respond with a single word - the correct category. Learn from these examples:
"
    )
    few_shot_examples = [
        {"text": "The film had stunning visuals and a beautiful story.", "label": "Positive"},
        {"text": "I didn't like the pacing. It felt too slow and boring.", "label": "Negative"},
    ]
    prompt = instruction + "

"
    for ex in few_shot_examples:
        prompt += f"Review: {ex['text']}
Sentiment: {ex['label']}

"

    prompt += f"Review: {text}
Sentiment:"
    return prompt

In this function:

  • We define an instruction that explains the task to the LLM.
  • Few-shot examples guide the LLM with example classifications.
  • The function dynamically constructs a prompt containing the instructions, examples, and the input text you want to classify.
  • Note: Causal LM, a particular model class trained for generating tasks, is used here for better output. Causal basically means, that the model only looks at the past words and predicts future words.
  • Zero-shot Prompting: For a zero-shot approach, we don't provide any examples. We rely entirely on the LLM's pre-existing knowledge and the instructions within the prompt.
  • Few-shot Prompting: In few-shot prompting, we add examples demonstrating the desired classification behavior. This helps the LLM understand the task better and often improves accuracy.
    
    def zero_shot_build_prompt(text):
    return (f"Classify the sentiment of the following movie review as Positive or Negative:

{text} Sentiment:")

How To Apply Prompt Engineering For Classifying Movies

Load and Apply A Model

Load Your Model using the sample code below. Here we are specifying a model name.

model_name = "microsoft/phi-2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
    device_map="auto",
)

Remember to select the right kind of model based on different kinds of tasks. Next run the follow code:

pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)

def classify(review):
    prompt = build_prompt(review)
    result = pipe(
        prompt,
        max_new_tokens=5,
        do_sample=False,
        return_full_text=False
    )[0]['generated_text']
    return result.strip().split()[0]

df['prediction'] = df['text'].apply(classify)
print(df.head())

The function build_prompt can build a proper prompt with few shot examples. Make sure to select to a function that does it in a single word only.

Pricing

Model Costs

The best part of using hugging face, is that it has free access to use the different models that you might want to use. There is an option for premium access as well. For that, just purchase additional compute units. You also get a free GPU to do model inference tasks. Therefore, at a minimum, the software can be utilized at no cost.

Benefits and Drawbacks of LLM-Based Text Classification

👍 Pros

Reduced development time and effort.

Lower computational costs.

Increased accessibility.

High performance.

Scalability.

👎 Cons

Initial prompt engineering requires careful design and optimization.

Increased complexity for fine-tuning models, depending on your use case.

Core Features

Hugging Face

The code that you can use from Hugging face is completely free of charge and includes the following.

  • Open Source models
  • No API Requirements
  • Prompt Engineering.

Use Cases

Utilizing this Code

You can use this code for multiple instances including, classifying your customers thoughts on your company, categorizing reviews and many more. With all of the examples that you can use and test and all the libraries you can use, using this tool will help save you a lot of time.

Frequently Asked Questions

What is prompt engineering?
Prompt engineering involves designing effective prompts to guide LLMs towards desired outputs. This removes the need for extensive training.
How does the Hugging Face Transformers library help with text classification?
The Hugging Face Transformers library provides easy access to a wide range of pre-trained LLMs, including models suitable for generative text-based tasks and decoder models, facilitating text classification with minimal code.
What is AutoModelforCausalLM Class?
With AutoModelforCausalLM Class, the focus here is on models trained for generative tasks. It gives a clear indication to the classification task. Causal basically means that the model only looks at past words and predicts future words.
What is zero shot, one shot, few shot examples?
Few-shot: Model is given a few examples on how to perform prompt. One shot: It’s just one example to help the model, Zero shot: There are no examples given, the model has to perform the task based on prior data and model construction. In Zero Shot the model does not know as to what has to be done. In Few Short, you provide the sample.

Further Exploration and Related Questions

How can the accuracy of a text classification model using LLMs be improved?
There are several strategies to improve the accuracy of text classification models: Prompt Engineering Techniques: Prompt engineering is a simple but powerful step in achieving your goal. You should: Refine Your Prompts: Carefully consider the wording of your instructions and examples. Experiment with different phrasings and formats to see what works best. Add More Examples: Increase the number of examples in your few-shot prompts. This provides the LLM with more context and can improve accuracy. Targeted Examples: Select examples that specifically address common errors or challenging cases. Evaluating With Model Accuracy: Test the software on a large sample to ensure consistent results, testing 10 rows or 100 rows won’t guarantee long-term stability and/or accuracy of any model. The higher the accuracy, means less errors for the future. Model Fine-Tuning Custom Training: If prompt engineering doesn't yield satisfactory results, consider fine-tuning the LLM on your specific dataset. Note that this requires a substantial amount of labeled data. Selecting the Right Model Experiment with Different LLMs: Not all LLMs are created equal. Try different models (e.g., different sizes or architectures) to find one that performs well on your task. Right Model Classes: AutoModelforCausalLM Class, with the focus on models trained for generative tasks, can clear indications to the classification task. Handling Complex Classifications Hierarchical Classification: For multi-class problems, consider structuring your categories hierarchically. Testing and Monitoring Regular Evaluation: Continuously evaluate your model's performance on new data to detect drift or degradation over time. Human-in-the-Loop: Implement a system where humans review and correct classifications, especially for high-stakes applications. This not only improves accuracy but also provides valuable feedback for refining your prompts or fine-tuning your model.

Most people like