https://simonwillison.net/2022/Jul/9/gpt-3-explain-code/ 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 ".*?"|'.*?' )| (?P \d*(\d\.|\.\d)\d* )| (?P \d+ )| (?P [_a-zA-Z][_a-zA-Z0-9]* )| (?P [(){}:\[\]=.,+*/-] )| ( \#.*$ )| ( \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 ".*?"|'.*?' )| (?P \d*(\d\.|\.\d)\d* )| (?P \d+ )| (?P [_a-zA-Z][_a-zA-Z0-9]* )| (?P [(){}:\[\]=.,+*/-] )| ( \#.*$ )| ( \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: The regular expression is made up of a number of different groups, each of which match a different type of token. The first group, "string", matches any text enclosed in double or single quotes. The second group, "float", matches any text that looks like a floating point number. The third group, "int", matches any text that looks like an integer. The fourth group, "id", matches any text that looks like a variable name. The fifth group, "punct", matches any text that looks like punctuation. The sixth group, "#.*$", matches any text that looks like a comment. The seventh group, "\s+", matches any text that looks like whitespace. What does (?xm) do? The (?xm) notation is a modifier that allows the regular expression to span multiple lines and to use comments. Why does it use match.lastgroup? The match.lastgroup variable contains the name of the last group that was matched by the regular expression. This is used to determine the type of token that was matched. This is a really good, really clear explanation of what's going on. I already knew how that code works though. Let's try something that I genuinely didn't understand. Explaining some JavaScript Chris Haynes tweeted an ingenious demo showing how the new Web Component Declarative Shadow DOM mechanism (described here) can be used to create a page that streams content from a server, filling in different regions of the page as the content arrives--without using any JavaScript! His demo is at enamel.pages.dev but only works for me in Chrome. For other browsers, he provides enamel.pages.dev/?js which loads a tiny polyfill. The polyfill code lives in enamel-lite.js and I could not figure out how it works. So I asked GPT-3. Explain this code: class Enamel extends HTMLElement { attemptPolyfillDSD() { const dsd = this.querySelector('template[shadowroot]'); if (dsd?.content) { const mode = dsd.getAttribute('shadowroot'); this.attachShadow({ mode }); this.shadowRoot.appendChild(dsd.content); dsd.remove(); return true; } return false; } connectedCallback() { if ( !HTMLTemplateElement.prototype.hasOwnProperty('shadowRoot') && !this.attemptPolyfillDSD() ) { const _observer = new MutationObserver(() => { if (this.attemptPolyfillDSD()) { _observer.disconnect(); } }); _observer.observe(this, { childList: true, }); } } } export default Enamel; This code polyfills the Shadow DOM API for older browsers that do not support it. OK, that sounds convincing, but it's not enough information for me. So I added another prompt: How does it do that? The code uses a MutationObserver to watch for changes to the element's children. When a new child is added, the code checks to see if that child is a