Here’s a fun meme!
For a programmer, I think this makes sense. Generative AI has been evolving rapidly. It is only a matter of time (if we are not already there) to reach a point where it will be able to generate source code of adequate, or even high-quality. And this will force us (programmers) to re-invent our role in the industry.
In this post, I am sharing my first pair programming experience with ChatGPT, which helped me to quickly solve a problem (and make an open source repo out of it).
The problem
The other day, I needed to generate a set of QR codes. I am trying to have my first book published (more on that on another post), and we (my team and I) want to enhance the experience by adding QR references on strategic places inside the book. Our first thought was to go and create them manually, using one of the QR generator services available for free. And then, limitations and blockers started coming our way:
- Some services had trial / freemium plans that allowed for the QR code creation but, after the trial’s end, only a limited set of the generated QR codes would continue to be functional.
- The task was time-consuming. It was counterproductive to have someone do it by hand, multiple times for different services.
- The services we spotted didn’t offer bulk QR code generation, we had to create our QR codes, one by one.
Train of thought
At first, we started looking for alternative services online, preferably with bulk QR code generation. While searching, I started thinking that making my own bulk QR code generator shouldn’t be that difficult. I didn’t have previous experience with QR generation applications. However, I was sure that with a little research, I would find some open source libraries that could help me build something quickly.
With the first Google search, I found some great blog posts about generating QR codes. Nowadays, Python is my go-to programming language so, I intentionally looked for solutions in Python. I started reading the first post, and that was when it struck me; what if I asked ChatGPT to build the application for me?
Doing Agile with ChatGPT
OK, I had ChatGPT to work as my software engineer. That meant I had to play the role of the Product Owner and let it know what we were trying to build. I started by crafting the following requirements (in the form of a ChatGPT prompt).
Hello Miss Lemon*,
I want you to help me create a Python script with the following spec:
- It will use the library Segno to create qr codes
- It will take as input a .csv file that will contain the following columns:
- url
- icon
- file name
- For every row of the .csv file the script will do the following:
- it will create a QR file in .png format with size 3000×3000 pixels.
- The QR file will have as file name the “file name” of the respective .csv column.
- The QR will lead to the url of the respective url column of the CSV.
- In the center of the QR it will include an icon with the file path of the respective icon column of the csv file.
- The png file will be saved in the parent folder inside a folder called qr_codes
* I am using a custom ChatGPT I have created, that I call Miss Lemon (inspired by the iconic character of Miss Lemon, the personal assistant of Hercule Poirot)
We wanted the result to look like this:
Proof of Concept
With the above prompt, ChatGPT created a proof of concept. In addition to the source code in Python, it also provided me with the following context:
This was very helpful as it highlighted which Python libraries I should have installed in order for the code to work and a sample of the input.csv file that was required for the bulk QR code generation.
The initial source code created by ChatGPT didn’t work 💩, due to the following error:
AttributeError: module 'PIL.Image' has no attribute 'ANTIALIAS'
But.. Stack Overflow quickly solved that, and I had my first QR code generated 🏆!
Iterations
Iteration #1: Functionality improvements
Now that I had a proof of concept, I wanted to make the “product” better. So I fed the result of the application back to ChatGPT and asked it to improve quality with the following prompt:
The new source code suggested by ChatGPT worked fine! Again, ChatGPT provided context on the enhancements:
Iteration #2: Source code documentation
Now that we had an application working properly, the next step was documenting our code. So, I requested ChatGPT to add proper docstrings and comments following Python’s best practices. Here’s the prompt and ChatGPT’s reply/reasoning:
I want you to document it following best practices on Python source code documentation. Also explain in the end why you documented it like that.
(Bonus) Basic Error Handling:
ChatGPT automatically implemented checks for missing files and invalid input data. For example, it added logic to verify the existence of icon files before attempting to overlay them on the QR codes. This ensured the script wouldn’t crash if something went wrong.
Iteration #3: README
With the script ready, I wanted to package the project in a way that would be easy for others to use. This meant adding a README.md
file with clear instructions. I asked ChatGPT to draft the README, and it delivered a comprehensive document that included:
- An overview of the project.
- Installation instructions for dependencies.
- Detailed usage steps with examples.
- Guidance on customizing QR code resolution and icon size.
- Error-handling tips for common issues.
You can find the README.md file, alongside the whole project, available in this Github repository.
Conclusions
In this post, we created a small-size (in terms of functionality) application. ChatGPT created the respective source code and, after “further discussion” via prompts, it improved the code base.
Notable highlights of the process were:
- ChatGPT was able to infer some of the requirements (i.e. adding basic error handling in the code).
- While the discussion was evolving, ChatGPT remembered and applied previous requests. For example, the request for a documented source code was done once, and it was automatically applied in every following iteration by ChatGPT.
I believe that Generative AI can help us craft high-quality source code by generating parts of it and iteratively improving them. For this process to be effective, we have to be able to converse with Generative AI models in a clear and structured way.
NOTE: As I continue to work on this project, I will probably either update this blog post or create follow-up posts, so stay tuned!