AgentCoder: Multi-Agent-based Code Generation with Iterative Testing and Optimisation

📄 arXiv: 2312.13010v3 📥 PDF

作者: Dong Huang, Jie M. Zhang, Michael Luck, Qingwen Bu, Yuhao Qing, Heming Cui

分类: cs.CL

发布日期: 2023-12-20 (更新: 2024-05-24)

备注: 24 pages, 12 figures


💡 一句话要点

提出AgentCoder,通过多智能体协作迭代测试优化代码生成。

🎯 匹配领域: 支柱九:具身大模型 (Embodied Foundation Models)

关键词: 代码生成 多智能体系统 迭代测试 大型语言模型 软件工程

📋 核心要点

  1. 现有代码生成模型难以平衡代码生成、测试用例生成和执行,导致代码质量难以保证。
  2. AgentCoder采用多智能体框架,程序员、测试设计者和执行者协同工作,迭代优化代码。
  3. 实验表明,AgentCoder在代码生成任务上显著优于现有模型和提示工程技术,token开销更低。

📝 摘要(中文)

Transformer架构的大型语言模型(LLMs)显著推动了自然语言处理(NLP)的进步。这些模型彻底改变了NLP任务,尤其是在代码生成方面,帮助开发者更高效地创建软件。然而,在代码片段生成与有效的测试用例生成和执行之间取得平衡仍然存在挑战。为了解决这些问题,本文提出了一种新颖的解决方案——多智能体辅助代码生成(AgentCoder),它包含一个具有专门智能体的多智能体框架:程序员智能体、测试设计者智能体和测试执行者智能体。在编码过程中,程序员智能体将专注于代码生成,并根据测试执行者智能体的反馈进行改进。测试设计者智能体将为生成的代码生成测试用例,测试执行者智能体将使用测试用例运行代码,并将反馈写入程序员。这种协作系统确保了强大的代码生成,超越了单智能体模型和传统方法的局限性。我们在9个代码生成模型和12种增强方法上进行的大量实验表明,AgentCoder在各种基准测试中优于现有的代码生成模型和提示工程技术。例如,AgentCoder (GPT-4) 在HumanEval和MBPP数据集中分别实现了96.3%和91.8%的pass@1,总token开销为56.9K和66.3K,而最先进的技术仅获得90.2%和78.9%的pass@1,总token开销为138.2K和206.5K。

🔬 方法详解

问题定义:现有代码生成模型,尤其是基于大型语言模型的模型,在生成代码时面临一个关键问题:如何有效地生成高质量、可靠的代码。现有的方法往往难以在代码生成的同时,生成充分的测试用例并执行测试,从而导致生成的代码可能存在缺陷或无法满足特定需求。此外,现有方法在计算资源和token开销方面也存在优化空间。

核心思路:AgentCoder的核心思路是引入多智能体协作机制,模拟软件开发团队中不同角色的分工与合作。通过将代码生成、测试用例设计和测试执行分别交给不同的智能体,实现代码的迭代优化和质量提升。这种方法借鉴了软件工程中的测试驱动开发(TDD)理念,将测试作为代码生成过程中的重要组成部分。

技术框架:AgentCoder包含三个主要智能体:程序员智能体、测试设计者智能体和测试执行者智能体。程序员智能体负责生成和改进代码;测试设计者智能体负责为生成的代码设计测试用例;测试执行者智能体负责执行测试用例,并将结果反馈给程序员智能体。整个流程是一个迭代过程,程序员智能体根据测试结果不断优化代码,直到满足预定的质量标准。

关键创新:AgentCoder的关键创新在于其多智能体协作框架,它将代码生成过程分解为多个独立的任务,并分配给不同的智能体执行。这种分工协作的方式能够更有效地利用大型语言模型的潜力,提高代码生成的质量和效率。与传统的单智能体方法相比,AgentCoder能够更好地模拟软件开发过程中的复杂交互,从而生成更可靠的代码。

关键设计:AgentCoder的具体实现细节(如智能体的具体prompt设计、迭代次数、测试用例生成策略等)在论文中可能没有详细描述,属于未知信息。但整体框架依赖于精心设计的prompt来引导每个智能体的行为,以及有效的反馈机制来确保迭代过程的收敛。

📊 实验亮点

AgentCoder (GPT-4) 在HumanEval和MBPP数据集中分别实现了96.3%和91.8%的pass@1,显著优于现有技术水平(90.2%和78.9%)。同时,AgentCoder的token开销也大幅降低,分别为56.9K和66.3K,而现有技术则为138.2K和206.5K。这表明AgentCoder在提高代码生成质量的同时,也降低了计算成本。

🎯 应用场景

AgentCoder具有广泛的应用前景,可用于自动化软件开发、代码辅助生成、智能编程教育等领域。它可以帮助开发者更高效地编写代码,减少错误,提高软件质量。此外,AgentCoder还可以作为一种教学工具,帮助学生学习编程和软件测试。

📄 摘要(原文)

The advancement of natural language processing (NLP) has been significantly boosted by the development of transformer-based large language models (LLMs). These models have revolutionized NLP tasks, particularly in code generation, aiding developers in creating software with enhanced efficiency. Despite their advancements, challenges in balancing code snippet generation with effective test case generation and execution persist. To address these issues, this paper introduces Multi-Agent Assistant Code Generation (AgentCoder), a novel solution comprising a multi-agent framework with specialized agents: the programmer agent, the test designer agent, and the test executor agent. During the coding procedure, the programmer agent will focus on the code generation and refinement based on the test executor agent's feedback. The test designer agent will generate test cases for the generated code, and the test executor agent will run the code with the test cases and write the feedback to the programmer. This collaborative system ensures robust code generation, surpassing the limitations of single-agent models and traditional methodologies. Our extensive experiments on 9 code generation models and 12 enhancement approaches showcase AgentCoder's superior performance over existing code generation models and prompt engineering techniques across various benchmarks. For example, AgentCoder (GPT-4) achieves 96.3\% and 91.8\% pass@1 in HumanEval and MBPP datasets with an overall token overhead of 56.9K and 66.3K, while state-of-the-art obtains only 90.2\% and 78.9\% pass@1 with an overall token overhead of 138.2K and 206.5K.