Automatic Smart Contract Comment Generation via Large Language Models and In-Context Learning
Junjie Zhao, Xiang Chen, Guang Yang, Yiheng Shen
TL;DR
This paper tackles automatic generation of smart contract comments by introducing SCCLLM, a three-phase framework that harnesses large language models through in-context learning. It pairs a two-stage demonstration retrieval (semantic, then syntactic/lexical) with a carefully constructed prompt to elicit high-quality comments for Solidity code via a ChatGPT API, without model fine-tuning. Empirical results on nearly 30k <method,comment> pairs from 40k+ contracts show that SCCLLM outperforms state-of-the-art baselines in both automatic metrics (BLEU and ROUGE variants) and human judgments, with ablations confirming the value of demonstration selection and the number of demonstrations. The work demonstrates the viability of using LLMs for software documentation tasks in the smart contract domain and points to prompts and demonstrations as critical levers for performance and generalization.
Abstract
The previous smart contract code comment (SCC) generation approaches can be divided into two categories: fine-tuning paradigm-based approaches and information retrieval-based approaches. However, for the fine-tuning paradigm-based approaches, the performance may be limited by the quality of the gathered dataset for the downstream task and they may have knowledge-forgetting issues. While for the information retrieval-based approaches, it is difficult for them to generate high-quality comments if similar code does not exist in the historical repository. Therefore we want to utilize the domain knowledge related to SCC generation in large language models (LLMs) to alleviate the disadvantages of these two types of approaches. In this study, we propose an approach SCCLLM based on LLMs and in-context learning. Specifically, in the demonstration selection phase, SCCLLM retrieves the top-k code snippets from the historical corpus by considering syntax, semantics, and lexical information. In the in-context learning phase, SCCLLM utilizes the retrieved code snippets as demonstrations, which can help to utilize the related knowledge for this task. We select a large corpus from a smart contract community Etherscan.io as our experimental subject. Extensive experimental results show the effectiveness of SCCLLM when compared with baselines in automatic evaluation and human evaluation.
