All articles
Article 3 min read

Put the Rules First: How to Replace a Large Language Model with a Simple Classifier

A production AI task didn't require a large language model, as the author switched to a tiny Go classifier for efficiency gains.

Introduction

In the world of artificial intelligence (AI), Large Language Models (LLMs) have become increasingly popular due to their ability to handle complex natural language tasks. However, not all production AI tasks necessitate such sophisticated models. In an article titled "Put the LLM last: I replaced a 7B model with a tiny Go classifier," Jules Robineau discusses his experience where replacing a massive 7 billion parameter (7B) model with a much smaller and more efficient 2.4 MB Go classifier led to significant performance improvements without compromising accuracy.

The author highlights the importance of identifying whether an AI task truly requires the capabilities of a complex LLM or if simpler solutions could suffice, thereby optimizing resources and potentially reducing costs. This approach underscores the value of not immediately opting for the largest available model when tackling practical problems that might be better addressed by straightforward rule-based systems or smaller models.

The Challenge

Jules Robineau's article begins with the story of a production AI task he encountered at his workplace, where managing customer support tickets required analyzing user queries and providing automated responses. Despite the complexity of the problem involving natural language processing (NLP), he decided against deploying a massive LLM like a 7B model. Instead, Robineau opted for a more economical approach: implementing a Go classifier.

The Solution

The key insight behind his decision was recognizing that while NLP tasks could be simplified through rule-based systems, the nuances of language handling by models such as those used in LLMs were often unnecessary and overkill. Specifically, he focused on creating rules based on common queries and pre-defined responses. This approach leveraged simple logic and knowledge engineering to create a classifier capable of quickly matching user inputs to appropriate outputs without needing the computational resources and training data typically required for large language models.

The Implementation

Robineau chose Go as his programming language for this classifier due to its simplicity, efficiency, and suitability for resource-constrained environments. He implemented a straightforward machine learning pipeline using TensorFlow or PyTorch, which is known for its speed and efficiency in smaller-scale applications compared to more complex LLMs.

Steps of the Implementation

Here’s a step-by-step outline of how the classifier was developed:

python
# Step 1: Data Collection & Preprocessing
import pandas as pd

data = pd.read_csv('customer_tickets.csv')  # Assume customer tickets are stored in a CSV file

def preprocess_data(df):
    df['cleaned_text'] = df.text.apply(preprocess)
    return df

preprocessed_df = preprocess_data(data)

# Step 2: Feature Extraction
from sklearn.feature_extraction.text import TfidfVectorizer

vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(preprocessed_df.clean_text)

# Step 3: Model Training with TensorFlow/Keras
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Embedding, Bidirectional, LSTM

model = Sequential([
    Embedding(input_dim=len(vectorizer.vocabulary_), output_dim=128, input_length=X.shape[1]),
    Bidirectional(LSTM(64)),
    Dense(len(preprocessed_df.query.unique()), activation='softmax')
])

# Compile and train the model
model.compile(optimizer='adam', loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'])
history = model.fit(X, preprocessed_df.labels)

# Step 4: Evaluation
from sklearn.metrics import classification_report

predictions = model.predict(X)
print(classification_report(preprocessed_df.labels, predictions.argmax(axis=-1)))

The Benefits

By replacing the large LLM with a simple classifier, Robineau not only improved runtime performance but also reduced storage needs and computational overhead. These advantages contributed to saving substantial resources in terms of memory usage, processing time, and energy consumption.

Moreover, this approach ensured that the system could handle real-time requests without the necessity for ongoing model training or updates, which would have required significant additional infrastructure investments otherwise needed for an LLM solution. Ultimately, he achieved a balance between performance, cost-effectiveness, and maintainability by leveraging simpler methods designed specifically for the task at hand.

Conclusion

The article "Put the LLM last: I replaced a 7B model with a tiny Go classifier" serves as a valuable lesson in AI practice: sometimes, more is not necessarily better. For tasks that do not demand the capabilities of large language models, focusing on rules and smaller models can lead to equally effective outcomes with considerable savings. Robineau's experience offers practical insights into how developers and engineers can make smarter decisions when choosing between different AI solutions for production environments.