little ai can’t write python code

Two posts back I wrote about installing GPT4All on my M1 MacBook Pro. At first blush I was impressed, as it seemed to answer the softball questions I asked it correctly and with style. Then I got a little more serious and asked it this question:

write a python program that reads a directoryand prints out everything it finds

To which GPT4All Falcon gave this answer:

Here's an example Python program that uses the`os` module to read a directory and print outeverything it finds:
import os# Set the directory pathdir_path = '/path/to/directory'# Check if the directory existsif not os.path.exists(dir_path):print("Directory does not exist")return# Iterate through all files in the directoryfor file in os.listdir(dir_path):# Check if the file is a directoryif os.path.isdir(os.path.join(dir_path, file)):# If it's a directory, recursively call this functionprint("Directory:", file)print("Files:", os.listdir(os.path.join(dir_path, file)))# Check if the directory is emptyif not os.path.exists(dir_path):print("Directory does not exist")
This program uses the `os.listdir` functionto get a list of all files in the directoryand the `os.path.join` function to constructthe full path for each file.It then calls itself recursively on anydirectories it finds, printing out thedirectory name and its contents.

Let’s examine the Python code. There is a blatant error on line 9. Python does not allow the use of a return outside of a proceedure. If you need to exit a script due to some condition, then you use Python’s exit keyword.

The second problem are lines 19-21. They don’t do anything. The message on line 21 is the same as the message on line 8. If the block of code in lines 19-21 is to determine if the directory is empty then it should say that.

Let’s clean up the code enough to actually work.

#!/usr/bin/env python3import os# Set the directory pathdir_path = '.'# Check if the directory existsif not os.path.exists(dir_path):print("Directory does not exist")exit(1)# Iterate through all files in the directoryfor file in os.listdir(dir_path):# Check if the file is a directoryif os.path.isdir(os.path.join(dir_path, file)):# If it's a directory, recursively call this functionprint("Directory:", file)print("Files:", os.listdir(os.path.join(dir_path, file)))

Now let’s test it. I’ve created some dummy directories and files. Here’s what the utility tree shows.

├── foo│   ├── bar│   │   ├── five│   │   ├── seven│   │   └── six│   ├── four│   ├── one│   ├── three│   └── two├── foo2

Now let’s run our corrected AI code and look at the output.

Directory: fooFiles: ['three', 'one', 'two', 'bar', 'four']Directory: foo2Files: []

It’s unsurprisingly disappointing. There’s a comment on line 15 (16 in the corrected code) that says to “recursively call this function” if a directory is detected. It doesn’t in part because there are no functions, and because the test for a directory doesn’t do anything but print out if it’s a directory.

The code looks like during its learning phase it swept up someone’s Stack Overflow question, but didn’t know enough to look at any answers, assuming there were any. Whatever ideas I might have had to incorporate the Falcon model into anything have been pretty much squelched. I’ll install some others and give them a spin.

Links

ChatGPT Isn’t Coming for Your Coding Job — Wired — https://www.wired.com/story/chatgpt-coding-software-crisis/

One thought on “little ai can’t write python code

  1. The basic (pun intended) problem with AI is not that it’s incapable of replacing human beings, it’s that people refuse to admit its limitations. Prepare for an even more imperfect future.

    Liked by 1 person

Comments are closed.