liveBook · Manning

Few-shot learning

As explained in Natural Language Processing in Action, Second Edition

Few-shot learning is adapting a model to perform a task from a small number of examples supplied at inference time, often in the prompt.

Few-shot learning asks a language model to pick up a task from a small set of examples supplied in a conversational prompt. The examples provide context for the next response rather than a separate training set. This can be useful when a chatbot needs to follow a particular interaction pattern, but matching the pattern is not the same as getting the answer right. section 10.1.1 section 12.5.2

A practical setup places task instructions, examples or conversation history, and the current user message in a prompt template. A LangChain conversation can carry earlier turns into that template. Keep the requested behavior explicit, then test the responses against the task’s actual requirements. section 12.5.2

>>> math_quiz_pt = """
... You are a math teacher that's teaching math to a third-grade
... student. Prompt the student to complete number sequences
... from the following list and compare their answer with the
... last number in the sequence:
...   - 9,10,11,12
...   - 38,39,40,41
...   - 2,4,6,8
...   - 1,5,9,13
...   {history}
...   student:{input}
...   tutor:"""

The math-tutor prompt supplies several number sequences and asks the model to compare a student’s answer with the expected next number. section 12.5.2

A plausible response that fails the task

For the sequence 2, 4, 6, 8, a student answers 12. The tutor replies, “Great job! Your answer is correct,” then says the next number is 10. It follows the encouraging style of a tutor without checking whether its judgment agrees with its own answer. A few-shot chatbot therefore still needs checks for requirements that must hold, especially when teaching children. section 12.5.2

Larger models may handle more complex in-prompt tasks, but scale is not a guarantee: measured capabilities can improve gradually, stay flat, or perform no better than chance. For a deployed chatbot, try prompts more than once, assess responses against the goal, and consider rule-based checks where an erroneous answer would matter. section 10.1.1 section 12.5.2

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines in-prompt learning and examines a chatbot failure, but does not provide a systematic few-shot prompting recipe.
Examples
Shows a number-sequence tutor approving 12 while identifying 10 as the next value.
What it assumes
Assumes basic Python for the chatbot code while introducing few-shot learning through prompts and observed responses.
What you can do after
Apply it via LangChain prompts, then evaluate generated answers against explicit task requirements.
How it connects
Connects prompt templates and conversation memory to model scaling and rule-based conversation management.

Also explained in

Compare how 18 books explain few-shot learning →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.