Code completion method and device, storage medium and equipment
By using the embedded vector of the object code file and the code snippet candidate library built in the IDE project in the code completion method, the problem of insufficient code completion accuracy in the existing technology is solved, and more efficient and accurate code completion is achieved. Effect.
Patent Information
- Application Number
- CN202411584829.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-07
- Publication Date
- 2025-02-18
AI Technical Summary
The existing code completion methods are insufficiently accurate when retrieving similar code snippets, resulting in inaccurate results of large language models, affecting the code completion effect.
By getting the context and snippet of the object code file, extracting its embedding vectors, and obtaining candidate code snippets similar to the object code snippet based on the pre-built code snippet candidate library. The code snippet candidate library is built based on all code files in the IDE project, expanding the search scope.
The retrieval efficiency and accuracy of similar code snippets are improved, making the generation results of large language models more accurate, thereby improving the effect of code completion.
Smart Images

Figure CN119474351A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of code development technology, and in particular to a code completion method, device, storage medium and equipment. Background Art
[0002] With the continuous deepening of informatization in various industries, the demand for different types of software such as customized software, commercial software, and embedded software has become increasingly strong. In the process of software development, the Integrated Development Environment (IDE) plays a very important role, and code completion is one of the most widely used functions. It can provide programmers with timely predictions of class names, method names, etc. during the software development process, reducing the typing burden of developers, while reducing spelling errors, and intuitively improving the efficiency of software development.
[0003] However, the existing code completion method mainly uses Jaccard similarity to find code snippets similar to the code to be completed in the code file opened in the IDE, and then splices them with the code snippet to be completed and the cursor position as a prompt, inputs the code generation large language model to generate code, and achieves the goal of code completion. However, this method does not accurately retrieve similar code snippets, which in turn leads to inaccurate generation results of the large model, thus affecting the final code completion effect. Summary of the invention
[0004] The main purpose of the embodiments of the present application is to provide a code completion method, apparatus, storage medium and device, which can improve the retrieval efficiency and accuracy of similar code fragments, thereby making the generation results of large models more accurate, thereby improving the final code completion effect.
[0005] The present application embodiment provides a code completion method, including:
[0006] Obtaining a target code file to be completed; extracting a target code context from the target code file, and extracting a target code fragment from the target code context;
[0007] Extracting a target embedding vector of the target code snippet, and acquiring a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector; the code snippet candidate library is built based on all code files included in an integrated development environment IDE project where the target code file is located;
[0008] The target code context and the candidate code snippets are used to construct a prompt instruction prompt, which is input into a code generation large language model to obtain a target code file after code completion output by the model.
[0009] In a possible implementation, the code snippet candidate library is constructed as follows, including:
[0010] All code files contained in the IDE project where the target code file is located are segmented according to the characteristics of the coding language to obtain N segmented code fragments; N is a positive integer greater than 0;
[0011] Extracting the segmentation embedding vectors corresponding to each of the N segmented code snippets;
[0012] The N segmented code snippets and their corresponding segmentation embedding vectors are used to form a code snippet candidate library.
[0013] In a possible implementation, the using the N segmented code snippets and their corresponding segmentation embedding vectors to form a code snippet candidate library includes:
[0014] The N segmented code snippets are numbered, and each segmented code snippet and its corresponding segmentation embedding vector and source code file are numbered to form a code snippet candidate library.
[0015] In a possible implementation, the method further includes:
[0016] When a code file changes in the IDE project, the segmented code snippets and corresponding segmentation embedding vectors corresponding to the changed code files in the code snippet candidate library are updated to obtain an updated code snippet candidate library.
[0017] In a possible implementation, extracting the target code context from the target code file, and extracting the target code fragment from the target code context, includes:
[0018] Obtaining a source code snippet of a context preset length of the position to be completed from the target code file, and marking the position to be completed with a preset symbol as the target code context;
[0019] According to the characteristics of the coding language of the target code context, the target code fragment is extracted.
[0020] In a possible implementation, extracting a target embedding vector of the target code snippet, and acquiring a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector, includes:
[0021] Input the target code snippet into an embedded embedding model to obtain a target embedding vector output by the model;
[0022] Using the faiss library, obtain the M segmented embedding vectors closest to the target embedding vector from the pre-built code snippet candidate library, and use the M segmented code snippets corresponding to the M segmented embedding vectors as candidate code snippets similar to the target code snippet; M is a positive integer greater than 0 and not greater than N.
[0023] In a possible implementation, after constructing a prompt instruction prompt using the target code context and the candidate code snippet and inputting it into a preset code generation model to obtain a target code file after code completion output by the model, the method further includes:
[0024] The target code file after code completion and the target code file to be completed are deduplicated to obtain the completed code generated by the model, and the completed code is displayed to the user in the form of gray code so that the user can decide whether to accept the result of this code completion.
[0025] The embodiment of the present application also provides a code completion device, including:
[0026] An acquisition unit, configured to acquire a target code file to be completed; and extract a target code context from the target code file, and extract a target code fragment from the target code context;
[0027] A first extraction unit is used to extract a target embedding vector of the target code snippet, and obtain a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector; the code snippet candidate library is built based on all code files included in an integrated development environment IDE project where the target code file is located;
[0028] The input unit is used to construct a prompt instruction prompt using the target code context and the candidate code fragment, and input it into the code generation language model to obtain the target code file result after the code completion output by the model.
[0029] In a possible implementation manner, the device further includes:
[0030] A segmentation unit, used for segmenting all code files contained in the IDE project where the target code file is located according to the characteristics of the coding language to obtain N segmented code fragments; wherein N is a positive integer greater than 0;
[0031] A second extraction unit, used for segmentation embedding vectors corresponding to each of the N segmented code snippets;
[0032] The composition unit is used to form a code snippet candidate library using the N segmented code snippets and their corresponding segmentation embedding vectors.
[0033] In a possible implementation, the constituent unit is specifically used for:
[0034] The N segmented code snippets are numbered, and each segmented code snippet and its corresponding segmentation embedding vector and source code file are numbered to form a code snippet candidate library.
[0035] In a possible implementation, the device further includes:
[0036] The updating unit is used to update the segmented code snippets and corresponding segmentation embedding vectors corresponding to the changed code files in the code snippet candidate library when a code file changes in the IDE project, so as to obtain an updated code snippet candidate library.
[0037] In a possible implementation, the acquiring unit includes:
[0038] A first acquisition subunit is used to acquire a source code fragment of a preset length of the context of the position to be completed from the target code file, and mark the position to be completed with a preset symbol as the target code context;
[0039] The extraction subunit is used to extract the target code fragment according to the characteristics of the coding language of the target code context.
[0040] In a possible implementation, the first extraction unit includes:
[0041] An input subunit, used for inputting the target code snippet into an embedded embedding model to obtain a target embedding vector output by the model;
[0042] The second acquisition subunit is used to use the faiss library to obtain M segmentation embedding vectors that are closest to the target embedding vector from a pre-built code snippet candidate library, and use the M segmented code snippets corresponding to the M segmentation embedding vectors as candidate code snippets similar to the target code snippet; M is a positive integer greater than 0 and not greater than N.
[0043] In a possible implementation, the device further includes:
[0044] The deduplication unit is used to deduplicate the target code file after the code completion and the target code file to be completed, obtain the code of the completed part generated by the model, and display the code of the completed part to the user in the form of gray code so that the user can decide whether to accept the result of this code completion.
[0045] The embodiment of the present application also provides a code completion device, including: a processor, a memory, and a system bus;
[0046] The processor and the memory are connected via the system bus;
[0047] The memory is used to store one or more programs, and the one or more programs include instructions. When the instructions are executed by the processor, the processor executes any one of the implementations of the above-mentioned code completion method.
[0048] An embodiment of the present application also provides a computer-readable storage medium, in which instructions are stored. When the instructions are executed on a terminal device, the terminal device executes any one of the implementation methods of the above-mentioned code completion method.
[0049] The embodiment of the present application further provides a computer program product. When the computer program product is run on a terminal device, the terminal device executes any one of the implementation modes of the above-mentioned code completion method.
[0050] A code completion method, apparatus, storage medium and device provided in an embodiment of the present application first obtain a target code file to be completed; then extract a target code context from the target code file, and extract a target code snippet from the target code context; then extract a target embedding vector of the target code snippet, and based on the target embedding vector, obtain a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library; wherein the code snippet candidate library is built based on all code files included in an IDE project where the target code file is located; then, a prompt instruction prompt is built using the target code context and the candidate code snippets, and the prompt is input into a large language model for code generation to obtain a target code file after code completion output by the model.
[0051] It can be seen that since the present application retrieves candidate code snippets similar to the target code snippet based on a code snippet candidate library constructed based on all code files contained in the IDE project where the target code file is located, compared to the current search scope which is only the code files opened in the current IDE, the search scope is expanded, the search efficiency and accuracy are improved, and thus the generation results of the large language model are more accurate, thereby improving the final code completion effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] 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 some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0053] Figure 1 A flowchart of a code completion method provided in an embodiment of the present application;
[0054] Figure 2 A schematic diagram of the composition of a code completion device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0055] With the rapid development of Internet technology, software has become increasingly large and its architecture has become increasingly complex. Different software covers all aspects of people's lives and plays an increasingly important role in various fields such as medical care, transportation, education and entertainment. However, with the continuous iteration and update of application software, developers need to deal with more code and more complex logic, which brings a continuously growing workload to developers. In this context, the code completion function came into being and has become a key function in IDE. It can complete the subsequent code elements according to the code context to help developers reduce the time and workload of manual input and debugging of code, thereby improving the work efficiency and code quality of developers.
[0056] At present, the existing code completion methods are mainly based on the pre-trained large language model (LLM) for code generation, and products with code completion functions are usually embedded in the integrated development environment (IDE) used by users in the form of plug-ins. They can understand the user's intention based on the current code and quickly give accurate code suggestions. The specific completion process can include the following three steps:
[0057] 1. Get the code context content in the code file currently being written by the user in the IDE, as well as the current position that needs to be completed (such as the position of the cursor).
[0058] 2. In the code file opened in the IDE (or other opened files), obtain multiple code snippets similar to the current code to be completed by calculating the Jaccard similarity.
[0059] 3. Use the information obtained in steps 1 and 2 to form a prompt and input the code on the server to generate a large language model. The model uses the code context and the current location that needs to be completed to refer to multiple similar code snippets for reasoning and returns the completion result to achieve the goal of code completion. The completion result is returned to the user as a reference, and the user chooses whether to accept the code suggestion.
[0060] It can be seen that, firstly, when the existing code completion method searches for code snippets similar to the code to be completed, the search scope is only the code files opened (or otherwise opened) in the IDE. Such a search scope may miss a lot of useful information.
[0061] Secondly, when searching for similar code snippets, existing code completion methods use Jaccard similarity to search for code similar to the code to be completed. This similarity calculation method is based on character similarity and cannot ensure that the searched similar code snippets are semantically similar to the code to be completed, which will ultimately affect the effect of code completion.
[0062] In addition, when searching for similar code snippets, the existing code completion method directly uses a certain number of lines before and after the position to be completed as a query request / instruction (query), and then searches for code snippets similar to the query. This method will make the semantics of the search unclear and may search for unwanted similar code.
[0063] It can be seen that the current existing code completion methods are not accurate enough in retrieving code snippets similar to the code to be completed, which may cause the generation results of the code generation large language model to be inaccurate, thereby affecting the final code completion effect.
[0064] To address the above-mentioned defects, the present application provides a project-level code completion method, which first obtains a target code file to be completed; then extracts a target code context from the target code file, and extracts a target code snippet from the target code context; then extracts a target embedding vector of the target code snippet, and based on the target embedding vector, obtains a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library; wherein the code snippet candidate library is built based on all code files contained in the IDE project where the target code file is located; then, a prompt instruction (prompt) is built using the target code context and the candidate code snippets, which is input into a large language model for code generation to obtain the code-completed target code file output by the model.
[0065] It can be seen that since the present application retrieves candidate code snippets similar to the target code snippet based on a code snippet candidate library constructed based on all code files contained in the IDE project where the target code file is located, compared to the current search scope which is only the code files opened in the current IDE (or other opened ones), the search scope is expanded, the search efficiency and accuracy are improved, and thus the generation results of the large language model are more accurate, thereby improving the final code completion effect.
[0066] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are 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.
[0067] First embodiment
[0068] See also Figure 1 , is a flowchart of a code completion method provided in this embodiment, the method comprising the following steps:
[0069] S101: Obtain a target code file to be completed; extract a target code context from the target code file, and extract a target code fragment from the target code context.
[0070] In this embodiment, any code file that needs to be completed in the code generation software or device such as the large language model (LLM) input by the user is defined as the target code file to be completed, and this embodiment does not limit the coding language type of the target code file, for example, the target code file can be a Java code file or a Vue code file. This embodiment also does not limit the length of the target code file. And this embodiment also does not limit the content and acquisition method of the target code file.
[0071] Specifically, after obtaining the target code file to be completed, in order to improve the code completion effect for the target code file, firstly, the code context of the position to be completed can be extracted from the target code file to be completed according to the preset rules, and defined as the target code context. Then, according to the position to be completed, a code snippet query is extracted from the target code context as the target code snippet to perform the subsequent step S102.
[0072] Among them, this application does not limit the content of the preset rules, which can be set according to actual conditions and experience values. An optional implementation method is that after obtaining the target code file to be completed, you can first obtain a source code snippet of the context preset length (the specific content is not limited, such as 3000 tokens above and 1000 tokens below) of the position to be completed from the target code file, that is, intercept a certain range of code content near the position to be completed as the source code snippet of the code context, and use a preset symbol (the specific content is not limited, and symbols that do not often appear in ordinary text, such as various letters and special compound combinations, etc.) to mark the position to be completed as the target code context.
[0073] It should be noted that, depending on the different programming scenarios, only the source code snippet of the preset length of the context above the position to be completed (such as in the scenario where the developer writes the code sequentially, there is no source code snippet after the current position to be completed, etc.) can be obtained as the target code context. Alternatively, only the source code snippet of the preset length of the context below the position to be completed (such as the developer positions the cursor at the beginning of a section of source code that has been written, and there is no source code snippet in front of the cursor, etc.) can be obtained as the target code context. Alternatively, the source code snippets of the preset length of the context above and below the position to be completed (such as 3000 tokens of the context above and 1000 tokens of the context below can be obtained) can be obtained at the same time as the target code context. It can be understood that when the source code snippets of a certain length of the context above and below the position to be completed are obtained at the same time, the lengths of the source code obtained for the context above and below can be the same or different.
[0074] On this basis, after determining the target code context, in order to facilitate the search for similar code snippets in subsequent steps, the target code snippet can be further extracted as a query based on the characteristics of the coding language of the target code context. Compared with the existing method of extracting a certain number of lines of context near the position to be completed as a query directly, the query extracted in this embodiment can better reflect the semantic information that needs to be completed.
[0075] For example: Take the target code context's coding language as Java and Vue as examples. When the target code context's coding language is Java, if the position to be completed is within a function, then this function fragment is used as the query; if the position to be completed is outside the function, then a certain range (such as 50 lines) of code near the position to be completed is intercepted as the query. When the target code context's coding language is Vue, if the position to be completed is within a function, then this function fragment is used as the query; if the position to be completed is within an HTML fragment, then the tag where HTML is located is found and this tag is used as the query. In other cases, the code within a certain range (such as 50 lines) near the position to be completed is still taken as the query.
[0076] S102: extracting a target embedding vector of the target code snippet, and obtaining a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector; wherein the code snippet candidate library is built based on all code files included in the integrated development environment IDE project where the target code file is located.
[0077] It should be noted that in order to improve the code completion effect for the target code file, when establishing the IDE project, the present application will pre-build a candidate library containing embedded vectors according to all the code files contained in the IDE project, so as to search for code snippets similar to the target code snippets (defined as candidate snippets here) more quickly and accurately. Specifically, an optional implementation method is that the method of building the code snippet candidate library can include the following three steps:
[0078] Step A: All code files contained in the IDE project where the target code file is located are segmented according to the characteristics of the coding language to obtain N segmented code fragments; wherein N is a positive integer greater than 0.
[0079] When searching for similar code snippets, the existing code completion method simply divides the code in the code files within the search range (i.e., the open or other opened code files) into code snippets according to a certain number of lines as the code snippets to be matched, and then calculates the Jaccard similarity to filter out similar code snippets. This method will result in a lack of continuity in the found code snippets, and may result in the similar code snippets found being semantic fragments.
[0080] Therefore, in order to overcome the above problems, this embodiment not only expands the search scope from the open or other opened code files to all files included in the current project, but also divides the codes in the code files within the range according to the characteristics of the coding language using different rules (the specific content of the rules is not limited and can be set according to actual conditions and experience values), and obtains N (the specific value is not limited and can be any integer greater than 0) divided code fragments of corresponding granularity to execute the subsequent step B, so that the similar code fragments subsequently screened out can be more meaningful.
[0081] For example: Take the code file contained in the IDE project as a Java code file. For the Java code file, you can first use treesitter to parse and extract functions, and then use each function as a code snippet. If a function has too many lines, use a sliding window to split the function into multiple code snippets. For other content except functions, split them into code snippets by line. Among them, the parameters of the sliding window can be set according to actual conditions and experience values. This embodiment does not limit this. For example, the sliding window size can be set to 50, the step size can be set to 25, etc.
[0082] Alternatively, taking the code file included in the IDE project as a Vue code file as an example, for the Vue code file, you can first use treesitter to parse and extract functions, and then use each function as a code snippet. If a function has too many lines, use a sliding window to divide the function into multiple code snippets. In addition, the .vue file contained in the IDE project contains some html codes. These html codes are nested by tags with different numbers of lines. Use treesitter to parse and search these tags in a hierarchical traversal manner. If a tag is too large, its subtags will be traversed, and tags of appropriate size (such as 50) will be added to the library as code snippets, and subtags that exceed the required size will continue to traverse their subtags. Repeat the above process so that the html part can be divided into code snippets of appropriate size, each of which is a complete tag. For other code content, it can still be divided into code snippets by line. Among them, the parameters of the sliding window can still be set according to actual conditions and experience values, and this embodiment does not limit this. For example, the sliding window size can still be set to 50, the step size can still be set to 25, etc.
[0083] Step B: Extract the segmented embedding vectors corresponding to each of the N segmented code snippets.
[0084] After all the code files contained in the IDE project are segmented to obtain N segmented code fragments through step A, the embedding vector extraction method that exists or will appear in the future can be further used to extract the embedding vectors corresponding to each of the N segmented code fragments (here defined as segmented embedding vectors). For example, the N segmented code fragments can be numbered (the numbering method is not limited, and the uniqueness of each number is required), and the numbers of each segmented code fragment are sequentially input into the embedding model to obtain the segmented embedding vectors corresponding to each of the N segmented code fragments output by the model, so as to execute the subsequent step C. Among them, the embedding model can adopt an existing model or a model obtained by training mentioned later, etc., which is not limited in this application.
[0085] Step C: Use the N segmented code snippets and their corresponding segmentation embedding vectors to form a code snippet candidate library.
[0086] After extracting the segmentation embedding vectors corresponding to each of the N segmented code snippets through step B, the segmented code snippets and their corresponding segmentation embedding vectors and source code files can be further sorted according to the numbers to form a code snippet candidate library. In the candidate library, each number corresponds to a segmented code snippet and a segmentation embedding vector in a one-to-one correspondence, and the segmented code snippet or its corresponding segmentation embedding vector can be searched according to the number. At the same time, the source code file of each segmented code snippet is also stored in the candidate library.
[0087] It should be noted that when the IDE project is opened for the first time, the above steps AC can be executed to build a code snippet candidate library for all code files according to the characteristics of the coding language. This process can be called a cold start, and the specific implementation process will not be repeated one by one.
[0088] In this way, by pre-building a library, not only the subsequent search scope for similar code segments is expanded, but also the search speed can be effectively improved. In addition, each code segment stored in the code segment candidate library is segmented using different rules according to the characteristics of the coding language, which can also better maintain semantic continuity. Compared with the existing code completion method of segmenting by line, it will not produce semantic fragments, which helps to improve the search accuracy for similar code segments.
[0089] In addition, an optional implementation method is that after the code snippet candidate library is constructed through the above steps AC, further, when there is a change in the code file in the IDE project, the segmented code snippets and the corresponding segmentation embedding vectors corresponding to the changed code files in the code snippet candidate library can be updated to obtain an updated code snippet candidate library.
[0090] For example, when a code file in an IDE project is modified, the previously saved segmented code snippets and corresponding segmentation embedding vectors from the code file can be deleted from the code candidate library, and then the code snippets can be extracted from the code file again and new embedding vectors can be generated, which are numbered and saved in the code candidate library. Alternatively, if a code file is added to the IDE project, an incremental update can be performed in the code candidate library. Alternatively, if a code file is deleted from the IDE project, the segmented code snippets and segmentation embedding vectors corresponding to the code file can be deleted from the candidate library.
[0091] On this basis, after extracting the target code snippet query from the target code file to be completed through step S101, the embedding vector extraction method that exists or will appear in the future can be further used to extract the embedding vector corresponding to the target code snippet (here defined as the target embedding vector), such as inputting the target code snippet into the embedding model to obtain the target embedding vector output by the model, and it can be represented as query_embed. Then, the faiss library can be used to calculate the similarity between the target embedding vector and each segmented embedding vector in the pre-built code snippet candidate library, and the M segmented code snippets corresponding to the M segmented embedding vectors are used as M candidate code snippets similar to the target code snippet to perform the subsequent step S103.
[0092] The specific value of M is not limited, and can be any positive integer greater than 0 and not greater than N.
[0093] It can be seen that this embodiment uses embedding vector extraction methods such as embedding models to assist in the retrieval of similar code segments. That is, when searching for similar code segments, embedding vector extraction methods such as embedding models are used to first embed the target code segment to obtain the target embedding vector, and then search the code candidate library for the top M segmentation embedding vectors that are most similar to the target embedding vector to obtain the corresponding M most similar code segments as candidate code segments. Therefore, the retrieval process of the present application for candidate code segments similar to the target code segment is: "target code segment query->target embedding vector->M most similar segmentation embedding vectors in the code candidate library->M most similar candidate code segments in the code candidate library", rather than the retrieval process "query code segment->most similar code segment" used by the existing code completion method when searching for similar code segments, thereby improving the search accuracy for candidate code segments similar to the target code segment, because they will be more similar in semantics.
[0094] In addition, it should be noted that when extracting the segmented embedding vectors corresponding to each of the N segmented code snippets in constructing the code snippet candidate library, and when extracting the target embedding vector of the target code snippet, the preferred extraction method is to use the embedding model obtained through training to perform extraction, and the present application does not limit the training method of the embedding model.
[0095] Specifically, the embedding model can be trained based on the principle of contrastive learning framework. Its core idea is to construct positive sample pairs and negative sample pairs, that is, similar codes and dissimilar codes, as the input of the Embedding model, and the model output is the embedding vector. In this way, through the method of contrastive learning, the value of the target loss function loss (the specific content is not limited, for example, the hinge loss function can be used as the target loss function, etc.) is calculated to constrain the model to output similar embedding vectors for the input positive sample pairs, and orthogonal embedding vectors for the input negative sample pairs, so as to obtain a sparse representation of the code. By analogy, through multiple rounds of model training, the model parameters are updated until the preset conditions are met. For example, when the value of the target loss function loss (such as the hinge loss function) is very small and basically unchanged, the update of the model parameters is stopped, and the training of the embedding model is completed, generating a trained embedding model.
[0096] S103: Construct a prompt instruction using the target code context and candidate code segments, and input it into the large language model for code generation to obtain the target code file with the code completed after model output.
[0097] In this embodiment, after extracting the target code context from the target code file through step S101 and obtaining candidate code segments similar to the target code segments from the pre-constructed code segment candidate library through step S102, a prompt instruction (prompt) can be further constructed using the target code context and candidate code segments. The content of this instruction can include but is not limited to the description of the target code context and similar candidate code segments, as well as commands requiring the large language model LLM to complete the code at the position to be completed. In this way, after inputting it into the large language model LLM for code generation, the large language model LLM can, according to this prompt instruction (prompt), obtain the target code file with the code completed, which includes the code at the position to be completed.
[0098] For example: The example content of the prompt instruction (prompt) constructed using the target code context and candidate code segments can be as follows:
[0099] "Suppose you are an expert in xx language. Please complete the code according to
Code
Requirements
[0100]
Code
[0101] <Similar code segment 1>
[0102] Content of code segment 1
[0103] < / Similar code segment 1>
[0104] <Similar code snippet 2>
[0105] Content of code snippet 2
[0106] < / Similar code snippet 2>
[0107] <Code context>
[0108] Content of code context
[0109] < / Code context>
[0110]
Requirement
[0111] On this basis, an optional implementation method is that for the target code file after code completion output by the model, it is also necessary to perform deduplication processing with the target code file to be completed to obtain the code of the completed part. This is because the content generated by the previous model is all the code of the target code file after completion, while what needs to be shown to the user is only the code of the completed part in the target code file. Therefore, after the deduplication processing, the obtained code of the completed part can be shown to the user in the form of gray code, so that the user can decide whether to accept the result of this code completion, thereby improving the user's coding experience.
[0112] It should be noted that the specific implementation method of the project - level code completion method described in this application is not limited. For example, it can exist in the form of a code completion plugin product. In this way, the plugin can first send a prompt instruction (prompt) constructed using the target code context and candidate code snippets to the code - generating large - language model LLM on the server side. The server then sends back the content generated by the code - generating large - language model LL (i.e., the target code file after code completion) to the plugin. Then, after receiving the result content (i.e., the target code file after code completion) sent back by the server, the plugin can perform deduplication processing on this result content (i.e., the target code file after code completion), and then show the processed code of the completed part to the user in the form of gray code for the user to decide whether to accept this completion.
[0113] In this way, through the execution of the above steps S101 - S03, multiple optimizations have been made to the entire code completion process. Thus, it is possible to significantly improve the final code completion effect without changing the performance of the code - generating large - language model LLM, thereby enhancing the user's coding experience.
[0114] In summary, the present embodiment provides a code completion method, which first obtains a target code file to be completed; extracts a target code context from the target code file, and extracts a target code snippet from the target code context; then extracts a target embedding vector of the target code snippet, and obtains a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector; wherein the code snippet candidate library is built based on all code files included in the IDE project where the target code file is located; then, a prompt instruction prompt is built using the target code context and the candidate code snippets, which is input into a code generation large language model to obtain the code-completed target code file output by the model.
[0115] It can be seen that since the present application retrieves candidate code snippets similar to the target code snippet based on a code snippet candidate library constructed based on all code files contained in the IDE project where the target code file is located, compared to the current search scope which is only the code files opened in the current IDE, the search scope is expanded, the search efficiency and accuracy are improved, and thus the generation results of the large language model are more accurate, thereby improving the final code completion effect.
[0116] Second embodiment
[0117] This embodiment will introduce a code completion device. For related content, please refer to the above method embodiment.
[0118] See also Figure 2 , is a schematic diagram of the composition of a code completion device provided in this embodiment, the device 200 includes:
[0119] The acquisition unit 201 is used to acquire a target code file to be completed; and extract a target code context from the target code file, and extract a target code fragment from the target code context;
[0120] A first extraction unit 202 is used to extract a target embedding vector of the target code snippet, and obtain a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector; the code snippet candidate library is built based on all code files included in the integrated development environment IDE project where the target code file is located;
[0121] The input unit 203 is used to construct a prompt instruction prompt using the target code context and the candidate code snippet, and input it into the code generation large language model to obtain the target code file after the code completion output by the model.
[0122] In an implementation of this embodiment, the device further includes:
[0123] A segmentation unit, used for segmenting all code files contained in the IDE project where the target code file is located according to the characteristics of the coding language to obtain N segmented code fragments; wherein N is a positive integer greater than 0;
[0124] A second extraction unit, used for segmentation embedding vectors corresponding to each of the N segmented code snippets;
[0125] The composition unit is used to form a code snippet candidate library using the N segmented code snippets and their corresponding segmentation embedding vectors.
[0126] In an implementation of this embodiment, the constituent unit is specifically used for:
[0127] The N segmented code snippets are numbered, and each segmented code snippet and its corresponding segmentation embedding vector and source code file are numbered to form a code snippet candidate library.
[0128] In an implementation of this embodiment, the device further includes:
[0129] The updating unit is used to update the segmented code snippets and corresponding segmentation embedding vectors corresponding to the changed code files in the code snippet candidate library when a code file changes in the IDE project, so as to obtain an updated code snippet candidate library.
[0130] In an implementation of this embodiment, the acquiring unit 201 includes:
[0131] A first acquisition subunit is used to acquire a source code fragment of a preset length of the context of the position to be completed from the target code file, and mark the position to be completed with a preset symbol as the target code context;
[0132] The extraction subunit is used to extract the target code fragment according to the characteristics of the coding language of the target code context.
[0133] In an implementation of this embodiment, the first extracting unit 202 includes:
[0134] An input subunit, used for inputting the target code snippet into an embedded embedding model to obtain a target embedding vector output by the model;
[0135] The second acquisition subunit is used to use the faiss library to obtain M segmentation embedding vectors that are closest to the target embedding vector from a pre-built code snippet candidate library, and use the M segmented code snippets corresponding to the M segmentation embedding vectors as candidate code snippets similar to the target code snippet; M is a positive integer greater than 0 and not greater than N.
[0136] In an implementation of this embodiment, the device further includes:
[0137] The deduplication unit is used to deduplicate the target code file after the code completion and the target code file to be completed, obtain the code of the completed part generated by the model, and display the code of the completed part to the user in the form of gray code so that the user can decide whether to accept the result of this code completion.
[0138] Furthermore, an embodiment of the present application also provides a code completion device, including: a processor, a memory, and a system bus;
[0139] The processor and the memory are connected via the system bus;
[0140] The memory is used to store one or more programs, and the one or more programs include instructions. When the instructions are executed by the processor, the processor executes any one of the implementation methods of the above-mentioned code completion method.
[0141] Furthermore, an embodiment of the present application also provides a computer-readable storage medium, in which instructions are stored. When the instructions are executed on a terminal device, the terminal device executes any one of the implementation methods of the above-mentioned code completion method.
[0142] Furthermore, an embodiment of the present application also provides a computer program product, which, when executed on a terminal device, enables the terminal device to execute any one of the implementation methods of the above-mentioned code completion method.
[0143] It can be known from the description of the above implementation mode that those skilled in the art can clearly understand that all or part of the steps in the above-mentioned embodiment method can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product can be stored in a storage medium such as ROM / RAM, a disk, an optical disk, etc., including several instructions for enabling a computer device (which can be a personal computer, a server, or a network communication device such as a media gateway, etc.) to execute the methods described in the various embodiments of the present application or certain parts of the embodiments.
[0144] It should be noted that the various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments, and the same or similar parts between the various embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description.
[0145] It should also be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the presence of other identical elements in the process, method, article or device including the elements.
[0146] 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: include: Get the target code file to be completed; and extracting a target code context from the target code file, and extracting a target code fragment from the target code context; Extracting a target embedding vector of the target code snippet, and acquiring a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector; the code snippet candidate library is built based on all code files included in an integrated development environment IDE project where the target code file is located; The target code context and the candidate code snippets are used to construct a prompt instruction prompt, which is input into a code generation large language model to obtain a target code file after code completion output by the model.
2. The method according to claim 1, characterized in that The code snippet candidate library is constructed as follows, including: All code files contained in the IDE project where the target code file is located are segmented according to the characteristics of the coding language to obtain N segmented code fragments; N is a positive integer greater than 0; Extracting the segmentation embedding vectors corresponding to each of the N segmented code snippets; The N segmented code snippets and their corresponding segmentation embedding vectors are used to form a code snippet candidate library.
3. The method according to claim 2, characterized in that The method of using the N segmented code snippets and their corresponding segmentation embedding vectors to form a code snippet candidate library includes: The N segmented code snippets are numbered, and each segmented code snippet and its corresponding segmentation embedding vector and source code file are numbered to form a code snippet candidate library.
4. The method according to claim 2, characterized in that: The method further comprises: When a code file changes in the IDE project, the segmented code snippets and corresponding segmentation embedding vectors corresponding to the changed code files in the code snippet candidate library are updated to obtain an updated code snippet candidate library.
5. The method according to claim 1, characterized in that The step of extracting the target code context from the target code file and extracting the target code fragment from the target code context comprises: Obtaining a source code snippet of a context preset length of the position to be completed from the target code file, and marking the position to be completed with a preset symbol as the target code context; According to the characteristics of the coding language of the target code context, the target code fragment is extracted.
6. The method according to claim 2, characterized in that The step of extracting a target embedding vector of the target code snippet, and acquiring a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector, includes: Input the target code snippet into an embedded embedding model to obtain a target embedding vector output by the model; Using the faiss library, obtain the M segmented embedding vectors closest to the target embedding vector from the pre-built code snippet candidate library, and use the M segmented code snippets corresponding to the M segmented embedding vectors as candidate code snippets similar to the target code snippet; M is a positive integer greater than 0 and not greater than N.
7. The method according to any one of claims 1 to 6, characterized in that: After constructing a prompt instruction prompt using the target code context and the candidate code snippet and inputting it into a preset code generation model to obtain a target code file after code completion output by the model, the method further includes: The target code file after code completion and the target code file to be completed are deduplicated to obtain the completed code generated by the model, and the completed code is displayed to the user in the form of gray code so that the user can decide whether to accept the result of this code completion.
8. A code completion device, characterized in that: include: An acquisition unit is used to acquire the target code file to be completed; and extracting a target code context from the target code file, and extracting a target code fragment from the target code context; A first extraction unit is used to extract a target embedding vector of the target code snippet, and obtain a candidate code snippet similar to the target code snippet from a pre-built code snippet candidate library based on the target embedding vector; The code snippet candidate library is constructed based on all code files included in the integrated development environment IDE project where the target code file is located; An input unit is used to construct a prompt instruction prompt using the target code context and the candidate code snippet, and input it into a code generation large language model to obtain a target code file after code completion output by the model.
9. A code completion device, characterized in that: include: Processor, memory, system bus; The processor and the memory are connected via the system bus; The memory is used to store one or more programs, wherein the one or more programs include instructions, and when the instructions are executed by the processor, the processor executes the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores instructions, and when the instructions are executed on a terminal device, the terminal device executes the method according to any one of claims 1 to 7.
Citation Information
Cited By
Code generation method for code completion, electronic equipment and storage medium
CN120892058A
Data completion method and device, electronic equipment and medium
CN120910049A
A data completion method and device, electronic equipment and medium
CN120910049B