Code review method based on RAG

Through the RAG-based code review method, the code review process is automated and structured opinions are automatically generated using large language models, which solves the problem that static code analysis tools cannot cover logical defects, improves the efficiency and comprehensiveness of code review, and saves human resources.

CN120540962APending Publication Date: 2025-08-26JIANGSU SUYUN INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510658030.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-21
Publication Date
2025-08-26

AI Technical Summary

Technical Problem

In the prior art, static code analysis tools cannot cover logical defects or provide context-related improvement suggestions, resulting in code reviews that rely on manual and inefficient, and have repetitive problems.

Method used

The RAG-based code review method is adopted to automatically generate review opinions through historical data accumulation, code chunking and vectorization, similar code search and large-model generation review opinions, combined with feedback optimization mechanism, and automatically generate structured opinions by using large language models.

Benefits of technology

It realizes automated code review, improves the comprehensiveness and efficiency of code problem discovery, reduces manual negligence, and saves human resources.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120540962A_ABST
    Figure CN120540962A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of code review, and discloses a code review method based on RAG, comprising the following steps: S1, historical data accumulation: querying a problem code segment and an artificial code review opinion through a query interface of a code review system, then, code snippets generated in the manual review stage and data of review opinions corresponding to the code snippets are obtained through an automatic code review calling interface, and the code snippets and the data are stored in a vector database; according to the method, the code problem is accurately found by utilizing the capabilities of the RAG and the large model, the code review opinions are automatically generated, all code review work which needs to be manually carried out originally can be automated, manpower is saved, a very strong code problem finding capability can be formed, and the code review efficiency is improved. And the problem finding capability of the original single code review personnel is exceeded, so that the code problem can be found more comprehensively, and the problem of missing due to negligence during manual code review can be effectively reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code review, and in particular to a code review method based on RAG. Background Art

[0002] Currently, during the software development process, code submitted by developers undergoes manual online review. Code reviewers read the code online and add comments to problematic lines of code, including questions, issues, and suggested changes. The code submitter then modifies and improves the code based on these comments. This code review process is highly manual, and the code defects discovered are highly repetitive, resulting in redundant and wasted manpower. Furthermore, manual review is more likely to miss issues, making manual review inefficient.

[0003] Existing static code analysis tools can detect some syntactic errors, but they cannot cover logical flaws or provide context-sensitive improvement suggestions. Therefore, there is an urgent need for an automated code review solution that combines historical experience with intelligent analysis. Summary of the Invention

[0004] (1) Technical problems solved

[0005] In response to the shortcomings of the existing technology, the present invention provides a RAG-based code review method, which mainly solves the problem that static code analysis tools in the existing technology can detect some syntax errors but cannot cover logical defects or provide context-related improvement suggestions.

[0006] (2) Technical solution

[0007] To achieve the above object, the present invention provides the following technical solutions:

[0008] A code review method based on RAG includes the following steps:

[0009] S1: Historical data accumulation: query problematic code segments and manual code review comments through the code review system's query interface. Then, the automatic code review call interface is used to obtain the code segments generated during the manual review phase and their corresponding review comments, and store them in the vector database.

[0010] S2: Code submission trigger: When a developer submits new code to the code repository, the review process is automatically triggered;

[0011] S3: Code segmentation and vectorization: splits the submitted code into logical blocks and converts them into vectors through the embedding model;

[0012] S4: Similar code retrieval, using code vectors as search parameters, searches for semantically similar code snippets and historical review comments in the vector database;

[0013] S5: The large model generates review comments. The retrieved historical review comments and code context are input into the large language model to generate structured review comments based on the preset template.

[0014] S3: Feedback optimization mechanism. The developer's acceptance or rejection of automatic review opinions can be fed back to the system. The feedback optimization mechanism dynamically adjusts the vector database retrieval weight and large model generation strategy based on the developer's acceptance or rejection of automatic review opinions, which is used to optimize the vector database weight and large model generation logic.

[0015] Furthermore, the review comments in S1 include problem descriptions and modification suggestions, and each piece of data in S1 includes a vectorized representation of a code snippet, the original code text, and associated review comment text and metadata.

[0016] Based on the above solution, the vectorized representation of the code snippet is generated by embedding the code semantics into the CodeBert model, and the metadata includes the problem type, code language and severity level.

[0017] As a further solution of the present invention, the determination method for dividing the submitted code into logical blocks in S2 is: any one of functions or classes.

[0018] Furthermore, the large language model in S2 is any one of qwen, DeepSeek or Llama.

