Code generation method and device, computer program product and electronic equipment

By performing fault checking and iterative repair on the initial code and combining it with the code improvement knowledge graph to generate a repair strategy, the problem of insufficient code generation accuracy in the existing technology is solved, and more efficient and accurate code generation is achieved.

CN120803424APending Publication Date: 2025-10-17CHINA TELECOM CORP LTD TECHNOLOGY INNOVATION CENTER +1
View PDF 0 Cites 4 Cited by

Patent Information

Application Number
CN202510934335.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-07
Publication Date
2025-10-17

AI Technical Summary

Technical Problem

The accuracy of code generation in the existing technology is low and there is a lack of targeted optimization mechanisms, which results in poor stability and accuracy of the generated code.

Method used

By generating initial code based on a pre-trained code generation model, code fault checking and fault repair are performed, and a repair strategy is generated using the code improvement knowledge graph. The repair code is iteratively repaired until the preset conditions are met and the target code is generated.

Benefits of technology

The accuracy and efficiency of code generation are improved, ensuring that the generated code meets the requirements while reducing unnecessary iterative repairs, and improving the stability and efficiency of code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120803424A_ABST
    Figure CN120803424A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of computers, and provides an agent generation method and device, a computer program product and electronic equipment. The method comprises the steps of generating an initial code corresponding to code task description information based on a pre-trained code generation model; code inspection is conducted on the initial code, when the inspection result indicates that the initial code has a code fault, the initial code is repaired based on the code fault, and a repaired code is generated; performing code logic error checking on the repair code, generating a repair strategy of the repair code based on the code improvement knowledge graph under the condition that a logic error checking result indicates that the repair code has a logic error, and inputting the repair strategy to the code generation model; enabling the code generation model to improve the repair code based on the repair strategy and the code task description, updating the repair code according to an improvement result, and iteratively executing the process until a preset condition is met, so as to obtain a target code. The code generation accuracy can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates to the technical field of computer, and in particular, to a code generation method, a code generation apparatus, a computer program product and an electronic device. BACKGROUND

[0002] Code generation refers to automatically generating code conforming to natural language description based on artificial intelligence, natural language processing and the like, thereby providing assistance for software developers.

[0003] In the related art, although a large language model can be used to automatically generate code, the accuracy of the generated code is low.

[0004] It should be noted that the information disclosed in the above background section is only used to strengthen the understanding of the background of the present disclosure, and therefore can include information that does not constitute prior art known to those of ordinary skill in the art. SUMMARY

[0005] The purpose of the present disclosure is to provide a code generation method and apparatus, a computer program product and an electronic device, thereby at least to some extent improving the accuracy of code generation.

[0006] Other characteristics and advantages of the present disclosure will become apparent from the following detailed description, or will be learned by practice of the present disclosure.

[0007] According to a first aspect of the present disclosure, a code generation method is provided, comprising: generating an initial code corresponding to code task description information based on a pre-trained code generation model; performing code fault checking on the initial code, and in a case where a fault checking result indicates that the initial code has a code fault, performing fault repair on the initial code based on the code fault to generate a repaired code; performing code logic error checking on the repaired code, and in a case where a logic error checking result indicates that the repaired code has a logic error, generating a repair strategy for the repaired code based on a pre-generated code improvement knowledge graph, inputting the repair strategy to the code generation model, so that the code generation model improves the repaired code based on the repair strategy and the code task description information, and updates the repaired code according to an improvement result, iteratively performing the process until a preset condition is met, and obtaining a target code corresponding to the code task description information according to the repaired code that meets the preset condition; wherein the preset condition includes reaching a first preset iteration number or the logic checking result indicating that the current improved repaired code does not have a logic error.

[0008] According to a second aspect of the present disclosure, a code generation apparatus is provided, comprising: an initial code generation module configured to generate an initial code corresponding to code task description information based on a pre-trained code generation model; an initial code repair module configured to perform code fault checking on the initial code, and in a case where a checking result indicates that the initial code has a code fault, repair the initial code based on the code fault to generate a repaired code; an iterative repair module configured to perform code logic error checking on the repaired code, and in a case where a logic error checking result indicates that the repaired code has a logic error, generate a repair strategy for the repaired code based on a pre-generated code improvement knowledge graph, input the repair strategy into the code generation model, so that the code generation model improves the repaired code based on the repair strategy and the code task description information, and updates the repaired code according to an improvement result, iteratively performs the process until a preset condition is met, and obtains a target code corresponding to the code task description information according to the repaired code that meets the preset condition; wherein the preset condition comprises reaching a first preset iteration number or the logic checking result indicating that the current improved repaired code has no logic error.

[0009] According to a third aspect of the present disclosure, a computer program product containing instructions which, when executed on a computer, cause the computer to perform the steps of the code generation method according to the first aspect is provided.

[0010] According to a fourth aspect of the present disclosure, a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, implements the code generation method according to the first aspect of the above-mentioned embodiments is provided.

[0011] According to a fifth aspect of the present disclosure, an electronic device is provided, comprising: a processor; and a storage device for storing one or more programs which, when executed by the one or more processors, cause the one or more processors to implement the code generation method according to the first aspect of the above-mentioned embodiments.

[0012] According to the above technical solutions, the code generation method, the code generation apparatus, and the computer program product and the electronic device implementing the code generation method according to the exemplary embodiments of the present disclosure have at least the following advantages and positive effects:

[0013] In a technical solution provided in some embodiments of the present disclosure, according to the code task description information, an initial code is generated based on a code generation model, and then code fault checking is performed on the initial code. In the case that the initial code has code faults, the initial code can be repaired first to obtain initial repaired code, and then code logic error checking is performed on the repaired code. In the case that the initial repaired code has logic errors, a repair strategy can be generated according to a knowledge graph to iteratively repair the initial repaired code until the code requirement is met or the iteration number is reached, and finally the target code is generated. Compared with related technologies, on the one hand, the present disclosure can improve the accuracy of code generation by iteratively repairing the initial code. On the other hand, the present disclosure first repairs the initial code, and then iteratively repairs the initial code in the case that the repair result has logic errors. In the case that the repair result has no logic errors, the target code can be directly obtained, which ensures the accuracy of the code and improves the efficiency of code generation without iteratively repairing all codes.

[0014] It should be understood that the foregoing general description and the following detailed description are only exemplary and explanatory, and are not limiting to the present disclosure. BRIEF DESCRIPTION OF DRAWINGS

[0015] The accompanying drawings, which are incorporated into and form part of the specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure. Obviously, the drawings in the following description are only some embodiments of the present disclosure, and other drawings can be obtained by those skilled in the art without creative labor on the basis of these drawings.

[0016] Figure 1 A schematic diagram of an exemplary system architecture to which embodiments of the present disclosure can be applied is shown;

[0017] Figure 2 A flowchart of a code generation method in an exemplary embodiment of the present disclosure is shown;

[0018] Figure 3 A flowchart of a method for repairing an initial code in an exemplary embodiment of the present disclosure is shown;

[0019] Figure 4 A flowchart of a method for generating a repair strategy in an exemplary embodiment of the present disclosure is shown;

[0020] Figure 5 A composition diagram of a code generation system in an exemplary embodiment of the present disclosure is shown;

[0021] Figure 6A flowchart showing another code generation method in an example embodiment of the present disclosure is shown.

[0022] Figure 7 A composition diagram of a code generation apparatus in an example embodiment of the present disclosure is shown.

[0023] Figure 8 A structure diagram of an electronic device in an example embodiment of the present disclosure is shown. DETAILED DESCRIPTION

[0024] Example implementations will now be described more fully with reference to the accompanying drawings. Example implementations can be implemented in any

[0025] The terms "one", "an", "the" and "said" are used to mean one or more elements, components, parts, etc.; the terms "comprises", "comprising", "has", "having" and "includes" are used to mean including, but not limited to, the listed elements, components, parts, etc.; the term "first", "second", "third", etc. are used to mean different or additional elements, components, parts, etc. and are not used to mean "one", "an", "the" or "said".

[0026] In addition, the accompanying drawings are included to provide a further understanding of the present disclosure and are incorporated in and constitute a part of this specification, illustrate embodiments of the present disclosure and serve to explain the principles of the present disclosure. The same reference numbers are used throughout the drawings to represent the same or similar parts. Descriptions of the drawings are as follows:

