Code generation method, system and electronic device based on heterogeneous multi-agent collaboration

By verifying the consistency and feasibility of the requirement document through the critic agent in the heterogeneous multi-agent collaboration framework, the problems of inconsistent task planning and cognitive bias in the existing technology are solved, the early correction of requirement deviations is achieved, and the quality and efficiency of code generation are improved.

CN119829038BActive Publication Date: 2025-09-23NAT UNIV OF DEFENSE TECH
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510079265.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-01-17
Publication Date
2025-09-23
Estimated Expiration
2045-01-17

AI Technical Summary

Technical Problem

Existing multi-agent collaboration frameworks lack a consistency verification mechanism for task requirements in code generation, and homogeneous large language models have cognitive biases, leading to incorrect task planning and waste of resources.

Method used

A heterogeneous multi-agent collaboration method is adopted, and a critic agent instantiated by a heterogeneous large language model is introduced to verify the consistency and feasibility of the requirement document. Through a closed-loop process of requirement analysis, verification, coding and testing, the accuracy and feasibility of the requirement document are ensured.

Benefits of technology

Discover and correct requirement deviations in the early stages, reduce the cost of later corrections, improve the robustness and reliability of code generation, reduce project risks, and improve software development efficiency and code quality.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119829038B_ABST
    Figure CN119829038B_ABST
Patent Text Reader

Abstract

This application relates to a code generation method, system, and electronic device based on heterogeneous multi-agent collaboration. The method incorporates a specialized critic agent instantiated as a heterogeneous large language model into the multi-agent collaboration process. The critic agent verifies the consistency and feasibility of the requirements document provided by the analyst agent, and proposes modification suggestions at an early stage. This allows deviations and problems to be discovered early in the task, reducing the cost and complexity of later revisions and improving the robustness and reliability of the code. This ensures that the generated results maintain a high level of correctness even in extreme situations or specific boundary conditions. This method has practical significance for improving software development efficiency, reducing project risks, and enhancing the quality of intelligent automation solutions.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of code generation technology, and in particular to a code generation method, system and electronic device based on heterogeneous multi-agent collaboration. Background Art

[0002] In recent years, with the significant progress of Large Language Models (LLMs) in fields such as natural language understanding and code generation, researchers have begun exploring the construction of multi-agent frameworks that leverage multiple LLMs to collaboratively tackle complex tasks. Such frameworks consist of multiple interacting agents (typically instantiated LLMs) supplemented by auxiliary components. These agents can perform roles such as analysis, design, implementation, and testing. By dividing and collaborating within the task flow, they effectively alleviate the limitations of a single LLM in handling long contexts, complex reasoning, or multi-step decision-making. Examples include the MetaGPT multi-agent framework and Devin.

[0003] Recent research has shown that multi-agent collaboration can, to some extent, offset the shortcomings of a single LLM. For example, effectively leveraging teamwork to solve highly complex code generation problems. By dividing roles, the cognitive burden of a single LLM handling extensive contexts and complex logic can be reduced. Leveraging post-verification methods such as test agents can improve the accuracy and stability of the final code. However, these existing frameworks still have significant shortcomings in the requirements analysis and verification phases, lacking a mechanism for independent verification of task plans early in the project.

[0004] In existing multi-agent collaboration frameworks based on large language models (LLMs), roles such as analysts, architects, developers, and testers are primarily instantiated using homogeneous large language models. Although these approaches have achieved some success in the field of automated code generation, they still have the following major drawbacks when dealing with complex and ambiguous code generation tasks:

[0005] (1) Lack of consistency verification mechanism for task requirements. Existing multi-agent approaches often rely too much on the plans of the analyst agent and fail to effectively verify whether these plans are consistent with the task requirements. This may lead to problems such as the analyst misunderstanding the task requirements, generating incorrect task plans, and guiding other agents to perform incorrect tasks, resulting in incorrect solutions and unnecessary waste of resources.

[0006] (2) Homogeneous large language models present cognitive biases. Current multi-agent frameworks assign 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 promptly detect and correct cognitive biases that arise in team members during task decision-making, and may even further amplify these biases, causing the team's goals to deviate from expectations. Summary of the Invention

[0007] Therefore, it is necessary to provide a code generation method, system, and electronic device based on heterogeneous multi-agent collaboration to address the above technical issues. This method effectively addresses the above technical issues by introducing a requirements verification mechanism in the early stages of a project to review the team's planning. Furthermore, through the role division and data verification of diversified agents, it improves the accuracy and robustness of multi-agent collaboration.

