Chunking and Diff based LLM Thinking
(Feynman-esque Monologue Begins)
Alright, alright, so we've got this... thing, this language model, right? It's like a parrot that's read every book ever written, but it doesn't really understand. It's good at mimicking, at pattern matching, but it's not a programmer. It's a statistical machine. That's our starting point. We can't forget that.
We want to tell it to change some code. Easy for us, we see the structure, the logic, the meaning. But for the parrot? It's just a stream of... tokens, they call them. Like letters, but bigger chunks. It doesn't see lines, it doesn't see functions the way we do.
So, the obvious way, telling it "change line 35," that's doomed. It'll get it wrong. It's like asking the parrot to find the 35th feather on its left wing. It might look like it knows, but it's just guessing. Diffs? Same problem, only fancier. It's all about positions, and the parrot doesn't have a good sense of position. It's relative, not absolute.
(Taps on a whiteboard, imaginary or real)
Okay, so what does the parrot do well? It's good at completing things. You give it a start, it gives you an end. Like a story. You give it "Once upon a time...", it'll spin you a yarn. It's good at patterns.
So, let's think about code in terms of patterns, not lines. What's the most obvious pattern in code? Functions! Classes! Blocks! Things with names. Those are like... landmarks. The parrot can see those, relatively speaking.
(Starts sketching a rough diagram of a code file, with boxes for functions)
We could say, "Hey parrot, find the function called do_the_thing
." That's something it can probably do. It's a pattern it's seen a million times. Then, we could say, "Give me that whole function back, but changed." Like we're asking it to rewrite a paragraph in a story, not a single word.
That's the chunking idea, right? We break the code into chunks, like chapters in a book. We don't ask it to edit sentences, we ask it to rewrite chapters. Less chance of messing up the little details.
But... what if it gets the chunk wrong? What if it changes the wrong function? We need a way to... check its work. Like a teacher grading a paper.
(Paces back and forth)
That's where the iteration comes in. We try it. We run the code. Does it work? Does it break? If it breaks, we show the parrot the error. "Hey, you messed up! Here's the mistake, fix it!" Like showing the parrot a misspelled word. It's surprisingly good at fixing its own mistakes, if you show it the mistake clearly.
But showing it the whole file again and again, that's wasteful. That's like making the parrot reread the entire book every time it misspells a word. We only need to show it the relevant part, the broken chunk.
(Snaps fingers)
So, here's the picture:
- Chunks: We think of the code in chunks, not lines. Functions, classes, logical blocks.
- Landmark: We tell the parrot to find the landmark (the chunk name).
- Rewrite: We ask it to rewrite the whole chunk. Not a diff, not a line, the whole thing.
- Check: We run the code. Does it work?
- Feedback: If it breaks, we show the parrot the error and the broken chunk. We ask it to fix it.
- Repeat: We keep doing this until it works. Like teaching a kid to ride a bike. We give them a push, they wobble, we correct them, they try again.
- Embrace the LLM's Nature: It's a statistical pattern-matcher, not a precise code manipulator.
- Chunking: Break the problem into manageable, logically independent units.
- Landmarks: Use names (functions, classes) as anchors for changes.
- Complete Rewrites: Ask for complete modified chunks, not diffs or line edits.
- Iterative Refinement: Use a feedback loop with error messages to guide the LLM.
- Script as Conductor: Let the script provides structural correctness (finding and extracting chunks).
(Looks at the imaginary whiteboard, then nods)
This... this feels right. It's not about forcing the parrot to be a programmer. It's about working with its strengths. It's about guiding it, not commanding it. It's about breaking the problem down into small, manageable pieces. It's about... feedback.
The key is the feedback loop. It is a conversation between the script and the model. The script provide precise information on the structure, the LLM provides the text changes.
(Feynman-esque Monologue Ends)
This Feynman-esque thought process highlights the core principles: