CN-121979533-A - Code examination opinion automatic generation method based on retrieval enhancement
Abstract
The invention discloses a code examination opinion generation method based on retrieval enhancement, which comprises the steps of 1, constructing a comparison learning training dataset fusing code change structural features and examination intention semantic features, 2, driving a universal pre-training code encoder to migrate and learn to a code examination field by using the comparison learning dataset to obtain a retriever for examination intention perception, 3, constructing a structured prompt template containing an anti-noise mechanism based on a recall history reference case of the retriever after fine adjustment as a reference context, and 4, combining the prompt template, and performing instruction fine adjustment on a large language model by using a low-rank self-adaptive technology (LoRA). The invention effectively solves the problem of serious generalization of the examination opinion in the prior art by the cooperation of remolding the retrieval vector space and the efficient fine adjustment of the parameters, remarkably improves the pertinence and the reference value of the automatically generated opinion, and realizes the expert-level automatic code examination opinion generation technology under the environment of low computing resources.
Inventors
- ZHANG JINGXUAN
- LI LIN
- CHEN JUNHAO
- LIANG XINYUE
- ZHANG JUN
Assignees
- 南京航空航天大学
Dates
- Publication Date
- 20260505
- Application Date
- 20260107
Claims (5)
- 1. An automated generation method of code censoring opinion based on retrieval enhancement is characterized by comprising the following steps: step 1, constructing a comparison learning data set fusing code change structural features and examination intention semantic features, wherein the data set is used for guiding a retriever to reconstruct similarity measurement standards based on examination intention; step 2, driving a universal pre-training code encoder to perform transfer learning to the code examination field by using a comparison learning data set, and constructing a code retriever based on examination intention perception; Step 3, similar retrieval is carried out on the code change to be inspected based on the finely tuned code retriever, top-K historical code changes with the most reference value and inspection opinions thereof are obtained to serve as reference contexts, and a structured prompt template is constructed; And 4, combining the predefined prompt framework, performing targeted tuning on the basic large language model by utilizing a low-rank self-adaptive (LoRA) technology, and generating a final code examination opinion.
- 2. The automatic generation method of code review opinions based on retrieval enhancement according to claim 1, wherein the step 1 is to construct a comparison learning dataset of fusion code modification structural features and review intention semantic features for guiding the retrieval to reconstruct similarity measurement standards based on the review intention, and specifically comprises: The method comprises the steps of obtaining historical code change and corresponding examination opinion data, respectively calculating structural similarity of the code change and semantic similarity of examination opinion, wherein the problem that human examination intention is difficult to capture by an existing method is emphasized, semantic features of examination opinion are extracted, unstructured natural language comments are mapped into high-dimensional vectors by means of a pre-trained sentence vector model, cosine similarity is calculated, so that inherent consistency of historical examination cases on thought logic and points of interest is quantified, meanwhile, structural similarity of the code change is calculated, scale similarity based on logarithmic normalization is calculated for eliminating extreme value deviation, type similarity based on Boolean consistency is calculated for capturing operation properties and mode similarity based on keyword matching to identify a specific programming paradigm, finally, a weighting strategy is adopted to fuse the structural similarity of high-weight semantic similarity and auxiliary similarity, a difficult-to-negative sample mining strategy based on double-threshold filtering is adopted, samples with significant difference between semantics or structure and anchor points are selected from residual samples, and learning training data containing high-degree discrimination samples, positive samples and negative contrast samples are constructed.
- 3. The automatic code review opinion generating method based on retrieval enhancement as defined in claim 1, wherein the step 2 uses a comparison learning data set to perform field adaptive fine tuning on a pre-trained code encoder to construct a code retriever based on review intention perception, and specifically comprises: A UniXcoder pre-training model supporting multiple programming languages is selected as a backbone encoder, a linear projection layer is cascaded at the output end of the encoder for mapping general code features to a specific contrast measurement space, a high-dimensional hidden state is mapped into a low-dimensional feature vector and L2 normalization processing is carried out, in the model training process, a mixed contrast optimization target consisting of triplet loss and InfoNCE loss is constructed, specifically, the geometric distance between samples is optimized by utilizing a triplet loss function, the anchor point and a positive sample are forced to be pulled up by the forced model in the vector space, the anchor point and a negative sample are pushed away by the forced model, meanwhile, the normalized similarity score of the positive sample is optimized by utilizing a InfoNCE loss function aiming at the requirement of accurately identifying target cases in a search task, the recognition probability and the sorting position of the positive sample can be obviously improved when the model faces to a plurality of negative sample interferences, and through the mixed supervision mechanism, the searcher not only has various measurement capacities of the geometric space, but also has the robust capacity under a complex search scene, and deep correlation between semantic code exceeding surface is forced by forcing the model to be deeply studied.
- 4. The automatic code review opinion generating method based on the search enhancement of claim 1, wherein the step 3 is based on a fine-tuned code retriever, performs semantic search on the code change to be reviewed, obtains Top-K most similar historical code changes and review opinions thereof as reference contexts, and constructs a structured prompt template, and specifically comprises: The method comprises the steps of carrying out vector coding on code change to be inspected by using a finely-adjusted retriever, retrieving historical cases through cosine similarity among vectors, constructing a three-level structured template comprising a system instruction layer, a context injection layer and a user input layer in order to guide a large model to enter an inference mode in a specific field and overcome the illusion problem caused by retrieval noise, setting a role alignment instruction of an expert code inspector in the system instruction layer to activate priori knowledge of the model in the code inspection field, embedding a retrieved historical reference case in the context injection layer, configuring an explicit negative constraint instruction and a noise filtering mechanism, and forcibly requesting the model to keep critical thinking when referring to the historical case through natural language instructions, and automatically ignoring retrieval results which are not in accordance with current change logic, thereby ensuring the accuracy of the generated basis.
- 5. The automatic code review opinion generation method based on search enhancement of claim 1, wherein the step 4 uses a low-rank adaptive technique to perform instruction fine tuning on a large language model, and specifically comprises: The method comprises the steps of using LoRA parameter college fine tuning strategies, freezing pre-training weights of all the convectors of a large language model, injecting a trainable low-rank matrix only in a query projection layer and a value projection layer bypass of a self-attention module, setting the rank of the low-rank matrix to be 8 to compress a trainable parameter space by adopting a deterministic low-rank mapping configuration strategy aiming at high requirements of long context input on display memory occupation and feature transfer under a retrieval enhancement scene, reserving display memory resources for gradient calculation of the long template, setting a scaling factor to be 16 to enhance updating weights of specific tasks, setting a discarding rate to be 0, aiming at constructing a deterministic feature transmission path, preventing high-value retrieval context features from being lost in a random discarding process, ensuring that the model can fully utilize retrieved reference information, and setting the loss of a prompting template part to be zero in the training process and only calculating cross entropy loss of a review opinion part generated by the model, thereby guiding how to concentrate on generating review conforming to the opinion specifications based on the given context of a learning expert.
Description
Code examination opinion automatic generation method based on retrieval enhancement Technical Field The invention belongs to the field of intelligent software engineering, and particularly belongs to the field of software quality assurance engineering. To a code review opinion generation method based on retrieval enhancement. Background The systematic execution of code reviews over the software development lifecycle is an indispensable engineering practice to effectively control software bugs introducing risks and improving code robustness. In the traditional software development flow, code examination mainly depends on manual work, which not only consumes a great deal of time and effort for developers, but also gradually makes it difficult for the manual examination to meet the requirements of modern agile development in terms of efficiency and coverage along with explosive growth of code scale and acceleration of iteration speed. Although there are automatic censoring methods based on static analysis tools in the prior art, such methods are usually based on predefined rule templates, can only detect grammar errors or simple style violations, cannot understand the deep semantic logic behind code changes, and also have difficulty in generating natural language censoring comments with interpretability and constructivity. In recent years, the continuous evolution of deep neural network methods, code generation and analysis techniques based on neural machine translation architecture and large-scale pre-trained language models are becoming research hotspots. Existing partial schemes attempt to directly generate code censoring opinions using a generic large language model, while improving the fluency of the language, there are significant limitations. On one hand, the general large language model has wide knowledge, but lacks the specialization in the code examination task of a specific project, the whole parameter fine adjustment has extremely high requirement on computational resources, is easy to cause disastrous forgetting of the model, is difficult to realize the adaptation of expert ability under a low-resource environment, and on the other hand, the existing method only depends on the parameter knowledge of the large model to process code change, the model is difficult to accurately align examination specifications and logic preference of the specific project, and the capability of capturing the core intention of code change is lacking, so that the generated examination opinion is too wide, lacks pertinence, and even generates the 'illusion' phenomenon of deviating facts. To solve the above problems, a search enhanced generation paradigm was introduced into the field, attempting to assist generation by searching for similar historical code changes. However, most of the search modules in the prior art adopt a general lexical matching or vector encoder which is not optimized for Code difference (Code Diff) features, so that the searched historical cases have low semantic similarity with the currently-to-be-examined codes, and the true effective reference information cannot be provided for the generation model. That is, the prior art fails to effectively establish a deep association between code change semantics and review opinions, resulting in the finally generated review opinions still to be improved in accuracy and reference value. Thus, there is a need for an automated code review method that can understand code change semantics in depth and incorporate high quality search context. Disclosure of Invention The invention aims to solve the technical problems of lack of pertinence, low reference value and difficulty in realizing efficient deployment under low computing resources of the code examination opinion automatically generated in the prior art. According to the invention, a contrast learning framework fused with multidimensional code change features and examination intention semantic features is constructed, the vector space of the code retriever is remodeled, so that the code retriever can capture deep examination logic, and an automatic generation technology with expert examination capability is realized under a limited computing resource environment by combining a low-rank self-adaptive fine tuning technology. By optimizing the cooperative work of the retriever and the generator, the high-quality retrieval context is utilized to assist the large model to understand the code change intention, so that the pertinence and the reference value of the automatic generation of the code examination opinion are remarkably improved. In order to achieve the technical purpose, the invention adopts the following technical scheme: The automatic code examination opinion generation method based on retrieval enhancement mainly comprises the following steps: step 1, a contrast learning data construction module, such as a reference numeral ① in fig. 1, constructs a contrast learning training data set fusing the code c