一句话:提出知识驱动提示链 KPC,将代码生成拆为迭代检查-重写 AI 链,显著提升 LLM 异常处理质量。
大语言模型在自动代码生成上表现突出,但生成高质量可靠代码仍困难,尤其缺乏良好编程实践。本文先进行实证研究,总结 LLM 异常处理的三大挑战:异常处理不完整、不正确以及滥用 try-catch;并比较不同粒度提示,发现细粒度知识驱动提示效果最佳。基于此,提出知识驱动提示链式代码生成方法 KPC,将代码生成分解为含迭代检查-重写步骤的 AI 链,并链接细粒度知识提示,帮助 LLM 考虑异常处理规范。作者在从 Java 官方 API 文档抽取的 3,079 个代码生成任务上评估,静态评价指标分别提升 109.86% 与 578.57%,动态验证在抽样数据集上减少 18 个运行时 bug。
Large Language Models (LLMs) have shown promising results in automatic code generation by improving coding efficiency to a certain extent. However, generating high-quality and reliable code remains a formidable task because of LLMs' lack of good programming practice, especially in exception handling. In this paper, we first conduct an empirical study and summarize three crucial challenges of LLMs in exception handling, i.e., incomplete exception handling, incorrect exception handling and abuse of try-catch. We then try prompts with different granularities to address such challenges, finding fine-grained knowledge-driven prompts works best. Based on our empirical study, we propose a novel Knowledge-driven Prompt Chaining-based code generation approach, name KPC, which decomposes code generation into an AI chain with iterative check-rewrite steps and chains fine-grained knowledge-driven prompts to assist LLMs in considering exception-handling specifications. We evaluate our KPC-based approach with 3,079 code generation tasks extracted from the Java official API documentation. Extensive experimental results demonstrate that the KPC-based approach has considerable potential to ameliorate the quality of code generated by LLMs. It achieves this through proficiently managing exceptions and obtaining remarkable enhancements of 109.86% and 578.57% with static evaluation methods, as well as a reduction of 18 runtime bugs in the sampled dataset with dynamic validation.
DOI 原文 · arXiv · PDF(开放获取) · DBLP
@inproceedings{abs-2309-15606,
title = {From Misuse to Mastery: Enhancing Code Generation with Knowledge-Driven AI Chaining},
author = {Xiaoxue Ren and Xinyuan Ye and Dehai Zhao and Zhenchang Xing and Xiaohu Yang},
booktitle = {ASE},
year = {2023},
doi = {10.1109/ase56229.2023.00143}
}