[0008] A 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:

[0009] Obtain the software development or code generation task requirements to be solved.

[0010] The analyst agent analyzes the task requirements according to the requirements analysis prompts and generates a requirements document.

[0011] The critic agent verifies the requirement document according to the requirement verification prompts and obtains a verification report.

[0012] If the verification report indicates that there are problems with the requirements document, the analyst agent will modify the requirements document based on the suggestions of the critic agent.

[0013] The programmer agent writes the initial code based on the coding prompts and verifies the revised requirements document.

[0014] The tester agent designs test functions and test cases based on the test prompts and the initial code, integrates the initial code with the test function and executes it to generate a test report.

[0015] 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.

[0016] In one implementation use case, the analyst agent analyzes the task requirements based on the requirements analysis prompts and generates a requirements document, including:

[0017] The analyst agent first fully understands the task requirements and decomposes the overall task into several subtasks based on the requirements analysis prompts.

[0018] 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 requirements to the deliverable results.

[0019] 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.

[0020] In one embodiment, the critic agent verifies the requirement document according to the requirement verification prompt and obtains a verification report including:

[0021] 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.

[0022] 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 will make modification suggestions in the verification report.

[0023] In one embodiment, if the verification report indicates that there are problems with the requirements document, the analyst agent modifies the requirements document based on the suggestions of the critic agent, including:

[0024] If the verification report indicates issues with the requirements document, the analyst agent updates the subtask definitions and adjusts key implementation steps based on the recommendations in the verification report to better meet the task requirements, generating an updated requirements document. If the verification report indicates the requirements document is correct, the analyst agent does not make any revisions.

[0025] In one embodiment, a programmer agent writes initial code based on coding prompts and verified revised requirements documents, including:

[0026] The programmer agent refers to the subtasks and high-level plans specified in the updated requirements document and converts them into specific code implementations.

[0027] After the code is written, the programmer agent checks the code line by line to correct possible syntax errors or obvious logical loopholes.

[0028] In one embodiment, the tester agent designs a test function and a test case based on the test prompt and the initial code, and integrates the initial code with the test function for execution, including:

[0029] 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.

[0030] 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.

[0031] In one embodiment, the programmer agent revises the initial code based on the test report and hands it over to the tester agent for execution again until the process termination condition is met, thereby completing code generation, including:

[0032] The programmer agent refers to the failed cases in the test report to locate logical errors or implementation defects in the code and make code corrections.

[0033] After the code correction is completed, the tester agent continues to retest the revised code. If all test cases are passed, the process terminates and the code test 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 are still not passed, the process terminates and the code generation is completed.

[0034] 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:

[0035] Obtain the software development or code generation task requirements to be solved.

[0036] The analyst agent analyzes the task requirements according to the requirements analysis prompts and obtains the initial requirements document.

[0037] The critic agent verifies the requirement documents in batches according to the requirement verification prompts. The critic agent and the analyst agent interactively verify and correct parts of the initial requirement documents, thereby improving the requirements in stages.

[0038] The programmer agent receives local test feedback from the tester agent while writing code according to coding prompts and verifying the revised requirement document until all tasks in the verified revised requirement document are completed and code generation is completed.

[0039] 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.

[0040] The analyst module is used to deeply understand and analyze the task requirements of software development or code generation to be solved based on the requirements analysis prompts, decompose the task requirements into several subtasks and sort them according to priority, and at the same time provide 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 based on the verification report fed back by the critic module.

[0041] 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.

[0042] 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. The Programmer Module also performs line-by-line self-checks on the initial code to correct potential syntax errors and logical omissions. The Programmer Module is also used to iteratively correct the code based on the test report feedback from the Tester Module.

[0043] The tester module is used to design test functions and test cases based on test prompts and initial codes, and merge the initial codes with test functions for execution. After executing all test cases, the test results are analyzed and a test report is output. The test report is then 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.

[0044] 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:

[0045] Obtain the software development or code generation task requirements to be solved.

[0046] The analyst agent analyzes the task requirements according to the requirements analysis prompts and generates a requirements document.

[0047] The critic agent verifies the requirement document according to the required verification prompts and obtains a verification report.