[0027] The code generation method in the related art lacks a targeted optimization mechanism, resulting in poor accuracy and stability of code generation.

[0028] To solve the above problems, the present disclosure provides a code generation method and apparatus, which can be applied toFigure 1 In the system architecture of the exemplary application environment shown.

[0029] like Figure 1 As shown, system architecture 100 may include a terminal device 110 and a server 120. Terminal device 110 may be a smartphone, tablet computer, desktop computer, laptop computer, smart wearable device, or other terminal device. Server 120 generally refers to a backend system that provides services related to the code generation method in this exemplary embodiment and may be a single server or a cluster of multiple servers. Terminal device 110 and server 120 may be connected via a wired or wireless communication link to exchange data.

[0030] In an exemplary embodiment, the above-mentioned code generation method can be executed by the terminal device 110. Accordingly, the code generation device can be set in the terminal device 110 to implement the corresponding module function. For example, the user uses the terminal device 110 to input code task description information, the code generation model pre-configured in the terminal device 110 can generate initial code according to the task description information, the code inspection model pre-configured in the terminal device 110 can check the initial code, the code fault repair model pre-configured in the terminal device 110 can repair the syntax errors detected, and jointly with the code generation model to repair complex logical problems to obtain repair code. In the case that the repair code still has logical errors, the repair strategy planning model pre-configured in the terminal device 110 can jointly generate a repair strategy corresponding to the repair code with the code generation model, the code inspection model and the code fault repair model, thereby guiding the code generation model to regenerate code according to the repair strategy until the regenerated code has no logical errors and the final target code is obtained.

[0031] In an exemplary embodiment, the above-described code generation method can be performed by server 120. Accordingly, a code generation device can be provided in server 120 to implement the corresponding module functions. For example, after a user enters a code task description on terminal device 110, terminal device 110 uploads the code task description to server 120, which then generates target code according to the method disclosed herein and returns the generated target code to terminal device 110.

[0032] In an exemplary embodiment, the above-mentioned code generation method can also be jointly performed by the terminal device 110 and the server 120. Correspondingly, part of the modules of the code generation apparatus can be arranged in the terminal device 110, and part of the modules can be arranged in the server 120 to realize the functions of the corresponding modules. For example, the terminal device 110 can be preconfigured with a code generation model and a code checking model, and the server 120 can be preconfigured with a code fault repair model and a repair strategy planning model, so as to generate the target code through the cooperation between the multiple models.

[0033] It should be understood that Figure 1 The number of terminal devices and servers in the system is only illustrative. According to the needs of implementation, there can be any number of terminal devices and servers. For example, the server 120 can be a stand-alone physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDNs, and basic cloud computing services such as big data and artificial intelligence platforms.

[0034] Figure 2 A flowchart of a code generation method in an exemplary embodiment of the present disclosure is shown. Referring to Figure 2 The method comprises:

[0035] Step S210, generating an initial code corresponding to the code task description information based on a pre-trained code generation model;

[0036] Step S220, performing code fault checking on the initial code, and in the case where the fault checking result indicates that the initial code has code faults, performing fault repair on the initial code based on the code faults to generate a repaired code;

[0037] Step S230, performing code logic error checking on the repaired code, and in the case where the logic error checking result indicates that the repaired code has logic errors, generating a repair strategy for the repaired code based on a pre-generated code improvement knowledge graph, inputting the repair strategy to the code generation model, so that the code generation model improves the repaired code based on the repair strategy and the code task description, and updates the repaired code according to the improvement result, iteratively performing the process until a preset condition is met, and obtaining the target code corresponding to the code task description information according to the repaired code that meets the preset condition.

[0038] In Figure 2In the technical solution provided by the illustrated embodiment, the initial code can be generated based on the code generation model according to the code task description information, and then the initial code is subjected to code fault checking. In the case where the initial code has code errors, the initial code can be subjected to fault repair to obtain initial repair code, and then the repair code is subjected to code logic error checking. In the case where the initial repair code has logic errors, the repair strategy can be generated according to the knowledge graph to iteratively repair the initial repair code until the code requirement is met or the iteration number is reached, and the final generated target code is obtained. Compared with related technologies, on the one hand, the accuracy of code generation can be improved by iteratively repairing the initial code; on the other hand, the initial code can be repaired first, and in the case where the fault repair result has logic errors, iterative repair is performed again. In the case where the fault repair result has no logic errors, the initial code after fault repair can be used as the target code, which improves the code generation efficiency while ensuring the accuracy of the code.

[0039] Next, the specific implementation of "step S210, generating initial code corresponding to code task description information based on pre-trained code generation model" will be described in detail.

[0040] In an exemplary embodiment, the pre-trained code generation model can include a code generation model based on a large language model. The large language model refers to a deep learning model trained using a large amount of text data, which can generate natural language text or understand the meaning of language text. The large language model can handle various natural language tasks such as text classification, question answering, and dialogue.

[0041] For example, a plurality of prompt words related to the code generation task can be generated in advance, such as a code task description and a code corresponding to the code task description. The existing large language model is fine-tuned and trained through the prompt word, and the pre-trained code generation model is obtained according to the training result.

[0042] For example, in response to the user input code task description information, the code task description information can be input into the pre-trained code generation model, and the initial code corresponding to the user input code task description information can be obtained according to the pre-trained code generation model.

[0043] In an exemplary embodiment, the pre-trained code generation model can be understood as an intelligent agent based on a large language model. Its main functions can include initial code generation, iterative code regeneration, context preservation, and historical memory.

[0044] For example, the initial code generation can be understood as generating a code generation task related task description information according to the natural language form of user input, generating a piece of semantic program code after understanding the intention, in which process, the code generation model only generates the corresponding program code according to the task description information, and does not depend on any other external prompt or repair strategy. Iterative code regeneration can be understood as targeted rewriting according to the repair strategy generated by the repair strategy planning model, thereby generating the corresponding repair code. The repair strategy can be in the form of natural language or structured form prompt to guide the code generation model to generate a new code version (i.e. new repair code) on the basis of the initial code, thereby ensuring the accuracy of the final generated target code to the greatest extent. Context preservation and history record can be understood as the code generation model can support the preservation of context information in multiple rounds of dialogue.

[0045] In an exemplary embodiment, the code generation model also supports multiple languages and multiple paradigms, that is, it can automatically determine the type of programming language to be used according to the code task description information, such as C language, Python language, etc., and can also automatically determine the development style, such as functional, object-oriented, etc.

[0046] Next, the specific implementation of "step S220, code fault checking is performed on the initial code, and in the case that the fault checking result indicates that the initial code has code faults, fault repair is performed on the initial code based on the code faults, and repair code is generated" is described in detail.

[0047] For example, the code fault checking of the initial code includes: performing static analysis on the initial code to check syntax errors in the initial code; performing runnability checking on the initial code, and in the case that it is determined that the initial code has runnability, performing logic error checking on the code according to the input first test case.

[0048] In an exemplary embodiment, the initial code can be checked for code faults by a pre-trained code checking model. The code checking model can be understood as a scriptable analysis intelligent agent, which can check the code for faults through static analysis and dynamic execution and testing.

[0049] The static analysis can be understood as using a syntax analyzer to analyze the structure of the code, so as to check syntax errors in the code, such as indentation errors, undefined variables, illegal syntax structures, and the like. The dynamic execution and testing can be understood as automatically loading and running test cases, checking whether the output of the test cases is consistent with the expected value, so as to check the logic errors of the code. If there is a logic error, the test fails, and the test name, failure reason, function call stack information, and the like are output.

[0050] For example, the pre-trained code checking model can perform static analysis on the initial code to identify whether there are basic problems such as syntax errors in the initial code. In the case where no syntax error is found, the code checking model attempts to run the initial code to determine whether the initial code can be normally executed. If it is determined that the initial code can be normally executed, the code checking model further executes the first test case input by the user to check whether the output of the initial code is consistent with the expected output provided by the user, thereby performing logic error checking on the initial code. In the case where all test cases pass the test, it indicates that the initial code function meets the user's demand, and the initial code can be directly used as the final target code, that is, the initial code is directly output, and the code generation is successful. In the case where the initial code has syntax errors or logic errors, the initial code is preliminarily repaired to obtain the initial repaired code.

