Code repairing method and device based on large language model, equipment and medium

By integrating code scanning tools and large language models to generate structured prompt word templates, and combining reinforcement learning optimization strategies, the problems of low efficiency and quality in code repair have been solved, achieving efficient and accurate code repair.

CN120909640AActive Publication Date: 2025-11-07BEIHANG UNIV

Patent Information

Application Number
CN202510941480.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-09
Publication Date
2025-11-07
Estimated Expiration
2045-07-09

AI Technical Summary

Technical Problem

Existing technologies for code repair are inefficient and of low quality, especially when faced with complex code logic and diverse errors. Existing automated repair methods cannot be fully automated or the repair results are unsatisfactory.

Method used

Error information is extracted using code scanning tools, structured prompt word templates are generated, and a large language model is used to generate repair code segments. Multi-dimensional evaluation and reinforcement learning are combined to optimize the prompt word templates, forming an end-to-end automated process.

Benefits of technology

It significantly improves the efficiency and accuracy of code repair, reduces human intervention, is suitable for batch code standardization issues repair on a large scale, reduces labor costs, and ensures the functional equivalence, syntactic compliance, and logical structure of the repaired code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120909640A_ABST
    Figure CN120909640A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code repairing method, device and equipment based on a large language model.The method comprises the following steps that a code to be repaired is analyzed through a code scanning tool on the basis of a coding specification, error information of the code to be repaired is obtained, the error information comprises an error number, a violation rule, a supplementary description, an error example, a correction example, an error type and an error code segment; generating a cue word template according to a historical repair sample and a coding specification of the large language model, and filling the cue word template with the error information to generate a filled cue word; and inputting the filled cue word into the large language model to generate a repair code segment, judging whether the repair code segment accords with a set conversion specification, and if the repair code segment accords with the conversion specification, replacing the code to be repaired with the repair code segment to generate a repaired code. According to the scheme, the code is repaired through an end-to-end automatic process, and the code repairing efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of computer software, and in particular to a code repair method and device based on a large language model, equipment and a medium. BACKGROUND

[0002] In the software development process, code repair is an important link in maintaining and optimizing code quality. With the increasing complexity of software systems, the types and quantities of errors in the code are also increasing, resulting in developers spending a lot of time and effort in repairing errors. Traditional code repair methods usually rely on manual analysis and modification of code, however, as the project scale expands and the development cycle shortens, this manual repair method is inefficient and prone to errors. Therefore, how to improve the efficiency and accuracy of code repair has become an important research topic in current software development.

[0003] Existing automated code repair techniques mainly rely on rule matching, static analysis or template-based methods, although they can identify and repair common code problems to some extent, these methods can usually only handle known error types and have limited repair capabilities. Especially when faced with complex code logic and diverse errors, existing technologies still have problems such as being unable to completely automate repair or having unsatisfactory repair results.

[0004] In recent years, deep learning and large language models (such as GPT, etc.) have made significant progress in natural language processing and programming. Large language models have strong text understanding and generation capabilities, and can analyze context information, reason and provide solutions. Code repair methods based on large language models have been gradually proposed and have achieved some preliminary results, but they generally face challenges such as how to efficiently combine code repair tasks with model capabilities, and how to ensure the accuracy and effectiveness of the repair without changing the original code logic.

[0005] Therefore, how to use the reasoning ability of large language models, combined with error information in the code, repair rules and examples, to propose an automated and intelligent code repair method, remains a problem to be solved. In particular, in the application process of large language models, how to construct appropriate prompt word templates to more accurately guide the model to repair and further improve the efficiency and quality of code repair has become an important direction for the development of the field. SUMMARY

