Code generation method and device based on large model, equipment, medium and product

Through the multi-model collaborative code generation method, using large models and static and dynamic analysis tools, the reliability and security issues of large model generated code are solved, efficient and reliable code generation and repair are achieved, and the stability and security of the software system are improved.

CN120848896AInactive Publication Date: 2025-10-28CHINA MOBILE (SUZHOU) SOFTWARE TECH CO LTD +1

Patent Information

Application Number
CN202511350214.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-22
Publication Date
2025-10-28
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

The codes generated by existing large models may contain syntax errors, logical loopholes, functional defects and other problems, leading to potential risks in the stability and security of the software system.

Method used

By using a multi-model collaborative approach, the first large code model generates initial code, the second large code model generates unit test code, and the third large code model performs code repair and iterative optimization based on edit distance regularization. Combined with static analysis tools and unit testing tools, the code's syntactic correctness and coverage are ensured, and the code is gradually optimized to the target code.

Benefits of technology

It improves the reliability and accuracy of code generation, reduces software security vulnerabilities, enhances the stability and security of software systems, and significantly improves the efficiency and quality of software development.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120848896A_ABST
    Figure CN120848896A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation method and device based on a large model, equipment, a medium and a product, and the method comprises the steps: carrying out the code generation through a first code large model according to target code demand data, and obtaining an initial code; according to the first code snippet, generating a test code through a second code large model to obtain a unit test code; according to the unit test code and the initial code, code repair based on editing distance regularization is conducted through a third code large model, and a to-be-analyzed code is obtained; performing iterative optimization on the to-be-analyzed code and the unit test code to obtain a target code corresponding to the target code demand data; according to the method, code generation, unit test generation and code repair are carried out in a multi-model cooperation mode, the reliability and accuracy of code generation can be effectively improved, software security vulnerabilities are reduced, and therefore the stability and security of a software system are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of artificial intelligence technology, and in particular to a method, apparatus, device, medium and product for code generation based on a large model. Background Technology

[0002] With the remarkable ability of pre-trained Large Language Models (LLMs) to understand and generate code content, LLMs can provide intelligent code completion and code question answering by learning from a large number of code bases, thus providing powerful assistance to programming.

[0003] Currently, existing solutions typically use large code models directly for code generation. By learning from a large codebase, these models can predict and complete code snippets left unfinished by developers, significantly improving programming efficiency and lowering the barrier to entry for programming. However, code generated by these large models may contain syntax errors, logical loopholes, and functional defects, failing to guarantee code reliability and potentially harboring security vulnerabilities, thus posing risks to the stability and security of software systems. Summary of the Invention

[0004] To address the problems existing in the prior art, embodiments of the present invention provide a code generation method, apparatus, device, medium, and product based on a large model, which can effectively improve the reliability of code generation, reduce security vulnerabilities, and thereby improve the stability and security of software systems.

[0005] In a first aspect, embodiments of the present invention provide a code generation method based on a large model, including: Based on the target code requirements data, code is generated using the first large code model to obtain the initial code; Based on the initial code, test code is generated using the second large code model to obtain unit test code; Based on the unit test code and the initial code, code repair based on edit distance regularization is performed using a third code big model to obtain the code to be analyzed. The code to be analyzed and the unit test code are iteratively optimized to obtain the target code corresponding to the target code requirement data.

[0006] As an improvement to the above solution, the step of performing code repair based on edit distance regularization using a third code large model, based on the unit test code and the initial code, to obtain the code to be analyzed includes: The initial code and the unit test code are input into a syntax analysis tool for code syntax analysis to obtain the syntax analysis results; If the syntax analysis result indicates the presence of a syntax error, the initial code and the syntax analysis result are input into the third code model for code repair based on edit distance regularization to obtain the code to be analyzed. If the syntax analysis result indicates that there are no syntax errors, the initial code is used as the code to be analyzed.

[0007] As an improvement to the above solution, the iterative optimization of the code to be analyzed and the unit test code to obtain the target code corresponding to the target code requirement data includes: The code to be analyzed and the unit test code are input into a unit test analysis tool for unit test analysis to obtain unit test analysis results. The unit test analysis results include: code execution feedback results and coverage analysis feedback results. The code execution feedback results are used to indicate whether the unit test code runs correctly. The coverage analysis feedback results are used to indicate the line coverage and branch coverage of the unit test code on the code to be analyzed. If the unit test analysis results meet a preset first condition, the code to be analyzed is taken as the target code; wherein, the first condition includes: the code execution feedback results in the unit test analysis results indicate that the execution is correct, and the line coverage indicated by the coverage analysis feedback results is greater than or equal to a preset first threshold and the branch coverage is greater than or equal to a preset second threshold. If the unit test analysis results do not meet the first condition, the code to be analyzed and the unit test code are iteratively optimized, and the optimized code to be analyzed and the unit test code are subjected to unit test analysis until the first condition or the preset iterative condition is met.

[0008] As an improvement to the above solution, the iterative optimization of the code to be analyzed and the unit test code includes: If the unit test analysis results meet the preset second condition, the code to be analyzed and the code execution feedback results are input into the third code big model for code repair based on edit distance regularization, and the next code to be analyzed is generated as the code to be analyzed in this round of iteration optimization; The second condition includes: the code execution feedback results in the unit test analysis results indicate a runtime error.

