liveBook · Manning

Code generation

As explained in How Large Language Models Work

Code generation is the automated production of computer-program source code from prompts, specifications, or other inputs.

Code generation uses an LLM to produce source code from prompts or other inputs. Code is a useful target because programming languages impose explicit rules, and generated output can be checked more objectively than ordinary prose. section 6.1 section 6.1.2

The model processes code as tokens. Case-sensitive names remain distinct, while comments and descriptive variable names connect programming instructions with human descriptions of intent. section 6.1

Generated code can be checked in several increasingly useful ways:

  • Compile it into an executable; compilation catches a substantial portion of incorrect code. section 6.1.2
  • Run unit tests that supply sample inputs and check whether the output is correct. section 6.1.2
  • Use a syntax parser during generation to reject token sequences that would create syntax errors. section 6.1.2

Catching a Python syntax error early

After generating `if(A > B)`, a syntax checker can reject a newline as the next token because the statement requires a colon, prompting an alternative token instead. section 6.1.2

Figure 6.5 A Python code example where the current tokens if(A > B) have been generated. If the next token produced by the LLM is a newline, a syntax error will occur because an if statement must end in a colon to be valid. Running a syntax checker on each new token allows us to catch this error and force the LLM to pick an alternative token that doesn’t cause a syntax error.
figure

Shows how token-by-token syntax checking can prevent an invalid continuation before generation finishes. section 6.1.2

How How Large Language Models Work covers it

How deep it goes
Explains token handling, compilation, unit tests, and incremental syntax checks, but does not detail implementation beyond these validation methods.
Examples
Uses Python and Java code, comments, descriptive names, compilation, unit tests, and a Python if-statement syntax check.
What you can do after
Diagnoses generated code by compiling it, testing its behavior, and detecting syntax errors during token generation.
How it connects
Connects code generation with LLMs, tokenization, compilers, unit tests, syntax parsers, supervised fine-tuning, and reinforcement learning from human feedback.

Also explained in

Compare how 17 books explain code generation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of How Large Language Models Work. The section links point to the parts of the book it draws on.