I wanted to see if AI could code me a complex app. Not a crappy...

@Dan_Jeffries1
Daniel Jeffries@Dan_Jeffries1
19 views Apr 14, 2024 ~18 min read
Advertisement
1
I wanted to see if AI could code me a complex app.

Not a crappy little one-off script. A real program.

Just one little problem: I mostly suck at coding.

So can AI make magic for someone like me?

Yeah. But...it's complicated.

Here's what I learned along the way.

1/
Media image
2
I wanted to understand the state of the art by giving myself a hard problem that was also really useful to me. 

But what is the state of the art? 

That's not clear and the only way to make it clear, just like anything in life, is to do it.

To start off I have some real disadvantages doing this project.  Number one, as I said, I'm just not a good coder.  I've studied a smattering of C/C++/Java/Bash and Python over the years.  Coding always felt very brittle, arcane and frustrating to me after I got past early tutorials.  I just can't think in code.

But I've also got some strong advantages too, especially over someone with no technical expertise whatsoever.

I've run tech teams as a CIO, been a lead systems architect on big projects, and worked on super complex infrastructure setups as an IT consultant for over a decade.  In short, I know how programs work and scale and run.

I'm also a writer and a clear thinker.  That means I know how to ask for what I want in crisp, precise language. 

Prompt engineering really ain't nothing but good writing and that's a big advantage in the age of asking machines to do things for us. 

In a way, taking to machines is critical thinking at scale.

What I discovered quickly is that if you're coming to coding from painting houses, it's going to be too much of a leap.  But if you're an engineering manager who coded in a former life or a systems engineer like I was when I was younger than it's possible.

What is happening?  Why?  How can I get it to do what I want?  How can I explain it better? What's missing?

The more of this kind of internal thinking you can do the better off your are in the coming age of AI.

2/
Media image
3
Now did AI do all the work for me.

Oh my God, no.

If you believe the Twitter hyper masters we're super close to having superhuman AI come up with complex logic and brand new algorithms in a flash. 

The great and powerful Oz of LLMs will come up with the architecture, write the code, test it, fix bugs, write the documentation, deploy it and monitor it for you!

All we have to do is show up and ask like we're Scotty in Star Trek and it is done.

Like magic.

Uh, nope. Not yet, at least.

youtu.be/hShY6xZWVGE

3/
4
Coding with AI is equal parts "holy shit, it fucking works" and utterly frustrating drudgery/pain and back again. 

It's still mostly the second part. 

It's a wave of emotional highs and lows. 

Don't get me wrong.  It's incredible that I can get a program ex nihilio, like the old testament God magicking up the Universe from the Word, but it's still not easy. 

It's fucking work.  Hard work.

Skilled coders are not going anywhere.

Anyone who tells you different is selling something.

If anything skilled coders who know how to ask for what they want clearly will be in even more demand as AI does more of the coding for us.  Systems engineers who understand good architecture and security will be needed more than ever as more insecure code and bad coding practices get pushed to production.  Coding managers who've left coding behind and "can't code" will soon be the folks coding again instead of waiting for their coders at every step.

Did I get a complex program written?

Yup.

I sure did.  And I'm going to walk you through parts of it and what I learned along the way.

But getting there was not easy.

/ 4
Media image
5
I started off with Sublime editor.

Sublime is basically a glorified text editor.  A very nice text editor but a text editor nevertheless.

And I quickly learned that this was a very bad choice for AI coding.

Sublime is fine if you know exactly what you're doing in code but I don't know exactly what I'm doing so it was exactly the wrong choice for me.

It meant a lot of cutting and pasting into GPT chats to get answers.  It was slow, boring and frustrating.

Okay, now I am getting this error, what does it mean?

Cut and paste new code and try again.

5/
Media image
6
I needed something better than a text editor.  My COO, a long time coder, saved me a lot of trouble by letting me know I should switch to Cursor AI, a fork of VS Code that uses AI natively throughout the app.

@cursor_ai is brilliant.

6/
Media image
7
It also has incredibly important features like "use with code base" when you ask a question, which helpfully feeds the codebase in as a reference.

