# Simon Willison’s Weblog

## Using GPT-3 to explain how code works

One of my favourite uses for the GPT-3 AI language model is generating explanations of how code works. It’s shockingly effective at this: its training set clearly include a vast amount of source code.

(I initially thought this was related to GitHub Copilot being built on GPT-3, but actually that’s built on a GPT-3 descendent called OpenAI Codex.)

Here are a few recent examples.

#### Explaining some Python

Ned Batchelder shared this brilliant Python code snippet on Twitter this morning:

import re

TOKEN_RX = r"""(?xm)
(?P<string> ".*?"|'.*?'             )|
(?P<float>  \d*(\d\.|\.\d)\d*       )|
(?P<int>    \d+                     )|
(?P<id>     [_a-zA-Z][_a-zA-Z0-9]*  )|
(?P<punct>  [(){}:=.,+*/-]      )|
(           \#.*$)| ( \s+ ) """ def tokens(text): for match in re.finditer(TOKEN_RX, text): if match.lastgroup: yield (match.lastgroup, match[0]) TEXT = """ x = 123 + "hello #99" # ignore me! print(hello.bye[0] + 3.14, 'single') """ for kind, text in tokens(TEXT): print(f"{kind:7}: {text=}") I pasted it into GPT-3 (here’s how to use the playground interface) and started asking questions. This uses the feature of GPT-3 where you can get a response, then add more text and run it again to get further details. Anything in bold is text that GPT-3 responded with. The rest are my prompts. Explain this code: import re TOKEN_RX = r"""(?xm) (?P<string> ".*?"|'.*?' )| (?P<float> \d*(\d\.|\.\d)\d* )| (?P<int> \d+ )| (?P<id> [_a-zA-Z][_a-zA-Z0-9]* )| (?P<punct> [(){}:=.,+*/-] )| ( \#.*$                   )|
(           \s+                     )
"""

def tokens(text):
for match in re.finditer(TOKEN_RX, text):
if match.lastgroup:
yield (match.lastgroup, match[0])

TEXT = """
x = 123 + "hello #99"  # ignore me!
print(hello.bye[0] + 3.14, 'single')
"""

for kind, text in tokens(TEXT):
print(f"{kind:7}: {text=}")


The code is defining a regular expression that will match different types of tokens in a string of text. The different groups defined in the regular expression will match different types of tokens, such as strings, floats, ints, ids, punctuation, and comments. The code will then iterate over the matches and yield the type of token and the text that was matched.

Explain the regular expression:

#### Explaining configuration files

Here’s another prompt pattern I stumbled across that seems to work really well: “Explain every line of this config file:”. I tried it against the Lit example from the Vite 3.0 starter templates:

Explain every line of this config file:

import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
build: {
lib: {
entry: 'src/my-element.js',
formats: ['es']
},
rollupOptions: {
external: /^lit/
}
}
})

This config file is written in JavaScript, using the ViteJS library. The first line imports the “defineConfig” function from the ViteJS library. This function allows the user to define a configuration object for the build process.

The next line exports the “defineConfig” function as the default export. This means that when this file is imported, the “defineConfig” function will be the only thing that is imported.

The “defineConfig” function takes one argument, which is an object. This object contains the configuration for the build process. The “build” property contains the configuration for the build tool. The “lib” property contains the configuration for the library that will be built. The “entry” property contains the path to the entry file for the library. The “formats” property contains the formats that the library will be built in. In this case, the library will only be built in the “es” format.

The “rollupOptions” property contains the configuration for the Rollup tool. The “external” property contains a regular expression that matches the names of the dependencies that should be excluded from the build. In this case, all dependencies whose names start with “lit” will be excluded from the build.

This is Using GPT-3 to explain how code works by Simon Willison, posted on 9th July 2022.