A code repair method, device and equipment based on a large language model and a medium

By integrating code analysis tools and large language models to generate structured prompt word templates, and combining reinforcement learning optimization strategies, the problem of low code repair efficiency and quality is solved, achieving efficient and automated code repair, which is suitable for fixing code standardization issues on a large scale.

CN120909640BActive Publication Date: 2026-07-14BEIHANG UNIV

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
BEIHANG UNIV
Filing Date
2025-07-09
Publication Date
2026-07-14

Smart Images

  • Figure CN120909640B_ABST
    Figure CN120909640B_ABST
Patent Text Reader

Abstract

The embodiment of the application provides a code repair method and device based on a large language model, equipment and medium, wherein 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 comprises an error number, a rule violation, a supplementary note, an error example, a correction example, an error type and an error code segment; according to the history 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 conforms to the set conversion specification; if the conversion specification is met, the to-be-repaired code is replaced with the repair code segment to generate a repaired code. The scheme repairs the code through an end-to-end automatic process, thereby improving the code repair efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer software technology, and in particular to a code repair method, apparatus, device, and medium based on a large language model. Background Technology

[0002] In software development, code repair is a crucial step in maintaining and optimizing code quality. As software systems become increasingly complex, the types and number of errors in the code also increase, leading developers to invest significant time and effort in fixing them. Traditional code repair methods typically rely on manual analysis and modification of the code; however, with the expansion of project scale and the shortening of development cycles, this manual approach is inefficient and prone to errors. Therefore, improving the efficiency and accuracy of code repair has become an important research topic in contemporary software development.

[0003] Existing automated code repair technologies primarily rely on rule matching, static analysis, or template-based methods. While these methods can identify and fix common code problems to some extent, they typically only handle known error types and have limited repair capabilities. Especially when faced with complex code logic and diverse errors, existing technologies still suffer from incomplete automation or unsatisfactory repair results.

[0004] In recent years, deep learning and large language models (such as GPT) have made significant progress in the fields of natural language processing and programming. Large language models possess powerful text understanding and generation capabilities, enabling them to perform reasoning and provide solutions by analyzing contextual information. Code repair methods based on large language models have been gradually proposed and have achieved some initial results, but they generally face challenges such as how to efficiently combine code repair tasks with the capabilities of the model, and how to ensure the accuracy and effectiveness of the repair without changing the original code logic.

[0005] Therefore, how to leverage the reasoning capabilities of large language models, combined with error messages, repair rules, and examples in the code, to propose an automated and intelligent code repair method remains a pressing issue. In particular, how to construct appropriate prompt word templates to more accurately guide the model in repair during the application of large language models, thereby further improving the efficiency and quality of code repair, has become an important direction for technological development in this field. Summary of the Invention

[0006] In view of this, embodiments of the present invention provide a code repair method based on a large language model to solve the technical problems of low efficiency and quality in existing code repair techniques. The method includes:

[0007] Based on coding standards, the code to be repaired is analyzed using code scanning tools to obtain error information of the code to be repaired. The error information includes error number, rule violation, supplementary explanation, error example, correction example, error type, and error code segment.

[0008] Based on the historical repair samples of the large language model and the encoding specifications, a prompt word template is generated, and the error information is filled into the prompt word template to generate a filled prompt word;

[0009] The filled prompt word is input into the large language model to generate a repair code segment. It is then determined whether the repair code segment conforms to the set conversion standard. If it does, the code to be repaired is replaced with the repair code segment to generate the repaired code.

[0010] This invention also provides a code repair device based on a large language model to address the technical problems of low efficiency and quality in code repair in the prior art. The device includes:

[0011] The error information extraction module is used to analyze the code to be repaired based on coding standards and using code scanning tools to obtain the error information of the code to be repaired. The error information includes error number, rule violation, supplementary explanation, error example, correction example, error type and error code segment.

[0012] The prompt word template filling module is used to generate a prompt word template based on the historical repair samples of the large language model and the encoding specification, and to fill the error information into the prompt word template to generate the filled prompt word;

[0013] The code repair module is used to input the filled prompt words into the large language model, generate a repair code segment, determine whether the repair code segment conforms to the set conversion standard, and if it conforms to the conversion standard, replace the code to be repaired with the repair code segment to generate the repaired code.

