Code review model training and code review method, device and equipment

By adaptively adjusting and expanding the model's search scope and obtaining multiple different context information, the problem that the existing RAG model fails to fully interpret and analyze the dependency library functions, and improves the ability to understand complex codes and the efficiency of code review.

CN119669034APending Publication Date: 2025-03-21BEIJING QIYI CENTURY SCI & TECH CO LTD
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202411801401.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-12-09
Publication Date
2025-03-21

AI Technical Summary

Technical Problem

The existing code review model based on the Retrieval Enhanced Generative Model (RAG) fails to fully interpret and analyze the complete functions and behavior of the dependent libraries, which affects the integrity of code comprehension.

Method used

By adaptively adjusting and extending the model's search scope, obtaining multiple different context information, so that the model can more comprehensively capture and process multi-level dependencies of the code, and enhance the model's context understanding ability.

Benefits of technology

It improves the model's understanding of complex code and enhances the efficiency and accuracy of code review.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119669034A_ABST
    Figure CN119669034A_ABST
Patent Text Reader

Abstract

The invention provides a code review model training method, device and equipment, and a code review method, device and equipment, and the code review model training method comprises the steps: obtaining a training sample which comprises a sample code and review information of the sample code; a retrieval sub-model in the pre-training code review model is utilized to retrieve the sample code, multiple pieces of context information are obtained, and different pieces of context information correspond to different retrieval ranges; combining the plurality of pieces of context information with the sample code to obtain a plurality of pieces of prompt information; generating initial review information of the multiple pieces of prompt information by utilizing a generation sub-model in the pre-training code review model; selecting output review information from the multiple pieces of initial review information; and based on the difference between the output review information and the review information of the sample code, optimizing model parameters of the pre-trained code review model to obtain a trained code review model. The context understanding capability of the model can be enhanced, and the understanding of the model on complex codes is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a code review model training and code review method, device and equipment. Background Art

[0002] Code review is an important part of the software development process. It aims to ensure code quality, reduce errors and ensure that the code meets the expected design specifications through code inspection and review.

[0003] The code review process needs to consider multiple aspects such as code logic, style, specifications, and performance. In order to improve the efficiency and accuracy of code review, a method based on the Retrieval-Augmented Generation (RAG) model is introduced in the relevant technology. The main process includes: using the retrieval system to retrieve the code to be reviewed, obtaining the retrieval results, generating prompt words with the retrieval results and sample codes, and generating review information of the prompt words according to the generation model.

[0004] However, existing RAG models fail to fully interpret and analyze the complete functions and behaviors of the dependent libraries, affecting the integrity of the overall code understanding. Summary of the invention

[0005] In order to solve the above technical problems, the present application provides a code review model training and code review method, device, equipment and storage medium, which obtains multiple different context information by adaptively adjusting and expanding the retrieval scope of the model, so that the model can more comprehensively capture and process the multi-level dependencies of the code, enhance the model's contextual understanding ability, and improve the model's understanding of complex code.

[0006] In a first aspect, the present application provides a method for training a code review model, the method comprising: obtaining a training sample, the training sample comprising a sample code and actual review information of the sample code; using a retrieval sub-model in a pre-trained code review model to retrieve the sample code to obtain multiple context information of the sample code, wherein different context information corresponds to different retrieval ranges, and the context information comprises context code related to the sample code and annotation information of the context code; combining the multiple context information of the sample code with the sample code respectively to obtain multiple prompt information of the sample code; using a generation sub-model in the pre-trained code review model to generate initial review information of the multiple prompt information of the sample code; selecting predicted review information of the sample code from the multiple initial review information of the sample code; optimizing the model parameters of the pre-trained code review model based on the difference between the predicted review information of the sample code and the actual review information of the sample code to obtain a trained code review model.

[0007] In a second aspect, the present application provides a code review method, the method comprising: obtaining the code to be reviewed; inputting the code to be reviewed into a trained code review model, and using the code review model to review the code to be reviewed, the code review model being trained by the training method of the code review model described in the first aspect above; receiving target review information output by the code review model.

[0008] In a third aspect, the present application provides a training device for a code review model, the device comprising: a sample acquisition module, used to acquire training samples, the training samples comprising sample code and actual review information of the sample code; a retrieval module, used to retrieve the sample code using a retrieval sub-model in a pre-trained code review model, and obtain multiple context information of the sample code, wherein different context information corresponds to different retrieval ranges, and the context information comprises context code related to the sample code and annotation information of the context code; a prompt information generation module, used to combine the multiple context information of the sample code with the sample code respectively, and obtain multiple prompt information of the sample code; an initial review information generation module, used to generate initial review information of multiple prompt information of the sample code using a generation sub-model in the pre-trained code review model; a predicted review information selection module, used to select predicted review information of the sample code from multiple initial review information of the sample code; a model parameter optimization module, used to optimize the model parameters of the pre-trained code review model based on the difference between the predicted review information of the sample code and the actual review information of the sample code, and obtain a trained code review model.

[0009] In a fourth aspect, the present application provides a code review device, which includes: a code acquisition module, used to acquire the code to be reviewed; a code review module, used to input the code to be reviewed into a trained code review model, and use the code review model to review the code to be reviewed, the code review model is trained by the training method of the code review model in the first aspect; a target review information receiving module, used to receive the target review information output by the code review model.

[0010] In a fifth aspect, the present application provides an electronic device comprising: one or more processors; a storage device for storing one or more programs; when the one or more programs are executed by one or more processors, the one or more processors implement the training method of the code review model as in the first aspect above, or implement the code review method as in the second aspect above.

[0011] In a sixth aspect, the present application provides a storage medium, which may be a computer-readable storage medium on which a computer program is stored, and when the program is executed by a processor, the program implements the training method of the code review model as in the first aspect above, or implements the code review method as in the second aspect above.

[0012] In a seventh aspect, an embodiment of the present application provides a computer program product, which includes a computer program or instructions, which, when executed by a processor, implements the training method of the code review model as in the first aspect above, or implements the code review method as in the second aspect above.

[0013] Compared with the prior art, the technical solution provided by the embodiments of the present application has the following advantages:

[0014] The embodiments of the present application provide a code review model training and code review method, apparatus, device and storage medium, wherein the code review model training method comprises: obtaining training samples, wherein the training samples comprise sample codes and actual review information of the sample codes; using a retrieval sub-model in a pre-trained code review model to retrieve the sample codes to obtain multiple context information of the sample codes, wherein different context information corresponds to different retrieval ranges, and the context information comprises context codes related to the sample codes and annotation information of the context codes; combining the multiple context information of the sample codes with the sample codes respectively to obtain multiple prompt information of the sample codes; using a generation sub-model in the pre-trained code review model to generate initial review information of the multiple prompt information of the sample codes; selecting predicted review information of the sample codes from the multiple initial review information of the sample codes; optimizing the model parameters of the pre-trained code review model based on the difference between the predicted review information of the sample codes and the actual review information of the sample codes to obtain a trained code review model.

[0015] By adaptively adjusting and expanding the retrieval scope of the model, multiple different contextual information is obtained, so that the model can more comprehensively capture and process the multi-level dependencies of the code, enhance the model's contextual understanding ability, and improve the model's understanding of complex code. BRIEF DESCRIPTION OF THE DRAWINGS

[0016] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0017] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0018] Figure 1 A flowchart of a method for training a code review model provided in an embodiment of the present application;

[0019] Figure 2 A schematic diagram of a process for obtaining training samples provided in an embodiment of the present application;

[0020] Figure 3 A flowchart of a code review method provided in an embodiment of the present application;

[0021] Figure 4 A flowchart of another code review method provided in an embodiment of the present application;

[0022] Figure 5 A schematic diagram of the structure of a training device for a code review model provided in an embodiment of the present application;

[0023] Figure 6 A schematic diagram of the structure of a code review device provided in an embodiment of the present application;

[0024] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0025] In order to more clearly understand the above-mentioned purposes, features and advantages of the present application, the scheme of the present application will be further described below. It should be noted that the embodiments of the present application and the features in the embodiments can be combined with each other without conflict.

[0026] In the following description, many specific details are set forth to facilitate a full understanding of the present application, but the present application may also be implemented in other ways different from those described herein; obviously, the embodiments in the specification are only part of the embodiments of the present application, rather than all of the embodiments.

[0027] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.

[0028] It should be noted that the concepts such as "first" and "second" mentioned in this application are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.

[0029] It should be noted that the modifications of "one" and "plurality" mentioned in the present application are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".

[0030] The code review method and model training method provided by the present application are described in detail below in conjunction with the accompanying drawings and specific implementation methods.

[0031] Figure 1 This is a flowchart of a method for training a code review model in an embodiment of the present application. This embodiment is applicable to the case of model training. The method can be executed by a training device for a code review model. The code review device can be implemented in software and / or hardware. The training device for the code review model can be configured in an electronic device.

[0032] like Figure 1 As shown, the training method of the code review model provided in the embodiment of the present application mainly includes steps S101-S106.

[0033] S101. Obtain training samples, where the training samples include sample codes and actual review information of the sample codes.

[0034] The training sample refers to a data pair consisting of a sample code and the actual review information of the sample code, wherein the sample code refers to the code used to train the code review model. The actual review information of the sample code is mainly the information recorded by the code review experts during the review of the sample code. The actual review information of the sample code mainly includes the quality of the sample code, the defect information of the sample code, the performance of the sample code, the optimization suggestions for the sample code, etc.

[0035] Specifically, the quality of the sample code can be expressed as excellent, needs to be improved, or poor. Among them, excellent, needs to be improved, and poor indicate that the code quality decreases in sequence. The defect information of the sample code mainly refers to the bugs existing in the sample code, such as: logical errors, syntax errors, security vulnerabilities, etc. The optimization suggestions for the sample code refer to the optimization or improvement suggestions for the code to improve the quality and maintainability of the code; for example, the modification suggestions may include: deleting duplicate code; simplifying logic, avoiding deep nesting, etc.

[0036] Acquiring a training sample includes: acquiring a code snippet from a code base as a sample code, and acquiring review information of the code snippet from the code base as actual review information of the sample code.

[0037] The embodiment of the present application further optimizes the step of "obtaining training samples", such as Figure 2 As shown, obtain training samples, including:

[0038] S201. Obtain a code instance from a code library.

[0039] Code examples mainly include code snippets, code comments, code functions, code module names, code change records, code review information, etc.

[0040] Obtain code instances of any one or more projects or classes from a code library as code instances, where the code library includes an open source code library and / or an internal code library.

[0041] S202: Extract code files from the code instance.

[0042] First, the code instance is preprocessed, and the preprocessing includes deleting irrelevant information in the code instance to obtain sample code. The irrelevant information includes at least one of the following: irrelevant comment information, debugging output information, irrelevant code fragments, and useless characters.

[0043] Irrelevant comments refer to comments that are not helpful for the model to understand the sample code, for example: the comments no longer match the current code implementation or are invalid. Comments simply repeat the information that is already clearly expressed in the code line without providing additional context or explanation. Incorrect comments that are inconsistent with the code behavior. Personal notes left by developers in comments, etc.

[0044] Irrelevant code fragments are codes that do not contribute to the operation and function of the program, or are irrelevant to the current function implementation. For example: quantities, functions, classes or modules that are defined but not called or referenced anywhere; identical or very similar code fragments that appear in multiple places; commented-out lines of code, etc.

[0045] Among them, useless characters refer to characters that do not affect code execution but may cause confusion or reduce code readability, such as extra spaces, extra line breaks, extra semicolons, placeholders in template codes, etc.

[0046] S203, determining whether the number of characters included in the code file exceeds a first set number, if yes, executing S204, if no, executing S205.

[0047] Among them, the first set number can be set according to the actual situation. Specifically, since the subsequent steps need to use the model to process the sample code, the number of characters that the model can process is limited due to the model's own structural limitations. Therefore, the above-mentioned first set number can be a value less than or equal to the maximum number of characters that the model can process. For example: assuming that the maximum number of characters that the model can process is 2048 tokens, then the first set number is set to be equal to or less than 2048 characters.

[0048] A code file can be understood as the original code obtained from a code library or other code address. Furthermore, a code file can be the code of a project, or some code fragments in the project code, such as a functional code block, a class code, a code function, a method code, etc.

[0049] The function of a code snippet refers to the specific function that the code snippet can achieve, for example, code snippet 1 is used to achieve the output function, code snippet 2 is used to achieve the input function, and code snippet 3 is used to achieve user interface update.

[0050] S204. Use the code file as sample code

[0051] If the number of characters included in the code file does not exceed the first set number, it means that the number of characters included in the code file does not exceed the maximum number of characters that the model can process. Therefore, the code file can be directly used as a sample code.

[0052] S205: Split the code file into multiple code segments, and use the code segments as sample codes, wherein the number of characters included in the code segments does not exceed a first set number.

[0053] If the number of characters included in the code file exceeds the first set number, it indicates that the number of characters included in the code file exceeds the maximum number of characters that the model can process, and therefore, the code file needs to be split into multiple code fragments. Further, the code file is split into multiple code fragments according to the above code functions.

[0054] Specifically, use static code analysis tools or libraries to parse code files and build an abstract syntax tree (AST), including functions, classes, loops, conditional statements, etc. Traverse the AST to identify basic logical units such as function definitions, class definitions, module-level variable declarations, and import statements. If logical boundaries are identified, extract specific code snippets. For each logical unit, extract the corresponding code snippet from the code file. Perform a deeper analysis on each code snippet to determine its function, and classify and name the code snippet according to its function.

[0055] Further, determine whether the number of characters included in the split code fragment exceeds the first set number. If not, use the split code fragment as a sample code. If yes, split the code fragment into multiple code fragments again, and determine again whether the number of characters included in the split code fragment exceeds the first set number, until the number of characters included in the split code fragment does not exceed the first set number.

[0056] S206. Determine actual review information of the sample code.

[0057] In one possible implementation, determining the actual review information of the sample code includes at least one of the following methods: generating the actual review information of the sample code using a large language model; extracting the actual review information of the sample code from a code instance; and obtaining the actual review information of the sample code from a submission log of the sample code.

[0058] In the embodiments of the present application, three methods for determining actual review information of sample codes are provided.

[0059] 1) Use large language models to generate actual review information for sample codes.

[0060] The sample code is input into the large language model, and the indicator word is used to instruct the large language model to output the review information of the code, and the review information output by the large language model is directly used as the actual review information of the sample code.

[0061] 2) Extract actual review information of sample codes from code examples.

[0062] The code repository generally stores code review information. In the process of obtaining a code instance, the code review information can be obtained as actual review information corresponding to the sample code.

[0063] 3) Obtain the actual review information of the sample code from the submission log corresponding to the sample code.

[0064] The code submission log includes code review information, and the code review information can be obtained from the submission log as actual review information of the sample code.

[0065] S207: Use a data pair consisting of a sample code and actual review information of the sample code as a training sample.

[0066] The sample codes and the corresponding actual review information are combined to obtain data pairs, which are then used to train the self-RAG model.

[0067] In an embodiment of the present application, a method for obtaining training samples is provided to prepare for model training and provide the model with structured and richly annotated training samples to enhance the model's understanding and generation capabilities.

[0068] In a possible implementation, the method further includes: extracting keywords of the sample code and establishing an association relationship between the keywords and the sample code; wherein the keywords are used to assist the code review model in retrieving code associated with the sample code.

[0069] Keywords in code refer to predefined words in programming languages ​​that have special meanings and uses. Keywords usually have special grammatical meanings in code and are used to build program control structures, define variable types, etc. Keywords are part of program syntax and are used to represent specific operations or structures. Since keywords are handled in a special way during compilation or interpretation, they cannot be used as variable names, function names, or other user-defined identifiers.

