Let’s code with ChatGPT

The friendly face of AI as imagined by Stable Diffusion 2.1.

Background

So, everyone has heard about the almighty chat GPT. It started with Dall-E in 2022 and a little later, in July, we got “Mid Journey” and then a bit later still “Stable Diffusion”. The thing they have in common is that these are AI art generators. With a text as input it will produce an image in a instant.This post is not about that but the large language model that is chat GPT.

I wrote this article back in november 2022, so the version of chat GPT used is 3. There is as of april 2023 version 4 that is supposed to be very powerful. I will have to investigate that too down the road.

What is it?

GPT in the name stands for Generative Pre-trained Transformer. Here is a link if you wish to try it out https://chat.openai.com/chat

As opposed to the AI art, this will only chat, not create stunning images. So why is it so popular? In this article, I will attempt to look behind the curtain and argue what is so great with this AI and why programmers should not be scared of it, but embrace it. At the same time highlight issues with such a powerful tool. With great power comes great responsibility.

What can it do?

So, ChatGPT is a free open-source chat bot which -to my surprise- is actually clever. In my tests during the past week, I’ve been able to ask it to write code in Matlab, Mathematica, Python, C#, Javascript, and OpenSCAD. The quality of the answers are varying but overall it’s impressive.

I narrowed the tests down to giving it problems and asking it to respond with scripts written in Matlab. The reason for doing so is that my original codes were written in Matlab and it is easy to follow even if you don’t know Matlab.

 

Example 1

In the first example I asked chatGPT to generate Matlab code that returns the number of Faro shuffles required to get back to the original order of a deck. This is what it returned after a few seconds

function shuffled_deck = faro_shuffle(deck)
  half1 = deck(1:floor(end/2));
  half2 = deck(floor(end/2)+1:end);
  shuffled_deck = [half1(:); half2(:)];
end

function num_shuffles = faro_shuffles(k)
  num_shuffles = 0;
  deck = 1:k;
  while ~isequal(deck, 1:k)
    deck = faro_shuffle(deck);
    num_shuffles = num_shuffles + 1;
  end
end

At first this looks really impressive. However, if you look closer, inside the function faro_shuffle half1 and half2 needs to be transposed. Inside the faro_shuffles function the loop will never enter because the deck is already 1:k. After we fix these issues the code works. Note: It only works for even numbered decks of cards.

Review: 4/5. It almost got there but not quite, it needed some help but very impressive.

Example 2

The next example is a simple problem that I got from “advent of code” day 1 in 2016 which I’ve written about https://matzjb.se/2016/12/08/advent-of-code-2016/

I basically asked it to create a turtle code where the inputs are L and R, which is rotating the turtle left and right 90 degrees. Here is the code that chat GPT produced:

function distance = turtleCode(filename)
    % Read instructions from the file
    instructions = readInstructions(filename);

    % Set the initial position and orientation of the turtle
    pos = [0, 0];
    heading = 90; % 90 degrees is pointing upwards

    % Iterate through the instructions and execute them
    for i = 1:length(instructions)
        instr = instructions{i};
        dir = instr(1);
        steps = str2double(instr(2:end));

        % Update the heading and position
        heading = mod(heading + (dir == 'L' ? -90 : 90), 360);
        if heading == 0
            pos = pos + [steps, 0];
        elseif heading == 90
            pos = pos + [0, steps];
        elseif heading == 180
            pos = pos + [-steps, 0];
        elseif heading == 270
            pos = pos + [0, -steps];
        end
    end

    % Calculate the distance to the final position
    distance = norm(pos);
end

function instructions = readInstructions(filename)
    % Read the file and split the lines
    lines = splitlines(fileread(filename));

    % Extract the instructions from the lines
    instructions = cell(length(lines), 1);
    for i = 1:length(lines)
        instr = lines{i};
        dir = instr(1);
        steps = str2double(instr(2:end));
        instructions{i} = [dir, num2str(steps)];
    end
end

I will not check if this is correct, but it looks fine to me. It rotates the direction of the turtle using mod and uses the norm function to get the distance the turtle travelled, nice.

Review: 5/5. This seems to do what we want and even if it’s wrong, we have a base to stand on.

 

For comparison, this is my “code golf” solution:

dRel      = textscan(fopen('input-Day-1.txt'), '%c%d', 'Delimiter', ',');
coords    = cumsum( exp(1i*(repelem(cumsum( 2*(dRel{1} == 'L') - 1 ), dRel{2}))*pi*.5) );
[vS, vI]  = sort(coords);
intsctn   = min( vI(~[abs(diff(vS)); 0]) );

I fed this to the AI and asked what the code does, here was its response:


The cumsum function is used to calculate the cumulative sum of the instructions, which effectively updates the position of the turtle based on the instructions. The exp function is used to convert the instructions to complex numbers, which represent rotations in the complex plane. The repelem function is used to repeat the cumulative sum of the instructions, and the pi and *.5 values are used to scale the rotations to the appropriate angles (90 degrees for left turns, 270 degrees for right turns).

I then went on to ask if it thought the code was clever, here was its response:

It is difficult to say definitively whether the code is “clever” or not without more context. However, the code does use a somewhat unconventional approach to solve the problem of calculating the final position of a turtle-like object based on instructions read from a file.

This is cool, chatGPT sounds as though it appreciate my way of solving the problem.

Closing remarks

So, even though this version of ChatGPT has some shortcomings, it is very impressive. I can see it becoming a very powerful tool that super charges me as a software engineer. Sort of like Google on steroids. I can reason with it logically and get tips on code in several languages. I believe that using chat GPT in this way will help us learn faster. I don’t think it even needs to be accurate all of the time. You the coder has to check if the code it produces is correct and learn from the choices it makes. Truly wonderful time to be alive.

 

ChatGPT is Google search on steroids.

 

Leave a Reply

Your email address will not be published. Required fields are marked *