[0019] Based on the above scheme, the specific process of the feedback optimization mechanism in S3 is as follows: (1) feedback data collection and annotation, recording the developer's operation on each review opinion and attaching reasons when rejecting, and then associating the feedback results with the original review opinions, code snippets, and retrieved historical cases for storage to build a labeled training data set; (2) vector database weight optimization, if the review opinions of a certain type of code snippet are frequently adopted, the weight of its corresponding vector during retrieval is increased; if a certain type of opinion is rejected, the language feature weight in its similarity calculation is reduced, and then based on the feedback data, the historical code snippet vectors in the vector database are fine-tuned, and finally, according to the feedback distribution, the index stratification strategy of the vector database is adjusted.

[0020] As a further solution of the present invention, the operation of the review opinion is: any one of adoption, rejection or adoption after modification, and the method for fine-tuning the historical code fragment vectors in the vector database is: using contrastive learning to shorten the vector distance of the adopted cases and to distance the rejected cases.

[0021] Furthermore, the index stratification strategy of the adjustment vector database adopts the hierarchical division in the HNSW algorithm.

[0022] (3) Beneficial effects

[0023] Compared with the prior art, the present invention provides a code review method based on RAG, which has the following beneficial effects:

[0024] 1. The present invention uses the capabilities of RAG and large models to more accurately discover code problems through automatic code review, and automatically generates code review opinions. It can also automate the code review work that originally required manual work, saving manpower.

[0025] 2. By gradually accumulating the code review opinions of code reviewers in an organization into a vector database, the present invention can form a very powerful code problem discovery capability, which exceeds the problem discovery capability of the original single code reviewer, thereby enabling more comprehensive discovery of code problems.

[0026] 3. The automated approach of the present invention can also effectively reduce problems that may be overlooked during manual code review, and the automated approach can significantly improve the efficiency and throughput of code review. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] Figure 1 A schematic diagram of the process structure of a RAG-based code review method proposed by the present invention;

[0028] Figure 2 This is a system flow diagram of a RAG-based code review method proposed by the present invention. DETAILED DESCRIPTION

[0029] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.

[0030] Example 1

[0031] Reference Figure 1-Figure 2 , a code review method based on RAG, including the following steps:

[0032] S1: Historical data accumulation: Use the code review system's query interface to query problematic code snippets and manual code review comments. Then, use the automated code review call interface to retrieve the code snippets and their corresponding review comments generated during the manual review phase and store them in a vector database. By gradually accumulating the code review comments of code reviewers in the organization into the vector database, a powerful code problem discovery capability is formed, surpassing the original problem discovery capabilities of a single code reviewer, enabling more comprehensive code problem discovery.

[0033] S2: Code submission trigger: When a developer submits new code to the code repository, the review process is automatically triggered;

[0034] S3: Code segmentation and vectorization: splits the submitted code into logical blocks and converts them into vectors through the embedding model;

[0035] S4: Similar code retrieval, using code vectors as search parameters, searches for semantically similar code snippets and historical review comments in the vector database;

[0036] S5: The large model generates review comments. The retrieved historical review comments and code context are input into the large language model to generate structured review comments based on the preset template.

[0037] S6: Feedback optimization mechanism. The developer's acceptance or rejection of automatic review opinions can be fed back to the system. The feedback optimization mechanism dynamically adjusts the vector database retrieval weight and large model generation strategy based on the developer's acceptance or rejection of automatic review opinions. It is used to optimize the vector database weight and large model generation logic. Through automatic code review, the capabilities of RAG and large models are used to more accurately discover code problems and automatically generate code review opinions. It can also automate all code review work that originally required manual work, saving manpower.

[0038] In the present invention, the review comments in S1 include problem descriptions and modification suggestions. Each data content in S1 includes a vectorized representation of the code snippet, the original code text, and the associated review comment text and metadata. The vectorized representation of the code snippet is generated by embedding the code semantics into the CodeBert model. The metadata includes the problem type, code language, and severity level. The judgment method for dividing the submitted code into logical blocks in S2 is: function, and the large language model in S2 is: Llama.

[0039] In particular, the specific process of the feedback optimization mechanism in S3 is as follows: (1) feedback data collection and annotation, recording the developer's operation on each review opinion, and adding reasons when rejecting, such as "false alarm" and "suggestion not applicable", and then associating the feedback results with the original review opinions, code snippets, and retrieved historical cases for storage to build a labeled training data set; (2) vector database weight optimization, if the review opinions of a certain type of code snippet are frequently adopted, then the weight of its corresponding vector during retrieval is increased, such as through weighted cosine similarity; if a certain type of opinion is rejected, then the language feature weight in its similarity calculation is reduced, such as due to code language mismatch, and then based on feedback data, such as the high frequency of certain problem types, the historical code snippet vectors in the vector database are fine-tuned, and finally, according to the feedback distribution, the index stratification strategy of the vector database is adjusted. The operation of the review opinion is: adopted after modification, and the method of fine-tuning the historical code snippet vectors in the vector database is: using contrastive learning to shorten the vector distance of adopted cases and push away rejected cases, and adjusting the index stratification strategy of the vector database adopts the hierarchical division in the HNSW algorithm.