[0014] This invention also provides a computer device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements any of the above-mentioned code repair methods based on a large language model, thereby solving the technical problem of low efficiency and quality of code repair in the prior art.

[0015] This invention also provides a computer-readable storage medium storing a computer program that executes any of the above-described code repair methods based on a large language model, in order to solve the technical problem of low efficiency and quality of code repair in the prior art.

[0016] Compared with the prior art, the beneficial effects that at least one technical solution adopted in the embodiments of this specification can achieve include at least:

[0017] By automatically extracting error information through code scanning tools, pre-generating and calling structured prompt word templates, automatically generating repair code through large language models, and automatically replacing and evaluating, an end-to-end automated process is formed, which greatly reduces human intervention. It is particularly suitable for batch standardization issues in large-scale code, significantly improving repair efficiency and reducing labor costs. Attached Figure Description

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

[0019] Figure 1 This is a flowchart of a code repair method based on a large language model provided in an embodiment of the present invention;

[0020] Figure 2 This is a structural block diagram of a computer device provided in an embodiment of the present invention;

[0021] Figure 3 This is a structural block diagram of a code repair device based on a large language model provided in an embodiment of the present invention. Detailed Implementation

[0022] The embodiments of this application will now be described in detail with reference to the accompanying drawings.

[0023] The following specific examples illustrate the implementation of this application. Those skilled in the art can easily understand other advantages and effects of this application from the content disclosed in this specification. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. This application can also be implemented or applied through other different specific embodiments, and the details in this specification can also be modified or changed based on different viewpoints and applications without departing from the spirit of this application. It should be noted that, in the absence of conflict, the following embodiments and features in the embodiments can be combined with each other. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0024] In this embodiment of the invention, a code repair method based on a large language model is provided, such as... Figure 1 As shown, the method includes:

[0025] Step S101: Based on the coding standards, analyze the code to be repaired using a code scanning tool to obtain the error information of the code to be repaired. The error information includes error number, rule violation, supplementary explanation, error example, correction example, error type, and error code segment.

[0026] Step S102: Based on the historical repair samples of the large language model and the encoding specifications, generate a prompt word template, and fill the error information into the prompt word template to generate the filled prompt word;

[0027] Step S103: Input the filled prompt word into the large language model to generate a repair code segment. Determine whether the repair code segment conforms to the set conversion standard. If it conforms to the conversion standard, replace the code to be repaired with the repair code segment to generate the repaired code.

[0028] Specifically, this invention integrates static and / or dynamic code analysis tools with pre-structured large language model prompt word templates to achieve intelligent repair of code that violates coding standards (including national standards such as GJB 8104-2013). The method first extracts key information such as error number, rule violation, and erroneous code segment, and generates a structured prompt word template by combining it with historical repair samples. Then, it generates functionally equivalent, syntactically compliant repair code that retains the original logical structure through a large language model, and constructs a feedback optimization mechanism based on multi-dimensional evaluation (including compliance, functional equivalence, risk defects, and readability detection).

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

[0030] In practice, the following steps are used to generate code repair prompt word templates based on historical repair samples and target specifications from the large language model:

[0031] The mapping relationship between error types and repair schemes in the historical repair samples of the large language model is analyzed, wherein the mapping relationship is used to record code repair operations with regularity; repair constraints are defined according to the coding specifications, and repair constraint fields are determined according to the repair constraints; prompt word templates are generated according to the mapping relationship, error context description field, repair constraint field and output format field.

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

[0033] The mapping between error types and fixes reveals that, based on a large sample of historical code fixes, developers typically employ certain remedial actions when faced with specific types of code errors. This regularity can be abstracted into a mapping relationship, namely the correspondence between each error type and its common or optimal fix. Establishing this mapping provides a crucial foundation of prior knowledge for automated code repair, thereby improving the accuracy and feasibility of fix recommendations.

[0034] The mapping relationship is constructed based on the analysis of historical code repair data. The specific process includes: first, extracting the original error code and its corresponding repair version from the repair samples; then, identifying the error type and its corresponding repair operation through semantic analysis, difference comparison, and pattern extraction techniques; finally, structurally expressing this one-to-one or one-to-many correspondence as a mapping table or rule set. For example, in the numerous "null pointer exception" repairs, common repair operations can be summarized as "adding null checks" and "initializing objects before use," etc.

[0035] In code repair based on a large language model, mapping relationships are used to generate prompt templates. Prompt templates typically include structured fields such as error context descriptions, repair constraints, and output formats. The mapping relationships serve to guide the model to understand and favor historically effective repair methods. By incorporating repair suggestions matching the error type into the template, the large language model can more accurately generate repair code that meets expectations, thereby improving the quality and stability of the repair process.

[0036] The mapping relationship between error types and remediation schemes not only enhances the context-awareness of large language models in handling code remediation tasks, but also makes the model's remediation behavior more controllable and interpretable. Especially in environments such as large-scale enterprise code governance, embedded system development, and security-sensitive scenarios, leveraging existing remediation experience for pattern migration is an efficient and low-risk approach. Furthermore, this mapping relationship also facilitates the auditing and backtracking of remediation suggestions, allowing for the tracing of the reasoning path when an error remediation suggestion arises.

[0037] Among them, the error context description field refers to the information field used to help the large language model better understand the scenario in which the error occurred. Such fields include: the name of the function, class or module where the error code is located, the context code fragment of the error code (such as several lines of code before and after), the location of the error (such as line number and column number), the definition and usage of variables, methods or objects related to the error, etc. Its purpose is to provide sufficient context information so that the large language model can generate more accurate and semantically consistent repair code.

[0038] The output format field refers to the field used to constrain the output content format 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 descriptions; the style of the output language (such as indentation style, naming conventions); whether the output format is JSON, Markdown code block, plain text, etc. The purpose is to control the degree of structure and parsability of the results generated by the large language model, so as to facilitate subsequent automatic replacement or integration into the toolchain.

[0039] In practice, the following steps are used to fill the error message into the prompt word template and generate the filled prompt word:

[0040] Set a preset structured placeholder for the prompt word template; embed error-prone repair warning text into the prompt word template, wherein the error-prone repair warning text includes a description of typical incorrect repair patterns for the error type; based on string replacement technology, according to the preset structured placeholder and the mapping relationship between the error type and the repair scheme, replace the corresponding preset structured placeholder with the error number, the rule violation, the supplementary explanation, the error example, the correction example, and the error code segment to generate the filled prompt word.

[0041] Specifically, the pre-generated prompt word template is invoked, and the error information extracted in the previous steps is filled into the corresponding fields according to the template structure. This includes: pre-defined structured placeholders in the template (represented in XML tag form), for example, error number: <error-id / > Violation of rules: <rule-desc / > Supplementary explanation: <additional / > Error example: <example-error-code / > Correction example: <example-correct-code / > Error code: <error-code / > .

[0042] Embedding error-prone repair warning text generated by the Large Language Model (LLM) in the template describes typical erroneous repair patterns for the current error type; based on string matching and replacement, according to the mapping relationship between placeholder type and extracted error information, the corresponding placeholders are replaced with error number, rule violation, supplementary explanation, error example, correction example, error code segment, etc., to form a complete input instruction.

[0043] Specifically, the completed prompt word is input into a dedicated code-fixing language model. Based on the prompt word instruction, this model generates a repair code segment that meets functional equivalence, syntactic correctness, and preserves logical structure. The generated repair code segment then replaces the original erroneous code segment (the code to be fixed).

[0044] In practice, the following steps are used to determine whether the repair code segment conforms to the set conversion specifications:

[0045] The first judgment condition is whether the repair code segment is functionally equivalent to the corresponding code to be repaired; the second judgment condition is whether the repair code segment conforms to the syntax specification of the code to be repaired; the third judgment condition is whether the repair code segment and the corresponding code to be repaired have the same logical structure; when the repair code segment simultaneously satisfies the first judgment condition, the second judgment condition and the third judgment condition, the repair code segment conforms to the set specification.

[0046] Specifically, the repair code segment output by the large language model needs to meet the following conditions: 1. The repair code segment is functionally equivalent to the original code segment; 2. The repair code segment conforms to the syntax rules of the target code; 3. The repair code segment retains the logical structure of the original code segment.