[0051] For example, Figure 3 A flowchart of a method for repairing initial code in an example embodiment of the present disclosure is shown. Referring to Figure 3 The method can include steps S310 to S340. Wherein:

[0052] In step S310, based on the syntax rule library in the code fault repair model, the syntax error in the initial code checked is repaired first.

[0053] For example, the code fault includes syntax errors and logic errors, and the fault repair model can directly repair simple syntax errors.

[0054] In an example embodiment, the code fault repair model can be understood as a rule-based syntax correction system. A machine learning model can be trained based on a rule library related to code repair to obtain the code fault repair model. The rule library related to code repair can include a syntax rule library, a syntax fault library, a fault repair feasibility knowledge base, and the like. Of course, the fault repair model can be a knowledge base configured with a rule library related to code repair, and the example embodiment does not specially limit this.

[0055] For example, the grammar rule library stores information such as code rules, code constraints, and grammar specifications for various types of programming languages. The grammar fault library stores possible fault information for various types of programming languages, such as fault type, fault cause, fault repair method, etc. The fault repair feasibility knowledge base stores information on whether various faults in various types of programming languages ​​can be repaired.

[0056] For example, a code fault repair model can receive error reports from a code inspection model, analyze them, and determine the specific syntax error type. For example, the fault repair model can input the erroneous code segment in the error report into the fault repair model, analyze it, and determine the fault type of the erroneous code segment. Alternatively, the fault repair model can match the problem type in the error report with the fault type in the fault rule library to determine the fault type of the erroneous code segment. The code fault repair model can also repair code structure, such as matching missing brackets, fixing indentation, suggesting similarly spelled keywords or function names, and completing missing definitions. For example, when a variable is undefined, the code fault repair model can repair incorrect syntax in the initial code based on the correct syntax rules in the grammar rule library. Furthermore, the code fault repair model can generate a repair suggestion log, recording the content and rationale of each modification operation, providing data support for subsequent model tuning. The code fault repair model can also determine the feasibility of the repair, ensuring that the repaired code can be executed normally by the interpreter and no longer generates new syntax errors.

[0057] In step S320, according to the first repair situation, errors to be repaired are determined from the code errors.

[0058] For example, the syntax errors successfully repaired by the code fault repair model can be marked as repaired errors, and then the syntax errors and logic errors detected by the code checking model other than the repaired errors can be determined as errors to be repaired.

[0059] In step S330 , the error to be repaired is input into the code generation model, and the code generation model is instructed to perform a second repair on the error to be repaired.

[0060] For example, the code fault repair model can receive the error report sent by the code inspection model, attempt to repair the error in the error report, directly point to the repair operation for the repairable error, and for the unrepairable error, i.e., the to-be-repaired error described above, the code fault repair model can initiate a joint repair request to the code generation model, i.e., the code fault repair model can send the to-be-repaired error to the code generation model, and let the code generation model repair the to-be-repaired error according to the input task description information under the prompt information of the to-be-repaired error.

[0061] In step S340, a repaired code is generated according to the repair results of the first repair and the second repair.

[0062] For example, after the initial code is repaired by the first repair and the second repair described above, an initial repaired code can be obtained.

[0063] Through steps S310 to S340 described above, the initial code can be cooperatively repaired by the code generation model and the code fault repair model, and the repair success rate of the initial code can be improved.

[0064] Next, the specific implementation of "step S230, performing code logic error inspection on the repaired code, in a case where the logic error inspection result indicates that the repaired code has a logic error, generating a repair strategy of the repaired code based on the pre-generated code improvement knowledge graph, inputting the repair strategy to the code generation model to make the code generation model improve the repaired code based on the repair strategy and the code task description, and updating the repaired code according to the improvement result, iteratively performing the process until a preset condition is met, and obtaining the target code corresponding to the code task description information according to the repaired code that meets the preset condition" is described in detail.

[0065] In an exemplary embodiment, the preset condition includes reaching a first preset iteration number or the logic inspection result indicating that the current improved repaired code has no logic error. The first preset iteration number can be determined according to requirements, for example, if the code generation process needs to be accelerated, the first preset iteration number can be set to be smaller, and if the code generation accuracy needs to be improved, the first preset iteration number can be set to be larger. The present exemplary embodiment does not specially limit this.

[0066] For example, the repaired code obtained in step S120 can be dynamically executed and tested for analysis by the code inspection model described above, so as to check whether the repaired code has a logic error.

[0067] In a case where the repair code obtained in the logic checking result indicating step S120 does not have a logic error, the repair code can be directly determined as the target code, the repair code is the code finally generated according to the task description information, and the repair code can be directly output to the user.

[0068] In a case where the repair code obtained in the logic checking result indicating step S120 has a logic error, a repair strategy of the repair code can be generated through a pre-trained repair strategy planning model, the repair code is repaired according to the repair strategy, and the repaired code is used as a new repair code. The new repair code is subjected to code logic error checking again, if there is still a logic error, the repair strategy is continuously generated, and the process is repeatedly executed until the new repair code does not have a logic error or a first preset iteration number is reached, and then the current new repair code can be output to the user as the target code.

[0069] In an exemplary embodiment, the pre-trained repair strategy planning model can be understood as a strategy planning intelligent agent based on a large language model, which can convert error analysis into modification suggestions. For example, the repair strategy planning model can generate a repair strategy for the repair code based on a code improvement knowledge graph.

[0070] For example, the strategy planning model can understand the logic error context, generate a repair strategy, and then generate a prompt word suitable for the code generation model according to the repair strategy. For example, the strategy planning model can read the error report provided by the code checking model, dynamic test failure details, and other information, analyze the possible logic defects (such as boundary conditions, data structure selection, control flow problems, etc.) corresponding to the failure causes, and then generate step-by-step guidance suggestions (i.e., repair strategies) according to the code improvement knowledge graph, such as “please try to modify the condition statement A to A&&B”, please continue “change the list to a set to improve the efficiency of duplicate detection”, the repair strategy planning model also supports the generation of repair strategies combining natural language and programmatic instructions, such as “the variable defined in line 3 should be of type float”, etc. The strategy planning model can convert the above logic defect checking results and repair instructions into prompt words in an input form suitable for the code generation model, so as to guide the code generation model to perform a new round of code repair and code generation. The repair strategy can also be updated according to the result feedback after each iteration to improve the model adaptation capability.

[0071] For example, the code improvement knowledge graph can be generated by: in the historical code generation process, when the logical error checking result indicates that the repair code has a logical error, performing a code improvement knowledge extraction operation to determine an error root cause knowledge base of the code generation; processing the error root cause knowledge base to generate a code improvement knowledge graph; wherein the code improvement knowledge extraction operation includes: based on the code checking model, extracting a context summary of the logical error and an error type of the logical error to perform a first code improvement knowledge extraction operation; based on the code fault repair model, determining repair feasibility and repair rules of the logical error to perform a second code improvement knowledge extraction operation; based on the code generation model, determining modification feasibility of a target code segment having the logical error and reasons why the repair code has a logical error to perform a third code improvement knowledge extraction operation.

[0072] For example, in the historical code generation process, when the logical error checking result indicates that the repair code has a logical error, the repair strategy planning model can instruct the code checking model to extract a context summary of the logical error and an error type of the logical error to obtain first code improvement knowledge; at the same time, the repair strategy planning model can instruct the code fault repair model to determine repair feasibility of the logical error from the perspective of fault, such as the code fault repair model determining that the historical repair success rate of a certain type of fault from a fault knowledge base is 0, then determining that the fault type cannot be repaired, in the case of determining that the repair feasibility is repairable, the code fault repair model provides repair rules to the repair strategy planning model at the same time, thereby obtaining second code improvement knowledge; at the same time, the repair strategy planning model can instruct the code generation model to analyze reasons why the repair code has a logical error, and instruct the code generation model to analyze modification feasibility of the code segment having a logical error from the perspective of code, such as analyzing modification feasibility of the code segment having a logical error according to code specifications, code constraints, syntax rules, etc., thereby obtaining third improvement knowledge.