[0006] Therefore, the embodiments of the present application provide a code repair method based on a large language model to solve the technical problem of low efficiency and quality of code repair in the prior art. The method comprises: The error information of the to-be-repaired code is obtained by analyzing the to-be-repaired code based on a coding specification through a code scanning tool, wherein the error information includes an error number, a rule violation, a supplementary description, an error example, a correction example, an error type, and an error code segment. The prompt word template is generated according to the historical repair sample of the large language model and the coding specification, and the error information is filled into the prompt word template to generate a filled prompt word. The filled prompt word is input into the large language model to generate a repair code segment, and it is determined whether the repair code segment meets a set conversion specification, if the repair code segment meets the conversion specification, the to-be-repaired code is replaced by the repair code segment to generate a repaired code.

[0007] The embodiment of the present application also provides a code repair device based on a large language model to solve the technical problem of low efficiency and quality of code repair in the prior art. The device comprises: An error information extraction module is configured to obtain error information of to-be-repaired code by analyzing the to-be-repaired code based on a coding specification through a code scanning tool, wherein the error information includes an error number, a rule violation, a supplementary description, an error example, a correction example, an error type, and an error code segment. A prompt word template filling module is configured to generate a prompt word template according to a historical repair sample of a large language model and the coding specification, and fill the error information into the prompt word template to generate a filled prompt word. A code repair module is configured to input the filled prompt word into the large language model to generate a repair code segment, determine whether the repair code segment meets a set conversion specification, if the repair code segment meets the conversion specification, replace the to-be-repaired code with the repair code segment to generate a repaired code.

[0008] The embodiment of the present application also provides a computer device, which comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements any of the above code repair methods based on a large language model to solve the technical problem of low efficiency and quality of code repair in the prior art.

[0009] The embodiment of the present application also provides a computer readable storage medium, which stores a computer program for implementing any of the above code repair methods based on a large language model to solve the technical problem of low efficiency and quality of code repair in the prior art.

[0010] Compared with the prior art, the above at least one technical solution adopted by the embodiment of the present application can achieve at least the following beneficial effects: The error information is automatically extracted by a code scanning tool, a structured prompt word template is pre-generated and called, a large language model automatically generates a repaired code, and automatic replacement and evaluation are performed, forming an end-to-end automatic process, which greatly reduces manual intervention, is particularly suitable for batch standard problem repair of large-scale codes, significantly improves repair efficiency, and reduces labor costs. BRIEF DESCRIPTION OF DRAWINGS

[0011] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed in the embodiments will be briefly introduced as follows. Obviously, the drawings in the following description only represent some of the embodiments of the present application, and those skilled in the art can obtain other drawings according to these drawings without creative labor.

[0012] Figure 1 is a flowchart of a code repair method based on a large language model provided by an embodiment of the present application; Figure 2 is a structural block diagram of a computer device provided by an embodiment of the present application; Figure 3 is a structural block diagram of a code repair device based on a large language model provided by an embodiment of the present application. DETAILED DESCRIPTION

[0013] The embodiments of the present application will be described in detail below with reference to the drawings.

[0014] The embodiments of the present application will be described in detail below with reference to the drawings.

[0015] In the embodiments of the present application, a code repair method based on a large language model is provided, as shown in Figure 1 The method comprises the following steps: Step S101: based on a coding specification, analyzing a to-be-repaired code by a code scanning tool to obtain error information of the to-be-repaired code, wherein the error information comprises an error number, a rule violation, a supplementary note, an error example, a correction example, an error type and an error code segment; Step S102: generating a prompt word template according to the historical repair samples of the large language model and the coding specification, and filling the error information into the prompt word template to generate a filled prompt word; Step S103: inputting the filled prompt word into the large language model to generate a repair code segment, judging whether the repair code segment conforms to the set conversion specification, and if it conforms to the conversion specification, replacing the code to be repaired with the repair code segment to generate a repaired code.

[0016] Specifically, the embodiment of the present application realizes intelligent repair of code that violates coding specifications (including national standards such as GJB 8104-2013, etc.) by integrating static and / or dynamic code analysis tools with pre-structured large language model prompt word templates. This method first extracts key information such as error numbers, violated rules, and error code segments, and generates a structured prompt word template in combination with historical repair samples. Then, through a large language model, a repair code that is functionally equivalent, syntactically compliant, and retains the original logical structure is generated, and a feedback optimization mechanism is built based on multi-dimensional evaluation (including compliance, functional equivalence, risk defects, and readability detection).

