Introduction
Code retrieval—the task of finding relevant code snippets based on natural language queries—is essential for modern software development tools like GitHub Copilot. However, developing effective retrieval models requires large-scale, high-quality "dual-modal" datasets consisting of code and corresponding natural language queries. Existing data collection methods often struggle with a "trilemma" of scale, quality, and cost. Docstrings are scalable but often too technical; community sites like Stack Overflow provide high-quality queries but are noisy and hard to scale; and manual annotation is prohibitively expensive. This paper introduces a novel framework to leverage Large Language Models (LLMs) for generating high-quality code retrieval datasets at scale.
Methodology: Context-Aware Generation
The researchers identified that LLMs often fail to accurately describe code when they lack understanding of internal function dependencies or niche third-party APIs. To address this, they proposed a three-part methodology:
- Context Enhancement: The prompt provided to the LLM is enriched with summaries of intra-repository function calls and documentation for less common third-party APIs retrieved via web scraping.
- Topological Sorting: To manage complex dependencies, functions are processed using a topological sort. "Leaf" functions (those with no internal dependencies) are summarized first. These summaries then serve as context for the functions that call them, allowing meaning to propagate through the repository's call graph.
- Two-Stage Generation and Filtering: The process involves first generating a technical functional summary, followed by a user-like intent-based query. Finally, an LLM-based "judge" scores the pair, discarding low-quality results.
The Query4Code Dataset
The primary output of this framework is Query4Code (Q4C), a massive dataset containing over 237,000 pairs of natural language queries and Python code snippets derived from 12,300 repositories. Unlike previous synthetic datasets, Q4C focuses on capturing user intent rather than just technical descriptions.
Key Results
The effectiveness of Query4Code was evaluated across five real-world benchmarks: CoSQA, SO-DS, StaQC, CoNaLa, and WebQueryTest. Models pre-trained on Q4C consistently outperformed those trained on the industry-standard CodeSearchNet (CSN). For instance, CodeBERT's performance on the CoSQA benchmark jumped from 56.34 to 59.80 (MRR) when using Q4C.
A critical finding was the impact of repository context on query quality. As shown in the evaluation of different LLMs, providing context consistently led to higher average scores for the generated queries, regardless of the number of internal function calls (call count).
Furthermore, the approach proved highly cost-effective. Generating a pair via LLM costs approximately $0.001 to $0.004, compared to roughly $0.20 for human annotation, while maintaining a high correlation with human quality judgments (Krippendorff’s Alpha = 0.858).
Conclusion
This research demonstrates that LLMs, when provided with appropriate repository-level context, can generate high-quality, scalable datasets for code retrieval. The resulting Query4Code dataset sets a new standard for synthetic data in the field. Future work will focus on extending this methodology to other programming languages and further refining the automated validation processes to minimize hallucinations.
Summary updated