Code completion method based on combination of retrieval enhancement and multiple modes

By introducing methods of retrieval enhancement and multimodal feature fusion in code completion technology, the problem of path combination explosion, syntactic structural features and semantic features in the existing technology is solved, and more efficient code completion is achieved.

CN119806502AActive Publication Date: 2025-04-11北京轻流智擎科技有限公司

Patent Information

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

AI Technical Summary

Technical Problem

The existing code completion technology has the problem of path combination explosion, the lack of sufficient extraction of syntactic structural features and semantic features of the source code, and the failure to utilize similarities between codes to assist in code completion.

Method used

Using a method based on the combination of search enhancement and multimodality, similar code snippets are retrieved to assist in the completion of the code to be completed, the path features from the leaf nodes of the abstract syntax tree to the completion of the node to be completed are extracted, and rich semantic information is provided through the information enhancement graph, and the multimodal features of the completed code to be completed are fused to complete the code.

Benefits of technology

Effectively utilize code similarity and syntax structure information, reduce the number of paths, alleviate the problem of path explosion, make full use of the syntax structure and semantic features of source code, and improve code completion efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119806502A_ABST
    Figure CN119806502A_ABST
Patent Text Reader

Abstract

The invention belongs to the technical field of code completion, and particularly relates to a code completion method based on combination of retrieval enhancement and multimodality, which comprises 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 performing code completion by applying the code completion model. According to the method, the to-be-completed code is subjected to retrieval enhancement, the similar code snippets are retrieved to assist the to-be-completed code in completion, the external similarity of the code can be well utilized, and the code completion efficiency is improved. The path features from the leaf nodes of the abstract syntax tree to the nodes to be complemented are extracted, so that the syntax structure information on the path can be well utilized, and meanwhile, the problem of path explosion can be relieved 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 is used to assist programmers in coding, automatically providing suggestions and completions for code snippets, functions, methods, variables, etc. that are relevant to the current context. It predicts the code that programmers may enter based on existing code, grammar rules, and context information, and provides options and suggestions that may be adopted. The code completion function can provide instant help during the coding process, reduce the workload of code input, improve coding efficiency and accuracy, and reduce the burden on developers. It has become one of the most commonly used operations for 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, the code completion method based on language models has made significant progress. It can sort the code completion candidates by probability, and can complete multiple lines of code and method parameters at the same time.

[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 the code is completed based on the above sequence.

[0005] Abstract Syntax Tree (AST) can extract grammatical structure information from source code. Compared with the identifier sequence representation method, AST can retain richer grammatical and structural features and is a common code representation method. In practical applications, it is usually necessary to traverse the abstract syntax tree into a node sequence and then use a deep learning model for modeling. Although this method can utilize the grammatical and structural features provided in the abstract syntax tree, it often loses some key structural information, such as parent-child relationship information.

[0006] Using grammatical structure information can help complete the grammatical structure in the code; semantic information represents logic, which can help discover the relationship between automatic code generation and completion context, and can also judge the quality of completion results. Although some existing code completion methods consider the grammatical structure information and semantic information of the source code, they do not fully extract the grammatical structure features and semantic features of the source code.

[0007] Based on the above, the existing code completion technology has 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, in order to solve the problems of path combination explosion, insufficient extraction of grammatical structure features and semantic features of source code, and failure to make good use of the similarity between codes to assist in code completion when extracting path features, a code completion method based on retrieval enhancement and multimodality is proposed. First, the code to be completed is retrieved and enhanced, and similar code fragments are retrieved 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 and solve the third problem mentioned above; extracting the path features from the leaf nodes of the abstract syntax tree to the nodes 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; add multiple types of edges 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, fuse the multimodal features of the abstract syntax tree of the code to be completed to fully utilize the grammatical structure features and semantic features of the source code for code completion, solving the second problem mentioned above. Summary of the invention

[0012] In view of the above problems and / or the existing problems in a code completion method based on combining retrieval enhancement with 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 code completion data sets from various channels, the data sets include code snippets to be completed and complete codes 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 result of the model during the training process.

[0021] 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 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 retrieval enhancement and multimodality described in the present invention, the specific steps of S21 are as follows:

[0025] S211: dividing the code in the source code database into code segments of equal length and using them as basic retrieval units, and then establishing 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: construct 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 to 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 c to be completed from D2 and D3;

[0030] S216: Calculate the semantic similarity between the code to be completed c and the code snippet 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 to obtain the representation vector of c, and the second CodeBERT model is used to encode the code fragments to obtain 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 to obtain the representation vector e c , input all the code fragments in D1 into the second CodeBERT model to obtain 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 . Denote them 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 . Denote them as the third candidate fragment set D3. 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] where 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 the cosine similarity and the string similarity to calculate the semantic similarity S between c and m. The calculation formula of the semantic similarity S is:

[0038]

[0039] 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, and e m is the representation vector of code snippet m, d is the 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.

[0040] 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 S221 is: using the completion code generator to generate the code completion result, which is as follows:

[0041] If the semantic similarity between the code c to be completed and the code snippet m is higher than the threshold, the next code snippet 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 in completion; 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 constructed by the CodeGPT model, and finally the code completion result is output.

[0042] 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 the multimodal features of the code c to be completed, and generating the code completion result, which is as follows:

[0043] 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 leaf nodes of the AST to the nodes to be completed, and the information enhancement graph features. The multimodal features are fused and the code completion result is generated.

[0044] As a preferred solution of the code completion method based on 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:

[0045] S22211: First, construct the AST of the code c to be completed, and then add corresponding types to the nodes in the AST according to the ASDL rules to provide more semantic information. Finally, the 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;

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

[0047] 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 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 n}, where n is the length of the AST node sequence, x i is the i-th token in the AST node sequence;

[0048] In S222, the path feature F from the leaf node of the AST of the code to be completed c to the node to be completed is extracted. p The specific steps are as follows:

[0049] S22221: Create a path set P = {p1, p2, p3, ..., p m}, where P contains the paths from all leaf nodes in the AST of the code to be completed c to the node to be completed, m is the number of paths, and pi is the i-th path in P;

[0050] S22222: Use multi-layer GRU to extract path p i The local path features of the multi-layer GRU are the path p i 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 by the contextual attention score α t , and then through α t Calculate the context vector c t :

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

[0052]

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

[0054] Path p i Local path characteristics It is expressed as:

[0055]

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

[0057] S22223: Extract path p i 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 i 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 i Mapped to a set of statements {s1,s2,…,s k}, k is the number of statements, let statement s i The feature vector corresponding to the node in CFG is vi , then the path p i Global path features It can be expressed as:

[0058]

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

[0060] S22224: Path p i The characteristic f i Its local path feature and global path features The dot product of:

[0061]

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

[0063] 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 :

[0064] F p =avg(f1,f2,f3,…,f m )

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

[0066] 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:

[0067] S22231, structural information enhancement map:

[0068] 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 Model 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, a calculation edge left and right is 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;

[0069] S222312: Then add the next token edge nextToken in TA-AST, where 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 to indicate that the value of one identifier is assigned to another identifier;

[0070] S222313: Then, according to the camel case naming rule, the leaf nodes in TA-AST are split into multiple sub-nodes by value, so as to solve the super-vocabulary problem to a certain extent; at the same time, a new node type subtoken and sub-node edge subToken_of are introduced in TA-AST;

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

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

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

[0074] F′ n =ReLU(W n ·F n +b n )

[0075] F′ e =ReLU(W e ·F e +b e )

[0076] Among them, F′ nis 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 initial edge feature, W n and W e is the weight parameter, b n and b e is the bias value;

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

[0078]

[0079] 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;

[0080] 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;

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

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

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

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

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

[0086] 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 data set into a training data set, a verification data set and a test data set, and then training the code completion model; first setting the semantic similarity threshold between the completion code and the code snippet m, then randomly initializing the model weight parameters and setting the model hyperparameters, and then selecting the loss function and using the 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.