[0047] In practice, the following steps are used to evaluate the repaired code and optimize the prompt word template based on the evaluation results:

[0048] After generating the repaired code, the repaired code is evaluated using a code scanning tool to generate evaluation results; based on the evaluation results, the prompt word template generation strategy and the quality of the repaired code generation are optimized.

[0049] In practice, the following steps are used to evaluate the repaired code using a code scanning tool and generate evaluation results:

[0050] Verify whether the repaired code conforms to the coding standards and generate a coding evaluation result; verify the consistency between the input and output of the repaired code and the code to be repaired through differential testing and generate a consistency evaluation result; identify whether the code to be repaired introduces new security vulnerabilities or performance degradations and generate a vulnerability or degradation evaluation result; evaluate the code comment rate and naming convention compliance of the repaired code and generate a standardization evaluation result; set coding weight, consistency weight, vulnerability or degradation weight, and standardization evaluation weight respectively, and generate an evaluation result based on the coding weight, coding evaluation result, consistency weight, consistency evaluation result, vulnerability or degradation weight, vulnerability or degradation evaluation result, standardization evaluation weight, and standardization evaluation result.

[0051] Specifically, the repaired code is evaluated from multiple dimensions using code scanning tools. The scoring items include compliance testing, functional equivalence testing, risk and defect detection, and readability testing.

[0052] Compliance testing: Verify whether the patch code conforms to coding standards;

[0053] Functional equivalence test: Verify the consistency of input and output between the repair code and the original code through differential testing;

[0054] Risk and defect detection: Identify new security vulnerabilities or performance degradation introduced by code fixes;

[0055] Readability check: Evaluate the code's comment rate and compliance with naming conventions.

[0056] In practice, the following steps are used to optimize the prompt word template generation strategy and improve the quality of code generation based on the evaluation results:

[0057] Using the evaluation results as a reward signal, a strategy gradient optimization model is constructed; the parameters of the large language model used to generate the prompt word template are adjusted through the strategy gradient optimization model.

[0058] Specifically, the score (evaluation result) is used as a reward signal for reinforcement learning to construct a policy gradient optimization model, which fine-tunes the performance of the Large Language Model (LLM) generated from the prompt word templates. Through this continuous feedback loop iteration, the prompt word template generation strategy is continuously optimized, thereby improving the quality and compliance rate of the subsequently generated fix code.

[0059] Policy gradient optimization is a key technique in reinforcement learning (RL) used to optimize the generation of prompt word templates, thereby improving the performance of large language models in code repair tasks. In reinforcement learning, a "policy" refers to the probability distribution of an agent taking a certain action in a given state. Policies can be fixed rules or learned and optimized through continuous model learning. In the "policy gradient" method, we model the policy as a parameterized function and use gradient descent to continuously update the policy parameters, enabling the model to obtain higher long-term rewards for a given task.

[0060] The policy gradient optimization model continuously tries to generate different prompt word templates and observes their quality scores in the code fixing task. Then, it adjusts the prompt word generation strategy in reverse based on these scores, so that the model tends to choose the better-performing prompt methods in subsequent generation.

[0061] In one embodiment of the present invention, large language models (such as GPT-like models) require prompt templates to guide their code repair work. The design of the prompts directly affects the quality of the final generated code. Therefore, a policy gradient optimization model is introduced to optimize the construction of prompt templates.

[0062] The specific process is as follows:

[0063] 1. The strategy model initially generates prompt word templates (e.g., "Please fix the syntax error in the following Python code").

[0064] 2. Input the prompt word into the large language model to generate a corrected code.

[0065] 3. Rate the code.

[0066] 4. Feed the scoring results back to the policy gradient model as a "reward signal".

[0067] 5. The policy gradient model optimizes the generation of prompt word templates through mathematical "gradient updates" based on these rewards.

[0068] Through multiple rounds of training, the policy model can continuously learn and generate higher-quality and more effective prompts, thereby helping the large language model produce higher-quality fix code.

