Introduction
Retrieval-Augmented Generation (RAG) has become a cornerstone for enabling Large Language Models (LLMs) to access domain-specific or rapidly evolving information. However, in the domain of code generation (RACG), static retrieval often falls short. When dealing with "long-tail" programming languages or updated libraries, a single retrieval step based on an initial query is frequently insufficient. Human programmers do not work this way; they iteratively search, test, and refine their understanding. EvoR (Evolving Retrieval) bridges this gap by transforming retrieval into a dynamic, iterative process that evolves alongside the code generation task.
Methodology: Synchronous Evolution
The core innovation of EvoR is the concept of synchronous evolution. Unlike traditional RAG, which performs a one-shot retrieval, EvoR operates in a continuous loop that refines both the search queries and the available knowledge base (the "Knowledge Soup") based on real-time execution feedback.
The pipeline consists of three primary components:
- Query Evolution: If a generated code draft fails execution, a "query evolver" LLM analyzes the error and the code to generate more precise retrieval queries for the next round.
- Knowledge Soup Evolution: The system maintains a dynamic knowledge base that integrates real-time web search results, official documentation, and execution feedback. Crucially, successful code snippets are added back into the soup as verified examples.
- The Iterative Loop: This process repeats for up to 30 iterations or until a termination condition is met, allowing the model to "learn" the nuances of a new library or language on-the-fly.
Experimental Setup
To evaluate EvoR's performance in challenging, unfamiliar scenarios, the authors introduced EVOR-BENCH. This benchmark includes:
- Scipy-M & Tensorflow-M: Modified Python libraries with altered API names to simulate updates and prevent data leakage from the LLM's pre-training.
- Ring & Pony: Two rare programming languages with limited representation in standard training sets.
Results and Discussion
EvoR demonstrates significant performance improvements over existing state-of-the-art methods. By treating execution feedback as a primary knowledge source, the system effectively filters noise and converges on correct solutions.
Key findings include:
- Superior Accuracy: EvoR achieved two to four times the execution accuracy of baselines like Reflexion and DocPrompting. On the Pony dataset with ChatGPT, performance jumped from 2.7% (DocPrompting) to 13.5% (EvoR).
- Model Agnosticism: The framework provides consistent gains across different base models, including ChatGPT and CodeLlama. For instance, CodeLlama's average performance rose from 16.0% with DocPrompting to 32.2% with EvoR.
- Synergy with Existing Agents: EvoR is a flexible pipeline that can be integrated with other agents. Combining EvoR with Reflexion yielded the highest average performance (37.9% for ChatGPT).
- Token Efficiency: While iterative, the system remains effective within reasonable token budgets, typically hitting a performance plateau between 4k and 16k tokens, avoiding the "lost in the middle" phenomenon.
Conclusion
EvoR represents a shift from static to dynamic retrieval in code generation. By mimicking the iterative discovery process of human developers, it enables LLMs to tackle complex tasks in unfamiliar environments. While the iterative nature introduces higher latency and cost compared to one-shot methods, the massive gains in accuracy make it a powerful tool for high-stakes or specialized coding tasks. Future work will focus on optimizing the "Knowledge Soup" to minimize retrieval noise and improve context window efficiency.
Summary updated