7/
Media image
8
It even has a life saving feature where you can press "Continue" when GPT runs out of space to finish its answer.  This happens because GPT has a limited token response output so it sometimes fails to finish outputting the updated code. The button basically instructs GPT to finish right where it left off and it works every time.

8/
Media image
9
When it came to errors, I quickly learned that @perplexity_ai was better at troubleshooting errors, because it can search the web. That means it can find much more timely answers. 

The Pro version is basically GPT4 or Claude Opus with RAG and a web index.  Plain old GPT or Claude are hamstrung by having outdated knowledge of libraries in Python or even its own OpenAI API calls.  They are going to make mistakes because they just don't have up to date info and actively maintained coding libraries are changing all the time.

So the answer to getting answers it to use a combo of Perplexity and going to the API documentation examples yourself to give the model context on how to use the updated library. 

In other words, code by example.  Same as it ever was.

This works.

Mostly.

9/
Media image
10
When you start out, the AI is going to give you a big single file program.  It will inevitably be called main.py.

As you add features it's going to get really, really unwieldy and unmaintainable.

If you don't ask for modularity right away you won't get it.

Even worse, GPT will really struggle as main.py gets bigger and bigger and bigger.  It chokes on it as it struggles to fit it into its context window.

This is a good moment to point out that AI can and will happily saddle you with atrocious coding practices. If you have no history in coding, systems, IT, or software you will have no idea that what you just created is just plain wrong.

It's insecure, unmaintainable and filled with things you never want to see in production code.  In short, whatever AI you use today is going to use every single bad programming habit known to man.

It will write quick and dirty code that is insecure, doesn't scale and will cause you major headaches later on.

10/
11
Here's just a few of the things it did with my program:

1) Set the OpenAI API key right in the file like so:  OPENAI_API_KEY=abc123

This is absolutely terrible practice because it's super easy to leak that key in hundreds of ways. Much better to put it in an environmental variable (not perfect but better) or in a secrets storage system like HashiCorp Vault (much better).

2) Created a list of outputs in memory and then flushed them all to a file at the end.

My script scrapes competitors or potential partner web pages and summarizes what they do and gets their pricing information. 

Keeping them in memory is fine for a few URLs but if you have 1,000, or 10,000 or more, it's going to balloon memory and if the program crashes you lose all your work.  Much better to write to the file as you go.

3) No state.

Since the original script did not keep track of its state, that means it has to run completely end to end, processing every URL. 

If you have to start over because of a crash all your work is lost and you start over from scratch.

4) No error handling.

5) Crappy modularity.

When I asked it to start breaking out functions into new files, it did it in a half-assed way with no real logic and left most of the functions in main.py

6) Made the app single threaded. It spidered each URL one by one, which is slow and takes a long time.

7) Made calls to the OpenAI API synchronously, which is super slow because the calls have to happen and then you have to wait for an answer.

Much better to make the calls asynchronously because your program can then make calls to the API and then does other things while waiting for those answers.

8) No rate limiting for the OpenAI API.

OpenAI and most other APIs out there have rate limits to ensure that you don't just bash away at it. 

OpenAI's rate limits are really low because LLM inference is expensive so you have to deal with it in code or you just get error after error.  If your program is not equipped to deal with the rate limits you could quickly find your web scraper fills your output file with "error, error, error" instead of information about your competitors and partners.

The list goes on and on. 

And if you don't understand these things from your own experience you won't even know to look for them or to ask the AI to fix it.

Want to know the best part?

After I got the program working I wanted to refactor it to make it even more modular and easier to maintain.  I wanted to create classes, split out functions into different files, etc.

This was not fun.

During this process, instead of porting over the good security and scaling practices I painstakingly built into the code through iterating with the AI, it brought back all the same fucking problems again!

It undid everything and reverted back to the old way of doing it.

Writing to the output file at every step?  Nope.  Back to storing it in memory.

Posting my API key right in the settings file?  You got it.  No more environmental variable for you.

I had to re-prompt the system to re-add all these features and go through the same painful debugging steps all over again.

