Defect repairing method based on large model feedback optimization
By constructing a guiding code searcher ICR, using syntax tree and large model feedback optimization training, dynamically selecting high-quality examples to guide large language models for defect repair, solving the problem of lack of semantic understanding in existing methods, and achieving more efficient and accurate defect repair results.
Patent Information
- Application Number
- CN202510586865.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-08
- Publication Date
- 2025-08-19
AI Technical Summary
The existing defect repair methods based on large language models lack semantic understanding capabilities, resulting in poor defect repair results.
Build a guiding code searcher ICR, use the syntax tree to capture the structural information in the query and examples, combine iterative training of the searcher with large model feedback optimization, and dynamically select high-quality examples to guide the large language model for defect repair by minimizing iterative training of customized tree structure loss functions.
Improve the quality and generalization ability of defect repair, and maintain the flexibility and efficiency of the method.
Smart Images

Figure CN120508293A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to processing code intelligence tasks using a large language model in the field of software engineering, and specifically to a defect repair method based on large model feedback optimization. Background Art
[0002] Code intelligence tasks aim to automate the analysis, understanding, and generation of code. Typical code intelligence tasks include code summarization, bug fixing, and program synthesis. These tasks are highly sought after because they promise to alleviate the time-consuming manual labor associated with comment writing, debugging, and code editing. As software systems continue to grow in complexity, there is a growing demand for methods that can efficiently and automatically understand, modify, and optimize source code. Recently, large language models have attracted widespread attention due to their impressive performance across a wide range of tasks. Given appropriate prompts, large language models demonstrate exceptional capabilities in various code intelligence tasks, achieving them without requiring parameter fine-tuning. Large language models are also highly sensitive to input prompts. To better guide large language models in code intelligence tasks, some research has explored contextual learning methods, which provide the model with several input and output examples to demonstrate how to follow instructions. Contextual learning requires no fine-tuning and enables effective knowledge transfer with only a small number of examples, making it particularly well-suited for handling diverse tasks and adapting to rapidly changing environments.
[0003] The selection of examples is crucial to the performance of contextual learning. Currently, scholars have proposed methods to retrieve contextual learning examples for various code intelligence tasks.
[0004] Non-Patent Literature 1 (Shuzheng Gao, Xin-Cheng Wen, Cuiyun Gao, Wenxuan Wang, Hongyu Zhang, and Michael R Lyu. 2023. What makes good in-context demonstrations for code intelligence tasks with large language models? In 2023 38th IEEE / ACM International Conference on Automated Software Engineering (ASE). IEEE, 761-773.)
[0005] Gao et al. found that BM25 can effectively retrieve ICL examples for various code intelligence tasks. The BM25 algorithm is an algorithm that calculates the relevance between sentences and documents and is widely used in the field of information retrieval.
[0006] Non-patent document 2 (Toufique Ahmed, Kunal Suresh Pai, Premkumar Devanbu, and Earl Barr. 2024. Automatic semantic augmentation of language model prompts (forcode summarization). In Proceedings of the IEEE / ACM 46th International Conference on Software Engineering. 1-13.)
[0007] Ahmed et al. improved their hints by engineering semantic facts after the BM25 retrieval examples, and they named this approach ASAP. Summary of the Invention
[0008] This paper addresses the shortcomings of existing bug repair methods based on large language models and proposes a bug repair method based on large model feedback optimization. This method combines large model feedback with the construction of an example retriever with semantic understanding capabilities. The high-quality examples retrieved by the retriever guide the large language model, achieving high-quality bug repair.
[0009] A defect repair method based on large model feedback optimization includes the following steps:
[0010] S1. First, the defect repair instructions are linked to the defective code to form a unified query format. The query contains both programming and natural languages, and syntax trees for both programming and natural languages are constructed.
[0011] S2. Preliminarily screen all examples in the sample dataset using the BM25 algorithm. Then, using the syntax trees for programming and natural languages constructed in step S1, the instruction code retriever (ICR) is used to assist in retrieval. A set of relevant examples from the preliminarily screened examples is selected for each query. The examples include both programming and natural languages, and syntax trees for the programming and natural languages are constructed.
[0012] S3. Using the large language model to score the relevant set of examples obtained in step S2, a contrastive learning algorithm is used to learn example patterns that are more useful for defect repair instructions based on the scores of the large language model. By continuously iterating different queries, training data and iterating the instruction code retriever (ICR), after training is completed, a trained instruction code retriever (ICR) is obtained;
[0013] S4. For each test set sample, the trained instruction code retriever ICR and the FAISS library are combined to retrieve the set of examples that are most helpful for the large language model reasoning process. The final input prompt words are constructed using the defect repair instructions, the retrieved most helpful example set, and the defective code. The final input prompt words are input into the large language model for reasoning to obtain the defect repair result.
[0014] This paper proposes a new method called Instructive Code Retriever (ICR) for querying context learning examples for various bug fixing tasks. During the training phase, the Instructive Code Retriever first uses a syntax tree to capture structural information in queries and examples. Then, it uses the inherent capabilities of the large model to determine which examples are more helpful for each query, thereby obtaining positive and negative samples for training the retriever. Finally, we iteratively train the retriever by minimizing a customized tree structure loss function between the retriever similarity and the sequence feedback provided by the large model. During the testing phase, the present invention uses the trained retriever to retrieve valuable examples. These examples are then used to construct prompt words to improve the performance of the large model on bug fixing tasks.
[0015] In step S2, the instruction code retriever ICR includes:
[0016] A dual-encoder architecture consisting of two BERT encoders in parallel.
[0017] In step S2, the dual encoder architecture consisting of two BERT encoders in parallel includes:
[0018] Given a mask query x and an example d, we first use the first BERT encoder E q Query x and the syntax tree x corresponding to the mask x st Encode and get the embedding vector E q (x) and E q (x st ), and then use another BERT encoder E d Syntax tree d corresponding to d and example d st Encode and get the embedding vector E d (d) and E d (d st ), each BERT encoder outputs the contextual embedding of the global semantic token as the embedding of the input, and then aggregates x and x respectively. st The embedding of d and d st The embedding is as follows:
[0019] E qt (x) = α1E q(x)+β1E q (x st )
[0020] E dt (d) = α2E d (d)+β2E d (d st )
[0021] Among them, α1, β1, α2, β2 are four training parameters;
[0022] Finally, vector E qt (x) and E dt The dot product between (d) is regarded as the similarity score between the query and the example.
[0023] In step S3, the large language model is used to score the relevant set of examples, specifically including:
[0024] For a set of examples (d1, d2, ..., d k ), the score S(d i ) is defined as the correct repair result y based on the mask query x and the reference, and the large language model outputs the probability of the correct repair result y according to a set of examples (d1, d2, ..., d k ) to obtain the ranking r(d i ).
[0025] In step S2, the contrastive learning algorithm minimizes the following loss function
[0026]
[0027] Where γ1 and γ2 are hyperparameters, is the batch tree loss function, is the ranking tree loss function.
[0028] In step S2, the batch tree loss function helps the instruction code retriever ICR distinguish the highest quality samples from its suboptimal samples, which is defined as follows:
[0029]
[0030] where d + represents the highest score in the batch, represents the suboptimal example in the batch, sim tree (x,d + ) represents the similarity score between the query and the example, Contains examples of suboptimal queries and other queries in the batch, and e is a natural constant;
[0031] The ranking tree loss function considers the order between suboptimal samples and is defined as follows:
[0032]
[0033] d i and d j are two different candidate examples, D is the set of all candidate examples corresponding to the current query x, sim tree (x,d) represents the similarity score between the query and the example, Contains suboptimal query examples and other query examples in the batch, e is a natural constant, r(d i ) means d i Ranking, r(d j ) means d j ranking.
[0034] In step S4, the final input prompt is constructed using the defect repair instructions, the retrieved most helpful example set, and the defective code, specifically including:
[0035] The number of examples is dynamically adjusted according to the maximum context length, and the final prompt word provided to the large model depends on the maximum input length limited by the model|C i | and the maximum output length |C o |,ensure that the length of the instruction to fix the bug, the length of the set of most helpful examples retrieved and the maximum output length,|C o The sum of the lengths of | is less than a hyperparameter |C m |, and ensure that |C m |<maximum input length|C i |, and make the final prompt word the maximum length of all prompt words that meet the conditions.
[0036] Furthermore, a defect repair method based on large model feedback optimization of the present invention includes the following steps:
[0037] S1. During the preprocessing and preparation phase, the present invention first connects task-specific instructions with the raw input to form a query in a unified format. Queries and examples may contain both programming and natural language, and the present invention constructs syntax trees for both programming and natural languages. The present invention selects relevant examples for each sample using a combination of preliminary screening and instruction code retrieval (ICR) retrieval, and scores these relevant examples using a large language model.
[0038] S2. During the training of the instruction code retriever (ICR), for each query, the first step is to retrieve a set of relevant examples from the corpus. The present invention then uses a large language model to evaluate whether these examples contribute to the large language model's reasoning. Based on the feedback from the language model, the present invention utilizes contrastive learning to learn example patterns that are more useful for a given task. The present invention continuously optimizes the training data and iteratively trains the retriever. After training, the instruction code retriever (ICR) can identify which examples contribute to the large language model's reasoning and which do not.
[0039] S3. During the inference phase, for each test set example, the present invention combines the trained instruction code retriever (ICR) and the FAISS library to retrieve the set of examples that are most helpful for the large language model's inference process. The present invention uses the task instructions, the retrieved examples, and the actual input of the current task to construct the final input prompt word for inference with the large language model.
[0040] Among them, step S1 includes: preprocessing and preparation
[0041] Furthermore, the task instruction is a short text describing the task goal. For example, in a bug fixing task, the task instruction can be "Fix the bug in this code." The present invention connects the task instruction with the original input for model training to form the instruction code retriever ICR model query, which has the following format: (x, y) = T i ⊕(m,n), where T i is the task instruction, (m,n) is the input-output data pair obtained from the training dataset, and ⊕ is the concatenation operator.
[0042] Furthermore, a syntax tree is a tree-like representation of the abstract grammatical structure of programs and natural languages. This paper uses the 'javalang' and 'ast' libraries to derive the abstract syntax tree of the code, while for the natural language portion, this paper uses the 'spacy' library to extract its syntax tree. 'ast', 'javalang', and 'spacy' are Python libraries.
[0043] Furthermore, the present invention is prepared to use a combination of preliminary screening and instruction code retriever ICR retrieval to select relevant examples for each sample. In the 0th round of iteration, when the retriever of the present invention has not yet been trained, the present invention uses BM25 retrieval to search the top k (about 50-100) examples (i.e., input-output pairs) of each sample in the training set, which have the highest BM25 scores. It should be noted that the present invention is currently processing the training set and can make full use of the true labels to narrow the scope and select the most relevant examples. To this end, the present invention calculates the similarity between the query in the 0th round of iteration (i.e., (x, y)) and the examples in the other training sets. In the 1st, 2nd, ..., nth rounds of iteration, once the retriever of the present invention has completed training, the present invention will use the trained retriever to find each mask query (x, -) = T in the training set. i The top K examples (i.e., input-output pairs) of ⊕(m, -) with the highest scores. At this stage, the true labels of the queries are masked to ensure consistency between the training and testing phases. The input to the instruction code retriever ICR includes the query itself and its syntax tree, as well as the examples and their syntax trees.
[0044] Furthermore, the present invention uses a large language model to score relevant examples. Specifically, for a given defective code and a reference correct repair result (x, y) and its example set (d1, d2, ..., d k ), the present invention defines the score of an example as the probability S(d) that the large language model outputs the true result y based on the input x and example y i ), as shown below:
[0045] S(d i )=P G (y|d i ,x)
[0046] d i represents the i-th example in the example set, P G is the conditional function.
[0047] According to a set of examples (d1, d2, ..., d k ) to obtain the ranking r(d i ). Examples with higher scores are ranked higher, r(d i ) is smaller.
[0048] Wherein, step S2 includes: instruction code retrieval ICR
[0049] Furthermore, given a mask query x and an example d, we first use the first BERT encoder E q Query x and the syntax tree x corresponding to the mask x st Encode and get the embedding vector Eq (x) and E q (x st ), and then use another BERT encoder E d Syntax tree d corresponding to d and example d st Encode and get the embedding vector E d (d) and E d (d st ), each BERT encoder outputs the contextual embedding of the global semantic token as the embedding of the input, and then aggregates x and x respectively. st The embedding of d and d st The embedding is as follows:
[0050] E qt (x) = α1E q (x)+β1E q (x st )
[0051] E dt (d) = α2E d (d)+β2E d (d st )
[0052] Among them, α1, β1, α2, β2 are four training parameters;
[0053] Finally, vector E qt (x) and E dt The dot product between (d) is regarded as the similarity score between the query and the example.
[0054] Furthermore, the contrastive learning algorithm adopts the method of minimizing the following loss function
[0055]
[0056] Where γ1 and γ2 are hyperparameters, is the batch tree loss, is the ranking tree loss;
[0057] The batch tree loss function helps the retriever distinguish between the highest quality samples and their suboptimal samples and is defined as follows:
[0058]
[0059] where d + represents the highest score in the batch, represents the suboptimal example in the batch, sim tree (x,d + ) represents the similarity score between the query and the example, Contains suboptimal examples for the query and examples for other queries in the batch, with e being a natural constant. Note that negative samples include not only suboptimal examples for the current query, but also examples for other queries in the same batch (irrelevant examples). The batch tree loss function employs the principle of contrastive learning, enabling the retriever to identify the examples in the batch that provide the most help in inferring the ground truth.
[0060] Furthermore, the ranking tree loss function considers the ranking between suboptimal samples and is defined as follows:
[0061]
[0062] d i and d j are two different candidate examples, and D is the set of all candidate examples corresponding to the current query x. tree (x,d) represents the similarity score between the query and the example, Contains the suboptimal query examples and other query examples in the batch, e is a natural constant. r(d i ) means d i Ranking, r(d j ) means d j The ranking, when d i Ranked higher than d j When (ie r(d i ) <r(d j )), this loss function will optimize the model to make the input x and d i The similarity between them is greater than that between x and d j The similarity between them, that is, sim tree (x,d i )>sim tree (x,d j ). Therefore, the retriever calculates higher similarity scores for higher-ranked examples. The larger the ranking difference r, the greater the optimization, which expands d i with d j The similarity gap between them.
[0063] Step S3 includes: task-specific reasoning method
[0064] Furthermore, for each test sample, we first use the trained instruction code retriever ICR retriever and FAISS library to retrieve the most relevant example set from the training data. d The encoder pre-encodes the embedding vectors of all training examples. For each test example, the present invention obtains its mask query (x test ,-) and calculate its code E qt , and then use the FAISS library to search for the inner product sim in the training data tree(x test ,d) is the largest series of examples. Then, the present invention arranges these examples in descending order to generate an example sequence D = (d1, d2…d L ).
[0065] Furthermore, the number of examples is dynamically adjusted according to the maximum context length. The final number of hint words provided to the large model depends on the maximum input length limited by the model |C i | and the maximum output length |C o |, the sum of the length of the instruction to ensure the defect repair, the length of the most helpful example set retrieved, and the maximum output length |C o The sum of the lengths of | is less than a hyperparameter |C m |, and ensure that |C m |<maximum input length|C i |, rather than using a fixed number of examples. This effectively avoids the example truncation problem. It also ensures that the final prompt word is the maximum length among all the prompt words that meet the conditions. Furthermore, the present invention uses the defect repair instructions, the retrieved example set, and the defective code of the current task to construct the final input prompt word, which has the following format:
[0066]
[0067] Instruction is the defect repair instruction, Exemplar1‖…|| is the retrieved example set, Input is the defective code for the current task, and Prompt is the final input prompt word obtained.
[0068] The present invention inputs it into a large model to obtain the final output.
[0069] This paper proposes a bug repair method using large-model feedback optimization. By building an example retriever with semantic understanding capabilities, it dynamically selects high-quality examples and guides a large language model to perform efficient and accurate bug repair. This method improves repair quality and generalization while maintaining flexibility, and has broad application prospects. BRIEF DESCRIPTION OF THE DRAWINGS
[0070] Figure 1 Flow chart of the method of the present invention;
[0071] Figure 2 Illustration of training the retriever model;
[0072] Figure 3 The difference between the present invention and other methods; DETAILED DESCRIPTION
[0073] The present invention trains the instruction code retriever (ICR) model on the following five datasets: B2F_small, B2F_medium, CSN-Java, CSN-Python, and Conala. Like all contextual learning methods, the present invention does not update the large language model itself, but only focuses on training the retriever.
[0074] Due to computational resource limitations, we only used a quarter of the data in the CSN-Python dataset for training and divided the CSN-Java dataset into four segments to accelerate training. When selecting relevant examples for each sample, we selected the K = 50 most relevant examples for each sample. Due to computational limitations, we performed three rounds of iterative training and observed that the loss converged after four rounds of training. Therefore, we trained the instruction code retriever (ICR) for four epochs per iteration.
[0075] Training was performed on a server equipped with eight NVIDIA A800-80G GPUs. To address duplication in the generated content, the present invention trimmed duplicates during post-processing, retaining only unique instances. Furthermore, because the present invention required training on multiple datasets of significantly varying sizes, the sampling rate for Conala was set to 3 samples per round, while the sampling rate for the other datasets was set to 0.7.
[0076] The present invention sets |C m |=2048, which corresponds to the maximum input length of the GPT-Neo-2.7B model. Given that the batch tree loss calculates sample similarity within a batch of queries, while the ranking tree loss only calculates samples within a single query, the batch tree loss is typically larger. To ensure the effectiveness of both loss functions, we set γ1 to 1 and γ2 to 4. During implementation, we fixed the random seed to ensure consistency in experimental results.
[0077] During the preprocessing and preparation phase, the present invention first connects task-specific instructions with the raw input to form a query in a unified format. Queries and examples may contain both programming and natural languages, and the present invention constructs syntax trees for both programming and natural languages, respectively. The present invention selects relevant examples for each sample using a combination of preliminary screening and instruction code retrieval (ICR) retrieval, and scores these relevant examples using a large language model.
[0078] S2. During the training of the instruction code retriever (ICR), for each query, the first step is to retrieve a set of relevant examples from the corpus. The present invention then uses a large language model to evaluate whether these examples contribute to the large language model's reasoning. Based on the feedback from the language model, the present invention utilizes contrastive learning to learn example patterns that are more useful for a given task. The present invention continuously optimizes the training data and iteratively trains the retriever. After training, the instruction code retriever (ICR) can identify which examples contribute to the large language model's reasoning and which do not.
[0079] S3. During the inference phase, for each test set example, the present invention combines the trained instruction code retriever (ICR) and the FAISS library to retrieve the set of examples that are most helpful for the large language model's inference process. The present invention uses the task instructions, the retrieved examples, and the actual input of the current task to construct the final input prompt word for inference with the large language model.
[0080] 1. Preprocessing and preparation stage
[0081] 1.1) A task instruction is a short text describing the task goal. For example, in a bug fixing task, the task instruction could be “Fix the bug in this code.” This paper connects the task instruction with the original input for model training to form an instruction code retriever (ICR) model query with the following format: (x, y) = T i ⊕(m,n), where T i is the task instruction, (m,n) is the input-output data pair obtained from the training dataset, and ⊕ is the concatenation operator.
[0082] 1.2) Syntax trees are tree-like representations of the abstract grammatical structure of programs and natural languages. This paper uses the 'javalang' and 'ast' libraries to derive the abstract syntax tree of the code, and the 'spacy' library to extract the syntax tree for the natural language portion. 'ast', 'javalang', and 'spacy' are Python libraries.
[0083] 1.3) The present invention prepares to use a combination of preliminary screening and instruction code retriever ICR retrieval to select relevant examples for each sample. In the 0th round of iteration, when the retriever of the present invention has not yet been trained, the present invention uses BM25 retrieval to search the top K examples (i.e., input-output pairs) of each sample in the training set, which have the highest BM25 scores. It should be noted that the present invention is currently processing the training set and can make full use of the true labels to narrow the scope and select the most relevant examples. To this end, the present invention calculates the similarity between the query in the 0th round of iteration (i.e., (x, y)) and the examples in other training sets. In the 1st, 2nd, ..., nth rounds of iteration, once the retriever of the present invention has completed training, the present invention will use the trained retriever to find each mask query (x, -) = T in the training set. i The top K examples (i.e., input-output pairs) of ⊕(m, -) with the highest scores. At this stage, the true labels of the queries are masked to ensure consistency between the training and testing phases. The input to the instruction code retriever ICR includes the query itself and its syntax tree, as well as the examples and their syntax trees.
[0084] 1.4) Furthermore, the present invention uses a large language model to score relevant examples. Specifically, for a given sample (x, y) and its example set (d1, d2, ..., d k ), the present invention defines the score of an example as the probability that the large language model outputs the true result y based on the input x and example y, as follows:
[0085] S(d i )=P G (y|d i ,x)
[0086] d i represents the i-th example in the example set, P G is the conditional function.
[0087] 1.5) Based on the score of the example, the present invention obtains the ranking r(d i ), which will guide the training of the retriever in the subsequent section. r(d i ) is as follows:
[0088] r(d i )=rank(j <i∣S(d j )>S(d i ))
[0089] S(d j ) refers to d j The probability that the large language model outputs the true result y under the input of Example 3, S(d i ) refers to d iThe probability that the large language model outputs the true result y under the input of example 3. Examples with higher scores are ranked higher, r(d i ) is smaller.
[0090] 2. Training the Instructional Code Retriever Model (Instruction Code Retriever ICR)
[0091] 2.1) Instruction Code Retrieval (ICR) is a dual encoder architecture consisting of two BERT encoders. Given a mask query x and an example d, we first use the first BERT encoder E q Query x and the syntax tree x corresponding to the mask x st Encode and get the embedding vector E q (x) and E q (x st ), and then use another BERT encoder E d Syntax tree d corresponding to d and example d st Encode and get the embedding vector E d (d) and E d (d st ), each BERT encoder outputs the contextual embedding of the global semantic token as the embedding of the input, and then aggregates x and x respectively. st The embedding of d and d st The embedding is as follows:
[0092] E qt (x) = α1E q (x)+β1E q (x st )
[0093] E dt (d) = α2E d (d)+β2E d (d st )
[0094] 2.2) α1, β1, α2, β2 are four trainable parameters. Finally, the present invention converts the vector E qt (x) and E dt (d) The dot product between them is regarded as the similarity score sim between the query and the example tree (x,d), as shown below:
[0095]
[0096] The model parameters are optimized by minimizing the following loss function:
[0097]
[0098] where γ1 and γ2 are hyperparameters. is the batch tree loss, is the ranking tree loss.
[0099] 2.3) The batch tree loss function helps the retriever distinguish between the highest quality samples and their suboptimal samples and is defined as follows:
[0100]
[0101] where d + represents the highest score in the batch, represents the suboptimal example in the batch, sim tree (x,d + ) represents the similarity score between the query and the example, Contains suboptimal examples for the query and examples for other queries in the batch, with e being a natural constant. Note that negative samples include not only suboptimal examples for the current query, but also examples for other queries in the same batch (irrelevant examples). The batch tree loss function employs the principle of contrastive learning, enabling the retriever to identify the examples in the batch that provide the most help in inferring the ground truth.
[0102] 2.4) The ranking tree loss function considers the ranking between suboptimal samples and is defined as follows:
[0103]
[0104] d i and d j are two different candidate examples, and D is the set of all candidate examples corresponding to the current query x. tree (x,d) represents the similarity score between the query and the example, Contains the suboptimal query examples and other query examples in the batch, e is a natural constant. r(d i ) means d i Ranking, r(d j ) means d j The ranking, when d i Ranked higher than d j When (ie r(d i ) <r(d j )), this loss function will optimize the model to make the input x and d i The similarity between them is greater than that between x and d j The similarity between them, that is, sim tree (x,d i )>sim tree (x,d j ). Therefore, the retriever calculates higher similarity scores for higher-ranked examples. The larger the ranking difference r, the greater the optimization, which expands d iwith d j The similarity gap between them.
[0105] 3. Task-Specific Reasoning
[0106] 3.1) For each test sample, first use the trained instruction code retriever ICR retriever and FAISS library to retrieve the most relevant example set from the training data. d The encoder pre-encodes the embedding vectors of all training examples. For each test example, the present invention obtains its mask query (x test ,-) and calculate its code E qt , and then use the FAISS library to search for the inner product sim in the training data tree (x test ,d) is the largest series of examples. Then, the present invention arranges these examples in descending order to generate a sequence D = (d1, d2…d L ).
[0107] 3.2) The final prompt word provided to the large model depends on the maximum input length of the model |C i | and the maximum output length |C o |, make sure |d i | is the length of the i-th example, |x test | is the length of the defect repair instruction, and |P| is the length of the final input prompt word. The present invention sets a hyperparameter |C m | and make sure |C m |<|C i |, rather than using a fixed number of examples. This effectively avoids the example truncation problem and ensures that the final prompt word is the maximum length among all the prompt words that meet the conditions.
[0108] 3.3) The present invention uses the task instructions, retrieved examples, and the actual input of the current task to construct the final input prompt word, which has the following format:
[0109] Instruction is the defect repair instruction, Exemplar1‖…|| is the retrieved example set, Input is the defective code for the current task, and Prompt is the final input prompt word obtained.
[0110] The present invention inputs it into a large model to obtain the final output.
[0111] The Instruction Code Retrieval (ICR) method proposed in this invention performs performance evaluation on multiple code intelligence tasks with different output forms. Therefore, this invention mainly uses the BLEU indicator for evaluation. BLEU is an evaluation metric widely used in bug fixing and code summary generation tasks. It quantifies the quality of the model output by comparing the similarity between the automatically generated text and the reference text. BLEU is mainly evaluated by analyzing the n-gram overlap between the candidate text and multiple reference texts, and the score is calculated based on the weighted accuracy of the n-grams in the generated text. An n-gram refers to n consecutive words in a text.
[0112] In order to evaluate the effectiveness of the basic model, the present invention first compares the performance of the instruction code retriever ICR with the baseline method on the open source model GPT-Neo-2.7B. Figure 3 The specific evaluation results are shown: in the bug repair task, the instruction code retriever ICR improves the BLEU-4 scores by 3.6 and 3.2 on the B2F_small and B2F_medium datasets respectively. In order to verify the generalization ability of the instruction code retriever ICR, the present invention migrates the instruction code retriever ICR model trained on GPT-Neo-2.7B to the more powerful large model Code Llama-13B for testing. Figure 3 As shown, the instruction code retriever ICR still significantly outperforms all baseline methods in multiple metrics.
[0113] The above embodiments are used to illustrate the present invention rather than to limit the present invention. Any modifications and changes made to the present invention within the spirit of the present invention and the protection scope of the claims shall fall within the protection scope of the present invention.
Claims
1. A defect repair method based on large model feedback optimization, characterized in that: The following steps are involved: S1. First, the defect repair instructions are linked to the defective code to form a unified query format. The query contains both programming and natural languages, and syntax trees for both programming and natural languages are constructed. S2. Preliminarily screen all examples in the sample dataset using the BM25 algorithm. Then, using the syntax trees for programming and natural languages constructed in step S1, the instruction code retriever (ICR) is used to assist in retrieval. A set of relevant examples from the preliminarily screened examples is selected for each query. The examples include both programming and natural languages, and syntax trees for the programming and natural languages are constructed. S3. Using the large language model to score the relevant set of examples obtained in step S2, a contrastive learning algorithm is used to learn example patterns that are more useful for defect repair instructions based on the scores of the large language model. By continuously iterating different queries, training data and iterating the instruction code retriever (ICR), after training is completed, a trained instruction code retriever (ICR) is obtained; S4. For each test set sample, the trained instruction code retriever ICR and the FAISS library are combined to retrieve the set of examples that are most helpful for the large language model reasoning process. The final input prompt words are constructed using the defect repair instructions, the retrieved most helpful example set, and the defective code. The final input prompt words are input into the large language model for reasoning to obtain the defect repair result.
2. The defect repair method based on large model feedback optimization according to claim 1 is characterized in that: In step S2, the instruction code retriever ICR includes: A dual-encoder architecture consisting of two BERT encoders in parallel.
3. The defect repair method based on large model feedback optimization according to claim 2 is characterized in that: In step S2, the dual encoder architecture consisting of two BERT encoders in parallel includes: Given a mask query x and an example d, we first use the first BERT encoder E q Query x and the syntax tree x corresponding to the mask x st Encode and get the embedding vector E q (x) and E q (x st ), and then use another BERT encoder E d Syntax tree d corresponding to d and example d st Encode and get the embedding vector E d (d) and E d (d st ), each BERT encoder outputs the contextual embedding of the global semantic token as the embedding of the input, and then aggregates x and x respectively. st The embedding of d and d st The embedding is as follows: E qt (x)=α1E q (x)+β1E q (x st ) E dt (d)=α2E d (d)+β2E d (d st ) Among them, α1, β1, α2, β2 are four training parameters; Finally, vector E qt (x) and E dt The dot product between (d) is regarded as the similarity score between the query and the example.
4. The defect repair method based on large model feedback optimization according to claim 1 is characterized in that: In step S3, the large language model is used to score the relevant set of examples, specifically including: For a set of examples (d1, d2, ..., d k ), the score S(d i ) is defined as the correct repair result y based on the mask query x and the reference, and the large language model outputs the probability of the correct repair result y according to a set of examples (d1, d2, ..., d k ) to obtain the ranking r(d i ).
5. The defect repair method based on large model feedback optimization according to claim 1 is characterized in that: In step S2, the contrastive learning algorithm minimizes the following loss function Where γ1 and γ2 are hyperparameters, is the batch tree loss function, is the ranking tree loss function.
6. The defect repair method based on large model feedback optimization according to claim 5 is characterized in that: In step S2, the batch tree loss function helps the instruction code retriever ICR distinguish the highest quality samples from its suboptimal samples, which is defined as follows: where d + represents the highest score in the batch, represents the suboptimal example in the batch, sim tree (x,d + ) represents the similarity score between the query and the example, Contains examples of suboptimal queries and other queries in the batch, and e is a natural constant; The ranking tree loss function considers the order between suboptimal samples and is defined as follows: d i and d j are two different candidate examples, D is the set of all candidate examples corresponding to the current query x, sim tree (x,d) represents the similarity score between the query and the example, Contains suboptimal query examples and other query examples in the batch, e is a natural constant, r(d i ) means d i Ranking, r(d j ) means d j ranking.
7. The defect repair method based on large model feedback optimization according to claim 1 is characterized in that: In step S4, the final input prompt is constructed using the defect repair instructions, the retrieved most helpful example set, and the defective code, specifically including: The number of examples is dynamically adjusted according to the maximum context length, and the final prompt word provided to the large model depends on the maximum input length limited by the model|C i | and the maximum output length |C o |,length of the instruction to ensure the bug fix, the length of the most helpful example set retrieved, and the maximum output length |C o The sum of the lengths of the three is less than a hyperparameter |C m |, and ensure that |C m |<Maximum input length|C i |, and make the final prompt word the maximum length of all prompt words that meet the conditions.