LLM Classification Is Feature Engineering

Hacker News by 16 min read 29x views
LLM Classification Is Feature Engineering

Share Post

LLMs-as-classifiers, prompts applied to a discourse and returning a label, suck to activity with. This is particularly achy because they often execute beautiful decently.

But let’s see immoderate of the things we’d want successful a classifier and spot really an LLM-as-classifier stacks up:

Calibration / Threshold Control LLM verdicts are often difficult labels; you tin get token log probabilities but location is nary system for believing these to beryllium well-calibrated. You tin inquire the LLM for its assurance and there’s nary logic to fishy that to beryllium well-calibrated either. As a related problem it’s past alternatively difficult to waste and acquisition disconnected precision and callback pinch these labels successful a opinionated way. Incorporating each disposable information LLMs activity awesome pinch unstructured information but we often person bully system information arsenic well. We tin paste this into the punctual nevertheless the LLM doesn’t really request to usage it. Even for prose parts of the punctual we don’t cognize if the LLM really utilized it aliases not1. Frustrating to opportunity the slightest and astir apt losing immoderate signal.

The LLM has immoderate anterior accusation baked successful which mightiness beryllium a mediocre fresh for our distribution. For lawsuit the LLM won’t cognize whether we’re testing connected a organization wherever our affirmative people is uncommon aliases an enriched organization wherever our affirmative people is comparatively prevalent. And I conjecture you tin springiness it that discourse but now you’ve sewage to modify that for each caller organization and also, arsenic successful our first point, it’s not clear that this will beryllium appropriately incorporated into the LLM’s judgement.

Interpretability In immoderate respects a LLM punctual is highly interpretable, aft each it’s written successful prose; unluckily it’s not clear that we cognize precisely what’s going connected wrong the LLM and what parts of the punctual are being followed correctly (or astatine all).

These failures are not the responsibility of the LLM: it’s not designed arsenic a classifier and so has nary system for plausibly doing immoderate of these things. But only because we’re reasoning of things incorrectly…

LLM Classification is characteristic engineering

With the due model that harnesses the LLM’s powerfulness we tin get the powerfulness of the LLM pinch the convenience of banal ML algorithms. For a sensation of what’s imaginable see wrapping the LLM verdict pinch a elemental logistic regression:

\[ p(y = 1 \mid x) = \sigma(\alpha + \beta \cdot LLM(x)) \]

Note that successful the typical lawsuit of \(\beta \rightarrow \infty\) this fundamentally recovers our LLM classifier!! But that’s a dumb parameter action policy. We should alternatively do our accustomed attack of estimating our parameters utilizing immoderate training data. This will past illness into 2 cases and we conscionable get the empirical estimates.

\[ p(y = k \mid LLM(x) = 1) = \frac{\sum_{i} I(y_{i} = k \text{ and } LLM(x_{i}) = 1)}{\sum_{i} I(LLM(x_{i}) = 1)} \]

Now let’s revisit our desiderata:

Calibration / Threshold Control As conscionable mentioned, conscionable utilizing the LLM prediction arsenic a characteristic we get 2 predictions astatine the empirical proportions (and frankincense calibrated successful expectation). As we adhd much features (see next) we will evidently get much unsocial points and, assuming our exemplary is decently flexible, these should beryllium astir well-calibrated (and we person ways to amended that). And since we person existent probabilities retired we tin now take our operating period to waste and acquisition disconnected precision and callback arsenic needed. Incorporating each disposable information This is conscionable a logistic regression truthful we tin evidently adhd successful different covariates. To accommodate to the different baselines the logistic regression is fresh to the training dataset and frankincense adapts to that baseline and covariate structure. We tin moreover reweight the examples to effort to target different distributions of interest. Interpretability We still person the problem of interpreting the LLM verdict itself but now we person a amended consciousness of really that verdict is contributing to our last determination (especially if we person different covariates included).

We’ve fundamentally recovered each of the bully properties we wanted from our model! Can we spell moreover further?

LLM Classification is really bully characteristic engineering

Suppose we are not pleased pinch the capacity of our classifier: what should we do? In the LLM-as-classifier lawsuit our only action is to effort messing pinch the prompt. This is an arcane undertaking astir which proposal abounds connected the net but contented is scarce. Best of luck to you.

From a ML constituent of position the measurement you make your exemplary amended is:

Collecting much data Admittedly this is uncool: the allure of LLMs-as-classifiers is that you person a training-free methodology. So it’s disappointing that you request information for training purposes successful this characteristic engineering paradigm. I’d reason though that this is little inconvenient than 1 mightiness think. Like we’re going to request a trial group successful bid to trial exemplary capacity anyways (you were going to quantify your capacity right?) truthful what’s a small much for training? Making your features better Making your features amended is analyzable erstwhile editting prose. We tin astatine slightest surface our features: if we judge that a characteristic should ever lead to a affirmative we tin empirically cheque this and debug appropriately. We tin besides measure the features themselves: we dainty them arsenic a secondary target (and recurse connected making that classifier better). Creating much features You tin usage the residuals successful your exemplary to effort and fig retired really to amended your prompts not by meddling pinch wording but alternatively by considering a broader group of features. We mightiness instantly see taking the log probability of our verdict token; alternatively we tin person aggregate runs of our classifier (if we person reasoning earlier the verdicts those logprobs tin spell to 0 aliases 1). We tin besides get much features from the LLM itself by asking for subverdicts aliases different features. Improving your exemplary architecture Finally exemplary architecture is purely plug-and-play. Don’t for illustration logistic regression, see xgboost aliases a neural network. Heck person a rules based strategy implemented by the LLM for each I care. All are adjacent game.

Test Case: Irony Detection

Let’s make this much actual utilizing an example. We’ll usage the SemEval 2018 Task 3 dataset2, a postulation of 4618 tweets (3834 train / 784 test) branded for irony by master annotators. Irony is simply a earthy fresh for this station it’s an NLP task wherever an LLM intelligibly has existent awesome and we use from the worldly knowledge implicitly embedded successful the LLM.

Our punctual asks the exemplary to make a binary irony judgment, and we tally it complete each the tweets astatine erstwhile arsenic a batch job:

from typing import Literal from pydantic import BaseModel, Field MODEL = "gemini-3.1-flash-lite" PROMPT_TEMPLATE = """\ Irony is erstwhile personification says 1 point but intends another, often for \ humorous aliases captious effect. It tin beryllium subtle: a tweet mightiness publication arsenic \ sincere astatine first glimpse but transportation an ironic reside done connection choice, \ context, aliases contrast. Consider the pursuing tweet and explanation it arsenic "Ironic" aliases "Not". {tweet}""" class Verdict(BaseModel): reasoning: str = Field( description="Brief reasoning: what connection aliases discourse suggests irony aliases sincerity." ) verdict: Literal["Ironic", "Not"] = Field( description='Whether the tweet is ironic ("Ironic") aliases not ("Not").' ) def build_request(row_id: int, tweet: str) -> dict: return { "contents": [ {"role": "user", "parts": [{"text": PROMPT_TEMPLATE.format(tweet=tweet)}]} ], "metadata": {"id": str(row_id)}, "config": { "response_mime_type": "application/json", "response_schema": Verdict, "temperature": 0, }, }

Performance

We get the pursuing capacity conscionable from this prompt

TPR FNR Brier Score F1 (@0.5)
0.965 0.035 0.259 0.747

It’s really rather singular really good this does arsenic one-shot. You wouldn’t expect this to beryllium imaginable without learning which is the cool point astir LLMs. Of people it’s still beautiful meh: the Brier people is rather bad arsenic we don’t person calibration (indeed conscionable random guessing gets america a Brier people of 0.25).

Desiderata

Calibration

We tin do amended pinch our logistic regression which achieves calibration (though statement it doesn’t impact the ordering truthful F1 is the same).

LLM verdict Fitted P(Ironic)
Ironic 0.687
Not 0.188
TPR FNR Brier Score F1 (@0.5)
0.965 0.035 0.175 0.747

Adding Features

Let’s see immoderate further LLM features. Firstly let’s return a speedy look astatine our misclassifications (they’re the aforesaid from either model)

tweet target verdict reasoning
“I can’t breathe!” was chosen arsenic the astir notable quote of t… Ironic Not The tweet presents a actual connection astir a quote being selected for a list, …
4:30 an opening my first brew now gonna beryllium a agelong night/day Not Ironic The tweet describes a business of drinking early successful the time arsenic a ’long night/da…
crushes are awesome until you recognize they’ll ne'er beryllium interes… Ironic Not The tweet expresses a common, relatable sentiment astir unrequited love. The use…
I conjecture my feline besides mislaid 3 pounds erstwhile she went to the vet a… Not Ironic The personification is utilizing hashtags related to fittingness and weight nonaccomplishment to picture a cat’…
@yWTorres9 clip to deed the books then Ironic Not The tweet is simply a straightforward, literal proposal to study, lacking immoderate linguis…
“Twig” is now “Sprig”—3 sec limit connected caller societal video plat… Ironic Not The tweet uses a neutral, descriptive reside to study connected a tech manufacture inclination wi…
Luv this Ironic Not The tweet is ambiguous; without further discourse aliases ocular cues, it is typical…
really, what other tin a food beryllium too a fish? @RBRNetwork1… Not Ironic The tweet uses a rhetorical mobility to constituent retired the obviousness of a statement…
@malesurvivor72 I deliberation it’s a safe stake it won’t fresh the cri… Not Ironic The building ‘won’t fresh the crime’ is simply a play connected the communal idiom ’the reward f…
loyalty vs. aforesaid protection loyalty vs. aforesaid protection loya… Not Ironic The repetitive, mantra-like building suggests a cynical aliases weary study ab…