[0070] For example, in Python programming language, common keywords include: if, elif, else: used for conditional statements. For, while: used for loop structures. Def: used to define functions. Class: used to define classes. Return: return values ​​from functions. Import: import modules or libraries. Try, except, finally: used for exception handling. True, False: Boolean values. None: represents null value or no result.

[0071] In the embodiment of the present application, keywords in the sample code can be extracted by rule method, statistical method, NLP method, deep learning algorithm, etc. A mapping relationship between keywords and sample codes is established to facilitate the code review model to retrieve codes related to the code to be reviewed during use.

[0072] S102. Use the retrieval sub-model in the pre-trained code review model to search the sample code to obtain multiple context information of the sample code, wherein different context information corresponds to different retrieval scopes, and the context information includes context code related to the sample code and annotation information of the context code.

[0073] Among them, the pre-trained code review model adopts the self-RAG model, which is an artificial intelligence model that combines retrieval enhancement technology and generation model. The main principle of the self-RAG model is to add a retrieval model on the basis of the generation model, so as to use external knowledge sources such as documents, databases or information on the Internet to enrich and calibrate the generated content during the generation process. In other words, the pre-trained code review model includes a retrieval sub-model and a review sub-model.

[0074] In traditional generative models, such as the Transformer-based sequence-to-sequence model, all knowledge comes from the parameters learned by the model during training. However, this approach may not be able to handle unseen entities or concepts, especially in cases where domain knowledge is required (such as the code review field). The Self-RAG model assists in the generation of review information by retrieving contextual information of the code.

[0075] The training process of the self-RAG model mainly includes two processes: unsupervised pre-training and model fine-tuning. Specifically, unsupervised training is a learning method that does not require explicit labeling of data. It learns the intrinsic structure of the data by letting the self-RAG model predict part of the data. The training of the code review model in the embodiment of the present application is mainly to perform supervised fine-tuning training on the self-RAG model that has undergone unsupervised pre-training, so that when the self-RAG model is used in the field of code review, it can generate more accurate review information.

[0076] The process of unsupervised pre-training mainly includes: constructing pseudo tasks to let the self-RAG model predict the missing information. For example, given a sample code and partial review information, let the self-RAG model predict the remaining review information. Or, given a sample code change and complete review information, let the model predict which parts of the code caused these opinions. Use pseudo tasks as supervisory signals to train the model so that the model learns to understand the relationship between code and review information.

[0077] Supervised fine-tuning training can also be called incremental training, which allows the self-RAG model to continuously update model parameters as new data arrives, rather than retraining the entire model from scratch.

[0078] Among them, the context information includes the context code related to the sample code and the annotation information of the context code. The context code refers to the environment and support surrounding a sample code (such as a function, method, class, etc.) so that the sample code can run and understand correctly. The context code contains all the necessary components so that the sample code can not only exist independently, but also perform its functions correctly in the entire program or system. For example: the context code related to the sample code includes: import statements, auxiliary functions and classes, global variables and constants, etc. Furthermore, the context code related to the sample code also includes: the code in the dependent library of the sample code.

[0079] Comments are textual descriptions added to the code to explain the code's function, logic, structure, or any information that may be helpful in understanding the code. Good comments can help quickly understand the purpose and working principle of the code, thereby improving development efficiency and code quality.

[0080] The code review model contains a sub-model specifically for retrieval. The retrieval sub-model can understand the code structure and semantics, and can find similar or related code snippets as sample codes based on the sample codes, and obtain the annotation information of the code snippets as the annotation information of the context code. The retrieval sub-model can be built based on different algorithms, such as content-based retrieval, collaborative filtering retrieval, deep learning method retrieval, etc.

[0081] Content-based retrieval refers to finding the best matching code snippet by comparing text similarity (such as cosine similarity, Jaccard similarity, etc.). Collaborative filtering means that if the model is trained based on a large amount of developer behavior data, it can make recommendations by analyzing the code usage patterns of other developers. Deep learning methods refer to using neural networks (such as the Transformer architecture) to encode sample code and search for snippets with similar encodings in a large code base.

[0082] Use the retrieval sub-model in the pre-trained code review model to retrieve sample codes in the following databases to obtain one or more contextual information: code hosting platform, official documentation and API references, internal code knowledge base, code content publicly available on the Internet, etc.

[0083] Specifically, the retrieval sub-model can dynamically adjust the size of the context window. The scope of the considered context can be dynamically expanded or reduced according to the needs of the current sample code. In addition, the retrieval sub-model also includes an attention mechanism, which can use attention weights to identify which parts of the context information are most important for the sample code, thereby adaptively adjusting the focus of the context information.

[0084] When the retrieval sub-model processes sample code, the amount of context required in different scenarios may be different. For example, understanding a simple function call may only require looking at the adjacent few lines of code, while understanding a complex algorithm implementation may require considering the contents of the entire file or even multiple related files. The retrieval sub-model can intelligently expand or reduce the size of the context window according to the specific needs of the current sample code to ensure that the most relevant context information is obtained. By dynamically adjusting the context window, the relevance and accuracy of the results can be improved while maintaining retrieval efficiency. A context window that is too large may cause information overload and increase noise; a context window that is too small may cause the loss of important background information. Therefore, dynamic adjustment helps to find the best balance between the two.

[0085] The attention mechanism allows the retrieval sub-model to evaluate the importance of each piece of contextual information obtained and assign different weights. For sample code, more attention can be paid to those code snippets or variable definitions that are critical to the current task. Based on the attention weights, the retrieval sub-model can automatically adjust its "focus", that is, which parts of the information should be given priority. This ability enables the retrieval sub-model to not only process directly adjacent code, but also span a larger distance to capture indirect but important dependencies.

[0086] The attention mechanism can help the model make better decisions, so that the Self-RAG model can accurately know which contextual information is most critical and can help generate more accurate and expected results.

[0087] In one possible implementation, a sample code is retrieved using a retrieval sub-model in a pre-trained code review model to obtain multiple context information of the sample code, including: using the retrieval sub-model in the pre-trained code review model to retrieve the sample code in a first code base to obtain first context information of the sample code, wherein the first code base is the code base of a project to which the sample code belongs; extracting dependencies of the sample code from the first context information of the sample code; determining the code snippet on which the sample code depends based on the dependencies; using the retrieval sub-model in the pre-trained code review model to retrieve the sample code in a second code base to obtain second context information of the sample code, wherein the second code base is the code base of the project to which the code snippet on which the sample code depends belongs; and fusing the first context information and the second context information to obtain multiple context information of the sample code.

[0088] The first code base refers to the code base of the project to which the sample code belongs, and the second code base refers to the code base to which the code snippet that has a dependency relationship with the sample code belongs, and the dependency relationship is the mutual dependency between different parts of the system. This dependency can occur at multiple levels, including but not limited to between files, modules, functions, classes or objects. The second code base refers to the project code base that contains the above-mentioned dependent code snippet. When the dependent code comes from an external library or a different project, the second code base is different from the first code base.

[0089] The sample code is analyzed using the retrieval sub-model to find context information related to the sample code, including other codes, comments, usage patterns, etc. surrounding the sample code.

[0090] From the context information, identify other functions, classes, modules, or other resources that the sample code directly or indirectly references. Based on the dependency relationship, determine which code snippets the sample code specifically depends on. Use the retrieval sub-model again to find the context information of the sample code in the second code base. This helps to understand how the sample code interacts with the external components it depends on. Finally, combine the results of the two-step retrieval to form a complete context information that shows the relationship between the sample code and its environment and its dependencies.

[0091] S103: Combine multiple context information of the sample code with the sample code respectively to obtain multiple prompt information of the sample code.