11/
Media image
12
Even when it is given examples of the correct call it often screws it up still.

Take a look at this repeated typo/hallucination that GPT-4 makes again and again and again.  Here is the incorrect line:

client.chat_completions.create()

But the correct call is this:

client.chat.completions.create()

Can you spot the error? It's putting in an underscore for a period.  My best guess is that it comes from the fact that a lot of the code examples on forums and blogs do something like this:

def get_gpt_response(messages):
chat_completion = client.chat.completions.create(
model="gpt-3.5-turbo-1106",
messages=messages,
functions = functions,
function_call="auto",
)
return chat_completion

It learns that chat_completion is just as likely a set of words to predict as chat.completions and it alternates between them.

This kind of stuff is absolutely maddening to track down!  I can't see someone with zero understanding of code fixing this kind of bug. They just wouldn't know where to start.  It's like finding a needle in a haystack in a big file.

12/
Media image
13
I ended up having to do a lot of old school troubleshooting, aka pasting error files into google/Perplexity and reading through forum posts to try to understand what the hell was going wrong in the scripts after the models just completely failed to fix a bug after a dozen iterations.

Eventually you just have to understand the problems yourself and find the solution.

I'm very used to that old grind. As a former sys admin I lived on the net hunting down answers when things went wrong.  It's one of the parts of my old job that I hated more than anything else.  It hasn't gone away.

As of today, AI can't magically fix everything. Remember that Devin, the AI software coding assistant, is considered state of the art because it can fix 13.86% of bugs on its own.  That is not a lot.  It's awesome but it is not a lot.

It means you have to understand your code well enough to fix the other 86.14% of bugs.

The biggest problem with coding with AI at this point is really context and by context I mean human context here.  As humans, we reference a file we just saw or an idea we just heard about all the time as we work or complete any complex task.  GPT is like a big dumb child that completely forgets about the things it just saw and heard.  So you have to loop and loop and loop through the same freaking mistakes multiple times because it reintroduces problems that it solved in the original code.

The second biggest problem is context windows, which are nothing but a glass ceiling on how many tokens you can give it before it barfs.  The biggest problem with context windows is they're just too short for doing anything useful.  To understand context the models are going to need to remember a lot more and be able to reference that knowledge easily.  They just can't do it right now no matter how many RAG tricks you throw at it.

So that is the shit part of it.

What did it get right?

A lot.

13/
Media image
14
Take something like Playwright. It's a modern framework written by Google to orchestrate a browser headlessly to run tests on websites.

I have zero idea how to use Playwright or where to even begin.

Yet out of the primordial soup of the LLM's weights I got this code below and it mostly just worked out of the box.

This is one of those awe inspiring moments where you can hear the choirs of angels sing.

14/
Media image
15
Next I faced other unexpected problems.  Originally the code was written to just shove all the content sucked from a website into GPT's gullet but since it was stuffed with images and tons of HTML code it just quickly overflow the context window.

I needed something to read the HTML, grab the text out of it and then chunk it up to fit the context window.

Enter Beautiful Soup and @langchain , two packages I'm familiar as a concept with but also have no idea how to use.

Here's the content processor class in the file called content_processor.py that the program calls in other scripts.

Again, another choirs of angel moment.  That code mostly just worked right away and again I have zero idea how to work with either package.

That's amazing stuff.

In the end I had a fully functional program that I am adding to all the time.

15/
Media image
16
I decided to create a new module today that can score companies based on heuristics I developed.  So I will have a new module that will add a few new lines to my CSV.  One is a general vibe based assessment from the model and the other is a hard score based on my rules.

I suddenly got access to Gemini 1.5 Multimodal with its 1M token context window and its incredibly promising.  Many of the problems I faced boil down to context window limits. 

RAG just doesn't solve it. 

You still run up against context windows even if it can search a vector database.

With Gemini I was able to paste in all my code and have it outline a plan to add heuristics and scoring and create the new files step by step.  It understood ALL of my code. 

That's a massive improvement just since I began writing this article.

16/
Media image
17
So is AI about to put everyone out of a job?