[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 step of S4 is: applying the trained code completion model to the code completion task, automatically completing the code c to be completed, and improving the efficiency of programmers in writing code.

[0088] Compared with existing technologies:

[0089] 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 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. 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, it is possible to make full use of the grammatical structure features and semantic features of the source code to complete the code. BRIEF DESCRIPTION OF THE DRAWINGS

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

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

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

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

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

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

[0096] The present invention provides a code completion method based on the combination of retrieval enhancement and multi-modality, 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 to perform retrieval enhancement on the code to be completed, and 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, and 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 a completion code generator to generate a code completion result. Otherwise, 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 a code completion result; then the code completion dataset is divided and the code completion model is trained, and 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:

[0097] The specific steps are as follows:

[0098] S1: Build code completion dataset;

[0099] The specific steps of S1 are: collecting a code completion dataset from various channels, where the dataset includes code snippets to be completed and the complete code after completion, so as to facilitate the training of the code completion model, where the complete code after completion is used to compare with the output result of the model during the training process;

[0100] S2: Build a code completion model;

[0101] The structure of the code completion model is as follows Figure 2As shown, firstly, the code to be completed c is searched and enhanced, and the code snippet m that is most similar to the code to be completed c is retrieved through the similar code retriever and the edit distance algorithm, and then the semantic similarity between c and m is calculated. If the semantic similarity is higher than the 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 leaf node of the AST to the node to be completed, and the information enhancement graph features, and these features are fused to generate the code completion result;

[0102] Among them, the specific steps of S2 are as follows:

[0103] 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;

[0104] Among them: The specific steps of S21 are as follows:

[0105] S211: dividing the code in the source code database into code fragments of equal length and using them as basic retrieval units, and then establishing a sequential index for each code fragment, wherein the source code database is obtained from various existing public code libraries or is self-written;

[0106] S212: using the BM25 algorithm to 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; BM25 (Best Matching 25) is an algorithm for information retrieval, which can be used to calculate the correlation between the query and the code;

[0107] S213: construct 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;

[0108] Wherein: 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 to 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;

[0109] S214: Use the edit distance algorithm to select n code snippets similar to the code c to be completed 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;

[0110] Specifically, the steps of S214 are as follows: Use the edit distance algorithm to calculate and sort the string similarity between c and the code snippets in D1, 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:

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

[0112]

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

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

[0115] Specifically, the steps of S215 are as follows: 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. Among them, 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 ;

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

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

[0118]

[0119] 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, and e m is the representation vector of code snippet m, d is the 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;

[0120] Cosine similarity: It means that the cosine value of the angle between two vectors in a vector space is used to measure the difference between two vectors. 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 the vectors a = (x1, y1) and b = (x2, y2), then the cosine similarity calculation formula of a and b is as follows:

[0121]

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

[0123]

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

[0125]

[0126] 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;

[0127] Among them, the specific steps of S221 are: using the completion code generator to generate a code completion result, as follows:

[0128] If the semantic similarity between the code to be completed c and the code snippet m is higher than the threshold, the next code snippet 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 in completion; 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 constructed by the CodeGPT model, and finally the code completion result is output; its CodeGPT model is a pre-trained language model based on the GPT architecture, which is specially optimized for programming tasks;

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

[0130] 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 leaf nodes of the AST to the nodes to be completed, and the information enhancement graph features. The multimodal features are fused and the code completion result is generated.

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

[0132] S22211: First, construct the AST of the code c to be completed, and then add corresponding types to the nodes in the AST according to the ASDL rules to provide more semantic information. Finally, the type-enhanced AST (Type Augment-AbstractSyntax Tree, 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, 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; 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, etc.; the ASDL rule contains information such as constructors, node types, and fields, where the field specifies the relationship between the parent class and each of its subclasses;

[0133] like Figure 3 As shown, the code c to be completed is converted into AST and then types are added 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;

[0134] S22212: Obtain an AST node sequence by performing a pre-order traversal on 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,____";

[0135] S22213: Use the Imformer model to extract AST node sequence features. The model structure diagram is as follows Figure 4 As shown in Figure 1, 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; RMSNorm is a lightweight normalization layer, and the block 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 n}, where n is the length of the AST node sequence, x i is the i-th token in the AST node sequence;

[0136] in:

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

[0138] (2) Introduce sentences 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:

[0139]

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

[0141] (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:

[0142]

[0143] In S222, the path feature F from the leaf node of the AST of the code to be completed c to the node to be completed is extracted. p The specific steps are as follows:

[0144] S22221: Create a path set P = {p1, p2, p3, ..., p m}, where P contains the paths from all leaf nodes in the AST of the code to be completed c to the node to be completed, m is the number of paths, and p i is the i-th path in P;

[0145] S22222: Use multi-layer GRU to extract path p i The local path features of the multi-layer GRU are the path p i 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 by the contextual attention score α t , and then through α t Calculate the context vector c t :

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

[0147]

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

[0149] Path p i Local path characteristics It is expressed as:

[0150]

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

[0152] 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.

[0153] S22223: Extract path p i 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 i 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 i Mapped to a set of statements {s1,s2,…,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 i Global path features It can be expressed as:

[0154]

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

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

[0157] S22224: Path p i The characteristic f i Its local path feature and global path features The dot product of:

[0158]

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

[0160] 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 :

[0161] F p =avg(f1,f2,f3,…,f m )

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

[0163] 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:

[0164] S22231, structural information enhancement map:

[0165] 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 Model 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, a calculation edge left and right is 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;

[0166] S222312: Then add the next token edge nextToken in TA-AST, where 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 to indicate that the value of one identifier is assigned to another identifier;

[0167] S222313: Then, according to the camel case naming rule, the leaf nodes in TA-AST are split into multiple child nodes by value, so as to solve the out-of-vocabulary problem to a certain extent; at the same time, new node types subtoken and child node edges subToken_of are introduced in TA-AST; its out-of-vocabulary problem (OOV) refers to the problem that in natural language processing (NLP) tasks, the model encounters words or symbols that have not been seen during training, resulting in the inability to correctly process these new words;

[0168] S222314: Finally, the Figure 5 The information-enhanced graph of the AST shown;

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

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

[0171] F′ n =ReLU(W n ·F n +b n )

[0172] F′ e=ReLU(W e ·F e +b e )

[0173] 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 initial edge feature, W n and W e is the weight parameter, b n and b e is the bias value;

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

[0175]

[0176] 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;

[0177] 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;

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

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

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

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

[0182] Among them, W c is the learnable parameter matrix, α, β and γ are c , F p 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

[0183] S22242: Take the token with the highest probability as the code completion result;

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

[0185] The specific steps of S3 are: divide the code completion dataset into a training dataset, a validation dataset, and a test dataset, and then train the code completion model; first set the semantic similarity threshold between the completion code and the code snippet m, then randomly initialize the model weight parameters and set the model hyperparameters, then select the loss function and use the optimization algorithm to update the parameters of the code completion model, and continuously evaluate the performance of the code completion model during the training process, so as to adjust the model structure and hyperparameters in time so that the model can converge quickly, and finally obtain a trained code completion model;

[0186] The specific embodiment of this step is:

[0187] Embodiment: a code completion data set is divided into a training data set, a validation data set and a test data set in a ratio of 8:1:1, and then a code completion model is trained; first, a semantic similarity threshold between a code to be completed c and a code snippet m is set to 0.6, then a model weight parameter is randomly initialized and a model hyperparameter is set, a cross entropy is used as a loss function, and then a stochastic gradient descent optimization algorithm is selected to update the parameters of the code completion model; during the training process, the validation data set is periodically used to evaluate the performance of the code completion model, so as to adjust the model structure and hyperparameters in time and enable the model to converge quickly; when the code completion model is trained until the loss value tends to converge, the training ends, and then a trained code completion model is obtained;

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

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

[0190] Although the present invention has been described above with reference to the embodiments, various modifications may be made thereto and parts thereof may be replaced by equivalents without departing from the scope of the present invention. In particular, as long as there is no structural conflict, the various features in the embodiments disclosed in the present invention may be used in combination with each other in any manner, and the fact that these combinations are not exhaustively described in this specification is only for the sake of omitting space and saving resources. 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: Build 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.

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

3. The code completion method based on retrieval enhancement and multimodality according to claim 1, characterized in that: 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.

4. The code completion method based on retrieval enhancement and multimodality according to claim 3, characterized in that: The specific steps of S21 are as follows: S211: dividing the code in the source code database into code fragments of equal length and using them as basic retrieval units, and then establishing a sequential index for each code fragment, 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: construct 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 to 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 c to be completed from D2 and D3; S216: Calculate the semantic similarity between the code to be completed c and the code snippet m.

5. The code completion method based on the combination of retrieval enhancement and multimodality according to claim 4 is 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 code c to be completed, obtaining the representation vector of c, and the second CodeBERT model is used to encode the code snippets, obtaining the representation vectors of the code snippets. The specific steps are as follows: Input the code c to be completed into the first CodeBERT model to obtain the representation vector e of c c , and 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 them, 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 string similarity between c and the code snippets in D1 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 size of the edit distance, 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 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 the cosine similarity and the 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, and e m is the representation vector of code snippet m, d is the 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.

6. The code completion method based on retrieval enhancement combined with multimodality according to claim 3, characterized in that: The specific steps of S221 are: using the completion code generator to generate a code completion result, as follows: If the semantic similarity between the code c to be completed and the code snippet m is higher than the threshold, the next code snippet 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 in completion; 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 constructed by the CodeGPT model, and finally the code completion result is output.

7. The code completion method based on retrieval enhancement combined with multimodality according to claim 6, characterized in that: The specific steps of S222 are: extracting the multimodal features of the code c to be completed, and generating a 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 leaf nodes of the AST to the nodes to be completed, and the information enhancement graph features. The multimodal features are fused and the code completion result is generated.

8. The code completion method based on retrieval enhancement combined with multimodality according to claim 7, characterized in that: In step S222, the AST node sequence feature F of the code to be completed c is extracted. c The specific steps are as follows: S22211: First, construct the AST of the code c to be completed, then add corresponding types to the nodes in the AST according to the ASDL rules to provide more semantic information, and finally obtain a type-enhanced AST, in which the mod type is added to the Module node of the AST; Add stmt type to FunctionDef, Return, Assign, For, while, If nodes; add expr type to BinOp node; in addition, in the leaf nodes of AST, add operator type to operator node; add identifier type to identifier node; S22212: Obtain an AST node sequence by performing a pre-order traversal on 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 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 n }, where n is the length of the AST node sequence, x i is the i-th token in the AST node sequence; In S222, the path feature F from the leaf node of the AST of the code to be completed c to the node to be completed is extracted. p The specific steps are as follows: S22221: Create a path set P = {p1, p2, p3, ..., p m }, where P contains the paths from all leaf nodes in the AST of the code to be completed c to the node to be completed, m is the number of paths, and p i is the i-th path in P; S22222: Use multi-layer GRU to extract path p i The local path features of the multi-layer GRU are the path p i 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 by the contextual attention score α t , and 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 i Local path characteristics It is expressed as: Among them, W f is the learnable parameter matrix; S22223: Extract path p i 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 i 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 i Mapped to a set of statements {s1,s2,…,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 i Global path features It can be expressed as: Among them, avg is the average value operation; S22224: Path p i The characteristic f i Its local path feature and global path features The dot product of: Then the path set P = {p1,p2,p3,…,p m The characteristics of each path in} can be expressed as F = {f1,f2,f3,…,f m }; 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"> m <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 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: 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 Model 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, a calculation edge left and right is 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: Then add the next token edge nextToken in TA-AST, where 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 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 sub-nodes by value, so as to solve the super-vocabulary problem to a certain extent; at the same time, a new node type subtoken and sub-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 initial 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 , where F g It is a feature vector of size 1×512; The specific steps of fusing the multimodal features of the code c to be completed 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, for F c , F p and F g The probability of generating a token is calculated by fusion and the softmax normalization function: softmax(W c (αF c +βF p +γF g )) Among them, W c is the learnable parameter matrix, α, β and γ are c , F p 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 S22242: Use the token with the highest probability as the code completion result.

9. The code completion method based on retrieval enhancement combined with multimodality according to claim 1, characterized in that: The specific steps of S3 are: dividing the code completion data set into a training data set, a verification data set and a test data set, and then training the code completion model; first setting the semantic similarity threshold between the completion code and the code snippet m, then randomly initializing the model weight parameters and setting the model hyperparameters, and then selecting a loss function and using an optimization algorithm to update the parameters of the code completion model, while 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.

10. The code completion method based on retrieval enhancement combined with 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 c to be completed, and improving the efficiency of programmers in writing code.

Citation Information

Patent Citations

  • Code search recommendation device and method based on open source knowledge

    CN112051986A

  • Code completion method and device and related equipment

    CN112527273A

  • Container basic mirror image recommendation method and system based on configuration code representation

    CN113296784A

  • Software demand and code mapping method and system

    CN113791757A

  • Code abstract automatic generation method based on information retrieval and neural network

    CN115408056A

Cited By

  • Code completion method based on multi-modal two-stage fine tuning

    CN121050727A

  • A code completion method based on multi-modal two-stage fine-tuning

    CN121050727B

  • Multi-source heterogeneous code conversion method and device for cross-language development

    CN121143797A

  • A multi-source heterogeneous code conversion method and device for cross-language development

    CN121143797B