[0092] In step S102, one context information of the sample code can be obtained, and multiple context information of the code to be reviewed can also be obtained. Each context information combined with the sample code can generate a prompt information. Among them, the prompt information includes the sample code and the context information. The context information helps the user understand the sample code to improve the accuracy of the review information.

[0093] In a possible implementation, the context information may be connected to the sample code to form prompt information, or the sample code may be connected to the context information to form prompt information. In the embodiment of the present application, the format of the prompt information is not limited, and the prompt information only needs to include the sample code and the context information.

[0094] In a possible implementation, multiple context information are respectively combined with sample codes to obtain multiple prompt information, including: for each context information, prompt information is generated based on the sample code, the context information and the review prompt word.

[0095] The review prompt is used to prompt the generation sub-model in the code review model to review the sample code and output the review information. The review prompt is for the code review model to better understand the user's intention.

[0096] The search prompt word may be: Please review in combination with the following context information and code. Exemplarily, the first prompt information is "Please review in combination with the following context information and code; the code is: AAAA; the context information is: BBBB", where AAAA is the specific code to be reviewed, and BBBB is the context information corresponding to the code to be reviewed. "Please review in combination with the following context information and code" is the review prompt word.

[0097] Furthermore, when the number of context information retrieved in step S102 exceeds a set number, top N context information with higher relevance may be screened out from the plurality of context information and respectively combined with the sample code to generate prompt information.

[0098] The above correlation can be understood as the correlation between the sample code and the context information, which can be expressed by similarity. For example, the similarity between each context information sample code is calculated, and the top N context information with higher similarity are combined with the sample code to generate prompt information.

[0099] Since context information with low similarity has limited effect on the review information of the sample code, the context information with low similarity is filtered out to reduce the amount of data processing for subsequent generation of sub-models.

[0100] S104: Generate initial review information of multiple prompt information of sample code by using the generation sub-model in the pre-trained code review model.

[0101] In the Self-RAG model, the generation sub-model refers to the structure that generates more accurate and contextual review information based on the retrieved context information and sample fragments. The generation sub-model mainly includes encoder, fusion mechanism, decoder and generation strategy.

[0102] The encoder is mainly used to encode the sample code and the context information of the sample code to generate a vector representation. Furthermore, the sample code and the context information of the sample code can be encoded together to capture the interaction between the two, or the sample code and the context information of the sample code can be encoded separately.

[0103] In the case where the sample code and the context information of the sample code are encoded separately, the fusion mechanism is used to combine the encoded representation of the sample code and the encoded representation of the context information. This can be achieved by at least one of the following methods, such as concatenation, weighted summation, or using an attention mechanism to let the generation sub-model focus on the most important part.

[0104] The decoder is mainly used to receive the output from the encoder and / or fusion layer and generate the initial review information based on this information. The decoder generates output word by word using an autoregressive method, and each step depends on the previously generated words and the context information provided by the encoder. During the generation process, the decoder may use the attention mechanism to dynamically focus on different parts of the context information to ensure that the generated review information is both faithful to the sample code and makes full use of the retrieved information.

[0105] To generate more diverse outputs, the generative submodel can adopt different strategies when decoding. For example, beam search keeps multiple possibilities at each step until a complete sentence is generated, while sampling introduces randomness to make the output more diverse.

[0106] The initial review information refers to the review information output by the generation sub-model of the code review that has not been evaluated by the evaluation model.

[0107] In the application embodiment, for multiple prompt information, the multiple prompt information is sequentially input into the generation sub-model in the code review model, and the review information fed back by the generation sub-model is received as the initial review information.

[0108] Specifically, the first prompt information is input into the generation sub-model in the code review model, and the review information fed back by the generation sub-model is received as the initial review information of the first prompt information. Then, the second prompt information is input into the generation sub-model in the code review model, and the review information fed back by the generation sub-model is received as the initial review information of the second prompt information, until all the first prompt information generate corresponding initial review information.

[0109] S105. Select predicted review information of the sample code from multiple initial review information of the sample code.

[0110] Score each piece of initial review information to obtain a score for each piece of initial review information, and use the initial review information with the highest score as the predicted review information.

[0111] The score of the initial review information is used to reflect the accuracy, feasibility and practicality of the initial review information; among them, the score of the initial review information is positively correlated with the accuracy, comprehensiveness and practicality of the initial review information, that is, the higher the score of the initial review information, the more accurate and comprehensive the initial review information is; the lower the score of the initial review information, the less accurate and less comprehensive the initial review information is.

[0112] In the embodiment of the present application, each initial review information is displayed, and the scores of each initial review information input by the experts are obtained. Alternatively, the initial review information is input into a trained evaluation model, and the trained evaluation model is used to score the initial review information to obtain the scores of each initial review information.

[0113] In one possible implementation, output review information is selected from a plurality of initial review information, including: using an evaluation sub-model in a pre-trained code review model to score each initial review information, wherein the score is used to reflect the accuracy, feasibility, and practicality of the initial review information; and using the initial review information with the highest score as the predicted review information.

[0114] The initial review information with the highest score indicates that the initial review information is the most accurate and comprehensive. Therefore, the initial review information with the highest score is used as the predicted review information of the sample code to obtain the most accurate and comprehensive review information.

[0115] The evaluation sub-model refers to a tool or method specifically used to quantitatively evaluate the review opinions generated by the sub-model output. The evaluation sub-model model aims to score the various indicators in the review opinions through certain rules and standards to understand the quality of the review opinions more objectively.

[0116] The evaluation sub-model can learn to predict the quality of the review information generated by the generation sub-model. In simple terms, the evaluation sub-model can identify which review information has higher quality and which review information has lower quality.

[0117] The evaluation sub-model can be trained independently or simultaneously with the retrieval sub-model and the generation sub-model.

[0118] First, the process of training the evaluation sub-model separately is introduced. The main training process includes: First, collect a large number of scores of human reviewers on training samples, that is, the actual scores of human reviewers on the actual review opinions of sample codes. Since score prediction is a regression problem in principle, linear regression, support vector machine, random forest, gradient boosting tree (GBDT), neural network and other algorithms can be selected as the basic model of the evaluation sub-model. Input the training sample into the basic model so that the basic model scores the actual review information of the sample code to obtain the predicted score, compare the predicted score with the actual score, and adjust the parameters of the basic model according to the difference between the predicted score and the actual score. When the difference between the predicted score output by the basic model and the actual score is less than the set value, that is, when the difference between the predicted score output by the basic model and the actual score is small, it indicates that the performance of the evaluation sub-model is satisfactory, and it can be deployed to the code review model, and the trained evaluation sub-model is used to score multiple initial review information.

[0119] An evaluation mechanism is introduced in the embodiment of the present application to evaluate the accuracy and comprehensiveness of multiple initial review information, and the initial review information with the highest score is used as the output review information of the sample code to further improve the accuracy and comprehensiveness of the review information.

[0120] S106. Based on the difference between the predicted review information of the sample code and the actual review information of the sample code, the model parameters of the pre-trained code review model are optimized to obtain a trained code review model.

[0121] Select an appropriate loss function (such as mean square error, cross entropy, etc.) to measure the gap between the predicted review information and the actual review information. Use the back-propagation algorithm to calculate the gradient of the loss relative to the model parameters, and update the model parameters through the gradient descent method to minimize the loss. Use grid search, random search, or Bayesian optimization to find the best parameter configuration and optimize the model parameters.

[0122] Furthermore, the predicted review information output by the model is compared with the actual review information of the sample code to identify the differences between the two. The model weights are adjusted or new features are introduced based on the identified differences. For example, if it is found that certain types of code problems are underestimated, the importance of the corresponding features can be increased. The training and evaluation process is repeated continuously to gradually improve the model performance until the expected goals are met.

[0123] In an embodiment of the present application, the self-RAG model is trained using sample code to obtain a code review model with retrieval and generation functions, so that the self-RAG model has stronger context processing capabilities when conducting code reviews, ensuring that accurate review information can still be generated when processing complex dependent libraries.

[0124] Based on the difference between the predicted review information of the sample code and the actual review information of the sample code, the model parameters of the pre-trained code review model are optimized, including: based on the difference between the predicted review information of the sample code and the actual review information of the sample code, the model parameters of the evaluation sub-model are optimized.

[0125] In the embodiment of the present application, the process of training the evaluation sub-model and the generation sub-model at the same time is introduced, and the main training process includes: Since the score prediction is a regression problem in principle, linear regression, support vector machine, random forest, gradient boosting tree (GBDT), neural network and other algorithms can be selected as the basic model of the evaluation sub-model. Obtain multiple initial review information output by the generation sub-model, and the actual score of each initial review information, which is obtained by the human reviewer reviewing and scoring the initial review information. The sample code and the initial review information are input into the basic model so that the basic model scores the initial review information of the sample code to obtain a predicted score, and the actual score of the predicted score and the initial review information is compared, and the parameters of the basic model are adjusted according to the difference between the predicted score and the actual score of the initial review information. When the difference between the predicted score output by the basic model and the actual score is less than the set value, that is, when the difference between the predicted score output by the basic model and the actual score is small, it indicates that the performance of the evaluation sub-model is satisfactory and the score that humans may give can be predicted. Further, an appropriate learning algorithm is used to optimize the evaluation sub-model to ensure that its predicted score is as close to the actual human score as possible.

[0126] In one possible implementation, the generation sub-model and the evaluation sub-model can be jointly tuned together. That is, after the generation sub-model generates the initial review information, it immediately sends it to the evaluation sub-model to obtain feedback, and adjusts the parameters of the generation model accordingly, thereby forming a closed-loop system to generate higher quality review information.

[0127] Specifically, after the generation sub-model generates multiple initial review information, the sample code and each initial review information are combined in sequence to form the input of the evaluation sub-model. The evaluation sub-model can score the generated initial review information according to predefined standards; based on the scoring results provided by the scoring model, a loss function is calculated. The loss function reflects the gap between the generated initial review information and the actual review information. Based on the loss value of the loss function, the gradient is calculated through the back propagation algorithm, and the parameters of the generation sub-model and the evaluation sub-model are updated. In this way, the generation sub-model can generate high-quality content that is closer to the expected high-quality content of the scoring model in the future, and the overfitting of the evaluation sub-model can be avoided.

[0128] Furthermore, a review information evaluation index is constructed, which is mainly used to evaluate the accuracy and feasibility of the review information. Accuracy is used to measure the consistency between the review information generated by the code review model and the opinions of human experts. Classification indicators such as precision, recall, and F1 score can be used. Or indicators such as BLEU and ROUGE in natural language processing can be used to evaluate the quality of generated review information. Feasibility is used to measure whether the review information is reasonable, practical, and helpful in improving code quality. This can be done through expert scoring or A / B testing.

[0129] Create an automated evaluation tool based on the above evaluation indicators to regularly evaluate the performance of the code review model and collect user feedback on the code review model. Use user feedback to optimize the code review model.

[0130] Based on the above embodiments, the present application embodiment provides a code review method. The code review method provided by the present application embodiment is mainly performed by using the device in the above embodiments to deploy the trained code review model. Figure 3 As shown, the code review method provided in the embodiment of the present application mainly includes the following steps S301-S303.

[0131] S301. Obtain the code to be reviewed.

[0132] The code to be reviewed refers to the source code that has been written but has not been reviewed or checked for quality assurance during the software development process. The code to be reviewed may include: newly submitted code, refactored code, modified code, newly opened code, etc.

[0133] Acquiring the code to be reviewed may include: acquiring newly submitted code from a code repository as the code to be reviewed, the code repository including an open source code repository and / or an internal code repository. Alternatively, acquiring the code to be reviewed may include: acquiring the code uploaded or sent by the code writer as the code to be reviewed through a collaborative platform or a network link.

[0134] In a specific implementation, the code base is GitLab as an example. Log in to the GitLab code base, set up a webhook in the GitLab code base, enter the URL address for receiving webhook events in the "Uniform Resource Locator (URL)" field of the webhook, which refers to the endpoint address that listens to the webhook on the server, and select the trigger event of the Webhook as "Commit Event (MergerequestEvents)". Select any programming language and framework that supports HTTP request processing to create a Webhook receiver. For example: Use Python and the Flask framework to create a Webhook receiver. Deploy the above Flask application to the server, set the Flask application to be externally accessible, and its URL can be accessed from the GitLab code base.

[0135] When there is a new submission or merge request in the code repository, the code repository will send a code submission request to a pre-set URL. The code submission request can be an HTTP POST request, which contains detailed information about the code submitted or merged into the code repository. Exemplarily, the detailed information of the code includes at least one of the following: code address. After receiving the code submission request, the server parses the code submission request, obtains the code address contained in the code submission request, and obtains the code snippet corresponding to the code address as the code to be reviewed.

[0136] In one possible implementation, obtaining the code to be reviewed includes: obtaining an initial code; determining whether the number of characters in the initial code exceeds a first set number; if the number of characters in the initial code does not exceed the first set number, using the initial code as the code to be reviewed; if the number of characters in the initial code exceeds the first set number, splitting the initial code into multiple code fragments, and using the code fragments as codes to be reviewed respectively, wherein the number of characters included in the code fragments does not exceed the first set number.

[0137] Among them, the first set number can be set according to the actual situation. Specifically, since the subsequent steps need to use the model to process the code to be reviewed, the number of characters that the model can process is limited due to the model's own structural limitations. Therefore, the above-mentioned first set number can be a value less than or equal to the maximum number of characters that the model can process. For example: assuming that the maximum number of characters that the model can process is 2048 tokens, then the first set number set must be equal to or less than 2048 characters.

[0138] The initial code can be understood as the original code obtained from the code library or other code addresses. Furthermore, the initial code can be the initial code of a project, or some code fragments in the initial code, such as: a functional code block, a class code, a code function, a method code, etc. The number of characters in the initial code refers to the number of characters contained in the initial code.

[0139] The function of a code snippet refers to the specific function that the code snippet can achieve, for example, code snippet 1 is used to achieve the output function, code snippet 2 is used to achieve the input function, and code snippet 3 is used to achieve user interface update.

[0140] If the number of characters included in the initial code exceeds the first set number, it indicates that the number of characters included in the initial code exceeds the maximum number of characters that the model can process, and therefore, the initial code needs to be split into multiple code fragments. Further, the code file is split into multiple code fragments according to the above code functions.

[0141] Specifically, use static code analysis tools or libraries to parse the initial code and build an abstract syntax tree (AST), including functions, classes, loops, conditional statements, etc. Traverse the AST to identify basic logical units such as function definitions, class definitions, module-level variable declarations, and import statements. If logical boundaries are identified, extract specific code snippets. For each logical unit, extract the corresponding code segment from the source code. Perform a deeper analysis of each code segment to determine its function. Classify and name the code segments according to their functions. Split the initial code into multiple code segments according to the above process.

[0142] Further, determine whether the number of characters included in the split sample fragment exceeds the first set number. If not, use the split code fragment as the sample code. If yes, split the code fragment into multiple code fragments again, and determine again whether the number of characters included in the split code fragment exceeds the first set number, until the number of characters included in the split code fragment does not exceed the first set number.

[0143] S302: Input the code to be reviewed into the trained code review model, and use the code review model to review the code to be reviewed.

[0144] The code review model provided in the embodiments of the present application is obtained after training by the training method of the code review model provided in the above embodiments. The specific training method can refer to the description in the above embodiments.

[0145] The process of the code review model reviewing the code to be reviewed is the same as the process of processing the sample code during the code review model training process, and the details can be referred to the description in the above embodiment.

[0146] S303: Receive target review information output by the code review model.

[0147] The target review information refers to the review result of the code to be reviewed obtained after the code review model reviews the code to be reviewed.

[0148] The target review information includes: defect information of the code to be reviewed.

[0149] Code defect information is also called errors, bugs or problems. Defect information refers to an incorrectness or flaw in the code to be reviewed, which causes the program's behavior to not meet expectations or requirements. Defects can appear in any part of the code to be reviewed, including but not limited to logic, syntax, interface definitions, configuration, etc. When these defects are executed, they may cause program crashes, incorrect results, performance degradation, security vulnerabilities and other problems.

[0150] In the embodiment of the present application, by receiving the defect information output by the code review model, the problem can be better understood and appropriate measures can be taken to improve product quality.

[0151] Furthermore, the target review information also includes: a score for the code to be reviewed and optimization suggestions for defect information. The score is used to reflect the quality of the code to be reviewed.

[0152] The score of the code to be reviewed usually refers to a quantitative indicator given after evaluating the quality, maintainability, efficiency, etc. of the code to be reviewed. This score can be used to help developers intuitively understand the quality of the code and identify areas that need improvement.

[0153] Optimization suggestions for defect information refer to proposing specific improvement measures or best practices after analyzing the identified code defects to ensure that the problems are effectively solved and prevent similar problems from happening again in the future. Common optimization suggestions include: direct repair, performance optimization, security improvement, etc.

[0154] Direct fixes are to provide clear solutions to fix the specific part of the code that caused the defect. Performance optimization is to directly point out where optimization can be done, such as reducing the number of database queries, caching the results of repeated calculations, etc. Security improvement is to take measures to strengthen security vulnerabilities. For example: parameterized queries to prevent SQL injection.

[0155] In the embodiment of the present application, by receiving the optimization suggestions output by the code review model, the designer can directly identify the direction of optimization, thereby improving the quality control level of the entire development process.

[0156] The code review method provided in the embodiment of the present application obtains the code to be reviewed, inputs the code to be reviewed into the trained code review model, uses the code review model to review the code to be reviewed, and receives the target review information output by the code review model. It can realize the automated review of the code, improve the efficiency of the code review, and improve the quality of the review opinions.

[0157] Based on the above embodiments, the present application further optimizes the code review method, such as Figure 4 As shown, the code review method provided in the embodiment of the present application mainly includes steps S401-S405.

[0158] S401. Obtain the code to be reviewed.

[0159] S401 provided in the embodiment of the present application is the same as the process of S301 in the above embodiment. For details, please refer to the description in the above embodiment, which will not be repeated in the embodiment of the present application.

[0160] S402: Use the search sub-model in the code review model to search the code to be reviewed, and obtain multiple context information of the code to be reviewed.

[0161] Different context information corresponds to different search scopes, and the context information includes context codes related to the code to be reviewed and annotation information of the context codes.

[0162] In a possible implementation, it also includes: detecting whether a retrieval prompt word is received; if a retrieval prompt word is received, executing the step of using the retrieval sub-model in the code review model to retrieve the code to be reviewed, and obtaining multiple context information of the code to be reviewed and subsequent steps; if no retrieval prompt word is received, using the generation sub-model in the code review model to generate target review information of the code to be reviewed.

[0163] A prompt word refers to a piece of information or question input by the user during the interaction with the code review model. It is used by the user to describe the requirements or ask questions to the code review model so that the code review model can understand the user's intention. A search prompt word can be understood as a prompt word that instructs the code review model to search for the code to be reviewed.

[0164] In the embodiment of the present application, user input data is received, and the input data includes the code to be reviewed. In addition, the input data may include a search prompt word, or may not include a search prompt word. This depends on whether the user needs to use the search model in the code review model when using the code review model.

[0165] Specifically, the text meaning of the user input data is understood and interpreted to determine whether the user has entered a search prompt word.

[0166] The search prompt may be: Please search for the context information of the following code. For example, the user input data is "Please search for the context information of the following code; the code is: AAAA", where AAAA is the specific code to be reviewed.

[0167] Furthermore, the above search prompt word may also include a specific type of limited context information. The specific type of context information includes annotation information, etc. For example, the search prompt word may be: please search for annotation information of the following code.

[0168] If a search prompt word is received, the code to be reviewed is searched using the search sub-model in the code review model to obtain multiple context information of the code to be reviewed, and review information is generated based on the context information to improve the correctness of the review information.

[0169] If no search prompt is received, the code review model is instructed not to use the sub-search model to search the code to be reviewed, but to directly use the generation sub-model in the code review model to generate target review information of the code to be reviewed. In this way, the generation efficiency of code review information can be improved.

[0170] The process of using the retrieval sub-model in the code review model to retrieve the code to be reviewed is the same as the process of using the retrieval sub-model in the code review model to retrieve the sample code. For details, please refer to the description in the training method of the code review model.

[0171] S403: Combine the multiple context information of the code to be reviewed with the code to be reviewed respectively to obtain multiple review prompt information of the code to be reviewed.

[0172] The process of combining multiple context information with the code to be reviewed to obtain multiple review prompt information is the same as the process of combining multiple context information with sample code to obtain multiple review prompt information. For details, please refer to the description in the training method of the code review model.

[0173] S404: using the generation sub-model in the code review model to generate initial review information of multiple review prompt information of the code to be reviewed.

[0174] The process of combining multiple context information with the code to be reviewed to obtain multiple review prompt information is the same as the process of combining multiple context information with sample code to obtain multiple review prompt information. For details, please refer to the description in the training method of the code review model.

[0175] S405 , selecting target review information of the code to be reviewed from multiple initial review information of the code to be reviewed.

[0176] In one possible implementation, target review information is selected from multiple initial review information, including: using an evaluation sub-model in a code review model to score the initial review information, wherein the score is used to reflect the accuracy, feasibility, and practicality of the initial review information; the initial review information with the highest score is used as the target review information for the code to be reviewed.

[0177] The process of selecting target review information from multiple initial review information is the same as the process of selecting output review information from multiple initial review information. For details, please refer to the description in the training method of the code review model.

[0178] In a possible implementation, a communication connection is established between the code review model and the instant messaging client, the target review information output by the code review model is sent to a set account in the instant messaging client, and the evaluation information made by the set account on the review information is received, and modification suggestions are made. The code review model is optimized using the evaluation information and modification suggestions to improve its review capability.

[0179] An embodiment of the present application provides a code review method, which includes: obtaining a code to be reviewed, searching the code to be reviewed using a retrieval sub-model in a code review model, obtaining multiple context information of the code to be reviewed, wherein different context information corresponds to different retrieval scopes, and the context information includes context code related to the code to be reviewed and annotation information of the context code, combining the multiple context information with the code to be reviewed to obtain multiple review prompt information, and using a generation sub-model in the code review model to generate initial review information of the multiple review prompt information. Select target review information from multiple initial review information. By adaptively adjusting and expanding the retrieval scope of the model, multiple different context information is obtained, so that the model can more comprehensively capture and process the multi-level dependencies of the code, enhance the context understanding ability of the model, and improve the model's understanding of complex code.

[0180] Figure 5 Schematic diagram of the structure of a training device for a code review model in an embodiment of the present application, such as Figure 5 As shown, the code review model training device 50 provided in the embodiment of the present application mainly includes: a sample acquisition module 51, a retrieval module 52, a prompt information generation module 53, an initial review information generation module 54, a predicted review information selection module 55 and a model parameter optimization module 56.

[0181] Among them, the sample acquisition module 51 is used to obtain training samples, and the training samples include sample codes and actual review information of the sample codes; the retrieval module 52 is used to use the retrieval sub-model in the pre-trained code review model to retrieve the sample codes to obtain multiple context information of the sample codes, wherein different context information corresponds to different retrieval ranges, and the context information includes context codes related to the sample codes and annotation information of the context codes; the prompt information generation module 53 is used to combine the multiple context information of the sample codes with the sample codes respectively to obtain multiple prompt information of the sample codes; the initial review information generation module 54 is used to use the generation sub-model in the pre-trained code review model to generate initial review information of multiple prompt information of the sample codes; the predicted review information selection module 55 is used to select the predicted review information of the sample codes from the multiple initial review information of the sample codes; the model parameter optimization module 56 is used to optimize the model parameters of the pre-trained code review model based on the difference between the predicted review information of the sample codes and the actual review information of the sample codes to obtain a trained code review model.

[0182] In one possible implementation, the sample acquisition module 51 is specifically used to obtain a code instance from a code library; extract a code file from the code instance; if the number of characters included in the code file does not exceed a first set number, use the code file as a sample code; if the number of characters included in the code file exceeds the first set number, split the code file into multiple code fragments, and use the code fragments as sample codes, and the number of characters included in the code fragments does not exceed the first set number; determine the actual review information of the sample code; and use the data pair constructed by the sample code and the actual review information of the sample code as a training sample.

[0183] In one possible implementation, the sample acquisition module 51, when used to determine the actual review information of the sample code, includes at least one of the following methods: using a large language model to generate the actual review information of the sample code; extracting the actual review information of the sample code from the code instance; obtaining the actual review information of the sample code from the submission log of the sample code.

[0184] In a possible implementation, it also includes: a keyword association module, which is used to extract keywords from the sample code and establish an association relationship between the keywords and the sample code; wherein the keywords are used to assist the code review model in retrieving the context code of the sample code.

[0185] In one possible implementation, the predicted review information selection module 55 is specifically used to score each initial review information of the sample code using the evaluation sub-model in the pre-trained code review model, and the score is used to reflect the accuracy, feasibility and practicality of the initial review information; the initial review information with the highest score is used as the predicted review information of the sample code; accordingly, the model parameter optimization module 56 is also used to optimize the model parameters of the evaluation sub-model based on the difference between the predicted review information of the sample code and the actual review information of the sample code.

[0186] In one possible implementation, the retrieval module 52 is specifically used to use the retrieval sub-model in the pre-trained code review model to retrieve the sample code in the first code base to obtain the first context information of the sample code, wherein the first code base is the code base of the project to which the sample code belongs; extract the dependency of the sample code from the first context information of the sample code; determine the code snippet on which the sample code depends based on the dependency; use the retrieval sub-model in the pre-trained code review model to retrieve the sample code in the second code base to obtain the second context information of the sample code, wherein the second code base is the code base of the project to which the code snippet on which the sample code depends belongs; and fuse the first context information and the second context information to obtain multiple context information of the sample code.

[0187] The training device for the code review model provided in the embodiment of the present application can execute the training method for the code review model provided in any embodiment of the present invention, and has the functional modules and beneficial effects corresponding to the execution method.

[0188] Figure 6 Schematic diagram of the structure of a code review device in an embodiment of the present application. Figure 6 As shown, the code review device 60 provided in the embodiment of the present application mainly includes: a code acquisition module 61, a code review module 62 and a target review information receiving module 63.

[0189] Among them, the code acquisition module 61 is used to obtain the code to be reviewed; the code review module 62 is used to input the code to be reviewed into a trained code review model, and use the code review model to review the code to be reviewed, and the code review model is trained by a training method of a code review model; the target review information receiving module 63 is used to receive the target review information output by the code review model.

[0190] In one possible implementation, the code acquisition module 61 is specifically used to obtain an initial code; determine whether the number of characters in the initial code exceeds a first set number; if the number of characters in the initial code does not exceed the first set number, the initial code is used as a code to be reviewed; if the number of characters in the initial code exceeds the first set number, the initial code is split into multiple code fragments, and the code fragments are respectively used as codes to be reviewed, and the number of characters included in the code fragments does not exceed the first set number.

[0191] In a possible implementation, the code acquisition module 61 is specifically configured to split the initial code into multiple code segments according to the functions of the code segments.

[0192] In a possible implementation, the target review information includes: defect information of the code to be reviewed.

[0193] In a possible implementation, the target review information also includes: a score of the code to be reviewed and optimization suggestions for defect information, where the score is used to reflect the quality of the code to be reviewed.

[0194] In one possible implementation, the code review module 62 is specifically used to use the retrieval sub-model in the code review model to search the code to be reviewed to obtain multiple context information of the code to be reviewed, wherein different context information corresponds to different retrieval scopes, and the context information includes context code related to the code to be reviewed and annotation information of the context code; the multiple context information of the code to be reviewed are respectively combined with the code to be reviewed to obtain multiple review prompt information of the code to be reviewed; the initial review information of the multiple review prompt information of the code to be reviewed is generated by the generation sub-model in the code review model; and the target review information of the code to be reviewed is selected from the multiple initial review information of the code to be reviewed.

[0195] In one possible implementation, the code review module 62 is specifically used to select target review information of the code to be reviewed from multiple initial review information of the code to be reviewed, including: using the evaluation sub-model in the code review model to score each initial review information of the code to be reviewed, wherein the score is used to reflect the accuracy, feasibility and practicality of the initial review information; and using the initial review information with the highest score as the target review information of the code to be reviewed.

[0196] In a possible implementation, it also includes: a retrieval judgment module, which is used to detect whether a retrieval prompt word is received; if a retrieval prompt word is received, executing the step of using the retrieval sub-model in the code review model to retrieve the code to be reviewed, and obtaining multiple context information of the code to be reviewed and subsequent steps; if the retrieval prompt word is not received, using the generation sub-model in the code review model to generate target review information of the code to be reviewed.

[0197] The code review device provided in the embodiment of the present application can execute the code review method provided in any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.

[0198] Figure 7 is a schematic diagram of the structure of an electronic device provided in this embodiment. The electronic device may include a training device or a code review device for a code review model, such as Figure 7 As shown, the electronic device 700 includes a processor 710, a memory 720, an input device 730, and an output device 740; the number of the processor 710 in the electronic device can be one or more. Figure 7 A processor 710 is taken as an example; the processor 710, the memory 720, the input device 730 and the output device 740 in the electronic device can be connected via a bus or other means. Figure 7 The example of connecting through bus is taken in the following.

[0199] The memory 720, as a computer-readable storage medium, can be used to store software programs, computer executable programs and modules, such as the training method of the code review model or the program instructions / modules corresponding to the code review method in the embodiment of the present invention. The processor 710 executes various functional applications and data processing of the electronic device by running the software programs, instructions and modules stored in the memory 720, that is, implements the training method of the code review model or the code review method provided in the embodiment of the present invention.

[0200] The memory 720 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system and at least one application required for a function; the data storage area may store data created according to the use of the terminal, etc. In addition, the memory 720 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other non-volatile solid-state storage device. In some instances, the memory 720 may further include a memory remotely arranged relative to the processor 710, and these remote memories may be connected to the electronic device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0201] The input device 730 may be used to receive input digital or character information and generate key signal input related to user settings and function control of the electronic device, and may include a keyboard, a mouse, etc. The output device 740 may include a display device such as a display screen.

[0202] This embodiment also provides a storage medium containing computer executable instructions, which, when executed by a computer processor, are used to implement the code review model training method or code review method provided in the embodiment of the present invention.

[0203] Of course, the storage medium containing computer executable instructions provided in an embodiment of the present invention is not limited to the above method operations, and the computer executable instructions can also execute related operations in the code review method or model training method provided in any embodiment of the present invention.

[0204] Through the above description of the implementation mode, the technicians in the relevant field can clearly understand that the present invention can be implemented by means of software and necessary general hardware, and of course it can also be implemented by hardware, but in many cases the former is a better implementation mode. Based on such an understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as a computer floppy disk, read-only memory (ROM), random access memory (RAM), flash memory (FLASH), hard disk or optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods of various embodiments of the present invention.