[0017] Specifically, static and / or dynamic code scanning tools (such as tools based on lexical, syntactic, and semantic analysis) are used to analyze the code to be repaired and extract key error information. The error information includes but is not limited to: error numbers, specific rules violated (based on national standards such as GJB 8104-2013 or other coding specifications), supplementary explanations, error examples, correction examples, and error code segments to be repaired. This step ensures the completeness and accuracy of error information extraction, providing accurate input for subsequent repair.

[0018] In specific implementation, the following steps are taken to generate a prompt word template for code repair according to the historical repair samples of the large language model and the target specification: The mapping relationship between error types and repair solutions in the historical repair samples of the large language model is analyzed, wherein the mapping relationship is used to record regular code repair operations; repair constraint conditions are defined according to the coding specification, and repair constraint fields are determined according to the repair constraint conditions; a prompt word template is generated according to the mapping relationship, error context description fields, repair constraint fields, and output format fields.

[0019] Specifically, the large language model pre-analyzes and generates a structured prompt template for code repair based on historical repair samples and coding specifications. The generation process of the prompt template includes: analyzing the mapping relationship between error types and repair solutions in historical repair samples; defining repair constraints according to target specifications; and finally forming a structured template containing error context description field, repair constraint field and output format field. The generated template is stored for subsequent calling.

[0020] The mapping relationship between error types and repair solutions is that developers usually take certain regular repair operations when facing specific types of code errors in a large number of historical code repair samples. This regularity can be abstracted as a mapping relationship, i.e. the corresponding relationship between each error type and its common or optimal repair solution. By establishing this mapping, important prior knowledge basis can be provided for automated code repair, thereby improving the accuracy and feasibility of repair suggestions.

[0021] The construction of the mapping relationship is based on the analysis of historical code repair data. The specific process includes: first extracting the original error code and its corresponding repair version in the repair sample; then identifying the error type and its corresponding repair operation through semantic analysis, difference comparison, pattern extraction and other technologies; finally, the one-to-one or one-to-many correspondence relationship is structured as a mapping table or rule set. For example, in the repair of the frequently occurring "null pointer exception", the common repair operations include "adding null check", "initializing the object before use", etc.

[0022] In the code repair process based on large language model, the mapping relationship is used for the generation of prompt template (PromptTemplate). The prompt template usually includes structured fields such as error context description, repair constraint, and output format. The mapping relationship plays the role of "guiding the model to understand and tend to the historical effective repair method". By incorporating the repair solution prompt matching the error type in the template, the large language model can more accurately generate repair code that meets the expectations, thereby improving the quality and stability of repair.

[0023] The mapping relationship between error types and repair solutions not only improves the context awareness ability of the large language model in handling code repair tasks, but also makes the model's repair behavior more controllable and interpretable. Especially in large-scale enterprise code governance, embedded system development, security-sensitive scenarios and other environments, using existing repair experience for pattern migration is an efficient and low-risk way. In addition, this mapping relationship also helps the audit and backtracking of repair suggestions, which can trace the reasoning path when an error repair suggestion is made.

[0024] The error context description field refers to a field for helping the large language model better understand the error occurrence scenario, such as the name of the function, class or module where the error code is located, the context code snippet (such as the previous and subsequent lines of code) of the error code, the location where the error occurs (such as line number, column number), the definition and use of variables, methods or objects related to the error, etc. The purpose is to provide sufficient context information so that the large language model can generate more accurate and semantically correct repair code.

[0025] The output format field refers to a field for constraining the content format of the output of the large language model, such as whether to output only the modified code segment or the complete code, whether the output content needs to include comments or change notes, the style of the output language (such as indentation style, naming conventions), and whether the output format is JSON, Markdown code block, plain text, etc. The purpose is to control the degree of structuring and parsability of the large language model generation results, so as to facilitate subsequent automatic replacement or integration into tool chains.

