Text: embeddings and the LLM classifier
Two honest ways to predict from free text. Turn it into features with a local embedder, or let a language model label it, scored on the same folds as any model.
What runs in CodeSplash
- Linear / logistic regression Verified in Python and R
You will be able to
- Add text to a tabular model through embeddings fitted inside folds
- Explain how the LLM classifier keeps its score honest
- Decide what to send to a language model, and when not to
Text is a data type
ReadFree text doesn't fit into a column of numbers. You have two broad options:
- Features: turn each text into numbers with an embeddingText embeddingA list of numbers from a language model that places similar texts close together, usable as model features., then train any learner on them.
- A labeler: ask a language model for the class directly, with some examples.
Both start from the same first move: treat them like any other model, and score them on rows they haven't seen.
Embeddings, inside the folds
ReadIn Setup, Text to embed (optional) turns free text into numeric features through a local Ollama embedding model. The text stays on this machine.
Embeddings have hundreds of dimensions, so they're compressed with PCA to Embedding components. That PCA is a learned step, so it's fitted on each fold's training rows only.
A worked run: a linear model on two numeric features and 3 components has an RMSE of 0.588 on 120 rows. Any learner can take these columns.
Keeping an LLM honest
PredictPredict first
A language model gets a few labeled examples per class, then labels a fold's rows. For a cross-validated score to be honest, where may a fold's examples come from?
Reveal
Only from that fold's training rows. Show a row as an example while it's being predicted, and you've handed over the answer. So each fold chooses its own examples, and the locked test rows draw theirs from the training rows. Scores use the same metrics as every other run, on the same folds.
Temperature
ExploreA language model samples its next token from a distribution. Lower Temperature and the top choice takes over. Raise it and answers vary.
DExplorable · Temperature and top-p
What do the sampling settings on a language model actually change?
With these settings, how likely is the single most likely token?
Below 1 sharpens the distribution; above 1 flattens it.
Keep the likeliest tokens until they hold this much probability.
“The p-value tells you how extreme”
- With these settings
- Default (temperature 1, no cut-off)
Twelve sampled tokens: extreme · surprising · surprising · unusual · rare · rare · unusual · surprising · rare · extreme · surprising · likely
Commit your prediction above to see the result
Make your prediction to see the result.
A classifier wants stable answers, and a low temperature helps. Even then, a model update can change its answers, so record which model you used. Answers are cached per text and prompt, and a re-run doesn't send what's already been answered.
What gets sent?
ReadThe texts and the class names are row values. So before a request goes out:
- The project's privacy policy decides whether this feature may run at all.
- Personal details in texts are masked, and class names that hold personal details are refused.
- A cloud model never sees more distinct texts than the project's row limit.
What will be sent shows the actual request. Read it first.
Should you?
ReflectYou have 200 labeled tickets and 50,000 to label. Compare the two routes: a local embedder with logistic regression, and an LLM classifier. Think about cost per run, privacy, how each changes when the wording changes, and which you could explain to an auditor.
Check your understanding
CheckpointCheck yourself
Why fit the embedding's PCA on each fold's training rows?
Show the answer
Answer: a
It's the same rule as for any learned preprocessing. The app applies it to embedding components too.
Check yourself
Which practice would inflate an LLM classifier's score?
Show the answer
Answer: b
The model would see the answer for those rows. A smaller number of examples just costs some accuracy, honestly.
Check yourself
Where's the best place to compare an LLM classifier with a logistic model on embeddings?
Show the answer
Answer: a
The LLM run joins the leaderboard with an llm badge, and it can be compared fold by fold with any other run.
Try it
In the appFor embeddings, set Text to embed (optional) in Setup. It needs a local Ollama model, which you install with ollama pull. For the labeler, set a class target and press LLM classifier… in the Learners step. Pick Text the model reads, Examples per class and, if you want, Instructions (optional).
import numpy as np
from sklearn.decomposition import PCA
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline
# Stand-in for embeddings: 64 numeric columns from any local embedder.
rng = np.random.default_rng(1)
latent = rng.normal(size=(200, 3))
emb = latent @ rng.normal(size=(3, 64)) + rng.normal(0, 0.1, size=(200, 64))
y = latent[:, 0] - 0.5 * latent[:, 1] + rng.normal(0, 0.3, 200)
model = make_pipeline(PCA(n_components=3), LinearRegression()) # PCA refits inside each fold
print(-cross_val_score(model, emb, y, cv=5, scoring="neg_root_mean_squared_error").mean())set.seed(1)
latent <- matrix(rnorm(600), 200)
emb <- latent %*% matrix(rnorm(192), 3) + matrix(rnorm(200 * 64, 0, 0.1), 200)
y <- latent[, 1] - 0.5 * latent[, 2] + rnorm(200, 0, 0.3)
fold <- sample(rep(1:5, length.out = 200))
rmse <- sapply(1:5, function(k) {
pca <- prcomp(emb[fold != k, ], rank. = 3) # fitted on training rows only
tr <- data.frame(y = y[fold != k], pca$x)
te <- data.frame(predict(pca, emb[fold == k, ]))
sqrt(mean((y[fold == k] - predict(lm(y ~ ., tr), te))^2))
})
mean(rmse)Recap
Recap- Embeddings turn text into features, and their compression stays inside each fold.
- An LLM classifier is a model too: a fold's examples come from its training rows, and it's scored like the others.
- Read what will be sent, and record the model you used.
Concepts in this lesson