Teaching Code LLMs to Use Autocompletion Tools in Repository-Level Code Generation

Chong Wang, Jian Zhang, Yebo Feng, Tianlin Li, Weisong Sun, Yang Liu, Xin Peng

Abstract

Large Language Models (LLMs) have demonstrated remarkable capabilities in generating standalone code snippets. However, they frequently fail in repository-level generation tasks where code depends on complex, multi-file structures. This paper introduces TOOLGEN, a framework that enables LLMs to recognize when they require external information and invoke autocompletion tools (e.g., Jedi) during the generation process. By combining probabilistic LLM outputs with deterministic static analysis, TOOLGEN reduces dependency errors and enhances the functional correctness of generated code.

Introduction: The Repository-Level Challenge

In professional software development, code is rarely isolated. It relies on a vast web of user-defined classes, methods, and variables spread across different files. While LLMs like CodeLlama excel at general programming tasks, they often "hallucinate" repository-specific members, leading to AttributeError or "no-member" errors when they attempt to guess names that do not exist in the specific codebase.

Comparison between vanilla CodeLlama prediction (leading to a no-member error) and Jedi completions used by TOOLGEN.

Traditional Integrated Development Environments (IDEs) mitigate this using static analysis tools. TOOLGEN bridges this gap by allowing the LLM to "hand off" control to these tools at critical junctures during the decoding process.

Methodology: The TOOLGEN Framework

TOOLGEN operates through a two-phase approach: offline fine-tuning and online tool-integrated generation.

1. Offline: Trigger Insertion and Fine-tuning

The researchers identified positions in Python code where autocompletion is typically required (e.g., following a . operator). They augmented a large dataset of Python functions with a special <COMP> token at these locations. Models such as CodeGPT, CodeT5, and CodeLlama were then fine-tuned on this data, teaching them to predict the <COMP> token when repository-level context is needed.

2. Online: Tool-Integrated Generation

During inference, if the model predicts <COMP>, it pauses and queries an external tool (Jedi). Jedi provides a list of valid completions based on the actual repository state. To integrate these suggestions, TOOLGEN employs Constraint Greedy Search. It constructs a prefix tree (trie) of all valid suggestions and restricts the LLM's next-token probabilities to those within the trie. This ensures the output is both contextually relevant and programmatically valid.

Architectural overview of TOOLGEN applied to (a) Decoder-only and (b) Encoder-Decoder models.

Experimental Setup and Results

The framework was evaluated on a newly curated dataset of over 12,000 Python functions from 671 real-world repositories, as well as the CoderEval benchmark which tests functional correctness through unit tests.

Key Performance Metrics

  • Dependency Coverage: TOOLGEN increased the coverage of repository-level dependencies by 31.4% to 39.1% across different models.
  • Static Validity: The rate of code passing a linter without dependency errors improved by 44.9% to 57.7%.
  • Functional Correctness: On CoderEval, TOOLGEN-llama showed a 25% improvement in Pass@1 compared to the vanilla baseline.

Discussion and Future Work

TOOLGEN demonstrates that "probabilistic" generation and "deterministic" static analysis are complementary. By invoking tools only when necessary, it maintains efficiency while drastically reducing errors.

However, limitations remain. The system's effectiveness is bound by the capabilities of the underlying static analysis tool; in dynamically typed Python code, type inference can sometimes fail. Future research may explore the use of beam search to further refine output quality and extending the framework to closed-source models and additional programming languages.

Summary updated

BibTeX
@article{wang2024teaching,
  author = {Chong Wang and Jian Zhang and Yebo Feng and Tianlin Li and Weisong Sun and Yang Liu and Xin Peng},
  title = {{Teaching Code LLMs to Use Autocompletion Tools in Repository-Level Code Generation}},
  journal = {ACM Transactions on Software Engineering and Methodology},
  year = {2024},
  url = {https://openreview.net/forum?id=1q4JsIXKfJ},
}
Figure