[0026] In specific implementation, the error information is filled into the prompt word template to generate a filled prompt word by the following steps: The preset structured placeholders of the prompt word template are set, and an easy-to-error repair warning text is embedded in the prompt word template, wherein the easy-to-error repair warning text includes a description of a typical misrepair mode for the error type; based on string replacement technology, the error number, the rule violation, the supplementary note, the error example, the correction example and the error code segment are replaced with the corresponding preset structured placeholders according to the mapping relationship between the preset structured placeholders and the error type and repair scheme, to generate the filled prompt word.

[0027] Specifically, the error information extracted in the above steps is filled into the corresponding fields according to the template structure by calling the pre-generated prompt word template. Specifically, the preset structured placeholders (represented in the form of XML tags) in the template include, for example, error number: <error-id / > , rule violation: <rule-desc / > , supplementary note: <additional / > , error example: <example-error-code / > , correction example: <example-correct-code / > , error code: <error-code / > .

[0028] An easy-to-error repair warning text generated by a large language model (LLM) is embedded in the template to describe a typical misrepair mode for the current error type; based on string matching and replacement, the error number, rule violation, supplementary note, error example, correction example and error code segment are replaced with the corresponding placeholders according to the mapping relationship between the placeholder type and the extracted error information, to form a complete input instruction.

[0029] Specifically, the filled complete prompt word is input into a special code repair large language model. Based on the prompt word instruction, the model generates a repaired code segment that meets the functional equivalence, syntax specification, and logical structure preservation. The generated repaired code segment is used to replace the original error code segment (to-be-repaired code).

[0030] In specific implementation, the following steps are used to determine whether the repaired code segment meets the set conversion specification: The functional equivalence of the repaired code segment and the corresponding to-be-repaired code is used as the first judgment condition; the syntax specification of the repaired code segment is used as the second judgment condition; the logical structure of the repaired code segment is used as the third judgment condition; when the repaired code segment meets the first judgment condition, the second judgment condition, and the third judgment condition at the same time, the repaired code segment meets the set specification.

[0031] Specifically, the repaired code segment output by the large language model needs to meet the following conditions: 1. The repaired code segment is functionally equivalent to the original code segment; 2. The repaired code segment meets the syntax specification of the target code; 3. The repaired code segment preserves the logical structure of the original code segment.

[0032] In specific implementation, the following steps are used to evaluate the repaired code and optimize the prompt word template based on the evaluation results: After generating the repaired code, the repaired code is evaluated by a code scanning tool to generate an evaluation result; based on the evaluation result, the prompt word template generation strategy and the repaired code generation quality are optimized.

[0033] In specific implementation, the following steps are used to evaluate the repaired code by a code scanning tool to generate an evaluation result: The repaired code is verified for compliance with coding specifications to generate a coding evaluation result; a difference test is used to verify the input-output consistency of the repaired code and the to-be-repaired code to generate a consistency evaluation result; new security vulnerabilities or performance degradation introduced by the to-be-repaired code are identified to generate a vulnerability or degradation evaluation result; the code annotation rate and naming specification compliance of the repaired code are evaluated to generate a specification evaluation result; coding weights, consistency weights, vulnerability or degradation weights, and specification evaluation weights are set respectively, and an evaluation result is generated based on the coding weights, the coding evaluation result, the consistency weights, the consistency evaluation result, the vulnerability or degradation weights, the vulnerability or degradation evaluation result, the specification evaluation weights, and the specification evaluation result.

[0034] Specifically, the repaired overall code is evaluated by a code scanning tool in multiple dimensions. The scoring items include compliance detection, functional equivalence detection, risk defect detection, and readability detection.

[0035] Compliance detection: verifying whether the repaired code complies with coding standards; Functional equivalence detection: verifying the input-output consistency of the repaired code and the original code through differential testing; Risk defect detection: identifying new security vulnerabilities or performance degradation introduced by the repaired code; Readability detection: evaluating code comment rate and naming standard compliance.