[0073] For example, entities and relationships of code improvement can be extracted from the first improvement knowledge, the second improvement knowledge, and the third improvement knowledge, and a code improvement knowledge graph can be generated according to the relationships between the entities. For example, code fault types, fault reasons, modification methods, and the like are extracted as entities, relationships between fault types and fault reasons, relationships between fault reasons and modification methods, relationships between fault types and modification methods, and the like.

[0074] For example, Figure 4 FIG. 1 shows a flowchart of a method for generating a repair strategy according to an example embodiment of the present disclosure. Figure 4 The method can include steps S410 to S440.

[0075] wherein:

[0076] In step S410, the repair priority and the repair method of the logical error are determined based on the pre-generated code improvement knowledge graph by the pre-trained repair strategy planning model, and a first-level repair plan of the repair code is obtained.

[0077] For example, the pre-generated code improvement knowledge graph can provide structured repair knowledge for the repair strategy rule model. For example, the triple relationship corresponding to (entity 1, relationship, entity 2) in the pre-generated code improvement knowledge graph can be used as a prompt word of the repair strategy planning model, so that the repair strategy planning model determines the repair priority and the repair method of each logical error based on the code improvement knowledge graph.

[0078] For example, an exemplary embodiment of step S410 can include determining an initial repair priority and an initial repair method of the logical error based on the pre-generated code improvement knowledge graph; generating repair feasibility and repair completion suggestions of the initial repair priority and the initial repair method based on a code fault repair model; adjusting the initial repair priority and the initial repair method according to the repair feasibility and the repair completion suggestions, and obtaining a first-level repair plan of the repair code according to the adjustment result.

[0079] For example, the context information of the logical error, the error type of the logical error, and other information can be input into the pre-trained repair strategy planning model, such as the error report output by the code checking model. Based on the pre-generated code improvement knowledge graph, the repair strategy planning model can obtain the repair priority and the corresponding repair method of each logical error, and use the repair priority and the repair method as the initial repair priority and the initial repair method to obtain a first-level initial repair plan. The first-level initial repair plan is sent to the code fault repair model, which can determine the repair feasibility and the repair completion suggestions of the repair method corresponding to each logical error according to historical experience, and then feed back the repair feasibility and the repair completion suggestions to the repair strategy planning model. The repair strategy planning model adjusts the first-level initial repair plan according to the feedback suggestions to obtain the final first-level repair plan. In this way, the first-level repair plan can be determined by multiple models in collaboration to improve the accuracy of the repair plan determination.

[0080] For example, the repair strategy planning model can also determine the repair resources required for the repair process, such as memory resources, and send the required repair resource information to the code generation model to pre-reserve the corresponding repair resources, ensuring the smooth execution of the subsequent repair process.

[0081] In step S420, the code inspection model is instructed by the pre-trained repair strategy planning model to generate task description information according to the code, to generate a second test case of the repair code, and to obtain a second-level repair plan of the repair code.

[0082] For example, when formulating the repair strategy, the repair strategy planning model can also instruct the code inspection model to generate a second test case of the repair code according to the code generation task description information, so as to obtain a second-level repair plan.

[0083] In step S430, the repair strategy planning model is pre-trained to determine the code structure optimization mode of the repair code and the optimization priority corresponding to the code structure optimization mode, and to obtain a third-level repair plan of the repair code.

[0084] For example, the repair strategy planning model can also determine the code structure optimization mode of the repair code and the optimization priority corresponding to each code structure optimization mode, so as to obtain a third-level repair plan of the repair code.

[0085] In step S440, the repair strategy of the repair code is determined according to the first-level repair plan, the second-level repair plan, and the third-level repair plan.

[0086] For example, the first-level repair plan, the second-level repair plan, the third-level repair plan, and a repair plan execution order of performing the first-level repair plan first, then performing the second-level repair plan, and finally performing the third-level repair plan are taken as the final repair strategy.

[0087] In other words, in the present disclosure, the repair strategy planning model can generate a multi-dimensional and multi-level repair strategy, and improve the performance of the generated code from multiple angles, so that the generated code not only meets the functional requirements but also has high code execution efficiency, and improves the quality of code generation.

[0088] For example, inputting the repair strategy into the code generation model to improve the repair code based on the repair strategy and the code task description includes: improving the repair code based on the first-level repair plan in the repair strategy and the code task description to obtain a candidate code; performing a logic error check on the candidate code based on the second test case in the second-level repair plan to execute the second-level repair plan; in the case where the second-level repair plan does not introduce new logic errors, executing the third-level repair plan on the candidate code; and obtaining an improvement result of the repair code according to the execution result of the third-level repair plan.

[0089] For example, the currently generated repair strategy can be input into a code generation model, and the code generation model can improve the repaired code according to the currently generated repair strategy and the code task description information input by the user. When improving the code task description information according to the repair strategy, the first layer repair plan in the repair strategy is executed according to the repair priority to improve the repaired code to obtain a candidate code, and then the second layer repair plan in the repair strategy is executed to perform a logical error check on the candidate code to ensure that the logical repair does not introduce new logical errors. If the logical repair introduces new logical errors, the code generation model stops the repair, restores to the code state before this repair, and sends an instruction to the repair strategy planning model to regenerate a repair strategy, and this repair is not counted in the iteration number. In this way, each repair is a targeted repair for the previous logical error, does not introduce new logical errors, and ensures the effectiveness of each repair. If the logical repair does not introduce new logical errors, the code generation model continues to execute the third layer repair strategy, that is, performs structural optimization on the code. Through the execution of the multi-layer repair strategy, the execution efficiency of the code can be improved while ensuring the functional requirements of the code.

[0090] For example, executing the third layer repair plan on the candidate code includes: traversing the code structure optimization mode according to an optimization priority corresponding to the code structure optimization mode; optimizing the code structure of the candidate code based on the currently traversed code structure optimization mode; performing a logical error check on the candidate code after the current structural optimization according to the second test case in the second layer repair plan; in a case where the logical error check result indicates that no new logical error is introduced, updating the candidate code based on the candidate code after the current structural optimization; in a case where the logical error check result indicates that a new logical error is introduced, not updating the candidate code, and continuing to traverse the next code structure optimization mode, and repeating the process until the code structure optimization mode is traversed.

[0091] For example, the code structure optimization mode mainly optimizes the structure of the code to improve the execution efficiency of the code. The optimization of the code structure is carried out on the premise of not affecting the functional requirements of the code. Therefore, the optimization of the code structure cannot introduce new logical errors. Therefore, the code generation model can traverse each code structure optimization mode in the third layer repair plan according to the order from high to low based on the optimization priority of the code structure optimization mode. The candidate code is optimized according to the code structure optimization mode currently traversed, the code checking model is notified to perform code logic checking on the code B obtained after the current structure optimization, the logical errors currently checked are compared and analyzed with the logical errors of the previous candidate code A, and in the case of determining that no new logical error is introduced, the improved code B is taken as a new candidate code, that is, the candidate code is updated according to the current improved code, and then the next structure optimization mode is traversed. The code is improved based on the new candidate code B according to the next structure optimization mode. In the case of determining that a new logical error is introduced, the candidate code is not updated, and the next structure optimization mode is traversed. That is, the candidate code A is optimized according to the next structure optimization mode based on the candidate code A, and the process is repeated until the last structure optimization mode is reached. The candidate code optimized by the last structure optimization mode is taken as the repair code corresponding to the current repair strategy.

[0092] For example, in generating the repair strategy, the code fault repair model can also perform code logic constraint conflict checking on the repair mode in the first layer repair plan based on the code fault repair model. In the case of determining that there is a code logic constraint conflict, the code generation model, the code checking model, the code fault repair model and the repair strategy planning model are started to arbitrate the target code logic constraint from the conflicting code logic constraints according to the historical repair experience of each model, that is, each model votes for the logical constraints in conflict, and the logical constraint with the most votes is determined as the target code logic constraint. Therefore, the target code logic constraint is taken as the code logic constraint corresponding to the repair mode, so as to ensure that the repair mode does not introduce new logical conflicts.

[0093] For example, in generating the repair strategy, the code fault repair model can also perform code logic constraint conflict checking on the repair mode in the first layer repair plan based on the code fault repair model. In the case of determining that there is a code logic constraint conflict, the code generation model, the code checking model, the code fault repair model and the repair strategy planning model are started to arbitrate the target code logic constraint from the conflicting code logic constraints according to the historical repair experience of each model, that is, each model votes for the logical constraints in conflict, and the logical constraint with the most votes is determined as the target code logic constraint. Therefore, the target code logic constraint is taken as the code logic constraint corresponding to the repair mode, so as to ensure that the repair mode does not introduce new logical conflicts.