[0040] Example 2

[0041] Reference Figure 1-Figure 2 , a code review method based on RAG, including the following steps:

[0042] S1: Historical data accumulation: Use the code review system's query interface to query problematic code snippets and manual code review comments. Then, use the automated code review call interface to retrieve the code snippets and their corresponding review comments generated during the manual review phase and store them in a vector database. By gradually accumulating the code review comments of code reviewers in the organization into the vector database, a powerful code problem discovery capability is formed, surpassing the original problem discovery capabilities of a single code reviewer, enabling more comprehensive code problem discovery.

[0043] S2: Code submission trigger: When a developer submits new code to the code repository, the review process is automatically triggered;

[0044] S3: Code segmentation and vectorization: splits the submitted code into logical blocks and converts them into vectors through the embedding model;

[0045] S4: Similar code retrieval, using code vectors as search parameters, searches for semantically similar code snippets and historical review comments in the vector database;

[0046] S5: The large model generates review comments. The retrieved historical review comments and code context are input into the large language model to generate structured review comments based on the preset template.

[0047] S6: Feedback optimization mechanism. The developer's acceptance or rejection of automatic review opinions can be fed back to the system. The feedback optimization mechanism dynamically adjusts the vector database retrieval weight and large model generation strategy based on the developer's acceptance or rejection of automatic review opinions. It is used to optimize the vector database weight and large model generation logic. Through automatic code review, the capabilities of RAG and large models are used to more accurately discover code problems and automatically generate code review opinions. It can also automate all code review work that originally required manual work, saving manpower.

[0048] In the present invention, the review comments in S1 include problem descriptions and modification suggestions. Each data content in S1 includes a vectorized representation of the code snippet, the original code text, and the associated review comment text and metadata. The vectorized representation of the code snippet is generated by embedding the code semantics into the CodeBert model. The metadata includes the problem type, code language, and severity level. The judgment method for dividing the submitted code into logical blocks in S2 is: class, and the large language model in S2 is: DeepSeek.

[0049] In particular, the specific process of the feedback optimization mechanism in S3 is as follows: (1) feedback data collection and annotation, recording the developer's operation on each review opinion, and adding reasons when rejecting, such as "false alarm" and "suggestion not applicable", and then associating the feedback results with the original review opinions, code snippets, and retrieved historical cases for storage to build a labeled training data set; (2) vector database weight optimization, if the review opinions of a certain type of code snippet are frequently adopted, then the weight of its corresponding vector during retrieval is increased, such as through weighted cosine similarity; if a certain type of opinion is rejected, then the language feature weight in its similarity calculation is reduced, such as due to code language mismatch, and then based on feedback data, such as the high frequency of certain problem types, the historical code snippet vectors in the vector database are fine-tuned, and finally, according to the feedback distribution, the index stratification strategy of the vector database is adjusted. The operation of the review opinion is: rejection, and the method of fine-tuning the historical code snippet vectors in the vector database is: using contrastive learning to shorten the vector distance of adopted cases and push away the rejected cases, and adjusting the index stratification strategy of the vector database adopts the hierarchical division in the HNSW algorithm.

[0050] Example 3

[0051] Reference Figure 1-Figure 2 , a code review method based on RAG, including the following steps:

[0052] S1: Historical data accumulation: Use the code review system's query interface to query problematic code snippets and manual code review comments. Then, use the automated code review call interface to retrieve the code snippets and their corresponding review comments generated during the manual review phase and store them in a vector database. By gradually accumulating the code review comments of code reviewers in the organization into the vector database, a powerful code problem discovery capability is formed, surpassing the original problem discovery capabilities of a single code reviewer, enabling more comprehensive code problem discovery.

[0053] S2: Code submission trigger: When a developer submits new code to the code repository, the review process is automatically triggered;

[0054] S3: Code segmentation and vectorization: splits the submitted code into logical blocks and converts them into vectors through the embedding model;

[0055] S4: Similar code retrieval, using code vectors as search parameters, searches for semantically similar code snippets and historical review comments in the vector database;

[0056] S5: The large model generates review comments. The retrieved historical review comments and code context are input into the large language model to generate structured review comments based on the preset template.

[0057] S6: Feedback optimization mechanism. The developer's acceptance or rejection of automatic review opinions can be fed back to the system. The feedback optimization mechanism dynamically adjusts the vector database retrieval weight and large model generation strategy based on the developer's acceptance or rejection of automatic review opinions. It is used to optimize the vector database weight and large model generation logic. Through automatic code review, the capabilities of RAG and large models are used to more accurately discover code problems and automatically generate code review opinions. It can also automate all code review work that originally required manual work, saving manpower.

