让大模型生成可解释的代码转换逻辑,比直接改代码更精准高效。
Don't Transform the Code, Code the Transforms: Towards Precise Code Rewriting using LLMs
- 用少量输入输出样例引导模型生成可复用的代码转换规则
- 7个任务中转换完全准确,其余也比直接重写更精确
- 转换逻辑可检查、可调试,适合需要高可靠性的场景
代码重构与优化工具需兼具快速与准确。大语言模型(LLMs)天生缺乏这两点,但仍有巨大应用潜力。本文提出不直接让模型改代码,而是让其生成代码转换逻辑。通过链式思维方法,基于少量输入/输出样例合成转换规则,并引入执行与反馈机制。相比直接重写,生成的转换逻辑可检查、可调试、可验证,且运行开销极小。我们在16个Python代码转换任务上测试,发现其中7个任务生成的转换完全精确,其余任务也比直接使用LLM重写更少出错。旨在推动提升LLM代码重写的精度研究。
原文摘要 · Abstract (English)
Tools for rewriting, refactoring and optimizing code should be fast and correct. Large language models (LLMs), by their nature, possess neither of these qualities. Yet, there remains tremendous opportunity in using LLMs to improve code. We explore the use of LLMs not to transform code, but to code transforms. We propose a chain-of-thought approach to synthesizing code transformations from a small number of input/output code examples that incorporates execution and feedback. Unlike the direct rewrite approach, LLM-generated transformations are easy to inspect, debug, and validate. The logic of the rewrite is explicitly coded and easy to adapt. The compute required to run code transformations is minute compared to that of LLM rewriting. We test our approach on 16 Python code transformations and find that LLM- generated transforms are perfectly precise for 7 of them and less imprecise than direct LLM rewriting on the others. We hope to encourage further research to improving the precision of LLM code rewriting.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。