[0094] Exemplarily, in the process of iteratively improving the repair code, in the case that the number of iterations reaches a second preset number of iterations, the text repair instruction in the repair mode is downgraded by the pre-trained repair strategy planning model, and a repair strategy is obtained according to the result of the downgrading processing; wherein, the downgrading processing is used to convert the text repair instruction into a code repair instruction, and the second preset number of iterations is less than the first preset number of iterations.

[0095] For example, in the case that the number of times that the code generation model continuously generates repair code with logical errors reaches a second preset number of iterations, the repair strategy planning model can trigger a mechanism for downgrading the text repair instruction in the repair mode when generating the repair strategy, that is, abstract text repair instructions are disassembled into more specific code repair instructions. For example, the repair instruction for modifying the loop logic is downgraded to the repair instruction "change for i in range(len(arr)) to for item in arr". Through the refinement and downgrading processing of the abstract repair instruction, the code generation model can more accurately understand the meaning of the repair instruction, and improve the accuracy of the repair.

[0096] Exemplarily, Figure 5 A schematic diagram of a code generation system in an exemplary embodiment of the present disclosure is shown. Referring to Figure 5 The code generation system in the present disclosure can include a code generation engine 51, a code evaluation module 52, a fault repair module 53, and a strategy planner 54. The code generation engine can be understood as the code generation model described above, the code evaluation module can be understood as the code inspection model described above, the fault repair module can be understood as the code fault repair model described above, and the strategy planner can be understood as the repair strategy planning model described above.

[0097] For example, the code generation engine 51 can receive a user input code task description, generate the code corresponding to the code task description, the code evaluation module 52 can perform code fault checking on the code generated by the code generation engine 51, find errors in the code, and send the checking result to the code generation engine 51 and the fault repair module 53, the fault repair module 53 can repair the code according to the error information in the code checking result sent by the code evaluation module, send the repaired new version of the code to the code evaluation module 52 again, and the code evaluation module performs code checking on the new version of the code again. At the same time, the fault repair module 53 can also analyze the error information in the code checking result sent by the code evaluation module 52 to obtain the reason for the code test failure, send the failure reason to the strategy planner 54, and the strategy planner 54 formulates a code repair strategy according to the failure reason and other information, and sends the code repair strategy to the code generation engine 51. The code generation engine 51 repairs the code generated in the previous cycle according to the repair plan, the evaluation result of the code evaluation module and the code task description information input by the user, generates a new code, and so on, until the new code generated does not exist logical error or the iteration number reaches the preset number, and the finally generated code is returned to the terminal where the user is located.

[0098] In an exemplary embodiment, the code generation engine 51, the code evaluation module 52, the fault repair module 53 and the strategy planner 54 can all be understood as an independent intelligent agent with certain functions, such as the code generation engine 51 can be understood as a code generation intelligent agent, the code evaluation module 52 can be understood as a code analysis intelligent agent, the fault repair module 53 can be understood as a code repair intelligent agent, and the strategy planner 54 can be understood as a decision-making intelligent agent. The intelligent agents work together to ultimately generate the target code corresponding to the user input code task description.

[0099] An agent refers to an agent that can perceive the environment and take actions to achieve specific goals. It can be software, hardware or a system, with autonomy, adaptability and interaction ability. The agent perceives changes in the environment (such as through sensors or data input), makes judgments and decisions based on knowledge and algorithms learned by itself, and then performs actions to affect the environment or achieve predetermined goals. Agents are widely used in the field of artificial intelligence, commonly used in automated systems, robots, virtual assistants and game characters, and the core is to be able to learn and evolve autonomously to better complete tasks and adapt to complex environments.

[0100] For example, the code generation engine can receive the natural language repair instruction prompt word sent by the strategy planner, further understand the user's intention according to the prompt word, and generate a logical code version. The code generation engine can also actively feed back the code repair feasibility (such as the A&&B condition cannot be implemented) to the strategy planner according to the code generation situation in the code iteration optimization stage, and trigger collaborative adjustment. The code generation engine can also realize multi-agent context synchronization, that is, keep the key decisions of other agents in the historical dialogue (such as variable completion records of the fault repair module), and ensure iteration consistency.

[0101] The code evaluation module can perform two-level error classification, for example, directly route syntax errors to the fault repair module, repair syntax errors through the fault repair module, and pass logical errors and error context summaries (such as "test failure is concentrated in the boundary value") to the strategy planner. The code evaluation module can also generate error priority labels (such as blocking errors / warnings) to guide other agents to process accordingly according to the error priority labels. When the code evaluation module detects a multi-module collaborative conflict, such as when the repaired code introduces new errors, it can also start a collaborative arbitration protocol to require the strategy planner to generate a repair strategy again.

[0102] The fault repair module 53 can handle common syntax errors based on a rule base, and the complex problem fault repair module 53 can initiate a joint repair request to the code generation engine 51. The fault repair module 53 can also provide repair feasibility feedback (such as unable to repair the type error in line 7, suggest refactoring) to the strategy planner to promote strategy optimization adjustment. The fault repair module 53 can also share knowledge with other agents, such as converting repair success cases into structured rules and broadcasting them to all other agents to improve the overall ability of the entire agent group.

[0103] The strategy planner can realize the role of a distributed decision center, that is, integrate error analysis of the code evaluation module, repair records of the fault repair module, repair feasibility feedback of the code generation engine, and other information to generate a multi-dimensional and multi-level repair plan. The strategy planner can also optimize the instruction structure in real time according to the agent feedback, such as downgrading the repair strategy instruction prompt word when the code generation engine fails multiple times. When multiple agents have different opinions on the repair scheme, such as the fault repair module suggesting to complete the variable and the code generation engine suggesting to delete the variable, the strategy planner can also arbitrate the final scheme based on historical cases.

[0104] An exemplary, Figure 6 A flowchart of another code generation method in an exemplary embodiment of the present disclosure is shown. Referring to Figure 6 The method can include steps S601 to S611. Wherein:

[0105] In step S601, the input task description and test case are obtained.

[0106] For example, a user enters a functional description of a programming task in natural language, such as "calculate the largest value in a set of numbers." The user also provides several sets of input and output examples as test cases, such as input [3, 7, 2] with an expected output of 7. This information together constitutes the task objective and evaluation criteria for code generation.

[0107] In step S602 , the code generation engine generates initial code.

[0108] For example, a code generation engine in a code generation system receives a task description and test cases and generates executable program code based on the task description. This generation process does not rely on external planning or logical analysis, but directly extracts key semantics from the input to construct the initial solution.

[0109] In step S603, a basic check is performed on the initial code.

[0110] For example, after initial code generation, the aforementioned code evaluation module is invoked to perform static analysis and runnability checks. This module identifies basic issues such as syntax errors, inconsistent indentation, and undefined variables. Furthermore, it attempts to run the code to determine whether it can be executed normally.

[0111] In step S604, the initial code is functionally verified using test cases.

[0112] If the code can run normally, the code evaluation module further executes the test case input by the user in the above step S601 to check whether the output of the initial code is consistent with the expected result provided by the user.

[0113] In step S605, it is determined whether the basic check and function verification are passed. If so, the process ends and the target code is obtained. Otherwise, it goes to step S606.

[0114] If the initial code does not have any syntax errors and all test cases pass, it means that the initial code function meets the requirements, the process ends, and the code generation is successful.

[0115] If errors are found in the initial code during syntax checking or functional verification, the system will start a planned iterative optimization process based on the current code and related error reports, that is, start the steps in the following steps S606 to S611.

[0116] In step S606, multiple agents collaborate to repair code errors.

[0117] The fault repair module receives the error report of the code evaluation module, directly performs repair for syntax errors that can be independently repaired (such as missing parentheses), and initiates a joint repair request to the code generation engine for complex problems that need to be coordinated (such as variable type conflicts). The generation engine returns a completion suggestion after the repair is integrated.

[0118] In step S607, the code evaluation module performs logical checking. If there is no logical error, the process ends, and the target code is obtained. If there is a logical error, the process proceeds to step S608.

