arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2406.03660cs.SE
收藏

重构为Pythonic惯用法:一种利用大语言模型的混合知识驱动方法

Refactoring to Pythonic Idioms: A Hybrid Knowledge-Driven Approach Leveraging Large Language Models

Zejun Zhang, Zhenchang Xing, Xiaoxue Ren, Qinghua Lu, Xiwei Xu

AI总结:

本文提出一种结合规则确定性与大语言模型适应性的混合方法,通过构建知识模块和分析规则接口实现Python代码的Pythonic惯用法重构,在多个惯用法上取得超过90%的准确率、F1分数、精确率和召回率。

摘要:

Pythonic惯用法在Python编程社区中备受重视并被广泛使用。然而,许多Python用户发现使用Pythonic惯用法具有挑战性。采用基于规则的方法或仅依赖大语言模型(LLM)的方法不足以克服代码惯用化过程中的三个持续挑战,包括代码遗漏、错误检测和错误重构。受规则的确定性和LLM的适应性的启发,我们提出了一种由三个模块组成的混合方法。我们不仅编写提示来指导LLM完成任务,还调用分析规则接口(ARI)来完成任务。ARI是通过提示LLM生成代码而产生的Python代码。我们首先构建一个包含ASTscenario、ASTcomponent和Condition三个元素的知识模块,并提示LLM生成Python代码以纳入ARI库供后续使用。之后,对于任何无语法错误的Python代码,我们从ARI库中调用ARI从ASTscenario中提取ASTcomponent,然后过滤掉不满足条件的ASTcomponent。最后,我们设计提示来指导LLM对代码进行抽象和惯用化,然后从ARI库中调用ARI将非惯用代码重写为惯用代码。接下来,我们对我们的方法、RIdiom和Prompt-LLM在RIdiom中的九个已建立的Pythonic惯用法上进行了全面评估。我们的方法在准确率、F1分数和召回率上表现出优越性,同时保持与RIdiom相当的精确率水平,对于每个惯用法的每个指标,这些值均持续超过或接近90%。最后,我们将评估扩展到四个新的Pythonic惯用法。我们的方法持续优于Prompt-LLM,在准确率、F1分数、精确率和召回率上均实现持续超过90%的指标值。

英文摘要:

Pythonic idioms are highly valued and widely used in the Python programming community. However, many Python users find it challenging to use Pythonic idioms. Adopting a rule-based approach or LLM-only approach is not sufficient to overcome three persistent challenges of code idiomatization including code miss, wrong detection and wrong refactoring. Motivated by the determinism of rules and adaptability of LLMs, we propose a hybrid approach consisting of three modules. We not only write prompts to instruct LLMs to complete tasks, but we also invoke Analytic Rule Interfaces (ARIs) to accomplish tasks. The ARIs are Python code generated by prompting LLMs to generate code. We first construct a knowledge module with three elements including ASTscenario, ASTcomponent and Condition, and prompt LLMs to generate Python code for incorporation into an ARI library for subsequent use. After that, for any syntax-error-free Python code, we invoke ARIs from the ARI library to extract ASTcomponent from the ASTscenario, and then filter out ASTcomponent that does not meet the condition. Finally, we design prompts to instruct LLMs to abstract and idiomatize code, and then invoke ARIs from the ARI library to rewrite non-idiomatic code into the idiomatic code. Next, we conduct a comprehensive evaluation of our approach, RIdiom, and Prompt-LLM on nine established Pythonic idioms in RIdiom. Our approach exhibits superior accuracy, F1-score, and recall, while maintaining precision levels comparable to RIdiom, all of which consistently exceed or come close to 90% for each metric of each idiom. Lastly, we extend our evaluation to encompass four new Pythonic idioms. Our approach consistently outperforms Prompt-LLM, achieving metrics with values consistently exceeding 90% for accuracy, F1-score, precision, and recall.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