[0036] In specific implementation, the following steps are taken to optimize the prompt word template generation strategy and improve the quality of the repaired code based on the evaluation results: A policy gradient optimization model is constructed using the evaluation results as the reward signal. The parameters of the large language model used to generate the prompt word template are adjusted through the policy gradient optimization model.

[0037] Specifically, the score (evaluation result) is used as the reward signal for reinforcement learning to build a policy gradient optimization model, which fine-tunes the large language model (LLM) for prompt word template generation. Through this continuous feedback loop iteration, the generation strategy of the prompt word template is continuously optimized, thereby improving the quality and standard compliance rate of the subsequently generated repaired code.

[0038] The policy gradient optimization model is a key technology in reinforcement learning (RL) that optimizes the way prompt word templates are generated, thereby improving the performance of large language models in code repair tasks. In reinforcement learning, "policy" refers to the probability distribution of an agent taking a certain action in a certain state. The policy can be a fixed rule or a model that is continuously learned and optimized. In the "policy gradient" (Policy Gradient) method, the policy is modeled as a parameterized function, and gradient descent is used to continuously update the policy parameters so that the model achieves higher long-term rewards in a given task.

[0039] The policy gradient optimization model continuously tries to generate different prompt word templates, observes their quality scores in code repair tasks, and then adjusts the generation strategy of the prompt word based on these scores, so that the model tends to choose better prompt methods in subsequent generation.

[0040] In one embodiment of the present application, a large language model (such as a GPT-based model) requires a prompt template to guide its code repair work. The design of the prompt directly affects the quality of the final generated code. Therefore, in order to optimize the construction method of the prompt template, a policy gradient optimization model is introduced.

[0041] The specific process is as follows: 1. The policy model initially generates a prompt template (for example: "Please fix the syntax error in the following Python code").

[0042] 2. Input the prompt into the large language model to generate a repaired code.

[0043] 3. Score the code.

[0044] 4. The score is fed back to the policy gradient model as a "reward signal".

[0045] 5. The policy gradient model optimizes the generation method of the prompt template through mathematical "gradient update" according to these rewards.

[0046] After multiple rounds of training, the policy model can continuously learn and generate more high-quality and effective prompts, thereby helping the large language model to produce higher-quality repair codes.

[0047] In one embodiment of the present application, assume that the initially provided prompt template is: "Please fix this code." The code generated by the large language model after repair has a low evaluation score, which may be due to inaccurate repair or non-compliance with specifications. The policy gradient model then generates a more detailed prompt in the next round, such as: "Please fix the logical error in the following Python code and ensure that it complies with the PEP8 coding specification, and add necessary comments." At this point, the quality of the repair code generated by the model has improved, and the evaluation score is higher. Therefore, the policy gradient algorithm will tend to "learn" this detailed prompt style and gradually reinforce this generation direction. With the continuation of iteration, the model will automatically adjust the wording, structure, requirements, etc. used in the prompt template, and eventually form a set of prompts that are most suitable for the current code repair task.

[0048] In this embodiment, a computer device is provided, as shown in Figure 2 which includes a memory 201, a processor 202, and a computer program stored in the memory and executable on the processor, wherein the processor implements any of the above-mentioned code repair methods based on a large language model when executing the computer program.

[0049] Specifically, the computer device can be a computer terminal, a server, or a similar computing device.

[0050] In the embodiment, a computer readable storage medium is provided, and the computer readable storage medium stores a computer program for executing any of the above code repair methods based on a large language model.

[0051] In particular, the computer readable storage medium includes permanent and non-permanent, removable and non-removable media, and can be implemented by any method or technology to store information. The information can be computer readable instructions, data structures, program modules or other data. Examples of computer readable storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette, magnetic tape disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information accessible by a computing device. According to the definition herein, the computer readable storage medium does not include transitory computer readable media such as modulated data signals and carriers.

