A code completion method based on retrieval enhancement and multimodality

By combining the code completion method of retrieval enhancement and multimodal features, the problems of path combination explosion and underutilization of grammatical structure and semantic features in the existing technology are solved, and a more efficient code completion effect is achieved.

CN119806502BActive Publication Date: 2025-09-09北京轻流智擎科技有限公司
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202411857009.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-17
Publication Date
2025-09-09
Estimated Expiration
2044-12-17

AI Technical Summary

Technical Problem

Existing code completion methods are prone to combinatorial explosion problems when extracting path features. They do not fully utilize the grammatical structure and semantic features of the source code, and do not use the similarities between codes to assist completion, resulting in low efficiency.

Method used

Combining retrieval enhancement and multimodal features, code completion results are generated by retrieving similar code snippets and the multimodal features of abstract syntax trees. The specific steps include building a code completion dataset, calculating semantic similarity using the CodeBERT model, screening candidate snippets using edit distance and cosine similarity, generating code completion results using CodeGPT, and extracting AST node sequence, path, and information-enhanced graph features for multimodal fusion.

Benefits of technology

It improves the efficiency of code completion, alleviates the problem of path combination explosion, fully utilizes the grammatical structure and semantic features of the source code, and enhances the accuracy and efficiency of code completion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119806502B_ABST
    Figure CN119806502B_ABST
Patent Text Reader

Abstract

The present invention belongs to the field of code completion technology, specifically a code completion method based on the combination of retrieval enhancement and multimodality, including the following specific steps: constructing a code completion data set, constructing a code completion model, dividing the code completion data set and training the code completion model, and applying the code completion model to perform code completion. The present invention performs retrieval enhancement on the code to be completed, retrieves similar code fragments to assist in completing the code to be completed, can make good use of the external similarity of the code, and improve the efficiency of code completion. Extracting the path features from the leaf nodes of the abstract syntax tree to the nodes to be completed can make good use of the grammatical structure information on the path, and can also alleviate the path explosion problem to a certain extent.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of code completion, and in particular to a code completion method based on the combination of retrieval enhancement and multimodality. Background Art

[0002] Code completion assists programmers with coding by automatically suggesting and completing code snippets, functions, methods, variables, and more relevant to the current context. Based on existing code, syntax rules, and contextual information, it predicts the code the programmer is likely to enter and provides options and suggestions that may be adopted. Code completion provides instant assistance during the coding process, reducing the workload of code entry, improving coding efficiency and accuracy, and alleviating the burden on developers. It has become one of the most commonly used operations among developers.

[0003] With the rapid development of big data and artificial intelligence, methods such as deep learning, natural language processing, and language models have promoted the intelligentization of code completion. Compared with traditional code completion methods, code completion methods based on language models have made significant progress. They can rank code completion candidates probabilistically and can complete multiple lines of code and method parameters simultaneously.

[0004] A computer program is composed of a series of characters. Therefore, the program code is converted into a character sequence or a token sequence, and then code completion is performed based on the above sequence.

[0005] Abstract Syntax Trees (ASTs) can extract grammatical structure information from source code. Compared to identifier sequence representations, ASTs retain richer grammatical and structural features and are a common method for representing code. In practical applications, ASTs are often traversed into node sequences and then modeled using deep learning models. While this approach leverages the grammatical and structural features provided by the AST, it often loses some key structural information, such as parent-child relationships.

[0006] Syntactic information helps complete the grammatical structure of the code; semantic information represents logic and can help discover the connection between automatically generated code and the completion context, and can also judge the quality of the completion results. Although some existing code completion methods consider the grammatical structure and semantic information of the source code, they do not fully extract the grammatical structure and semantic features of the source code.

[0007] Based on the above, existing code completion technologies have the following problems:

[0008] (1) Extracting path features from leaf nodes to leaf nodes of the abstract syntax tree to represent the code. As the complexity of the tree structure increases, the path combination explosion problem is likely to occur, which is not conducive to improving the efficiency of code completion.

[0009] (2) Although some existing code completion methods consider the grammatical structure and semantic information of the source code, they do not fully extract the grammatical structure and semantic features of the source code;

[0010] (3) Only the information of the code to be completed is used, and the similarity between codes is not used to assist in code completion, resulting in low code completion efficiency.

[0011] Therefore, to address the problems of path combination explosion when extracting path features, insufficient extraction of the grammatical structure and semantic features of the source code, and failure to utilize the similarity between codes to assist in code completion, a code completion method based on the combination of retrieval enhancement and multimodality is proposed. First, the code to be completed is search-enhanced to retrieve similar code fragments to assist in completing the code to be completed. The beneficial effect is to utilize the similarity of the code to improve the efficiency of code completion, thus solving the third problem mentioned above. Extracting the path features from the leaf nodes of the abstract syntax tree to the node to be completed can fully utilize the grammatical structure information on the path while reducing the number of paths, and can also alleviate the path explosion problem to a certain extent, that is, improve the first problem mentioned above. Adding multiple types of edges and child node types to the abstract syntax tree to generate an information enhancement graph to provide rich semantic information. At the same time, the multimodal features of the abstract syntax tree of the code to be completed are integrated to fully utilize the grammatical structure and semantic features of the source code for code completion, thus solving the second problem mentioned above. Summary of the Invention

[0012] In view of the above problems and / or the problems existing in the existing code completion method based on the combination of retrieval enhancement and multimodality, the present invention is proposed.

[0013] Therefore, the purpose of the present invention is to provide a code completion method based on the combination of retrieval enhancement and multimodality, which can solve the above-mentioned existing problems.

[0014] To solve the above technical problems, according to one aspect of the present invention, the present invention provides the following technical solutions:

[0015] A code completion method based on the combination of retrieval enhancement and multimodality includes the following specific steps:

[0016] S1: Construct code completion dataset;

[0017] S2: Build a code completion model;

[0018] S3: Divide the code completion dataset and train the code completion model;

[0019] S4: Apply the code completion model to complete the code.

[0020] As a preferred solution of the code completion method based on the combination of retrieval enhancement and multimodality described in the present invention, the specific step of S1 is: collecting a code completion dataset from various channels, where the dataset contains code snippets to be completed and the complete code after completion, so as to facilitate the training of the code completion model, wherein the complete code after completion is used to compare with the output results of the model during the training process.

[0021] As a preferred solution of the code completion method based on the combination of search enhancement and multimodality described in the present invention, the specific steps of S2 are as follows:

[0022] S21: Retrieve the code snippet m that is most similar to the code to be completed c, and calculate the semantic similarity between c and m;

[0023] S22: If the semantic similarity between the code to be completed c and the code snippet m is higher than the threshold, execute S221 to generate a code completion result; otherwise, execute S222 to generate a code completion result.

[0024] As a preferred solution of the code completion method based on the combination of search enhancement and multimodality described in the present invention, the specific steps of S21 are as follows:

[0025] S211: Divide the code in the source code database into code segments of equal length and use them as basic retrieval units, and then create a sequential index for each code segment, wherein the source code database is obtained from various existing public code libraries or is self-written;

[0026] S212: Using the BM25 algorithm, retrieve t code snippets similar to the code to be completed c from the source code database to form a first candidate code snippet set D1;

[0027] S213: Build a similar code retriever to retrieve n code snippets similar to the code to be completed c from D1 to form a second candidate code snippet set D2;

[0028] S214: Using the edit distance algorithm, select n code snippets similar to the code to be completed c from D1 to form a third candidate code snippet set D3;

[0029] S215: Filter out the code fragment m that is most similar to the code to be completed c from D2 and D3;

[0030] S216: Calculate the semantic similarity between the code to be completed c and the code fragment m.

[0031] As a preferred solution of a code completion method based on the combination of retrieval enhancement and multi-modal in the present invention, wherein: the similar code retriever in S213 is composed of two CodeBERT models. Among them, the first CodeBERT model is used to encode the code c to be completed, obtaining the representation vector of c, and the second CodeBERT model is used to encode the code fragments, obtaining the representation vectors of the code fragments; the specific steps are as follows: input the code c to be completed into the first CodeBERT model, obtaining the representation vector e c , input all the code fragments in D1 into the second CodeBERT model, obtaining the representation vectors {e d1 , e d2 , e d3 , …, e dt} of each code fragment in D1; finally, calculate the dot product of e c and each vector in {e d1 , e d2 , e d3 , …, e dt} and sort them, and select the top n code fragments with the highest ranking from the sorted result queue A t , denoted as the second candidate code fragment set D2, where n < t, and the values of n and t are determined during the experiment;

[0032] The specific steps of S214 are as follows: use the edit distance algorithm to calculate and sort the string similarity between c and the code fragments in D1, and select the top n code fragments with the highest ranking from the sorted result queue B t , denoted as the third candidate fragment set D3, and use sim1(c, h) to represent the string similarity between c and a certain code fragment h:

