Code completion method and device, storage medium and computer equipment
Through multi-angle analysis and multi-objective algorithm calculation, a code combination with the smallest information loss is generated, which solves the problem of low matching caused by a single code retrieval dimension, and improves the accuracy and information integrity of code completion.
Patent Information
- Application Number
- CN202411486026.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-23
- Publication Date
- 2024-12-31
- Estimated Expiration
- 2044-10-23
AI Technical Summary
In the prior art, the dimensions are single in the code search process, resulting in a low degree of matching between the matching code and the code to be completed, reducing the accuracy of code completion.
The preset code summary model summarizes the complete code, generates the first summary information, and performs multi-angle analysis with the second summary information of the code snippet in the code base, uses multiple target algorithms to calculate the similarity, and generates candidate codes Set and complete code is determined by a combination of codes with minimal information loss.
It improves the accuracy and matching of code completion, ensures the information integrity of the complete code, and enhances the accuracy of code completion.
Smart Images

Figure CN119225735A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software development technology, and in particular to a code completion method, device, storage medium and computer equipment. Background Art
[0002] With the development of computer development technology, software developers can use code generation tools to complete unfinished code when writing code, thereby greatly improving the efficiency of code development. In this process, the unfinished code can be searched in the search library, and then the unfinished code and the retrieved external code are sent to the code generation tool to obtain the completed code.
[0003] However, in the process of code retrieval, code snippets are mainly matched by calculating the code overlap. The dimension considered during retrieval is too single, resulting in a low match between the matched external code and the unfinished code, thereby reducing the accuracy of code completion. Summary of the invention
[0004] The purpose of the present application is to solve at least one of the above-mentioned technical defects, especially the technical defect that in the prior art, during the code retrieval process, code fragments are matched mainly by calculating the code overlap, and the dimension considered during the retrieval is too single, resulting in a low matching degree between the matched external code and the unfinished code, thereby reducing the accuracy of code completion.
[0005] In a first aspect, the present application provides a code completion method, the method comprising:
[0006] Get the code to be completed;
[0007] Summarizing the code to be completed based on a preset code summarization model to obtain first summary information of the code to be completed, and determining second summary information corresponding to each code snippet in a preset code library;
[0008] Using multiple target algorithms to respectively calculate the similarity between the first summary information and each of the second summary information, and determining a set of candidate codes corresponding to the code to be completed according to the similarity between the first summary information and each of the second summary information calculated by each target algorithm;
[0009] The candidate codes in the candidate code set are arranged and combined, and after selecting a target code combination with the smallest information loss from the arrangement and combination results of the candidate code set based on information loss, a completion code corresponding to the code to be completed is determined according to the target code combination.
[0010] In one embodiment, summarizing the code to be completed based on a preset code summarization model to obtain first summary information of the code to be completed includes:
[0011] Parsing the code to be completed using a preset code summary model to obtain key features of the code to be completed;
[0012] and, using the code summary model to perform context analysis on the code to be completed to obtain context information of the code to be completed;
[0013] The first summary information of the code to be completed is determined according to the key features and context information of the code to be completed.
[0014] In one embodiment, the using of multiple target algorithms to respectively calculate the similarity between the first summary information and each of the second summary information, and determining a set of candidate codes corresponding to the code to be completed according to the similarity between the first summary information and each of the second summary information calculated by each target algorithm, includes:
[0015] Determine multiple target algorithms according to the first summary information and each of the second summary information, and evaluate the contribution value of each target algorithm in the historical code completion task;
[0016] Determine the weight of each target algorithm based on the contribution value corresponding to each target algorithm;
[0017] For each second summary information, using each target algorithm respectively to calculate the similarity between the second summary information and the first summary information;
[0018] According to the weight of each target algorithm, weighted summation is performed on the similarities calculated by each target algorithm to obtain the target similarity between the second summary information and the first summary information;
[0019] When determining the target similarity between each of the second summary information and the first summary information, N code snippets with the highest target similarity are selected from the code snippets corresponding to each of the second summary information, and a candidate code set corresponding to the code to be completed is generated based on the selected code snippets.
[0020] In one embodiment, the step of permuting and combining the candidate codes in the candidate code set, selecting a target code combination with the smallest information loss from the permutation and combination results of the candidate code set based on information loss, and then determining the completion code corresponding to the code to be completed according to the target code combination includes:
[0021] Determine the preset code completion model;
[0022] Performing a full permutation of each candidate code in the candidate code set to determine a plurality of code combinations; each code combination includes M candidate codes;
[0023] Input each code combination into the code completion model for a forward propagation to determine the information loss value of each code combination according to each code combination and its corresponding output result;
[0024] A code combination with the smallest information loss value is selected as a target code combination, and the target code combination and the code to be completed are input into the code completion model to obtain a completion code corresponding to the code to be completed.
[0025] In one embodiment, before executing the step of inputting the target code combination and the code to be completed into the code completion model, the method further includes:
[0026] Identifying whether a comment symbol exists in the target code combination;
[0027] If it exists, determining the position of the code comment in the target code combination according to the comment symbol in the target code combination;
[0028] Based on the positions of the code comments in the target code combination, the code comments in the target code combination are deleted.
[0029] In one embodiment, before executing the step of inputting the target code combination and the code to be completed into the code completion model, the method further includes:
[0030] Determine the attention ratio of each line of code in the target code combination;
[0031] Delete the code lines whose attention ratio in the target code combination is lower than a preset threshold.
[0032] In one embodiment, the process of generating the code library includes:
[0033] Get the project code;
[0034] Determine the number of sliding rows and the number of window rows, and divide the project code in sequence according to the number of sliding rows and the number of window rows to obtain multiple code fragments; the number of sliding rows is less than the number of window rows;
[0035] A code base is generated based on the multiple code snippets obtained by the division.
[0036] In a second aspect, the present application provides a code completion device, the device comprising:
[0037] Code acquisition module, used to obtain the code to be completed;
[0038] A code summarizing module, used for summarizing the code to be completed based on a preset code summarizing model, obtaining first summary information of the code to be completed, and determining second summary information corresponding to each code fragment in a preset code library;
[0039] a similarity calculation module, configured to respectively calculate the similarity between the first summary information and each of the second summary information using a plurality of target algorithms, and determine a set of candidate codes corresponding to the code to be completed according to the similarity between the first summary information and each of the second summary information calculated by each target algorithm;
[0040] The code completion module is used to arrange and combine the candidate codes in the candidate code set, and after selecting a target code combination with the smallest information loss from the arrangement and combination results of the candidate code set based on information loss, determine the completion code corresponding to the code to be completed according to the target code combination.
[0041] In a third aspect, the present application provides a storage medium storing computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the code completion method described in any of the above embodiments.
[0042] In a fourth aspect, the present application provides a computer device, comprising: one or more processors, and a memory;
[0043] The memory stores computer-readable instructions, and when the one or more processors execute the computer-readable instructions, the steps of the code completion method described in any one of the above embodiments are performed.
[0044] It can be seen from the above technical solutions that the embodiments of the present application have the following advantages:
[0045] The present application provides a code completion method, apparatus, storage medium and computer device. The method comprises: when obtaining the code to be completed, first summarizing the code to be completed and each code snippet in a preset code library, so as to obtain first summary information of the code to be completed and second summary information of each code snippet in the code library; then, using the summary information as calculation data, determining the similarity between the code to be completed and each code snippet respectively; in this process, the code itself is not directly used to calculate the similarity, but the summary information of the code is used to calculate the similarity; the summary information includes not only the semantic information of the code itself, but also its main function, code style and annotation, etc., so as to avoid the subsequent matching of code snippets with high code overlap but large differences in semantic logic as candidate codes, resulting in low accuracy of the final completed code; and by analyzing and summarizing the code from multiple angles, the matching degree between the completed code and the code to be completed can be improved, thereby enhancing the accuracy of code completion. And when calculating the similarity, multiple target algorithms are used for calculation, so that the advantages of multiple target algorithms can be combined to improve the accuracy of similarity calculation, and then the candidate code set is determined according to the similarity, and the completion code corresponding to the code to be completed is determined according to the target code combination with the smallest information loss in the candidate code set. In this way, the code combination is screened from the perspective of information loss, which can ensure the information integrity of the target code combination used when determining the completion code, so as to further improve the accuracy of code completion. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0047] Figure 1 A flowchart of a code completion method provided in an embodiment of the present application;
[0048] Figure 2 A schematic diagram of a process for summarizing the code to be completed based on a preset code summarization model provided in an embodiment of the present application;
[0049] Figure 3 A schematic diagram of a process for determining a set of candidate codes corresponding to a code to be completed provided in an embodiment of the present application;
[0050] Figure 4 An example diagram of a code completion method provided in an embodiment of the present application;
[0051] Figure 5A schematic diagram of the structure of a code completion device provided in an embodiment of the present application;
[0052] Figure 6 An internal structure diagram of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0053] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0054] In one embodiment, the present application provides a code completion method, and the following embodiment is described by applying the method to a server. It can be understood that the code completion method can be executed by a single server or a server cluster composed of multiple servers, and the present application does not make specific restrictions on this.
[0055] like Figure 1 As shown, the present application provides a code completion method, the method comprising:
[0056] S101: Obtain the code to be completed.
[0057] The code to be completed refers to the unfinished code entered by the developer.
[0058] In this step, when the developer is writing code, the code completion method can be triggered by right-click menu selection, menu selection, etc. When the server detects that the code completion method is triggered, the code to be completed can be obtained from the client where the developer writes the code.
[0059] It should be noted that the operation to trigger the code completion method can be preset, for example, it can be set to be triggered by clicking a specific button or menu, or it can be set to automatically detect the developer's input and automatically trigger. This application does not make specific restrictions on this.
[0060] S102: Summarize the code to be completed based on a preset code summary model to obtain first summary information of the code to be completed, and determine second summary information corresponding to each code snippet in a preset code library.
[0061] The code summary model is used to perform multi-dimensional analysis and summary on the input code, obtain code summary information and output it. The code library includes multiple code snippets, which are snippets in the project code of the project to which the code to be completed belongs.
[0062] In this step, when the code to be completed is obtained, a preset code summary model can be determined, and then the code summary model is used to summarize the code to be completed to obtain the first summary information of the code to be completed output by the code summary model. At this time, the second summary information corresponding to each code snippet in the preset code library will also be determined.
[0063] Specifically, since the second summary information corresponding to each code snippet in the code base is used each time a code completion task is performed, each code snippet in the code base can be summarized in advance, and can be directly determined and used in the code completion task. It should be noted that the second summary information of the code snippets in the code base does not necessarily remain unchanged. After the code summary model is updated, the new code summary model can be used to re-summarize each code snippet in the code base to update the second summary information of each code snippet. In addition, when there are updates to the code snippets in the code base, the updated code snippets can also be re-summarized.
[0064] It is understandable that the first summary information and the second summary information can be understood as a kind of summary information, which refers to the information obtained by analyzing the code in multiple dimensions and summarizing the analysis results. The summary information includes but is not limited to code semantic information, code style, main functions and structure, annotations, code readability, performance information, etc. Therefore, by reflecting the information of the code in multiple dimensions through summary information, the similarities and differences of different codes in multiple dimensions can be considered when calculating the similarity based on the summary information, so as to improve the positive contribution of the similarity calculation process to the code completion task, thereby improving the accuracy of code completion.
[0065] In addition, the code summary model of the present application can be obtained by using code snippets as training samples, using the summary information of the code snippets as sample labels, and using the loss function to train the preset pre-trained model. During the training process, the server can input the training samples and their corresponding sample labels into the pre-trained model for forward propagation, and use the loss function in the back propagation of the pre-trained model to adjust the parameters of the pre-trained model until the pre-trained model meets certain iteration conditions. The pre-trained model that has been trained can be used as the code summary model.
[0066] S103: using multiple target algorithms to respectively calculate the similarity between the first summary information and each second summary information, and determining a candidate code set corresponding to the code to be completed according to the similarity between the first summary information and each second summary information calculated by each target algorithm.
[0067] Among them, similarity is used to measure the similarity between summary information. The target algorithm is used to calculate the similarity between two summary information, such as cosine similarity, Euclidean distance, Jaccard similarity, etc.
[0068] In this step, multiple target algorithms can be used to calculate the similarity between the first summary information and each second summary information respectively, and then the candidate code set corresponding to the code to be completed is determined according to the similarity between the first summary information and each second summary information calculated by each target algorithm.
[0069] Specifically, the candidate code set refers to a set of code snippets with high similarity between the second summary information and the first summary information. The similarity is calculated based on the summary information, and then the candidate code records are determined based on the similarity, so that multiple code snippets with high similarity to the first summary information of the code to be completed can be obtained, thereby improving the accuracy of code completion.
[0070] S104: performing permutations and combinations on the candidate codes in the candidate code set, and selecting a target code combination with the minimum information loss from the permutations and combinations of the candidate code set based on the information loss, and then determining a completion code corresponding to the code to be completed according to the target code combination.
[0071] After determining the candidate code set, considering the information loss, the candidate codes in the candidate code set can be arranged and combined, so that multiple code combinations, that is, the arrangement and combination results, can be obtained, and then the target code combination with the smallest information loss is selected from the multiple code combinations, and finally the completion code corresponding to the code to be completed is determined according to the target code combination. After obtaining the completion code, the code to be completed can be completed to obtain a complete code module. And because the information integrity of the code combination used to determine the completion code is guaranteed to the greatest extent, the development efficiency can be guaranteed while also improving the fit between the completion code and the code to be completed.
[0072] In another embodiment, in order to further improve the generation effect of code completion, the code to be completed and its corresponding completion code obtained in S104 can be summarized as the code to be completed in S102, and subsequent steps are performed until the iteration stop condition is met. It should be noted that the iteration stop condition can be reaching a preset number of iterations, or the difference between the completion code obtained in the current iteration and the completion code obtained in the previous iteration is less than a threshold value, and this application does not impose specific restrictions on this. However, for the consideration of code completion efficiency, the number of iterations should not be too high.
[0073] In one example, in order to further improve the accuracy of code completion, two rounds of iterations can be used, and the code to be completed in the second round of iteration is obtained by combining the code to be completed and the completed code in the first round of iteration. This makes the summary information of the second round of iteration richer and includes the context information of the original code to be completed, that is, the completed code in the first round of iteration. In this way, when comparing similarities, there is a chance to match more suitable code snippets, thereby improving the accuracy of code completion.
[0074] The present application provides a code completion method, apparatus, storage medium and computer device. The method comprises: when obtaining the code to be completed, first summarizing the code to be completed and each code snippet in a preset code library, so as to obtain first summary information of the code to be completed and second summary information of each code snippet in the code library; then, using the summary information as calculation data, determining the similarity between the code to be completed and each code snippet respectively; in this process, the code itself is not directly used to calculate the similarity, but the summary information of the code is used to calculate the similarity; the summary information includes not only the semantic information of the code itself, but also its main function, code style and annotation, etc., so as to avoid the subsequent matching of code snippets with high code overlap but large differences in semantic logic as candidate codes, resulting in low accuracy of the final completed code; and by analyzing and summarizing the code from multiple angles, the matching degree between the completed code and the code to be completed can be improved, thereby enhancing the accuracy of code completion. And when calculating the similarity, multiple target algorithms are used for calculation, so that the advantages of multiple target algorithms can be combined to improve the accuracy of similarity calculation, and then the candidate code set is determined according to the similarity, and the completion code corresponding to the code to be completed is determined according to the target code combination with the smallest information loss in the candidate code set. In this way, the code combination is screened from the perspective of information loss, which can ensure the information integrity of the target code combination used when determining the completion code, so as to further improve the accuracy of code completion.
[0075] like Figure 2 As shown, in one embodiment, the code to be completed is summarized based on a preset code summary model to obtain first summary information of the code to be completed, including:
[0076] S201: parse the code to be completed using a preset code summary model to obtain key features of the code to be completed.
[0077] Among them, key features refer to the core elements of the code to be completed, which include but are not limited to code structure, grammatical structure, code readability, code performance, etc.
[0078] In this step, the server can analyze the syntax of the code to be completed, and then identify the elements such as keywords, variables, and function calls in the code to be completed. Then, based on the analysis results of the syntax of the code to be completed, the code structure and syntax structure can be determined, and then the code readability and code performance can be determined based on the code structure, syntax structure and other information of the code to be completed. Then, the information obtained by the analysis is summarized to obtain the key features of the code to be completed.
[0079] S202: Performing context analysis on the code to be completed using the code summary model to obtain context information of the code to be completed.
[0080] The context information refers to the environment and background information around the code to be completed, including but not limited to code style, semantic logic, main functions, developer's intentions, etc.
[0081] In this step, the server determines the code style and semantic logic by analyzing the context structure of the code to be completed, and analyzes the environment and background information around the code to be completed, and then infers information such as the intention and main functions of the code, and then summarizes the information obtained from the analysis to obtain the context information of the code to be completed.
[0082] S203: Determine first summary information of the code to be completed according to key features of the code to be completed and context information.
[0083] Specifically, when determining the key features and context information of the code to be completed, the key features and context information of the code to be completed may be preprocessed, and the preprocessed key features and context information may be merged to obtain the first summary information of the code to be completed.
[0084] It can be understood that the above-mentioned process of summarizing the code to be completed based on the code summarization model to obtain the first summary information is also applicable to the process of obtaining the corresponding summary information of other code snippets based on the code summarization model. In other words, the method principles in this embodiment can be applied to summarize the code snippets of the code library to obtain the second summary information.
[0085] In this embodiment, the code to be completed is summarized in multiple dimensions based on the code summary model, so that the first summary information finally obtained not only includes the semantic logic and code structure of the code itself, but also includes code style, code readability, main functions, code performance and other information, so that the subsequent similarity calculation can comprehensively evaluate the similarity between the summary information from multiple aspects, and then select a more suitable code snippet, thereby improving the accuracy of code completion.
[0086] like Figure 3As shown, in one embodiment, multiple target algorithms are used to respectively calculate the similarity between the first summary information and each second summary information, and according to the similarity between the first summary information and each second summary information calculated by each target algorithm, a candidate code set corresponding to the code to be completed is determined, including:
[0087] S301: Determine multiple target algorithms according to the first summary information and each second summary information, and evaluate the contribution value of each target algorithm in the historical code completion task.
[0088] Among them, the contribution value is used to evaluate the performance of the target algorithm in the historical code completion task, that is, to evaluate the contribution of the target algorithm to the accuracy of the historical code completion task.
[0089] In this step, multiple similarity algorithms that match the code information represented by the first summary information and each second summary information can be selected to determine multiple target algorithms. Then, the contribution value of each target algorithm in the historical code completion task can be determined by combining the completion effect of the historical code completion task and the weight of the target algorithm used in the completion process.
[0090] Furthermore, when determining multiple target algorithms, the first summary information and each second summary information can be analyzed through a pre-trained deep learning model, and then multiple algorithms with better prediction performance can be selected from the preset algorithm library as target algorithms. The first summary information and the second summary information can also be divided into multiple types, and the best target algorithm combination used for each type can be pre-set. When determining the first summary information and each second summary information, the best target algorithm combination can be determined by judging the type of the first summary information and each second summary information, and then querying the pre-set data to determine the best target algorithm combination to determine multiple target algorithms. The specific determination method can be selected according to actual needs, and this application does not impose specific restrictions on this.
[0091] S302: Determine the weight of each target algorithm according to the contribution value corresponding to each target algorithm.
[0092] In this step, when the contribution value of each target algorithm is calculated, the proportion of each target algorithm in the sum of the contribution values of all target algorithms may be counted to determine the weight of each target algorithm.
[0093] S303: For each piece of second summary information, use each target algorithm to calculate the similarity between the second summary information and the first summary information.
[0094] For each second summary information, each target algorithm is used to calculate the similarity between the second summary information and the first summary information. At this time, the similarity between the second summary information calculated by each target algorithm and the second summary information can be obtained.
[0095] S304: performing weighted summation on the similarities calculated by each target algorithm according to the weight of each target algorithm to obtain the target similarity between the second summary information and the first summary information.
[0096] Exemplarily, assuming that the determined target algorithms include target algorithm A and target algorithm B, the weight of target algorithm A is 0.7, the weight of target algorithm B is 0.3, the similarity between the second summary information C and the first summary information D is calculated using target algorithm A to obtain similarity a, and the similarity between the second summary information C and the first summary information D is calculated using target algorithm B to obtain similarity b. At this time, the target similarity between the second summary information C and the first summary information D can be determined to be similarity a*0.7+similarity b*0.3.
[0097] S305: When determining the target similarity between each second summary information and the first summary information, select N code snippets with the highest target similarity from the code snippets corresponding to each second summary information, and generate a candidate code set corresponding to the code to be completed based on the selected code snippets.
[0098] Wherein, N is a positive integer less than the number of code snippets in the code base. The setting of N can be considered from aspects such as code completion efficiency and data richness, and this application does not make specific restrictions on this.
[0099] In this step, based on the target similarity between each second summary information and the first summary information, N code snippets with the highest target similarity to the first summary information are selected from the code snippets corresponding to each second summary information, and then the selected code snippets are combined into a candidate code set.
[0100] The most suitable target algorithm combination is determined through the code information represented by the first summary information and each second summary information to determine the multiple target algorithms used this time. In this way, the target algorithm that best matches the current code completion task can be selected, and the advantages of calculating similarity of multiple target algorithms can be maximized. In addition, the weight of each target algorithm can be determined by the performance of the determined target algorithm in the historical code completion task, which can further give play to the advantages of each target algorithm and flexibly adjust the weights of different target algorithms, thereby ensuring the quality of the code snippets in the candidate code set and the matching degree with the code to be completed.
[0101] In another embodiment, the weight of the target algorithm can also be determined by pre-setting the weights of all target algorithms based on the characteristics of the code completion task. After determining the target algorithm to be used, the corresponding weight can be directly obtained. The weights of different target algorithms can also be determined based on the number of keywords in the code.
[0102] In one embodiment, the candidate codes in the candidate code set are arranged and combined, and a target code combination with the minimum information loss is selected from the arrangement and combination results of the candidate code set based on information loss, and then a completion code corresponding to the code to be completed is determined according to the target code combination, including:
[0103] S1: Determine a preset code completion model.
[0104] The code completion model is used to analyze the target code combination and the code to be completed and output the completion code of the code to be completed with the target code combination as the model prompt. Specifically, the input items of the code completion model are the model prompt and the code to be completed, wherein the model prompt refers to the target code combination.
[0105] It is understandable that the code completion model of the present application can be obtained by using the code sample data to be completed and its corresponding model prompt code data as training samples, the following code snippets of the code sample data to be completed as sample labels, and training the preset pre-trained model using the loss function. During the training process, the server can input the training samples and their corresponding sample labels into the pre-trained model for forward propagation, and use the loss function in the back propagation of the pre-trained model to adjust the parameters of the pre-trained model until the pre-trained model meets certain iteration conditions, and the trained pre-trained model can be used as the code completion model.
[0106] S2: Perform a full permutation of each candidate code in the candidate code set to determine multiple code combinations.
[0107] Each code combination contains M candidate codes, and M is less than or equal to the number of candidate codes in the candidate code set.
[0108] For example, assuming that there are three candidate codes in the candidate code set, namely candidate code A, candidate code B and candidate code C, when M is set to 2, by fully permuting the candidate codes in the candidate code set, six code combinations can be obtained, as follows: [candidate code A, candidate code B], [candidate code B, candidate code A], [candidate code A, candidate code C], [candidate code C, candidate code A], [candidate code B, candidate code C], [candidate code C, candidate code B].
[0109] S3: Input each code combination into the code completion model for a forward propagation to determine the information loss value of each code combination according to each code combination and its corresponding output result.
[0110] Among them, the information loss value is used to quantify the degree of information loss of the code combination in the code completion model.
[0111] In this step, each code combination is first input into the code completion model for a forward propagation, and then the information loss value of each code combination in this forward propagation is calculated.
[0112] S4: Select the code combination with the smallest information loss value as the target code combination, and input the target code combination and the code to be completed into the code completion model to obtain the completion code corresponding to the code to be completed.
[0113] In this step, in the process of inputting the target code combination and the code to be completed into the code completion model, the target code combination serves as a model prompt, and the code to be completed serves as model input data.
[0114] It can be understood that, since the completion code is obtained based on the code to be completed when the completion code is obtained for the first time in the code completion task, the completion code for the code to be completed at this time can select the code below the determined code as the completion code. If a second or more iterations are to be performed, the completion code is obtained based on the code to be completed and the completion code of the previous iteration, so the above-mentioned processing is not required.
[0115] Specifically, since the code completion model is a large language model, when a code combination is input into the code completion model as a model prompt, the code completion model will convert the model prompt (code combination) into a matrix. This conversion process will cause a certain amount of information loss. Therefore, the server will fully arrange the candidate codes in the candidate code set, so that all possible arrangements can be listed to find the combination with the smallest information loss, and then calculate the information loss value of the code combination for each arrangement and combination respectively, and then select the code combination with the smallest information loss value as the final model prompt delivered to the code completion model, so as to achieve the purpose of retaining the effective information in the model prompt to the greatest extent, thereby enhancing the accuracy of the completed code output by the code completion model.
[0116] In one embodiment, before executing the step of inputting the target code combination and the code to be completed into the code completion model, the code completion method further includes:
[0117] S1: Identify the comment symbols that exist in the target code assembly.
[0118] S2: If it exists, determine the position of the code comment in the target code combination according to the comment symbol in the target code combination.
[0119] S3: based on the position of the code comment in the target code combination, delete the code comment in the target code combination.
[0120] The comment symbol refers to the symbol used in the code to mark the start or end of the comment.
[0121] In this embodiment, by detecting comment symbols, when a comment symbol is recognized, if the comment symbol is a single-line comment symbol, the code comment on the line where the comment symbol is located is directly deleted; if the comment symbol is a multi-line comment start symbol, the multi-line comment end symbol corresponding to the multi-line comment start symbol is further recognized, and then the comments between the multi-line comment start symbol and its corresponding multi-line comment end symbol are deleted.
[0122] Since many comments are difficult to read and contain little effective information, in order to improve the reasoning performance of the code completion model, you can choose to delete the comments in the target code combination to improve the efficiency of code completion.
[0123] In one embodiment, before executing the step of inputting the target code combination and the code to be completed into the code completion model, the code completion method further includes:
[0124] S1: Determine the attention share of each line of code in the target code combination.
[0125] S2: Delete the code lines whose attention share in the target code combination is lower than the preset threshold.
[0126] Among them, the attention ratio is used to measure the importance of the code line for the code completion task of the code completion model.
[0127] In this embodiment, the attention ratio of each line of code in the target code combination can be determined according to the type of keywords involved in each line of code. After the attention ratio of each line of code is calculated, the attention ratio of each line of code can be compared with a preset threshold, and then the code lines in the target code combination whose attention ratio is lower than the preset threshold are deleted. In this way, invalid information in the target code combination can be reduced, the reasoning efficiency and reasoning performance of the code completion model can be improved, and the efficiency of code completion can be improved.
[0128] The methods described in the above two embodiments are to improve the reasoning efficiency and reasoning performance of the code completion model from the direction of simplifying the model prompts. One or two of these methods can be used in actual applications, or the prompts can be simplified from other perspectives. This application does not impose any specific restrictions on this.
[0129] In one embodiment, the process of generating a code library includes:
[0130] S1: Get the project code.
[0131] S2: Determine the number of sliding rows and the number of window rows, and divide the project code in sequence according to the number of sliding rows and the number of window rows to obtain multiple code fragments.
[0132] Among them, the number of sliding rows is less than the number of window rows.
[0133] S3: Generate a code library based on the multiple code snippets obtained by division.
[0134] The number of sliding lines refers to the number of code lines that the sliding window slides each time, and the number of window lines refers to the number of code lines contained in the sliding window. In an example, the number of sliding lines can be set to 10, and the number of window lines can be set to 20.
[0135] In this embodiment, after comprehensively considering the length and integrity of the code snippet, a fixed number of lines is used to divide the project code. Since it is impossible to determine whether the division position is reasonable, in order to ensure the integrity of the code snippet, the number of sliding lines is set to be smaller than the number of window lines. This ensures that the project code is divided into code snippets while maintaining the integrity of the information contained therein, and can also control the length of the code snippet to avoid the code snippet being too long, which will lead to a large target code combination being obtained later, affecting the reasoning efficiency of the code completion model.
[0136] For example, suppose a code file has 40 lines of code. When the number of sliding lines is set to 10 and the number of window lines is set to 20, lines 1 to 20 are the first code fragment. After sliding 10 lines, lines 11 to 30 are the second code fragment. After sliding 10 lines again, lines 21 to 40 are the third code fragment. At this point, the division of the code file is completed.
[0137] In one example, if Figure 4 As shown, Figure 4 This is an example diagram of a code completion method provided in an embodiment of the present application. Figure 4 In the figure, LLM represents the large language model, that is, the code completion model in this application, the unfinished code is the code to be completed in this application, and the result refers to the output completed code. The warehouse file can be understood as the project code file. And after the result is obtained in the figure, the dotted line indicates that iteration can be performed to obtain a more accurate code completion result.
[0138] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0139] The following is a description of a code completion device provided in an embodiment of the present application. The code completion device described below and the code completion method described above can be referenced to each other.
[0140] like Figure 5 As shown, the present application provides a code completion device 400, the device comprising:
[0141] The code acquisition module 401 is used to acquire the code to be completed;
[0142] A code summarizing module 402 is used to summarize the code to be completed based on a preset code summarizing model, obtain first summary information of the code to be completed, and determine second summary information corresponding to each code snippet in a preset code library;
[0143] A similarity calculation module 403 is used to calculate the similarity between the first summary information and each second summary information respectively using multiple target algorithms, and determine a candidate code set corresponding to the code to be completed according to the similarity between the first summary information and each second summary information calculated by each target algorithm;
[0144] The code completion module 404 is used to arrange and combine the candidate codes in the candidate code set, and after selecting the target code combination with the minimum information loss from the arrangement and combination results of the candidate code set based on the information loss, determine the completion code corresponding to the code to be completed according to the target code combination.
[0145] In one embodiment, the code summary module includes:
[0146] A code parsing submodule is used to parse the code to be completed and each code snippet of the preset code library respectively by using a preset code summary model to obtain key features of the code to be completed and each code snippet of the code library;
[0147] A code analysis submodule is used to perform context analysis on the code to be completed and each code snippet of the code library using the code summary model to obtain context information of each code snippet of the code to be completed and the code library;
[0148] The code summary submodule is used to determine first summary information of the code to be completed according to key features and context information of the code to be completed, and to determine second summary information corresponding to each code snippet according to key features and context information of each code snippet in the code library.
[0149] In one embodiment, the similarity calculation module includes:
[0150] An algorithm evaluation submodule, used to determine a plurality of target algorithms according to the first summary information and each second summary information, and evaluate a contribution value of each target algorithm in the historical code completion task;
[0151] The weight determination submodule is used to determine the weight of each target algorithm according to the contribution value corresponding to each target algorithm;
[0152] A first calculation submodule, configured to use each target algorithm to calculate the similarity between each second summary information and the first summary information;
[0153] A second calculation submodule is used to perform weighted summation on the similarities calculated by each target algorithm according to the weight of each target algorithm, so as to obtain the target similarity between the second summary information and the first summary information;
[0154] The set determination submodule is used to select N code snippets with the highest target similarity from the code snippets corresponding to each second summary information when determining the target similarity between each second summary information and the first summary information, and generate a candidate code set corresponding to the code to be completed based on the selected code snippets.
[0155] In one embodiment, the code completion module includes:
[0156] A model determination submodule is used to determine a preset code completion model;
[0157] The code combination determination submodule is used to fully arrange each candidate code in the candidate code set to determine multiple code combinations; each code combination contains M candidate codes;
[0158] A loss value determination submodule, used for inputting each code combination into the code completion model for a forward propagation, so as to determine the information loss value of each code combination according to each code combination and its corresponding output result;
[0159] The code completion submodule is used to select the code combination with the smallest information loss value as the target code combination, and input the target code combination and the code to be completed into the code completion model to obtain the completion code corresponding to the code to be completed.
[0160] In one embodiment, before executing the step of inputting the target code combination and the code to be completed into the code completion model in the code completion submodule, the code completion device further includes:
[0161] An identification submodule for identifying annotation symbols present in the target code combination;
[0162] A position determination submodule, for determining the position of the code annotation in the target code combination according to the annotation symbol in the target code combination, if any;
[0163] The first deleting submodule is used to delete the code comments in the target code combination based on the positions of the code comments in the target code combination.
[0164] In one embodiment, before executing the step of inputting the target code combination and the code to be completed into the code completion model in the code completion submodule, the code completion device further includes:
[0165] The proportion determination submodule is used to determine the attention proportion of each line of code in the target code combination;
[0166] The second deletion submodule is used to delete the code lines whose attention ratio in the target code combination is lower than a preset threshold.
[0167] In one embodiment, the code summary module includes:
[0168] Code acquisition submodule, used to obtain project code;
[0169] The code division submodule is used to determine the number of sliding lines and the number of window lines, and divide the project code in sequence according to the number of sliding lines and the number of window lines to obtain multiple code fragments; the number of sliding lines is less than the number of window lines;
[0170] The code base generation submodule is used to generate a code base based on the multiple code snippets obtained by division.
[0171] The division of each module in the above-mentioned code completion device is only for illustration. In other embodiments, the code completion device can be divided into different modules as needed to complete all or part of the functions of the above-mentioned code completion device. Each module in the above-mentioned code completion device can be implemented in whole or in part by software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory in the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.
[0172] In one embodiment, the present application further provides a storage medium storing computer-readable instructions. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the code completion method described in any one of the above embodiments.
[0173] In one embodiment, the present application further provides a computer device having computer-readable instructions stored therein. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the code completion method described in any one of the above embodiments.
[0174] Indicatively, Figure 6 As shown, Figure 6 This is a schematic diagram of the internal structure of a computer device provided in an embodiment of the present application. The computer device 500 may be provided as a server. Figure 6 , the computer device 500 includes a processing component 502, which further includes one or more processors, and a memory resource represented by a memory 501, for storing instructions executable by the processing component 502, such as an application. The application stored in the memory 501 may include one or more modules, each corresponding to a set of instructions. In addition, the processing component 502 is configured to execute instructions to perform the code completion method of any of the above embodiments.
[0175] The computer device 500 may further include a power supply component 503 configured to perform power management of the computer device 500, a wired or wireless network interface 504 configured to connect the computer device 500 to a network, and an input / output (I / O) interface 505. The computer device 500 may operate based on an operating system stored in the memory 501, such as Windows Server TM, Mac OS X TM, Unix TM, Linux TM, Free BSD TM, or the like.
[0176] Those skilled in the art will understand that Figure 6The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.
[0177] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish an entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Moreover, the term "include", "comprise" or any other variant thereof is intended to cover non-exclusive inclusion, so that the process, method, article or equipment including a series of elements not only include those elements, but also include other elements that are not clearly listed, or also include elements inherent to such process, method, article or equipment. In the absence of more restrictions, the elements limited by the sentence "comprise one..." do not exclude the existence of other identical elements in the process, method, article or equipment including the elements. Herein, the singular "one", "one" and "described / the" may also include plural forms, unless the context clearly indicates another way. It should also be understood that the terms "include / comprise" or "have" etc. specify the existence of stated features, wholes, steps, operations, components, parts or combinations thereof, but do not exclude the possibility of the existence or addition of one or more other features, wholes, steps, operations, components, parts or combinations thereof. At the same time, the term "and / or" used in this specification includes any and all combinations of the relevant listed items.
[0178] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The various embodiments can be combined as needed, and the same or similar parts can refer to each other.
[0179] The above description of the disclosed embodiments enables those skilled in the art to implement or use the present application. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present application. Therefore, the present application will not be limited to the embodiments shown herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A code completion method, characterized in that: The method comprises: Get the code to be completed; Summarizing the code to be completed based on a preset code summarization model to obtain first summary information of the code to be completed, and determining second summary information corresponding to each code snippet in a preset code library; Using multiple target algorithms to respectively calculate the similarity between the first summary information and each of the second summary information, and determining a set of candidate codes corresponding to the code to be completed according to the similarity between the first summary information and each of the second summary information calculated by each target algorithm; The candidate codes in the candidate code set are arranged and combined, and after selecting a target code combination with the smallest information loss from the arrangement and combination results of the candidate code set based on information loss, a completion code corresponding to the code to be completed is determined according to the target code combination.
2. The code completion method according to claim 1, characterized in that: The step of summarizing the code to be completed based on a preset code summarization model to obtain first summary information of the code to be completed includes: Parsing the code to be completed using a preset code summary model to obtain key features of the code to be completed; and, using the code summary model to perform context analysis on the code to be completed to obtain context information of the code to be completed; The first summary information of the code to be completed is determined according to the key features and context information of the code to be completed.
3. The code completion method according to claim 1, characterized in that: The using multiple target algorithms to respectively calculate the similarity between the first summary information and each of the second summary information, and determining a set of candidate codes corresponding to the code to be completed according to the similarity between the first summary information and each of the second summary information calculated by each target algorithm, includes: Determine multiple target algorithms according to the first summary information and each of the second summary information, and evaluate the contribution value of each target algorithm in the historical code completion task; Determine the weight of each target algorithm based on the contribution value corresponding to each target algorithm; For each second summary information, using each target algorithm respectively to calculate the similarity between the second summary information and the first summary information; According to the weight of each target algorithm, weighted summation is performed on the similarities calculated by each target algorithm to obtain the target similarity between the second summary information and the first summary information; When determining the target similarity between each of the second summary information and the first summary information, N code snippets with the highest target similarity are selected from the code snippets corresponding to each of the second summary information, and a candidate code set corresponding to the code to be completed is generated based on the selected code snippets.
4. The code completion method according to claim 1, characterized in that: The method of permuting and combining the candidate codes in the candidate code set, and selecting a target code combination with the smallest information loss from the permutation and combination results of the candidate code set based on information loss, and then determining a completion code corresponding to the code to be completed according to the target code combination includes: Determine the preset code completion model; Performing a full permutation of each candidate code in the candidate code set to determine a plurality of code combinations; each code combination includes M candidate codes; Input each code combination into the code completion model for a forward propagation to determine the information loss value of each code combination according to each code combination and its corresponding output result; A code combination with the smallest information loss value is selected as a target code combination, and the target code combination and the code to be completed are input into the code completion model to obtain a completion code corresponding to the code to be completed.
5. The code completion method according to claim 4, characterized in that: Before executing the step of inputting the target code combination and the code to be completed into the code completion model, the method further includes: Identifying whether a comment symbol exists in the target code combination; If it exists, determining the position of the code comment in the target code combination according to the comment symbol in the target code combination; Based on the positions of the code comments in the target code combination, the code comments in the target code combination are deleted.
6. The code completion method according to claim 4 or 5, characterized in that: Before executing the step of inputting the target code combination and the code to be completed into the code completion model, the method further includes: Determine the attention ratio of each line of code in the target code combination; Delete the code lines whose attention ratio in the target code combination is lower than a preset threshold.
7. The code completion method according to claim 1, characterized in that: The process of generating the code base includes: Get the project code; Determine the number of sliding rows and the number of window rows, and divide the project code in sequence according to the number of sliding rows and the number of window rows to obtain multiple code fragments; the number of sliding rows is less than the number of window rows; A code base is generated based on the multiple code snippets obtained by the division.
8. A code completion device, characterized in that: The device comprises: Code acquisition module, used to obtain the code to be completed; A code summarizing module, used for summarizing the code to be completed based on a preset code summarizing model, obtaining first summary information of the code to be completed, and determining second summary information corresponding to each code fragment in a preset code library; a similarity calculation module, configured to respectively calculate the similarity between the first summary information and each of the second summary information using a plurality of target algorithms, and determine a set of candidate codes corresponding to the code to be completed according to the similarity between the first summary information and each of the second summary information calculated by each target algorithm; The code completion module is used to arrange and combine the candidate codes in the candidate code set, and after selecting a target code combination with the smallest information loss from the arrangement and combination results of the candidate code set based on information loss, determine the completion code corresponding to the code to be completed according to the target code combination.
9. A storage medium, characterized in that: The storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps of the code completion method according to any one of claims 1 to 7.
10. A computer device, characterized in that: include: one or more processors, and memory; The memory stores computer-readable instructions, and when the computer-readable instructions are executed by the one or more processors, the steps of the code completion method according to any one of claims 1 to 7 are performed.
Citation Information
Patent Citations
Code completion method and device, electronic equipment and computer readable storage medium
CN115291941A
Intelligent contract code completion method based on deep neural network and global cache
CN118092889A
Code completion method and device, electronic equipment and medium
CN118227106A
Generating vector representations of code capturing semantic similarity
US20200104631A1
Cited By
Self-adaptive code processing method and device based on artificial intelligence and intelligent agent
CN120909565A
An artificial intelligence-based adaptive code processing method and device, and an agent
CN120909565B