[0119] The repaired code is again submitted to the code evaluation module for execution. The focus of this round of evaluation is whether the logical function is correct. The system compares the code execution result with the expected value of the test case to determine whether the program has implemented the correct business logic. If it does not meet the requirements, it proceeds to step S608. If it meets all the test case requirements, the process ends.

[0120] In step S608, a multi-agent collaborates to develop a logical repair strategy.

[0121] When the code evaluation module reports a logical error, the code generation system will call the strategy planner. The strategy planner automatically builds a strategy for logical repair based on the error performance and task description, which includes error cause analysis, modification suggestions, and logical ideas.

[0122] For example, during the generation of historical code, the strategy planner can first perform joint diagnosis of the error root cause. Specifically, the strategy planner sends an error analysis instruction to the code evaluation module, requesting the generation of an error context summary and the extraction of error pattern tags. At the same time, the strategy planner requests the fault repair module to evaluate the repairability of the target code segment and receives feedback on the repair feasibility. At the same time, the strategy planner can receive feedback on code modifiability from the code generation engine. Next, the strategy planner fuses these knowledge, with the error tags and failure context provided by the code evaluation module for locating the core defects, the code modification constraints and reusable repair rules provided by the fault repair module for evaluating modification safety, and the historical generation failure reasons fed back by the code generation engine for judging instruction executability. The strategy planner builds an error root cause knowledge base and generates a code improvement knowledge graph to provide decision-making basis for subsequent repair strategies.

[0123] After generating the code improvement knowledge graph, in the current repair process, the strategy planner can generate a repair strategy for the current code based on the information fed back by other agents, such as the error tags and failure context provided by the code evaluation module, the fault repair feasibility information and completion suggestions fed back by the fault repair module, and the error modification feasibility information fed back by the code generation engine, based on the code improvement knowledge graph.

[0124] Meanwhile, in the current repair process, the strategy planner can update the above error root cause knowledge base as new knowledge of the other agents in the current fusion, so as to update the code improvement knowledge graph based on the updated root cause knowledge base at the appropriate time.

[0125] For example, the strategy planner can generate a three-level repair plan based on the code improvement knowledge graph. The first level is emergency repair, that is, to determine the repair priority and repair method of the logical error. The second level is the verification layer, that is, the strategy planner requires the code evaluation module to generate new test cases, and lets the fault repair module check the potential variable action on the conflict. The third level is the structure optimization layer, which focuses on the improvement of the code structure. If the code structure improvement introduces new errors, the code generation system triggers the rollback mechanism. The specific implementation process of the rollback mechanism can refer to the execution process of the third-level repair plan described above, and will not be described here.

[0126] In addition, the code generation system also has an adaptive instruction optimization mechanism. When the code generation engine fails continuously, the strategy planner triggers a degradation mechanism to decompose the abstract instruction into more granular operations. If the fault repair module feedbacks that there is a constraint conflict in the variable scope, the system starts a multi-agent voting mechanism, and each module votes for the conflicting constraint scheme according to the historical success rate, and finally selects the high-vote scheme to join the repair strategy.

[0127] The code generation system can generate a machine-executable and human-readable collaborative repair strategy document as a structured product of the multi-agent decision-making process, providing clear guidance and traceability for subsequent execution. For example, in response to user queries about the code generation process, the user can be fed back the collaborative repair strategy to understand the code repair process. Historical repair strategies and current repair strategies can also be transmitted to the code generation engine, allowing the code generation engine to understand and perform the current repair.

[0128] In step S609, the code generation engine regenerates the code according to the repair strategy.

[0129] The strategy planner can convert the generated repair strategy into a prompt word for the code generation engine, and deliver it to the code generation engine as a new input of the code generation engine. The code generation engine regenerates the improved code version based on the user input code task description, the repair strategy prompt word and the current code state.

[0130] In step S610, the code evaluation module tests and evaluates the newly generated code. If the evaluation is passed, the process ends and the target code is obtained. If the evaluation is not passed, go to step S611.

[0131] In step S611, it is judged whether the first preset iteration number is reached. If yes, the flow ends and the target code is obtained. If no, the flow goes to step S608.

[0132] For example, the newly generated code is run by the code evaluation module to verify the test case again. If it passes, the flow ends. If it still fails, it returns to step S608 to continue to develop the repair strategy. The system presets a maximum iteration number. If the test still fails after reaching the upper limit, the flow ends and outputs the current optimal version for manual reference.

[0133] In the present disclosure, the code is generated based on two stages, i.e., an initial generation stage and a strategy-guided optimization stage. In the initial generation stage, the initial code is quickly generated, and then the targeted iteration correction is performed according to the evaluation stage, so as to avoid the overall interruption caused by one round of failure, improve the code generation accuracy, and improve the code generation success rate.

[0134] At the same time, in the present disclosure, the agent system composed of the code generation engine, the code evaluation module, the fault repair module, and the strategy planner realizes the division of labor and cooperation of code generation, evaluation, repair, and optimization, replaces the traditional single model processing mode, and improves the system efficiency and stability. In addition, by introducing the strategy planner module, the structured repair strategy can be developed based on the language model to fuse multiple aspects of knowledge, guide the code generation engine to accurately modify the logic error, and enhance the ability and interpretability of the system to handle complex problems.

[0135] In addition, it should be noted that the above-described figures are only schematic illustrations of the processes included in the method according to the exemplary embodiments of the present disclosure, and are not for limiting purposes. It is easy to understand that the processes shown in the above-described figures do not indicate or limit the time sequence of these processes. In addition, it is also easy to understand that these processes can be executed synchronously or asynchronously, for example, in multiple modules.

[0136] Further, the exemplary embodiments of the present disclosure also provide a code generation device. Referring to Figure 7As shown, the code generation apparatus 700 comprises the following program modules: an initial code generation module 710 configured to generate initial code corresponding to code task description information based on a pre-trained code generation model; an initial code repair module 720 configured to perform code fault checking on the initial code, and in a case where the checking result indicates that the initial code has code faults, repairing the initial code based on the code faults to generate repaired code; an iterative repair module 730 configured to perform code logic error checking on the repaired code, and in a case where the logic error checking result indicates that the repaired code has logic errors, generating a repair strategy for the repaired code based on a pre-generated code improvement knowledge graph, inputting the repair strategy to the code generation model, so that the code generation model improves the repaired code based on the repair strategy and the code task description information, and updates the repaired code according to the improvement result, iteratively performing the process until a preset condition is met, and obtaining target code corresponding to the code task description information according to the repaired code that meets the preset condition; wherein the preset condition comprises reaching a first preset iteration number or the logic checking result indicating that the current improved repaired code has no logic errors.

[0137] In an exemplary embodiment, based on the foregoing embodiment, the code fault checking on the initial code comprises: performing static analysis on the initial code to check syntax errors in the initial code; performing runnability checking on the initial code, and in a case where it is determined that the initial code has runnability, performing logic error checking on the code according to input first test cases.

[0138] In an exemplary embodiment, based on the foregoing embodiment, in a case where the checking result indicates that the initial code has code faults, the code fault repair based on the code faults to generate repaired code comprises: performing first repair on the checked syntax errors in the initial code based on a syntax rule library in the code fault repair model; determining to-be-repaired errors from the code errors according to the first repair; inputting the to-be-repaired errors to the code generation model to instruct the code generation model to perform second repair on the to-be-repaired errors; and generating repaired code according to the repair results of the first repair and the second repair.

[0139] In an example embodiment, based on the foregoing embodiment, the code improvement knowledge graph generation method comprises: in the historical code generation process, if the logical error checking result indicates that the repair code has a logical error, performing a code improvement knowledge extraction operation to determine an error root cause knowledge base of the code generation; processing the error root cause knowledge base to generate a code improvement knowledge graph; wherein the code improvement knowledge extraction operation comprises: based on the code checking model, extracting a context summary of the logical error and an error type of the logical error to perform a first code improvement knowledge extraction operation; based on the code fault repair model, determining repair feasibility and repair rules of the logical error to perform a second code improvement knowledge extraction operation; based on the code generation model, determining modification feasibility of a target code segment that has the logical error and reasons why the repair code has a logical error to perform a third code improvement knowledge extraction operation.

