Sub topic 12
Using an LLM as a Labeling Source
Assume you know Python, supervised text classification, precision, recall, F1, basic probability, and regular expressions. This chapter treats a large language model (LLM) as one noisy labeling source among several. It does not treat the model's answer as ground truth.
Why use an LLM for labeling?
An LLM can classify text according to a natural-language rubric. This is useful when rules are difficult to express with regular expressions. For example, a prompt can ask whether a support message reports a billing problem, even when customers use many different phrases.
LLM labeling trades cost and consistency for speed:
- It can label many examples faster than manual work.
- It can follow richer instructions than a simple keyword rule.
- Its answers can vary across calls.
- It can misunderstand ambiguous or rare examples.
- Its confidence language does not guarantee correctness.
Use a fixed label set, such as positive and negative. Ask for one label only, then validate the returned text before storing it.
Repeated answers and agreement
One LLM response gives little evidence about reliability. You can sample several responses with a nonzero temperature and measure agreement. If four of five valid responses say positive, the agreement is 0.8.
Agreement measures consistency, not accuracy. A model can consistently make the same mistake. Compare accepted labels with a small hand-labeled gold set before using them for training.
from collections import Counter
LABELS = {"positive", "negative"}
def llm_label(text, ask, samples=5):
prompt = (
"Classify the review as positive or negative. "
"Return exactly one label.\n\nReview: " + text
)
answers = []
for _ in range(samples):
answer = ask(prompt).strip().lower().rstrip(".")
if answer in LABELS:
answers.append(answer)
if not answers:
return None, 0.0
label, count = Counter(answers).most_common(1)[0]
agreement = count / len(answers)
return label, agreement
def demo_ask(prompt):
text = prompt.lower()
return "negative" if any(
word in text for word in ("terrible", "broken", "refund")
) else "positive"
examples = [
"The battery is excellent.",
"The device arrived broken and needs a refund.",
]
for text in examples:
label, agreement = llm_label(text, demo_ask)
if label is not None and agreement >= 0.8:
print(label, round(agreement, 2), text)The ask function represents an LLM client. The demo uses a deterministic substitute so the example runs without an API key. In production, configure the model, temperature, prompt, and retry behavior explicitly.
Filtering labels
A practical filter can require both:
- a minimum agreement threshold, such as 0.8
- a minimum valid-response rate
Reject examples with no valid label or unresolved ties. Store the original text, prompt version, model version, individual answers, and final label. This makes later audits possible.
Combining an LLM with other sources
An LLM should usually become one column in the label matrix. Keyword rules, lexicons, metadata, and human labels can provide independent evidence. A label model can then estimate source behavior instead of treating every LLM answer as certain.
Risks and safeguards
LLM labels can reflect prompt wording, stereotypes, or an unintended shortcut. They can also leak information if the prompt includes a future field or a human-produced answer that would not be available at prediction time. Synthetic text generated by an LLM has related risks: it may repeat the model's vocabulary and reduce coverage of real user language.
Test labels across time periods, subgroups, text lengths, and difficult cases. Change one source at a time and compare performance on the same gold set.
Exercise
Five valid LLM answers are positive, positive, negative, positive, and negative. Should a filter with a minimum agreement of 0.8 accept the label?
Show answer
No. The majority label has agreement 3/5, or 0.6, so the example should be rejected or reviewed.