Skip to main content
QUICK REVIEW

[Paper Review] Generating Code with the Help of Retrieved Template Functions and Stack Overflow Answers

Dawn Drain, Changran Hu|arXiv (Cornell University)|Apr 12, 2021
Software Engineering ResearchComputer Science14 references6 citations
TL;DR

This paper proposes a retrieval-augmented code generation framework that enhances code autocompletion by retrieving relevant template functions from CodeSearchNet and intent-snippet pairs from CoNaLa (Stack Overflow). It uses a fusion representation model for retrieval and a sequence-to-sequence generator conditioned on both the function signature and retrieved references, achieving a 44% BLEU score improvement when retrieving template functions via Maximal Marginal Relevance (MMR) reranking.

ABSTRACT

We approach the important challenge of code autocompletion as an open-domain task, in which a sequence-to-sequence code generator model is enhanced with the ability to attend to reference code snippets supplied by a semantic code search engine. In this work, we present a novel framework to precisely retrieve template functions as well as intent-snippet pairs and effectively train such a retrieval-guided code generator. To demonstrate the effectiveness of our model designs, we perform extensive experiments with CodeSearchNet which contains template functions and CoNaLa which contains Stack Overflow intent-snippet pairs. We also investigate different retrieval models, including Elasticsearch, DPR, and our fusion representation search model, which currently holds the number one spot on the CodeSearchNet leaderboard. We observe improvements by leveraging multiple database elements and further gain from retrieving diverse data points by using Maximal Marginal Relevance. Overall, we see a 4% improvement to cross-entropy loss, a 15% improvement to edit distance, and a 44% improvement to BLEU score when retrieving template functions. We see subtler improvements of 2%, 11%, and 6% respectively when retrieving Stack Overflow intent-snippet pairs. We also create a novel Stack Overflow-Function Alignment dataset, which consists of 150K tuples of functions and Stack Overflow intent-snippet pairs that are of help in writing the associated function, of which 1.7K are manually curated.

Motivation & Objective

  • To address the limitation of closed-book code generation by enabling models to access external code references.
  • To improve code autocompletion quality by retrieving relevant template functions and Stack Overflow answers as context.
  • To design a flexible, retrieval-guided code generation framework that decouples retrieval and generation for efficiency and scalability.
  • To evaluate the impact of diverse retrieval strategies, including Maximal Marginal Relevance (MMR), on code generation quality.
  • To create and release a novel SOFA dataset of 150K aligned Stack Overflow intent-snippet pairs for future research.

Proposed method

  • The framework uses a neural code search model to retrieve relevant template functions from CodeSearchNet and intent-snippet pairs from CoNaLa based on the function's docstring and signature.
  • Retrieval is performed using multiple models: Elasticsearch, DPR, and a proposed fusion representation model, which achieves state-of-the-art performance on CodeSearchNet.
  • The code generator is a pre-trained sequence-to-sequence transformer conditioned on the function signature, docstring, and retrieved reference snippets.
  • Retrieval results are re-ranked using Maximal Marginal Relevance (MMR) to balance relevance and diversity, with λ = 0.5 to balance relevance and redundancy.
  • The retrieval and generation components are trained separately, enabling modular and efficient training and inference.
  • The system supports both template function retrieval (from GitHub) and intent-snippet retrieval (from CoNaLa), with ablation studies on retrieval quality and diversity.

Experimental results

Research questions

  • RQ1Can retrieving external code references improve code autocompletion beyond the model's internal context?
  • RQ2How does the choice of retrieval model (e.g., Elasticsearch, DPR, fusion representation) affect code generation quality?
  • RQ3Does re-ranking retrieved references using Maximal Marginal Relevance (MMR) lead to better code generation than greedy retrieval?
  • RQ4How do different databases—CodeSearchNet (template functions) versus CoNaLa (Stack Overflow intent-snippet pairs)—compare in improving code generation?
  • RQ5To what extent does retrieving diverse, non-redundant references improve code generation metrics like BLEU, edit distance, and cross-entropy loss?

Key findings

  • Retrieving template functions from CodeSearchNet using the fusion representation model and MMR re-ranking led to a 44% improvement in BLEU-4 score, a 15% reduction in edit distance, and a 4% decrease in cross-entropy loss.
  • Using MMR for re-ranking retrieved templates doubled the benefit of retrieving multiple templates, significantly improving edit distance and BLEU scores compared to greedy retrieval.
  • When retrieving Stack Overflow intent-snippet pairs from CoNaLa, the model achieved a 6% improvement in BLEU-4, 11% in edit distance, and 2% in cross-entropy loss, though gains were more modest than with template functions.
  • The fusion representation model outperformed both Elasticsearch and DPR on CodeSearchNet, securing the top position on the CodeSearchNet leaderboard.
  • The SOFA dataset, containing 150K manually curated function-Stack Overflow pairings, was created and released to support future research in retrieval-augmented code generation.
  • Excluding identical retrieved functions during training prevented degenerate behavior and improved generalization, highlighting the importance of retrieval filtering.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.