Code review opinion automatic generation method based on retrieval enhancement
By constructing a contrastive learning framework that integrates multi-dimensional features and a low-rank adaptive fine-tuning technique, the problem of code review comments lacking specificity and reference value in existing technologies is solved, achieving efficient and expert-level code review comment generation, which is suitable for low-computing-resource environments.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- NANJING UNIV OF AERONAUTICS & ASTRONAUTICS
- Filing Date
- 2026-01-07
- Publication Date
- 2026-05-05
AI Technical Summary
The code review comments automatically generated in the existing technology lack specificity and have low reference value. Furthermore, they are difficult to deploy efficiently in environments with low computing resources. The existing retrieval modules cannot effectively establish a deep correlation between the semantics of code changes and the review comments.
We construct a comparative learning framework that integrates multi-dimensional code change features and review intent semantic features. Through domain-adaptive fine-tuning and low-rank adaptive fine-tuning techniques, we optimize the collaborative work between the retrieval and generator. We utilize high-quality retrieval context to assist the large model in understanding code change intent and generate highly targeted and valuable review opinions.
It achieves a paradigm shift in retrieval from syntax-level matching to intent-level matching, and the generated review opinions have expert-level relevance and reference value. It solves the illusion and blind obedience problem of generative large models in professional code review scenarios, and provides an efficient and scalable automated code review system.
Smart Images

Figure CN121979533A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of intelligent software engineering, specifically the field of software quality assurance engineering. It relates to a method for generating code review comments based on enhanced retrieval. Background Technology
[0002] To effectively control the risks introduced by software defects and improve code robustness, systematically performing code reviews throughout the software development lifecycle is an indispensable engineering practice. In traditional software development processes, code reviews primarily rely on manual processes. This not only consumes a significant amount of time and energy from developers, but also, with the explosive growth of code size and the accelerating pace of iteration, manual reviews are increasingly unable to meet the efficiency and coverage requirements of modern agile development. Although there are existing automated review methods based on static analysis tools, these methods are typically based on predefined rule templates and can only detect syntax errors or simple style violations. They cannot understand the deep semantic logic behind code changes and are also difficult to generate interpretable and constructive natural language review comments.
[0003] In recent years, with the continuous evolution of deep neural network methods, code generation and analysis techniques based on neural machine translation architectures and large-scale pre-trained language models have gradually become research hotspots. Some existing solutions attempt to directly utilize general-purpose large language models to generate code review comments. While these methods improve fluency, they still have significant limitations. On the one hand, although general-purpose large language models possess extensive knowledge, they lack specialization in code review tasks specific to certain projects. Furthermore, full parameter fine-tuning not only demands extremely high computational resources but also easily leads to catastrophic forgetting of the model, making it difficult to achieve expert-level capabilities in low-resource environments. On the other hand, existing methods rely solely on the parameter knowledge of the large model itself to handle code changes. The model struggles to accurately align with the review guidelines and logical preferences of specific projects, lacking the ability to capture the core intent of code changes. This results in overly broad and unspecific review comments, and even the creation of 'illusions' that deviate from reality.
[0004] To address the aforementioned issues, a retrieval-enhanced generative paradigm has been introduced into this field, attempting to assist in generation by retrieving similar historical code changes. However, most existing retrieval modules employ general lexical matching or vector encoders not optimized for code diff features, resulting in low semantic similarity between retrieved historical cases and the current code under review. This fails to provide truly effective reference information for the generative model. In other words, existing technologies have failed to effectively establish a deep correlation between code change semantics and review opinions, leading to room for improvement in the accuracy and reference value of the final generated review opinions. Therefore, there is an urgent need for an automated code review method that can deeply understand code change semantics and combine it with high-quality retrieval context. Summary of the Invention
[0005] The technical problem this invention aims to solve is to address the shortcomings of existing technologies by providing an automated code review comment generation method based on retrieval enhancement. This method addresses the issues of lack of specificity, low reference value, and difficulty in efficient deployment under low computing resources in automatically generated code review comments. This invention constructs a contrastive learning framework that integrates multi-dimensional code change features and review intent semantic features, reshaping the vector space of the code retrieval machine to capture deep review logic. Combined with low-rank adaptive fine-tuning technology, it achieves an automated generation technology with expert-level review capabilities under limited computing resources. By optimizing the collaborative work between the retrieval machine and the generator, and utilizing high-quality retrieval context to assist the large model in understanding code change intent, the method significantly improves the specificity and reference value of automatically generated code review comments.
[0006] To achieve the above-mentioned technical objectives, the technical solution adopted by the present invention is as follows:
[0007] An automated method for generating code review comments based on enhanced retrieval mainly includes the following steps:
[0008] Step 1, as follows Figure 1 The comparative learning data construction module (number ①) constructs a comparative learning training dataset that integrates code change structural features and censorship intent semantic features, which is used to guide the retrieval machine to reconstruct a similarity metric based on censorship intent.
[0009] Step 2, as follows Figure 1 The domain adaptive fine-tuning module of the retrieval system (No. ②) uses a contrastive learning dataset to drive the general pre-trained code encoder to transfer learning to the code review domain, and builds a code retrieval system based on review intent awareness.
[0010] Step 3, as follows Figure 1 The RAG fine-tuning instruction construction module (number ③) performs a similar search on the code changes to be reviewed based on the fine-tuned code searcher, obtains the Top-K most valuable historical code changes and their review comments as reference context, and constructs a structured prompt template.
[0011] Step 4, as follows Figure 1 The generator instruction fine-tuning module (number ④) combines the predefined prompting framework and uses low-rank adaptive (LoRA) technology to perform targeted optimization on the basic large language model, generating the final code review comments.
[0012] The following is a detailed explanation of each step in the above technical solution:
[0013] Step 1 describes constructing a comparative learning training dataset that integrates structural features of code changes and semantic features of censorship intent. This aims to overcome the limitations of traditional retrieval methods that rely solely on surface syntactic features of code text for similarity matching. By integrating objective structural features of code changes with subjective semantic features of censorship opinions, positive and negative sample pairs are constructed to guide the retrieval system in reconstructing a similarity metric based on censorship intent. Specifically:
[0014] First, historical code changes and their corresponding review comments are acquired. Then, the comprehensive similarity between historical code pairs is calculated from two dimensions: the structural features of the code change itself and the semantic features of the historical review comments corresponding to the code change. A similarity weighting strategy is used to construct positive and negative similarity sample pairs required for training the retrieval system. On the one hand, for the structural similarity calculation of code changes, this invention constructs a multi-dimensional similarity measurement space that perceives code change differences. Its similarity calculation covers three aspects: change scale similarity, change type similarity, and code pattern similarity. On the other hand, for the subjective semantic similarity calculation of review comments, this invention uses a pre-trained language model to extract the semantic vector of the review comments and calculates the cosine similarity to capture the consistency of human reviewers at the level of review intent. Finally, the structural similarity score of the code changes and the semantic similarity score of the review comments are fused through a weighting strategy. In order to guide the retrieval system to focus on the review intent, this invention gives higher weight to the semantic features of the review comments in the comprehensive similarity calculation, specifically using the following weighting calculation method: S total =α·S sem +(1-α)·S struct Among them, S sem For semantic similarity, S struct For structural similarity, α is a semantic weight coefficient (e.g., set to 0.7) to ensure that positive sample pairs judged as highly similar are highly consistent in terms of "review focus". Based on this comprehensive similarity, anchor data, positive sample data, and negative sample data are selected within the data group of the same programming language to construct triple training data for comparative learning. Each sample in the triple consists of a 'code change fragment' and its corresponding 'review comments'. Positive samples are those with high semantic similarity to the anchor. Figure 1 The model is trained to distinguish between different censorship scenarios by using historical examples that are consistent with the anchor points and negative examples that are significantly different from the anchor points in terms of semantics or structure.
[0015] The domain-adaptive fine-tuning of the code retrieval tool based on the contrastive learning strategy described in step 2 aims to transfer the implicit prior knowledge of censorship intent from the dataset constructed in step 1 to the pre-trained model. By reshaping the vector representation space, the retrieval tool gains the ability to capture deep censorship logic beyond the surface form of the code. Specifically:
[0016] This invention selects a multilingual, pre-trained code model (such as UniXcoder) as the backbone encoder and introduces a trainable linear projection layer at its output to adapt to the contrastive learning requirements of specific tasks. During training, the triplet data constructed in step 1 is input in batches into the general encoder to obtain its high-dimensional vector representation. Subsequently, triplet loss and InfoNCE loss are used to calculate distance constraints between vectors, jointly constructing distance constraints in the representation space. Gradient descent optimization is then performed via backpropagation to iteratively adjust the model weights. Mathematically, this process forces the model to optimize its vector space distribution, narrowing the Euclidean distance between anchor code and positive sample code with similar censorship intent, while simultaneously increasing the distance between anchor code and negative sample code that only has superficial syntactic similarity but different censorship intent. After this domain-adaptive fine-tuning, the code difference vector generated by the retrieval system is no longer merely a mapping of lexical features, but a hybrid feature representation that integrates code change semantics and potential censorship concerns.
[0017] The contextual retrieval and suggestion construction based on the fine-tuned retrieval tool described in step 3 aims to provide the generative model with expert-level contextual information that has a high signal-to-noise ratio and high reference value. It serves as a crucial bridge connecting the historical knowledge base and the generative model using the fine-tuned and optimized retrieval tool from step 2. Specifically:
[0018] First, the domain-adapted retrieval tool is invoked to obtain the representation vector of the target code change, which is then mapped to the optimized semantic representation space. Next, an efficient similarity retrieval is performed in the vector database to retrieve the Top-K most similar historical code changes and their corresponding review comments as reference context. Subsequently, a structured prompt template (Prompt) incorporating expert role settings and a noise-resistant context learning mechanism is constructed. The template is designed with three key layers. First, the system instruction layer explicitly instructs the generating model to focus the latent semantic space on the professional domain of a "senior code reviewer." Second, the context enhancement layer embeds retrieved TOP-K historical cases as "reference examples" to stimulate the model's contextual learning ability. To prevent the model from being interfered with by potentially irrelevant information in the search results, this invention introduces noise filtering instructions, which force the model to have the ability to discriminate the search content while referring to historical patterns, focusing on the specific logic of the target code change. If the search case is not applicable, it is automatically ignored, thus preventing the model from generating "illusions" based on incorrect searches. Finally, the user input layer, which is the code change to be reviewed, is the core object of the large language model's inference. This layer is located at the bottom of the prompt template, explicitly instructing the large model to focus on the current code change content and, combined with the system instructions and noise-resistant search context from the previous layers, generate highly targeted final review opinions.
[0019] Step 4, which involves fine-tuning the large language model's instructions based on Low-Rank Adaptive (LoRA), uses the filled-in prompt template as input and leverages efficient parameter fine-tuning techniques to unleash the potential of the large language model for specific code review tasks. This aims to address the high computational resource consumption issue associated with full fine-tuning while preserving the generalization ability of the general-purpose large model. Specifically:
[0020] This invention uses the open-source large language model Llama 3 series as the base model. During the fine-tuning phase, the main parameters of the base model are fixed, and a trainable low-rank update matrix is appended to the projection transformation part of each attention mechanism layer in the Transformer architecture. The sample pairs consisting of the context-information-containing prompt template constructed in step 3 and the standard review comments are input into the model. By minimizing the cross-entropy loss between the generated text and the actual review comments, only the aforementioned low-rank parameters are optimized. This approach enables the model to quickly adapt to the language style, terminology, and reasoning logic specific to code review tasks with extremely low memory usage and computational cost, ultimately outputting high-quality code review comments.
[0021] The present invention has the following beneficial effects:
[0022] First, this invention overcomes the semantic gap between code differences and review intent, achieving a paradigm shift from "syntax-level matching" to "intent-level matching" in retrieval. Existing technologies are mostly limited to the edit distance or general vector similarity on the surface of the code text, often failing to capture the deep logic behind code changes. This invention innovatively constructs a contrastive learning framework that fuses heterogeneous features, aligning the objective physical characteristics (scale, type, pattern) of code changes with the subjective semantic characteristics of review opinions through strong supervised signals. This method transforms the retrieval tool from merely a text matching tool into one possessing a "review intuition" similar to that of human experts, accurately identifying historical cases where, despite different code styles, similar logical flaws or specification issues triggered the same review opinions. This provides the generation stage with truly valuable, shared-origin contextual references.
[0023] Secondly, this invention establishes a generation mechanism that is robust to noise and interpretable, significantly solving the problems of "illusion" and "blind obedience" in professional code review scenarios for generative large models. Traditional end-to-end generation methods are prone to producing seemingly plausible but ultimately flawed general suggestions, while ordinary search enhancement is easily misled by low-quality search results. This invention, through a "noise-resistant context learning" strategy, embeds explicit expert role constraints and noise filtering instructions into the suggestion process, forcing the large model to maintain critical thinking while utilizing search knowledge. This mechanism ensures that the generated review opinions are based on real historical norms and can automatically filter out inapplicable noise interference, thereby outputting high-quality opinions that are highly targeted, have a low false positive rate, and conform to project-specific specifications.
[0024] Third, this invention achieves efficient collaboration between general large-scale model knowledge and domain-specific fine-tuning strategies, constructing a highly scalable automated review system. This invention does not simply rely on the parameter size of the large model, but rather achieves complementary capabilities through a dual path of "retrieval enhancement" and "LoRa (Locally Automated Retrieval and Retrieval)." The retrieval engine provides an infinitely expandable external knowledge base, addressing the issues of outdated knowledge and lack of specific project context. The fine-tuned large model provides professional reasoning logic and language style, addressing the problem of the general model not understanding professional jargon. This architecture not only avoids the catastrophic risk of forgetting associated with full-scale fine-tuning, but also enables the method to continuously evolve with updates to the historical codebase, providing an agile, customizable, and continuously evolving automated solution for software engineering tasks using different programming languages or project styles. Attached Figure Description
[0025] Figure 1 This invention provides an overall flowchart for a method of automating code review comments generation;
[0026] Figure 2 A flowchart for constructing a contrastive learning dataset is provided by this invention;
[0027] Figure 3 This invention provides a domain-adaptive fine-tuning architecture diagram for a code change retrieval tool.
[0028] Figure 4 This is a schematic diagram illustrating the structure of a search-enhanced structured prompt template provided by the present invention. Detailed Implementation
[0029] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings and specific embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the invention.
[0030] Although the steps in this invention are arranged by numbers, this is not intended to limit the strict order of the steps. Unless the order of the steps is explicitly stated or the execution of a step requires other steps as a basis, the relative order of the steps can be adaptively adjusted according to actual computing resources or system architecture. Furthermore, it is understood that the term "and / or" as used herein refers to and covers any and all possible combinations of one or more of the associated listed items. The overall process steps used in this invention are as follows: Figure 1 As shown.
[0031] Step 1: Construct a comparative learning dataset that integrates structural features of code change and semantic features of censorship intent. The core of this step is to transform the raw, unstructured code change data into structured triples containing positive and negative sample associations, providing the retrieval machine with supervisory signals regarding "censorship intent." The data construction flowchart is shown below. Figure 2 As shown.
[0032] First, the system obtains paired data of real code changes (Code Diff) and review comments covering nine mainstream programming languages, including Java, Python, and C++, from high-quality code review data sources in the field of software engineering (such as the CodeReviewer dataset).
[0033] Next, for each pair of code change instances, a quantitative analysis of their comprehensive consistency across multiple feature dimensions is performed. Specifically, the structural similarity of code changes covering three modules and the semantic feature similarity of review comments are calculated separately. Regarding scale similarity calculation, to address the issue that the distribution of code change line counts often exhibits a long-tailed distribution and significant dispersion, a smoothing normalization strategy based on the natural logarithm function is designed. By performing a logarithmic transformation on the number of changed lines to compress the numerical range, the bias effect of extremely large-scale changes on similarity calculation is eliminated, thereby obtaining robust scale similarity features. Let L... A and L B Let S be the number of changed rows for the two samples, and their scale similarity S. size The calculation formula is:
[0034]
[0035] In calculating type similarity, regular expressions are used to match metadata in code difference blocks to construct Boolean feature vectors representing add, delete, and modify operations, and the Hamming distance between the two vectors is calculated as the consistency score S. typeIn terms of pattern similarity calculation, a pattern matching mechanism based on a predefined keyword set is constructed to address the reusability of specific patterns in code engineering logic. This mechanism detects the co-occurrence of key programming paradigms such as null value checking, exception handling, and resource management in different code samples, and calculates the consistency ratio of Boolean feature vectors to obtain the pattern similarity S. pattern To calculate the semantic similarity of review comments, this invention inputs the review comments into a pre-trained sentence vector model all-MiniLM-L6-v2. After passing through a 6-layer Transformer architecture of the MiniLM model, the output vectors of its pooling layers are extracted as vector representations of the review comments, and the cosine similarity S between the vector representations is calculated. sem .
[0036] Finally, according to the preset weighting formula (such as S) total =0.7×S sem +0.3×(0.3S size +0.3S type +0.4S pattern A comprehensive score is synthesized. Based on this score, a triplet sampling strategy is implemented to sample approximately 20,000 high-quality data points from the historical review database as the base dataset for contrastive learning. For each sample in this dataset, it is used as an anchor point, and the sample with the best comprehensive evaluation result is identified as a similar sample to ensure maximum consistency at the review intent level. Simultaneously, a hard negative sample mining strategy based on dual threshold filtering is adopted to select samples with significant feature differences from those samples with low semantic or structural similarity to the anchor point as negative examples, thereby completing the construction of the contrastive learning triplet training set.
[0037] Step 2: Domain-Adaptive Fine-Tuning of the Code Censor Retrieval Based on the Contrastive Learning Dataset. The core of this step is to input the triplet data constructed in Step 1 into the pre-trained model, and train a retrieval responsive to censorship intent by reshaping the distribution of the vector space. The retrieval fine-tuning architecture is as follows: Figure 3 As shown.
[0038] First, the UniXcoder model, which supports multiple programming languages, is selected as the backbone encoder. Its accompanying tokenizer is used to tokenize the code text in the triples. This process decomposes the code string into a sequence of subwords and performs truncation or padding operations based on a maximum sequence length of 512 to ensure that all input sequences have a uniform length. This tokenization process generates two key normalized input tensors: one representing the tokenized ID sequence, and the other a mask indicating the positions involved in the computation.
[0039] Next, the constructed tensor input encoder undergoes forward propagation. To map general code features to a specific contrast vector space, this invention cascades a linear projection layer at the [CLS] position of the UniXcoder model output, mapping the original 768-dimensional high-dimensional hidden state to a 256-dimensional low-dimensional feature vector and performing L2 normalization. To optimize the distribution characteristics of the vector space, this invention uses a hybrid loss function consisting of triplet loss and InfoNCE loss as the optimization objective. The total loss function is defined as:
[0040] L total =0.5L triplet +0.5L infoNCE ;
[0041] The triplet loss aims to constrain the geometric distance between samples, calculated based on the Euclidean distance difference between samples, with a preset boundary threshold of 0.5. The InfoNCE loss aims to optimize the normalized similarity score of positive samples, forcing the model to significantly improve the recognition probability and ranking position of positive samples even when faced with multiple negative sample interferences. It is calculated based on the cosine similarity and cross-entropy between anchor points and samples, and a temperature coefficient of 0.07 is introduced for scaling. During model training, the AdamW optimizer is configured with a weight decay coefficient of 0.01, combined with a cosine annealing learning rate scheduling strategy. The backpropagation algorithm minimizes the above total loss, thereby updating the parameters of the encoder and projection layer. This forces the retrieval model to narrow the distance between codes with similar censorship intentions in the vector space, while widening the distance between codes that only have superficial syntactic similarity but different intentions.
[0042] Step 3: Contextual Retrieval and Suggestion Construction Based on the Fine-tuned Retrieval Tool. The core of this step is to utilize the fine-tuned retrieval tool to recall high-value historical experience and assemble it into noise-resistant, expert-level instructions. The suggestion template structure is as follows: Figure 4 As shown.
[0043] First, for the target code change, the code change text is mapped to a feature vector with implicit review intent by the retrieval tool fine-tuned in step 2, and the cosine similarity between the vector and the case vector in the code review library is calculated. The K historical code changes with the highest similarity and their review comments are recalled as reference context.
[0044] Next, the assembly process of the structured prompt template is executed. To guide the performance of the large model on a specific task, this invention designs an input template containing three logical levels: the first is the system instruction layer, which fills in explicit expert role setting instructions to guide the model into the reasoning mode of a senior reviewer; the second is the context injection layer, which formats the recalled Top-K historical cases into text blocks and embeds them into the template. In particular, this invention introduces explicit negative constraint instructions (such as "WARNING: Ignore irrelevant examples...") here to establish a noise filtering mechanism; the third is the user input layer, which concatenates the changes to the target code to be reviewed. These three levels together constitute a robust model input sequence with rich context.
[0045] Step 4: Fine-tuning of large model instructions based on Low-Rank Adaptive (LoRA). The core of this step is to efficiently fine-tune the parameters based on the long contextual hints (Prompt) constructed in Step 3, effectively giving the model code a professional language style and inference logic.
[0046] This invention uses the Llama 3.1-8B model as the generation base. Because step 3 introduces Top-K historical code change cases and corresponding expert instructions, the length of the input model's prompt sequence increases significantly. Full fine-tuning would require high computational resources, and the large number of intermediate activation states could easily lead to memory overflow (OOM) or extremely low training efficiency. Therefore, considering the characteristics of long text input in RAG scenarios, this invention employs a low-rank adapter technique in the fine-tuning stage to efficiently update model parameters. Customized low-rank dimensions and scaling factors are used to ensure the stability of the fine-tuning process and the final performance.
[0047] First, considering the high-density reference information retrieved in the input sequence, the rank of the low-rank matrix is set to a small value of 8 to preserve gradient flow over long contexts within a limited GPU memory budget. This low-rank setting significantly compresses the number of trainable parameters, leaving valuable GPU memory for handling longer RAG contexts. Second, to ensure the model can effectively capture subtle features of code review even with a very small number of parameters, the scaling factor α is set to 16, with a scaling ratio of 2:1, thereby enhancing the update weights for specific tasks. Furthermore, given that the retrieval context in the input is carefully selected high-value information, to avoid random feature loss affecting the model's learning of reference cases, the dropout rate of the LoRA layer is set to 0, ensuring that all contextual features participate in forward propagation and gradient calculation during training.
[0048] During training, the Prompt constructed in step 3 is concatenated with the actual review comments as training samples, and training is performed using the next word prediction task. When calculating the cross-entropy loss, a partitioned loss calculation strategy is applied, calculating only the loss gradient of the "review comments" part generated by the model, while setting the loss of the "Prompt input" part to zero. In this way, the system only needs to update a very small number of low-rank parameters to achieve deep adaptation of the model to a specific review task, significantly reducing memory usage and computational costs while ensuring the professionalism and accuracy of the generated opinions.
[0049] Finally, using the finely tuned model described above, inference and generation are achieved, and the model can be deployed for actual code review tasks. The specific process is as follows: Figure 1 The reasoning application module (number ⑤) first recalls historically relevant cases following the mechanism in step 3, then constructs a fault-tolerant, enhanced retrieval suggestion. This suggestion is then imported into the generative model, optimized in step 4, to obtain the output. The generative model generates the final code review opinion based on learned domain review logic and real-time injected reference knowledge. This output not only better aligns with human expert standards in terms of language style but also provides more constructive modification suggestions for specific logical vulnerabilities in the current code, combining retrieved historical experience, thus achieving an improvement from "general-purpose language generation" to "expert-level assisted review."
Claims
1. A method for automatically generating code review comments based on enhanced retrieval, characterized in that, include: Step 1: Construct a comparative learning dataset that integrates code change structural features and censorship intent semantic features. This dataset is used to guide the retrieval machine to reconstruct a similarity metric based on censorship intent. Step 2: Use the contrastive learning dataset to drive the general pre-trained code encoder to perform transfer learning in the code review domain, and build a code retrieval machine based on review intent awareness; Step 3: Based on the fine-tuned code searcher, perform a similarity search on the code changes to be reviewed, obtain the Top-K most valuable historical code changes and their review comments as reference context, and build a structured prompt template; Step 4: Combining the predefined hint framework, use low-rank adaptive (LoRA) technology to perform targeted optimization on the basic large language model and generate the final code review comments.
2. The method for automatically generating code review comments based on enhanced retrieval according to claim 1, characterized in that, Step 1 involves constructing a comparative learning dataset that integrates code change structural features and censorship intent semantic features. This dataset guides the retrieval system to reconstruct a similarity metric based on censorship intent. Specifically, this includes: First, historical code changes and their corresponding review comments are acquired. The structural similarity of the code changes and the semantic similarity of the review comments are calculated separately. Specifically, addressing the difficulty of existing methods in capturing human review intent, semantic features of the review comments are extracted. A pre-trained sentence vector model is used to map unstructured natural language comments into high-dimensional vectors, and cosine similarity is calculated to quantify the inherent consistency of historical review cases in terms of thought logic and focus. Simultaneously, the structural similarity of the code changes is calculated. Considering the diversity of code change features, scale similarity based on log-normalization is used to eliminate extreme numerical bias, type similarity based on Boolean consistency is used to capture operational properties, and pattern similarity based on keyword matching is used to identify specific programming paradigms. Finally, a weighted strategy is used to fuse high-weight semantic similarity with auxiliary structural similarity. A hard negative sample mining strategy based on double threshold filtering is used to select samples from the remaining samples that differ significantly from the anchor points semantically or structurally. This constructs a contrastive learning triplet training data containing high-discrimination anchor point samples, positive samples, and negative samples.
3. The method for automatically generating code review comments based on enhanced retrieval according to claim 1, characterized in that, Step 2 utilizes a contrastive learning dataset to perform domain-adaptive fine-tuning on the pre-trained code encoder, constructing a code retrieval tool based on censorship intent awareness. Specifically, this includes: The UniXcoder pre-trained model, which supports multiple programming languages, was selected as the backbone encoder. To map general code features to a specific contrast measurement space, a linear projection layer was cascaded at the encoder output to map high-dimensional hidden states into low-dimensional feature vectors and perform L2 normalization. During model training, a hybrid contrast optimization objective consisting of triplet loss and InfoNCE loss was constructed. Specifically, the triplet loss function was used to optimize the geometric distance between samples, forcing the model to bring anchor points closer to positive samples and push anchor points further away from negative samples in the vector space. At the same time, to meet the requirement of accurately identifying target cases from a large number of candidates in the retrieval task, the InfoNCE loss function was used to optimize the normalized similarity score of positive samples, forcing the model to significantly improve the recognition probability and ranking position of positive samples even when faced with multiple negative sample interferences. Through this hybrid supervision mechanism, the retrieval machine not only has various measurement capabilities in geometric space, but also robust discrimination capabilities in complex retrieval scenarios, forcing the model to go beyond surface lexical matching and deeply learn the deep semantic relationship between code changes and censorship intent.
4. The method for automatically generating code review comments based on enhanced retrieval, as described in claim 1, is characterized in that... Step 3, based on the fine-tuned code retrieval tool, performs semantic retrieval on the code changes to be reviewed, obtains the Top-K most similar historical code changes and their review comments as reference context, and constructs a structured prompt template, specifically including: A finely tuned retrieval tool is used to vectorize changes to the code to be reviewed, and historical cases are retrieved using cosine similarity between vectors. To guide the large model into a domain-specific reasoning mode and overcome the illusion problem caused by retrieval noise, a three-level structured template is constructed, comprising a system instruction layer, a context injection layer, and a user input layer. In the system instruction layer, role alignment instructions for expert code reviewers are set to activate the model's prior knowledge in the code review domain. In the context injection layer, retrieved historical reference cases are embedded, and explicit negative constraint instructions and noise filtering mechanisms are configured. Natural language instructions force the model to maintain critical thinking when referencing historical cases, automatically ignoring retrieval results that do not conform to the current change logic, thereby ensuring the accuracy of the generated basis.
5. The method for automatically generating code review comments based on search enhancement according to claim 1, characterized in that, Step 4 uses low-rank adaptive techniques to fine-tune the instructions of the large language model, specifically including: A LoRA parameter fine-tuning strategy is employed to freeze the pre-trained weights of all Transformer layers in the large language model, injecting trainable low-rank matrices only as bypasses in the query projection and value projection layers of the self-attention module. Addressing the high demands on memory usage and feature transfer from long contextual inputs in retrieval enhancement scenarios, a deterministic low-rank mapping configuration strategy is adopted. The rank of the low-rank matrix is set to 8 to compress the trainable parameter space, reserving memory resources for gradient calculations in long prompts. The scaling factor is set to 16 to enhance the update weights for specific tasks, and the dropout rate is set to 0. This aims to construct a deterministic feature transfer path, preventing the loss of high-value retrieval contextual features during random dropout and ensuring the model fully utilizes the retrieved reference information. During training, a partitioned loss calculation strategy is used, setting the loss of the prompt template part to zero and only calculating the cross-entropy loss of the review comments generated by the model. This guides the model to focus on learning how to generate review comments that conform to expert standards based on the given context.