MPCODER: Multi-user Personalized Code Generator with Explicit and Implicit Style Representation Learning

Zhenlong Dai, Chang Yao, WenKang Han, Ying Yuan, Zhipeng Gao, Jingyuan Chen

Abstract

Large Language Models (LLMs) have demonstrated great potential for assisting developers in their daily development. However, most research focuses on generating correct code, how to use LLMs to generate personalized code has seldom been investigated. To bridge this gap, we proposed MPCoder (Multi-user Personalized Code Generator) to generate personalized code for multiple users. To better learn coding style features, we utilize explicit coding style residual learning to capture the syntax code style standards and implicit style learning to capture the semantic code style conventions. We train a multi-user style adapter to better differentiate the implicit feature representations of different users through contrastive learning, ultimately enabling personalized code generation for multiple users. We further propose a novel evaluation metric for estimating similarities between codes of different coding styles. The experimental results show the effectiveness of our approach for this novel task.

Introduction

While Large Language Models (LLMs) like CodeLlama have become indispensable tools for modern software development, they typically generate code in a generic, "average" style. In professional environments, however, developers and organizations adhere to specific, often idiosyncratic, coding standards regarding indentation, variable naming, and architectural patterns.

Existing approaches to personalization face two primary challenges: scalability and style capture. Fine-tuning a massive LLM for every individual developer is computationally prohibitive. Furthermore, coding style is multifaceted, comprising both explicit syntax styles (easily defined rules like spacing) and implicit semantic styles (nuanced habits like logic structuring). MPCODER is designed to bridge this gap by enabling a single model to generate personalized code for multiple users through a lightweight, scalable framework.

The MPCODER Framework

MPCODER utilizes a two-pronged approach to capture the full spectrum of a developer's coding habits without the need for full-model fine-tuning.

Explicit Coding Style Learning

To capture syntax-level preferences, the framework employs Checkstyle to extract 25 specific syntax attributes, such as ParameterName or SeparatorWrap. To help the LLM internalize these rules, the researchers introduced Residual Learning. In this setup, the model is prompted to compare two code snippets that are identical in function but differ by a single style attribute. This "A vs. B" comparison forces the model to isolate and represent specific style elements more effectively than viewing them in isolation.

Implicit Coding Style Learning

To capture the "unwritten rules" of a developer’s style—the semantic conventions that are harder to quantify—MPCODER uses a Multi-user Style Adapter. This is a lightweight component integrated into the LLM. To prevent the model from conflating the styles of different users, the authors utilize Contrastive Learning. This mathematical approach ensures that the hidden vector representations (style signatures) of the same user are pushed closer together, while those of different users are pushed further apart.

Evaluation Methodology

Datasets

As no public dataset existed for multi-user personalized code generation, the authors curated two new datasets from the Programming Teaching Assistant (PTA) platform:

  • PCIDense: 50 users with an average of 115 records each, representing developers with extensive histories.
  • PCISparse: 1,121 users with an average of 31 records each, representing a broader, less-documented user base.
  • HumanEval-X: Used to ensure that personalization does not come at the cost of functional correctness.

Coding Style Score (CSS)

Traditional metrics like BLEU are insufficient for measuring style, as they focus on word overlap rather than structural habits. The authors proposed the Coding Style Score (CSS), which represents code as a 24-dimensional vector of style violations and uses Jensen-Shannon divergence to measure the similarity between the generated code and the user's historical style.

Key Results and Insights

Experimental results demonstrate that MPCODER significantly outperforms standard LLMs and baseline adapters in style matching:

  • Style Accuracy: On the PCIDense dataset, MPCODER achieved a CSS of 64.50%, compared to just 48.73% for the base CodeLlama model.
  • Human Preference: In a user study, participants preferred MPCODER’s personalized output 43% of the time, while the next best baseline (L-LDB) was preferred only 37% of the time, and standard adapters only 12%.
  • Efficiency: The framework is highly scalable; adding a new user to the system results in a negligible increase in parameters (approximately $3 \times 10^{-4}\%$), making it practical for large organizations.
  • Correctness: Crucially, MPCODER maintains high functional correctness. The results on HumanEval-X show that the model remains competitive with the base model, proving that personalization does not necessarily degrade the model's ability to solve programming tasks.

Conclusion and Future Work

MPCODER demonstrates that it is possible to generate highly personalized code for a large number of users within a single, efficient framework. By combining explicit residual learning with implicit contrastive learning, the model captures the nuance of individual developer habits.

Future research will focus on expanding the framework beyond Java to other languages like Python and C++. Additionally, the authors aim to improve performance in "sparse" data scenarios, as the model currently requires a baseline amount of historical data to accurately capture a user's implicit style.

Summary updated

Abstract

Large Language Models (LLMs) have demonstrated great potential for assisting developers in their daily development. However, most research focuses on generating correct code, how to use LLMs to generate personalized code has seldom been investigated. To bridge this gap, we proposed MPCoder (Multi-user Personalized Code Generator) to generate personalized code for multiple users. To better learn coding style features, we utilize explicit coding style residual learning to capture the syntax code style standards and implicit style learning to capture the semantic code style conventions. We train a multi-user style adapter to better differentiate the implicit feature representations of different users through contrastive learning, ultimately enabling personalized code generation for multiple users. We further propose a novel evaluation metric for estimating similarities between codes of different coding styles. The experimental results show the effectiveness of our approach for this novel task.
BibTeX
@inproceedings{dai2024mpcoder,
  author = {Zhenlong Dai and Chang Yao and WenKang Han and Ying Yuan and Zhipeng Gao and Jingyuan Chen},
  title = {{MPCODER: Multi-user Personalized Code Generator with Explicit and Implicit Style Representation Learning}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=YL65McMYHj},
}
Figure