[0140] In an example embodiment, based on the foregoing embodiment, the repair strategy generation method comprises: based on the pre-generated code improvement knowledge graph, determining a repair priority and a repair method of the logical error by using a pre-trained repair strategy planning model to obtain a first layer repair plan of the repair code; instructing the code checking model to generate a second test case of the repair code according to the code generation task description information by using the pre-trained repair strategy planning model to obtain a second layer repair plan of the repair code; determining a code structure optimization method of the repair code and an optimization priority corresponding to the code structure optimization method by using the pre-trained repair strategy planning model to obtain a third layer repair plan of the repair code; and determining the repair strategy of the repair code according to the first layer repair plan, the second layer repair plan, and the third layer repair plan.

[0141] In an example embodiment, based on the foregoing embodiment, the repair strategy generation method comprises: based on the pre-generated code improvement knowledge graph, determining a repair priority and a repair method of the logical error by using a pre-trained repair strategy planning model to obtain a first layer repair plan of the repair code; instructing the code checking model to generate a second test case of the repair code according to the code generation task description information by using the pre-trained repair strategy planning model to obtain a second layer repair plan of the repair code; determining a code structure optimization method of the repair code and an optimization priority corresponding to the code structure optimization method by using the pre-trained repair strategy planning model to obtain a third layer repair plan of the repair code; and determining the repair strategy of the repair code according to the first layer repair plan, the second layer repair plan, and the third layer repair plan.

[0142] In an exemplary embodiment, based on the foregoing embodiment, the inputting the repair strategy into the code generation model to enable the code generation model to improve the repair code based on the repair strategy and the code task description information comprises: improving the repair code according to the first layer repair plan in the repair strategy and the code task description information to obtain a candidate code; performing a logical error check on the candidate code based on a second test case in the second layer repair plan to execute the second layer repair plan; and executing the third layer repair plan on the candidate code in the case where the second layer repair plan does not introduce new logical errors.

[0143] According to the execution result of the third layer repair plan, an improved result of the repair code is obtained.

[0144] In an exemplary embodiment, based on the foregoing embodiment, the executing the third layer repair plan on the candidate code comprises: traversing the code structure optimization mode according to an optimization priority corresponding to the code structure optimization mode; optimizing the code structure of the candidate code based on the currently traversed code structure optimization mode, performing a logical error check on the candidate code after the current structure optimization according to the second test case in the second layer repair plan, updating the candidate code based on the candidate code after the current structure optimization in the case where the logical error check result indicates that no new logical error is introduced, and not updating the candidate code and continuing to traverse the next code structure optimization mode in the case where the logical error check result indicates that a new logical error is introduced, and repeating the process until the code structure optimization mode is traversed.

[0145] In an exemplary embodiment, based on the foregoing embodiment, the device further comprises a constraint conflict checking module configured to perform a code logic constraint conflict check on the repair mode based on a code fault repair model, and in the case where it is determined that there is a code logic constraint conflict, performing arbitration on the code logic constraint with the conflict by the code generation model, the code checking model, the code fault repair model and the repair strategy planning model to determine a target code logic constraint.

[0146] In an exemplary embodiment, based on the foregoing embodiment, the device further comprises a degradation mechanism triggering module configured to, in the case where the number of iterations reaches a second preset number of iterations, perform degradation processing on a text repair instruction in the repair mode by a pre-trained repair strategy planning model to obtain a repair strategy according to the result after the degradation processing; wherein the degradation processing is used to convert the text repair instruction into a code repair instruction, and the second preset number of iterations is less than the first preset number of iterations.

[0147] The specific details of parts of the above apparatus have been described in detail in the method part embodiments, and the details not disclosed can be referred to the content of the method part embodiments, and thus will not be described again.

[0148] It should be noted that although several modules or units of the device for action execution are mentioned in the above detailed description, such division is not mandatory. Indeed, according to the example embodiments of the present disclosure, the features and functionalities of two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functionalities of one module or unit described above can be further divided into embodied by multiple modules or units.

[0149] Further, although the steps of the methods of the present disclosure are described in a particular order in the figures, this is not required or implied in any way, and one can perform the steps in any order, or perform only some of the steps, or perform the steps in a different order, or perform the steps in a different way, and still achieve desirable results.

[0150] The example embodiments of the present disclosure also provide a computer program product. The computer program product includes a computer program, which, when executed by a processor, implements the code generation method described above.

[0151] In an embodiment, the computer program product can be a tangible product containing the computer program, such as a computer readable storage medium storing the computer program. The readable storage medium can be a storage medium based on electrical, magnetic, optical, electromagnetic, infrared, etc. signals, including but not limited to: random access memory (RAM), read-only memory (ROM), magnetic tape, floppy disk, flash memory (Flash), mechanical hard disk (HDD), solid state disk (SSD), etc. For example, the computer program product can be implemented as a non-volatile storage medium storing the computer program, such as read-only memory, Nand flash memory, etc.

[0152] In an embodiment, the computer program product can be an intangible product containing the computer program. For example, the computer program product can be implemented as a virtual digital product, such as an executable file, installation package, etc. digital file storing the computer program.

[0153] The code of the computer program can be written in one or more programming languages. Programming languages ​​include C, Java, C++, Python, etc. The program code can be executed entirely on the user computing device, partially on the user computing device, or as a separate software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server. In the case of a remote computing device, the remote computing device can be connected to the user computing device via any type of network, such as a local area network (LAN), a wide area network (WAN), etc., or can be connected to an external computing device (e.g., via an Internet connection provided by a carrier).

[0154] Computer programs can be carried or transmitted via electrical, magnetic, optical, electromagnetic, infrared, or other signals. Electronic devices can convert signals carrying computer programs into digital signals to run the computer programs. When the computer program is run on an electronic device, its code is used to cause the electronic device to execute (more specifically, to cause the processor of the electronic device to execute) the method steps of various exemplary embodiments of the present disclosure, such as the code generation method described above.

[0155] The exemplary embodiments of the present disclosure further provide an electronic device, such as the aforementioned terminal device 110 or server 120. The electronic device may include a processor and a memory. The memory stores executable instructions for the processor, such as a computer program. The processor executes the executable instructions to perform the method steps of the various exemplary embodiments of the present disclosure. Furthermore, the electronic device may also include a display for displaying a graphical user interface.

[0156] Reference below Figure 8 , the electronic device is exemplarily described in the form of a general-purpose computing device. It should be understood that Figure 8 The electronic device 800 shown is only an example and should not limit the functions and scope of use of the embodiments of the present disclosure.

[0157] like Figure 8 As shown, the electronic device 800 may include a processor 810 , a memory 820 , a bus 830 , an I / O (input / output) interface 840 , a network adapter 850 , and a display 860 .

[0158] The memory 820 can include volatile memory, such as RAM 821, cache unit 822, and non-volatile memory, such as ROM 823. The memory 820 can also include one or more program modules 824, which include, but are not limited to: an operating system, one or more application programs, other program modules, and program data, each or a combination thereof can include an implementation of a network environment. For example, the program modules 824 can include the modules in the apparatuses described above.

[0159] The processor 810 can include one or more processing units, such as: the processor 810 can include an application processor (AP), a modem processor, a graphics processing unit (GPU), an image signal processor (ISP), a controller, an encoder, a decoder, a digital signal processor (DSP), a baseband processor, and / or a neural-network processing unit (NPU), etc.

[0160] The processor 810 can be configured to execute executable instructions stored in the memory 820, such as can execute the code generation method described above.

[0161] The bus 830 is configured to connect different components of the electronic device 800, and can include a data bus, an address bus, and a control bus.

[0162] The electronic device 800 can communicate with one or more external devices 900 (such as a keyboard, a mouse, an external controller, etc.) through the I / O interface 840.

[0163] The electronic device 800 can communicate with one or more networks through the network adapter 850, such as the network adapter 850 can provide a mobile communication solution such as 3G / 4G / 5G, or provide a wireless communication solution such as a wireless local area network, Bluetooth, near field communication, etc. The network adapter 850 can communicate with other modules of the electronic device 800 through the bus 830.

[0164] The electronic device 800 can display a graphical user interface, such as a generated target code interface, through the display 860.

[0165] Although Figure 8Other hardware and / or software modules can also be included in electronic device 800, including but not limited to microcode, device drivers, redundant processors, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, etc., which are not shown.

