Prompt Optimization for LLM Code Generation via Reinforcement Learning
This paper proposes a reinforcement learning framework using Proximal Policy Optimization to iteratively refine prompts for LLM-based code generation via a hybrid action space and test-driven rewards, achieving significant performance improvements on benchmarks like MBPP+, HumanEval+, and APPS across multiple backbone models.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
Imagine you are trying to teach a very talented but slightly confused robot how to write computer code. You give the robot a set of instructions (a "prompt"), and it tries to write a program. Sometimes, the robot gets it right immediately. Often, it makes mistakes, like using the wrong tools or misunderstanding the goal.
This paper is about a new way to teach that robot how to understand your instructions better, not by changing the robot itself, but by getting you (the prompt) to speak more clearly. The authors call this "Prompt Optimization," and they use a clever trick called Reinforcement Learning to do it.
Here is how the system works, broken down into simple concepts:
1. The Problem: The "Confused Robot"
Large Language Models (LLMs) are like the robot. They can write code, but if your instructions are vague, the code will have bugs.
- Old way: You guess a better way to ask the question, try it, and if it fails, you guess again. This is slow and random.
- The paper's idea: Let's teach a "Coach" (an AI agent) to figure out the best way to ask the question, step-by-step, based on how well the robot's code works.
2. The Coach: The Reinforcement Learning Agent
Think of the Coach as a game player. The game is: "Get the robot to write perfect code."
- The Goal: The robot must pass a series of "test cases" (like a math quiz where the robot has to solve specific problems correctly).
- The Loop:
- The Coach looks at the current instruction.
- The Coach decides: "Should I leave the instruction alone? Should I swap a few words? Or should I completely rewrite the sentence to be clearer?"
- The robot tries to write code based on that new instruction.
- The code is tested.
- The Coach gets a score (a reward) based on how many tests the robot passed.
3. The Coach's Toolkit: Three Moves
The Coach has three specific moves to improve the instructions:
- Direct Generation (The "Wait and See"): The Coach decides the current instruction is fine and just asks the robot to try again. Sometimes, the robot just needs a second chance to get lucky with its random thinking.
- Lexical Mutation (The "Word Swap"): Inspired by how nature evolves, the Coach makes small, random changes to the words. Maybe it swaps "list" for "array" or adds a missing comma. It's like shuffling a deck of cards to see if a new combination works better.
- Semantic Rewriting (The "Big Picture"): The Coach asks another AI to completely rephrase the instruction to make the meaning clearer. It's like saying, "Instead of telling the robot to 'fix the car,' tell it to 'replace the spark plugs.'"
4. The Secret Sauce: The "Shaped Reward"
This is the most important part of the paper.
- The Old Way (Binary Reward): In many systems, you only get a point if the robot gets 100% of the tests right. If it gets 99% right, you get zero points. This is like a teacher failing a student who got 99% on a test. The Coach gets no clue on how to improve.
- The New Way (Shaped Reward): The authors give the Coach points for partial progress.
- If the robot passes 0 tests: Big penalty.
- If the robot passes 50% of the tests: You get 0.5 points.
- If the robot passes 100%: You get 1.0 point.
- Why it matters: This tells the Coach, "Hey, you're getting warmer! Keep going in that direction." It turns a "pass or fail" game into a "climb the mountain" game.
5. The Results: Did it Work?
The researchers tested this "Coach" on three different sets of coding challenges (MBPP+, HumanEval+, and APPS) using three different robot models (CodeT5+, CodeLLaMA, and DeepSeek-Coder).
They compared their Coach against:
- Random-Hybrid: A Coach that picks moves randomly (like a monkey throwing darts).
- EPiC & Reflexion: Other smart methods that try to fix prompts but don't use this specific "partial credit" scoring system.
The Outcome:
The PPO Coach (the one with the "Shaped Reward") won every time.
- On the hardest tests, it improved the success rate significantly compared to the random approach.
- For example, with one of the robot models, it jumped from a 31% success rate (Random) to 57% (PPO).
- Even when the robot didn't get a perfect score, the "Soft" score showed that the Coach was consistently guiding the robot closer to the right answer, step by step.
Summary
The paper shows that if you want an AI to write better code, you shouldn't just hope for the best. Instead, you can train a "Coach" to learn how to tweak your instructions. By giving the Coach credit for partial improvements (not just perfect scores), the system learns to navigate the messy process of fixing code much faster and more effectively than previous methods.
In short: It's like teaching a student to solve a puzzle not by waiting for them to get it 100% right to say "Good job," but by saying "Good job, you got three pieces in the right spot, now try moving this one," until the whole picture is complete.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.