liveBook · Manning

Generation

As explained in Natural Language Processing in Action, Second Edition

Natural language generation, or text generation, is the computational production of human-readable natural-language text.

Natural language generation (NLG), or text generation, produces human-readable language from a computational process. It is the writing side of natural language processing: a chatbot uses it to compose a reply, while a search engine might use it to summarize results. Pasting a name into a fixed greeting is string manipulation; other generation methods commonly represent output numerically and decode it into readable text. section 1.1.2 section 1.8

A language model generates by estimating which word is likely to follow the words already present. Training on text lets it learn word-occurrence patterns; a prompt supplies the starting context. It chooses a word, adds that word to the context, and repeats. The most probable choice need not win every time: a temperature setting affects how often less-probable choices are selected. This is a way to produce new text, not a guarantee that the text is true. section 10.2 section 11.0

Generation serves different jobs depending on the surrounding pipeline:

  • Autocomplete finishes a sentence; spelling and grammar tools help revise one. section 1.1.2
  • Chatbots compose responses, while search engines can consolidate or summarize results. section 1.8
  • Code assistants can suggest more than a retrieved snippet: a Copilot example starts with a TypeScript comment and function signature and generates the remaining function. section 1.3.1

Try a small next-word generator

Fit scikit-learn’s CountVectorizer to bigrams from sentences in 100 Wikipedia articles. Calculate how often each second word follows a given first word, then use Python’s random.choice to autocomplete sentences. Compare the results with an LLM such as Llama 2. The experiment makes next-word prediction tangible without treating the simpler model and an LLM as equivalent. section 10.4 section 10.2

Figure 10.2 Stochastic chameleons deciding words one at a time
figure

The word-by-word probability diagram shows one possible path through generation; each newly chosen word affects the context for later choices. section 10.2

Where factual accuracy matters, distinguish fluent wording from justified claims. A knowledge graph can supply stored facts for grounded text generation and support logical inferences; next-word statistics alone do not provide that grounding. Generated content can also contain mistakes or bias, so useful output still needs scrutiny. section 11.0 section 1.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains decoding and iterative next-word probabilities, with temperature as a selection control; leaves detailed model implementation aside.
Examples
Uses chatbot replies, search summaries, TypeScript code suggestions, and a Python bigram-autocomplete exercise.
What it assumes
Builds from plain-language explanations of numerical representations and next-word probabilities; the exercise assumes basic Python and scikit-learn use.
What you can do after
Build it from scratch as a bigram autocomplete exercise, then evaluate/understand it against LLM completions and factual-grounding limits.
How it connects
Connects to natural language understanding, embeddings, decoding, chatbots, semantic search, language models, and knowledge graphs.

Also explained in

Compare how 20 books explain generation →

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.