[0069] In one embodiment of the invention, suppose the initially provided prompt template is: "Please fix this code." The code generated after the large language model fixes it receives a low evaluation score, possibly because the fix is ​​inaccurate or does not conform to the specifications. Then, in the next round, the policy gradient model attempts to generate more detailed prompts, such as: "Please fix the logical errors in the following Python code and ensure it conforms to the PEP8 coding standard, adding necessary comments."

[0070] At this point, the quality of the repair code generated by the model has improved, resulting in a higher evaluation score. Therefore, the policy gradient algorithm tends to "learn" this style of detailed hints, gradually reinforcing this generation direction. As iterations continue, the model automatically adjusts the wording, structure, and requirements used in the hint template, ultimately forming a hint style best suited for the current code repair task.

[0071] In this embodiment, a computer device is provided, such as... Figure 2 As shown, it includes a memory 201, a processor 202, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, it implements any of the above-mentioned code repair methods based on a large language model.

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

[0073] In this embodiment, a computer-readable storage medium is provided, which stores a computer program that executes any of the above-described code repair methods based on a large language model.

[0074] Specifically, computer-readable storage media include both permanent and non-permanent, removable and non-removable media, which can store information using any method or technology. Information can be computer-readable instructions, data structures, modules of programs, 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, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transferable medium that can be used to store information accessible by a computing device. As defined herein, computer-readable storage media do not include transient media, such as modulated data signals and carrier waves.

[0075] Based on the same inventive concept, this invention also provides a code repair device based on a large language model, as described in the following embodiments. Since the principle of the code repair device based on a large language model is similar to that of 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 repeated details will not be elaborated further. As used below, the terms "unit" or "module" can refer to a combination of software and / or hardware that performs a predetermined function. Although the device described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.

[0076] Figure 3 This is a structural block diagram of a code repair device based on a large language model according to an embodiment of the present invention, such as... Figure 3 As shown, it includes: an error message extraction module 301, a prompt word template filling module 302, and a code repair module 303. The structure is described below.

[0077] The error information extraction module 301 is used to analyze the code to be repaired based on the coding standard and through a code scanning tool to obtain the error information of the code to be repaired. The error information includes error number, rule violation, supplementary explanation, error example, correction example, error type and error code segment.

[0078] The prompt word template filling module 302 is used to generate a prompt word template based on the historical repair samples of the large language model and the encoding specification, and to fill the error information into the prompt word template to generate a filled prompt word;

[0079] The code repair module 303 is used to input the filled prompt words into the large language model, generate a repair code segment, determine whether the repair code segment conforms to the set conversion standard, and if it conforms to the conversion standard, replace the code to be repaired with the repair code segment to generate the repaired code.

[0080] In one embodiment, the prompt template filling module includes:

[0081] A mapping relationship generation unit is used to parse the mapping relationship between error types and repair schemes in the historical repair samples of the large language model, wherein the mapping relationship is used to record regular code repair operations;

[0082] The unit for determining repair constraint fields is used to define repair constraint conditions according to the coding specification and to determine repair constraint fields according to the repair constraint conditions.

[0083] The prompt word template generation unit is used to generate a prompt word template based on the mapping relationship, the error context description field, the repair constraint field, and the output format field.

[0084] In one embodiment, the prompt template filling module further includes:

[0085] The placeholder generation unit is used to set the preset structured placeholders for the prompt word template;

[0086] The warning text embedding unit is used to embed error-prone repair warning text into the prompt word template, wherein the error-prone repair warning text includes a description of typical mis-repair patterns for the error type;

[0087] The prompt word filling unit is used to replace the corresponding preset structured placeholders with the error number, the rule violation, the supplementary explanation, the error example, the correction example, and the error code segment based on string replacement technology and according to the mapping relationship between the preset structured placeholders and the error type and the repair scheme, so as to generate the filled prompt word.

[0088] In one embodiment, the code repair module includes:

[0089] The first judgment condition generation unit is used to take whether the repair code segment and the corresponding code to be repaired are functionally equivalent as the first judgment condition;

[0090] The second judgment condition generation unit is used to take the conformity of the repair code segment with the syntax specification of the code to be repaired as the second judgment condition;

[0091] The third judgment condition generation unit is used to take the logical structure of the repair code segment and the corresponding code to be repaired as the third judgment condition;