[0009] As an improvement to the above solution, the iterative optimization of the code to be analyzed and the unit test code includes: If the unit test analysis results meet the preset third condition, the code to be analyzed and the coverage analysis feedback results are input into the second code big model to generate the next unit test code, which serves as the unit test code for this round of iterative optimization. The third condition includes: the code execution feedback result in the unit test analysis result indicates that the execution is correct, and the line coverage indicated by the coverage analysis feedback result is less than the first threshold and the branch coverage is less than the second threshold.

[0010] As an improvement to the above scheme, the iteration condition includes: the number of repetitions of unit test analysis is greater than a preset threshold; wherein, when the number of repetitions of unit test analysis reaches the threshold, but the unit test analysis result does not meet the first condition when the number of repetitions of unit test analysis reaches the threshold, the code to be analyzed and the code execution feedback result obtained in the last round are output.

[0011] As an improvement to the above scheme, the loss function of the third code large model includes a basic loss term and a regularization term based on token-granularity edit distance; the regularization term based on token-granularity edit distance is calculated based on insertion cost, deletion cost, and replacement cost.

[0012] As an improvement to the above scheme, the training process of the third code-based large model includes: Construct multiple instruction pairs; wherein, the instruction pairs include error code samples and correct code samples; The error code samples of the instruction pairs are used as input, and the correct code samples of the instruction pairs are used as labels. These are fed into a pre-constructed third code model for model training until the preset convergence condition is met, resulting in a trained third code model.

[0013] As an improvement to the above solution, the method further includes: During the training process of the third code big model, a regularization term and a basic loss term based on token-granularity edit distance are calculated based on the correct code samples and the incorrect code samples. The total loss between the correct code sample and the incorrect code sample is calculated based on the regularization term based on the token-granularity edit distance, the basic loss term, and the hyperparameters. The model parameters of the third code-large model are updated using the total loss.

[0014] As an improvement to the above scheme, the calculation process of the regularization term based on token-granularity edit distance includes: By comparing the correct code sample and the incorrect code sample, determine the number of insertion operations, deletion operations, and replacement operations required to get from the incorrect code sample to the correct code sample; Based on the number of insertion operations, the number of deletion operations, the number of replacement operations, and the insertion cost, deletion cost, and replacement cost, calculate the token-granularity edit distance-based regularization term between the correct code sample and the incorrect code sample.

[0015] Secondly, embodiments of the present invention provide a code generation apparatus based on a large model, comprising: The first code generation module is used to generate initial code based on the target code requirement data and the first code big model. The second code generation module is used to generate unit test code based on the initial code and through the second code model. The code repair module is used to perform code repair based on edit distance regularization using a third code big model based on the unit test code and the initial code to obtain the code to be analyzed. The code optimization module is used to iteratively optimize the code to be analyzed and the unit test code to obtain the target code corresponding to the target code requirement data.

[0016] Thirdly, embodiments of the present invention provide a code generation device based on a large model, comprising: a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein the processor executes the computer program to implement the code generation method based on a large model as described in any one of the first aspects.

[0017] Fourthly, embodiments of the present invention provide a computer-readable storage medium storing a computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to execute the code generation method based on a large model as described in any one of the first aspects.

[0018] Fifthly, embodiments of the present invention provide a computer program product, including a computer program / instruction that, when executed by a processor, implements the code generation method based on a large model as described in any one of the first aspects.

[0019] Compared to existing technologies, this invention provides a code generation method, apparatus, device, medium, and product based on a large model. The method generates initial code from target code requirement data using a first large code model; based on the initial code, it generates unit test code using a second large code model; based on the unit test code and the initial code, it performs code repair based on edit distance regularization using a third large code model to obtain code to be analyzed; and iteratively optimizes the code to be analyzed and the unit test code to obtain the target code corresponding to the target code requirement data. This invention utilizes a multi-model collaborative approach for code generation, unit test generation, and code repair, effectively improving the reliability and accuracy of code generation, reducing software security vulnerabilities, and thus enhancing the stability and security of the software system. Attached Figure Description

[0020] To more clearly illustrate the technical solution of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0021] Figure 1 This is a flowchart of a code generation method based on a large model provided in an embodiment of the present invention; Figure 2 This is a schematic diagram of the system framework for high-reliability code generation provided in an embodiment of the present invention; Figure 3 This is a structural block diagram of a code generation device based on a large model provided in an embodiment of the present invention; Figure 4 This is a structural block diagram of a code generation device based on a large model provided in an embodiment of the present invention. Detailed Implementation

[0022] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.

[0023] It is understood that the various numerical designations used in the embodiments of this invention are merely for descriptive convenience and are not intended to limit the scope of this application. The order of the process numbers does not imply the order of execution; the execution order of each process should be determined by its function and internal logic.

[0024] In embodiments of the invention, relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, without necessarily requiring or implying any such actual relationship or order between these entities or operations. The terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising..." does not exclude the presence of additional identical elements in the process, method, article, or apparatus that includes said element. The term "a plurality or several" refers to two or more.