[0166] Furthermore, the above-described diagrams are merely schematic illustrations of the processes included in the method according to the exemplary embodiments of the present disclosure, and are not intended for limiting purposes. It is readily understood that the processes shown in the above-described diagrams do not indicate or limit the time sequence of the processes. In addition, it is readily understood that the processes can be executed synchronously or asynchronously, for example, in a plurality of modules.

[0167] As can be seen, the technical solutions of the present disclosure can be implemented as a method, an apparatus, a system, a computer program product, a storage medium, an electronic device, and the like. Those skilled in the art can understand that various aspects of the present disclosure can be implemented in the following forms: a complete hardware implementation, a complete software implementation (including firmware, microcode, etc.), or a combination of hardware and software aspects, which can be referred to as "circuitry", "module", or "system", respectively.

[0168] It should be understood that the present disclosure is not limited to the specific process steps or structural aspects already described and shown in the drawings, and various modifications and changes can be made without departing from the scope of the present disclosure. Those skilled in the art, based on the specific embodiments provided by the present disclosure, will readily conceive of other embodiments. Therefore, the specific embodiments provided by the present disclosure are merely exemplary, and the scope and spirit of the present disclosure are indicated by the claims, and should encompass any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure, and include common knowledge or conventional technical means in the technical field of the present disclosure that are not disclosed by the present disclosure.

Claims

1. A code generation method, characterized in that: include: Generate the initial code corresponding to the code task description information based on the pre-trained code generation model; performing a code fault check on the initial code, and if a fault check result indicates that the initial code has a code fault, performing fault repair on the initial code based on the code fault to generate a repair code; Performing a code logic error check on the repair code. If the logic error check result indicates that the repair code has a logic error, generating a repair strategy for the repair code based on a pre-generated code improvement knowledge graph, inputting the repair strategy into the code generation model so that the code generation model improves the repair code based on the repair strategy and the code task description information, and updating the repair code according to the improvement result. This process is iteratively executed until a preset condition is met, and obtaining a target code corresponding to the code task description information based on the repair code that meets the preset condition; The preset conditions include reaching a first preset number of iterations or the logic check result indicating that the current improved repair code does not have any logic errors.

2. The method according to claim 1, characterized in that The performing code fault checking on the initial code comprises: Performing static analysis on the initial code to check for syntax errors in the initial code; The initial code is checked for operability, and when it is determined that the initial code is operable, the code is checked for logic errors according to an input first test case.

3. The method according to claim 1, characterized in that When the checking result indicates that the initial code has a code fault, repairing the initial code based on the code fault to generate a repair code includes: Performing a first repair on the detected grammatical errors in the initial code based on a grammatical rule library in a code fault repair model; According to the first repair situation, determining an error to be repaired from the code fault; Inputting the error to be repaired into the code generation model, and instructing the code generation model to perform a second repair on the error to be repaired; A repair code is generated according to the repair results of the first repair and the second repair.

4. The method according to claim 1, wherein The method for generating the code-improved knowledge graph includes: In the historical code generation process, if the logic error check result indicates that the repair code has a logic error, performing a code improvement knowledge extraction operation to determine a knowledge base of root causes of errors in code generation; Processing the error root cause knowledge base to generate a code improvement knowledge graph; The code improvement knowledge extraction operation includes: Extracting a context summary of the logic error and an error type of the logic error based on a code inspection model to perform a first code improvement knowledge extraction operation; Based on the code fault repair model, determining the repair feasibility and repair rules of the logical error to perform a second code improvement knowledge extraction operation; Based on the code generation model, the feasibility of modifying the target code segment with the logic error and the reason why the repair code has the logic error are determined to perform a third code improvement knowledge extraction operation.

5. The method according to claim 1, wherein The repair strategy for generating the repair code based on the pre-generated code improvement knowledge graph includes: Using a pre-trained repair strategy planning model, the repair priority and repair method of the logic error are determined based on a pre-generated code improvement knowledge graph, and a first-level repair plan for the repair code is obtained; Instructing the code checking model to generate task description information according to the code through the pre-trained repair strategy planning model, generating a second test case for the repair code, and obtaining a second-level repair plan for the repair code; Determining a code structure optimization method for the repair code and an optimization priority corresponding to the code structure optimization method through a pre-trained repair strategy planning model, thereby obtaining a third-level repair plan for the repair code; A repair strategy for the repair code is determined according to the first-layer repair plan, the second-layer repair plan, and the third-layer repair plan.

6. The method according to claim 5, characterized in that The first-level repair plan for determining the repair priority and repair method of the logical error based on the pre-generated code improvement knowledge graph to obtain the repair code includes: Determine the initial repair priority and initial repair method of the logic error based on the pre-generated code improvement knowledge graph; Based on the code fault repair model, generate the initial repair priority and the repair feasibility of the initial repair method and repair completion suggestions; According to the repair feasibility and the repair completion suggestion, the initial repair priority and the initial repair method are adjusted, and a first-level repair plan for the repair code is obtained according to the adjustment result.

7. The method according to claim 5, characterized in that Inputting the repair strategy into the code generation model so that the code generation model improves the repair code based on the repair strategy and the code task description information includes: Improve the repair code according to the first-level repair plan in the repair strategy and the code task description information to obtain candidate code; Based on the second test case in the second-tier repair plan, performing a logic error check on the candidate code to execute the second-tier repair plan; If the second-level repair plan does not introduce new logical errors, executing the third-level repair plan on the candidate code; According to the execution result of the third-layer repair plan, an improvement result of the repair code is obtained.

8. The method according to claim 7, characterized in that Executing the third-level repair plan on the candidate code includes: Traversing the code structure optimization method according to the optimization priority corresponding to the code structure optimization method; The code structure of the candidate code is optimized based on the currently traversed code structure optimization method, and according to the second test case in the second-layer repair plan, a logic error check is performed on the candidate code after the current structure optimization. When the logic error check result indicates that no new logic error is introduced, the candidate code is updated based on the currently optimized candidate code. When the logic error check result indicates that a new logic error is introduced, the candidate code is not updated, and the next code structure optimization method is continued to be traversed, and the process is repeated until all the code structure optimization methods are traversed.

9. The method according to claim 5, characterized in that The method further comprises: The repair method is checked for code logic constraint conflicts based on the code fault repair model. When it is determined that a code logic constraint conflict exists, the conflicting code logic constraints are adjudicated through the code generation model, the code inspection model, the code fault repair model and the repair strategy planning model to determine the target code logic constraints.

10. The method according to claim 5, characterized in that The method further comprises: When the number of iterations reaches a second preset number of iterations, the text repair instruction in the repair method is downgraded using a pre-trained repair strategy planning model, and a repair strategy is obtained according to the result of the downgrading process; The degradation process is used to convert the text repair instruction into a code repair instruction, and the second preset number of iterations is less than the first preset number of iterations.

11. A code generating device, characterized in that: include: An initial code generation module is configured to generate initial code corresponding to the code task description information based on a pre-trained code generation model; an initial code repair module configured to perform a code fault check on the initial code, and if a fault check result indicates that the initial code has a code fault, repair the initial code based on the code fault and generate a repair code; an iterative repair module configured to perform a code logic error check on the repair code, and if the logic error check result indicates that the repair code has a logic error, generate a repair strategy for the repair code based on a pre-generated code improvement knowledge graph, input the repair strategy into the code generation model, so that the code generation model improves the repair code based on the repair strategy and the code task description information, and updates the repair code according to the improvement result, iteratively executing this process until a preset condition is met, and obtaining a target code corresponding to the code task description information according to the repair code that meets the preset condition; The preset conditions include reaching a first preset number of iterations or the logic check result indicating that the current improved repair code does not have any logic errors.

12. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 10 is implemented.

13. An electronic device, characterized in that: include: one or more processors; A storage device for storing one or more programs, which, when executed by the one or more processors, causes the one or more processors to implement the method according to any one of claims 1 to 10.

Citation Information

Cited By

  • Training data generation method and device, electronic equipment and storage medium

    CN121580010A

  • Three-dimensional static layout scheme generation method based on LLM supporting iterative correction

    CN121615456A

  • Code modification method and device based on semantic update fragment generation

    CN121722426A

  • Code modification method and device based on semantic update fragment generation

    CN121722426B