I think we can all agree that GPT (Generative Pre-trained Transformer) can be useful but also happily shows its flaws. Like all tools, it has its place in our toolbelt, so let’s learn a little more about GPT.
How Was GPT Trained
Like other professions’ tools that are crafted and forged, GPT model was crafted (trained) to become what the creator’s OpenAI wanted. Their goal for GPT was to create a helpful model, always providing useful responses to ANY query.
A side effect of training this way is that it doesn’t always give accurate information. It does not have live access to the internet and was only trained on data up to 2021.
By knowing these things, we can figure out when to use it and when not to; we can’t use it if we expect any new information to be a part of its response. We must also be skeptical about any information it gives us as it will always try to be helpful, even though we don’t find lies helpful (never lie kids).
Utilizing GPT Professionally
I use GPT as a paired programming partner. I needed to refactor some code while working on a test data generation tool. I knew I could offload some of this work for GPT. Below, is an outline of how I utilized GPT to help.
The ask: We had a C# data-generation tool that inserted four prescription records for any patient added. To add variability to the test data, we wanted to add a random amount of prescriptions to the patient, anywhere from four to sixteen.
Current code: The four records were hardcoded into a GenerateScripts()
method on the Prescription
class that returned a list of prescriptions.
My refactor: Because we now needed to generate more prescriptions for a patient, hardcoding was not the way. I wanted to change the Prescription
class into a base class and have derived classes for unique prescriptions. Each newly derived Prescription
class would just set the properties of the prescription in its constructor.
Work to offload to GPT: I needed to offload something to GPT. However, it didn’t know the code structure, and I didn’t want to give it any information that I didn’t need to, so what could I hand off?
Since the task was to generate a random number of prescriptions for each patient and I knew I was planning on making Prescription
a base class, I needed a way to select a randomly derived class.
Reflection to the rescue! It’s been a while since I’ve done reflection, but reflection isn’t new (it’s pre-2021), so GPT will be familiar. I could explain the class structures to GPT and ask for a method that returns a random number of prescription objects that inherit the Prescription
base class!
While GPT worked on that, I worked on creating all the derived classes for the prescriptions. Perfect.
My GPT prompt: “I need a C# method (named GenerateScripts) that returns a list of Prescription objects. This will be a random number of objects that I can pass in as a parameter, using reflection randomly select an object that inherits the Prescription base class. Don’t let multiple of the same object be added to the list.”
GPT response:
public static List<Prescription> GenerateScripts(int count) { List<Type> prescriptionTypes = Assembly.GetExecutingAssembly() .GetTypes() .Where(type => type.IsSubclassOf(typeof(Prescription))) .ToList(); List<Prescription> prescriptions = new List<Prescription>(); for (int i = 0; i < count; i++) { Type randomType = prescriptionTypes[random.Next(prescriptionTypes.Count)]; if (prescriptions.Any(p => p.GetType() == randomType)) { i--; continue; } Prescription prescription = (Prescription)Activator.CreateInstance(randomType); prescriptions.Add(prescription); } return prescriptions; }
If you look at this and think I could do better, that’s probably true; I could. From this first go, I could already see I needed to remove the static (I didn’t ask for that); it was going to add multiple instances of the same derived prescription class to the list (which I asked it NOT to do!).
Was GPT useful? Well, I didn’t need to go search for how to do reflection myself (I now have a personalized example to go back to in the future). I’ll still need to do some work, but I was able to copy-paste this method into the code, and it worked on the first try (after removing the static)!
Some of this is also still GPT being general purpose, me learning how to use it, and knowing it’s like a first year and 10+ year developer at the same time. I need to give it exact instructions all while it knows and can recall very specific information in seconds.
Conclusion
Despite its occasional inaccuracies, GPT is a remarkable tool that can greatly enhance productivity across various domains. Understanding its underlying workings and limitations empowers us to use it effectively and harness its capabilities for idea generation, research, writing assistance, and workflow optimization.
Let’s start leveraging GPT as the powerful productivity tool that it is – happy prompting!
How do you use GPT? Do you think it’s trash or treasure? Share in the comments below, and head to the Keyhole Dev Blog for more content.