Hell no it's not.

Again, if anything, top notch coders who can think in code and define problems clearly are going to be more in demand.

If you can think about thinking you've got a tremendous advantage in the coming world.

17/
Media image
18
All this reminds me a lot of past developments in tech. 

In the early days, it was hard to make good looking websites.  You had to write all the HTML yourself and learn to use Photoshop and other tools and stack it all together. 

Then along comes the LAMP stack and suddenly it's a lot easier to create higher quality websites.  You still need skills but the barrier to entry dropped. 

Then Wordpress comes along, built on top of the LAMP stack.  Even easier. 

After that you get plugins to Wordpress like Divi that let me drag and drop together a beautiful website and pull from extensive templates made by awesome designers.

Now we have @webflow for amazing REACT sites.

Each time we move up the stack, abstracting away a lower level of problems, we enable more people to make a great website.  Each new abstraction makes it easier to do something complex.

The same thing happens with every new technology.  It brings more people into the fold.

We benefit tremendously from the breakthroughs of the people who came before us.  Each new tool makes it easier to do bigger and better things. 

We stand on the shoulders of giants every day.

We can make a mud hut without tools but once we get hammers and nails and standard wooden board sizes we can build big houses and grand buildings.  Once we get steel girders and cement we can stack skyscrapers into the clouds.

Everyone can make a cool website now.

The big web development teams haven't gone away.  They've multiplied.  The big websites have only gotten more complicated and powerful in what they can do.  Amazon is not making their website with Wordpress and drag and drop editors.

We always get new capabilities at the top and the bottom of the chain.

People who embrace AI and work closely with it will displace people who refuse to work with it.

We're about to have a lot more programs.  Right now if you can dream up a program that doesn't exist, it's still hard to create with AI but it can be done.

It will get easier and easier to whip up good programs as the years go by.  Maybe there's no commercial reason for anyone to build the program you want because it doesn't have the potential of a massive market and big sales, but it might be just the program you need.

Now you can have it.  Before AI you couldn't have it because nobody would build it.

We're entering an area of personalized coding. 

Dream up an app and build it for yourself, even if nobody else needs it. It doesn't matter if nobody else wants it because the barrier to entry will get so much lower.

People who can critically think, plan, strategize, reason and imagine will have a massive advantage in the coming world.  Because I know what I want and I know how to ask for it, I can get a program I want out of AI.

Don't get me wrong.  It's still a pain in the ass.  But this is the worst it will ever be and it will only get better from here.

If someone is out there telling you not to study computer science or IT or system engineering don't listen to a word they say.  The more you know about great architecture and scalability and security the better off you'll be because you'll know what to ask the AI to do.  Even better, you'll know how to judge the output.  You'll look at it and know if it's right or wrong better than someone with no skills.

Traditional artists sometimes worry that image generating programs make it too easy for anyone to create art and that they'll all be out of a job.  But the best results from AI come from people who already have great art skills. A great artist gets more from a paintbrush and watercolors than someone who's a dabbler like me. Great artists get more out of AI too.

AI is just another tool.  I'll say that again because people seem to not understand it. AI is just another tool.

It's not a God.  It's not sentient.  It's not all knowing and all powerful.  It's a tool.

Skilled people always get more out of tools.

I can do wonders with Photoshop and decent work with Figma and Webflow.  But a great designer can work those programs like magic.

And a great programmer will know how to get the best out of coding AI.

Keep studying.  Don't listen to fools.  Keep learning.  Keep updating your skills.  Adapt.  Grow learn.  Always. That's the way forward in the future where AI is your constant companion and friend.

I'm already dreaming up so many new programs that would never get built. I can build them now.

The only limits are my imagination.

And my patience.

18/
Media image
19
You can check out the code for the project on my GitHub here:

github.com/the-laughing-m…

Thanks for reading.

19/
Actions
What You Can Do
  • Export as PDF or Markdown
  • Batch Export to Notion
  • Bookmark & Highlight
  • Screenshot Tweet
Create Free Account

Includes 7-day Premium trial

Advertisement