[0025] Please see Figure 1 , Figure 1 This is a flowchart illustrating a code generation method based on a large model, provided by an embodiment of the present invention. The code generation method based on a large model can be executed by terminal devices such as servers, computers, and other computer-based devices. Specifically, the code generation method based on a large model includes: S11: Based on the target code requirement data, generate the initial code using the first large code model; The target code requirement data includes user-inputted code instructions, which are used to clarify the code generation goals (such as code function description, code input and output parameters, etc.), constraints (such as code implementation language, runtime environment, coding standards, etc.) and specific requirements (such as the logic, steps or algorithms for function implementation, code organization requirements, test case data, etc.) to the first code model.

[0026] For example, an example of code instructions to calculate and return the sum and product of all integers in a list is as follows: Write a Python function `sum_product(numbers: List[int]) ->Tuple[int, int]` to solve the following problem: For a given list of integers, return a tuple consisting of a sum and a product of all the integers in the list.

[0027] An empty list should have a sum of 0 and an empty product of 1.

[0028] >>>sum_product([]); (0, 1) / / Test case for sum_product, indicating that the sum_product function is called with an empty list as an argument, and the result (0, 1) is the expected output; >>>sum_product([1, 2, 3, 4]); (10, 24) / / This is a test case for `sum_product`, indicating that the `sum_product` function is called with the list [1, 2, 3, 4] as parameters, and the expected output is (10, 24). By inputting the user's code instruction into the first code model, a code response to that code instruction can be generated, i.e., the initial code (which can also be described as code snippet 1).

[0029] S12: Based on the initial code, test code is generated using the second code model to obtain unit test code; Inputting the generated initial code (code snippet 1) into the second large code model can generate corresponding unit test cases, i.e., unit test code (which can also be described as code snippet 2).

[0030] S13: Based on the unit test code and the initial code, perform code repair based on edit distance regularization using the third code big model to obtain the code to be analyzed; Based on the unit test code (code snippet 2), we can analyze whether there are syntax errors in the initial code (code snippet 1). Subsequently, we can use a third code big model to perform code repair based on edit distance regularization on the initial code to determine the code to be analyzed (which can also be described as code snippet 3).

[0031] S14: Iteratively optimize the code to be analyzed and the unit test code to obtain the target code corresponding to the target code requirement data.

[0032] For the code to be analyzed (also described as code snippet 3) and unit test code (also described as code snippet 2) obtained above, the code to be analyzed is iteratively optimized by repeatedly executing the unit test analysis and code repair process, thereby obtaining the target code corresponding to the target code requirement data. This embodiment of the invention uses a multi-model collaborative approach for code generation, unit test generation, and code repair. While ensuring that the generated code meets high coverage standards, it can effectively improve the reliability and accuracy of code generation, reduce software security vulnerabilities, thereby improving the stability and security of the software system, and significantly improving the efficiency and quality of software development. Simultaneously, by adopting a multi-model collaborative approach, code generation, unit test generation, and code repair can be executed in parallel, which can improve the utilization of system computing resources, avoid idle or wasted computer hardware resources, and improve computer operating efficiency.

[0033] It should be noted that the first code big model, the second code big model, and the third code big model can be code big models such as CodeLLM, CodeGen, InCoder, Code LLaMA, StarCoder, and Codex. The code big models used by the first code big model, the second code big model, and the third code big model can be the same or different, and no specific limitation is made in the embodiments of the present invention.

[0034] In this embodiment of the invention, the training process of the first, second, and third large code models is not specifically limited. For example, the first, second, and third large code models all use CodeLLM as an example. Multiple code instruction samples and corresponding verified correct code samples are collected. The code instruction samples are used as input and the correct code samples are used as labels. They are fed into Code LLM for model training to learn the syntax rules, variable naming habits, and logical structures (such as the nesting rules of loops and conditional branches) of the code, so as to obtain the predicted code. The model parameters of Code LLM are updated by calculating the loss between the predicted code and the correct code samples. The training is iterated until Code LLM converges to obtain the first large code model. Similarly, multiple verified correct code samples and corresponding unit test case samples (such as boundary test cases for null inputs, extreme value inputs, and error type inputs) are collected. The correct code samples are used as input, and the unit test case samples are used as labels, and fed into the Code LLM for model training to obtain predicted unit test code. The model parameters of the Code LLM are updated by calculating the loss between the predicted unit test code and the unit test case samples. This iterative training continues until the Code LLM converges, resulting in the second large code model. The training process for the third large code model is described below and will not be detailed here.

[0035] In an optional embodiment, S13: Based on the unit test code and the initial code, code repair based on edit distance regularization is performed using a third code large model to obtain the code to be analyzed, including: The initial code and the unit test code are input into a syntax analysis tool for code syntax analysis to obtain the syntax analysis results; If the syntax analysis result indicates the presence of a syntax error, the initial code and the syntax analysis result are input into the third code model for code repair based on edit distance regularization to obtain the code to be analyzed. If the syntax analysis result indicates that there are no syntax errors, the initial code is used as the code to be analyzed.

