人类规则是否必要?利用思维链推理与上下文学习生成可复用 API
Are Human Rules Necessary? Generating Reusable APIs with CoT Reasoning and In-Context Learning
AI总结:
本文提出 Code2API,利用思维链推理和少样本上下文学习自动将 Stack Overflow 代码片段转化为可复用 API,无需训练或人工规则,在参数与返回值识别上显著超越 APIzator,并验证了跨语言泛化能力。
摘要:
受大语言模型(LLMs)在解决复杂编码任务方面巨大潜力的启发,本文提出了一种名为 Code2API 的新方法,用于自动对 Stack Overflow 代码片段执行 API 化(APIzation)。Code2API 不需要额外的模型训练或任何人工制定的规则,并且可以轻松部署在个人计算机上,无需依赖其他外部工具。具体而言,Code2API 通过精心设计的提示引导大语言模型为给定的代码片段生成结构良好的 API。为了从大语言模型中引出知识和逻辑推理能力,我们使用了思维链(chain-of-thought, CoT)推理和少样本上下文学习(few-shot in-context learning),这有助于大语言模型充分理解 API 化任务,并以类似于开发者的方式逐步解决该任务。我们的评估表明,Code2API 在识别方法参数(65%)和返回语句(66%)方面达到了与人工生成结果相当的显著准确率,分别比当前最先进的方法 APIzator 高出 15.0% 和 16.5%。此外,与 APIzator 相比,我们的用户研究表明 Code2API 在生成有意义的方法名称方面表现出更优越的性能,甚至超过了人类水平的表现,并且开发者更愿意使用我们的方法生成的 API,这凸显了我们工具在实际应用中的适用性。最后,我们成功地将我们的框架扩展到 Python 数据集,取得了与 Java 相当的性能,验证了我们工具的泛化能力。
英文摘要:
Inspired by the great potential of Large Language Models (LLMs) for solving complex coding tasks, in this paper, we propose a novel approach, named Code2API, to automatically perform APIzation for Stack Overflow code snippets. Code2API does not require additional model training or any manual crafting rules and can be easily deployed on personal computers without relying on other external tools. Specifically, Code2API guides the LLMs through well-designed prompts to generate well-formed APIs for given code snippets. To elicit knowledge and logical reasoning from LLMs, we used chain-of-thought (CoT) reasoning and few-shot in-context learning, which can help the LLMs fully understand the APIzation task and solve it step by step in a manner similar to a developer. Our evaluations show that Code2API achieves a remarkable accuracy in identifying method parameters (65%) and return statements (66%) equivalent to human-generated ones, surpassing the current state-of-the-art approach, APIzator, by 15.0% and 16.5% respectively. Moreover, compared with APIzator, our user study demonstrates that Code2API exhibits superior performance in generating meaningful method names, even surpassing the human-level performance, and developers are more willing to use APIs generated by our approach, highlighting the applicability of our tool in practice. Finally, we successfully extend our framework to the Python dataset, achieving a comparable performance with Java, which verifies the generalizability of our tool.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。