Sub topic 13
Alternative: Prompting a Generative Model
Assume you know basic Python, supervised text classification, and the general idea of a pretrained language model. This chapter treats a generative model as a classifier by asking it to produce a label instead of calculating an entailment score.
The working model
A generative language model predicts the next token in a sequence. A prompt can describe the allowed categories, provide the input text, and require a specific output format. The model then generates a label.
For example:
Classify the message using exactly one label:
- billing
- technical support
- sales
Message: I was charged twice for my subscription.
Label:The model may generate billing. This is called zero-shot prompting because the prompt contains instructions but no labeled examples. If you include examples of messages and their labels, the method becomes few-shot prompting.
This approach differs from an NLI classifier. An NLI model compares a premise with a hypothesis for each candidate label. A generative model reads one instruction and writes an answer. Generative prompting can express more complex rules, but its output is less naturally constrained.
A minimal Hugging Face example
The following example uses FLAN-T5, an instruction-tuned sequence-to-sequence model. It generates text from a text prompt.
from transformers import pipeline
classifier = pipeline(
"text2text-generation",
model="google/flan-t5-base",
)
labels = ["billing", "technical support", "sales"]
text = "I was charged twice for my subscription this month."
prompt = f"""Classify the customer message.
Return exactly one label from this list: {", ".join(labels)}.
Do not explain your answer.
Customer message: {text}
Label:"""
result = classifier(
prompt,
max_new_tokens=8,
do_sample=False,
)[0]["generated_text"].strip()
print("raw output:", result)
normalized = result.lower().strip()
if normalized in labels:
print("accepted label:", normalized)
else:
print("abstain: unexpected model output")do_sample=False makes decoding deterministic by choosing the most likely continuation. max_new_tokens limits how much the model can write. These settings reduce, but do not eliminate, extra text such as The answer is billing.
Always validate the generated result against the allowed labels. Never use arbitrary generated text directly as a database category or permission decision.
Prompt design and common errors
A useful prompt specifies:
- The task and the input field.
- The complete set of allowed labels.
- Whether one or several labels are permitted.
- The exact output format.
- What to do when no label fits.
Clear label descriptions often work better than short internal names. For example, payment and subscription charges gives the model more meaning than billing_code_7.
A generative model can invent a label, return two labels when one is required, or copy words from the message instead of selecting a category. It can also follow instructions hidden inside the input text. Treat user text as untrusted data and place it in a clearly marked field.
A prompt can request JSON, but generated JSON still requires parsing and schema validation. If invalid output is common, use constrained decoding or an NLI or embedding-based method instead.
When to choose this approach
Prompting is useful when categories have detailed definitions, outputs need explanations, or the task requires several related decisions. It also avoids training a classifier.
Its costs include slower generation, variable output formatting, and potentially higher inference cost. NLI pipelines are usually easier to constrain to a fixed label list. Embedding similarity is often faster for large collections of texts. Fine-tuning is preferable when you have enough labeled data and need stable, high-volume predictions.
Exercise
Why should an application validate a generative classifier's output against the allowed label list?
Show answer
The model may generate explanations, multiple labels, or a label that was not allowed. Validation prevents malformed or unsafe categories from entering later systems.