[0033] sim1(c, h) = L[c l [h l

[0034]

[0035] Among them, the two-dimensional array L is used to record the edit distance size, c l represents the length of the code c to be completed, and h l represents the length of the code fragment h;

[0036] The specific steps of S215 are as follows: according to the sorted result queues A t and B t , calculate the comprehensive ranking of each code fragment in D2 and D3, and take the code fragment with the highest comprehensive ranking as the code fragment m most similar to c. Among them, denote the ranking of a certain code fragment in A​t and B t The rankings in are k1 and k2, then the comprehensive ranking of its code snippet is where ρ1 and ρ2 are the equilibrium code snippet in A t and B t The weight parameter in ;

[0037] The specific steps of S216 are: combining cosine similarity and string similarity to calculate the semantic similarity S between c and m. The calculation formula of the semantic similarity S is:

[0038]

[0039]

[0040] Among them, sim1(c,m) represents the string similarity between c and m calculated by the edit distance algorithm, sim2(c,m) represents the cosine similarity between c and m, λ1 and λ2 are weight parameters for balancing string similarity and cosine similarity, e m is the representation vector of code snippet m, d is the representation vector e c and e m In addition, semantic similarity refers to combining cosine similarity and string similarity to measure the similarity between the code to be completed and a certain code snippet.

[0041] As a preferred solution of the code completion method based on the combination of search enhancement and multimodality described in the present invention, the specific step of S221 is: using the completion code generator to generate the code completion result, which is as follows:

[0042] If the semantic similarity between the code to be completed c and the code fragment m is higher than the threshold, the next code fragment m1 of m is obtained from the source code database according to the index value of m, and m and m1 are concatenated to obtain m'=[m,m1], where m' can assist in completing c; then c and m' are concatenated to obtain x'=[c,m'], and x' is used as the input of the completion code generator, where the completion code generator is built by the CodeGPT model, and finally the code completion result is output.

[0043] As a preferred solution of the code completion method based on the combination of retrieval enhancement and multimodality described in the present invention, the specific step of S222 is: extracting multimodal features of the code to be completed c and generating a code completion result, as follows:

[0044] If the semantic similarity between the code to be completed c and the code snippet m is lower than the threshold, the multimodal features of the code to be completed c are extracted, including the AST node sequence features of c, the path features from the AST leaf node to the node to be completed, and the information enhancement graph features. The multimodal features are fused and the code completion result is generated.

[0045] As a preferred solution of the code completion method based on the combination of retrieval enhancement and multimodality described in the present invention, wherein: the AST node sequence feature F of the code to be completed c is extracted in S222 c The specific steps are as follows:

[0046] S22211: First, construct the AST of the code to be completed c. Then, according to the ASDL rules, add corresponding types to the nodes in the AST to provide more semantic information. Finally, a type-enhanced AST is obtained, in which the mod type is added to the Module node of the AST; the stmt type is added to the FunctionDef, Return, Assign, For, while, and If nodes; the expr type is added to the BinOp node; in addition, in the leaf nodes of the AST, the operator type is added to the operator node; and the identifier type is added to the identifier node.

[0047] S22212: Obtain an AST node sequence by performing a pre-order traversal on the TA-AST;

[0048] S22213: Use the Imformer model to extract AST node sequence features. The AST node sequence first passes through multiple attention heads to calculate the attention of each position in the sequence to other positions, then passes through the RMS Norm layer for normalization, and then passes through the residual connection and the block feedforward neural network to enhance the feature representation of the AST node sequence. Finally, the AST node sequence feature F is output through the RMS Norm layer and the residual connection. c , where F c is a feature vector of size 1×512; the multiple attention heads include standard attention heads, sentence-guided attention heads, and parent-child attention heads; RMS Norm is a lightweight normalization layer, and the block-wise feedforward neural network divides the input sequence into blocks by length and calculates each block separately, thereby reducing memory requirements while maintaining computational efficiency; and assuming that the AST node sequence X = {x1, x2, x3, ... x r …,x z}, where z is the length of the AST node sequence, x r The rth token in the AST node sequence;

[0049] The S222 extracts the path feature F from the leaf node of the AST of the code to be completed c to the node to be completed p The specific steps are as follows:

[0050] S22221: Create a path set P = {p1, p2, p3, ... p v …,p u}, where P contains all the paths from the leaf nodes in the AST of the code to be completed c to the node to be completed, u is the number of paths, and p v is the vth path in P;

[0051] S22222: Use multi-layer GRU to extract path p v The local path features of the multi-layer GRU are the path p v The corresponding path sequence; let the hidden state of GRU at time t be h t , introduce contextual attention to calculate h t Compared with the hidden states h1~h1 at the previous time 1~t-1 t-1 The relationship between t , then through α t Calculate the context vector c t :

[0052] α t =softmax(W T tanh(W h1 [h1,h2,…,h t-1 ]+W h2 h t ))

[0053]

[0054] Among them, W, W h1 、W h2 is the learnable parameter matrix;

[0055] Path p v Local path characteristics Expressed as:

[0056]

[0057] Among them, W f is the learnable parameter matrix;

[0058] S22223: Extract path p vThe global path features of the code to be completed are first constructed, where each node in the CFG corresponds to a statement of the code to be completed c. Node2Vec is used to calculate the feature vector corresponding to each node in the CFG. In this process, the path p v There may be multiple statements in the AST, where one statement corresponds to multiple AST nodes. As long as the path passes through one or more nodes in the AST corresponding to a statement, it is considered to have passed through its statement; therefore, the path p v Mapped to a set of statements {s1,s2,…s i …,s k}, k is the number of statements, let statement s i The feature vector corresponding to the node in CFG is v i , then the path p v Global path characteristics of It can be expressed as:

[0059]

[0060] Among them, avg is the average value operation;

[0061] S22224: path p v The characteristic f i Its local path characteristics and global path features The dot product of:

[0062]

[0063] Then the path set P={p1,p2,p3,…p v …,p u The characteristics of each path in} can be expressed as F = {f1,f2,f3,…f v …,f u};

[0064] S22225: Fusion of multiple path features to obtain the path feature F from the leaf node of AST to the node to be completed p :

[0065] F p =avg(f1,f2,f3,…f v …,f u )

[0066] Among them, avg is the average operation, F p It is a feature vector of size 1×512;

[0067] The information enhancement graph feature F of the AST of the code to be completed c is extracted in S222 gThe specific steps are as follows:

[0068] S22231, structural information enhancement map:

[0069] S222311: Use the fields in the ASDL rules to add type information to the edges of TA-AST, where the type information represents the relationship between the nodes at both ends of the edge, wherein a function body edge "body" is added between the Modle node and the FunctionDef node, a function body edge "body" is also added between the FunctionDef node and the Assign node and the Return node, a function name edge "name" is added between the FunctionDef node and the identifier node, a function parameter edge "args" is added between the FunctionDef node and the Args node, a parameter edge "arg" is added between the Args node and the identifier node, a target edge "targets" is added between the Assign node and the identifier node, a value edge "value" is added between the Assign node and the BinOp node, calculation edges "left" and "right" are added between the BinOp node and the left and right identifier nodes, and an operator edge "op" is added between the BinOp node and the operator node;

[0070] S222312: Next, add the next token edge nextToken to the TA-AST. The edge points from one leaf node to another leaf node according to the text order of the code to be completed c. At the same time, add the assignment edge assign_edge to indicate that the value of one identifier is assigned to another identifier.

[0071] S222313: Then, according to the camel case naming rule, the leaf nodes in TA-AST are split into multiple child nodes by value, which can solve the super vocabulary problem to a certain extent. At the same time, a new node type subtoken and child node edge subToken_of are introduced in TA-AST;

[0072] S222314: Finally, the information enhancement graph of AST is formed;

[0073] S22232, use the HetSheaf model to extract the features of each node in the information enhancement graph:

[0074] S222321: Feature embedding for nodes and edges in information-enhanced graphs:

[0075] F' n =ReLU(W n ·F n +b n )

[0076] F' e =ReLU(W e·F e +b e )

[0077] Among them, F' n is the node feature after embedding, F' e is the edge feature after embedding, ReLU is the activation function, F n is the initialized node feature, F e is the initialized edge feature, W n and W e is the weight parameter, b n and b e is the bias value;

[0078] S222322: Pass messages between nodes and update node characteristics:

[0079]

[0080] Among them, N(i) is the neighbor set of node i, and are the characteristics of node i and node j, is the feature of the edge between node i and node j, W e1 and W e2 is the weight parameter, b' is the bias value;

[0081] S222323: Use the average aggregation function to fuse the features of each node to obtain the information enhanced graph feature F g , where F g It is a feature vector of size 1×512;

[0082] The specific steps of fusing the multimodal features of the code to be completed c in S222 to generate the code completion result are as follows:

[0083] S22241: Get the multimodal feature F of the code to be completed c c , F p and F g After that, F c , F p and F g The fusion is performed and the probability of generating a token is calculated through the softmax normalization function:

[0084] softmax(W c (αF c +βF p +γF g ))

[0085] Among them, W c is the learnable parameter matrix, α, β and γ are F c , Fp and F g The scaling factor of F is α+β+γ=1 and α, β, γ∈(0~1), and F is adjusted by dynamically adjusting α, β and γ during model training. c , F p and F g The weight of

[0086] S22242: Use the token with the highest probability as the code completion result.

[0087] As a preferred solution of the code completion method based on the combination of retrieval enhancement and multimodality described in the present invention, the specific steps of S3 are: dividing the code completion dataset into a training dataset, a validation dataset, and a test dataset, and then training the code completion model; first setting the semantic similarity threshold between the completed code and the code snippet m, then randomly initializing the model weight parameters and setting the model hyperparameters, then selecting a loss function and using an optimization algorithm to update the parameters of the code completion model, and continuously evaluating the performance of the code completion model during the training process to timely adjust the model structure and hyperparameters so that the model can converge quickly, and finally obtaining a trained code completion model.

[0088] As a preferred solution of the code completion method based on the combination of retrieval enhancement and multimodality described in the present invention, the specific step of S4 is: applying the trained code completion model to the code completion task, automatically completing the code to be completed c, and improving the efficiency of programmers in writing code.

[0089] Compared with existing technologies:

[0090] The present invention performs retrieval enhancement on the code to be completed, retrieves similar code fragments to assist in completing the code to be completed, and can make good use of the external similarity of the code to improve the efficiency of code completion. Extracting the path features from the leaf nodes of the abstract syntax tree to the node to be completed can make good use of the grammatical structure information on the path, and can also alleviate the path explosion problem to a certain extent. Adding multiple edge types and child node types to the abstract syntax tree to generate an information enhancement graph to provide rich semantic information, and at the same time fusing the multimodal features of the abstract syntax tree of the code to be completed, can make full use of the grammatical structure features and semantic features of the source code to complete the code. BRIEF DESCRIPTION OF THE DRAWINGS

[0091] Figure 1 It is a schematic diagram of the process of the present invention;

[0092] Figure 2 This is a schematic diagram of the code completion model structure of the present invention;

[0093] Figure 3Add type diagram for AST node of the present invention;

[0094] Figure 4 This is a schematic diagram of the Imformer model structure of the present invention;

[0095] Figure 5 This is a schematic diagram of information enhancement of AST of the present invention. DETAILED DESCRIPTION

[0096] To make the objectives, technical solutions and advantages of the present invention more clear, the embodiments of the present invention will be described in further detail below with reference to the accompanying drawings.

[0097] The present invention provides a code completion method based on the combination of retrieval enhancement and multimodality, please refer to Figure 1-Figure 5 , including a dataset construction module, a code completion model construction module, a model training module and a model application module. First, a code completion dataset is constructed for model training; then a code completion model is constructed, and the code to be completed is retrieved and enhanced. The code snippet m that is most similar to the code to be completed is retrieved through a similar code retriever and an edit distance algorithm. Then, the semantic similarity between the code to be completed and the code snippet m is calculated. If the semantic similarity is higher than a threshold, the code to be completed and m are concatenated and input into the completion code generator to generate the code completion result. Otherwise, the multimodal features of the code to be completed are extracted, including the AST node sequence features of the code to be completed, the path features from the leaf nodes of the AST to the nodes to be completed, and the information enhancement graph features. These features are fused to generate the code completion result; then the code completion dataset is divided and the code completion model is trained. Finally, the code completion model is applied for code completion. The code completion method flow is as follows: Figure 1 As shown; and the specific steps are as follows:

[0098] The specific steps are as follows:

[0099] S1: Construct code completion dataset;

[0100] The specific steps of S1 are: collecting code completion datasets from various channels. The datasets contain code snippets to be completed and the complete code after completion, so as to facilitate the training of the code completion model. The complete code after completion is used to compare with the output results of the model during the training process;

[0101] S2: Build a code completion model;

[0102] The structure of the code completion model is as follows Figure 2As shown in the figure, first, the code to be completed c is searched and enhanced. The code snippet m that is most similar to the code to be completed c is retrieved through a similar code retriever and an edit distance algorithm. Then, the semantic similarity between c and m is calculated. If the semantic similarity is higher than a threshold, c and m are concatenated and input into the completion code generator to generate the code completion result. Otherwise, the multimodal features of the code to be completed c are extracted, including the AST node sequence features of the code to be completed c, the path features from the AST leaf nodes to the nodes to be completed, and the information enhancement graph features. These features are fused to generate the code completion result.

[0103] The specific steps of S2 are as follows:

[0104] S21: Retrieve the code snippet m that is most similar to the code to be completed c, and calculate the semantic similarity between c and m;

[0105] The specific steps of S21 are as follows:

[0106] S211: Divide the code in the source code database into code segments of equal length and use them as basic retrieval units, and then create a sequential index for each code segment, wherein the source code database is obtained from various existing public code libraries or is self-written;

[0107] S212: Using the BM25 algorithm, t code snippets similar to the code to be completed c are retrieved from the source code database to form a first candidate code snippet set D1. BM25 (Best Matching 25) is an information retrieval algorithm that can be used to calculate the relevance between the query and the code.

[0108] S213: Build a similar code retriever to retrieve n code snippets similar to the code to be completed c from D1 to form a second candidate code snippet set D2;

[0109] The similar code retriever in S213 is composed of two CodeBERT models, wherein the first CodeBERT model is used to encode the code to be completed c to obtain the representation vector of c, and the second CodeBERT model is used to encode the code fragment to obtain the representation vector of the code fragment; the specific steps are: input the code to be completed c into the first CodeBERT model, and obtain the representation vector e of c c , input all code snippets in D1 into the second CodeBERT model, and obtain the representation vector {e d1 ,e d2 ,e d3 ,…,e dt}; Finally, calculate e separately c With {e d1 ,ed2 , e d3 , …, e dt Take the dot product of each vector in} and sort them, and select the top n code snippets with the highest rankings from the sorted result queue A t and denote them as the second candidate code snippet set D2, where n < t, and the values of n and t are determined during the experiment; its CodeBERT model is a pre-trained model based on the Transformer architecture, specifically designed to process program code and natural language;

[0110] S214: Use the edit distance algorithm to select n code snippets similar to the to-be-completed code c from D1 to form the third candidate code snippet set D3; its edit distance algorithm is a publicly available method for measuring the similarity between two strings;

[0111] Specifically, the steps of S214 are: Use the edit distance algorithm to calculate the string similarity between c and the code snippets in D1 and sort them, and select the top n code snippets with the highest rankings from the sorted result queue B t and denote them as the third candidate snippet set D3. Use sim1(c, h) to represent the string similarity between c and a certain code snippet h:

[0112] sim1(c, h) = L[c l [g l

[0113]

[0114] where the two-dimensional array L is used to record the edit distance. c l represents the length of the to-be-completed code c, and h l represents the length of the code snippet h;

[0115] S215: Select the code snippet m that is most similar to the to-be-completed code c from D2 and D3;

[0116] Specifically, the steps of S215 are: According to the sorted result queues A t and B t , calculate the comprehensive rankings of each code snippet in D2 and D3, and select the code snippet with the highest comprehensive ranking as the code snippet m that is most similar to c. Denote the rankings of a certain code snippet in A t and B t as k1 and k2, then the comprehensive ranking of its code snippet is where ρ1 and ρ2 are weight parameters that balance the code snippets in A t and B t ;

[0117] ​S216: Calculate the semantic similarity between the code to be completed c and the code fragment m;

[0118] The specific steps of S216 are: combining cosine similarity and string similarity to calculate the semantic similarity S between c and m. The calculation formula of the semantic similarity S is:

[0119]

[0120] Among them, sim1(c,m) represents the string similarity between c and m calculated by the edit distance algorithm, sim2(c,m) represents the cosine similarity between c and m, λ1 and λ2 are weight parameters for balancing string similarity and cosine similarity, e m is the representation vector of code snippet m, d is the representation vector e c and e m In addition, semantic similarity refers to combining cosine similarity and string similarity to measure the similarity between the code to be completed and a code snippet;

[0121] Cosine similarity: It measures the difference between two vectors by the cosine value of the angle between them in a vector space. The closer the cosine value is to 1, the closer the angle is to 0, indicating that the two vectors are more similar. The closer the cosine value is to 0, the closer the angle is to 90 degrees, indicating that the two vectors are less similar. Suppose vectors a = (x1, y1) and b = (x2, y2), then the cosine similarity calculation formula of a and b is as follows:

[0122]

[0123] Where d is the distance between vector a and vector b, and the length of vector c in the rectangular coordinate system is:

[0124]

[0125] The cosine calculation formula can be simplified to:

[0126]

[0127] S22: If the semantic similarity between the code to be completed c and the code snippet m is higher than the threshold, execute S221 to generate a code completion result; otherwise, execute S222 to generate a code completion result;

[0128] The specific steps of S221 are: using the completion code generator to generate the code completion result, as follows:

[0129] If the semantic similarity between the code to be completed c and the code snippet m is higher than a threshold, the next code snippet m1 of m is obtained from the source code database based on the index value of m. m and m1 are concatenated to obtain m' = [m, m1], where m' can assist in completing c. Then c and m' are concatenated to obtain x' = [c, m'], which is used as the input of the completion code generator, where the completion code generator is built using the CodeGPT model. Finally, the code completion result is output; the CodeGPT model is a pre-trained language model based on the GPT architecture, which is specially optimized for programming tasks.

[0130] The specific steps of S222 are: extracting the multimodal features of the code to be completed c and generating the code completion result, as follows:

[0131] If the semantic similarity between the code to be completed c and the code snippet m is lower than the threshold, the multimodal features of the code to be completed c are extracted, including the AST node sequence features of c, the path features from the AST leaf node to the node to be completed, and the information enhancement graph features. The multimodal features are fused to generate the code completion result;

[0132] Among them, S222 extracts the AST node sequence feature F of the code to be completed c c The specific steps are as follows:

[0133] S22211: First, construct the AST of the code to be completed c. Then, according to the ASDL rules, add corresponding types to the nodes in the AST to provide more semantic information. Finally, a type-augmented AST (TA-AST) is obtained, in which the mod type is added to the Module node of the AST; the stmt type is added to the FunctionDef, Return, Assign, For, while, and If nodes; and the expr type is added to the BinOp node. In addition, in the leaf nodes of the AST, the operator type is added to the operator node; and the identifier type is added to the identifier node. Its ASDL (Abstract Syntax Description Language) rule is a rule used to describe the syntax of a programming language, and is often used to declare data types, structures, and relationships. The ASDL rule contains information such as constructors, node types, and fields, where the fields specify the relationship between the parent class and each of its subclasses.

[0134] like Figure 3 As shown, after converting the code c to be completed into AST, add types to its nodes. In this way, the nodes in TA-AST can be expressed in the form of "type: value", where "____" is the node to be completed;

[0135] S22212: Obtain an AST node sequence by performing a pre-order traversal on the TA-AST; for example, Figure 3 Perform a pre-order traversal of the TA-AST in the function, and finally obtain an AST node sequence "Mod:Module,stmt:FunctionDef,identifier:foo,argments:args,identifier:intMax,identifier:intMin,……,stmt:Return,____";

[0136] S22213: Use the Imformer model to extract AST node sequence features. The model structure diagram is as follows Figure 4 As shown in the figure, the AST node sequence first passes through multiple attention heads to calculate the attention of each position in the sequence to other positions, then passes through the RMS Norm layer for normalization, passes through the residual connection and then passes through the block feedforward neural network to enhance the feature representation of the AST node sequence, and finally passes through the RMS Norm layer and residual connection to output the AST node sequence feature F c , where F c is a feature vector of size 1×512; the multiple attention heads include standard attention heads, sentence-guided attention heads, and parent-child attention heads; RMS Norm is a lightweight normalization layer, and the block-wise feedforward neural network divides the input sequence into blocks by length and calculates each block separately, thereby reducing memory requirements while maintaining computational efficiency; and let the AST node sequence X = {x1, x2, x3, ...x r …,x z}, where z is the length of the AST node sequence, x r The rth token in the AST node sequence;

[0137] in:

[0138] (1) A standard self-attention mechanism is used in the standard attention head (here we only apply the on-site self-attention mechanism method), allowing the model to dynamically pay attention to other positions in the entire sequence while processing information at each position;

[0139] (2) Introduce the sentence to guide the attention head to determine x i with x j Is it from the same statement; create a matrix S n ×n , if x i with x j From the same statement, then s ij =0, otherwise s ij = -∞, where s ijRepresents the value of the i-th row and j-th column of the matrix S, then the statement guides the attention head s It can be expressed as:

[0140]

[0141] Where Q, K, V are query matrix, key matrix and value matrix, is the scaling factor, and softmax is the normalization function;

[0142] (3) Introduce the parent-child attention head to determine x i with x j Is there a parent-child relationship? Create a matrix T n×n , if x i with x j If there is a parent-child relationship, then t ij =0, otherwise t ij = -∞, where t ij Represents the value of the i-th row and j-th column of matrix T, then the parent-child attention head t It can be expressed as:

[0143]

[0144] The S222 extracts the path feature F from the leaf node of the AST of the code to be completed c to the node to be completed p The specific steps are as follows:

[0145] S22221: Create a path set P = {p1, p2, p3, ... p v …,p u}, where P contains all the paths from the leaf nodes in the AST of the code to be completed c to the node to be completed, u is the number of paths, and p v is the vth path in P;

[0146] S22222: Use multi-layer GRU to extract path p v The local path features of the multi-layer GRU are the path p v The corresponding path sequence; let the hidden state of GRU at time t be h t , introduce contextual attention to calculate h t Compared with the hidden states h1~h1 at the previous time 1~t-1 t-1 The relationship between t , then through α t Calculate the context vector c t :

[0147] α t =softmax(W Ttanh(W h1 [h1,h2,…,h t-1 ]+W h2 h t ))

[0148]

[0149] Among them, W, W h1 、W h2 is the learnable parameter matrix;

[0150] Path p v Local path characteristics Expressed as:

[0151]

[0152] Among them, W f is the learnable parameter matrix;

[0153] Its gated recurrent unit (GRU) is a variant of recurrent neural network (RNN) for processing sequence data. It aims to address the shortcomings of traditional RNN in capturing long-term dependencies. GRU is relatively simple and computationally efficient.

[0154] S22223: Extract path p v The global path features of the code to be completed are first constructed, where each node in the CFG corresponds to a statement of the code to be completed c. Node2Vec is used to calculate the feature vector corresponding to each node in the CFG. In this process, the path p v There may be multiple statements in the AST, such as Figure 3 The path from the leaf node intGap of the AST to the node to be completed passes through the statements "intGap = intMax - intMin" and "return____"; one statement corresponds to multiple AST nodes, and as long as the path passes through one or more nodes in the AST corresponding to a statement, it is considered to have passed through its statement; therefore, the path p v Mapped to a set of statements {s1,s2,…s i …,s k}, k is the number of statements, let statement s i The feature vector corresponding to the node in CFG is v i , then the path p v Global path characteristics of It can be expressed as:

[0155]

[0156] Among them, avg is the average value operation;

[0157] Its Node2Vec is a node embedding algorithm for graph-structured data, which converts nodes into low-dimensional vector representations;

[0158] S22224: path p v The characteristic f i Its local path characteristics and global path features The dot product of:

[0159]

[0160] Then the path set P={p1,p2,p3,…p v …,p u The characteristics of each path in} can be expressed as F = {f1,f2,f3,…f v …,f u};

[0161] S22225: Fusion of multiple path features to obtain the path feature F from the leaf node of AST to the node to be completed p :

[0162] F p =avg(f1,f2,f3,…f v …,f u )

[0163] Among them, avg is the average operation, F p It is a feature vector of size 1×512;

[0164] The information enhancement graph feature F of the AST of the code to be completed c is extracted in S222 g The specific steps are as follows:

[0165] S22231, structural information enhancement map:

[0166] S222311: Use the fields in the ASDL rules to add type information to the edges of TA-AST, where the type information represents the relationship between the nodes at both ends of the edge, wherein a function body edge "body" is added between the Modle node and the FunctionDef node, a function body edge "body" is also added between the FunctionDef node and the Assign node and the Return node, a function name edge "name" is added between the FunctionDef node and the identifier node, a function parameter edge "args" is added between the FunctionDef node and the Args node, a parameter edge "arg" is added between the Args node and the identifier node, a target edge "targets" is added between the Assign node and the identifier node, a value edge "value" is added between the Assign node and the BinOp node, calculation edges "left" and "right" are added between the BinOp node and the left and right identifier nodes, and an operator edge "op" is added between the BinOp node and the operator node;

[0167] S222312: Next, add the next token edge nextToken to the TA-AST. The edge points from one leaf node to another leaf node according to the text order of the code to be completed c. At the same time, add the assignment edge assign_edge to indicate that the value of one identifier is assigned to another identifier.

[0168] S222313: Then, according to the camel case naming convention, the leaf nodes in TA-AST are split into multiple child nodes by value to partially solve the out-of-vocabulary problem. At the same time, a new node type subtoken and child node edge subToken_of are introduced in TA-AST. The out-of-vocabulary problem (OOV) refers to the problem in natural language processing (NLP) tasks where the model encounters words or symbols that it has not seen during training, resulting in the inability to correctly process these new words.

[0169] S222314: Finally formed as Figure 5 The information-enhanced graph of the AST shown;

[0170] S22232, use the HetSheaf model to extract the features of each node in the information enhancement graph:

[0171] S222321: Feature embedding for nodes and edges in information-enhanced graphs:

[0172] F' n =ReLU(W n ·F n +b n )

[0173] F'e =ReLU(W e ·F e +b e )

[0174] Among them, F' n is the node feature after embedding, F' e is the edge feature after embedding, ReLU is the activation function, F n is the initialized node feature, F e is the initialized edge feature, W n and W e is the weight parameter, b n and b e is the bias value;

[0175] S222322: Pass messages between nodes and update node characteristics:

[0176]

[0177] Among them, N(i) is the neighbor set of node i, and are the characteristics of node i and node j, is the feature of the edge between node i and node j, W e1 and W e2 is the weight parameter, b' is the bias value;

[0178] S222323: Use the average aggregation function to fuse the features of each node to obtain the information enhanced graph feature F g , where F g It is a feature vector of size 1×512;

[0179] Its HetSheaf model (heterogeneous layer neural network) is designed to handle heterogeneous graphs containing different types of nodes and edges. It uses cellular sheaves to directly model the heterogeneity of the graph, encoding different data types in the graph structure without the need for a complex network architecture. The average aggregation function is to take the average of the features of each node;

[0180] The specific steps of fusing the multimodal features of the code to be completed c in S222 to generate the code completion result are as follows:

[0181] S22241: Get the multimodal feature F of the code to be completed c c , F p and F g After that, F c , F p and F g The fusion is performed and the probability of generating a token is calculated through the softmax normalization function:

[0182] softmax(W c (αF c +βF p +γF g ))

[0183] Among them, W c is the learnable parameter matrix, α, β and γ are F c , F p and F g The scaling factor is α+β+γ=1 and α,β,γ∈(0~1), and F is adjusted by dynamically adjusting α, β and γ during model training. c , F p and F g The weight of

[0184] S22242: Use the token with the highest probability as the code completion result;

[0185] S3: Divide the code completion dataset and train the code completion model;

[0186] The specific steps of S3 are as follows: divide the code completion dataset into training, validation, and test datasets, and then train the code completion model; first, set the semantic similarity threshold between the completed code and the code snippet m, then randomly initialize the model weight parameters and set the model hyperparameters; then select a loss function and use an optimization algorithm to update the parameters of the code completion model. Meanwhile, the performance of the code completion model is continuously evaluated during training to timely adjust the model structure and hyperparameters to ensure rapid model convergence, ultimately obtaining a trained code completion model;

[0187] The specific embodiment of this step is:

[0188] Example: A code completion dataset is divided into a training dataset, a validation dataset, and a test dataset in a ratio of 8:1:1, and then a code completion model is trained. First, the semantic similarity threshold between the code to be completed c and the code snippet m is set to 0.6. Then, the model weight parameters and model hyperparameters are randomly initialized, and the cross entropy is used as the loss function. Then, a stochastic gradient descent optimization algorithm is selected to update the parameters of the code completion model. During the training process, the validation dataset is periodically used to evaluate the performance of the code completion model, so that the model structure and hyperparameters can be adjusted in a timely manner to enable the model to converge quickly. When the code completion model is trained until the loss value tends to converge, the training ends, and a trained code completion model is obtained.

[0189] S4: Apply the code completion model to complete the code;

[0190] Among them, the specific steps of S4 are: applying the trained code completion model to the code completion task, automatically completing the code to be completed c, and improving the efficiency of programmers in writing code.

[0191] Although the present invention has been described above with reference to embodiments, various modifications may be made thereto and equivalent components may be substituted without departing from the scope of the present invention. In particular, as long as there are no structural conflicts, the various features of the embodiments disclosed herein may be combined with each other in any manner, and the omission of an exhaustive description of such combinations in this specification is solely for the sake of space and resource conservation. Therefore, the present invention is not limited to the specific embodiments disclosed herein, but includes all technical solutions falling within the scope of the claims.

Claims

1. A code completion method based on the combination of retrieval enhancement and multimodality, characterized in that: The specific steps are as follows: S1: Construct code completion dataset; S2: Build a code completion model; S3: Divide the code completion dataset and train the code completion model; S4: Apply the code completion model to complete the code; The specific steps of S2 are as follows: S21: Retrieve the code snippet m that is most similar to the code to be completed c, and calculate the semantic similarity between c and m; S22: If the semantic similarity between the code to be completed c and the code snippet m is higher than the threshold, execute S221 to generate a code completion result; otherwise, execute S222 to generate a code completion result; The specific steps of S221 are: using the completion code generator to generate the code completion result, as follows: If the semantic similarity between the code to be completed c and the code fragment m is higher than the threshold, the next code fragment m1 of m is obtained from the source code database according to the index value of m, and m and m1 are concatenated to obtain m. ' =[m,m1], where m ' Can assist c to complete; then splice c and m ' Get x ' =[c,m ' ], change x ' As the input of the completion code generator, where the completion code generator is built by the CodeGPT model and finally outputs the code completion result; The specific steps of S222 are: extracting the multimodal features of the code to be completed c and generating the code completion result, as follows: If the semantic similarity between the code to be completed c and the code snippet m is lower than the threshold, the multimodal features of the code to be completed c are extracted, including the AST node sequence features of c, the path features from the AST leaf node to the node to be completed, and the information enhancement graph features; The specific steps of extracting the information enhancement graph feature Fg of the AST of the code to be completed c in S222 are as follows: S22231, structural information enhancement map: S222311: Use the fields in the ASDL rules to add type information to the edges of TA-AST, where the type information represents the relationship between the nodes at both ends of the edge, wherein a function body edge "body" is added between the Modle node and the FunctionDef node, a function body edge "body" is also added between the FunctionDef node and the Assign node and the Return node, a function name edge "name" is added between the FunctionDef node and the identifier node, a function parameter edge "args" is added between the FunctionDef node and the Args node, a parameter edge "arg" is added between the Args node and the identifier node, a target edge "targets" is added between the Assign node and the identifier node, a value edge "value" is added between the Assign node and the BinOp node, calculation edges "left" and "right" are added between the BinOp node and the left and right identifier nodes, and an operator edge "op" is added between the BinOp node and the operator node; S222312: Next, add the next token edge nextToken to the TA-AST. The edge points from one leaf node to another leaf node according to the text order of the code to be completed c. At the same time, add the assignment edge assign_edge to indicate that the value of one identifier is assigned to another identifier. S222313: Then, according to the camel case naming rule, the leaf nodes in TA-AST are split into multiple child nodes by value, which can solve the super vocabulary problem to a certain extent. At the same time, a new node type subtoken and child node edge subToken_of are introduced in TA-AST; S222314: Finally, the information enhancement graph of AST is formed; S22232, use the HetSheaf model to extract the features of each node in the information enhancement graph: S222321: Feature embedding for nodes and edges in information-enhanced graphs: F' n =ReLU(W n ·F n +b n ) F' e =ReLU(W e ·F e +b e ) Among them, F' n is the node feature after embedding, F' e is the edge feature after embedding, ReLU is the activation function, F n is the initialized node feature, F e is the initialized edge feature, W n and W e is the weight parameter, b n and b e is the bias value; S222322: Pass messages between nodes and update node characteristics: Among them, N(i) is the neighbor set of node i, and are the characteristics of node i and node j, is the feature of the edge between node i and node j, W e1 and W e2 is the weight parameter, b' is the bias value; S222323: Use the average aggregation function to fuse the features of each node to obtain the information enhanced graph feature F g ; Among them, F g It is a feature vector of size 1×512; Fuse multimodal features and generate code completion results.

2. The code completion method based on the combination of retrieval enhancement and multimodality according to claim 1, characterized in that: The specific steps of S1 are: collecting code completion datasets from various channels, where the datasets contain code snippets to be completed and the complete code after completion, so as to facilitate the training of the code completion model, wherein the complete code after completion is used to compare with the output results of the model during the training process.

3. The code completion method based on the combination of search enhancement and multimodality according to claim 1, characterized in that: The specific steps of S21 are as follows: S211: Divide the code in the source code database into code segments of equal length and use them as basic retrieval units, and then create a sequential index for each code segment, wherein the source code database is obtained from various existing public code libraries or is self-written; S212: Using the BM25 algorithm, retrieve t code snippets similar to the code to be completed c from the source code database to form a first candidate code snippet set D1; S213: Build a similar code retriever to retrieve n code snippets similar to the code to be completed c from D1 to form a second candidate code snippet set D2; S214: Using the edit distance algorithm, select n code snippets similar to the code to be completed c from D1 to form a third candidate code snippet set D3; S215: Filter out the code fragment m that is most similar to the code to be completed c from D2 and D3; S216: Calculate the semantic similarity between the code to be completed c and the code fragment m.

4. The code completion method based on the combination of retrieval enhancement and multimodality according to claim 3, characterized in that: The similar code retriever in S213 consists of two CodeBERT models. Among them, the first CodeBERT model is used to encode the to-be-completed code c to obtain the representation vector of c, and the second CodeBERT model is used to encode the code snippets to obtain the representation vectors of the code snippets. The specific steps are as follows: Input the to-be-completed code c into the first CodeBERT model to obtain the representation vector e c , Input all the code snippets in D1 into the second CodeBERT model to obtain the representation vectors {e d1 , e d2 , e d3 , …, e dt} of each code snippet in D1; Finally, calculate the dot product of e c and each vector in {e d1 , e d2 , e d3 , …, e dt}, sort the results, and select the top n code snippets with the highest rankings from the sorted result queue A t . Denote them as the second candidate code snippet set D2, where n < t, and the values of n and t are determined during the experiment; The specific steps of S214 are: using the edit distance algorithm to calculate the string similarity between c and the code snippets in D1 and sorting them, and selecting the code snippets from the sorting result queue B t The top n code snippets with the highest ranking are selected and recorded as the third candidate snippet set D3. Sim1(c,h) is used to represent the string similarity between c and a code snippet h: sim1(c,h)=L[c l ][h l ] Among them, the two-dimensional array L is used to record the edit distance size, c l Indicates the length of the code c to be completed, h l Indicates the length of the code snippet h; The specific steps of S215 are: t and B t , calculate the comprehensive ranking of each code snippet in D2 and D3, and take the code snippet with the highest comprehensive ranking as the code snippet m that is most similar to c, where a code snippet is recorded in A t and B t The rankings in are k1 and k2, then the comprehensive ranking of its code snippet is where ρ1 and ρ2 are the equilibrium code snippet in A t and B t The weight parameter in ; The specific steps of S216 are: combining cosine similarity and string similarity to calculate the semantic similarity S between c and m. The calculation formula of the semantic similarity S is: Among them, sim1(c,m) represents the string similarity between c and m calculated by the edit distance algorithm, sim2(c,m) represents the cosine similarity between c and m, λ1 and λ2 are weight parameters for balancing string similarity and cosine similarity, e m is the representation vector of code snippet m, d is the representation vector e c and e m In addition, semantic similarity refers to combining cosine similarity and string similarity to measure the similarity between the code to be completed and a certain code snippet.

5. The code completion method based on the combination of retrieval enhancement and multimodality according to claim 1, characterized in that: The AST node sequence feature F of the code to be completed c is extracted in S222 c The specific steps are as follows: S22211: First, construct the AST of the code to be completed c. Then, according to the ASDL rules, add corresponding types to the nodes in the AST to provide more semantic information. Finally, a type-enhanced AST is obtained, in which the mod type is added to the Module node of the AST. Add the stmt type to the FunctionDef, Return, Assign, For, while, and If nodes; add the expr type to the BinOp node; in addition, in the leaf nodes of the AST, add the operator type to the operator node; and add the identifier type to the identifier node; S22212: Obtain an AST node sequence by performing a pre-order traversal on the TA-AST; S22213: Use the Imformer model to extract AST node sequence features. The AST node sequence first passes through multiple attention heads to calculate the attention of each position in the sequence to other positions, then passes through the RMS Norm layer for normalization, and then passes through the residual connection and the block feedforward neural network to enhance the feature representation of the AST node sequence. Finally, the AST node sequence feature F is output through the RMS Norm layer and the residual connection. c , where F c is a feature vector of size 1×512; the multiple attention heads include standard attention heads, sentence-guided attention heads, and parent-child attention heads; RMS Norm is a lightweight normalization layer, and the block-wise feedforward neural network divides the input sequence into blocks by length and calculates each block separately, thereby reducing memory requirements while maintaining computational efficiency; and assuming that the AST node sequence X = {x1, x2, x3, ... x r …,x z }, where z is the length of the AST node sequence, x r The rth token in the AST node sequence; The S222 extracts the path feature F from the leaf node of the AST of the code to be completed c to the node to be completed p The specific steps are as follows: S22221: Create a path set P = {p1, p2, p3, ... p v …,p u }, where P contains all the paths from the leaf nodes in the AST of the code to be completed c to the node to be completed, u is the number of paths, and p v is the vth path in P; S22222: Use multi-layer GRU to extract path p v The local path features of the multi-layer GRU are the path p v The corresponding path sequence; let the hidden state of GRU at time t be h t , introduce contextual attention to calculate h t Compared with the hidden states h1~h1 at the previous time 1~t-1 t-1 The relationship between , represented as the contextual attention score α t , then through α t Calculate the context vector c t : α t =softmax(W T tanh(W h1 [h1,h2,…,h t-1 ]+W h2 h t )) Among them, W, W h1 、W h2 is the learnable parameter matrix; Path p v Local path characteristics Expressed as: Among them, W f is the learnable parameter matrix; S22223: Extract path p v The global path features of the code to be completed are first constructed, where each node in the CFG corresponds to a statement of the code to be completed c. Node2Vec is used to calculate the feature vector corresponding to each node in the CFG. In this process, the path p v There may be multiple statements in the AST, where one statement corresponds to multiple AST nodes. As long as the path passes through one or more nodes in the AST corresponding to a statement, it is considered to have passed through its statement; therefore, the path p v Mapped to a set of statements {s1,s2,…s i …,s k }, k is the number of statements, let statement s i The feature vector corresponding to the node in CFG is v i , then the path p v Global path characteristics of It can be expressed as: Among them, avg is the average value operation; S22224: path p v The characteristic f i Its local path characteristics and global path features The dot product of: Then the path set P={p1,p2,p3,…p v …,p u The characteristics of each path in} can be expressed as F = {f1,f2,f3,…f v …,f u }; S22225: Fusion of multiple path features to obtain the path feature F from the leaf node of AST to the node to be completed p : <h2 style=";text-align:left;direction:ltr">F<h2 style=";text-align:left;direction:ltr"> p <h2 style=";text-align:left;direction:ltr"> (avg(f1,f2,f3,…f)<h2 style=";text-align:left;direction:ltr"> v <h2 style=";text-align:left;direction:ltr"> …,f<h2 style=";text-align:left;direction:ltr"> u <h2 style=";text-align:left;direction:ltr"> ) Among them, avg is the average operation, F p It is a feature vector of size 1×512; The specific steps of fusing the multimodal features of the code to be completed c in S222 to generate the code completion result are as follows: S22241: Get the multimodal feature F of the code to be completed c c , F p and F g After that, F c , F p and F g The fusion is performed and the probability of generating a token is calculated through the softmax normalization function: softmax(W c (αF c +βF p +γF g )) Among them, W c is the learnable parameter matrix, α, β and γ are F c , F p and F g The scaling factor is α+β+γ=1 and α,β,γ∈(0~1), and F is adjusted by dynamically adjusting α, β and γ during model training. c , F p and F g The weight of S22242: Use the token with the highest probability as the code completion result.

6. The code completion method based on the combination of search enhancement and multimodality according to claim 1, characterized in that: The specific steps of S3 are as follows: dividing the code completion dataset into a training dataset, a validation dataset, and a test dataset, and then training the code completion model; first setting a semantic similarity threshold between the completed code and the code snippet m, then randomly initializing the model weight parameters and setting the model hyperparameters, then selecting a loss function and using an optimization algorithm to update the parameters of the code completion model, and continuously evaluating the performance of the code completion model during the training process so as to timely adjust the model structure and hyperparameters so that the model can converge quickly, and finally obtaining a trained code completion model.

7. The code completion method based on the combination of search enhancement and multimodality according to claim 1, characterized in that: The specific steps of S4 are: applying the trained code completion model to the code completion task, automatically completing the code to be completed c, and improving the efficiency of programmers in writing code.

Citation Information

Patent Citations

  • Code completion method based on layer-by-layer fusion abstract syntax tree

    CN117573084A