[0048] If the verification report indicates that there are problems with the requirements document, the analyst agent will modify the requirements document according to the critic agent's suggestions. If the verification report indicates that the requirements document is correct, the analyst agent will not make any revisions.

[0049] The programmer agent writes the initial code based on the coding prompts and verifies the revised requirements document.

[0050] The tester agent designs test functions and test cases based on the test prompts and the initial code, integrates the initial code with the test function and executes it to generate a test report.

[0051] The programmer agent revises and iterates the initial code based on the test report until the process termination conditions are met, completing code generation.

[0052] The aforementioned code generation method, system, and electronic device based on heterogeneous multi-agent collaboration incorporates a specialized critic agent instantiated from a heterogeneous large language model into the multi-agent collaboration process. The critic agent verifies the consistency and feasibility of the requirements document provided by the analyst agent and suggests modifications at an early stage, identifying deviations and issues early in the task. This reduces the cost and complexity of later corrections, improves the robustness and reliability of the code, and ensures that the generated results maintain a high level of correctness even in extreme situations or specific boundary conditions. This method has practical significance for improving software development efficiency, reducing project risks, and enhancing the quality of intelligent automation solutions. BRIEF DESCRIPTION OF THE DRAWINGS

[0053] Figure 1 1 is a flow chart of a code generation method based on heterogeneous multi-agent collaboration in one embodiment;

[0054] Figure 2 A multi-agent framework diagram in one embodiment;

[0055] Figure 3 FIG. 1 is a diagram showing the internal structure of an electronic device in one embodiment. DETAILED DESCRIPTION

[0056] In order to make the purpose, technical solutions and advantages of this application more clear, the following further describes this application in detail with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain this application and are not intended to limit this application.

[0057] A Large Language Model (LLM) is a large-scale model trained through deep learning that can understand and generate natural language text. This model can understand the meaning of user input based on context and generate reasonable text output.

[0058] A multi-agent collaboration framework combines multiple agents with different functions or roles (in this case, role modules based on LLM) into a single system. Each agent collaborates with others based on their role responsibilities to jointly complete complex tasks.

[0059] In one embodiment, Figure 1 As shown, a code generation method based on heterogeneous multi-agent collaboration is provided. The homogeneous large language model is used to instantiate the analyst agent, programmer agent, and tester agent, and the heterogeneous large language model is used to instantiate the critic agent. 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 the homogeneous large language model. Each agent clarifies its own responsibilities and input and output formats through prompt engineering. The method includes the following steps:

[0060] Step 100: Obtain the task requirements for software development or code generation to be solved.

[0061] Specifically, the task requirements for software development or code generation to be solved are obtained from the user (or upstream module). These requirements are usually in natural language form and include functional descriptions, input and output format requirements, performance indicators or boundary conditions, etc.

[0062] Step 102: The analyst agent analyzes the task requirements according to the requirements analysis prompts and generates a requirements document.

[0063] Specifically, the Analyst Agent deeply understands and analyzes the input task requirements, breaking down the complex task requirements into several subtasks, ranking and prioritizing each subtask, and then providing 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 the basis for subsequent verification and implementation.

[0064] A requirements document is a document that describes the software requirements, including a prioritized list of subtasks and key implementation steps. This document provides a solid foundation for subsequent coding and testing.

[0065] Step 104: The critic agent verifies the consistency and feasibility of the requirement document according to the requirement verification prompts and generates a verification report.

[0066] Specifically, the Critic agent verifies the requirements document generated by the Analyst agent based on the requirements verification prompts. By reverse-engineering each subtask in the requirements document, it infers the expected requirements and determines whether they align with the original task requirements. Simultaneously, it performs a illustrative logical verification of the high-level plan from a practical feasibility perspective to check the Analyst agent's understanding of the requirements. If any issues are identified, the Critic agent generates a Verification Report, providing targeted modification suggestions.

[0067] Verification work includes consistency and feasibility assessments, which identify potential issues. Consistency assessments compare the logic of subtasks in the requirements document with the original task requirements. If the subtasks are inconsistent with the original task requirements, the requirements document must be revised. Feasibility assessments logically analyze the high-level plans described in the requirements document to determine their technical feasibility and implementability.

[0068] The Verification Report is a feedback document produced by the critic agent, which points out problems and deviations in the requirements document and provides modification suggestions.

[0069] 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, promptly identifying and correcting blind spots and deviations in requirements understanding. This reduces the cost of subsequent revisions at an early stage and improves development efficiency.

