Mid-level interview questionAsked at Anthropic · interview write-up
Turn an LLM into a binary classifier using its log-probs and log-sum-exp.
Model answer
Don't parse generated text; read the next-token distribution after the prompt.
import torch
def p_yes(logits, yes_ids, no_ids):
# logits: (V,) next-token logits
lp = logits.log_softmax(-1)
ly = torch.logsumexp(lp[yes_ids], -1)
ln = torch.logsumexp(lp[no_ids], -1)
return torch.sigmoid(ly - ln)
Each label has several token spellings, so combine each group with log-sum-exp, then renormalise over the two groups: a sigmoid of the difference. Threshold it after calibrating on labelled data, since raw probabilities are often skewed, and check token ids, because a leading space changes them.
Practise the rest
10 Structured outputs questions, 492 in all, each with a model answer. Free.
More from Anthropic interview questions.
More Structured outputs interview questions
- What is in-context learning, and what does each example in the prompt cost you?
- Your API's JSON mode is on. In what ways can an output still break the code that reads it?
- Your few-shot sentiment prompt lists six positive examples, then two negative ones at the very end. What's the risk, and what would you do?
- You add a repair step that keeps the first JSON object and closes open braces. Nearly every output now parses, but half are still invalid. Why?
- Each attempt gives valid output 90% of the time. A colleague proposes up to three attempts to reach 99.9%. What do you say?
- Where in the stack can you push a model towards a format, and what would you use for a classifier that must never invent a label?