In ray of this let’s modify our punctual arsenic follows

from typing import Literal from pydantic import BaseModel, Field MODEL = "gemini-3.1-flash-lite" PROMPT_TEMPLATE = """\ Analyse the pursuing tweet on respective dimensions. Tweet: {tweet} First, explanation it arsenic "Ironic" aliases "Not" (irony is erstwhile the writer says 1 \ thing but intends different — not simply disapproval aliases complaint). Then reply \ each question: 1. Is the tweet trying to beryllium funny aliases humorous (regardless of whether it's ironic)? 2. Does the tweet picture a realistic, plausible business aliases event? 3. Would you request to cognize the reply thread, existent news, aliases different outer \ context to understand the author's intent? 4. Does the tweet definitive a genuine title aliases frustration? 5. Does the tweet picture a antagonistic aliases frustrating business utilizing \ positive aliases upbeat connection (i.e. is location a mismatch betwixt the business \ and really it is described)? 6. Is the tweet self-deprecating — does the writer make nosy of aliases \ belittle themselves? 7. Does the tweet incorporate an definitive opposition aliases juxtaposition of 2 \ things (e.g. "X but Y", "while X, Y", "sure, X")? 8. Is the tweet a rhetorical mobility — a mobility not expecting a \ literal answer? 9. Does the tweet springiness what appears to beryllium a compliment aliases praise but \ is really captious aliases dismissive (a backhanded compliment)? 10. Is the tweet directed arsenic disapproval astatine a circumstantial named person, \ organisation, aliases nationalist figure? 11. Ignoring reside and connection prime entirely: is the underlying business \ described objectively antagonistic aliases unfortunate (e.g. illness, failure, \ injustice, bad luck)? 12. Is the tweet making a direct, sincere captious aliases governmental constituent — \ i.e. the disapproval is meant literally, not ironically? (A tweet tin beryllium \ critical and non-ironic.) 13. Does the writer definitive approval, enthusiasm, aliases ceremony of \ something that is intelligibly bad aliases undesirable (e.g. "love erstwhile X" wherever \ X is evidently awful)? 14. Does the writer feign astonishment aliases daze astatine thing that is really \ predictable, obvious, aliases expected (e.g. "shocked, conscionable shocked", \ "who could person seen this coming")? 15. Does the tweet usage mock enthusiasm — over-the-top affirmative connection \ (exclamations, "amazing!", "so great!") applied to thing bad aliases \ frustrating? 16. Does the tweet incorporate a pun, wordplay, aliases a deliberate double \ meaning unrelated to irony (the humour comes from the connection itself, \ not from saying the other of what is meant)? 17. Is the tweet chiefly astir politics, politicians, government, \ elections, aliases governmental ideology? 18. Is the tweet chiefly astir a celebrity, athlete, sports team, \ or intermezo figure? 19. Is the tweet astir the author's individual regular life, routine, aliases \ mundane business (commute, weather, food, work, sleep)?""" class Features(BaseModel): reasoning: str = Field( description="Brief reasoning covering the irony verdict and each nineteen dimensions." ) verdict: Literal["Ironic", "Not"] = Field( description='Whether the tweet is ironic ("Ironic") aliases not ("Not").' ) is_humorous: bool = Field( description="True if the tweet is trying to beryllium funny aliases humorous." ) realistic_situation: bool = Field( description="True if the tweet describes a realistic, plausible situation." ) requires_context: bool = Field( description="True if knowing the author's intent requires outer context." ) is_complaint: bool = Field( description="True if the tweet expresses a genuine title aliases frustration." ) sentiment_mismatch: bool = Field( description="True if the tweet describes a negative/frustrating business utilizing affirmative language." ) is_self_deprecating: bool = Field( description="True if the writer makes nosy of aliases belittles themselves." ) has_contrast: bool = Field( description="True if the tweet contains an definitive opposition aliases juxtaposition of 2 things." ) is_rhetorical_question: bool = Field( description="True if the tweet is simply a rhetorical mobility not expecting a literal answer." ) is_backhanded_compliment: bool = Field( description="True if the tweet gives evident praise that is really captious aliases dismissive." ) targets_person_or_org: bool = Field( description="True if the tweet is directed arsenic disapproval astatine a circumstantial named person, organisation, aliases nationalist figure." ) situation_is_negative: bool = Field( description="True if the underlying business described is objectively antagonistic aliases unfortunate, sloppy of tone." ) is_literal_criticism: bool = Field( description="True if the tweet makes a direct, sincere captious aliases governmental constituent (criticism is meant literally, not ironically)." ) author_endorses_bad_outcome: bool = Field( description="True if the writer expresses support aliases ceremony of thing intelligibly bad aliases undesirable." ) feigned_surprise: bool = Field( description="True if the writer feigns daze aliases astonishment astatine thing predictable aliases obvious." ) mock_enthusiasm: bool = Field( description="True if the tweet uses over-the-top affirmative connection applied to thing bad aliases frustrating." ) is_pun_or_wordplay: bool = Field( description="True if the tweet contains a pun aliases wordplay wherever humour comes from the connection itself alternatively than saying the other of what is meant." ) is_political: bool = Field( description="True if the tweet is chiefly astir politics, politicians, government, elections, aliases governmental ideology." ) is_celebrity_or_sports: bool = Field( description="True if the tweet is chiefly astir a celebrity, athlete, sports team, aliases intermezo figure." ) is_mundane_daily_life: bool = Field( description="True if the tweet is astir the author's individual regular life, routine, aliases mundane situation." ) def build_request(row_id: int, tweet: str) -> dict: return { "contents": [ {"role": "user", "parts": [{"text": PROMPT_TEMPLATE.format(tweet=tweet)}]} ], "metadata": {"id": str(row_id)}, "config": { "response_mime_type": "application/json", "response_schema": Features, "temperature": 0, }, }