[0070] Enhance team robustness and deviation correction capabilities: By introducing different LLMs into the team, the system is equipped with heterogeneous thinking methods, enabling 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 final solution's inadaptability to special use cases. By introducing a 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 code output quality and robustness, thereby effectively overcoming the technical shortcomings of the existing technology.

[0071] In the traditional software development process, requirements analysis and verification are critical early steps. If requirements are misunderstood, missing, or have significant deviations, subsequent coding and testing will be performed under incorrect conditions, increasing project resource consumption and the risk of failure. This multi-agent framework introduces a critic agent to verify the requirements document during the requirements analysis phase. The verification report output by the critic agent is returned to the analyst agent for updating and revising the requirements document (RD), thereby promptly correcting requirements deviations before the coding and testing phases. This closed-loop process of "requirements analysis → verification → revision" aims to detect deviations and problems in the requirements phase early, avoiding the need to invest large amounts of resources later to remedy errors in the requirements phase. This reduces the amount of code rework from a technical perspective, improving development efficiency and stability.

[0072] The critic agent and the analyst agent are derived from different model architectures or training methods, resulting in distinct cognitive and judgment criteria. This heterogeneous agent (the critic agent) verifies the consistency and feasibility of the requirements document provided by the analyst agent and suggests modifications at an early stage, thereby reducing subsequent development resource waste and the risk of error propagation.

[0073] 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.

[0074] Specifically, if the critic agent's verification report points out problems in the requirement document, the analyst agent will modify the requirement document based on the critic agent's suggestions.

[0075] 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.

[0076] Step 108: The programmer agent writes initial code based on the coding prompts and verified revised requirements document.

[0077] Specifically, based on the requirements document verified by the critic agent and revised by the analyst agent, the programmer agent implements preliminary code according to key plans and subtasks. The programmer agent then performs line-by-line self-checks on the generated code to correct potential syntax errors or logical omissions.

[0078] 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.

[0079] Specifically, the tester agent designs appropriate test functions and test cases based on the initial code and test prompts, and then executes the code and test functions together. The test cases are formatted as assertion statements. After executing all test cases, the tester agent analyzes the results and outputs a test report that lists any failed test cases and the reasons for the failure.

[0080] A test report is generated 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.

[0081] Prompt Engineering: The process of designing and optimizing the text prompts fed into a large language model. By adjusting the content and format of prompts, the model can more accurately understand instructions, roles, and data formats, ensuring that the agent can perform the intended task.

[0082] Step 112: The programmer agent revises the initial code based on 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.

[0083] 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 based on the test report and execute the test steps again.

[0084] The aforementioned code generation method based on heterogeneous multi-module collaboration incorporates a specialized critic agent instantiated from a heterogeneous large language model into the multi-agent collaborative process. The critic agent verifies the consistency and feasibility of the requirements document generated by the analyst agent and suggests modifications at an early stage, identifying deviations and issues early in the task. This reduces the cost and complexity of later corrections, improves the robustness and reliability of the code, and ensures that the generated results maintain a high level of correctness even in extreme situations or specific boundary conditions. This has practical implications for improving software development efficiency, reducing project risks, and enhancing the quality of intelligent automation solutions.

