Code completion method and system based on large language model
By integrating the quality assessment mechanism and Monte Carlo tree search technology into the decoding phase of the large language model, the problems of high computational resource consumption, generated code that does not comply with specifications, and lack of real-time quality assessment in code completion methods based on large language models are solved. Efficient and accurate code completion effects are achieved.
Patent Information
- Application Number
- CN202411899850.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-23
- Publication Date
- 2025-01-24
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The code completion method based on large language models has problems such as high computing resource consumption, the generation of code may not comply with programming specifications and logical requirements, and the lack of a quality evaluation mechanism for real-time generated results.
Use Transformer architecture to train a large language model, and integrate the quality assessment mechanism and Monte Carlo tree search technology during the decoding stage, quickly explore the candidate space, and screen the best code completion results.
It has significantly improved the accuracy and efficiency of code completion, reduced the follow -up correction work caused by errors, avoided unnecessary waste of computing resources, and achieved efficient and accurate code complement.
Smart Images

Figure CN119356686A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a code completion method and system based on a large language model. Background Art
[0002] In the field of code completion, traditional methods mainly rely on algorithms based on rules or statistical models to predict the next possible code snippet by parsing code structure, grammatical rules, and contextual information. However, with the increasing diversity and complexity of programming languages, these methods have gradually exposed their limitations. In recent years, with the rise of large language model technology, code completion methods based on large language models have gradually become mainstream. These methods generate high-quality code completion suggestions by training large language models containing billions or even trillions of parameters and deeply understanding the code context.
[0003] Although code completion methods based on large language models have made significant progress, they still have some shortcomings: First, large language models need to process a large number of candidate tokens and perform complex probability calculations during the reasoning process, which leads to high computing resource consumption and long reasoning time.
[0004] Second, although large language models can generate coherent and meaningful text, in code completion tasks, the generated code may not fully meet programming specifications, logic, or context requirements. This may lead to uneven quality of completion results and even introduce errors or vulnerabilities.
[0005] 3. Traditional code completion methods based on large language models often rely on the final completion results directly generated by the model, and lack a real-time evaluation mechanism for the quality of the generated results during the reasoning process. This causes the model to waste a lot of computing resources on generating low-quality candidate tokens, and is unable to adjust the strategy in time to optimize the final completion results. Summary of the invention
[0006] Based on this, the purpose of the present invention is to propose a code completion method and system based on a large language model to solve the above-mentioned problems.
[0007] According to a code completion method based on a large language model proposed in the present invention, the method includes: Train a large language model for code completion based on the Transformer architecture and integrate quality assessment into the Transformer decoding phase; Input the code snippet to be completed and its context information into the large language model; The Transformer analyzes the context information of the code snippet to be completed, and generates a set of candidate tokens that match the context information and their corresponding probability distribution for each token position to be completed. In the decoding stage of Transformer, the quality assessment mechanism and Monte Carlo tree search technology are combined to quickly explore the candidate space and select the best token for code completion for each token position to be completed.
[0008] Furthermore, the Transformer analyzes the context information of the code snippet to be completed, and generates a set of candidate tokens matching the context information and their corresponding probability distribution for each token position to be completed, including: Initialize the Transformer model as a policy network to generate candidate tokens and their initial probability distribution; Pass the current code snippet as input to the decoder of the Transformer model to generate a set of candidate tokens and their corresponding probability distributions for the current token position to be completed; In the decoding phase of Transformer, the quality assessment mechanism and Monte Carlo tree search technology are combined to quickly explore the candidate space, and the best token is selected for code completion for each token position to be completed, including: Initialize the root node of the Monte Carlo tree search, which represents the current code snippet state or context information; For the token position to be completed, each candidate token generated by the Transformer model is treated as a candidate node in order according to the probability distribution; The Monte Carlo tree search is used to quickly evaluate the quality of the corresponding candidate tokens and the overall quality of their potential subsequent token combinations to obtain the evaluation results. Based on the evaluation results, the most promising candidate tokens are selected for expansion, new nodes are generated, and the statistical information of the Monte Carlo tree is updated; When the search termination condition is reached, the search is terminated, and the candidate node with the highest evaluation score is selected from the Monte Carlo tree, and the corresponding candidate token is used as the best code completion result for the current token position to be completed; For each token position to be completed in the code snippet, repeat the above steps: generate candidate tokens through Transformer and evaluate and select the best token through Monte Carlo tree search until all positions are completed.
[0009] Furthermore, the Monte Carlo tree search is used to quickly evaluate the quality of the corresponding candidate token and the overall quality of its potential subsequent token combination, including: Generate child nodes of the candidate node and initialize them; Starting from the candidate node, a random simulation is performed to generate subsequent token combinations, and each corresponding generated code snippet is evaluated through a reward function to obtain a reward value; The reward value obtained by simulation is back-propagated to each node on the search path to update the evaluation score and visit count of each node.
[0010] Furthermore, according to the evaluation results, the most promising candidate tokens are selected for expansion, new nodes are generated, and the statistical information of the Monte Carlo tree is updated, including: According to the evaluation scores and visit times of the nodes in the Monte Carlo tree, as well as the exploration parameters, the most promising candidate tokens are selected and new nodes are generated, indicating the status of the code snippet after the corresponding candidate token is selected; Update the evaluation score and visit count of the new node, as well as relevant information about its parent node.
[0011] Furthermore, the node sequence with the highest evaluation score is selected from the Monte Carlo tree and output as a result of code completion, including: After the search is terminated, the node sequence with the highest evaluation score is selected from the Monte Carlo tree to represent the path formed by selecting the most promising candidate token starting from the root node until a node representing the complete code snippet is reached; The filtered node sequence is output as the result of code completion.
[0012] Furthermore, before obtaining the reward value by evaluating each code snippet generated by the reward function, the method further includes: Collect high-quality code bases and developer feedback, analyze and extract multiple dimensions of code quality; Learn an embedding space for measuring code quality; Based on the learned embedding space, a multi-level reward function is designed to evaluate the position of the code snippet in the embedding space and give corresponding rewards based on its similarity with high-quality code snippets; For each generated code snippet, it is evaluated using the learned embedding space and reward function to obtain a reward value.
[0013] Furthermore, the learning of an embedding space for measuring code quality includes: Designing a neural network model, wherein the neural network model accepts the code snippet as input and outputs a high-dimensional vector; The collected high-quality and low-quality codes are used as training data, and the neural network model is trained through deep metric learning technology, where the training goal is to make the distance between high-quality code fragments in the embedding space close and the distance between low-quality code fragments in the embedding space far.
[0014] Furthermore, the current code snippet is passed as input to the decoder of the Transformer model to generate a set of candidate tokens and their corresponding probability distributions for the current token position to be completed, including: Convert the current code snippet into a token sequence; Add special markers at the beginning and end of the token sequence; Assigning a position code to each token in the token sequence; Convert the encoded token sequence and position encoding into tensor form; Pass the input tensor to the decoder of the Transformer model, and based on the generated code and the information of the position to be completed, as well as the attention mechanism, generate a prediction of the current token position to be completed through the decoder, and output a corresponding probability distribution vector, where each element of the probability distribution vector corresponds to a token, which is used to represent the probability of each token appearing in the current context; According to the probability distribution vector, the first K tokens with the highest probabilities are selected as candidate tokens.
[0015] The present invention also proposes a code completion system based on a large language model, the system comprising: Model building module: used to train a large language model for code completion based on the Transformer architecture and integrate the quality assessment mechanism into the Transformer decoding stage; Input module: used to input the code snippet to be completed and its context information into the large language model; Generation module: used to analyze the context information of the code snippet to be completed through Transformer, and generate a set of candidate tokens matching the context information and their corresponding probability distribution for each token position to be completed; Screening module: It is used to quickly explore the candidate space in the decoding stage of Transformer by combining the quality assessment mechanism and Monte Carlo tree search technology, and select the best token for code completion for each token position to be completed.
[0016] In summary, the code completion method based on a large language model of the present invention utilizes the powerful sequence modeling capability of the Transformer architecture to train a large language model specifically for code completion. The Transformer can deeply capture the contextual dependencies of the code, providing a solid foundation for accurate code completion. In the decoding phase of the Transformer, a quality assessment mechanism is integrated to perform real-time evaluation of the generated candidate tokens, ensuring that the screened tokens are not only highly matched with the current context, but also have excellent overall code quality, thereby significantly improving the accuracy of code completion.
[0017] Compared with the final completion result directly generated by the model in the traditional method, the present invention generates a set of candidate tokens and their probability distribution for the current token to be completed in the decoding stage of the Transformer, so as to provide more possibilities for subsequent quality evaluation and selection, and perform quality evaluation in the Transformer decoding stage. This instant adjustment enables the model to exclude low-quality and rule-incompliant candidate tokens at an early stage, significantly improving the accuracy of code completion and reducing subsequent correction work caused by erroneous completion, thereby avoiding unnecessary waste of computing resources. In combination with the Monte Carlo tree search technology, the present invention can efficiently explore the candidate space and quickly screen out the best candidate tokens. This strategy not only improves the search efficiency, but also avoids potential errors or inefficient completion caused by a single selection. The introduction of the Monte Carlo tree search technology further optimizes the search process, reduces invalid searches and computing overhead, and makes the entire code completion process more efficient and energy-saving.
[0018] Additional aspects and advantages of the present invention will be given in part in the following description and in part will be obvious from the following description or will be learned through embodiments of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] The above and / or additional aspects and advantages of the present invention will become apparent and easily understood from the description of the embodiments in conjunction with the following drawings, in which: Figure 1 This is a flowchart of a code completion method based on a large language model according to the first embodiment of the present invention; Figure 2 This is a system block diagram of a code completion system based on a large language model according to Embodiment 2 of the present invention. DETAILED DESCRIPTION
[0020] In order to facilitate the understanding of the present invention, the present invention will be described more fully below with reference to the relevant drawings. Several embodiments of the present invention are given in the drawings. However, the present invention can be implemented in many different forms and is not limited to the embodiments described herein. On the contrary, the purpose of providing these embodiments is to make the disclosure of the present invention more thorough and comprehensive.
[0021] It should be noted that when an element is referred to as being "fixed to" another element, it may be directly on the other element or there may be a central element. When an element is considered to be "connected to" another element, it may be directly connected to the other element or there may be a central element at the same time. The terms "vertical", "horizontal", "left", "right" and similar expressions used herein are for illustrative purposes only.
[0022] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which the present invention belongs. The terms used herein in the specification of the present invention are only for the purpose of describing specific embodiments and are not intended to limit the present invention. The term "and / or" used herein includes any and all combinations of one or more of the related listed items. Embodiment 1
[0023] See also Figure 1 The present invention proposes a code completion method based on a large language model, the method comprising steps S101 to S104: S101, trains a large language model for code completion based on the Transformer architecture, and integrates a quality assessment mechanism into the Transformer decoding phase.
[0024] It should be noted that a large language model for code completion is trained based on the Transformer architecture, and a quality assessment mechanism is integrated into the decoding stage. This large code completion prediction model that combines the Transformer architecture and the quality assessment mechanism can greatly improve the accuracy and efficiency of code completion.
[0025] The Transformer model has powerful sequence modeling capabilities and attention mechanisms, making it very suitable for processing sequence data such as code. During the training phase, the model receives a large number of code snippets as input, and gradually masters the language rules of the code by learning the grammatical structure, logical relationships, and style characteristics of these code snippets. This process usually involves splitting the code into words, subwords, or characters, and assigning them unique IDs to form a token sequence that the model can understand. Subsequently, through self-supervised learning, the model will try to predict the next token in the sequence, thereby continuously adjusting its parameters to improve the accuracy of the prediction.
[0026] However, relying solely on the predictive power of the Transformer model is not enough, because code completion not only needs to generate grammatically correct code, but also needs to ensure the quality and readability of the code. Therefore, this embodiment incorporates a quality assessment mechanism in the decoding stage. Each time multiple candidate tokens are generated, the model not only considers the grammatical and semantic matching of the tokens, but also screens and sorts the candidate tokens through the quality assessment mechanism. Specifically, the model evaluates the fit of each candidate token with the generated code snippet, and whether it meets high-quality code standards and pre-set rules, such as grammatical correctness, logical consistency, style consistency, etc.
[0027] S102: input the code snippet to be completed and its context information into the large language model.
[0028] It should be noted that the code snippet to be completed usually contains one or more missing tokens (such as variable names, method calls, control structures, etc.). This snippet is incomplete and may even contain syntax errors, which need to be completed through the big prediction model. Among them, token refers to a basic unit in the source code analysis process, which is the smallest valid element that constitutes the source code text.
[0029] The contextual information of the code snippet to be completed is crucial to understanding the intention of the code snippet to be completed. It may include the previous code lines, function definitions, class structures, imported modules, etc. This information provides the model with the necessary background to help the model predict the missing token more accurately.
[0030] After the code snippet to be completed is input into the model, the model converts it into a format that the model can understand. This usually involves splitting the code into words, subwords, or characters and assigning them unique IDs (ie tokens). This process may also include adding special markers to mark the beginning and end of the sequence, and assigning position codes to each token to indicate its position in the sequence. In practical applications, in order to improve processing efficiency, multiple code snippets can also be combined into a batch for input. This requires the model to be able to handle sequences of different lengths and to maintain data consistency within the batch.
[0031] In the Transformer-based large language model, an encoder-decoder architecture is used. The encoder is responsible for processing context information and encoding it into a fixed-size vector representation (i.e., context embedding). The decoder receives this context embedding and the token sequence of the code snippet to be completed (which may contain special <mask>or <pad>The model generates a probability distribution for each possible token, indicating the likelihood of each token appearing in the current context.
[0032] S103, analyzing the context information of the code snippet to be completed through Transformer, and generating a set of candidate tokens matching the context information and their corresponding probability distribution for each token position to be completed.
[0033] It should be noted that the Transformer model analyzes the context information of the code snippet to be completed, and generates a set of candidate tokens that match the context information and their corresponding probability distribution for each token to be completed. This is a complex process involving encoding, decoding, the use of attention mechanisms, and output processing. This process fully utilizes the powerful sequence modeling capabilities of the Transformer model and the flexibility of the attention mechanism, providing an efficient and accurate solution for the code completion task.
[0034] Specifically, first, the Transformer model receives the code snippet to be completed and its context information as input. This information is converted into a token sequence that the model can understand. Each token represents a word, subword, or character in the code and is assigned a unique ID. At the same time, in order to identify the beginning and end of the sequence and provide position information for each token, special markers (such as <bos> 、 <eos>) and position encoding.
[0035] Through the encoder part of the Transformer model, the context information is further processed into one or more vector representations (i.e., context embeddings). These embeddings capture key information such as the grammatical structure, semantic relations, and style characteristics of the code snippet.
[0036] In the decoder part of the Transformer model, the contextual embedding of the code snippet to be completed is used as input to generate candidate tokens that match the context for each token position to be completed. The decoder uses the attention mechanism to flexibly focus on different parts of the input sequence to predict the missing token based on the contextual information.
[0037] For each possible token, the decoder calculates a probability value, which indicates the likelihood of the token appearing in the current context. These probability values together form a probability distribution vector, where each element corresponds to the probability of a token. Based on this probability distribution vector, the top K tokens with the highest probabilities can be selected as candidate tokens. These candidate tokens not only match the context information, but are also grammatically and semantically reasonable completion options.
[0038] In the Transformer model, self-attention or cross-attention can be used. Self-attention allows the model to focus on other tokens in the input sequence when processing each token, thereby capturing global dependencies. Cross-attention is used for the decoder to focus on the context embedding generated by the encoder when processing the token position to be completed, thereby achieving effective use of context information. In order to improve the expressiveness of the attention mechanism, a multi-head attention mechanism can also be used. The model calculates multiple attention distributions in parallel and merges their results to obtain richer context information.
[0039] S104, in the decoding stage of Transformer, combines the quality assessment mechanism and Monte Carlo tree search technology to quickly explore the candidate space, and selects the best token for code completion for each token position to be completed.
[0040] It should be noted that combining the quality assessment mechanism and Monte Carlo tree search technology in the decoding stage of Transformer to select the best candidate token for each token position to be completed for code completion is a complex but efficient strategy that integrates deep learning, search algorithms and heuristic rules. This strategy can significantly improve the accuracy and efficiency of code completion and provide developers with a more intelligent and efficient programming assistance tool.
[0041] Specifically, the decoding stage of the Transformer is responsible for gradually generating the target sequence (i.e., code) based on the context information provided in the encoding stage. In each decoding step, the model predicts a set of most likely tokens for each token position to be completed based on the current output sequence and context information.
[0042] The quality assessment mechanism is used to conduct real-time quality assessment on each candidate token generated by Transformer. The key evaluation indicators can include grammatical correctness, semantic rationality, context relevance, code style consistency, and other aspects. It can also include some other preset rules, such as code length limit, call limit of specific functions or methods, naming conventions, security checks, etc. These evaluation criteria can ensure the quality of the generated code and customization requirements.
[0043] The quality assessment mechanism can rely on reward functions, pre-trained models, heuristic rules, static analysis tools, or statistical information of historical code bases. In a specific implementation below, quality assessment based on reward functions is described in detail, and the evaluation results can be used as evaluation criteria in the Monte Carlo Tree Search (MCTS) search process. The MCTS technology is used to quickly explore the candidate space, and the exploration process combines quality assessment to screen out the best candidate tokens. It simulates multiple possible code completion paths and scores each path (i.e., each generated code snippet) according to the quality assessment mechanism to find the optimal or approximately optimal solution. Specifically, MCTS starts from the current state and gradually expands the search tree. At each node, the most potential candidate token is selected for expansion, and the quality of the token and its subsequent possible combinations is evaluated. Through iterative search and evaluation, MCTS can gradually focus on those candidate paths that are most likely to lead to high-quality completion. MCTS combines randomness and heuristics to efficiently explore a large candidate space with limited computing resources. At the same time, its combination with the quality assessment mechanism makes the search process more intelligent and targeted, and acts on the decoding stage of Transformer, enabling the model to more intelligently filter out tokens based on real-time quality assessment and search feedback.
[0044] In this example, the powerful sequence modeling capability and attention mechanism of Transformer provide a high-quality candidate token generation basis for MCTS, while MCTS further improves the accuracy and efficiency of code completion through efficient search strategies. The synergy between the two enables the model to perform well in complex code completion tasks.
[0045] Based on step S101 to step S104, the present invention utilizes the powerful sequence modeling capability of the Transformer architecture to train a large language model specifically for code completion. The Transformer can deeply capture the contextual dependencies of the code, providing a solid foundation for accurate code completion. A quality assessment mechanism is integrated into the decoding phase of the Transformer to perform real-time evaluation of the generated candidate tokens, ensuring that the selected tokens are not only highly matched with the current context, but also have excellent overall code quality, thereby significantly improving the accuracy of code completion.
[0046] Compared with the final completion result directly generated by the model in the traditional method, the present invention generates a set of candidate tokens and their probability distribution for the current token to be completed in the decoding stage of the Transformer, so as to provide more possibilities for subsequent quality evaluation and selection, and perform quality evaluation in the Transformer decoding stage. This instant adjustment enables the model to exclude low-quality and rule-incompliant candidate tokens at an early stage, significantly improving the accuracy of code completion and reducing subsequent correction work caused by erroneous completion, thereby avoiding unnecessary waste of computing resources. In combination with the Monte Carlo tree search technology, the present invention can efficiently explore the candidate space and quickly screen out the best candidate tokens. This strategy not only improves the search efficiency, but also avoids potential errors or inefficient completion caused by a single selection. The introduction of the Monte Carlo tree search technology further optimizes the search process, reduces invalid searches and computing overhead, and makes the entire code completion process more efficient and energy-saving.
[0047] The following is a further detailed introduction to a code completion method based on a large language model according to an embodiment of the present invention: Further optionally, in step S102, the context information of the code snippet to be completed is analyzed by the Transformer, and a set of candidate tokens matching the context information and their corresponding probability distributions are generated for each token position to be completed, including: Initialize the Transformer model as a policy network to generate candidate tokens and their initial probability distribution; The current code snippet is passed as input to the decoder of the Transformer model to generate a set of possible candidate tokens and their corresponding probability distributions for the current token position to be completed.
[0048] Understandably, in the code completion task, the Transformer model, as a policy network, will analyze the context information of the current code snippet and generate a set of candidate tokens closely related to the context and their corresponding probability distributions for the current token position to be completed. These candidate tokens are possible options for code completion, and the probability distribution provides the model's confidence in each candidate token. These candidate tokens constitute the candidate nodes of the MCTS search tree, and after MCTS search and evaluation, the best candidate token is found to complete the code for the current token position to be completed. The Transformer model then predicts the next token position based on the generated code.
[0049] Further optionally, in step S103, in the decoding stage of the Transformer, the quality assessment mechanism and the Monte Carlo tree search technology are combined to quickly explore the candidate space, and the best token is selected for code completion for each token position to be completed, including: Initialize the root node of the Monte Carlo tree search, which represents the current code snippet state or context information; For the token position to be completed, each candidate token generated by the Transformer model is treated as a candidate node in order according to the probability distribution; The Monte Carlo tree search is used to quickly evaluate the quality of the corresponding candidate tokens and the overall quality of their potential subsequent token combinations to obtain the evaluation results. Based on the evaluation results, the most promising candidate tokens are selected for expansion, new nodes are generated, and the statistical information of the Monte Carlo tree is updated; When the search termination condition is reached, the search is terminated, and the candidate node with the highest evaluation score is selected from the Monte Carlo tree, and the corresponding candidate token is used as the best code completion result for the current token position to be completed; For each token position to be completed in the code snippet, repeat the above steps: generate candidate tokens through Transformer and evaluate and select the best token through Monte Carlo tree search until all positions are completed.
[0050] Understandably, in the Transformer decoding stage, for the current token position to be completed, according to the generated candidate tokens and their probability distribution, these candidate tokens are sequentially used as candidate nodes of MCTS to quickly evaluate the quality of each candidate token and its potential subsequent combination (referring to the token sequence or combination that may be generated after the current candidate token is selected) in real time. Here, not only the direct impact of the current candidate token is considered, but also the long-term impact of its selection on the subsequent token generation and code completion process. For each candidate node (i.e., candidate token), MCTS quickly evaluates its quality and its impact on subsequent code completion by simulating possible future completion paths. The quality evaluation mechanism here can be designed based on the reward function. During the evaluation process, a dynamic pruning strategy can be used to avoid excessive exploration of unlikely candidate nodes, thereby reducing computational overhead. Based on the evaluation results of MCTS, the candidate token with the highest evaluation score is selected as the best choice for the current token position to be completed. This selection may take into account the quality of the candidate token itself, the overall quality of its potential subsequent combinations, and statistical information during the search process (such as the number of node visits and evaluation scores). When the search termination condition is reached (such as reaching the maximum search depth, time limit, or evaluation score threshold), the search process is terminated and the final code completion result is output.
[0051] In summary, Monte Carlo tree search and quality assessment are performed during the Transformer decoding phase, and act on the candidate tokens for the current token position to be completed that are generated by the Transformer in real time, so that the model can screen and exclude those low-quality candidate tokens that do not conform to programming rules and contexts at an early stage, so as to focus more on evaluating and utilizing those high-quality, rule-compliant candidate tokens. This strategy not only significantly improves the accuracy of code completion, but also greatly reduces the need for later corrections due to incorrect completion, effectively avoiding the waste of computing resources. And by combining the powerful generation capabilities of the Transformer model with the rapid exploration and real-time quality assessment capabilities of MCTS, efficient and accurate code completion is achieved, and the optimal code completion solution can be quickly found.
[0052] Further optionally, the method of quickly evaluating the quality of the corresponding candidate token and the overall quality of its potential subsequent token combination through Monte Carlo tree search includes: Generate child nodes of the candidate node and initialize them; Starting from the candidate node, a random simulation is performed to generate subsequent token combinations, and each corresponding generated code snippet is evaluated through a reward function to obtain a reward value; The reward value obtained by simulation is back-propagated to each node on the search path to update the evaluation score and visit count of each node.
[0053] Understandably, the use of Monte Carlo tree search technology has shown significant advantages in evaluating the overall quality of candidate tokens and their potential subsequent token combinations. Specifically, the scheme first generates and initializes the child nodes of the candidate node. Then, starting from the candidate node, it generates a variety of subsequent token combinations through random simulation, and uses the reward function to quantitatively evaluate these combinations to obtain the reward value of each code snippet (i.e., each path). Importantly, these reward values will be accurately back-propagated to each node on the search path, prompting the model to dynamically update the evaluation score (i.e., the probability or value of the node as the optimal choice) and the number of visits of each node according to the actual simulation results. This series of operations not only greatly improves the evaluation efficiency, enabling the model to quickly screen high-quality candidate tokens under limited resources, but also significantly enhances the accuracy and reliability of decision-making. In addition, the feedback mechanism of MCTS provides valuable learning signals for the model, promotes its continuous optimization of generation strategies, and shows strong adaptability, which can be flexibly applied to different programming environments and tasks. Therefore, the scheme achieves efficient and accurate code completion by integrating the rapid exploration, real-time evaluation and feedback update mechanism of Monte Carlo tree search.
[0054] Further optionally, the most promising candidate tokens are selected for expansion according to the evaluation results, new nodes are generated, and statistical information of the Monte Carlo tree is updated, including: According to the evaluation scores and visit times of the nodes in the Monte Carlo tree, as well as possible exploration parameters, the most promising candidate tokens are selected and new nodes are generated, indicating the status of the code snippet after the corresponding candidate token is selected; Update the evaluation score and visit count of the new node, as well as relevant information about its parent node.
[0055] Understandably, by selecting the most promising candidate tokens for expansion based on the evaluation scores, access times, and exploration parameters of the nodes in the Monte Carlo tree, a new node representing the state of the code snippet after the candidate token is selected is generated. This step not only reflects the model's ability to accurately identify high-quality candidate tokens, but also effectively expands the search space by generating new nodes. At the same time, the evaluation scores, access times, and related information of the new nodes and their parent nodes are updated to ensure the real-time and accuracy of the Monte Carlo tree statistical information.
[0056] Further optionally, the step of selecting a node sequence with the highest evaluation score from the Monte Carlo tree and outputting the result of code completion includes: After the search is terminated, the node sequence with the highest evaluation score is selected from the Monte Carlo tree to represent the path formed by selecting the most promising candidate token starting from the root node until a node representing the complete code snippet is reached; The filtered node sequence is output as the result of code completion.
[0057] It is understandable that after the Monte Carlo tree search is terminated, the node sequence with the highest evaluation score is selected, starting from the root node, and the most potential candidate tokens are selected along the way until the end node representing the complete code snippet is reached. This enables the accurate identification of high-quality code completion paths and ensures that the final output code completion results have extremely high reliability and accuracy.
[0058] Further optionally, before evaluating each code snippet generated by the reward function to obtain a reward value, the process further includes: Collect high-quality code bases and developer feedback, analyze and extract multiple dimensions of code quality; Learn an embedding space for measuring code quality; Based on the learned embedding space, a multi-level reward function is designed to evaluate the position of the code snippet in the embedding space and give corresponding rewards based on its similarity with high-quality code snippets; For each generated code snippet, it is evaluated using the learned embedding space and reward function to obtain a reward value.
[0059] Understandably, by widely collecting high-quality code bases and absorbing developer feedback, we have analyzed and refined multiple key dimensions for measuring code quality, covering grammatical correctness, logical consistency, style consistency, readability, maintainability, common use, and other aspects, or some other preset rules. Learn an embedding space for accurately measuring code quality, and based on this embedding space, design a multi-level reward function that can carefully evaluate the position of the code snippet in the embedding space and give corresponding rewards based on its similarity to the high-quality code snippet. Therefore, each time a code snippet is generated, the system can perform a comprehensive and accurate evaluation through the learned embedding space and reward function to derive an accurate reward value. This not only improves the comprehensiveness and accuracy of the code snippet evaluation, but also provides a strong guarantee for generating high-quality, compliant code.
[0060] Further optionally, the learning of an embedding space for measuring code quality includes: Designing a neural network model, wherein the neural network model accepts the code snippet as input and outputs a high-dimensional vector; The collected high-quality and low-quality codes are used as training data, and the neural network model is trained through deep metric learning technology, where the training goal is to make the distance between high-quality code fragments in the embedding space close and the distance between low-quality code fragments in the embedding space far.
[0061] Understandably, a neural network model is designed that can receive code snippets as input and output an information-rich high-dimensional vector, i.e., the embedded representation of the code, through its deep structure. The collected high-quality and low-quality codes are used as training data, and the model is trained through deep metric learning techniques, such as triple loss, contrast loss and other training methods. The model can be continuously optimized during the training process, and its goal is to ensure that in the embedding space, high-quality code snippets can be closely gathered, while low-quality code snippets are relatively scattered. The learned embedding space can effectively distinguish between high and low code quality.
[0062] Further optionally, the current code snippet is passed as input to the decoder of the Transformer model to generate a set of candidate tokens and their corresponding probability distributions for the current token position to be completed, including: Convert the current code snippet into a token sequence; Add special markers at the beginning and end of the token sequence; Assigning a position code to each token in the token sequence; Convert the encoded token sequence and position encoding into tensor form; Pass the input tensor to the decoder of the Transformer model, and based on the generated code and the information of the position to be completed, as well as the attention mechanism, generate a prediction of the current token position to be completed through the decoder, and output a corresponding probability distribution vector, where each element of the probability distribution vector corresponds to a token, which is used to represent the probability of each token appearing in the current context; According to the probability distribution vector, the first K tokens with the highest probabilities are selected as candidate tokens.
[0063] As you can understand, first, the generated code and the position information to be completed are converted into a token sequence that the model can recognize, and the start and end of the sequence are clarified by adding special markers. Subsequently, a position code is assigned to each token to capture its position information in the sequence, which is the core of Transformer processing sequence data. The encoded token sequence and position code are further converted into tensor form, which is convenient for model processing, and in batch processing scenarios, the input tensors of multiple code snippets can be flexibly combined. Next, these input tensors are fed into the decoder of the Transformer model. With the help of the attention mechanism, the decoder can deeply analyze the context of the generated code and the position to be completed, so as to accurately predict the token of the token position to be completed, and output a detailed probability distribution vector. Each element in this vector corresponds to a token, which intuitively reflects the possibility of each token appearing in the current context. According to the probability distribution, the top K most likely candidate tokens can be screened out to provide high-quality and diverse choices for code completion tasks.
[0064] Embodiment 2 See also Figure 2 The present invention proposes a code completion system based on a large language model, the system comprising: Model building module: used to train a large language model for code completion based on the Transformer architecture and integrate the quality assessment mechanism into the Transformer decoding stage; Input module: used to input the code snippet to be completed and its context information into the large language model; Generation module: used to analyze the context information of the code snippet to be completed through Transformer, and generate a set of candidate tokens matching the context information and their corresponding probability distribution for each token position to be completed; Screening module: It is used to quickly explore the candidate space in the decoding stage of Transformer by combining the quality assessment mechanism and Monte Carlo tree search technology, and select the best token for code completion for each token position to be completed.
[0065] Optionally, the generation module is further configured to: Initialize the Transformer model as a policy network to generate candidate tokens and their initial probability distribution; The current code snippet is passed as input to the decoder of the Transformer model to generate a set of candidate tokens and their corresponding probability distributions for the current token position to be completed.
[0066] Optionally, the screening module is further configured to: Initialize the root node of the Monte Carlo tree search, which represents the current code snippet state or context information; For the token position to be completed, each candidate token generated by the Transformer model is treated as a candidate node in order according to the probability distribution; The Monte Carlo tree search is used to quickly evaluate the quality of the corresponding candidate tokens and the overall quality of their potential subsequent token combinations to obtain the evaluation results. Based on the evaluation results, the most promising candidate tokens are selected for expansion, new nodes are generated, and the statistical information of the Monte Carlo tree is updated; When the search termination condition is reached, the search is terminated, and the candidate node with the highest evaluation score is selected from the Monte Carlo tree, and the corresponding candidate token is used as the best code completion result for the current token position to be completed; For each token position to be completed in the code snippet, repeat the operation: generate candidate tokens through Transformer and evaluate and select the best token through Monte Carlo tree search until all positions are completed.
[0067] Optionally, the screening module is further configured to: Generate child nodes of the candidate node and initialize them; Starting from the candidate node, a random simulation is performed to generate subsequent token combinations, and each corresponding generated code snippet is evaluated through a reward function to obtain a reward value; The reward value obtained by simulation is back-propagated to each node on the search path to update the evaluation score and visit count of each node.
[0068] Optionally, the screening module is further configured to: According to the evaluation scores and visit times of the nodes in the Monte Carlo tree, as well as the exploration parameters, the most promising candidate tokens are selected and new nodes are generated, indicating the status of the code snippet after the corresponding candidate token is selected; Update the evaluation score and visit count of the new node, as well as relevant information about its parent node.
[0069] Optionally, the screening module is further configured to: After the search is terminated, the node sequence with the highest evaluation score is selected from the Monte Carlo tree to represent the path formed by selecting the most promising candidate token starting from the root node until a node representing the complete code snippet is reached; The filtered node sequence is output as the result of code completion.
[0070] Optionally, the screening module is further configured to: Collect high-quality code bases and developer feedback, analyze and extract multiple dimensions of code quality; Learn an embedding space for measuring code quality; Based on the learned embedding space, a multi-level reward function is designed to evaluate the position of the code snippet in the embedding space and give corresponding rewards based on its similarity with high-quality code snippets; For each generated code snippet, it is evaluated using the learned embedding space and reward function to obtain a reward value.
[0071] Optionally, the screening module is further configured to: Designing a neural network model, wherein the neural network model accepts the code snippet as input and outputs a high-dimensional vector; The collected high-quality and low-quality codes are used as training data, and the neural network model is trained through deep metric learning technology, where the training goal is to make the distance between high-quality code fragments in the embedding space close and the distance between low-quality code fragments in the embedding space far.
[0072] Optionally, the generation module is further configured to: Convert the current code snippet into a token sequence; Add special markers at the beginning and end of the token sequence; Assigning a position code to each token in the token sequence; Convert the encoded token sequence and position encoding into tensor form; Pass the input tensor to the decoder of the Transformer model, and based on the generated code and the information of the position to be completed, as well as the attention mechanism, generate a prediction of the current token position to be completed through the decoder, and output a corresponding probability distribution vector, where each element of the probability distribution vector corresponds to a token, which is used to represent the probability of each token appearing in the current context; According to the probability distribution vector, the first K tokens with the highest probabilities are selected as candidate tokens.
[0073] The above-mentioned embodiments only express several implementation methods of the present invention, and the description thereof is relatively specific and detailed, but it cannot be understood as limiting the scope of the patent of the present invention. It should be pointed out that, for ordinary technicians in this field, several variations and improvements can be made without departing from the concept of the present invention, which all belong to the protection scope of the present invention. Therefore, the protection scope of the patent of the present invention shall be subject to the attached claims.< / eos> < / bos> < / pad> < / mask>
Claims
1. A code completion method based on a large language model, characterized in that: The method comprises: Train a large language model for code completion based on the Transformer architecture and integrate quality assessment into the Transformer decoding phase; Input the code snippet to be completed and its context information into the large language model; The Transformer analyzes the context information of the code snippet to be completed, and generates a set of candidate tokens that match the context information and their corresponding probability distribution for each token position to be completed. In the decoding stage of Transformer, the quality assessment mechanism and Monte Carlo tree search technology are combined to quickly explore the candidate space and select the best token for code completion for each token position to be completed.
2. The code completion method based on a large language model according to claim 1, characterized in that: The Transformer analyzes the context information of the code snippet to be completed, and generates a set of candidate tokens matching the context information and their corresponding probability distribution for each token position to be completed, including: Initialize the Transformer model as a policy network to generate candidate tokens and their initial probability distribution; Pass the current code snippet as input to the decoder of the Transformer model to generate a set of candidate tokens and their corresponding probability distributions for the current token position to be completed; In the decoding phase of Transformer, the quality assessment mechanism and Monte Carlo tree search technology are combined to quickly explore the candidate space, and the best token is selected for code completion for each token position to be completed, including: Initialize the root node of the Monte Carlo tree search, which represents the current code snippet state or context information; For the token position to be completed, each candidate token generated by the Transformer model is treated as a candidate node in order according to the probability distribution; The Monte Carlo tree search is used to quickly evaluate the quality of the corresponding candidate tokens and the overall quality of their potential subsequent token combinations to obtain the evaluation results. Based on the evaluation results, the most promising candidate tokens are selected for expansion, new nodes are generated, and the statistical information of the Monte Carlo tree is updated; When the search termination condition is reached, the search is terminated, and the candidate node with the highest evaluation score is selected from the Monte Carlo tree, and the corresponding candidate token is used as the best code completion result for the current token position to be completed; For each token position to be completed in the code snippet, repeat the above steps: generate candidate tokens through Transformer and evaluate and select the best token through Monte Carlo tree search until all positions are completed.
3. The code completion method based on a large language model according to claim 2, characterized in that: The Monte Carlo tree search is used to quickly evaluate the quality of the corresponding candidate token and the overall quality of its potential subsequent token combination, including: Generate child nodes of the candidate node and initialize them; Starting from the candidate node, a random simulation is performed to generate subsequent token combinations, and each corresponding generated code snippet is evaluated through a reward function to obtain a reward value; The reward value obtained by simulation is back-propagated to each node on the search path to update the evaluation score and visit count of each node.
4. The code completion method based on a large language model according to claim 3, characterized in that: According to the evaluation results, the most promising candidate tokens are selected for expansion, new nodes are generated, and the statistical information of the Monte Carlo tree is updated, including: According to the evaluation scores and visit times of the nodes in the Monte Carlo tree, as well as the exploration parameters, the most promising candidate tokens are selected and new nodes are generated, indicating the status of the code snippet after the corresponding candidate token is selected; Update the evaluation score and visit count of the new node, as well as relevant information about its parent node.
5. The code completion method based on a large language model according to claim 2, characterized in that: The node sequence with the highest evaluation score is selected from the Monte Carlo tree and output as the result of code completion, including: After the search is terminated, the node sequence with the highest evaluation score is selected from the Monte Carlo tree to represent the path formed by selecting the most promising candidate token starting from the root node until a node representing the complete code snippet is reached; The filtered node sequence is output as the result of code completion.
6. The code completion method based on a large language model according to claim 3, characterized in that: Before obtaining the reward value by evaluating each code snippet generated by the reward function, the method further includes: Collect high-quality code bases and developer feedback, analyze and extract multiple dimensions of code quality; Learn an embedding space for measuring code quality; Based on the learned embedding space, a multi-level reward function is designed to evaluate the position of the code snippet in the embedding space and give corresponding rewards based on its similarity with high-quality code snippets; For each generated code snippet, it is evaluated using the learned embedding space and reward function to obtain a reward value.
7. The code completion method based on a large language model according to claim 6, characterized in that: The method learns an embedding space for measuring code quality, including: Designing a neural network model, wherein the neural network model accepts the code snippet as input and outputs a high-dimensional vector; The collected high-quality and low-quality codes are used as training data, and the neural network model is trained through deep metric learning technology, where the training goal is to make the distance between high-quality code fragments in the embedding space close and the distance between low-quality code fragments in the embedding space far.
8. The code completion method based on a large language model according to claim 2, characterized in that: The current code snippet is passed as input to the decoder of the Transformer model to generate a set of candidate tokens and their corresponding probability distributions for the current token position to be completed, including: Convert the current code snippet into a token sequence; Add special markers at the beginning and end of the token sequence; Assigning a position code to each token in the token sequence; Convert the encoded token sequence and position encoding into tensor form; Pass the input tensor to the decoder of the Transformer model, and based on the generated code and the information of the position to be completed, as well as the attention mechanism, generate a prediction of the current token position to be completed through the decoder, and output a corresponding probability distribution vector, where each element of the probability distribution vector corresponds to a token, which is used to represent the probability of each token appearing in the current context; According to the probability distribution vector, the first K tokens with the highest probabilities are selected as candidate tokens.
9. A code completion system based on a large language model, characterized in that: The system comprises: Model building module: used to train a large language model for code completion based on the Transformer architecture and integrate the quality assessment mechanism into the Transformer decoding stage; Input module: used to input the code snippet to be completed and its context information into the large language model; Generation module: used to analyze the context information of the code snippet to be completed through Transformer, and generate a set of candidate tokens matching the context information and their corresponding probability distribution for each token position to be completed; Screening module: It is used to quickly explore the candidate space in the decoding stage of Transformer by combining the quality assessment mechanism and Monte Carlo tree search technology, and select the best token for code completion for each token position to be completed.
Citation Information
Patent Citations
Keyword generation method based on Monte Carlo tree search, keyword generation model based on reinforcement learning and electronic equipment
CN110688450A
Enhanced Transform-based code completion method under lexical element granularity
CN116661797A
Code completion method based on artificial intelligence
CN118276913A
Code automatic completion method, system and device based on deep reinforcement learning
CN118519681A
Cited By
Large language model code generation system and method oriented to complex requirements
CN119938006A
Code dynamic completion method for large language model
CN120085873A
An Encoding Dynamic Completion Method for Large Language Models
CN120085873B
Code generation model training method and electronic equipment
CN120822039A