We’ll besides commencement adding successful immoderate deterministic features that we tin compute:

Feature What it measures
is_reply Tweet starts pinch “@” (a reply)
has_url Tweet contains a URL
tweet_length Character magnitude of the tweet
n_hashtags Number of “#” characters
n_exclamations Number of “!” characters
n_caps_words Number of all-caps words (length > 1)
has_ellipsis Tweet contains “…”
starts_with_quote Tweet starts pinch a quotation mark
n_emoji Number of emoji characters

So do we spot improvements? We comparison 3 nested models: verdict only, verdict + each LLM features, verdict + each features (LLM + rule-based).

Features Brier F1 (test)
Verdict only 0.175 0.747
+ LLM features 0.131 0.768
+ rule-based features 0.127 0.779

We spot a clear use from each level of further features including the deterministic features which dishonesty extracurricular of the LLM.

Interpretability

The coefficient fig shows which features the exemplary really relies on, controlling for each others:

 Logistic regression coefficients (± 1 SE), sorted by |coefficient|.

Figure 1: Logistic regression coefficients (± 1 SE), sorted by |coefficient|.

Comparison pinch published results

How does our attack comparison to the published lit connected this dataset?

Table 1: F1 scores connected SemEval 2018 Task 3A trial set.

System F1
Random baseline 0.373
SVM + tf-idf (paper NRC baseline) 0.589
THU\_NGN (competition winner)3 0.705
NTUA-SLP (post-competition LSTM + attention)4 0.786
LLM difficult explanation (this post) 0.747 (0.712–0.778)
LLM + LR 0.779 (0.746–0.81)

We spot that our first LLM classifier thumps the title victor handily (0.747 vs 0.705). With the characteristic engineering position we person overlapping CIs pinch the post-competition authorities of the art, utilizing thing but a logistic regression connected apical of LLM-extracted features.

Conclusion

Getting LLMs into style to reliably service arsenic classifiers is difficult activity but perchance highly impactful. There’s much and much investigation that relies connected LLMs for classification: for illustration the How People Use ChatGPT which uses LLMs to categorize conversations pinch LLMs5 aliases the “slop-vestigation” of the Huggingface incident. We’re going to request to get precocious value results retired of these tools.

Fortunately, there’s a increasing assemblage of papers which are making this point.

Personally I americium willing successful investigating agentic classifiers. Instead of a fixed characteristic group aliases people connection you empower the LLM to analyse itself. The LLM tin usage features of the investigative process arsenic features erstwhile classifying: fundamentally grading itself connected the rigor and comprehensiveness of the investigation. And pinch a reliable trial group we tin make statistically valid inferences connected the results!

Other Article Hacker News
Close Right Ads
Close Left Ads