Code generation method and system based on heterogeneous multi-agent cooperation and electronic equipment
By introducing a heterogeneous large language model to the multi-agent collaboration framework, the consistency and feasibility verification of the demand documents is solved, and the problem of lack of consistency verification and cognitive bias in the existing technology is improved, and the accuracy and robustness of code generation are improved.
Patent Information
- Application Number
- CN202510079265.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-17
- Publication Date
- 2025-04-15
- Estimated Expiration
- 2045-01-17
AI Technical Summary
The existing multi-agent collaboration framework based on large language models lacks the consistency verification mechanism for task requirements in code generation tasks, and there is cognitive bias in homogeneous large language models, making it difficult to discover and correct cognitive biases of team members in the task decision-making process.
The code generation method of heterogeneous multi-agent collaboration is adopted to introduce a commentator agent instantiated by the heterogeneous large language model to verify the consistency and feasibility of the requirement documents, and make modification suggestions in the early stages to improve the accuracy and robustness of multi-agent collaboration.
It effectively solves the deviation problem between the requirements analysis and verification stages, reduces the cost and complexity of later corrections, improves the robustness and reliability of the code, and enables the generated results to maintain a high level of correctness when facing extreme situations or specific boundary conditions.
Smart Images

Figure CN119829038A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of code generation, and in particular to a code generation method, system and electronic device based on heterogeneous multi-agent collaboration. Background Art
[0002] In recent years, as Large Language Models (LLM) have made significant progress in natural language understanding, code generation and other fields, researchers have begun to try to build a multi-agent framework that uses multiple LLMs to collaboratively handle complex tasks. Such a framework consists of multiple agents that interact with each other (usually instantiations of LLMs), supplemented by auxiliary components. These agents can take on roles such as analysis, design, implementation, and testing, and effectively alleviate the ability of a single LLM to handle long contexts, complex reasoning, or multi-step decisions through division of labor and collaboration in the task process. For example: MetaGPT multi-agent framework and Devin.
[0003] Research in recent years has shown that multi-agent collaboration can make up for the shortcomings of a single LLM to a certain extent. For example: effectively use teamwork to solve highly complex code generation problems. Through role division, reduce the cognitive burden of a single LLM in dealing with long contexts and complex logic. Use post-verification methods such as test agents to improve the accuracy and stability of the final code results. However, these existing frameworks still have obvious defects in the requirements analysis and verification stages, and have not yet formed a mechanism for independent verification of task planning in the early stages of the project.
[0004] In the existing multi-agent collaboration framework based on large language models (LLMs), the roles of analysts, architects, developers, testers, etc. are mainly instantiated with homogeneous large language models. Although these methods have achieved certain results in the field of automatic code generation, they still have the following major shortcomings when dealing with complex and ambiguous code generation tasks: (1) Lack of consistency verification mechanism for task requirements. Existing multi-agent methods usually rely too much on the plans of the analyst agent, but fail to effectively verify whether these plans are consistent with the task requirements. This may lead to problems such as analysts misunderstanding task requirements, generating incorrect task plans, and guiding other agents to perform incorrect tasks, resulting in incorrect solutions and unnecessary waste of resources.
[0005] (2) Homogeneous large language models have cognitive biases. The current multi-agent framework assigns specific roles and responsibilities to different agents through prompt engineering, but all agents are based on the same large language model (LLM). This homogeneous structure makes it difficult to timely detect and correct the cognitive biases that team members have in the task decision-making process, and may even further amplify these biases, causing the team's goals to deviate from expectations. Summary of the invention
[0006] Based on this, it is necessary to provide a code generation method, system and electronic device based on heterogeneous multi-agent collaboration to address the above technical problems. This method introduces a requirement verification mechanism in the early stage of the project, reviews the team's planning, and improves the accuracy and robustness of multi-agent collaboration through diversified agent role division and data verification, thereby effectively solving the above technical problems.
[0007] A code generation method based on heterogeneous multi-agent collaboration, using a homogeneous large language model to instantiate an analyst agent, a programmer agent and a tester agent, and using a heterogeneous large language model to instantiate a critic agent; the method comprises: Get the software development or code generation task requirements to be solved.
[0008] The analyst agent analyzes the task requirements according to the requirements analysis prompts and generates a requirements document.
[0009] The critic agent verifies the requirement document according to the requirement verification prompts and obtains a verification report.
[0010] If the verification report points out problems with the requirements document, the analyst agent modifies the requirements document based on the critic agent's suggestions.
[0011] The programmer agent writes the initial code based on the coding prompts and verifies the revised requirements document.
[0012] The tester agent designs test functions and test cases according to the test prompts and the initial code, integrates the initial code with the test function and executes it to generate a test report.
[0013] The programmer agent revises the initial code according to the test report and hands it over to the tester agent for execution again until the process termination condition is met, thus completing the code generation.
[0014] In one of the implementation use cases, the analyst agent analyzes the task requirements based on the requirements analysis prompts and generates a requirements document, including: The analyst agent first fully understands the task requirements and decomposes the overall task into several subtasks based on the requirements analysis prompts.
[0015] The analyst agent sorts and prioritizes each subtask and designs several high-level key steps for the subtask; the key steps describe the logical path from the starting point requirements to the deliverable results.
[0016] The analyst agent generates a requirement document based on the output template; the requirement document includes: a prioritized subtask list, key steps and corresponding instructions.
[0017] In one embodiment, the critic agent verifies the requirement document according to the requirement verification prompt and obtains a verification report including: The critic agent performs reverse reasoning on each subtask in the requirements document, infers the expected requirements, and determines whether they are consistent with the initial task requirements; if there are any inconsistencies, the critic agent will point them out in the verification report.
[0018] The critic agent selects a representative use case scenario from the initial task requirements, performs logical deduction based on the analyst agent's high-level plan, and determines whether the analyst agent's high-level plan is technically feasible. If it is found that the plan logic is not rigorous, lacks feasibility, or has deviations, the critic agent will make modification suggestions in the verification report.
[0019] In one embodiment, if the verification report indicates that there are problems with the requirement document, the analyst agent modifies the requirement document according to the suggestions of the critic agent, including: If the verification report indicates that there are problems with the requirements document, the analyst agent will update the subtask definitions and adjust the key implementation steps according to the suggestions in the verification report to better meet the task requirements and generate an updated requirements document. If the verification report indicates that the requirements document is correct, the analyst agent will not make any revisions.
[0020] In one embodiment, the programmer agent writes initial code based on coding prompts and verified revised requirements documents, including: The programmer agent refers to the subtasks and high-level plans that have been clarified in the updated requirements document and converts them into specific code implementations.
[0021] After the code is written, the programmer agent checks the code line by line to correct possible syntax errors or obvious logical loopholes.
[0022] In one embodiment, the tester agent designs a test function and a test case according to the test prompt and the initial code, and integrates the initial code with the test function for execution, including: The tester agent runs the test cases one by one and records the execution results; if a case fails, the reason for the failure is recorded in the result array.
[0023] After all test cases are executed, the tester agent generates a test report based on the execution results; the test report includes: failed cases, reasons for failure and suggested improvement directions.
[0024] In one embodiment, the programmer agent revises the initial code according to the test report and again hands it over to the tester agent for execution until the process termination condition is met, thereby completing code generation, including: The programmer agent refers to the failed cases in the test report, locates the logical errors or implementation defects in the code, and makes code corrections.
[0025] After the code correction is completed, the tester agent continues to retest the revised code. If all test cases are passed, the process is terminated and the code testing is completed. If all test cases are not passed, the programmer agent continues to revise the initial code according to the test report. If the number of iterations exceeds the preset value and all test cases cannot be passed, the process is terminated and the code generation is completed.
[0026] A second code generation method based on heterogeneous multi-agent collaboration uses a homogeneous large language model to instantiate an analyst agent, a programmer agent, and a tester agent, and uses a heterogeneous large language model to instantiate a critic agent; the method includes: Get the software development or code generation task requirements to be solved.
[0027] The analyst agent analyzes the task requirements according to the requirements analysis prompts and obtains the initial requirements document.
[0028] The critic agent verifies the requirement document in batches according to the requirement verification prompts. The critic agent and the analyst agent interactively verify and correct parts of the initial requirement document, thereby improving the requirements in stages.
[0029] The programmer agent simultaneously receives local test feedback from the tester agent during the process of writing code according to coding hints and verifying the revised requirement document until all tasks in the revised requirement document are completed and code generation is completed.
[0030] A code generation device based on heterogeneous multi-module collaboration includes: an analyst module, a programmer module and a tester module instantiated using a homogeneous large language model, and a critic module instantiated using a heterogeneous large language model.
[0031] The analyst module is used to deeply understand and analyze the task requirements of software development or code generation to be solved according to the requirements analysis prompts, decompose the task requirements into several subtasks and sort them according to priority, and at the same time give several high-level key implementation steps based on the subtasks, output the requirements document, and feed back the requirements document to the critic module; it is also used to revise the requirements document according to the verification report fed back by the critic module.
[0032] The critic module verifies the requirement document according to the requirement verification prompts. If any problems are found, a verification report is output and fed back to the analyst module. The Programmer Module is used to write the initial code according to the coding prompts, key plans and subtasks after the requirement document has been reviewed by the Critics Module and revised by the Analyst Module, and to perform line-by-line self-checking on the initial code to correct potential syntax errors and logical omissions; it is also used to iteratively correct the code according to the test report feedback from the Tester Module.
[0033] The tester module is used to design test functions and test cases based on the test prompts and initial code, and merge the initial code with the test function for execution. After executing all test cases, the results are analyzed and a test report is output. The test report will be fed back to the programmer module until all test cases pass or the preset upper limit of iterations is reached; the test report includes failed test cases, reasons for failure, and suggestions for improvement.
[0034] An electronic device includes a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented: Get the software development or code generation task requirements to be solved.
[0035] The analyst agent analyzes the task requirements according to the requirements analysis prompts and generates a requirements document.
[0036] The critic agent verifies the requirement document according to the required verification prompts and obtains a verification report.
[0037] If the verification report indicates that there are problems with the requirements document, the analyst agent will modify the requirements document according to the suggestions of the critic agent. If the verification report indicates that the requirements document is correct, the analyst agent will not make any revisions.
[0038] The programmer agent writes the initial code based on the coding hints and verifies the revised requirements document.
[0039] The tester agent designs test functions and test cases according to the test prompts and the initial code, integrates the initial code with the test function and executes it to generate a test report.
[0040] The programmer agent revises and iterates the initial code based on the test report until the process termination conditions are met, completing code generation.
[0041] The above-mentioned code generation method, system and electronic device based on heterogeneous multi-agent collaboration, by adding a critic agent instantiated by a special heterogeneous large language model into the multi-agent collaboration process, uses the critic agent to verify the consistency and feasibility of the requirement document given by the analyst agent, and proposes modification suggestions at an early stage, so as to timely discover deviations and problems in the early stage of the task, reduce the cost and complexity of later corrections, improve the robustness and reliability of the code, and enable the generated results to maintain a high level of correctness in the face of extreme situations or specific boundary conditions. It has practical significance for improving software development efficiency, reducing project risks, and improving the quality of intelligent automation solutions. BRIEF DESCRIPTION OF THE DRAWINGS
[0042] Figure 1 A schematic diagram of a flow chart of a code generation method based on heterogeneous multi-agent collaboration in one embodiment; Figure 2 A multi-agent framework diagram in one embodiment; Figure 3 FIG. 4 is a diagram showing the internal structure of an electronic device in one embodiment. DETAILED DESCRIPTION
[0043] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0044] The Large Language Model (LLM) is a large-scale model trained through deep learning that can understand and generate natural language text. The model can understand the meaning of user input based on the context and generate reasonable text output.
[0045] The multi-agent collaboration framework refers to combining multiple agents with different functions or roles (in this invention, role modules based on LLM) into the same system. Each agent collaborates with each other according to its role responsibilities to jointly complete complex tasks.
[0046] In one embodiment, Figure 1As shown, a code generation method based on heterogeneous multi-agent collaboration is provided. The analyst agent, programmer agent and tester agent are instantiated using a homogeneous large language model, and the critic agent is instantiated using a heterogeneous large language model. The critic agent and the analyst agent are instantiated using different large language models to achieve multi-angle thinking and review of task requirements and reduce cognitive bias caused by homogeneous large language models. Each agent clarifies its own responsibilities and input and output formats through prompt engineering. The method includes the following steps: Step 100: Obtain the task requirements of software development or code generation to be solved.
[0047] Specifically, the task requirements of software development or code generation to be solved are obtained from the user (or upstream module). The task requirements are usually in natural language form, including functional descriptions, input and output format requirements, performance indicators or boundary conditions, etc.
[0048] Step 102: The analyst agent analyzes the task requirements according to the requirements analysis prompts and generates a requirements document.
[0049] Specifically, the analyst agent deeply understands and analyzes the input task requirements, decomposes the complex task requirements into several subtasks, sorts and prioritizes each subtask, and gives several high-level key implementation steps based on the subtasks. These steps are output by the analyst agent in the form of a requirements document, which provides a basis for subsequent verification and implementation.
[0050] A requirements document is a document used to describe software requirements, including a prioritized list of subtasks and key implementation steps. The requirements document can provide a basis for subsequent coding and testing.
[0051] Step 104: The critic agent verifies the consistency and feasibility of the requirement document according to the requirement verification prompt and generates a verification report.
[0052] Specifically, the critic agent verifies the requirements document generated by the analyst agent according to the requirements verification prompts, infers the expected requirements by reverse reasoning for each subtask in the requirements document, and determines whether it is consistent with the initial task requirements. At the same time, the high-level plan is verified by example logic from the perspective of practical feasibility to check whether the analyst agent's understanding of the requirements is reasonable. If problems are found, the critic agent outputs a verification report (VerificationReport) and provides targeted modification suggestions.
[0053] Verification work includes consistency assessment and feasibility assessment, which are used to identify potential problems. Consistency assessment refers to the process of comparing the logic of the subtasks in the requirements document with the original task requirements. If the subtasks are inconsistent with the original task requirements, the requirements document needs to be modified. Feasibility assessment refers to the logical deduction of the high-level plan described in the requirements document to determine whether it is technically feasible and implementable.
[0054] The Verification Report is a feedback document produced by the critic agent, which points out the problems and deviations in the requirement document and provides modification suggestions.
[0055] In terms of technical implementation, an independent critic agent with different cognitive characteristics from the analyst agent conducts a technical review of the initial solution to promptly discover and correct blind spots and deviations in the understanding of requirements. This can reduce the cost of subsequent repeated revisions in the early stages and improve development efficiency.
[0056] Enhance team robustness and deviation correction capabilities: By introducing different LLMs into the team, the system is equipped with heterogeneous thinking methods, which can achieve multi-angle review and heterogeneous analysis in the technical process. This will effectively improve the fault tolerance and code generation quality in the face of extreme scenarios, and reduce the inadaptability of the final solution to special use cases. By introducing the requirements verification mechanism and heterogeneous large language model roles in the early stages of the project, the technical solution of the present invention has significant advantages in reducing resource consumption and improving the quality and robustness of code output, thereby effectively overcoming the technical shortcomings existing in the prior art.
[0057] In the traditional software development process, requirements analysis and verification are very critical early steps. If the requirements are misunderstood, missing, or have large deviations, the subsequent coding and testing phases will be executed under the wrong premise, increasing the resource consumption and risk of failure of the project. The multi-agent framework of this method introduces a critic agent to verify the requirements document in the requirements analysis phase, and returns the verification report output by the critic agent to the analyst agent to update and revise the requirements document (RD), so as to correct the requirements deviation in time before the coding and testing phases. This closed-loop process of "requirements analysis → verification → revision" aims to discover deviations and problems in the requirements phase as early as possible, avoiding the need to invest a lot of resources in the later stage to remedy errors in the requirements phase, reducing the number of code reworks from a technical logic perspective, and improving development efficiency and stability.
[0058] The critic agent and the analyst agent come from different model architectures or training methods, and thus have differentiated cognitive and judgment standards. The heterogeneous agent (critic agent) verifies the consistency and feasibility of the requirement documents given by the analyst agent, and proposes modification suggestions at an early stage to reduce the waste of subsequent development resources and reduce the risk of error propagation.
[0059] Step 106: If the verification report indicates that there are problems with the requirement document, the analyst agent modifies the requirement document according to the suggestions of the critic agent.
[0060] Specifically, if the verification report of the critic agent points out problems in the requirement document, the analyst agent will modify the requirement document according to the suggestions of the critic agent.
[0061] By utilizing the verification report feedback link, the analyst agent can revise the requirement document at an early stage, thereby reducing the negative impact on the subsequent coding and testing stages from the source.
[0062] Step 108: The programmer agent writes initial code based on the coding prompts and verified revised requirements document.
[0063] Specifically, based on the requirements document verified by the critic agent and revised by the analyst agent, the programmer agent implements the preliminary code according to the key plans and subtasks. Afterwards, the programmer agent performs self-checks on the generated code line by line to correct potential syntax errors or logical omissions.
[0064] Step 110: The tester agent designs test functions and test cases according to the test prompts and the initial code, integrates the initial code with the test function and executes it, and generates a test report.
[0065] Specifically, the tester agent designs applicable test functions and test cases based on the initial code and test prompts, and combines the code with the test function for execution; the format of the test case is an assertion statement. After executing all test cases, the tester agent analyzes the results and outputs a test report, which lists the failed test cases and the reasons for the failure.
[0066] The test report is produced by the tester agent after executing the test case. It lists the failed cases and their causes, and provides a basis for the programmer agent to modify the code.
[0067] Prompt Engineering: The process of designing and optimizing the text prompts that are fed into a large language model. By adjusting the content and form of the prompts, the model can more accurately understand the instructions, roles, and data formats to ensure that the agent can perform the expected tasks.
[0068] Step 112: The programmer agent revises the initial code according to the test report and hands it over to the tester agent for execution again until the process termination condition is met, completing the code generation.
[0069] Specifically, if the code passes all test cases, the task is successfully completed and the process ends. If a test fails, the programmer agent can revise the code according to the test report and execute the test steps again.
[0070] In the above-mentioned code generation method based on heterogeneous multi-module collaboration, the method adds a critic agent instantiated by a special heterogeneous large language model to the multi-agent collaboration process, uses the critic agent to verify the consistency and feasibility of the requirement document given by the analyst agent, and proposes modification suggestions at an early stage, so as to timely discover deviations and problems in the early stage of the task, reduce the cost and complexity of later corrections, improve the robustness and reliability of the code, and enable the generated results to maintain a high level of correctness in the face of extreme situations or specific boundary conditions. It has practical significance for improving software development efficiency, reducing project risks, and improving the quality of intelligent automation solutions.
[0071] The framework of this method is not limited to specific task types or language models. Different LLMs can be flexibly expanded and replaced as critics or other roles according to actual needs, making it applicable to a variety of complex software development and code generation scenarios. In addition, this method is not only applicable to code generation tasks, but can also be extended to solving other multi-step complex problems. For example: (1) In natural language processing tasks, the links of text understanding and extracting key information are taken as analyst tasks, and the critic agent verifies the extracted key points, thereby reducing the subsequent text classification or summary generation errors. (2) In the automated configuration of industrial control systems, the analyst agent generates the device parameter configuration plan, the critic agent verifies the feasibility of the plan in terms of security and stability, and finally the programmer agent and the tester agent perform control logic programming and testing. Regardless of the application field it is extended to, the core idea remains the same: verification and correction are performed in the early stages of the task to reduce costly rework in the later stages.
[0072] In one embodiment, step 102 includes: the analyst agent first fully understands the task requirements, and decomposes the overall task into several subtasks according to the requirements analysis prompts; the analyst agent sorts and prioritizes each subtask, and designs several high-level key steps for the subtasks; the key steps describe the logical path from the starting point requirements to the deliverable results; the analyst agent generates a requirements document based on the output template; the requirements document includes: a subtask list with priorities, key steps and corresponding instructions.
[0073] Specifically, the advantages of the present invention can be clearly demonstrated through in-depth analysis and reasoning of the operating logic of the existing multi-agent collaboration framework. In the traditional solution, when the analyst role has a cognitive bias in understanding the task requirements, due to the lack of independent verification and review links, the bias will be unconditionally accepted in the subsequent code writing and testing links, resulting in errors that continue to spread throughout the development process. Since each role is instantiated by a homogeneous large language model (LLM), the cognitive tendencies and preferences between these models tend to be consistent, making it difficult to spontaneously question and correct deviations in the analysis phase. The final result is: errors in the requirements analysis link are discovered in the later testing phase, which means that more resources need to be invested in repeated corrections and iterations, which not only increases development costs and time, but also weakens the original intention and value of multi-agent collaboration.
[0074] The analyst agent breaks down complex requirements into multiple subtasks and provides several high-level key steps. This requirement document is presented in a structured form, allowing the critic agent, programmer agent, and tester agent to clearly understand and call it, thereby achieving collaborative work with clear division of labor. This hierarchical and structured requirement description method facilitates rapid problem identification and optimization solutions in the verification and implementation stages.
[0075] In one embodiment, step 104 includes: the critic agent performs reverse reasoning on each subtask in the requirement document, infers the expected requirements, and determines whether they are consistent with the initial task requirements; if there are inconsistencies, the critic agent points them out in the verification report; the critic agent selects a representative use case scenario from the initial task requirements, performs logical deduction based on the high-level plan of the analyst agent, and determines whether the high-level plan of the analyst agent is technically feasible. If it is found that the plan logic is not rigorous, lacks feasibility or has deviations, the critic agent proposes modification suggestions in the verification report.
[0076] Specifically, the verification work of the critic agent mainly includes consistency evaluation and feasibility evaluation. In the consistency evaluation process, the expected requirements are inferred by reverse reasoning for each subtask in the requirements document, and whether they are consistent with the initial task requirements is determined. At the same time, in the feasibility evaluation process, the high-level plan is verified by exemplary logic from the perspective of actual feasibility to check whether the Analyst's understanding of the requirements is reasonable.
[0077] This invention introduces a "Critic" role after the requirements analysis is completed, and uses heterogeneous LLM (a model different from the Analyst type) to verify the consistency and feasibility of the initial version of the requirements document, thereby achieving early error screening. This innovation leads to chain improvements in technical logic: First, demand deviations are identified and corrected before they are passed on to programming and testing stages, effectively curbing the waste of resources caused by the spread of errors in the later stages.
[0078] Secondly, due to the differences in thinking styles and knowledge preferences between Critic and Analyst, their independent review of the requirements document can discover blind spots that are easily overlooked in homogeneous teams, thereby enhancing the team's sensitivity to and ability to correct cognitive biases.
[0079] As the requirements document becomes more accurate and complete through repeated verification and revision in the early stages, the subsequent work of the Programmer and Tester will be carried out on a clearer and more accurate basis, which not only improves the accuracy of code generation, but also makes the writing and execution of test cases more efficient.
[0080] For complex scenarios and edge test cases, this mechanism of ensuring correct requirements at the source greatly improves the robustness and stability of the final code solution, reduces the bit error rate and subsequent adjustment costs, and improves task completion speed and overall quality.
[0081] To sum up, the advantage of the present invention is that it is clearly presented through logical deduction: the innovative link of introducing heterogeneous LLM for early demand verification in the multi-agent collaborative system effectively reduces the technical cost and time consumption caused by demand deviation, and enhances the team's resilience and reliability in dealing with complex problems, thereby making the overall development process more efficient and quality-assured.
[0082] In one embodiment, step 106 includes: if the verification report indicates that there are problems with the requirements document, the analyst agent updates the subtask definition and adjusts the key implementation steps according to the suggestions in the verification report to better meet the task requirements, and generates an updated requirements document, which is more complete in terms of consistency and feasibility. If the verification report indicates that the requirements document is correct, the analyst agent does not make any revisions.
[0083] In one embodiment, step 108 includes: the programmer agent refers to the subtasks and high-level plans that have been specified in the updated requirements document and converts them into specific code implementations; after the code is written, the programmer agent checks the code line by line to correct possible syntax errors or obvious logical loopholes.
[0084] Specifically, the programmer agent checks and corrects the code after the program is written, which can reduce the error rate in the subsequent testing phase and reduce the cost of modification.
[0085] In one embodiment, step 110 includes: the tester agent runs the test cases one by one and records the execution results; if a case fails, the reason for failure is recorded in the result array; after all test cases are executed, the tester agent generates a test report based on the execution results; wherein the test report includes: failed cases, reasons for failure and suggested improvement directions.
[0086] In one embodiment, step 112 includes: the programmer agent refers to the failed case in the test report, locates the logical error or implementation defect in the code, and corrects the code; after the code correction is completed, the tester agent continues to retest the corrected code, if all test cases are passed, the process is terminated and the code generation is completed; if all test cases are not passed, the programmer agent continues to revise the initial code according to the test report, if the number of iterations exceeds the preset value and all test cases cannot be passed, the process is terminated and the code generation is completed.
[0087] Preferably, the preset value of the number of iterations is set to 4 to avoid an infinite loop.
[0088] In a specific complex software development task, the user needs to implement a data processing program to extract specific features from complex input data and statistically analyze the results.
[0089] The analyst agent first decomposes the task into several subtasks (such as data cleaning, feature extraction, and statistical analysis) based on the user's description, and gives a high-level implementation plan to generate a requirement document.
[0090] After the critic agent obtained the requirement document, it found that the analyst agent understood the subtask of "statistical analysis" as a simple addition, but ignored the more complex statistics (such as distribution analysis or regression model) actually required by the user. The critic agent pointed out this problem in the verification report and gave modification suggestions (such as requiring the Analyst to add details of the statistical analysis subtask and align it with the user's initial requirements).
[0091] After receiving the verification report from the critic agent, the analyst agent revises the requirements document, redefines subtasks (analysis or regression model), improves key plans to better meet task requirements, and generates an updated requirements document.
[0092] After the revision, the programmer agent implements the code according to the new requirements document, and the tester agent tests the code, using various boundary conditions and extreme data sets to verify the program to ensure the robustness of the program logic. If the code fails under certain specific inputs, the tester agent will output a test report to enable the programmer agent to correct the code until all test cases are passed or the maximum number of iterations is reached.
[0093] The final code can more accurately meet the user's initial task requirements and has high robustness in the face of complex scenarios and extreme data.
[0094] In this method, LLM (Large Language Model) is used as the basis of the agent, but different natural language processing (NLP) or artificial intelligence models can also be used as substitutes in implementation. As long as the model used can understand, decompose, verify and generate code, the purpose of the invention can be met. For example: use a multimodal model (which can understand text, graphics and other data) as an analyst agent or critic agent to verify requirements in more complex scenarios. Use traditional logical reasoning engines and formal verification tools to supplement the requirements given by the analyst agent. In this case, the critic agent can be implemented by a combination of LLM and formal tools to form a heterogeneous verification system.
[0095] In this method, the names of the agents are determined according to the role settings. In this method, the roles are set as Analyst, Critic, Programmer, and Tester. However, other role naming and division of labor can also be used to achieve the same function. For example: merge the Critic into a "Requirement Quality Reviewer" role, which, in addition to consistency and feasibility testing, can also be responsible for risk prediction and priority redistribution of requirements. Add an "Architecture Designer" role to take on the tasks of architecture design and abstract layer definition between Analyst and Programmer, thereby further ensuring the rationality of requirement decomposition. In this case, the Critic still verifies the requirements and architecture layers to ensure consistency of the final goals.
[0096] In one embodiment, a second code generation method based on heterogeneous multi-agent collaboration is proposed, which instantiates an analyst agent, a programmer agent, and a tester agent based on a large language model, and instantiates a critic agent using a heterogeneous large language model; the method includes: Step 200: Obtain the task requirements of software development or code generation to be solved.
[0097] Step 202: The analyst agent analyzes the requirements according to the requirements analysis prompts and generates an initial requirements document.
[0098] Step 204: The critic agent verifies the requirement document in batches according to the requirement verification prompts. The critic agent and the analyst agent interactively verify and correct parts of the initial requirement document, thereby improving the requirements in stages.
[0099] Step 206: The programmer agent synchronously receives partial test feedback from the tester agent during the process of writing code according to the verified revised requirement document until all tasks in the verified revised requirement document are completed and code generation is completed.
[0100] Specifically, the process in the method described in steps 100 to 112 is requirement analysis (Analyst) → requirement verification (Critic) → requirement modification (Analyst) → coding (Programmer) → testing (Tester) → loop iteration. In this embodiment: the requirement analysis and verification stages are carried out alternately: after the Analyst proposes the first version of the RD, the critic agent does not give all feedback at once, but verifies and corrects the parts of the RD in an interactive manner (multiple rounds of dialogue), thereby improving the requirements in stages. This embodiment still achieves the purpose of discovering and correcting requirement deviations at an early stage, but the verification process is more iterative, fine-grained and dynamic. The testing (Tester) stage is embedded in the coding process in advance: the local inspection feedback of the tester agent is synchronously received during the process of the programmer agent writing code. This alternative method of continuous integration and continuous testing (CI / CD) can accelerate the improvement of code quality and achieve similar technical effects under the goal of the present invention to improve the final code quality and robustness.
[0101] It is worth noting that the specific implementation of this method is not limited to the ideas proposed in this application, as long as it meets the invention purpose of early discovery and elimination of demand deviations and enhancing code quality and robustness. For example: (1) Verify requirements using a multi-stage expert system and formal methods.
[0102] Replace LLM-based Critic with an expert system (Expert System) based on logical rules and formal verification tools. In the early stage, after Analyst generates RD, Expert System uses predefined rules and constraints to conduct a preliminary check on RD, and finds defects by comparing the format and quality requirements of the standard software requirements specification (Software Requirements Specification, SRS). Then, use formal verification tools (such as Alloy and Z3) to model and verify key conditions in the requirements (such as input and output constraints). When the tool finds inconsistencies or infeasibility, Analyst modifies RD based on the results. This solution is consistent with the original solution in terms of technical goals, that is, reducing deviations and resource waste in subsequent development through early verification.
[0103] (2) Introducing a multi-mode collaborative verification team.
[0104] In addition to the LLM role, you can also use keyword matchers based on statistical models, named entity recognition models based on deep learning, and independent performance estimators (through simulation runs or time complexity analysis tools) to verify requirements in multiple dimensions. In this way, even if Critic is not used as a single role, the goals of consistency, feasibility, and robustness verification can be achieved through the collaboration of multiple tools.
[0105] It should be understood that although Figure 1 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 1 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.
[0106] In one embodiment, a code generation device based on heterogeneous multi-module collaboration is provided, the device comprising: an analyst module, a programmer module and a tester module instantiated using a homogeneous large language model, and a critic module instantiated using a heterogeneous large language model; wherein, The analyst module is used to deeply understand and analyze the task requirements of software development or code generation to be solved according to the requirements analysis prompts, decompose the task requirements into several subtasks and sort them according to priority, and give several high-level key implementation steps based on the subtasks, output the requirements document, and feed back the requirements document to the critic module; it is also used to modify the initial requirements document according to the verification report fed back by the critic module.
[0107] The critic module is used to verify the consistency and feasibility of the requirement document according to the requirement verification prompts. If any problems are found, a verification report is output and fed back to the analyst module. The Programmer Module is used to write the initial code according to the coding prompts, key plans and subtasks after the requirement document has been reviewed by the Critics Module and revised by the Analyst Module, and to perform line-by-line self-checking on the initial code to correct potential syntax errors and logical omissions; it is also used to iteratively correct the code according to the test report feedback from the Tester Module.
[0108] The tester module is used to design applicable test functions and test cases based on the initial code and test prompts, and merge the code and test functions for execution. After executing all test cases, the results are analyzed and a test report is output. The test report will be fed back to the programmer module until all test cases pass or the preset upper limit of iterations is reached; the test report includes failed test cases, reasons for failure, and suggestions for improvement.
[0109] The multi-agent framework is based on the collaborative work of multiple roles instantiated by the Large Language Model (LLM). Each role clarifies its own responsibilities and input and output formats through prompt engineering. The framework discovers and corrects demand deviations in the early stages by analyzing (Analyst role), verifying (Critic role), coding (Programmer role) and testing (Tester role) the requirements, thereby improving the accuracy and robustness of code generation.
[0110] In one of the embodiments, the analyst module is also used to first fully understand the requirements, decompose the overall task into several subtasks according to the input prompts, sort and prioritize each subtask, and design several high-level key steps for the subtasks; the key steps describe the logical path from the starting requirements to the deliverable results; it is also used to generate a requirement document based on the output template; the requirement document includes: a subtask list with priorities, key steps and corresponding instructions.
[0111] In one embodiment, the critic verification module is also used to reversely reason each subtask in the requirement document, infer the expected requirements, and determine whether they are consistent with the initial task requirements; if there are any inconsistencies, they are pointed out in the verification report; it is also used to select a representative use case scenario from the initial task requirements, perform logical deduction based on the high-level plan of the analyst module, and determine whether the high-level plan of the analyst module is technically feasible. If it is found that the plan logic is not rigorous, lacks feasibility, or has deviations, modification suggestions are made in the verification report.
[0112] In one embodiment, the analyst module is further configured to update the subtask definition, adjust the key implementation steps, and generate an updated requirement document in accordance with the suggestions in the verification report if the verification report indicates that there are problems with the requirement document, so as to better meet the task requirements. If the verification report indicates that the requirement document is correct, the analyst module does not make any revisions.
[0113] In one embodiment, the programmer module refers to the subtasks and high-level plans that have been specified in the updated requirements document and converts them into specific code implementations; after the code is written, the programmer module checks the code line by line to correct possible syntax errors or obvious logical loopholes.
[0114] In one of the embodiments, the tester module is also used to run the test cases one by one and record the execution results; if a case fails, the reason for failure is recorded in the result array; after all test cases are executed, the module generates a test report based on the execution results; the test report includes: failed cases, reasons for failure and suggested improvement directions.
[0115] In one of the embodiments, the programmer module is also used to refer to the failed cases in the test report, locate the logical errors or implementation defects in the code, and correct the code; after the code correction is completed, the tester module continues to retest the corrected code. If all the test cases are passed, the process is terminated and the code generation is completed; if all the test cases are not passed, the programmer agent continues to revise the initial code according to the test report. If the number of iterations exceeds the preset value and all the test cases cannot be passed, the process is terminated and the code generation is completed.
[0116] For the specific definition of the code generation device based on heterogeneous multi-module collaboration, please refer to the definition of the code generation method based on heterogeneous multi-module collaboration above, which will not be repeated here. The various modules in the above-mentioned code generation device based on heterogeneous multi-module collaboration can be fully or partially implemented by software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules. In one embodiment, an electronic device is provided, which can be a terminal, and its internal structure diagram can be as shown in the figure. Figure 3 As shown. The electronic device includes a processor, a memory, a network interface, a display screen and an input device connected through a system bus. Among them, the processor of the electronic device is used to provide computing and control capabilities. The memory of the electronic device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The network interface of the electronic device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a code generation method based on heterogeneous multi-module collaboration is implemented. The display screen of the electronic device can be a liquid crystal display screen or an electronic ink display screen, and the input device of the electronic device can be a touch layer covering the display screen, or a button, trackball or touchpad set on the housing of the electronic device, or an external keyboard, touchpad or mouse, etc.
[0117] Those skilled in the art will understand that Figure 3 The structure shown in the figure is merely a block diagram of a partial structure related to the scheme of the present application, and does not constitute a limitation on the electronic device to which the scheme of the present application is applied. The specific electronic device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components.
[0118] In one embodiment, an electronic device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps in the above-mentioned code generation method embodiment based on heterogeneous multi-module collaboration when executing the computer program.
[0119] The technical features of the above embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0120] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A code generation method based on heterogeneous multi-agent collaboration, characterized in that: A homogeneous large language model is used to instantiate an analyst agent, a programmer agent, and a tester agent, and a heterogeneous large language model is used to instantiate a critic agent; the method comprises: Obtain the task requirements for software development or code generation to be solved; The analyst agent analyzes the task requirements according to the requirements analysis prompt and generates a requirements document; The critic agent verifies the requirement document according to the requirement verification prompt to obtain a verification report; If the verification report indicates that there are problems with the requirement document, the analyst agent modifies the requirement document according to the suggestions of the critic agent; The programmer agent writes initial code based on coding prompts and verified revised requirements documents; The tester agent designs test functions and test cases according to the test prompts and the initial code, integrates the initial code with the test function and executes it, and generates a test report; The programmer agent revises the initial code according to the test report and hands it over to the tester agent for execution again until the process termination condition is met, thus completing the code generation.
2. The code generation method based on heterogeneous multi-agent collaboration according to claim 1 is characterized in that: The analyst agent analyzes the task requirements according to the requirements analysis prompt and generates a requirements document, including: The analyst agent first fully understands the requirements and decomposes the overall task into several subtasks according to the requirements analysis prompts; The analyst agent sorts and prioritizes each subtask and designs several high-level key steps for each subtask; the key steps describe the logical path from the starting point requirements to the deliverable results; The analyst agent generates a requirement document according to the output template; the requirement document includes: a subtask list including priorities, key steps and corresponding instructions.
3. The code generation method based on heterogeneous multi-agent collaboration according to claim 1 is characterized in that: The critic agent verifies the requirement document according to the requirement verification prompt and obtains a verification report, including: The critic agent performs reverse reasoning on each subtask in the requirement document, infers the expected requirements, and determines whether they are consistent with the initial task requirements; if there are any inconsistencies, the critic agent points them out in the verification report; The critic agent selects a representative use case scenario from the initial task requirements, performs logical deduction based on the high-level plan of the analyst agent, and determines whether the high-level plan of the analyst agent is technically feasible. If it is found that the plan logic is not rigorous, lacks feasibility or has deviations, the critic agent proposes modification suggestions in the verification report.
4. The code generation method based on heterogeneous multi-agent collaboration according to claim 3 is characterized in that: If the verification report indicates that there are problems with the requirement document, the analyst agent modifies the requirement document according to the suggestions of the critic agent, including: If the verification report points out that there are problems with the requirements document, the analyst agent will update the subtask definitions and adjust the key implementation steps in accordance with the suggestions in the verification report to better meet the task requirements and generate an updated requirements document; if the verification report indicates that the requirements document is correct, the analyst agent will not revise the requirements document.
5. The code generation method based on heterogeneous multi-agent collaboration according to claim 1 is characterized in that: The programmer agent writes initial code based on coding prompts and verifies the revised requirements document, including: The programmer agent refers to the subtasks and high-level plans specified in the updated requirements document and converts them into specific code implementations; After the code is written, the programmer agent checks the code line by line to correct possible syntax errors or obvious logical loopholes.
6. The code generation method based on heterogeneous multi-agent collaboration according to claim 1 is characterized in that: The tester agent designs a test function and a test case according to the test prompt and the initial code, and integrates the initial code with the test function for execution, including: The tester agent runs the test cases one by one and records the execution results; if a case fails, the failure reason is recorded in the result array; After all test cases are executed, the tester agent generates a test report based on the execution results; wherein the test report includes: failed cases, reasons for failure and suggested improvement directions.
7. The code generation method based on heterogeneous multi-agent collaboration according to claim 1 is characterized in that: The programmer agent revises the initial code according to the test report and hands it over to the tester agent for execution again until the process termination condition is met, completing the code generation, including: The programmer agent refers to the failed cases in the test report to locate the logic errors or implementation defects in the code and make code corrections; After the code correction is completed, the tester agent continues to retest the revised code. If all test cases are passed, the process is terminated and the code testing is completed. If all test cases are not passed, the programmer agent continues to revise the initial code according to the test report. If the number of iterations exceeds the preset value and all test cases cannot be passed, the process is terminated and the code generation is completed.
8. A second code generation method based on heterogeneous multi-agent collaboration, characterized in that: A homogeneous large language model is used to instantiate an analyst agent, a programmer agent, and a tester agent, and a heterogeneous large language model is used to instantiate a critic agent; the method comprises: Obtain the task requirements for software development or code generation to be solved; The analyst agent analyzes the task requirements according to the requirements analysis prompt to obtain an initial requirements document; The critic agent verifies the requirement document in batches according to the requirement verification prompt, and the critic agent and the analyst agent verify and correct parts of the initial requirement document in an interactive manner, thereby improving the requirements in stages; The programmer agent simultaneously receives local test feedback from the tester agent during the process of writing initial code according to coding prompts and verifying the revised requirement document until all tasks in the revised requirement document are completed and code generation is completed.
9. A code generation device based on heterogeneous multi-module collaboration, characterized in that: The device comprises: an analyst module, a programmer module and a tester module instantiated using a homogeneous large language model, and a critic module instantiated using a heterogeneous large language model; The analyst module is used to deeply understand and analyze the task requirements of software development or code generation to be solved according to the requirements analysis prompts, decompose the task requirements into several subtasks and sort them according to priority, and at the same time give several high-level key implementation steps based on the subtasks, output the requirements document, and feed back the requirements document to the critic module; it is also used to revise the requirements document according to the verification report fed back by the critic module to make it better meet the task requirements. The critic module is used to verify the requirement document according to the requirement verification prompt, and if any problem is found, output a verification report and feed it back to the analyst module; The programmer module is used to write the initial code according to the key plans and subtasks of the coding prompts after the requirement document has been revised by the critic module and the analyst module, and to perform self-check on the initial code line by line to correct potential syntax errors and logical omissions; and is also used to iteratively correct the code according to the test report fed back by the tester module; The tester module is used to design test functions and test cases according to the test prompts and initial codes, and to merge the codes with the test functions for execution. After executing all the test cases, the results are analyzed, and a test report is output, which is then fed back to the programmer module until all the test cases pass or the preset upper limit of the number of iterations is reached. The test report includes the failed test cases, the reasons for the failure, and suggestions for improvement.
10. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, it implements the code generation method based on heterogeneous multi-agent collaboration described in any one of claims 1 to 7.
Citation Information
Patent Citations
Personalized complex report generation method based on multi-agent system
CN118569237A
Intelligent demand structuring method and system and application thereof
CN119225693A
Cited By
Code generation method and device based on multiple agents, medium and program product
CN120315688A
Multi-agent based code generation method, device, medium and program product
CN120315688B
Aviation airborne software-oriented large model driven agent collaborative code generation method
CN120762648A
Code generation method and system based on large model
CN120763072A
Test case generation method and system, computer system, readable medium and program product
CN120821672A