liveBook · Manning

Chapter 3: Classifying user intent

What you will learn in chapter 3 of Building LLM Applications with DSPy

The introduction explains text classification and frames intent classification as mapping varied customer requests to a predefined set of intents.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains text classification and frames intent classification as mapping varied customer requests to a predefined set of intents.

Key terms: classification, label, class, sentiment classification, intent classification, intent classifier, predefined set of intents

3.1 Creating a baseline classifier

Defines a baseline as a simple working first attempt that supports iteration and helps gauge task difficulty.

Key terms: baseline, successive iterations

3.1.1 Utterances

Defines the short user messages classified for intent and explains why longer text is harder to classify.

Key terms: intent classification, user utterances, intent

3.1.2 Multiclass, multilabel, and dynamic classification

This section distinguishes three ways to describe an intent-classification problem: by the number of possible classes, the number of labels per utterance, and whether the classes can change.

Key terms: binary classification, multiclass classification, classes, single-label, multilabel, utterance, intent, static classification, dynamic classification

3.2 Downloading and preparing the ATIS dataset

The section prepares ATIS airline requests for a baseline intent classifier by selecting the test data, inspecting it, and cleaning up its intent labels.

Key terms: ATIS dataset, Hugging Face, datasets Python library, zero-shot prompting, test data, pandas dataframe, user intents, value_counts(), intent names, Flight Booking Request

3.3 Building a DSPy intent classifier

This section shows how a minimal DSPy intent classifier turns a message and possible labels into a prediction using a reusable prompt template.

Key terms: language model, DSPy signature, DSPy module, Prediction, prompt template, input fields, inspect_history(), utterance, labels, labels parameter

3.3.1 The signature

This section explains how a class-based DSPy signature defines the intent classifier’s inputs and output, and how types and descriptions shape the LM’s prompt.

Key terms: dspy.Signature, InputFields, OutputFields, message, labels, intent_label, class-based signature, string signature, docstring, field description

3.3.2 The module

The section turns an intent-classification signature into a working DSPy baseline and explains how to try it on test messages while accounting for LM calls.

Key terms: Predict module, signature, program, message, labels, intent_label, test data, dataframe, prediction, LM call

3.4 Using the OpenAI API directly

This section explains why direct LM APIs are worth understanding and where DSPy’s abstraction does—and does not—replace them.

Key terms: DSPy APIs, API signatures, prompts, responses, embeddings, OpenAI, API, parameters, Claude, Gemini

3.4.1 Classification using the OpenAI API

The section walks through intent classification with the OpenAI API and explains why hand-built prompts become harder to maintain than DSPy signatures and modules.

Key terms: OpenAI client, system prompt, intent labels, user message, utterance, create(), handcrafted prompt, signatures, modules, substrings

3.4.2 System and user roles

This section explains how system and user messages separate a task’s definition from its input, and why that distinction matters for security.

Key terms: system prompt, user message, Signature, Module, prompt injections, hierarchy of roles, user prompt, LM provider

3.4.3 Comparing DSPy with direct use of LM APIs

This section compares a DSPy intent classifier with direct LM API use and explains why DSPy’s conveniences matter more as applications grow.

Key terms: LM providers, OpenAI API, signature, module, system and user messages, Prediction object, JSON, Structured Outputs, manual tuning, retune

3.5 Dynamic labels vs. static labels

This section contrasts fixed intent labels with changing production labels and explains why LMs are useful when new intents arise.

Key terms: static set of classes, dynamic labels, intents, DSPy interface, dynamic sets of labels, LM-based classification, none of the above, other, unknown, clustering assumption

3.5.1 Dynamic intent classification

The section shows how a dynamic intent classifier can handle messages that do not fit any provided label and what can be done with those messages.

Key terms: signature, docstring, None, choices, intent label

3.5.2 Static intent classification

This section explains why a listed set of intent labels does not by itself constrain an LM’s output and how a typed output field can restrict the allowed labels.

Key terms: static intent classification, Literal, output field, fixed vocabulary

3.6 Handling multiple intents

The section explains why multilabel intent classification is preferable to compound labels, how to adapt a DSPy classifier for it, and why a successful example still needs evaluation.

Key terms: single-label, multilabel, compound intents, DSPy, docstring, output field, single test, proper evaluation, optimize the prompt

3.7 Viewing the history

This section explains how to inspect DSPy’s LM-call history, retrieve generated prompts, and track usage and costs.

Key terms: inspect_history(), history, input and output tokens, cost, prompt text, prompt template, Adapter, LM object, lm.history.clear(), n

3.8 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Basic prompting and DSPy Chapter 4: Evaluating DSPy programs →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.