Ask HN: How is ChatGPT able to run code?

4 points by dakiol ↗ HN
In https://news.ycombinator.com/item?id=33847479 it seems that ChatGPT was able to "execute" Python code (https://www.engraved.blog/content/images/2022/12/image-10.png). I somehow understand how ChatGPT can generate code (it has been trained on billions of lines of code)... but I don't fully understand how it can execute code. Execution of code requires 100% emulation of a given compiler. If X can emulate 100% a compiler, then effectively X is a compiler. Now, perhaps ChatGPT cannot execute 100% ANY Python snippet of code, but only some subset?

Or is it "just" that the AI has been trained on so much code (and the output of the code executed) that it effectively can replace a Python compiler (or any other compiler)?

4 comments

[ 4.6 ms ] story [ 21.3 ms ] thread
Its not executing code, its guessing what the answer is, the same way you would do mentally.
I know it's guessing it... but if by guessing it outputs the correct answer, well then what's the difference between the AI and the actual Python compiler? That's my question: how is the AI able to guess the correct answer just like the actual Python compiler?

Outputting code and poems is one thing (there is no "correct" poem nor piece of code). Executing code is another: there is only one correct execution output for a given piece of code.

I would theorize that, the AI is "merely" manipulating an internal array (either the text-answer box, or some internal thinking layer) while abiding by the "compilation rules" of whatever Language the user has input. This producing a result that mirrors that of a proper compiler. To test this hypothesis, I would input python programs with more variables/functions/language features. I would expect the non-compiler AI to return an invalid response at some point, unable to "step theough" complex programs with only basic pattern matching.

I don't know if rigorous CS proofs of "Turing Machine-Emulation" also hold for a Language model.

I used the prompt ‘Calculate the permutations of the world “safer” in python’

It gave me the correct code (which shows all 120 permutations when ran in Python) :

    import itertools
    word = "safer"
    # Get all permutations of the word
    permutations = itertools.permutations(word)
    # Print each permutation
    for permutation in permutations:
      print(permutation)
It then showed the output, but got it wrong. It listed 24 permutations, then said ‘As you can see, there are 24 possible permutations of the word "safer".‘

Interestingly, if you ask it how many permutations “safer” has, not asking about code, it invents some maths that produces 24 (see my reply in another post), so I guess it’s the understanding of permutations which is off.