[0052] Based on the same inventive concept, the embodiment of the present application also provides a code repair device based on a large language model, as described in the following embodiment. Since the code repair device based on a large language model solves the problem by the same principle as the code repair method based on a large language model, the implementation of the code repair device based on a large language model can refer to the implementation of the code repair method based on a large language model, and the repeated parts will not be described. The term "unit" or "module" used below can be a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiment is preferably implemented in software, hardware or a combination of software and hardware is also possible and is contemplated.

[0053] Figure 3 is a structural block diagram of the code repair device based on a large language model of the embodiment of the present application, as shown in Figure 3 The structure is described below.

[0054] The error information extraction module 301 is configured to analyze the code to be repaired by a code scanning tool based on a coding specification to obtain error information of the code to be repaired, wherein the error information includes error number, rule violation, supplementary description, error example, correction example, error type and error code segment. The prompt word template filling module 302 is configured to generate a prompt word template according to the historical repair sample of the large language model and the coding specification, fill the error information into the prompt word template, and generate a filled prompt word. The code repair module 303 is configured to input the filled prompt word into the large language model, generate a repaired code segment, determine whether the repaired code segment meets the set conversion specification, replace the code to be repaired with the repaired code segment if the repaired code segment meets the conversion specification, and generate a repaired code.

[0055] In an embodiment, the prompt word template filling module includes: The mapping relationship generation unit is configured to analyze a mapping relationship between error types and repair solutions in the historical repair sample of the large language model, where the mapping relationship is used to record regular code repair operations. The repair constraint field determination unit is configured to define a repair constraint condition according to the coding specification, and determine a repair constraint field according to the repair constraint condition. The prompt word template generation unit is configured to generate a prompt word template according to the mapping relationship, the error context description field, the repair constraint field, and the output format field.

[0056] In an embodiment, the prompt word template filling module further includes: The placeholder generation unit is configured to set a preset structured placeholder of the prompt word template. The warning text embedding unit is configured to embed a common error repair warning text in the prompt word template, where the common error repair warning text includes a description of a typical misrepair mode for the error type. The prompt word filling unit is configured to replace the error number, the rule violation, the supplementary description, the error example, the correction example, and the error code segment with corresponding preset structured placeholders based on a string replacement technique, according to the preset structured placeholder and the mapping relationship between the error type and the repair solution, to generate the filled prompt word.

[0057] In an embodiment, the code repair module includes: The first judgment condition generation unit is configured to take whether the repaired code segment and the corresponding code to be repaired are functionally equivalent as a first judgment condition. The second judgment condition generation unit is configured to take whether the repaired code segment meets the syntax specification of the code to be repaired as a second judgment condition. The third judgment condition generation unit is configured to take whether the repaired code segment and the corresponding code to be repaired have the same logical structure as a third judgment condition. The specification judgment unit is configured to determine that the repair code segment conforms to the set specification when the repair code segment simultaneously satisfies the first judgment condition, the second judgment condition and the third judgment condition.

[0058] In one embodiment, the device further comprises a template optimization module.

[0059] In one embodiment, the template optimization module comprises: The evaluation result generation unit is configured to generate an evaluation result by evaluating the repaired code through a code scanning tool after generating the repaired code. The prompt word template optimization unit is configured to optimize a prompt word template generation strategy and repair code generation quality based on the evaluation result.

[0060] In one embodiment, the evaluation result generation unit is configured to verify whether the repaired code conforms to a coding specification to generate a coding evaluation result; verify whether the input and output of the repaired code and the code to be repaired are consistent through differential testing to generate a consistency evaluation result; identify whether the code to be repaired introduces new security vulnerabilities or performance degradation to generate a vulnerability or degradation evaluation result; evaluate the code comment rate and naming specification compliance of the repaired code to generate a specification evaluation result; set a coding weight, a consistency weight, a vulnerability or degradation weight, and a specification evaluation weight, respectively, and generate an evaluation result based on the coding weight, the coding evaluation result, the consistency weight, the consistency evaluation result, the vulnerability or degradation weight, the vulnerability or degradation evaluation result, the specification evaluation weight, and the specification evaluation result.

