ChatGPT Code Generation Examples Part 1

As has everyone else, I’ve been spending some time trying to work ChatGPT into my daily workflows. One of the most powerful examples I’ve found recently is it’s code generation ability. These examples were so impressive to me that I’ve been sharing them with everyone I’ve spoken to, and it convinced me to write my first blog post in 8 or 9 months :)

Both of these examples are particularly great as I was unable to find anything that did this through Google. Apologies for all of the screenshots, I wanted to show the full conversation with all of ChatGPT’s amazingness, but also show its flaws. I’ll walk you through two problems that ChatGPT was able to solve for me:

The first problem:

I have been working on building a VM base image pipeline for machines hosted in Google Cloud, and was developing a way to manage the lifecycle of the images I create. Google Cloud provides some best practices here that recommends the following:

  • The most recent image is active, and is the image that teams pull when building a VM
  • The second most recent image is marked as deprecated, which means it can still be used, but is not the default when you target my image family
  • The third most recent image is marked as obsolete, which makes in unavailable for use
  • The fourth most recent image is deleted

The gcloud documentation for doing uses this command to accomplish this:

gcloud compute images deprecate $IMAGE_NAME \\
    --state $STATE \\
    --replacement $REPLACEMENT

Pretty simple, just replace IMAGE_NAME with the image you want to change the state of, STATE with the state you desire, and REPLACEMENT with the replacement image. However, I’m going to be continuously building these base images, and don’t want to manually run 4 commands each time to maintain a proper image lifecycle.

So, I turned to ChatGPT:

Untitled

Fair enough, this is about what I expected. It more or less regurgitated the commands from the documentation above. I then took it a step further: I asked for specific commands to maintain the lifecycle I wanted:

Untitled

Awesome! Everything worked perfectly, but there is still one big flaw. These are all ad-hoc commands with hardcoded arguments, and I want to automate this process entirely. I asked it to do this in a loop:

Untitled

Even better! It knew to query the list of all images and then mark them accordingly. I then recognized an edge case: if for some reason there are more than 4 images in the image family when this runs, it won’t do anything with them. So, I asked it to delete anything older than the third image:

Untitled

This worked like a charm! I discovered one final edge case. If there aren’t enough images in the family, this will throw an exception as it will be running commands on an image that doesn’t exist. I asked ChatGPT to make it safe:

Untitled

This was amazing! I now had a nearly complete script after only about 5 minutes of chatting. There were a couple of small issues with the code, such as not adding the show-deprecated flag to the initial list command. If this flag isn’t passed, you may not see the images that you need to delete or mark as obsolete/deprecated. I simply asked ChatGPT to add this:

Untitled

This is where it got slightly confused, I wanted it to add --show-deprecated to the list command, but it decided to filter out deprecated images in the safety check via grep, essentially doing nothing. I corrected it:

Untitled

Exactly, what I wanted, but we still needed to remove the grep filter form the safety check:

Untitled

So close! For some reason, it just added an entirely new conditional rather than modifying the existing one. It’s also only referencing the active images throughout the rest of the script, which is incorrect. One final ask:

Untitled

Boom! This works flawlessly. I started with a very vague description of what I wanted to do, and with a little bit of conversation, I was able to get a 100% working solution that I could not find much on through Google. All in all, this was seriously impressive. Even though there was a lot of nit-picking and corrections that needed to be made, I got 90% of what I needed in no time, and it was able to resolve the other issues with a little but of back and forth.

This was amazing to me, and while not nearly as impressive as other examples, this is what truly made me realize the power of this technology and inspired me to continue using it regularly. Since this interaction, I have used it for countless other tasks, similar and not, more difficult and less.