[0092] The conformity judgment unit is used to determine that the repair code segment conforms to the set specifications when the repair code segment simultaneously satisfies the first judgment condition, the second judgment condition, and the third judgment condition.

[0093] In one embodiment, the above-described apparatus further includes a template optimization module.

[0094] In one embodiment, the template optimization module includes:

[0095] The evaluation result generation unit is used to evaluate the repaired code using a code scanning tool after the repaired code is generated, and to generate evaluation results.

[0096] The prompt word template optimization unit is used to optimize the prompt word template generation strategy and fix the quality of the generated code based on the evaluation results.

[0097] In one embodiment, the evaluation result generation unit is used to verify whether the repaired code conforms to the coding specifications and generate a coding evaluation result; to verify whether the input and output of the repaired code and the code to be repaired are consistent through differential testing and generate a consistency evaluation result; to identify whether the code to be repaired introduces new security vulnerabilities or performance degradation and generate a vulnerability or degradation evaluation result; to evaluate the code comment rate and naming specification compliance of the repaired code and generate a specification evaluation result; and to set coding weight, consistency weight, vulnerability or degradation weight, and 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.

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

[0099] The embodiments of the present invention achieve the following technical effects:

[0100] This invention significantly improves the accuracy and code compliance of automated code repair by dynamically optimizing the prompt word template generation strategy through reinforcement learning. It is suitable for efficient, batch processing of code compliance issues during software development. It solves the problems of low efficiency, insufficient accuracy, and poor compliance with coding standards (especially mandatory standards such as national standards) in existing technologies. Through automatic extraction of error information by code scanning tools, pre-generation and invocation of structured prompt word templates, automatic generation of repair code by a large language model, and automatic replacement and evaluation, an end-to-end automated process is formed, greatly reducing manual intervention. It is particularly suitable for batch repair of code compliance issues on a large scale, significantly improving repair efficiency and reducing labor costs. The structured prompt word templates accurately guide the large language model (LLM), and combined with a closed-loop feedback optimization mechanism, it ensures that the repaired code... Under the premise of functional equivalence, syntactic compliance, and preservation of the original logical structure, it significantly improves repair efficiency and code quality. Based on multi-dimensional evaluation results (compliance, functional equivalence, risk defects, readability) as reward signals for reinforcement learning, it adopts a policy gradient optimization algorithm to dynamically fine-tune the prompt word template generation model, forming a "generation-evaluation-optimization" closed loop, continuously improving the guidance effect of prompt word templates, thereby driving the steady growth of the compliance rate and overall quality of repaired code. It supports code scanning tools that are compatible with different coding standards (such as national standards, industry standards, and enterprise rules), and through historical repair sample learning and feedback optimization mechanisms, the prompt word templates 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 that require strict adherence to coding standards (such as military and financial industries), achieving large-scale deployment across projects and standards.

[0101] Obviously, those skilled in the art should understand that the modules or steps of the above-described embodiments of the present invention can be implemented using general-purpose computing devices. They can be centralized on a single computing device or distributed across a network of multiple computing devices. Optionally, they can be implemented using computer-executable program code, thereby storing them in a storage device for execution by a computing device. In some cases, the steps shown or described can be performed in a different order than those presented here, or they can be fabricated as separate integrated circuit modules, or multiple modules or steps can be fabricated as a single integrated circuit module. Thus, the embodiments of the present invention are not limited to any particular hardware and software combination.

[0102] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, various modifications and variations can be made to the embodiments of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.

Claims

1. A code repair method based on a large language model, characterized in that, include: Based on coding standards, the code to be repaired is analyzed using code scanning tools to obtain error information of the code to be repaired. The error information includes error number, rule violation, supplementary explanation, error example, correction example, error type, and error code segment. Based on the historical repair samples of the large language model and the encoding specifications, a prompt word template is generated, and the error information is filled into the prompt word template to generate the filled prompt word. This includes: setting a preset structured placeholder for the prompt word template; embedding error-prone repair warning text in the prompt word template, wherein the error-prone repair warning text includes a description of typical mis-repair patterns for the error type; and based on string replacement technology, according to the preset structured placeholder and the mapping relationship between the error type and the repair scheme, replacing the corresponding preset structured placeholder with the error number, the rule violation, the supplementary explanation, the error example, the correction example, and the error code segment to generate the filled prompt word. The filled prompt word is input into the large language model to generate a repair code segment. It is then determined whether the repair code segment conforms to the set conversion standard. If it does, the code to be repaired is replaced with the repair code segment to generate the repaired code.