[0061] In one embodiment, the prompt word template optimization unit is configured to use the evaluation result as a reward signal to build a policy gradient optimization model; and adjust parameters of the large language model used to generate the prompt word template through the policy gradient optimization model.

[0062] The embodiments of the present application achieve the following technical effects: The embodiment of the present application significantly improves the accuracy and standard compliance of automatic code repair by dynamically optimizing the prompt word template generation strategy through reinforcement learning, and is suitable for efficient and batch processing of code standardization problems in the software development process; solves the problems of low code repair efficiency, insufficient repair accuracy and low compliance with coding standards (especially mandatory standards such as national standards) in the prior art; through automatic extraction of error information by a code scanning tool, pre-generation and calling of structured prompt word templates, automatic generation of repair code by a large language model, and automatic replacement and evaluation, an end-to-end automatic process is formed, which greatly reduces manual intervention, is particularly suitable for batch repair of large-scale code standardization problems, significantly improves repair efficiency, and reduces labor costs; through the accurate guidance of a large language model (LLM) by a structured prompt word template, and combined with a closed-loop feedback optimization mechanism, the repair efficiency and code quality are significantly improved under the premise of ensuring the functional equivalence of the repaired code, the syntax compliance and the preservation of the original logic structure; based on the multi-dimensional evaluation results (compliance, functional equivalence, risk defects and readability) as the reward signal of reinforcement learning, the prompt word template generation model is dynamically fine-tuned by using a policy gradient optimization algorithm, forming a "generation-evaluation-optimization" closed loop, and continuously improving the guiding effect of the prompt word template, thereby driving the compliance rate and overall quality of the repaired code to steadily increase; supporting the adaptation of code scanning tools to different coding standards (such as national standards, industry standards and enterprise rules), and through the historical repair sample learning and feedback optimization mechanism, the prompt word template can dynamically adapt to different error types and project requirements, the core process does not depend on a specific large language model, and can be flexibly applied to various software development scenarios (such as military and finance) that need to strictly follow coding standards, realizing the large-scale deployment across projects and standards.

[0063] Obviously, those skilled in the art should understand that each module or each step of the above-mentioned embodiment of the present application can be realized by a general computing device, which can be concentrated on a single computing device or distributed on a network composed of multiple computing devices, and can be realized by program code executable by a computing device, so that it can be stored in a storage device and executed by a computing device, and in some cases, the steps shown or described can be executed in an order different from that shown here, or they can be manufactured into individual integrated circuit modules, or multiple modules or steps can be manufactured into a single integrated circuit module. Thus, the embodiment of the present application is not limited to any specific hardware and software combination.

[0064] The above only describes the preferred embodiments of the present application and is not intended to limit the present application. For those skilled in the art, the embodiments of the present application can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.

Claims

1. A code repair method based on a large language model, characterized in that, The method comprises the following steps: Based on the coding specification, the code scanning tool is used to analyze the to-be-repaired code to obtain error information of the to-be-repaired code, wherein the error information includes error number, rule violation, supplementary description, error example, correction example, error type and error code segment; According to the historical repair sample of the large language model and the coding specification, a prompt word template is generated, and the error information is filled into the prompt word template to generate a filled prompt word; The filled prompt word is input into the large language model to generate a repair code segment, and it is judged whether the repair code segment meets the set conversion specification, if it meets the conversion specification, the to-be-repaired code is replaced by the repair code segment to generate repaired code.