[0036] For example, the generated initial code (code snippet 1) and unit test code (code snippet 2) are fed into a syntax analysis tool, such as an inspection toolbox, so that the static analysis tool of the inspection toolbox can analyze the syntax of the code, such as the syntax of the initial code (code snippet 1) and the unit test code (code snippet 2), and obtain the syntax analysis results. The syntax analysis results are used to indicate whether there are syntax errors in the initial code (code snippet 1) and the unit test code (code snippet 2).

[0037] The review toolkit is a comprehensive platform integrating various code analysis tools, testing tools, and process management functions. It is used to conduct full-dimensional quality assessments of code, including syntactic correctness, logical integrity, security, and compliance. Static analysis tools, without running the code, identify potential problems by analyzing the code text, syntax structure, and logical flow. They do not require code execution; they complete the check simply by parsing the code's "static characteristics" (such as syntax rules, variable references, and control flow). This is the core functional module of the review toolkit.

[0038] Based on the results of the syntax analysis, determine whether there are any syntax errors in the initial code (code snippet 1).

[0039] If there is a syntax error, the initial code generated above (code snippet 1) and the syntax analysis results are sent to the third code model for code repair to obtain the repaired code (which can also be described as code snippet 3). The repaired code (code snippet 3) is used as the code to be analyzed in unit test analysis (which can also be described as code snippet 4). If there are no syntax errors, the initial code generated above (code snippet 1) will be used directly as the code to be analyzed in the unit test analysis (code snippet 4).

[0040] In this embodiment of the invention, the initial code automatically generated by user instructions and the corresponding unit test code are combined with static analysis tools to achieve self-checking and repair of syntax. This can intercept basic syntax errors, reduce resource waste, ensure the syntax correctness of the code to be analyzed, provide reliable basic input for subsequent steps, improve the accuracy of results such as unit test analysis, and avoid chain failures caused by basic errors.

[0041] In an optional embodiment, S14: Iterative optimization is performed on the code to be analyzed and the unit test code to obtain the target code corresponding to the target code requirement data, including: The code to be analyzed and the unit test code are input into the unit test analysis tool to perform unit test analysis and obtain the unit test analysis results. The unit test analysis results include: code execution feedback results and coverage analysis feedback results; the code execution feedback results are used to indicate whether the unit test code runs correctly; the coverage analysis feedback results are used to indicate the line coverage and branch coverage of the unit test code on the code to be analyzed.

[0042] If the unit test analysis results meet a preset first condition, the code to be analyzed is taken as the target code; wherein, the first condition includes: the code execution feedback results in the unit test analysis results indicate that the execution is correct, and the line coverage indicated by the coverage analysis feedback results is greater than or equal to a preset first threshold and the branch coverage is greater than or equal to a preset second threshold. If the unit test analysis results do not meet the first condition, the code to be analyzed and the unit test code are iteratively optimized, and the optimized code to be analyzed and the unit test code are subjected to unit test analysis until the first condition or the preset iterative condition is met.

[0043] Specifically, the iterative optimization of the code to be analyzed and the unit test code includes: If the unit test analysis results meet the preset second condition, the code to be analyzed and the code execution feedback results are input into the third code big model for code repair based on edit distance regularization, and the next code to be analyzed is generated as the code to be analyzed in this round of iteration optimization; The second condition includes: the code execution feedback results in the unit test analysis results indicate a runtime error.

[0044] If the unit test analysis results meet the preset third condition, the code to be analyzed and the coverage analysis feedback results are input into the second code big model to generate the next unit test code, which serves as the unit test code for this round of iterative optimization. The third condition includes: the code execution feedback result in the unit test analysis result indicates that the execution is correct, and the line coverage indicated by the coverage analysis feedback result is less than the first threshold and the branch coverage is less than the second threshold.

[0045] The iteration conditions include: the number of repetitions of unit test analysis is greater than a preset threshold; wherein, if the number of repetitions of unit test analysis reaches the threshold, but the unit test analysis result does not meet the first condition when the number of repetitions of unit test analysis reaches the threshold, the code to be analyzed and the code execution feedback result obtained in the last round are output.