2. The code repair method based on a large language model as described in claim 1, characterized in that, Based on historical repair samples and target specifications from the large language model, code repair prompt word templates are generated, including: The mapping relationship between error types and repair schemes in the historical repair samples of the large language model is analyzed, wherein the mapping relationship is used to record code repair operations with regularity; The repair constraint conditions are defined according to the coding specifications, and the repair constraint fields are determined according to the repair constraint conditions. A prompt word template is generated based on the mapping relationship, the error context description field, the repair constraint field, and the output format field.

3. The code repair method based on a large language model as described in claim 1, characterized in that, Determining whether the repair code segment conforms to the set conversion specifications includes: The first criterion is whether the repair code segment and the corresponding code to be repaired are functionally equivalent. The second judgment condition is that the repaired code segment conforms to the syntax specification of the code to be repaired; The third judgment condition is that the logical structure of the repair code segment is the same as that of the corresponding code to be repaired. When the repair code segment simultaneously satisfies the first judgment condition, the second judgment condition, and the third judgment condition, the repair code segment conforms to the set conversion specification.

4. The code repair method based on a large language model as described in any one of claims 1 to 3, characterized in that, Also includes: After generating the repaired code, the repaired code is evaluated using a code scanning tool to generate evaluation results; Based on the evaluation results, the prompt word template generation strategy was optimized and the code generation quality was improved.

5. The code repair method based on a large language model as described in claim 4, characterized in that, The repaired code is evaluated using a code scanning tool, and evaluation results are generated, including: Verify whether the repaired code conforms to the coding specifications, and generate a coding evaluation result; The consistency between the input and output of the repaired code and the code to be repaired is verified by differential testing, and a consistency evaluation result is generated. Identify whether the code to be repaired introduces new security vulnerabilities or performance degradation, and generate vulnerability or degradation assessment results; Evaluate the code comment rate and naming convention compliance of the repaired code, and generate a standardization evaluation result; Each component is assigned a coding weight, a consistency weight, a vulnerability or degradation weight, and a specification evaluation weight. An evaluation result is generated 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.

6. The code repair method based on a large language model as described in claim 4, characterized in that, Based on the evaluation results, the prompt word template generation strategy was optimized and the code generation quality was improved, including: Using the evaluation results as reward signals, a strategy gradient optimization model is constructed; The parameters of the large language model used to generate the prompt word template are adjusted using the strategy gradient optimization model.

7. A code repair device based on a large language model, characterized in that, include: The error information extraction module is used to analyze the code to be repaired based on coding standards and using code scanning tools to obtain the error information of the code to be repaired. The error information includes error number, rule violation, supplementary explanation, error example, correction example, error type and error code segment. The prompt word template filling module is used to generate a prompt word template based on the historical repair samples of the large language model and the encoding specification, and to fill the error information into the prompt word template to generate the filled prompt word; The prompt word template filling module includes: The placeholder generation unit is used to set the preset structured placeholders for the prompt word template; The warning text embedding unit is used to embed error-prone repair warning text into the prompt word template, wherein the error-prone repair warning text includes a description of typical mis-repair patterns for the error type; The prompt word filling unit is used to replace the corresponding preset structured placeholders with the error number, the rule violation, the supplementary explanation, the error example, the correction example, and the error code segment based on string replacement technology and according to the mapping relationship between the preset structured placeholders and the error type and the repair scheme, so as to generate the filled prompt word; The code repair module is used to input the filled prompt words into the large language model, generate a repair code segment, determine whether the repair code segment conforms to the set conversion standard, and if it conforms to the conversion standard, replace the code to be repaired with the repair code segment to generate the repaired code.

8. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the computer program, it implements the code repair method based on a large language model as described in any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that performs the code repair method based on a large language model according to any one of claims 1 to 6.