David Amos: ChatGPT Is An Extra-Ordinary Python Programmer :
by:
blow post content copied from Planet Python
click here to view original post
This article contains affiliate links. See my affiliate disclosure for more information.
I like ChatGPT. A lot. It&aposs the best digital writing assistant I&aposve ever used.
If my time using ChatGPT has taught me anything, though, it&aposs that ChatGPT isn&apost a brilliant writer. I&aposm not worried about my job. And if my experience using ChatGPT to write a Python program from scratch is any indication, programmers don&apost need to worry either.
But you should be paying attention.
A Promising Failure
I&aposve been thinking about building a terminal app to practice typing for a while, so I decided to give ChatGPT a shot at building it.
This was my prompt:
ChatGPT&aposs program is surprisingly close what I want:
"well-commented"It&aposs impressive how much ChatGPT gets right here. The code runs error-free. It gets the accuracy calculation correct and computes raw speed in words per minute. It is, for all practical purposes, a functional typing analysis app.
But it doesn&apost color the user input as I requested:
Again, the code runs. Here&aposs a sample execution:
That&aposs not quite what I want, but it&aposs getting closer. I tell ChatGPT that the program doesn&apost work as expected and reiterate that the colors should be applied to the text as the user types it.
Things begin to derail:
It looks reasonable, but it doesn&apost work:
I spent a good half an hour trying to get ChatGPT to fix its code, but things just kept getting weirder and weirder. I let it know how I felt before I closed the chat:
In reality, though, I was impressed. Like I&aposd found for generating text, ChatGPT struggles to create good code en masse. But it captured enough of what I asked for that it inspired a new plan of attack:
Operate on smaller chunks.
A Productive Approach
Up until now, I had interacted with ChatGPT as a consumer with no knowledge of programming. For my second attempt, I took a more active role in solving the problem and gave ChatGPT much more specific instructions.
I split up the program into several functions. I already knew how to tie them together, and was curious to see if ChatGPT could fill in the gaps.
This surprised me. Based on my previous conversation, I expected a solution using sys.stdin.read(1)
. It would be incorrect, but consistent with the kind of errors I saw before.
It&aposs also worth noting that ChatGPT&aposs implementation won&apost work on Windows machines. I use a mac, so I left the code as is and continued:
Pretty good! Just needs some color:
Insert comment about f-strings.Next, I asked ChatGPT to write a function to calculate the gross typing speed. Here&aposs what I got:
This calculates raw typing speed by dividing the number of words typed by the time taken. However, this metric ignores the length of words and overlooks symbols such as spaces and punctuation. None of these alter the word count, but they do impact typing speed. To account for this, the gross speed considers any sequence of five characters to be a word.
It&aposs a flaw in ChatGPT&aposs coding, but an easy manual fix:
def typing_speed(text, start_time, end_time):
words = len(text) / 5
minutes = (end_time - start_time) / 60
return words / minutes
I moved on to the next function I had in mind:
This one really stands out to me. The unsolicited addition of printing the typing speed threw me off at first. But then it gave me an idea. I altered my design:
ChatGPT had most of the pieces it needed now. Could it finish the program for me?
The accuracy()
function works, but can be simplified. I can do that manually:
def accuracy(typed_list):
correct = 0
for typed, expected in typed_list:
if typed == expected:
correct += 1
return correct / len(typed_list) * 100
I&aposd also never seen ChatGPT use comments as placeholders like that. That&aposs a nice way to shorten a long code block that references prior chat interactions.
I put it all together and ran it:
Not sure that characterizes gentlemen, but ok.Exactly what I wanted. The whole process took about 10 minutes.
Thoughts On ChatGPT
Take them or leave them.
- ChatGPT struggles to generate long bodies of text coherently. That&aposs not surprising, given that it&aposs optimized for short, chat-style interactions.
- I&aposm skeptical that large language models will ever be able to generate large, complex software automatically. You can, however, guide a model to build something relatively complex.
- ChatGPT codes like an expert beginner. It can help you be productive, but it can&apost be trusted.
- The best use cases for ChatGPT-like models are refactoring, reformatting, and improving small code snippets. I think my experiment also shows that ChatGPT can be useful for quickly prototyping an idea.
- I can&apost help but wonder if large language models will be used to improve translation from natural to machine language, enabling a new paradigm of spoken-word programming.
The era of the programmer is not over. But the landscape is changing.
Want more like this?
One email, every Saturday, with one actionable tip.
Always less than 5 minutes of your time.
February 12, 2023 at 06:30PM
Click here for more details...
=============================
The original post is available in Planet Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================
Post a Comment