[0085] The framework of this method is not limited to specific task types or language models. It can flexibly expand and replace different LLMs 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 process of understanding text and extracting key information is used as the analyst task, and the critic agent verifies the extracted key points, thereby reducing the subsequent text classification or summary generation error. (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 tester agent perform control logic programming and testing. Regardless of the application field it is extended to, its core idea remains the same: verification and correction are performed in the early stages of the task to reduce expensive rework in the later stages.

[0086] In one embodiment, step 102 includes: the analyst agent first fully understands the task requirements, and decomposes the overall task into several subtasks based on 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 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.

[0087] 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 traditional solutions, 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 continuing 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.

[0088] The Analyst Agent breaks down complex requirements into multiple subtasks and provides several high-level key steps. This structured requirements document allows the Critic, Programmer, and Tester Agents to clearly understand and utilize it, enabling collaborative work with clear division of labor. This hierarchical, structured requirements description facilitates rapid problem identification and optimization solutions during verification and implementation.

[0089] 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 will point 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.

[0090] Specifically, the Critic's verification work primarily involves consistency assessment and feasibility evaluation. The consistency assessment process involves inferring the expected requirements by performing reverse reasoning on each subtask in the requirements document and determining whether they align with the original task requirements. Meanwhile, the feasibility assessment process involves performing exemplary logical verification of high-level plans from a practical perspective to verify the Analyst's understanding of the requirements.

[0091] This invention introduces a "Critic" role after the requirements analysis is completed, and uses a 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 triggers a series of improvements in technical logic:

[0092] First, demand deviations are identified and corrected before they are passed on to programming and testing, effectively curbing the waste of resources caused by the spread of errors in the later stages.

[0093] 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.

[0094] As the requirements document becomes more accurate and complete through repeated verification and revision in the early stages, the subsequent work of programmers and testers 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.

[0095] 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.

[0096] In summary, the advantage of the present invention lies in its clear presentation through logical deduction: the innovative link of introducing heterogeneous LLMs for early demand verification in a multi-agent collaborative system effectively reduces the technical cost and time consumption caused by demand deviations, and enhances the team's resilience and reliability in dealing with complex problems, thereby making the overall development process more efficient and quality-assured.

[0097] In one embodiment, step 106 includes: if the verification report indicates issues with the requirements document, the analyst agent updates subtask definitions and adjusts key implementation steps based on the recommendations in the verification report to better meet the task requirements, thereby generating an updated requirements document that 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.

[0098] In one embodiment, step 108 includes: the programmer agent refers to the subtasks and high-level plans that have been defined 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.

[0099] 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 modification cost.

[0100] 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.

[0101] In one embodiment, step 112 includes: the programmer agent refers to the failed case in the test report, locates the logical errors or implementation defects 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 terminates 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 a preset value and all test cases are still not passed, the process terminates and the code generation is completed.

[0102] Preferably, the preset value of the number of iterations is set to 4 to avoid infinite loops.

[0103] 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.

[0104] The analyst agent first decomposes the task into several subtasks (such as data cleaning, feature extraction, and statistical analysis) based on the user description, and gives a high-level implementation plan to generate a requirements document.

[0105] After receiving the requirements document, the critic agent discovered that the analyst agent had interpreted the "statistical analysis" subtask as a simple summation, overlooking the more complex statistics (such as distribution analysis or regression models) actually required by the user. The critic agent identified this issue in the verification report and provided suggested changes (for example, requiring the analyst to add more details to the statistical analysis subtask and align it with the user's initial requirements).

[0106] After receiving the verification report from the critic agent, the analyst agent revises the requirements document, redefines the subtasks (analysis or regression model), improves the key plans to better meet the task requirements, and generates an updated requirements document.

[0107] After the revisions, the programmer agent implements the code according to the new requirements document. The tester agent then tests the code, verifying it using various boundary conditions and extreme datasets to ensure logical robustness. If the code fails under certain inputs, the tester agent generates a test report, prompting the programmer agent to correct the code until it passes all test cases or reaches the maximum number of iterations.

[0108] 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.

[0109] This method uses an LLM (Large Language Model) as the basis for the agent, but different natural language processing (NLP) or artificial intelligence models can also be used as substitutes for implementation. As long as the model used can understand, decompose, verify, and generate code, the purpose of the invention can be met. For example, a multimodal model (capable of understanding text, graphics, and other data) can be used as an analyst agent or critic agent to verify requirements in more complex scenarios. Traditional logical reasoning engines and formal verification tools can be used to supplement the verification of the requirements given by the analyst agent. In this case, the critic agent can be implemented by combining the LLM and formal tools, thus forming a heterogeneous verification system.

[0110] In this method, the names of the agents are determined based on 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 functionality. For example, the Critic can be merged into a "Requirements Quality Reviewer" role. In addition to consistency and feasibility verification, this role can also be responsible for risk prediction and reallocation of requirements priorities. An "Architecture Designer" role can be added to take on the tasks of architecture design and abstraction 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 ultimate goals.

[0111] In one embodiment, a second code generation method based on heterogeneous multi-agent collaboration is proposed. The method 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:

[0112] Step 200: Obtain the task requirements for software development or code generation to be solved.

[0113] Step 202: The analyst agent analyzes the requirements according to the requirements analysis prompts and generates an initial requirements document.

[0114] 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.

[0115] Step 206: The programmer agent synchronously receives partial test feedback from the tester agent during the process of writing code according to the verified and revised requirement document until all tasks in the verified and revised requirement document are completed and code generation is completed.

[0116] Specifically, the process described in steps 100 to 112 is: requirements analysis (Analyst) → requirements verification (Critic) → requirements modification (Analyst) → coding (Programmer) → testing (Tester) → iteration. In this embodiment, the requirements analysis and verification phases are interleaved: after the Analyst submits an initial draft design (RD), the Critic agent does not provide comprehensive feedback all at once. Instead, the Critic agent interactively verifies and corrects parts of the RD in multiple rounds of dialogue, thereby refining the requirements in stages. This embodiment still achieves the goal of identifying and correcting deviations from requirements early, but the verification process is more iterative, fine-grained, and dynamic. The testing (Tester) phase is pre-embedded in the coding process: while the Programmer agent is writing code, the Tester agent receives localized feedback from the Tester agent. This alternative continuous integration and continuous testing (CI / CD) approach can accelerate code quality improvement and achieve similar technical effects to the present invention's goal of improving final code quality and robustness.

[0117] 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 detection and elimination of demand deviations and enhancing code quality and robustness. For example:

[0118] (1) Verify requirements using multi-stage expert systems and formal methods.

[0119] The LLM-based Critic was replaced with a logical rule-based expert system (Expert System) and formal verification tools. Initially, after the Analyst generates a design revision (RD), the Expert System uses predefined rules and constraints to conduct a preliminary check of the RD, identifying defects against the format and quality requirements of the standard Software Requirements Specification (SRS). Formal verification tools (such as Alloy and Z3) are then used to model and verify key requirements (such as input and output constraints). When the tool discovers inconsistencies or infeasibility, the Analyst modifies the RD based on the findings. This solution aligns with the original solution in terms of its technical objectives: reducing deviations and resource waste in subsequent development through early verification.

[0120] (2) Introducing a multi-mode collaborative verification team.

[0121] In addition to the LLM role, statistical model-based keyword matchers, deep learning-based named entity recognition models, and independent performance estimators (through simulation runs or time complexity analysis tools) can also be used to verify requirements across multiple dimensions. This allows for the collaborative achievement of consistency, feasibility, and robustness verification through multiple tools, even without employing the Critic as the sole role.

[0122] 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. In addition, 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.

[0123] In one embodiment, a code generation device based on heterogeneous multi-module collaboration is provided, which 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; wherein,

[0124] The analyst module is used to deeply understand and analyze the task requirements of software development or code generation to be solved based on the requirements analysis prompts, decompose the task requirements into several subtasks and sort them according to priority, and at the same time provide 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 based on the verification report fed back by the critic module.

[0125] 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.

[0126] 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. The Programmer Module also performs line-by-line self-checks on the initial code to correct potential syntax errors and logical omissions. The Programmer Module is also used to iteratively correct the code based on the test report feedback from the Tester Module.

[0127] The tester module is used to design applicable test functions and use 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 speed test cases, reasons for failure and improvement suggestions.

[0128] The multi-agent framework uses multiple roles instantiated by a Large Language Model (LLM) to collaborate. Each role clarifies its responsibilities and input and output formats through prompt engineering. By analyzing requirements (Analyst role), verifying (Critic role), coding (Programmer role), and testing (Tester role), the framework identifies and corrects requirements deviations at an early stage, thereby improving the accuracy and robustness of code generation.

[0129] In one embodiment, the analyst module is further 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; and is also used to generate a requirements document based on the output template; the requirements document includes: a subtask list with priorities, key steps and corresponding instructions.

[0130] In one embodiment, the critic verification module is also used to perform reverse reasoning on 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.

[0131] In one embodiment, if the verification report indicates issues with the requirements document, the analyst module is further configured to update subtask definitions and adjust key implementation steps based on the recommendations in the verification report to better meet the task requirements, thereby generating an updated requirements document. If the verification report indicates that the requirements document is correct, the analyst module does not make any revisions.

[0132] In one embodiment, the programmer module 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 module checks the code line by line to correct possible syntax errors or obvious logical loopholes.

[0133] In one embodiment, the tester module is also used to run 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.

[0134] In one embodiment, the programmer module is also used to refer to the failed cases in the test report, locate logical errors or implementation defects in the code, and perform code corrections; after the code correction is completed, the tester module 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 a preset value and all test cases are still not passed, the process is terminated and the code generation is completed.

[0135] Regarding 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 implemented in whole or in part 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 below. Figure 3As shown. The electronic device includes a processor, a memory, a network interface, a display screen and an input device connected via a system bus. 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 via 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 provided on the housing of the electronic device, or an external keyboard, touchpad or mouse, etc.

[0136] Those skilled in the art will understand that Figure 3 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the electronic device to which the solution 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 component arrangement.

[0137] In one embodiment, an electronic device is provided, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the steps in the above-mentioned embodiment of the code generation method based on heterogeneous multi-module collaboration are implemented.

[0138] The technical features of the above embodiments can be combined arbitrarily. 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.

[0139] The above-described embodiments merely represent several implementation methods of the present application. While the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the present application. It should be noted that a person of ordinary skill in the art may make various modifications and improvements without departing from the spirit of the present application, and these modifications and improvements fall within the scope of protection of the present application. Therefore, the scope of protection of the present application shall be determined by the appended 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 includes: 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 prompts and generates a requirements document; wherein the process of analyzing the task requirements includes: the analyst agent first comprehensively understands the requirements and decomposes the overall task into a number of subtasks according to the requirements analysis prompts; the analyst agent sorts and prioritizes each subtask and designs a number of high-level key steps for the subtasks; the key steps describe the logical path from the starting requirements to the deliverable results; the analyst agent generates a requirements document according to the output template; the requirements document includes: a subtask list with priorities, key steps and corresponding instructions; The critic agent verifies the requirements document according to the requirements verification prompt and obtains a verification report. The verification process of the requirements document includes: the critic agent performs reverse reasoning on each subtask in the requirements document to infer the expected requirements and determine 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 is not logically rigorous, lacks feasibility, or has deviations, the critic agent proposes modification suggestions in the verification report. If the verification report indicates that there are problems with the requirements document, the analyst agent modifies the requirements document according to the suggestions of the critic agent. The process of modifying the requirements document includes: if the verification report indicates that there are problems with the requirements document, the analyst agent updates the subtask definitions and adjusts the key implementation steps according to the suggestions in the verification report to better meet the task requirements, thereby generating an updated requirements document; if the verification report indicates that the requirements document is correct, the analyst agent does not revise the requirements document. The programmer agent writes initial code based on coding prompts and verified revised requirements documents; The tester agent designs test functions and test cases based on 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, completing the code generation.

2. 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 verifying 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.

3. 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 test case based on 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, failure reasons and suggested improvement directions.

4. 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 based on 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 test cases in the test report to locate the logical 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 terminates and the code test 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 are still not passed, the process terminates and the code generation is completed.

5. 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 includes: Obtain the task requirements for software development or code generation to be solved; The analyst agent analyzes the task requirements according to the requirement analysis prompt using the process of analyzing the task requirements according to claim 1 to obtain an initial requirement document; The critic agent verifies the requirement document in batches according to the requirement verification prompt. The critic agent and the analyst agent interactively verify and correct parts of the initial requirement document using the process of verifying and modifying the requirement document as described in claim 1, thereby improving the requirement in stages. The programmer agent synchronously 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 verified revised requirement document are completed and code generation is completed.

6. A code generation device based on heterogeneous multi-agent collaboration, characterized in that: The device implements code generation based on heterogeneous multi-agent collaboration using the code generation method based on heterogeneous multi-agent collaboration according to claim 1; 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 conduct an in-depth understanding and analysis of the software development or code generation task requirements to be solved based on the requirements analysis prompts, decompose the task requirements into several subtasks and sort them according to priority, and provide several high-level key implementation steps based on the subtasks, output a requirements document, and feed the requirements document back to the critic module; it is also used to revise the requirements document based on the verification report fed back by the critic module to make it better meet the task requirements; The reviewer module is used to verify the requirement document according to the requirement verification prompts, and if any problems are found, output a verification report and feed it back to the analyst module; The programmer module is used to write initial code according to the coding prompts, key plans and subtasks, based on the requirements document that has been reviewed by the critic 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; and is also used to iteratively correct the code based on the test reports fed back by the tester module; The tester module is used to design test functions and test cases based on test prompts and initial codes, and merge the codes with test functions for execution. After executing all test cases, the test results are analyzed and a test report is output. The test report is then 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.

7. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the code generation method based on heterogeneous multi-agent collaboration described in any one of claims 1 to 4 is implemented.

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