[0205] It is worth noting that in the embodiments of the training device of the above-mentioned code review model or the code review device, the various units and modules included are only divided according to functional logic, but are not limited to the above-mentioned division, as long as the corresponding functions can be achieved; in addition, the specific names of the functional units are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of the present invention.

[0206] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.

[0207] The above is only a specific implementation of the present application, so that those skilled in the art can understand or implement the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein can be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to these embodiments herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A method for training a code review model, characterized in that: Methods include: Acquire a training sample, wherein the training sample includes a sample code and actual review information of the sample code; Using a retrieval sub-model in a pre-trained code review model to retrieve the sample code, and obtain multiple context information of the sample code, wherein different context information corresponds to different retrieval scopes, and the context information includes context code related to the sample code and annotation information of the context code; Combining the plurality of context information of the sample code with the sample code respectively to obtain a plurality of prompt information of the sample code; Generate initial review information of multiple prompt information of the sample code using the generation sub-model in the pre-trained code review model; Selecting predicted review information of the sample code from a plurality of initial review information of the sample code; Based on the difference between the predicted review information of the sample code and the actual review information of the sample code, the model parameters of the pre-trained code review model are optimized to obtain a trained code review model.

2. The method according to claim 1, characterized in that The obtaining of training samples comprises: Get code examples from the code repository; Extract code files from code examples; If the number of characters included in the code file does not exceed a first set number, the code file is used as a sample code; If the number of characters included in the code file exceeds the first set number, split the code file into multiple code fragments, and use the code fragments as sample codes, wherein the number of characters included in the code fragments does not exceed the first set number; determining actual review information for the sample code; A data pair constructed by the sample code and the actual review information of the sample code is used as a training sample.

3. The method according to claim 2, characterized in that The determining of the actual review information of the sample code includes at least one of the following methods: Generating actual review information of the sample code using a large language model; Extracting actual review information of the sample code from the code instance; The actual review information of the sample code is obtained from the submission log of the sample code.

4. The method according to claim 1, characterized in that Also includes: Extract keywords of the sample code, and establish an association relationship between the keywords and the sample code; wherein the keywords are used to assist the code review model in retrieving context codes of the sample code.

5. The method according to claim 1, characterized in that The step of selecting the predicted review information of the sample code from the initial review information of the plurality of sample codes comprises: Using the evaluation sub-model in the pre-trained code review model to score each initial review information of the sample code, the score is used to reflect the accuracy, feasibility and practicality of the initial review information; Using the highest-scoring initial review information as the predicted review information for the sample code; Accordingly, based on the difference between the predicted review information of the sample code and the actual review information of the sample code, the model parameters of the pre-trained code review model are optimized, including: Based on the difference between the predicted review information of the sample code and the actual review information of the sample code, the model parameters of the evaluation sub-model are optimized.

6. The method according to claim 1, characterized in that The method of using the retrieval sub-model in the pre-trained code review model to retrieve the sample code to obtain multiple context information of the sample code includes: Using a retrieval sub-model in a pre-trained code review model, the sample code is searched in a first code base to obtain first context information of the sample code, wherein the first code base is a code base of a project to which the sample code belongs; extracting the dependency relationship of the sample code from the first context information of the sample code; Determine, based on the dependency relationship, a code segment on which the sample code depends; Using the retrieval sub-model in the pre-trained code review model, the sample code is searched in a second code base to obtain second context information of the sample code, wherein the second code base is a code base of a project to which the code snippet to which the sample code depends belongs; The first context information and the second context information are merged to obtain a plurality of context information of the sample code.

7. A code review method, characterized in that: Methods include: Get the code to be reviewed; Inputting the code to be reviewed into a trained code review model, and using the code review model to review the code to be reviewed, wherein the code review model is trained by the training method for a code review model according to any one of claims 1 to 6; Receive target review information output by the code review model.

8. The method according to claim 7, characterized in that Get the code to be reviewed, including: Get the initial code; Determining whether the number of characters in the initial code exceeds a first set number; If the number of characters in the initial code does not exceed the first set number, the initial code is used as a code to be reviewed; If the number of characters in the initial code exceeds the first set number, the initial code is split into multiple code fragments, and the code fragments are respectively used as codes to be reviewed, and the number of characters included in the code fragments does not exceed the first set number.

9. The method according to claim 8, characterized in that The initial code is split into multiple code fragments, including: The initial code is split into multiple code fragments according to the functions of the code fragments.

10. The method according to claim 7, characterized in that The target review information includes: defect information of the code to be reviewed.

11. The method according to claim 10, characterized in that The target review information also includes: a score of the code to be reviewed and optimization suggestions for the defect information, wherein the score is used to reflect the quality of the code to be reviewed.

12. The method according to claim 7, characterized in that The step of inputting the code to be reviewed into a trained code review model and using the code review model to review the code to be reviewed includes: Using the retrieval sub-model in the code review model to search the code to be reviewed, and obtaining a plurality of context information of the code to be reviewed, wherein different context information corresponds to different retrieval scopes, and the context information includes context code related to the code to be reviewed and annotation information of the context code; Combining the plurality of context information of the code to be reviewed with the code to be reviewed respectively to obtain a plurality of review prompt information of the code to be reviewed; Generate initial review information of multiple review prompt information of the code to be reviewed by using the generation sub-model in the code review model; The target review information of the code to be reviewed is selected from a plurality of initial review information of the code to be reviewed.

13. The method according to claim 12, characterized in that Selecting target review information of the code to be reviewed from multiple initial review information of the code to be reviewed includes: Using the evaluation sub-model in the code review model to score each initial review information from the code to be reviewed, wherein the score is used to reflect the accuracy, feasibility and practicality of the initial review information; The initial review information with the highest score is used as the target review information of the code to be reviewed.

14. The method according to claim 12, characterized in that Also includes: Detect whether the search prompt word is received; If a search prompt word is received, the steps of searching the code to be reviewed by using the search sub-model in the code review model to obtain a plurality of context information of the code to be reviewed and subsequent steps are performed; If the search prompt word is not received, the target review information of the code to be reviewed is generated using the generation sub-model in the code review model.

15. A training device for a code review model, characterized in that: The device includes: A sample acquisition module is used to acquire training samples, where the training samples include sample codes and actual review information of the sample codes; A retrieval module, used to retrieve the sample code using the retrieval sub-model in the pre-trained code review model to obtain multiple context information of the sample code, wherein different context information corresponds to different retrieval scopes, and the context information includes context code related to the sample code and annotation information of the context code; A prompt information generating module, used for combining the plurality of context information of the sample code with the sample code respectively to obtain a plurality of prompt information of the sample code; An initial review information generation module, used to generate initial review information of multiple prompt information of the sample code by using the generation sub-model in the pre-trained code review model; A prediction review information selection module, used for selecting the prediction review information of the sample code from a plurality of initial review information of the sample code; The model parameter optimization module is used to optimize the model parameters of the pre-trained code review model based on the difference between the predicted review information of the sample code and the actual review information of the sample code to obtain a trained code review model.

16. A code review device, characterized in that: The device includes: Code acquisition module, used to obtain the code to be reviewed; A code review module, used for inputting the code to be reviewed into a trained code review model, and using the code review model to review the code to be reviewed, wherein the code review model is trained by the training method of the code review model according to any one of claims 1 to 6; The target review information receiving module is used to receive the target review information output by the code review model.

17. An electronic device, characterized in that the device include: one or more processors; A storage device for storing one or more programs; When one or more programs are executed by one or more processors, the one or more processors implement the training method of the code review model as described in any one of claims 1 to 6, or implement the code review method as described in any one of claims 7 to 14.

18. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, it implements the training method of the code review model as described in any one of claims 1-6, or the code review method as described in any one of claims 7-14.

Citation Information

Cited By

  • Intelligent code review method and system

    CN119848881A

  • Intelligent review method and system

    CN121009875A

  • Database code review method based on context enhancement and multi-dimensional confidence filtering

    CN121935131A