[0046] In this embodiment of the invention, the code to be analyzed (code snippet 4) and the unit test code (code snippet 2) are fed together into a unit test analysis tool for unit test analysis. The unit test analysis tool is a dedicated tool used to evaluate the quality, effectiveness, and coverage of unit test code. Based on the unit test analysis results, the following process is performed: In the event of a runtime error, the code to be analyzed (code snippet 4) and the code execution feedback results (such as the code's execution status, execution results, and reasons for the execution error) are sent to the third code model for code repair, resulting in the next code to be analyzed. Then, the process returns to the previous step and sends the next code to be analyzed and the unit test code (code snippet 2) together to the unit test analysis tool for unit test analysis.

[0047] If the code runs correctly and the line coverage is greater than or equal to the first threshold (e.g., 80%) and the branch coverage is greater than or equal to the second threshold (e.g., 80%), then the code to be analyzed (code snippet 4) is considered reliable code and is output as target code.

[0048] If the operation is correct, and the line coverage is less than the first threshold (e.g., 80%) and the branch coverage is less than the second threshold (e.g., 80%), then the code to be analyzed (code snippet 4) and the coverage analysis feedback results (e.g., line coverage, branch coverage, etc.) are sent to the second large code model to generate the next unit test code, which will be used as the unit test code for this round of iteration optimization. Then, the process returns to the previous step, and the code to be analyzed (code snippet 4) and the next unit test code are sent together to the unit test analysis tool for unit test analysis.

[0049] The system will repeatedly execute the above review and repair process, setting a threshold for the number of repetitions (e.g., 3). If reliable code cannot be output within the limited number of repetitions (i.e., the unit test analysis results show that the line coverage is greater than or equal to the first threshold (e.g., 80%) and the branch coverage is greater than or equal to the second threshold (e.g., 80%)), then the code to be analyzed in the last round of repair and the code execution feedback results will be output, allowing users to intervene manually. This can prevent the system from having too low response efficiency and improve the efficiency of code generation.

[0050] This invention employs a multi-model collaborative approach, with each model responsible for code generation, unit test generation, and code repair. It combines the initial code automatically generated based on user-input code commands with corresponding unit test code, along with static analysis and dynamic testing tools, to achieve a self-review and iterative repair mechanism, ensuring the generation of reliable code. Figure 2 As shown, while meeting the high coverage standard, it achieves excellent reliability, reduces software security vulnerabilities, thereby improving the stability and security of the software system and significantly improving the efficiency and quality of software development.

[0051] In one optional embodiment, the loss function of the third code-large model includes a basic loss term and a regularization term based on token-granularity edit distance; the regularization term based on token-granularity edit distance is calculated based on insertion cost, deletion cost, and replacement cost.

[0052] In this embodiment of the invention, in the code repair scenario, the third code big model introduces a regularization term based on token-granularity edit distance, which can enhance the code repair capability and improve the accuracy of the system.

[0053] The training process of the third large code model includes: Construct multiple instruction pairs; wherein, the instruction pairs include error code samples and correct code samples; The error code samples of the instruction pairs are used as input, and the correct code samples of the instruction pairs are used as labels. These are fed into a pre-constructed third code model for model training until the preset convergence condition is met, resulting in a trained third code model.

[0054] Specifically, during the model training process of the third code big model, a regularization term and a basic loss term based on token granularity edit distance are calculated based on the correct code samples and the incorrect code samples. The total loss between the correct code sample and the incorrect code sample is calculated based on the regularization term based on the token-granularity edit distance, the basic loss term, and the hyperparameters. The model parameters of the third code-large model are updated using the total loss.

[0055] Specifically, the calculation process of the regularization term based on token-granularity edit distance includes: By comparing the correct code sample and the incorrect code sample, determine the number of insertion operations, deletion operations, and replacement operations required to get from the incorrect code sample to the correct code sample; Based on the number of insertion operations, the number of deletion operations, the number of replacement operations, and the insertion cost, deletion cost, and replacement cost, calculate the token-granularity edit distance-based regularization term between the correct code sample and the incorrect code sample.

[0056] For example, when enhancing code repair capabilities, the first step is to construct instruction pairs (i.e., prompt and response question-and-answer pairs), such as constructing appropriate instruction pairs from error code samples and correct code samples.

[0057] In code repair scenarios, the edit distance between erroneous and correct code is often very small, perhaps just a mismatched parenthesis or a lambda statement error. When the model is fine-tuned on instruction data in code repair scenarios, it directly uses the erroneous code as the predicted output, resulting in a low loss function value. This may mean that the model tends to simply copy the input as the output, rather than learning the actual mapping relationship between input and output. This makes the model appear to be successfully trained (low loss), but completely loses its actual code repair capability. Therefore, this embodiment of the invention introduces a token-based edit distance regularization term (i.e., a penalty term) into the loss function of the third code-based model. This makes the model focus on the tokens representing the root cause of the error, overcoming the model's "laziness" and improving its code repair capability.

[0058] At the token granularity, the edit distance is defined as follows: Insertion cost: The cost of inserting one token each time; Deletion cost: The cost of deleting a token each time; Replacement cost: The cost of replacing a token each time; For the sequence of correct code samples and a sequence of corresponding error code samples Given sequences of lengths m and n, the edit distance at the token granularity is calculated as follows: (1); in, Represents a sequence of error code samples Sequence to the correct code sample The total insertion cost is obtained by summing the insertion costs over the number of insertion operations; Represents a sequence of error code samples Sequence to the correct code sample The total deletion cost is obtained by summing the deletion costs over the number of deletion operations. Represents a sequence of error code samples Sequence to the correct code sample The total replacement cost is obtained by summing the replacement costs over the number of replacement operations.

[0059] The total loss function of the third-generation large model is as follows: (2); in, It is the basic loss term (e.g., calculated based on the cross-entropy loss function); It is a token-based edit distance regularization term that measures how well the predicted output is converted into correct code. The minimum operating cost. It is a hyperparameter used to balance the importance of the base loss term and the edit distance regularization term.

[0060] During the training process, the error code samples are input into the third code model, and the corresponding correct code samples are used as labels for model training. For the prediction output of each round of the model, the total loss is calculated by sampling the above formula (2). Based on the calculated total loss, the model parameters are adjusted by backpropagation, and the training process is repeated until the model converges.

[0061] This invention, by introducing a token-based edit distance regularization term into a large model, ensures the correctness of the generated code syntax and semantics while avoiding the model from simply copying or generating erroneous code that is too similar to the training data. This significantly improves the accuracy of code repair, solves complex code error problems, and enhances the model's code repair capabilities.

[0062] This invention employs a multi-model collaborative mechanism to automatically generate code and corresponding unit tests from user instructions. Combined with static analysis and dynamic testing tools, it achieves a self-review and iterative repair mechanism, ensuring that the generated code meets high coverage standards while achieving excellent reliability, significantly improving the efficiency and quality of software development. Simultaneously, by introducing a token-based edit distance regularization term into the large model, it ensures the correctness of the generated code syntax and semantics while avoiding the model simply copying or generating erroneous code that is too similar to the training data. This significantly improves the accuracy of code repair and enhances the automation and intelligence of code generation and repair.

[0063] See Figure 3 , Figure 3 This invention provides a structural block diagram of a code generation device based on a large model, the code generation device based on a large model comprising: The first code generation module 11 is used to generate initial code based on the target code requirement data and the first code big model. The second code generation module 12 is used to generate unit test code based on the initial code and through the second code model. Code repair module 13 is used to perform code repair based on edit distance regularization using a third code big model based on the unit test code and the initial code to obtain the code to be analyzed; The code optimization module 14 is used to iteratively optimize the code to be analyzed and the unit test code to obtain the target code corresponding to the target code requirement data.

[0064] In an optional embodiment, the code repair module 13 includes: The syntax analysis unit is used to input the initial code and the unit test code into the syntax analysis tool for code syntax analysis and to obtain the syntax analysis results. The first code repair unit is used to input the initial code and the syntax analysis result into the third code big model to perform code repair based on edit distance regularization when the syntax analysis result indicates that there is a syntax error, so as to obtain the code to be analyzed; The first code determination unit is used to take the initial code as the code to be analyzed when the syntax analysis result indicates that there is no syntax error.

[0065] In an optional embodiment, the code optimization module 14 includes: The unit test analysis unit is used to input the code to be analyzed and the unit test code into a unit test analysis tool for unit test analysis to obtain unit test analysis results. The unit test analysis results include: code execution feedback results and coverage analysis feedback results. The code execution feedback results are used to indicate whether the unit test code runs correctly. The coverage analysis feedback results are used to indicate the line coverage and branch coverage of the unit test code on the code to be analyzed. The target code generation unit is used to take the code to be analyzed as target code when the unit test analysis results meet the preset first conditions; wherein, the first conditions include: the code execution feedback results in the unit test analysis results indicate that the execution is correct, and the line coverage indicated by the coverage analysis feedback results is greater than or equal to a preset first threshold and the branch coverage is greater than or equal to a preset second threshold. The iterative optimization unit is used to perform iterative optimization on the code to be analyzed and the unit test code when the unit test analysis result does not meet the first condition, and to perform unit test analysis on the optimized code to be analyzed and the unit test code until the first condition or the preset iterative condition is met.

[0066] In one optional embodiment, the iterative optimization unit includes: The second code repair unit is used to input the code to be analyzed and the code execution feedback result into the third code big model to perform code repair based on edit distance regularization when the unit test analysis result meets the preset second condition, and generate the next code to be analyzed as the code to be analyzed in this round of iteration optimization; The second condition includes: the code execution feedback results in the unit test analysis results indicate a runtime error.

[0067] In one optional embodiment, the iterative optimization unit includes: The unit test code generation unit is used to input the code to be analyzed and the coverage analysis feedback result into the second code big model when the unit test analysis result meets the preset third condition, and generate the next unit test code as the unit test code for this round of iterative optimization; The third condition includes: the code execution feedback result in the unit test analysis result indicates that the execution is correct, and the line coverage indicated by the coverage analysis feedback result is less than the first threshold and the branch coverage is less than the second threshold.

[0068] In an optional embodiment, the iteration condition includes: the number of repetitions of unit test analysis is greater than a preset threshold; wherein, when the number of repetitions of unit test analysis reaches the threshold, but the unit test analysis result does not meet the first condition when the number of repetitions of unit test analysis reaches the threshold, the code to be analyzed and the code execution feedback result obtained in the last round are output.

[0069] In one optional embodiment, the loss function of the third code-large model includes a basic loss term and a regularization term based on token-granularity edit distance; the regularization term based on token-granularity edit distance is calculated based on insertion cost, deletion cost, and replacement cost.

[0070] In an optional embodiment, the device further includes: An instruction pair construction module is used to construct multiple instruction pairs; wherein, the instruction pair includes error code samples and correct code samples; The model training module is used to take the error code samples of the instruction pair as input and the correct code samples of the instruction pair as labels, and feed them into the pre-built third code large model for model training until the preset convergence condition is met, so as to obtain the trained third code large model.

[0071] In an optional embodiment, the device further includes: The first loss calculation module is used to calculate the regularization term and the basic loss term based on the token granularity edit distance during the model training process of the third code big model, according to the correct code samples and the incorrect code samples. The second loss calculation module is used to calculate the total loss between the correct code sample and the incorrect code sample based on the token-granularity edit distance regularization term, the basic loss term, and the hyperparameters. The model parameter update module is used to update the model parameters of the third code model using the total loss.

[0072] In an optional embodiment, the first loss calculation module includes: The operation count determination unit is used to compare the correct code sample and the error code sample to determine the number of insertion operations, deletion operations, and replacement operations required to go from the error code sample to the correct code sample. The regularization loss calculation unit is used to calculate the regularization term based on token-granularity edit distance between the correct code sample and the incorrect code sample, according to the number of insertion operations, the number of deletion operations, the number of replacement operations, and the insertion cost, the deletion cost, and the replacement cost.

[0073] It should be noted that the working process of each module in the code generation device based on a large model described in the embodiments of the present invention can refer to the working process of the code generation method based on a large model described in the above embodiments, and the technical effect achieved is the same as that of the code generation method based on a large model described in the above embodiments, which will not be repeated here.

[0074] See Figure 4 , Figure 4 This is a structural block diagram of a code generation device based on a large model provided in an embodiment of the present invention. The code generation device includes a processor 21, a memory 22, and a computer program stored in the memory 22 and executable on the processor 21. When the processor 21 executes the computer program, it implements the steps in the various embodiments of the code generation method based on the large model described above, such as steps S11 to S14.

[0075] For example, the computer program may be divided into one or more modules / units, which are stored in the memory 22 and executed by the processor 21 to complete the present invention. The one or more modules / units may be a series of computer program instruction segments capable of performing specific functions, which describe the execution process of the computer program in the large-model-based code generation device.

[0076] The large-model-based code generation device may include, but is not limited to, a processor 21 and a memory 22. Those skilled in the art will understand that the schematic diagram is merely an example of a large-model-based code generation device and does not constitute a limitation on the device. It may include more or fewer components than illustrated, or combine certain components, or use different components. For example, the large-model-based code generation device may also include input / output devices, network access devices, buses, etc.

[0077] The processor 21 can be a Central Processing Unit (CPU), or other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor can be a microprocessor or any conventional processor. The processor 21 is the control center of the large-model-based code generation device, connecting all parts of the device via various interfaces and lines.

[0078] The memory 22 can be used to store the computer programs and / or modules. The processor 21 implements various functions of the large-model-based code generation device by running or executing the computer programs and / or modules stored in the memory 22 and calling the data stored in the memory 22. The memory 22 may mainly include a program storage area and a data storage area. The program storage area may store the operating system, at least one application program required for a function (such as sound playback function, image playback function, etc.), etc.; the data storage area may store data created according to the use of the mobile phone (such as audio data, phonebook, etc.). In addition, the memory 22 may include high-speed random access memory, and may also include non-volatile memory, such as hard disk, memory, plug-in hard disk, smart media card (SMC), secure digital (SD) card, flash card, at least one disk storage device, flash memory device, or other volatile solid-state storage device.

[0079] Wherein, if the modules / units integrated by the code generation device based on the large model are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, all or part of the processes in the methods of the above embodiments of the present invention can also be implemented by a computer program instructing related hardware. The computer program can be stored in a computer-readable storage medium, and when the computer program is executed by the processor 21, it can implement the steps of the various method embodiments described above. Wherein, the computer program includes computer program code, which can be in the form of source code, object code, executable file, or some intermediate form, etc. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, portable hard drive, magnetic disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal, and software distribution medium, etc.

[0080] It should be noted that the device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Furthermore, in the accompanying drawings of the device embodiments provided by this invention, the connection relationships between modules indicate that they have communication connections, which can be specifically implemented as one or more communication buses or signal lines. Those skilled in the art can understand and implement this without any creative effort.

[0081] The above description represents the preferred embodiments of the present invention. It should be noted that, for those skilled in the art, various improvements and modifications can be made without departing from the principles of the present invention, and these improvements and modifications are also considered to be within the scope of protection of the present invention.

Claims

1. A code generation method based on a large model, characterized in that, include: Based on the target code requirements data, code is generated using the first large code model to obtain the initial code; Based on the initial code, test code is generated using the second large code model to obtain unit test code; Based on the unit test code and the initial code, code repair based on edit distance regularization is performed using a third code big model to obtain the code to be analyzed. The code to be analyzed and the unit test code are iteratively optimized to obtain the target code corresponding to the target code requirement data.

2. The code generation method based on a large model as described in claim 1, characterized in that, The step involves performing code repair based on edit distance regularization using a third large code model, based on the unit test code and the initial code, to obtain the code to be analyzed, including: The initial code and the unit test code are input into a syntax analysis tool for code syntax analysis to obtain the syntax analysis results; If the syntax analysis result indicates the presence of a syntax error, the initial code and the syntax analysis result are input into the third code model for code repair based on edit distance regularization to obtain the code to be analyzed. If the syntax analysis result indicates that there are no syntax errors, the initial code is used as the code to be analyzed.

3. The code generation method based on a large model as described in claim 1, characterized in that, The iterative optimization of the code to be analyzed and the unit test code to obtain the target code corresponding to the target code requirement data includes: The code to be analyzed and the unit test code are input into a unit test analysis tool for unit test analysis to obtain unit test analysis results. The unit test analysis results include: code execution feedback results and coverage analysis feedback results. The code execution feedback results are used to indicate whether the unit test code runs correctly. The coverage analysis feedback results are used to indicate the line coverage and branch coverage of the unit test code on the code to be analyzed. If the unit test analysis results meet a preset first condition, the code to be analyzed is taken as the target code; wherein, the first condition includes: the code execution feedback results in the unit test analysis results indicate that the execution is correct, and the line coverage indicated by the coverage analysis feedback results is greater than or equal to a preset first threshold and the branch coverage is greater than or equal to a preset second threshold. If the unit test analysis results do not meet the first condition, the code to be analyzed and the unit test code are iteratively optimized, and the optimized code to be analyzed and the unit test code are subjected to unit test analysis until the first condition or the preset iterative condition is met.

4. The code generation method based on a large model as described in claim 3, characterized in that, The iterative optimization of the code to be analyzed and the unit test code includes: If the unit test analysis results meet the preset second condition, the code to be analyzed and the code execution feedback results are input into the third code big model for code repair based on edit distance regularization, and the next code to be analyzed is generated as the code to be analyzed in this round of iteration optimization; The second condition includes: the code execution feedback results in the unit test analysis results indicate a runtime error.

5. The code generation method based on a large model as described in claim 3, characterized in that, The iterative optimization of the code to be analyzed and the unit test code includes: If the unit test analysis results meet the preset third condition, the code to be analyzed and the coverage analysis feedback results are input into the second code big model to generate the next unit test code, which serves as the unit test code for this round of iterative optimization. The third condition includes: the code execution feedback result in the unit test analysis result indicates that the execution is correct, and the line coverage indicated by the coverage analysis feedback result is less than the first threshold and the branch coverage is less than the second threshold.

6. The code generation method based on a large model as described in claim 3, characterized in that, The iteration conditions include: the number of repetitions of unit test analysis is greater than a preset threshold; wherein, if the number of repetitions of unit test analysis reaches the threshold, but the unit test analysis result does not meet the first condition when the number of repetitions of unit test analysis reaches the threshold, the code to be analyzed and the code execution feedback result obtained in the last round are output.

7. The code generation method based on a large model as described in claim 1 or 4, characterized in that, The loss function of the third code model includes a basic loss term and a regularization term based on token-granularity edit distance; the regularization term based on token-granularity edit distance is calculated based on insertion cost, deletion cost, and replacement cost.

8. The code generation method based on a large model as described in claim 7, characterized in that, The training process of the third code-based large model includes: Construct multiple instruction pairs; wherein, the instruction pairs include error code samples and correct code samples; The error code samples of the instruction pairs are used as input, and the correct code samples of the instruction pairs are used as labels. These are fed into a pre-constructed third code model for model training until the preset convergence condition is met, resulting in a trained third code model.

9. The code generation method based on a large model as described in claim 8, characterized in that, The method further includes: During the training process of the third code big model, a regularization term and a basic loss term based on token-granularity edit distance are calculated based on the correct code samples and the incorrect code samples. The total loss between the correct code sample and the incorrect code sample is calculated based on the regularization term based on the token-granularity edit distance, the basic loss term, and the hyperparameters. The model parameters of the third code-large model are updated using the total loss.

10. The code generation method based on a large model as described in claim 9, characterized in that, The calculation process of the regularization term based on token-granularity edit distance includes: By comparing the correct code sample and the incorrect code sample, determine the number of insertion operations, deletion operations, and replacement operations required to get from the incorrect code sample to the correct code sample; Based on the number of insertion operations, the number of deletion operations, the number of replacement operations, and the insertion cost, deletion cost, and replacement cost, calculate the token-granularity edit distance-based regularization term between the correct code sample and the incorrect code sample.

11. A code generation device based on a large model, characterized in that, include: The first code generation module is used to generate initial code based on the target code requirement data and the first code big model. The second code generation module is used to generate unit test code based on the initial code and through the second code model. The code repair module is used to perform code repair based on edit distance regularization using a third code big model based on the unit test code and the initial code to obtain the code to be analyzed. The code optimization module is used to iteratively optimize the code to be analyzed and the unit test code to obtain the target code corresponding to the target code requirement data.

12. A code generation device based on a large model, characterized in that, include: A processor, a memory, and a computer program stored in the memory and configured to be executed by the processor, wherein the processor, when executing the computer program, implements the code generation method based on a large model as described in any one of claims 1 to 10.

13. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein, when the computer program is executed, it controls the device where the computer-readable storage medium is located to perform the code generation method based on a large model as described in any one of claims 1 to 10.

14. A computer program product comprising a computer program / instructions, characterized in that, When the computer program / instruction is executed by the processor, it implements the code generation method based on a large model as described in any one of claims 1 to 10.

Citation Information

Patent Citations

  • Code automatic generation method and device, equipment and storage medium

    CN117492726A

  • Code generation method, code generation model and code modification model training method

    CN118981317A

  • Method and device for determining code patch, equipment and storage medium

    CN120162243A

  • Conversational unit test generation using large language model

    WO2024191709A1

Cited By

  • Code generation method, power line loss abnormality diagnosis method, electronic device, storage medium and program product

    CN121541863A

  • Iterative chart code generation method based on large chart and code error correction model

    CN122261566A

  • Iterative chart code generation method based on chart and code correction large model

    CN122261566B