liveBook · Manning

Classification

As explained in Building LLM Applications with DSPy

The task of assigning an input to one or more predefined categories.

Classification assigns an input to one or more predefined categories. A task is binary or multiclass according to how many classes are available, and single-label or multilabel according to how many labels each input may receive. Labels can also be static or dynamic. section 3.1.2 section 3.8

For language-model classification, clear classes and inputs that fit one class make the task easier. Ambiguous messages may be hard to classify reliably; an Unknown option can provide a fallback. With DSPy, a baseline classifier specifies a language model, signature, and module. section 2.1.1 section 3.8 section 5.6

Choose the classification setup to match the task:

  • Use binary classification for two classes or multiclass classification for three or more. section 3.1.2
  • Use single-label classification when each input has one intent; use multilabel classification when it may have several. section 3.1.2
  • Use static labels for a fixed class set or dynamic labels when the set can change. section 3.1.2

Customer intent

A message such as “My new email address is bob.smith@domain.com” can be classified as Update Email. A request to find a flight and report its food options has two intents, making it a multilabel example. section 2.1.1 section 3.1.2

figure
prompt = '''You are an expert customer intent classifier. Your goal is to classify customer messages into one of 5 predefined classes.
If you are not sure, return Unknown.

Labels:
1. Cancel Subscription
2. Update Email
3. Refund Request
4. Bug Report
5. Unknown

The output should be a JSON with the following structure:
{reasoning: "", intent: ""}

---------
Example:
Customer message: I want to end my subscription

---------
Example Output:
{Reasoning: "The main object is subscription and the main action is ending hence the user wants to cancel his subscription"
Intent: "Cancel Subscription"}

---------
Customer Message: My new email address is bob.smith@domain.com
Intent: 
'''

This prompt specifies customer-intent labels, an Unknown fallback, and a structured output for a language model. section 2.1.1

Classification can be a practical alternative to open-ended generation when a task can be expressed as choosing among clear categories. One evaluated classifier scored about 91% without optimization; optimization produced some gains, but small differences may reflect random variation. section 5.6

How Building LLM Applications with DSPy covers it

How deep it goes
Distinguishes binary, multiclass, single-label, multilabel, static, and dynamic tasks, then names DSPy baseline components.
Examples
Uses customer-intent prompts, an ambiguous service complaint, and a flight request with two intents.
What you can do after
Build a baseline classifier by specifying a language model, DSPy signature, and DSPy module.
How it connects
Connects classification to language models, prompt engineering, DSPy, and sentiment analysis.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building LLM Applications with DSPy. The section links point to the parts of the book it draws on.