Adventures with ChatGPT
Or, how I learned to stop worrying and just teach ChatGPT to code with xinjs…
I've been slow to jump on the ChatGPT bandwagon in large part because a lot of the code I write is very hard and somewhat novel, and ChatGPT isn't really good at that kind of thing. What it's really good at is grokking lots of examples, including documentation, and then adapting it to a specific request, often with mistakes.
E.g. I have gotten various LLMs to write me code to do something very mundane, such as traverse all the documents in a directory and pass them through a function in Node.js. This isn't hard to do, but it's tedious and time-consuming, and in the end, the code I would have written won't be markedly superior to something any decent coder would write.
ChatGPT (et al.) excels at this kind of task. Think of it as a pretty bright intern who will read any amount of source code and documentation and delve into StackOverflow to figure out how to solve a problem, but won't come up with anything new and won't actually test their code at all before handing it to you.
Now, I'm sure there are workflows which you can fairly easily assemble that would get the resulting code, try to wrap it in tests, try to run it, and so forth, and then iterate until they get through the flow without exploding.
OK, so I have an intern!
Yesterday I decided to play around with ChatGPT because it had been a minute.
The first thing I tried to do was get it to help me improve the TypeScript declaration of the xin proxy in xinjs by explaining what it was intended to do, and it seems to me that ChatGPT did a great job there, although the code it produced was a toy, and I need to spend some time figuring out if I can apply the strategy to the actual code.
In any event, a lot quicker and more effective than the time I've spent trying to spelunk the TypeScript documentation, miscellaneous blogs, or StackOverflow.
So, I got curious.
One of the suggested prompts was "make me a personal website" or something along those lines. So I tried it and it was… fine.
Then I decided to explain the way xinjs's elements proxy works, and asked it to write the code using elements. Here's what I told ChatGPT:
I'm using a library that provides a proxy named
elements. Its properties are element factories, soelements.div()produces an HTMLDivElement.The factories will take their arguments as children (strings are treated as text nodes) or bags of properties (e.g.
{class: 'foo bar', title: 'baz'}will add those attributes.E.g. this Javascript
import { elements } from 'xinjs' const { label, span, input } = elements
document.body.append(label(span('Date'), input({ type: 'date' })))Would add the following to document.body
<label> <span>Date</span> <input type="date"> </label>
Could you rewrite the HTML code using the elements proxy?
And ChatGPT immediately produced this:
import { elements } from 'xinjs';
// Destructure the element factories from the proxy
const { header, nav, h1, section, a, div, p, ul, li, img, footer } = elements;</p>
// Create the HTML structure using element factories
const pageContent = div({ class: 'container' }, [
section({ id: 'bio', class: 'bio' }, [
h2('About Me'),
p('I am a software developer with over thirty years of experience. My expertise spans full-stack development, with a career-long passion for usability and creating intuitive user experiences.')
]),
section({ id: 'portfolio', class: 'portfolio' }, [
h2('Portfolio'),
h3('Technical Projects'),
p('Here are some examples of my technical work:'),
ul([
li(a({ href: '#' }, 'Project 1: Description')),
li(a({ href: '#' }, 'Project 2: Description')),
li(a({ href: '#' }, 'Project 3: Description'))
]),
h3('Artistic Projects'),
p('Some of my artistic work:'),
img({ src: 'example-artwork.jpg', alt: 'Example Artwork' })
]),
...
I should note here that there's a major error in this fragment I didn't pick until I started proof-reading this post, weeks after starting it. Code from LLMs is rife with idiotic errors that often aren't immediately apparent.
I got so excited I tried explaining how xinjs's Component class works to see if I could get it to produce web-components the xinjs way. This took a few attempts, and one thing I learned from this is that ChatGPT seems better at learning from simple, clear examples than more complex examples.
I started out by pointing it at an example in GitHub and asking it to build the components along the same lines. This was a disaster, especially in that it looked sorta, kinda right but wouldn't have worked at all.
I spent a bunch of time refining my prompts, and the most helpful prompts provided focused examples, e.g. I used this prompt after the third or fourth attempt to explain how elementCreator was a static method of Component that did x and y, and it produced incorrect code with incorrect explanations of what it was doing.
No. Here's a simple example that should help.export class MyThing extends Component { content = () => div('my thing is here') } export const myThing = MyThing.elementCreator({tag: 'my-thing'})
After a few prompts like this, ChatGPT provided pretty flawless results. But a lot of the time it really feels like you're just training a conscientious but eager fool to imitate examples. It's kind of like how you wished regexp worked, with the understand that, just as with regexp, you really want to verify that it didn't blow up in some subtle but exciting way.
Aside: "artificial intelligence"? it's not even artificial stupidity…
If you're just dealing with text, it can take a while using ChatGPT to realize that ChatGPT isn't "intelligent" at all. It is predictive text completion with an insane knowledge base, but it simply doesn't understand anything at all.
But if you start working with image generation, it's immediately obvious that (1) the image generator doesn't understand what it's being asked to do, and (2) ChatGPT doesn't understand the image it's been given.
If you look at this example (and this is literally its response to my initial request) it does a great job of transforming the prompt into a concept and then generating a prompt to produce that concept. The text portion of the pipeline is fabulous, and it really feels like it understands. The fact the text engine is not even stupid doesn't become immediately apparent.
But the image is just horribly wrong in so many ways, and as I iterated on the prompt trying to improve the results it just got worse and worse. E.g. I never even suggested inserting a React Logo but it started putting them everywhere. (Like a lot of programming tools out there, ChatGPT just freaking loves React.)
So, the image generator just kind of free associates on the input text and produces something but it doesn't know that, for example, you can't push something up a hill if it's already at the top of the hill. It doesn't know that the stuff on the hill and the stuff on the boulder need to be different things and just puts anything you ask for in both places or neither.