[0058] In the present invention, the review comments in S1 include problem descriptions and modification suggestions. Each data content in S1 includes a vectorized representation of the code snippet, the original code text, and the associated review comment text and metadata. The vectorized representation of the code snippet is generated by embedding the code semantics into the CodeBert model. The metadata includes the problem type, code language, and severity level. The judgment method for dividing the submitted code into logical blocks in S2 is: function, and the large language model in S2 is: qwen.

[0059] In particular, the specific process of the feedback optimization mechanism in S3 is as follows: (1) feedback data collection and annotation, recording the developer's operation on each review opinion, and adding reasons when rejecting, such as "false alarm" and "suggestion not applicable", and then associating the feedback results with the original review opinions, code snippets, and retrieved historical cases for storage to build a labeled training data set; (2) vector database weight optimization, if the review opinions of a certain type of code snippet are frequently adopted, then the weight of its corresponding vector during retrieval is increased, such as through weighted cosine similarity; if a certain type of opinion is rejected, then the language feature weight in its similarity calculation is reduced, such as due to code language mismatch, and then based on feedback data, such as the high frequency of certain problem types, the historical code snippet vectors in the vector database are fine-tuned, and finally, according to the feedback distribution, the index stratification strategy of the vector database is adjusted. The operation of the review opinion is: adoption, and the method of fine-tuning the historical code snippet vectors in the vector database is: using contrastive learning to shorten the vector distance of adopted cases and push away rejected cases, and the index stratification strategy of the vector database is adjusted using the hierarchical division in the HNSW algorithm.

[0060] In the description herein, it should be noted that relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Furthermore, the terms "include," "comprise," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, article, or apparatus that includes a list of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, article, or apparatus.

Claims

1. A code review method based on RAG, characterized in that: The following steps are involved: S1: Historical data accumulation: query problematic code segments and manual code review comments through the code review system's query interface. Then, the automatic code review call interface is used to obtain the code segments generated during the manual review phase and their corresponding review comments, and store them in the vector database. S2: Code submission trigger: When a developer submits new code to the code repository, the review process is automatically triggered; S3: Code segmentation and vectorization: splits the submitted code into logical blocks and converts them into vectors through the embedding model; S4: Similar code retrieval, using code vectors as search parameters, searches for semantically similar code snippets and historical review comments in the vector database; S5: The large model generates review comments. The retrieved historical review comments and code context are input into the large language model to generate structured review comments based on the preset template. S6: Feedback optimization mechanism. The developer's acceptance or rejection of automatic review opinions can be fed back to the system. The feedback optimization mechanism dynamically adjusts the vector database retrieval weight and large model generation strategy based on the developer's acceptance or rejection of automatic review opinions, which is used to optimize the vector database weight and large model generation logic.

2. A code review method based on RAG according to claim 1, characterized in that: The review comments in S1 include problem descriptions and modification suggestions, and each piece of data in S1 includes a vectorized representation of a code snippet, the original code text, and associated review comment text and metadata.

3. A code review method based on RAG according to claim 2, characterized in that: The vectorized representation of the code snippet is generated by embedding the code semantics into the CodeBert model, and the metadata includes the problem type, code language and severity level.

4. The RAG-based code review method according to claim 1, characterized in that: The determination method for dividing the submitted code into logical blocks in S2 is: any one of functions or classes.

5. The RAG-based code review method according to claim 4, characterized in that: The large language model in S2 is any one of qwen, DeepSeek or Llama.

6. The RAG-based code review method according to claim 1, characterized in that: The specific process of the feedback optimization mechanism in S3 is as follows: (1) feedback data collection and annotation, recording the developer's operation on each review opinion and attaching reasons when rejecting, and then associating the feedback results with the original review opinions, code snippets, and retrieved historical cases for storage to build a labeled training data set; (2) vector database weight optimization, if the review opinions of a certain type of code snippet are frequently adopted, then the weight of its corresponding vector during retrieval is increased; if a certain type of opinion is rejected, then the language feature weight in its similarity calculation is reduced, and then based on the feedback data, the historical code snippet vectors in the vector database are fine-tuned, and finally, according to the feedback distribution, the index stratification strategy of the vector database is adjusted.

7. The RAG-based code review method according to claim 6, characterized in that: The operation of the review opinion is: any one of adoption, rejection or adoption after modification. The method for fine-tuning the historical code fragment vectors in the vector database is: using contrastive learning to shorten the vector distance of the adopted cases and distance the rejected cases.

8. The RAG-based code review method according to claim 7, characterized in that: The index stratification strategy for adjusting the vector database adopts the hierarchical division in the HNSW algorithm.