Code completion method and device
By utilizing hash functions and large language models in the code repository and combining them with semantic information from the code context, efficient and accurate code completion is achieved, solving the low accuracy issue in existing technologies and improving development efficiency.
Patent Information
- Application Number
- CN202510725248.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-30
- Publication Date
- 2025-09-12
AI Technical Summary
Existing code completion technologies cannot effectively utilize semantic information in the code context, resulting in low completion accuracy and affecting development efficiency.
By using multiple hash functions to determine the hash value sequence and hash bucket of the target code snippet in a pre-configured code repository, candidate code snippets with high similarity are retrieved and used as prompt words for the large language model for code completion.
The accuracy and efficiency of code completion are improved, and the semantic understanding capabilities of large language models are utilized to improve developer efficiency.
Smart Images

Figure CN120631370A_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of this specification relate to the field of machine learning, and more particularly, to a code completion method and apparatus. Background Art
[0002] Code completion is a technology widely used in modern programming environments. It aims to automatically complete the code that developers are typing based on the code context, improve development efficiency, and reduce manual input errors.
[0003] Early code completion technologies were typically rule-based and statistically model-based. These approaches are limited in that they can only model fixed-window lengths and fail to understand the semantic information in the code context. Therefore, a method is needed to incorporate semantic information from the code context to improve code completion accuracy and, in turn, enhance developer efficiency. Summary of the Invention
[0004] One or more embodiments of this specification describe a code completion method and apparatus, which searches a preconfigured code repository for code snippets similar to the code snippet to be completed as prompts for input into a large language model (LLM) to instruct the large language model to output the completed code.
[0005] In a first aspect, a code completion method is provided, comprising:
[0006] According to the target code snippet to be completed, based on a plurality of preset hash functions, a target hash value sequence is determined, and then a plurality of target hash buckets to which the target code snippet belongs are determined;
[0007] Determining a plurality of candidate code snippets in a code repository based on the plurality of target hash buckets; the code repository includes a plurality of code snippets and a plurality of hash buckets to which each code snippet belongs, determined based on the plurality of hash functions;
[0008] Determine, based on the similarity between each candidate code fragment and the target code fragment, a plurality of reference code fragments with the highest similarity rankings;
[0009] A target prompt word including the target code snippet and the multiple reference code snippets is input into a large language model, and the large language model is instructed to output a completion code for the target code snippet.
[0010] In some possible implementations, determining a target hash value sequence includes:
[0011] Inputting the plurality of target code elements included in the target code segment into an arbitrary first hash function respectively, and determining a minimum value among the plurality of hash values obtained as a first target minimum hash value;
[0012] The target minimum hash values determined according to the hash functions are sorted in a predetermined order to determine the target hash value sequence.
[0013] In some possible implementations, determining the target hash buckets to which the target code fragment belongs includes:
[0014] Dividing the target hash value sequence into a plurality of target hash value sequence segments according to a preset first segmentation method;
[0015] The plurality of target hash buckets are determined according to the plurality of target hash value sequence segments.
[0016] In some possible implementations, the code repository is constructed through the following steps:
[0017] Get multiple code snippets;
[0018] For any first code segment, determining a first hash value sequence corresponding to the first code segment according to each code element contained therein and the multiple hash functions;
[0019] Determining, according to the first hash value sequence, a plurality of first hash buckets to which the first code snippet belongs;
[0020] An index is established between the plurality of first hash buckets and the first code segment.
[0021] In some possible implementations, obtaining multiple code snippets includes:
[0022] Get multiple complete code blocks;
[0023] According to the code hierarchy relationship in any first complete code block, the codes contained in each code hierarchy are respectively determined as corresponding code fragments to determine a plurality of code fragments corresponding to the first complete code block.
[0024] In some possible implementations, a number of candidate code snippets are obtained by searching the code repository, including:
[0025] The code snippets in each target hash bucket in the code repository are determined as candidate code snippets.
[0026] In some possible implementations, determining a plurality of reference code segments with top similarity rankings includes:
[0027] Determining a first number of code elements commonly contained between any first reference code segment and the target code segment;
[0028] A ratio of the first number to a second number of code elements included in the target code segment is determined as the similarity between the first reference code segment and the target code segment.
[0029] In some possible implementations, the following further comprises:
[0030] Obtaining user information of the user; the user information includes at least one of the following: role information, past code preference information, and current code feature information;
[0031] The target prompt word also includes the user information.
[0032] In some possible implementations, the following further comprises:
[0033] Determine the number of codes to be completed and the end position of the code according to the current cursor position in the target code snippet;
[0034] The target prompt word also includes the code quantity and the code end position.
[0035] In some possible implementations, the target code snippet is a database query language related code snippet; determining the amount of code to be completed and the code end position includes:
[0036] Determine the number of code lines that need to be completed based on the keywords contained in the code line where the current cursor is located, as the code number; the code line number includes a single line or multiple lines;
[0037] When the number of the code lines is multiple lines, the end position of the multiple lines of code is determined as the code end position.
[0038] In some possible implementations, the target code snippet is a database query language related code snippet; the method further includes:
[0039] Determining a target data table operated by the target code snippet according to a current cursor position in the target code snippet;
[0040] The target prompt word also includes table structure information of the target data table.
[0041] In some possible implementations, determining the target data table operated by the target code snippet includes:
[0042] Determining the code level of the target code fragment according to the keywords and code separators contained in the target code fragment;
[0043] The data table contained in the target code level containing the current cursor is determined as the target data table.
[0044] In a second aspect, a code completion device is provided, comprising:
[0045] a hash bucket determining unit configured to determine, according to a target code segment to be completed and based on a plurality of preset hash functions, a target hash value sequence, and further determine a plurality of target hash buckets to which the target code segment belongs;
[0046] a candidate code determination unit configured to determine, based on the plurality of target hash buckets, a plurality of candidate code snippets in a code repository; the code repository including a plurality of code snippets and a plurality of hash buckets to which each code snippet belongs, determined based on the plurality of hash functions;
[0047] A reference code determination unit is configured to determine, based on the similarities between each candidate code segment and the target code segment, a plurality of reference code segments with the highest similarity rankings;
[0048] The code completion unit is configured to input a target prompt word containing the target code snippet and the multiple reference code snippets into a large language model, and instruct the large language model to output a completion code for the target code snippet.
[0049] In a third aspect, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed in a computer, the computer is caused to execute the method of the first aspect.
[0050] In a fourth aspect, a computing device is provided, comprising a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, the method of the first aspect is implemented.
[0051] The code completion method and apparatus proposed in the embodiments of this specification use multiple preset hash functions to perform hash calculations on the target code snippet to be completed, obtaining a target hash value sequence containing multiple hash values. This target hash value sequence can be used to determine the target hash buckets to which the target code snippet belongs. Then, based on these target hash values, a preconfigured code repository is searched to obtain candidate code snippets belonging to the same target hash buckets, which serve as preliminary screening of code snippets similar to the target code snippet. The code repository contains multiple code snippets, and each code snippet determines its respective hash bucket based on the same multiple hash functions.
[0052] Next, the similarity between these candidate code snippets obtained through preliminary screening and the target code snippets is calculated respectively, and then the top-ranked reference code snippets are input into the large language model, instructing the large language model to complete the target code snippet based on these reference code snippets to obtain the completed code.
[0053] The embodiments of this specification utilize a double search process of initial screening and secondary screening to ensure both accuracy and efficiency of similar code retrieval, enabling rapid and accurate retrieval of similar codes. Furthermore, leveraging the semantic understanding capabilities of a large language model can further improve the relevance and adoption rate of completed codes. BRIEF DESCRIPTION OF THE DRAWINGS
[0054] In order to more clearly illustrate the technical solutions of the multiple embodiments disclosed in this specification, the following will briefly introduce the drawings required for the description of the embodiments. Obviously, the drawings described below are only the multiple embodiments disclosed in this specification. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0055] Figure 1 A schematic diagram illustrating an implementation scenario of a code completion method according to an embodiment is shown;
[0056] Figure 2 A flowchart illustrating a method for constructing a code repository according to one embodiment;
[0057] Figure 3 A flowchart illustrating determining a plurality of code snippets according to a hierarchical relationship of a complete code block according to one embodiment is shown;
[0058] Figure 4 A flowchart showing a code completion method according to one embodiment is shown;
[0059] Figure 5 A schematic diagram illustrating determining several target hash buckets according to one embodiment is shown;
[0060] Figure 6 A schematic block diagram of a code completion apparatus according to an embodiment is shown. DETAILED DESCRIPTION
[0061] The solution provided in this specification is described below in conjunction with the accompanying drawings.
[0062] Figure 1 A schematic diagram of an implementation scenario of a code completion method according to an embodiment is shown. Figure 1In the example, the code snippet to be completed can be an unfinished code snippet written in any programming language. The programming language can be a programming language such as Java or C++, or a query language such as SQL, without limitation.
[0063] First, use multiple preset hash functions to act on the code snippets to be completed respectively to obtain multiple hash values. These hash values can form a hash value sequence after being arranged in a predetermined order. Then, multiple target hash buckets are determined based on the hash value sequence, for example, n target hash buckets, and the code snippets to be completed belong to these n target hash buckets. Next, based on these n target hash buckets, retrieve several code snippets that also belong to any one of the n target hash buckets in the pre-configured code repository as candidate code snippets, for example, there are p candidate code snippets. The code repository includes multiple code snippets and m hash buckets. Each code snippet is pre-determined based on the same multiple hash functions as mentioned above, and then determines the several hash buckets to which it belongs. The specific process of constructing the code repository will be described in detail in the subsequent part of this specification.
[0064] Since the same hash function and hash bucket division method are used, the code snippets belonging to the same hash bucket are similar to each other. Therefore, candidate code snippets 1 to candidate code snippets p are similar to the code snippet to be completed.
[0065] The above completes the preliminary search for similar code snippets from the code repository. When the code repository has a large number of code snippets, the number of candidate code snippets obtained from the preliminary search will still be large, and there may be candidate code snippets with low similarity. Therefore, the p candidate code snippets can be further screened.
[0066] Next, the p candidate code snippets are compared with the code snippet to be completed for similarity, and the top q code snippets are selected as reference code snippets. These q reference code snippets and the code snippet to be completed together form a prompt word, which is input into the pre-trained large language model. The large language model is instructed to complete the code snippet to be completed using the q reference code snippets as a reference, and then output the completed code for the code snippet to be completed.
[0067] This retrieval method, based on multiple hash functions and hash buckets, can quickly retrieve code snippets similar to the one to be completed from a vast existing code repository. Further similarity comparisons are performed to identify the top-ranked snippets, which serve as a reference for code completion using the large language model. This improves retrieval efficiency and the accuracy of the final completed code.
[0068] The following describes the specific implementation steps of the above code completion method in conjunction with specific embodiments.
[0069] Before describing the specific implementation steps of the code completion method, the specific implementation steps of constructing the above code repository are first described. Figure 2 A flowchart of a method for constructing a code repository according to an embodiment is shown. The execution subject of the method can be any platform, server, or device cluster with computing and processing capabilities. Figure 2 As shown, the method at least includes: step 202, obtaining multiple code snippets; step 204, for any first code snippet, determining a first hash value sequence corresponding to the first code snippet based on the various code elements contained therein and the multiple hash functions; step 206, determining a number of first hash buckets to which the first code snippet belongs based on the first hash value sequence; step 208, establishing indexes between the number of first hash buckets and the first code snippet.
[0070] The specific execution process of each of the above steps is described below.
[0071] First, in step 202, a plurality of code snippets are obtained.
[0072] The code snippets may be code snippets generated during a previous development process, such as multiple code snippets obtained by dividing a complete code file according to a predetermined division rule. The code snippets may be based on the same programming language or different programming languages, without limitation.
[0073] Then, in step 204, for any first code segment, a first hash value sequence corresponding to the first code segment is determined according to each code element contained therein and a plurality of preset hash functions.
[0074] The preset multiple hash functions may be, for example, hash functions h1 to h2. s , a total of s. The first code fragment is processed using the s hash functions respectively to obtain corresponding s first hash values. The s first hash values are sorted in a predetermined order, for example, from hash function h1 to hash function h s The first hash value sequence corresponding to the first code fragment is determined.
[0075] Next, in step 206 , a number of first hash buckets to which the first code snippet belongs are determined based on the first hash value sequence.
[0076] In one embodiment, each first hash value in the first hash value sequence may be determined as a corresponding first hash bucket, and the first code segment may belong to s first hash buckets.
[0077] In another embodiment, the first hash value sequence may be divided into b sequence segments, and then each sequence segment is determined as a corresponding first hash bucket. Then, the first code snippet will belong to the b first hash buckets.
[0078] In this embodiment, determining the first hash buckets to which the first code snippet belongs in step 206 specifically includes: dividing the first hash value sequence into a plurality of first hash value sequence segments according to a preset first segmentation method; and determining the first hash buckets based on the plurality of first hash value sequence segments.
[0079] Finally, in step 208 , an index is established between the plurality of first hash buckets and the first code snippet.
[0080] The above index may be an inverted index between each first hash bucket and the first code snippet. The first code snippet may be retrieved from the code repository through any of the first hash buckets.
[0081] It should be noted that, when establishing the indexes between the first hash buckets and the first code snippet in step 208, it is also necessary to record the position of each first hash bucket in the original first hash value sequence. When subsequently searching in the code repository based on the hash buckets, the position of the hash bucket to be searched is first determined, and then the corresponding code snippet is retrieved from the hash bucket corresponding to the position.
[0082] For example, when the first hash value sequence is divided into three first hash value sequence segments, and the three first hash value sequence segments are respectively determined as first hash bucket 1, first hash bucket 2, and first hash bucket 3. When establishing indexes, the index at the first position is the index between first hash bucket 1 and the first code snippet, the index at the second position is the index between first hash bucket 2 and the first code snippet, and the index at the third position is the index between first hash bucket 3 and the first code snippet. Therefore, the first code snippet belongs to first hash bucket 1 at the first position, first hash bucket 2 at the second position, and first hash bucket 3 at the third position, respectively.
[0083] For the multiple code snippets in step 202 , indexes between hash buckets and code snippets are established using the methods described in steps 204 to 208 respectively, so as to construct a code repository.
[0084] In a specific example, assume there are six hash functions, h1 through h6, and the first segmentation method is set to group every two hash values. Hash functions h1 through h6 are used to process the first code snippet, respectively, obtaining hash values 3, 0, 1, 6, 3, and 2. These six hash values are then sorted in the order of h1 through h6 to determine the first hash value sequence {3, 0, 1, 6, 3, 2} corresponding to the first code snippet. Next, the first hash value sequence {3, 0, 1, 6, 3, 2} is segmented into groups of two hash values, resulting in three first hash value sequence segments: {3, 0}, {1, 6}, and {3, 2}. The three sequence segments are respectively determined as hash buckets {3,0}, {1,6} and {3,2}, and indexes are established between them and the first code fragment, specifically the index between the hash bucket {3,0} at the first position and the first code fragment, the index between the hash bucket {1,6} at the second position and the first code fragment, and the index between the hash bucket {3,2} at the third position and the first code fragment.
[0085] For a second code snippet different from the first code snippet, assuming the same process yields three second hash value sequence segments: {3,0}, {2,6}, and {2,2}. When indexing, the second code snippet and the first code snippet belong to the same hash bucket {3,0} at position 1, to hash bucket {2,6} at position 2, and to hash bucket {2,2} at position 3.
[0086] The above describes the process of establishing a code repository based on multiple code snippets. In some possible implementations, the complete code in the code file can be divided more reasonably to improve the integrity of each code snippet obtained by the division.
[0087] In one embodiment, the step 202 of obtaining multiple code snippets includes:
[0088] Acquire multiple complete code blocks; and determine the codes contained in each code level as corresponding code fragments according to the code level relationship in any first complete code block to determine a plurality of code fragments corresponding to the first complete code block.
[0089] A complete code block can be relatively independent and complete code content. For example, a class or method in the Java language, or a complete query statement (such as a SELECT statement) in a database query language (SQL language). A complete code block often has a hierarchical relationship within it, and the code contained in any level is also a relatively complete code unit. Therefore, based on the code hierarchy within the complete code block, multiple relatively complete code units can be extracted from the complete code block from the bottom up, as the code snippets corresponding to the complete code block.
[0090] For example, Figure 3 A flowchart of determining several code snippets according to the hierarchical relationship of a complete code block according to one embodiment is shown. Figure 3 In the example, located Figure 3 The complete code block on the left is a query statement based on the SQL language, specifically:
[0091]
[0092]
[0093] This query statement is used to query data from a table determined by an inner query statement. And this inner query statement queries data from the JOIN result of the table determined by two inner query statements. Parsing the syntax of this SQL statement can get the following Figure 3 The SQL syntax tree in the middle describes the code hierarchy of the code block on the left, which is a tree-like hierarchical relationship.
[0094] Starting from the leaf nodes of the tree, the codes contained in the code level represented by each node are determined as several code fragments corresponding to the complete code block.
[0095] Specifically, the code snippet contained in the code level represented by the left leaf node of the tree is (corresponding to Figure 3 Code snippet 1 on the right):
[0096] SELECT *
[0097] FROM table1
[0098] JOIN table2
[0099] ON table1.a=table2.a
[0100] WHERE table1.b=123
[0101] The code snippet contained in the code level represented by the right leaf node of the tree is (corresponding to Figure 3 Code snippet 2 on the right):
[0102] SELECT *
[0103] FROM table3
[0104] The code snippets contained in the code level represented by the second-level node of the tree are (corresponding to Figure 3 Code snippet 3 on the right):
[0105]
[0106] The code fragment contained in the code level represented by the first-level node of the tree is the complete code block itself (corresponding to Figure 3 Code snippet 4 on the right).
[0107] It can be seen that each code snippet has relatively complete functions and meanings.
[0108] In one embodiment, since the number of code elements (e.g., method names, variable names, keywords, etc.) contained in each code snippet may vary greatly, directly inputting the code snippet into a hash function to obtain a hash value may not be able to effectively bucket the code snippets. Therefore, in this embodiment, determining the first hash value sequence corresponding to the first code snippet in step 204 includes:
[0109] Input each code element included in the first code fragment into any second hash function, and determine the minimum value among the multiple hash values obtained as the second minimum hash value; sort the minimum hash values determined according to the various hash functions in a predetermined order to determine the first hash value sequence.
[0110] In other embodiments, other methods may be used to determine the corresponding hash value according to the hash function. For example, the maximum value among the obtained multiple hash values may be determined as the corresponding hash value. This is not limited here.
[0111] The above describes the specific implementation steps of constructing a code repository. The following describes the specific implementation steps of the incomplete code method based on the code repository.
[0112] Figure 4 A flowchart of a code completion method according to an embodiment is shown. The execution subject of the method can be any platform or server or device cluster with computing and processing capabilities. Figure 4As shown, the method at least includes: step 402, determining a target hash value sequence based on a target code snippet to be completed based on multiple preset hash functions, and then determining a number of target hash buckets to which the target code snippet belongs; step 404, determining a number of candidate code snippets in a code repository based on the multiple target hash buckets; the code repository includes multiple code snippets and a number of hash buckets to which each code snippet belongs determined based on the multiple hash functions; step 406, determining a number of reference code snippets with a high similarity ranking based on the similarity between each candidate code snippet and the target code snippet; step 408, inputting a target prompt word containing the target code snippet and the multiple reference code snippets into a large language model, instructing the large language model to output a completion code for the target code snippet.
[0113] The specific execution process of each of the above steps is described below.
[0114] First, in step 402, according to the target code segment to be completed, based on a plurality of preset hash functions, a target hash value sequence is determined, and then a number of target hash buckets to which the target code segment belongs are determined.
[0115] The target code snippet can be written in any programming language and is not limited here.
[0116] The target hash value sequence may be obtained by using the multiple hash functions to process the target code segments respectively, and sorting the obtained multiple hash values in a predetermined order.
[0117] In one embodiment, determining the target hash value sequence in step 402 includes:
[0118] Inputting multiple target code elements contained in the target code segment into any first hash function respectively, determining the minimum value among the multiple hash values obtained as a first target minimum hash value; and sorting the target minimum hash values determined according to the respective hash functions in a predetermined order to determine the target hash value sequence.
[0119] The target code elements contained in the target code fragment are input into respective hash functions to determine corresponding target minimum hash values according to the respective hash functions. These target minimum hash values are then sorted according to a predetermined order to determine the target hash value sequence. The predetermined order is the same order used when processing the code fragments in the code repository.
[0120] In another implementation, the target code segments may be directly input into respective hash functions, and the obtained hash values may be sorted in a predetermined order to determine the target hash value sequence.
[0121] The plurality of target hash buckets in step 402 may be determined according to a target hash value sequence.
[0122] In one embodiment, determining the target hash buckets to which the target code snippet belongs in step 402 includes:
[0123] The target hash value sequence is divided into a plurality of target hash value sequence segments according to a preset first dividing method; and the plurality of target hash buckets are determined according to the plurality of target hash value sequence segments.
[0124] The first segmentation method can be any method for segmenting the target hash value sequence into a plurality of target hash value sequence segments, and it only needs to use the same segmentation method as when constructing the code repository. The lengths of the target hash value sequence segments can be the same or different, and there is no limitation here.
[0125] For example, when the total number of hash functions is h, the length of the corresponding target hash value sequence is also h. The sequence can be divided into b target hash value sequence segments, and the length of each target hash value sequence segment is h / b (the values of h and b can be preset so that h / b is an integer).
[0126] In another embodiment, each hash value in the target hash value sequence may be determined as a corresponding target hash bucket.
[0127] Similar to when constructing the code repository, each target hash bucket determined in step 402 also has position information. Each target hash bucket is ranked first, second, and so on according to its position in the original target hash value sequence.
[0128] The process of step 402 is illustrated below by using an example. Figure 5 FIG. 1 shows a schematic diagram of determining several target hash buckets according to an embodiment. Figure 5 As shown, there are six hash functions. After the target code snippet passes through each hash function and takes the minimum hash value, the resulting target hash value sequence is {0, 5, 1, 0, 0, 1}. The target hash value sequence is then divided into three target hash value sequence segments, each containing two hash values: {0, 5}, {1, 0}, and {0, 1}. These three target hash value sequence segments are then used as the target hash buckets at positions 1, 2, and 3, respectively. The target code snippet at position 1 belongs to hash bucket {0, 5}, at position 2 to hash bucket {1, 0}, and at position 3 to hash bucket {0, 1}.
[0129] Then, in step 404, several candidate code snippets are determined in the code repository based on the several target hash buckets; the code repository includes multiple code snippets and several hash buckets to which each code snippet belongs determined based on the multiple hash functions.
[0130] Since the method for determining the hash bucket to which each code snippet belongs in the code repository is the same as the method for determining the target hash bucket to which the target code snippet belongs, the code snippets at the same location that belong to the same hash bucket are similar code snippets determined based on the multiple hash functions.
[0131] In one embodiment, searching a code repository to obtain a plurality of candidate code snippets includes: determining a code snippet located in each target hash bucket in the code repository as a candidate code snippet.
[0132] Each target hash bucket has its own position, and the code snippets in each target hash bucket at the corresponding position in the code repository are determined as candidate code snippets.
[0133] For example, in Figure 5 In the example, the code snippets in the {0,5} hash bucket at the first position, the {1,0} hash bucket at the second position, and the {0,1} hash bucket at the third position in the code repository are determined as candidate code snippets.
[0134] After a number of candidate code snippets are determined from the code repository, next, in step 406 , a number of reference code snippets with high similarity rankings are determined based on the similarities between each candidate code snippet and the target code snippet.
[0135] For any first reference code segment in each candidate code segment, the similarity between the two can be determined based on the proportion of identical code elements between the code elements included in the first reference code segment and the code elements included in the target code segment.
[0136] In one embodiment, determining the plurality of reference code segments with the highest similarity rankings in step 406 includes:
[0137] Determine a first number of code elements commonly contained in any first reference code segment and the target code segment; and determine a ratio of the first number to a second number of code elements contained in the target code segment as the similarity between the first reference code segment and the target code segment.
[0138] Since the target code snippet is a code snippet to be completed, it often contains relatively few code elements. The reference code snippet, on the other hand, is a relatively complete code snippet and contains relatively more code elements. The difference between the two makes the traditional Jaccard similarity less effective. Therefore, the embodiment of this specification uses the ratio of the first number to the second number of code elements contained in the target code snippet as the similarity between the first reference code snippet and the target code snippet to improve the effectiveness of the selected reference code snippet.
[0139] In other embodiments, step 406 determines the sum of the number of code elements contained in the first reference code fragment and the target code fragment as the third number, and determines the ratio of the first number of code elements commonly contained in the first reference code fragment and the target code fragment to the third number as the similarity between the first reference code fragment and the target code fragment.
[0140] The multiple reference code snippets screened out in step 406 can be used as references when the large language model completes the target code snippet.
[0141] Finally, in step 408 , the target prompt word including the target code snippet and the multiple reference code snippets is input into the large language model, instructing the large language model to output the completion code of the target code snippet.
[0142] The target prompt word may include the target code snippet and the multiple reference code snippets, and may include instruction text instructing the large language model to perform a completion operation.
[0143] In one embodiment, the instruction text may be: "Please understand the structure, function, and implementation details of the following reference code snippets, and use this information to complete the missing parts of the target code."
[0144] In other implementations, other forms of target prompt words may be constructed as long as they can instruct the large language model to perform code completion.
[0145] Through the above steps 402 to 408, similar code snippets to the target code snippet can be quickly and accurately retrieved from a huge code repository, and used as a reference for the large language model during code completion, thereby improving the adoption rate of the final completed code.
[0146] In some possible implementations, different users have different coding styles or preferences, such as naming and structural styles. Furthermore, the user's role (specific occupation) can also influence the specific functionality of the code they write. Adding this information to the target prompt and feeding it into the large language model can further improve completion accuracy.
[0147] In some possible implementations, the method further includes:
[0148] Step a, obtaining user information of the user; the user information includes at least one of the following: role information, past code preference information, and current code feature information.
[0149] In this embodiment, the target prompt word in step 408 also includes the user information.
[0150] The user's role information includes at least one of the following: data processing, engineering development, algorithm development, product manager, and operation.
[0151] The past code preference information includes at least one of the following: naming style, structural style, indentation style, and line breaking style.
[0152] The current code feature information includes at least one of the following: whether there is a code with repeated form.
[0153] In some possible implementations, the method further includes:
[0154] Step b: determining the amount of code to be completed and the end position of the code according to the current cursor position in the target code fragment.
[0155] In this embodiment, the target prompt word in step 408 also includes the code number and the code end position.
[0156] In some more specific implementations, the target code snippet is a database query language related code snippet; and the step b of determining the amount of code to be completed and the end position of the code includes:
[0157] According to the keywords contained in the code line where the current cursor is located, the number of code lines that need to be completed is determined as the code number; the number of code lines includes a single line or multiple lines; when the number of code lines is multiple lines, the end position of the multiple lines of code is determined as the code end position.
[0158] For example, when the keyword is IF, ON, or other keywords, a single line of code needs to be completed; when the keyword is SELECT, FROM, or other keywords, multiple lines of code need to be completed.
[0159] In some possible implementations, the target code snippet is a database query language related code snippet; the method further includes:
[0160] Step c: determining the target data table operated by the target code snippet according to the current cursor position in the target code snippet.
[0161] In this embodiment, the target prompt word in step 408 also includes table structure information of the target data table.
[0162] The table structure information of the target data table includes at least one of the following: table name, column name, data type, whether null values are allowed, default value, constraint, primary key, index, and comment.
[0163] In some more specific implementations, determining the target data table operated by the target code snippet in step c includes:
[0164] The code level of the target code segment is determined according to the keywords and code delimiters contained in the target code segment; and the data table contained in the target code level containing the current cursor is determined as the target data table.
[0165] The current cursor position is the location where the code is being written. The difference between the writing process of database query language and conventional programming language is that the writing method of conventional programming language is from top to bottom, according to the actual execution logic. The writing process of database query language is usually written from the outside to the inside, layer by layer, and each layer is used to operate the corresponding data table. For example Figure 3 The code snippet on the left side of the code snippet will be written first during the writing process.
[0166]
[0167] Content, then write the brackets in FROM
[0168]
[0169] Then write the contents in the inner FROM brackets and JOIN brackets.
[0170] Therefore, through step c, the target data table that the user currently wants to operate can be determined, and the table structure information of the target data table is also added to the target prompt word to further improve the completion accuracy.
[0171] According to another embodiment, a code completion device is also provided. Figure 6 FIG. 1 is a schematic block diagram of a code completion device according to an embodiment, which can be deployed in any device, platform or device cluster with computing and processing capabilities. Figure 6As shown, the apparatus 600 includes:
[0172] The hash bucket determining unit 602 is configured to determine a target hash value sequence according to a target code segment to be completed based on a plurality of preset hash functions, and further determine a number of target hash buckets to which the target code segment belongs;
[0173] The candidate code determination unit 604 is configured to determine a plurality of candidate code snippets in a code repository according to the plurality of target hash buckets; the code repository includes a plurality of code snippets and a plurality of hash buckets to which each code snippet belongs, determined based on the plurality of hash functions;
[0174] The reference code determining unit 606 is configured to determine a plurality of reference code segments with the highest similarity rankings based on the similarities between the candidate code segments and the target code segment;
[0175] The code completion unit 608 is configured to input the target prompt word containing the target code snippet and the multiple reference code snippets into the large language model, and instruct the large language model to output the completion code of the target code snippet.
[0176] In some possible implementations, the apparatus 600 further includes:
[0177] The acquiring unit is configured to acquire user information of the user; the user information includes at least one of the following: role information, past code preference information, and current code feature information;
[0178] The target prompt word also includes the user information.
[0179] In some possible implementations, the apparatus 600 further includes:
[0180] A first determining unit is configured to determine the amount of code to be completed and the end position of the code according to the position of the current cursor in the target code fragment;
[0181] The target prompt word also includes the code quantity and the code end position.
[0182] In some possible implementations, the target code segment is a database query language related code segment; the apparatus 600 further includes:
[0183] a second determining unit configured to determine, according to a current cursor position in the target code snippet, a target data table operated by the target code snippet;
[0184] The target prompt word also includes table structure information of the target data table.
[0185] According to another embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed in a computer, the computer is caused to execute the method described in any one of the above embodiments.
[0186] According to yet another embodiment, a computing device is provided, including a memory and a processor, wherein the memory stores executable code, and when the processor executes the executable code, the method described in any one of the above embodiments is implemented.
[0187] The various embodiments in this specification are described in a progressive manner. Similar parts between the various embodiments can be referred to in conjunction with each other. Each embodiment focuses on the differences from other embodiments. In particular, the device embodiments are generally similar to the method embodiments, so the description is relatively simple. For relevant parts, refer to the description of the method embodiments.
[0188] The foregoing description of this specification describes specific embodiments. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0189] It should be noted that, in this document, relational terms such as first and second, etc., are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprises," "comprising," or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. In the absence of further limitations, an element defined by the phrase "comprising a ..." does not exclude the presence of other identical elements in the process, method, article, or device comprising the element.
[0190] Those skilled in the art will understand that all or part of the steps to implement the above embodiments may be accomplished by hardware, or may be accomplished by a program instructing the relevant hardware, and the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a disk, or an optical disk, etc.
[0191] The specific implementation methods described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific implementation method of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A code completion method, comprising: According to the target code snippet to be completed, based on a plurality of preset hash functions, a target hash value sequence is determined, and then a plurality of target hash buckets to which the target code snippet belongs are determined; Determining a plurality of candidate code snippets in a code repository based on the plurality of target hash buckets; the code repository includes a plurality of code snippets and a plurality of hash buckets to which each code snippet belongs, determined based on the plurality of hash functions; Determine, based on the similarity between each candidate code fragment and the target code fragment, a plurality of reference code fragments with the highest similarity rankings; A target prompt word including the target code snippet and the multiple reference code snippets is input into a large language model, and the large language model is instructed to output a completion code for the target code snippet.
2. The method according to claim 1, wherein Determine the target hash value sequence, including: Inputting the plurality of target code elements included in the target code segment into an arbitrary first hash function respectively, and determining a minimum value among the plurality of hash values obtained as a first target minimum hash value; The target minimum hash values determined according to the hash functions are sorted in a predetermined order to determine the target hash value sequence.
3. The method according to claim 1, wherein Determining a number of target hash buckets to which the target code fragment belongs, including: Dividing the target hash value sequence into a plurality of target hash value sequence segments according to a preset first segmentation method; The plurality of target hash buckets are determined according to the plurality of target hash value sequence segments.
4. The method according to claim 1, wherein The code repository is constructed through the following steps: Get multiple code snippets; For any first code segment, determining a first hash value sequence corresponding to the first code segment according to each code element contained therein and the multiple hash functions; Determining, according to the first hash value sequence, a plurality of first hash buckets to which the first code snippet belongs; An index is established between the plurality of first hash buckets and the first code segment.
5. The method according to claim 4, wherein Get multiple code snippets, including: Get multiple complete code blocks; According to the code hierarchy relationship in any first complete code block, the codes contained in each code hierarchy are respectively determined as corresponding code fragments to determine a plurality of code fragments corresponding to the first complete code block.
6. The method according to claim 1, wherein Searching the code repository yields several candidate code snippets, including: The code snippets in each target hash bucket in the code repository are determined as candidate code snippets.
7. The method according to claim 1, wherein Identify multiple reference code snippets with the highest similarity ranking, including: Determining a first number of code elements commonly contained between any first reference code segment and the target code segment; A ratio of the first number to a second number of code elements included in the target code segment is determined as the similarity between the first reference code segment and the target code segment.
8. The method according to claim 1, further comprising: Get the user's user information; The user information includes at least one of the following: role information, past code preference information, and current code feature information; The target prompt word also includes the user information.
9. The method according to claim 1, further comprising: Determine the number of codes to be completed and the end position of the code according to the current cursor position in the target code snippet; The target prompt word also includes the code quantity and the code end position.
10. The method according to claim 9, wherein: The target code snippet is a database query language related code snippet; Determine the amount of code that needs to be completed and where the code ends, including: Determine the number of code lines that need to be completed based on the keywords contained in the code line where the current cursor is located, as the code number; the code line number includes a single line or multiple lines; When the number of the code lines is multiple lines, the end position of the multiple lines of code is determined as the code end position.
11. The method according to claim 1, wherein The target code segment is a database query language related code segment; the method further includes: Determining a target data table operated by the target code snippet according to a current cursor position in the target code snippet; The target prompt word also includes table structure information of the target data table.
12. The method according to claim 11, wherein Determining a target data table operated by the target code snippet includes: Determining the code level of the target code fragment according to the keywords and code separators contained in the target code fragment; The data table contained in the target code level containing the current cursor is determined as the target data table.
13. A code completion device, comprising: a hash bucket determining unit configured to determine, according to a target code segment to be completed and based on a plurality of preset hash functions, a target hash value sequence, and further determine a plurality of target hash buckets to which the target code segment belongs; a candidate code determination unit configured to determine, based on the plurality of target hash buckets, a plurality of candidate code snippets in a code repository; the code repository including a plurality of code snippets and a plurality of hash buckets to which each code snippet belongs, determined based on the plurality of hash functions; A reference code determination unit is configured to determine, based on the similarities between each candidate code segment and the target code segment, a plurality of reference code segments with the highest similarity rankings; The code completion unit is configured to input a target prompt word containing the target code snippet and the multiple reference code snippets into a large language model, and instruct the large language model to output a completion code for the target code snippet.
14. A computer-readable storage medium having a computer program stored thereon, which, when executed in a computer, causes the computer to execute the method according to any one of claims 1 to 12.
15. A computing device comprising a memory and a processor, wherein: The memory stores executable code, and when the processor executes the executable code, the method according to any one of claims 1 to 12 is implemented.