2. The large language model-based code repair method of claim 1, wherein, According to the historical repair sample of the large language model and the target specification, a prompt word template for code repair is generated, comprising: Analyzing the mapping relationship between error type and repair scheme in the historical repair sample of the large language model, wherein the mapping relationship is used to record the regular code repair operation; Define repair constraint conditions according to the coding specification, and determine repair constraint fields according to the repair constraint conditions; According to the mapping relationship, the error context description field, the repair constraint field and the output format field, a prompt word template is generated. 3.The large language model-based code repair method of claim 2, wherein, Fill the error information into the prompt word template to generate a filled prompt word, comprising: Set the preset structured placeholder of the prompt word template; Embed an easy-to-fix repair warning text in the prompt word template, wherein the easy-to-fix repair warning text includes a description of the typical misrepair mode for the error type; Based on the string replacement technology, the error number, the rule violation, the supplementary description, the error example, the correction example and the error code segment are replaced by the corresponding preset structured placeholder according to the mapping relationship between the error type and the repair scheme to generate the filled prompt word. 4.The large language model-based code repair method of claim 1, wherein, Judging whether the repair code segment meets the set conversion specification, comprising: Whether the repair code segment and the corresponding to-be-repaired code are functionally equivalent is taken as a first judgment condition; The repair code segment meets the syntax specification of the to-be-repaired code as a second judgment condition; The repair code segment and the corresponding to-be-repaired code have the same logical structure as a third judgment condition; When the repair code segment meets the first judgment condition, the second judgment condition and the third judgment condition at the same time, the repair code segment meets the set conversion specification.

5. The large language model-based code repair method according to any one of claims 1 to 4, characterized in that, Further comprising: After generating the repaired code, the repaired code is evaluated by the code scanning tool to generate an evaluation result; Through the evaluation result, the prompt word template generation strategy and the repair code generation quality are optimized.

6. The large language model-based code repair method of claim 5, wherein, Evaluating the repaired code by the code scanning tool to generate an evaluation result, comprising: Verify whether the repaired code meets the coding specification to generate a coding evaluation result; Through differential testing, verify whether the input and output of the repaired code and the to-be-repaired code are consistent to generate a consistency evaluation result; identifying whether the to-be-repaired code introduces a new security vulnerability or performance degradation, and generating a vulnerability or degradation evaluation result; evaluating a code comment rate and a naming specification compliance degree of the repaired code, and generating a specification evaluation result; setting a coding weight, a consistency weight, a vulnerability or degradation weight, and a specification evaluation weight, respectively, and generating an evaluation result according to the coding weight, the coding evaluation result, the consistency weight, the consistency evaluation result, the vulnerability or degradation weight, the vulnerability or degradation evaluation result, the specification evaluation weight, and the specification evaluation result.

7. The large language model-based code repair method of claim 5, wherein, Through the evaluation result, the prompt word template generation strategy and the repaired code generation quality are optimized, including: constructing a policy gradient optimization model by taking the evaluation result as a reward signal; adjusting parameters of the large language model used to generate the prompt word template through the policy gradient optimization model. 8.A code repair apparatus based on a large language model, characterized in that, including: an error information extraction module configured to analyze to-be-repaired code based on a coding specification through a code scanning tool to obtain error information of the to-be-repaired code, wherein the error information includes an error number, a rule violation, a supplementary description, an error example, a correction example, an error type, and an error code segment; a prompt word template filling module configured to generate a prompt word template according to historical repair samples of a large language model and the coding specification, and fill the error information into the prompt word template to generate a filled prompt word; a code repair module configured to input the filled prompt word into the large language model to generate a repaired code segment, judge whether the repaired code segment complies with a set conversion specification, replace the to-be-repaired code with the repaired code segment if the repaired code segment complies with the conversion specification, and generate repaired code.

9. A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, The processor implements the large language model-based code repair method in any of claims 1 to 7 when executing the computer program.

10. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program for implementing the large language model-based code repair method in any of claims 1 to 7.

Citation Information

Patent Citations

  • Code generation optimization method and device for large language model, equipment and medium

    CN117724695A

  • Code review and optimization method driven by large language model

    CN119512556A

  • Code defect detection method and system based on large model

    CN119690512A

  • Code generation method and device based on large model and electronic equipment

    CN119759356A

  • Iterative ai prompt optimization for video generation

    US20240292070A1

Cited By

  • Delaration element code conversion method and device

    CN121683700A

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

    CN121722426A

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

    CN121722426B

  • Semantic exploration-based large language model code defect repair method and device

    CN122285053A