Feedback iteration type code reconstruction method based on large language model
By combining the feedback iterative code reconstruction method of large language models and enhanced genetic algorithms, the problems of low efficiency, limited optimization range and lack of evolutionary optimization capabilities of traditional code reconstruction methods are solved, and intelligent and automated code reconstruction is realized, improving code quality and development efficiency.
Patent Information
- Application Number
- CN202510100613.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-22
- Publication Date
- 2025-05-16
AI Technical Summary
Traditional code reconstruction methods rely on manual analysis and modification, have low efficiency, limited optimization range, and lack evolutionary optimization capabilities, making it difficult to meet the rapid iteration needs of modern software development.
The feedback iterative code reconstruction method based on the large language model is adopted, combined with the enhanced genetic algorithm, and the code problems are automatically analyzed and optimized through initial population evaluation, population selection, evolutionary operator application and iterative termination conditions.
It significantly improves the intelligence level of code reconstruction, expands the optimization scope, improves the reconstruction efficiency, can automatically analyze code problems, and generate code with lower circle complexity, clearer logical structure, higher readability and maintainability.
Smart Images

Figure CN120010906A_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a feedback iterative code reconstruction method based on a large language model, and belongs to the field of computer software maintenance and evolution. Background Art
[0002] As the complexity of software development continues to increase, the readability and maintainability of code have become particularly important; code refactoring, as a process of reorganizing code to improve its structure without changing its external behavior, has been widely used in software engineering. However, traditional code refactoring has the following problems: 1. Over-reliance on manual work: Traditional code refactoring usually requires developers to manually identify problems in the code and modify them, which relies heavily on the experience and ability of developers, and manual analysis and modification are easily affected by subjective judgment, which may result in incomplete optimization; 2. Limited optimization scope: Traditional refactoring methods are mostly limited to the optimization of a single goal (such as improving code readability or reducing redundancy), and lack systematic and comprehensive consideration of multi-dimensional indicators; at the same time, fixed rules are often used in the optimization process, which lacks flexibility; 3. Low efficiency: Manual refactoring requires developers to analyze the code section by section, and the automation capabilities of traditional tools are limited; as modern software development requires higher and higher iteration speeds, traditional methods are difficult to keep up with the pace of rapid delivery; 4. Lack of evolutionary optimization capabilities: Traditional methods are often based on static rules and pattern matching, and cannot dynamically adjust optimization strategies, which results in traditional tools lacking the ability to explore further once they fall into a local optimum.
[0003] The Chinese invention with the publication number CN118409741A discloses a code generation method based on a large language model, including the following sub-steps: S1: receiving the data model layer generated by the user's input requirement document and the database field, and taking the received data as the source text; S2: forming prompt words according to the source text, the ChatGLM2 model obtains the function signature, and outputs the text content; S3: forming prompt phrases according to the source text, the WizardCoder model obtains the file path and code content, and outputs the text content; S4: constructing folders and files according to the code path, writing the code content, and obtaining the conversion of the target programming language. This scheme uses the large language model as a white box tool for code generation, and adjusts and trains the parameters and gradients of the large language model, which undoubtedly puts forward higher professional requirements for users and is not convenient for widespread use; and this method does not have the ability to self-adjust, and lacks the ability to optimize code generation. Summary of the invention
[0004] The technical problem to be solved by the present invention is to provide a feedback iterative code refactoring method based on a large language model, which realizes automatic refactoring of codes, expands the optimization range of codes, improves refactoring efficiency and has evolutionary optimization capabilities.
[0005] The present invention adopts the following technical solution:
[0006] The feedback iterative code reconstruction method based on a large language model of the present invention adopts the following steps:
[0007] S1. Create an initial population and evaluate fitness: Use an enhanced genetic algorithm to combine the code provided by the user with the initial prompt template, deliver it to the large language model to reconstruct the code, and use the code returned by the large language model as the reconstructed code; evaluate the fitness of the reconstructed code as the fitness of the initial prompt template used by the code, and determine whether it meets the termination condition. If it does, output the reconstructed code with the highest fitness; if not, put the initial prompt template and its fitness as the initial population into the prompt pool for iteration;
[0008] S2. Selecting population individuals: using the prompt template in the prompt pool as the parent population; selecting individuals in the parent population as individuals to be evolved to participate in subsequent evolution;
[0009] S3, judging whether to enable the super mutation operator: judging whether the super mutation operator needs to be enabled by the fitness of the individual to be evolved, and then selecting an evolution operator. If the super mutation operator is enabled, the evolution operator is supermutated, and the evolution operator after the super mutation is used as the evolution operator to be used. If the super mutation operator is not enabled, the evolution operator is directly used as the evolution operator to be used.
[0010] S4, using the evolution operator: using the evolution operator to be used and the individual to be evolved to combine and deliver the large language model and generate a new prompt, using the new prompt as the offspring individual, then similar to S1, combining the offspring individual with the code to be reconstructed and delivering it to the large language model to generate the reconstructed code, and evaluating the reconstructed code generated by the offspring individual to obtain the fitness of the offspring individual;
[0011] S5. Check whether the iteration meets the termination condition. If so, stop the iteration and output the population individual with the highest fitness in the current prompt pool and its corresponding reconstructed code. If not, adopt the high-score retention strategy to compare with the fitness of the parent individuals in the prompt pool to retain the high-fitness individuals to update the prompt pool, and return to S2 for the next round of iteration.
[0012] The fitness evaluation in S1 of the present invention specifically adopts the following steps:
[0013] S101, using the test case provided by the user to test whether the refactored code maintains the same function, if it passes the test case, the next step of evaluation is carried out, otherwise the fitness score of the refactored code is zero;
[0014] S102, using CodeBLEU to compare the codes before and after reconstruction. If the weighted N-gram score of this indicator is higher than 0.9, it is judged that the two pieces of code are similar, and the reconstruction does not make enough changes, and the fitness score of the reconstructed code is zero;
[0015] S103. Compare the cyclomatic complexity, number of code lines, number of code characters, and PyLint score of the code before and after the refactoring to obtain a comprehensive change evaluation of the refactored code, and standardize the evaluation into a fitness score.
[0016] The operation of selecting the individual to be evolved in S2 of the present invention is based on the fitness of the parent individual, and a selection operator is randomly selected to determine the individual to be evolved;
[0017] The selection operators include a full random selection operator, a tournament selection operator, a roulette selection operator and a random traversal sampling operator, and one of them is randomly selected during use.
[0018] The evolutionary operators in S3 of the present invention include direct mutation operators, crossover operators and quasi-differential evolution operators; specifically, they are as follows:
[0019] Direct mutation operator: Through the natural language understanding and processing capabilities of LLM, a large language model is used to understand and replace the natural language in the parent prompt template to generate a new prompt template;
[0020] Crossover operator: drives the large language model to extract, transform and combine the elements of different prompt templates, so that the generated new prompt template retains the core information of the original prompt and has a new expression form;
[0021] Quasi-differential evolution operator: select individuals from two prompt pools, and use the idea of differential evolution algorithm to use these two individuals to drive the large language model to generate new individuals. Specifically: the elite individual is analogous to the basis vector in the differential evolution algorithm, and two individuals selected from the prompt pool are delivered to make the large language model find the difference between the two prompts as the difference vector, and then use the large language model to mutate them to generate mutation prompts as mutation vectors. Finally, a randomly selected individual is used to cross with the elite individual to generate the final prompt template.
[0022] The hypermutation operator in S3 of the present invention is specifically: performing fitness analysis on the reconstructed code, finding the items with the lowest scores in cyclomatic complexity, number of code lines, number of code characters and readability in its fitness score as the main problem of the code, and adding a prompt segment in the evolution operator according to the problem to guide it to generate prompt templates more targetedly.
[0023] The iteration termination condition in S5 of the present invention is two conditions, namely, a preset fitness threshold and a maximum number of iterations. If either of the two conditions is satisfied, the iteration is stopped.
[0024] The present invention S103 evaluates the code by three indicators: the number of code characters, the number of code lines, or the cyclomatic complexity, specifically using the following steps:
[0025] S1011, extracting the measurement information of the code before refactoring is called baseline, and then extracting the measurement information of the code after refactoring is called current;
[0026] S1012. By analyzing the number of code lines and characters, a scaling factor is set, where k is a dynamic value. Adjusting the k value can scale the sigmoid function. The difference value difference is obtained by combining the difference impact factor and the difference between baseline and current:
[0027] difference=scaling_factor*(baseline–current)
[0028] S1013. Use sigmoid function Map the difference to (0,1) to obtain the fitness S i , where i is the number of characters (char), lines of code (LOC), or cyclomatic complexity (CC); the process is:
[0029] S i =sigmoid(difference)
[0030] The adjusted k value is as follows: k CC =11, k char =8.8, k LOC =6.27; so define S CC ,S char With S LOC as follows:
[0031]
[0032] The scoring strategy for PyLint is as follows: Since the code must pass the test case before the comprehensive information feedback, the weights of Error and Warning are reduced in the Pylint configuration file, and the scores of Refactor and Convention are focused on, because these two scores can well reflect the readability, dependency complexity and code duplication information of the code; accordingly, the internal weight of PyLint is changed to obtain the weighted PyLint fitness S PyLint, where fatal stands for fatal error, error stands for error, warning stands for warning, refactor stands for refactoring suggestion, convention stands for violation of convention, and statement stands for baseline value. For the specific meaning of the parameters, please refer to the official documentation of PyLint on GitHub:
[0033]
[0034] Through these summary information, the comprehensive fitness score S of the improved code is obtained with weights of 1, 1, 3.5, and 4.5. CC With S PyLint is a ten-point system, and S Char , S LOC After unifying it into a one-point system, we can get:
[0035] S=1*(S Char +S LOC )*10+0.35*S CC +0.45*S PyLint .
[0036] The fitness threshold of the present invention is 7.8, and the maximum number of iterations is 6.
[0037] The positive effects of the present invention are as follows: the present invention significantly improves the intelligent level of code reconstruction by combining a large language model with an enhanced genetic algorithm; automatically analyzes code problems based on multi-dimensional evaluation indicators including cyclomatic complexity, number of code lines, number of code characters and PyLint scores; the entire process from prompt template generation to code optimization does not require human intervention, greatly reducing development time and cost; the optimized code has lower cyclomatic complexity, clearer logical structure, higher readability and maintainability; and reduces errors introduced by manual operations through automated processes, thereby improving the reliability of code quality.
[0038] The present invention can provide an efficient and intelligent code reconstruction solution for modern software development, expand the scope of code optimization, improve reconstruction efficiency, and provide strong technical support for improving development efficiency and optimizing code quality. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Attached Figure 1 It is a schematic diagram of the structure of the implementation process of the present invention;
[0040] Attached Figure 2 This is a schematic diagram of the fitness evaluation process structure of the present invention;
[0041] Attached Figure 3 Schematic diagram of the evolution operator and hypermutation operator structure of the present invention;
[0042] Attached Figure 4 It is a schematic diagram of the structure of the selection method of the present invention;
[0043] Attached Figure 5 This is a schematic diagram of the initialization structure of the present invention;
[0044] Attached Figure 6 The present invention provides a schematic diagram of the evolutionary algorithm structure. DETAILED DESCRIPTION
[0045] As attached Figure 1 As shown, the feedback iterative code reconstruction method based on a large language model of the present invention adopts the following steps:
[0046] S1. Create the initial population and evaluate the fitness: Adopt the idea of enhanced genetic algorithm, combine the code provided by the user with the initial prompt template, deliver it to the large language model to reconstruct the code, and use the code returned by the large language model as the reconstructed code. Evaluate the fitness of the reconstructed code as the fitness of the initial prompt template used by the code, and judge whether it meets the termination condition. If it meets the condition, output the reconstructed code with the highest fitness; if not, put the initial prompt template and its fitness as the initial population into the prompt pool for iteration;
[0047] The method for evaluating the fitness of refactored code uses the following steps:
[0048] S101, using the test case provided by the user to test whether the refactored code maintains the same function, if it passes the test case, the next step of evaluation is carried out, otherwise the fitness score of the refactored code is zero;
[0049] S102. Compare the codes before and after reconstruction using CodeBLEU. If the weighted N-gram score of this indicator is too high, the two codes are judged to be similar, that is, the reconstruction does not make enough changes, and the fitness score of the reconstructed code is zero.
[0050] S103. Compare the cyclomatic complexity, number of code lines, number of code characters, and PyLint score of the code before and after the refactoring to obtain a comprehensive change evaluation of the refactored code, and standardize the evaluation into a fitness score.
[0051] S2. Selecting individuals from the population: using the prompt template in the prompt pool as the parent population; selecting individuals from the parent population as individuals to be evolved to participate in subsequent evolution; the operation of selecting individuals to be evolved is based on the fitness of the parent individuals, and randomly selecting a selection operator to determine the selected parent individuals; the selection operators include a full random selection operator, a tournament selection operator, a roulette selection operator, and a random traversal sampling operator, and one of them is randomly selected during use;
[0052] S3, judging whether to enable the super mutation operator: judging whether the super mutation operator needs to be enabled by the fitness of the individual to be evolved, and then selecting an evolution operator. If the super mutation operator is enabled, the evolution operator is supermutated, and the evolution operator after the super mutation is used as the evolution operator to be used. If the super mutation operator is not enabled, the evolution operator is directly used as the evolution operator to be used.
[0053] The evolution operator is specifically: the evolution operator is essentially a prompt; through the evolution operator, the large language model is combined with the existing prompt template to generate a new prompt template, completing an evolution operation; the evolution operator of the present invention includes: a direct mutation operator, a crossover operator and a quasi-differential evolution operator; specifically as follows:
[0054] Direct mutation operator: relying on the strong natural language understanding and processing capabilities of LLM, the present invention uses a large language model to understand and synonymously replace the natural language in the parent prompt template, thereby generating a new prompt template;
[0055] Crossover operator: drives the large language model to extract, transform and combine the elements of different prompt templates, so that the generated new prompt template retains the core information of the original prompt and has a new expression form;
[0056] Quasi-differential evolution operator: select individuals (prompt templates) from two prompt pools, and use the idea of differential evolution algorithm to use these two individuals to drive the large language model to generate new individuals. Specifically: the elite individual is analogous to the basis vector in the differential evolution algorithm, and two individuals selected from the prompt pool are delivered to make the large language model find the difference between the two prompts as the difference vector, and then the large language model is used to mutate them to generate a mutation prompt as the mutation vector. Finally, a randomly selected individual is used to cross with the elite individual to generate the final prompt template.
[0057] The super mutation operator specifically analyzes the main problems of the code through the code fitness, and adds some natural language to the target evolution operator according to the problem to guide it to generate prompt templates more targetedly; because it mutates the operator applied to the evolution operator, it is called the super mutation operator;
[0058] S4, using evolutionary operators: using the evolutionary operators to be used and the individuals to be evolved to form a prompt and delivering it to the large language model so that it generates a new prompt, and using the new prompt as the offspring individual; then similar to S1, combining the offspring individual with the code to be reconstructed and delivering it to the large language model so that it generates the reconstructed code, and evaluating the reconstructed code generated by the offspring individual to obtain the fitness of the offspring individual;
[0059] S5. Check whether the iteration meets the termination condition. If so, stop the iteration and output the population individual with the highest fitness in the current prompt pool and its corresponding reconstructed code. If not, use the high-score retention strategy to compare the fitness of the parent individual in the prompt pool to update the prompt pool, and return to S2 for the next round of iteration. The specific fitness threshold is 7.8, and the maximum number of iterations is 6.
[0060] As attached Figure 2 As shown, the reconstructed code of the present invention and the prompt template used in the reconstruction are used as input to generate the fitness of the prompt template. The fitness evaluation is specifically as follows: first, a code function test is performed to test whether the reconstructed code can pass the test case provided by the user to ensure its functional correctness, and then CodeBLEU (CodeBLEU is an indicator for automatically evaluating the comprehensive quality of the code) is used to compare the two code segments, and the weight parameter of CodeBLEU is set to (0, 1, 0, 0); that is, the weighted N-gram indicator is used to compare and evaluate the two code segments; if the functional test fails, or the score of CodeBLEU reaches 0.95 or above, the fitness of the reconstructed code is 0, otherwise the code is evaluated by the three indicators of the number of code characters (char), the number of lines of code (LOC) or the cyclomatic complexity (CC), and the following steps are specifically used:
[0061] S1011, extracting the measurement information of the code before reconstruction, called baseline (i.e., the benchmark value, used for comparison with the measurement information value of the code after reconstruction), and then extracting the measurement information of the code after reconstruction, called current (i.e., the current value, used for comparison with the measurement information value of the code before reconstruction);
[0062] S1012. By analyzing the number of code lines and characters, a scaling factor is set, where k is a dynamic value. Adjusting the k value can scale the sigmoid function to achieve the desired effect. The difference value difference is obtained by combining the difference influencing factor and the difference between baseline and current:
[0063] difference=scaling_factor*(baseline–current)
[0064] S1013. Use sigmoid function Map the difference to (0,1) to obtain the fitness S i , where i is the number of characters (char), lines of code (LOC), or cyclomatic complexity (CC); the process is:
[0065] S i=sigmoid(difference)
[0066] Although the evaluation logic for the number of lines of code and the number of characters is consistent, there are slight differences in the setting of scaling_factor: in real code, it is reasonable to optimize 10% to 30% of the number of characters and 15% to 40% of the number of lines of code. Excessive changes may affect the function of the program. The k value is adjusted to get a score of 0.9 when it is close to the upper limit of the ideal situation (cyclomatic complexity is reduced by 20%, the number of characters is reduced by 25%, and the number of lines of code is reduced by 35%). Combined with the slow speed of the sigmoid function approaching the mapping upper limit, this avoids encouraging LLM to over-optimize these two indicators and damage the code function; the adjusted k value is as follows: k CC =11, k char =8.8, k LOC =6.27; so define S CC ,S char With S LOC as follows:
[0067]
[0068] The present invention uses the following scoring strategy for PyLint: since the code must pass the test case before the comprehensive information feedback, the weights of Error and Warning are reduced in the Pylint configuration file, and the scores of Refactor and Convention are focused on, because these two scores can well reflect the readability, dependency complexity and code duplication information of the code; accordingly, the internal weight of PyLint is changed to obtain the weighted PyLint fitness S PyLint : fatal represents fatal error, error represents error, warning represents warning, refactor represents refactoring suggestion, convention represents violation of convention, statement represents baseline value, and the specific meaning of the parameters can be found in the official documentation of PyLint on GitHub:
[0069]
[0070] Through this summary information, with weights of 1, 1, 3.5, and 4.5 (S Char S LOC With S PyLint The comprehensive fitness score S of the improved code is obtained by unifying it into a ten-point system:
[0071] S=1*(S Char +S LOC )*10+0.35*S CC +0.45*S PyLint .
[0072] As attached Figure 3 The figure shows the logical relationship between the hypermutation operator, the hint optimization and the evolution operator, that is, the hint optimization is to refactor the code of the large language model through the hint (the hint is formed after the hint template is combined with the initial code to be refactored); the evolution enables the large language model to rewrite the hint through the evolution operator; the hypermutation operator modifies the evolution operator so that it can drive the large language model to rewrite the hint more specifically; among them, the direct hint optimization puts the code to be refactored into the hint template, delivers it to the large language model, and uses it as a black box tool to refactor the code, obtains the fitness of the refactored code and associates it back to the hint used; the evolution implementation is how the evolution operator acts on the hint template: the evolution operator is combined with the hint template, the large language model is delivered, and the hint template is evolved using its powerful natural language understanding and natural language processing capabilities; an evolution hint optimization consists of The invention is composed of mutation implementation and direct prompt optimization: firstly, the prompt template is evolved, and then the code is reconstructed using the evolved prompt template; super mutation implementation: according to the fitness of the reconstructed code, the low-scoring items in the fitness composition are checked, and a language segment that helps to make up for the shortcomings of the code is added to the evolution operator, and then the evolution operator after super mutation is used in combination with the prompt template to deliver a large language model, so that it can perform prompt evolution more targetedly to achieve the purpose of code reconstructing. For example, if the cyclomatic complexity score of the reconstructed code is too low, the sentence "At the same time, the prompt needs to instruct the large language model to reduce the cyclomatic complexity of the code" is added after the evolution operator to increase the targetedness of the generated prompt. The multi-dimensional evaluation indicators based on cyclomatic complexity, number of code lines, number of code characters and PyLint score enable the invention to automatically analyze code problems.
[0073] As attached Figure 4 As shown, there are four selection methods used in the enhanced genetic algorithm of the present invention for selection, including selecting individuals to be evolved in S2, selecting evolution operators to be used for evolution in S3, and selecting elite individuals in S4 to use elite mutation operators and quasi-differential evolution operators and selecting better individuals to update the prompt pool; that is, the present invention applies the method for selecting individuals to be evolved, the method for selecting evolution operators to be used, the elite designation method and the prompt pool update method;
[0074] Among them, in S2, the evolutionary operator selection method is to be used to randomly select one of four traditional selection operators based on the fitness of the prompt template to extract the mutation participant in the prompt pool. This method maximizes the chance of low fitness operators being selected while ensuring that high fitness operators have a high selection rate, so as to avoid falling into the local optimum; the traditional selection operators include a full random selection operator, a roulette selection operator, a tournament selection operator and a random traversal sampling operator;
[0075] The method for selecting the evolutionary operator to be used in S3 is: using a fully random strategy to extract the evolutionary operator to mutate the selected parent individual.
[0076] The elite designation method used by the elite mutation operator and the quasi-differential evolution operator described in S4 is: elite individuals are selected by the following conditions:
[0077] 1. Individuals with low CodeBLEU-(0,1,0,0) weight parameter scores and high CodeBLEU-(0,0,0.5,0.5) weight parameter scores will be labeled as "key", because this means that the refactored code has a large structural difference while maintaining the same function as the original code;
[0078] 2. Select the individual with the highest fitness among the individuals with key tags as the elite individual;
[0079] 3. If there is no key individual, the individual with the highest fitness is selected as the elite individual.
[0080] Finally, in S4, the prompt pool update algorithm selects the prompt template to be added to the prompt pool according to the following conditions:
[0081] 1. Pass the functional test;
[0082] 2. CodeBLEU-(0,1,0,0) weight parameter score meets the requirements;
[0083] 3. The comprehensive fitness score is higher than the prompt templates in the existing prompt pool.
[0084] As attached Figure 5 As shown, the present invention uses the code to be improved provided by the user as the initial code, combines it with the initial prompt template and delivers it to the large language model (see the attached Figure 3 The prompt template used is added to the prompt pool for subsequent prompt evolution algorithm to use, so that the large language model can be used as a refactoring tool to generate refactored code. Figure 2 The fitness evaluation shown compares the initial code with the refactored code to evaluate the refactored code and generate the fitness of the code, and associates the fitness back to the hint template used for the code refactoring, and saves the hint template and its fitness.
[0085] As attached Figure 6 As shown, through the attached Figure 4 The selection method shown is in the prompt pool (the first round of iteration is attached Figure 5 The prompt templates and evolution operators to be evolved are extracted from all available evolution operators, and the fitness information of the prompt templates to be evolved is used to determine whether to perform the following steps on the evolution operators: Figure 3If a super mutation is performed, the evolution operator after the super mutation is used as the evolution operator to be used, and the prompt template and the evolution operator to be used are combined into a complete prompt and delivered to the large language model to generate a new prompt. This completes a prompt evolution. Subsequently, Figure 5 The initialization process shown is similar to that shown in the figure. The evolved prompt template is combined with the initial code and delivered to the large language model. The fitness of the prompt template generated by this evolution is obtained through the reconstructed code. After a sufficient number of prompt evolution processes, a judgment is made based on the fitness of all the evolved prompt templates: whether all the existing prompt templates, including the evolved prompt templates and the prompt templates in the prompt pool, have generated codes that meet the reconstruction requirements; if so, the iteration is terminated, and the code that meets the conditions is used as the final output of the present invention; otherwise, the attached code is used. Figure 4 The prompt pool updating method shown updates the prompt pool to a preset maximum population of prompt templates with the highest fitness, and performs the next round of iteration until the iteration ends. Accordingly, the present invention significantly improves the intelligence level of code refactoring through the combination of a large language model and an enhanced genetic algorithm. The entire process from prompt template generation to code optimization does not require human intervention, greatly reducing development time and cost, and providing an efficient and intelligent code refactoring solution for modern software development.
[0086] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than a detailed description of the present invention with reference to the aforementioned embodiments. Those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein; and these modifications or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A feedback iterative code refactoring method based on a large language model, characterized in that: Use the following steps: S1. Create an initial population and evaluate its fitness: Use an enhanced genetic algorithm to combine the code provided by the user with the initial prompt template, deliver it to the large language model to reconstruct the code, and use the code returned by the large language model as the reconstructed code; evaluate the fitness of the reconstructed code as the fitness of the initial prompt template used by the code, and determine whether it meets the termination condition. If so, output the reconstructed code with the highest fitness; If it is not satisfied, the initial prompt template and its fitness are put into the prompt pool as the initial population for iteration; S2. Selecting population individuals: using the prompt template in the prompt pool as the parent population; selecting individuals in the parent population as individuals to be evolved to participate in subsequent evolution; S3, judging whether to enable the super mutation operator: judging whether the super mutation operator needs to be enabled by the fitness of the individual to be evolved, and then selecting an evolution operator. If the super mutation operator is enabled, the evolution operator is supermutated, and the evolution operator after the super mutation is used as the evolution operator to be used. If the super mutation operator is not enabled, the evolution operator is directly used as the evolution operator to be used. S4, using the evolution operator: using the evolution operator to be used and the individual to be evolved to combine and deliver the large language model and generate a new prompt, using the new prompt as the offspring individual, then similar to S1, combining the offspring individual with the code to be reconstructed and delivering it to the large language model to generate the reconstructed code, and evaluating the reconstructed code generated by the offspring individual to obtain the fitness of the offspring individual; S5. Check whether the iteration meets the termination condition. If so, stop the iteration and output the population individual with the highest fitness in the current prompt pool and its corresponding reconstructed code. If not, adopt the high-score retention strategy to compare with the fitness of the parent individuals in the prompt pool to retain the high-fitness individuals to update the prompt pool, and return to S2 for the next round of iteration.
2. According to claim 1, a feedback iterative code refactoring method based on a large language model is characterized in that: The fitness evaluation in S1 specifically adopts the following steps: S101, using the test case provided by the user to test whether the refactored code maintains the same function, if it passes the test case, the next step of evaluation is carried out, otherwise the fitness score of the refactored code is zero; S102, using CodeBLEU to compare the codes before and after reconstruction. If the weighted N-gram score of this indicator is higher than 0.9, it is judged that the two pieces of code are similar, and the reconstruction does not make enough changes, and the fitness score of the reconstructed code is zero; S103. Compare the cyclomatic complexity, number of code lines, number of code characters, and PyLint score of the code before and after the refactoring to obtain a comprehensive change evaluation of the refactored code, and standardize the evaluation into a fitness score.
3. The feedback iterative code refactoring method based on a large language model according to claim 1, characterized in that: The operation of selecting the individuals to be evolved in S2 is based on the fitness of the parent individuals, and the selection operator is randomly selected to determine the individuals to be evolved; The selection operators include a full random selection operator, a tournament selection operator, a roulette selection operator and a random traversal sampling operator, and one of them is randomly selected during use.
4. The feedback iterative code refactoring method based on a large language model according to claim 1, characterized in that: The evolution operators in S3 include direct mutation operators, crossover operators, and differential evolution-like operators; the details are as follows: Direct mutation operator: Through the natural language understanding and processing capabilities of LLM, a large language model is used to understand and replace the natural language in the parent prompt template to generate a new prompt template; Crossover operator: drives the large language model to extract, transform and combine the elements of different prompt templates, so that the generated new prompt template retains the core information of the original prompt and has a new expression form; Quasi-differential evolution operator: select individuals from two prompt pools, and use the idea of differential evolution algorithm to use these two individuals to drive the large language model to generate new individuals. Specifically: the elite individual is analogous to the basis vector in the differential evolution algorithm, and two individuals selected from the prompt pool are delivered to make the large language model find the difference between the two prompts as the difference vector, and then use the large language model to mutate them to generate mutation prompts as mutation vectors. Finally, a randomly selected individual is used to cross with the elite individual to generate the final prompt template.
5. The feedback iterative code refactoring method based on a large language model according to claim 1, characterized in that: The hypermutation operator in S3 is specifically as follows: perform fitness analysis on the refactored code, find out the items with the lowest scores in cyclomatic complexity, number of code lines, number of code characters and readability in its fitness score as the main problem of the code, and add prompt segments to the evolution operator based on the problem to guide it to generate prompt templates more targetedly.
6. The feedback iterative code refactoring method based on a large language model according to claim 1, characterized in that: The iteration termination condition in S5 is two conditions, namely, a pre-set fitness threshold and a maximum number of iterations. If any one of the two conditions is satisfied, the iteration is stopped.
7. The feedback iterative code refactoring method based on a large language model according to claim 2, characterized in that: S103 evaluates the code by the three indicators of the number of code characters, the number of code lines or the cyclomatic complexity, specifically using the following steps: S1011, extracting the measurement information of the code before refactoring is called baseline, and then extracting the measurement information of the code after refactoring is called current; S1012. By analyzing the number of code lines and characters, a scaling factor is set, where k is a dynamic value. Adjusting the k value can scale the sigmoid function. The difference value difference is obtained by combining the difference impact factor and the difference between baseline and current: difference=scaling_factor*(baseline–current) S1013. Use sigmoid function Map the difference to (0,1) to obtain the fitness S i , where i is the number of characters (char), lines of code (LOC), or cyclomatic complexity (CC); the process is: S i =sigmoid(difference) The adjusted k value is as follows: k CC =11, k char =8.8, k LOC =6.27; so define S CC , S char With S LOC as follows: The scoring strategy for PyLint is as follows: Since the code will pass the test case before the comprehensive information feedback, the weights of Error and Warning are reduced in the Pylint configuration file, and the scores of Refactor and Convention are observed, because these two scores can well reflect the readability, dependency complexity and code duplication information of the code; accordingly, the internal weight of PyLint is changed to obtain the weighted PyLint fitness S PyLint : Through these summary information, the comprehensive fitness score S of the improved code is obtained with weights of 1, 1, 3.5, and 4.5: S=1*(S Char +S LOC )*10+0.35*S CC +0.45*S PyLint 。 8. The feedback iterative code refactoring method based on a large language model according to claim 6, characterized in that: The fitness threshold is 7.8, and the maximum number of iterations is 6.
Citation Information
Patent Citations
Code generation method based on large language model
CN118409741A
Cited By
Planetary gearbox fault diagnosis algorithm automatic generation system and method based on large language model
CN121233973A
Code set generation method and device suitable for language model evolution
CN121349417A