Code completion retrieval enhancement method and device, electronic equipment and storage medium
By obtaining the target semantic vector of the code block and the candidate semantic vectors in the vector database for similarity sampling, determining the reference text block and applying a large language model for code completion, it solves the incomplete context information and low generated code quality in the code completion model. The problem of this problem improves the accuracy of code completion and user adoption rate, and improves programming efficiency.
Patent Information
- Application Number
- CN202411146184.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-20
- Publication Date
- 2024-11-22
AI Technical Summary
During the code completion process, the existing code completion model has problems such as incomplete context information and low quality of model generation code, resulting in a decrease in the accuracy of code completion results and user adoption rate.
By obtaining the target semantic vector of the current code block, searching for candidate semantic vectors of candidate text blocks in the vector database, sampling based on similarity, determining the reference text block, and using a large language model to complete the code to improve the code completion Accuracy and diversity.
Improve the accuracy of code completion results and user adoption rate, improve the code generation performance of large language models, alleviate the problems of high repetition and small differences between candidate text blocks, simplify the developer development process, and improve the Programming efficiency.
Smart Images

Figure CN119003751A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer vision technology, and in particular to a code completion retrieval enhancement method, device, electronic device and storage medium. Background Art
[0002] During the software development phase, software developers write code based on actual needs. However, in reality, with the large-scale development of software applications, software architecture is also moving towards standardization. The scenarios and architectures encountered by developers have already been implemented by their peers, and more than half of the codes are similar or completely consistent. For software developers, repeating code implementation is a meaningless task. In order to allow them to focus on more important business logic, the code completion model came into being.
[0003] The code completion model learns most of the open source code during the training phase and has the logical ability to complete the code based on the existing code, which can greatly improve the efficiency of code development. In the practical application of the code completion model, although it can assist software developers in completing part of the code based on the code context, there are still problems such as incomplete context information and low quality of model-generated code. In this case, the Retrieval Augmented Generation (RAG) strategy came into being. The RAG strategy combines search technology with the prompt word function of the Large Language Model (LLM), asks questions to the LLM, and uses the information found by the search algorithm as the background context. The questions and the searched background context are integrated into the prompt words of the LLM.
[0004] However, in the retrieval-recall process, due to the extremely uneven distribution of codes, the recalled reference codes lack diversity, and due to the input length limitation of LLM, the external knowledge that the RAG strategy can introduce is limited, making the code ultimately generated by LLM lose its creativity and degenerate into a tool that can only repeat codes, reducing the accuracy of the code completion results and the user adoption rate. Summary of the invention
[0005] The present invention provides a code completion retrieval enhancement method, device, electronic device and storage medium to solve the defects in the prior art.
[0006] The present invention provides a code completion retrieval enhancement method, comprising: Get the target semantic vector of the context information of the current code block; Based on the target semantic vector, searching for candidate semantic vectors of multiple candidate text blocks in a vector database; the vector database is used to store semantic vectors of text blocks of different project files; Based on the similarity between the plurality of candidate semantic vectors and the target semantic vector, sampling the plurality of candidate text blocks to determine a target number of reference text blocks; Based on the reference text block and the context information, a large language model is applied to complete the current code block.
[0007] According to a code completion retrieval enhancement method provided by the present invention, based on the similarity between the plurality of candidate semantic vectors and the target semantic vector, the plurality of candidate text blocks are sampled to determine a target number of reference text blocks, including: Determining a sampling probability of each of the candidate text blocks based on similarities between the plurality of candidate semantic vectors and the target semantic vector; Based on the sampling probability of each candidate text block, a plurality of candidate text blocks are sampled to determine the reference text block.
[0008] According to a code completion retrieval enhancement method provided by the present invention, the sampling probability of each candidate text block is determined based on the similarity between the plurality of candidate semantic vectors and the target semantic vector, including: Calculating a radial basis function value of each candidate semantic vector based on the similarity between each candidate semantic vector and the target semantic vector; Based on the radial basis function value of each candidate semantic vector, the sampling probability of each candidate text block is determined.
[0009] According to a code completion retrieval enhancement method provided by the present invention, the sampling probability of each candidate text block is determined based on the radial basis function value of each candidate semantic vector, including: Based on the normalized result of the radial basis function value of each candidate semantic vector, the sampling probability of each candidate text block is determined.
[0010] According to a code completion retrieval enhancement method provided by the present invention, the sampling probability of each candidate text block is determined based on the radial basis function value of each candidate semantic vector, and further includes: For any candidate semantic vector among the plurality of candidate semantic vectors, based on the product of the similarities between the candidate semantic vector and any other candidate semantic vector, calculating an influence parameter of the other candidate semantic vector on the candidate semantic vector; Calculate the product of the influence parameter of any other candidate semantic vector on any candidate semantic vector and the radial basis function value of any candidate semantic vector; Based on the normalized result of the multiplication result, the sampling probability of the candidate text block corresponding to any candidate semantic vector is determined.
[0011] According to a code completion retrieval enhancement method provided by the present invention, the method of applying a large language model based on the reference text block and the context information to complete the current code block includes: Constructing prompt word information based on the reference text block and the context information; The prompt word information is input into the large language model to obtain a code snippet of the completion position in the current code block output by the large language model.
[0012] According to a code completion retrieval enhancement method provided by the present invention, the prompt word information is input into the large language model to obtain a code snippet of the completion position in the current code block output by the large language model, and then includes: receiving user feedback information regarding the code snippet; If the feedback information is negative feedback information, the large language model re-outputs a new code snippet.
[0013] The present invention also provides a code completion retrieval enhancement device, comprising: An acquisition module is used to obtain a target semantic vector of the context information of the current code block; A retrieval module, used to search for candidate semantic vectors of multiple candidate text blocks in a vector database based on the target semantic vector; the vector database is used to store semantic vectors of code segments of different project files; A sampling module, configured to sample a plurality of the candidate text blocks based on similarities between the plurality of the candidate semantic vectors and the target semantic vector, and determine a target number of reference text blocks; A completion module is used to complete the current code block by applying a large language model based on the reference text block and the context information.
[0014] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the code completion retrieval enhancement method as described in any one of the above is implemented.
[0015] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the code completion retrieval enhancement method as described in any one of the above is implemented.
[0016] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the code completion retrieval enhancement method as described above is implemented.
[0017] The code completion retrieval enhancement method, device, electronic device and storage medium provided by the present invention can obtain external relevant knowledge for code completion by searching in a vector database, which is helpful to improve the accuracy of code completion results and improve the code completion and generation performance of large language models. By sampling multiple candidate text blocks through the similarity between multiple candidate semantic vectors and the target semantic vector, and determining the target number of reference text blocks, the problem of high repetition and small difference between the candidate text blocks can be alleviated. While meeting the input size limit of the large language model, the diversity of reference text blocks is guaranteed, which helps to improve the user adoption rate of code completion results. Through automated code completion, this method can provide simple and powerful support for the developer's development process, improve programming efficiency, and help developers focus on more important businesses, thereby creating greater value. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] In order to more clearly illustrate the technical solutions in the present invention or 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 invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0019] Figure 1 It is a flowchart of the code completion retrieval enhancement method provided by the present invention.
[0020] Figure 2 It is a structural schematic diagram of the code completion retrieval enhancement device provided by the present invention.
[0021] Figure 3 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION
[0022] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0023] In recent years, there has been an endless stream of research on how to use the code capabilities of large models to assist human developers in their work, most of which focus on how to improve the capabilities of LLM models and how to utilize the capabilities of LLM models. In terms of human-computer collaborative programming, this can be achieved by constructing proprietary plug-ins in modern code editors. The plug-ins can sense the code environment and use exclusive LLMs to achieve human-computer collaborative programming. When developers write code in the code editor, the plug-in analyzes the current programming environment and the written code to understand the programming intent and the functions that need to be implemented, and relies on the Jaccard distance to measure the similarity between the code context and the reference code in the database, thereby introducing additional reference information to the LLM. Developers modify, accept or reject the generated results based on actual conditions. This flexible approach allows programmers to maintain coding style and final code quality.
[0024] For LLM, without changing the capabilities of the model itself, the simplest way to improve the quality of system generation is to introduce new knowledge, a process called RAG strategy. Under the existing RAG strategy, retrieval enhancement is divided into two important steps. First, an offline database is built to extract data from a large number of text files, segment the text, and use an embedding model to vectorize and store it in the database. In the actual application stage, the user's request is parsed into one or more query vectors, and the most similar document vectors are retrieved from the vectorized database. These related documents are used as context to input into the model to be generated. This method that combines retrieval and generation can effectively improve the accuracy and relevance of the generated content, and is particularly suitable for tasks such as question answering, text summarization, etc.
[0025] In the code development scenario, code data is highly structured data. Due to the encapsulation and inheritance characteristics of high-level programming languages, the same functional logic is often completed by codes distributed in multiple files. Relying solely on the context of the current code file cannot help LLM understand the work being done by the developer. The lack of awareness of the complete logic of the current code file will cause the generated code to deviate from the code logic. The RAG strategy can expand the amount of input information of LLM and help LLM unify the code style. However, in the retrieval-recall process, due to the extremely uneven distribution of code and the lack of diversity in the recalled reference code, the external knowledge that the RAG strategy can introduce is limited, which makes the code finally generated by LLM lose its creativity, thereby reducing the accuracy of the code completion results and the user adoption rate.
[0026] Based on this, an embodiment of the present invention provides a code completion retrieval enhancement method.
[0027] Figure 1 FIG. 1 is a flow chart of a code completion retrieval enhancement method provided in an embodiment of the present invention. Figure 1 As shown, the method comprises: S1, obtain the target semantic vector of the context information of the current code block; S2, based on the target semantic vector, searching for candidate semantic vectors of multiple candidate text blocks in a vector database; the vector database is used to store semantic vectors of text blocks of different project files; S3, based on the similarity between the plurality of candidate semantic vectors and the target semantic vector, sampling the plurality of candidate text blocks to determine a target number of reference text blocks; S4: Based on the reference text block and the context information, a large language model is applied to complete the current code block.
[0028] Specifically, a code completion search enhancement method provided in an embodiment of the present invention is executed by a code completion search enhancement device, which can be configured as a plug-in in a computer, which can be a local computer or a cloud computer, and the local computer can be a computer, a tablet, etc., which is not specifically limited here. Therefore, the execution body of the code completion search enhancement method can be a code completion search enhancement plug-in, and code completion can be achieved by calling the plug-in.
[0029] First, step S1 is performed to obtain a target semantic vector of context information of a current code block. The current code block may be a code block where a mouse cursor is located, and a code snippet after the mouse cursor position in the current code block needs to be generated to implement code completion.
[0030] Here, the code position after the mouse cursor position in the current code block is the completion position, and the code completion retrieval enhancement method provided in the embodiment of the present invention can be triggered by pressing the Enter key on the keyboard to start executing step S1.
[0031] Since strongly typed code languages encapsulate logic in classes and functions, or deploy them in code blocks and ordinary code lines, context information can be determined based on whether the mouse cursor position is within a function. If the mouse cursor position is within a function, the entire unfinished function is used as the context information of the current code block; if the mouse cursor position is outside a function, the first number of lines of code before the mouse cursor position and the second number of lines of code after the mouse cursor position can be used as the context information of the current code block. The first number of lines and the second number of lines can be set as needed, for example, the first number of lines can be 50 and the second number of lines can be 10.
[0032] The context information of the current code block may include known codes contained in the current code block and information of the completion position. The information of the completion position may be the coordinates of the completion position, which is used to locate the completion position.
[0033] The target semantic vector can be realized through an embedding model, that is, the context information of the current code block is input into the embedding model, and the context information of the current code block is converted into an initial semantic vector through the embedding model, and the initial semantic vector is normalized to obtain the target semantic vector. The target semantic vector is a normalized vector used to characterize the semantic features of the current code segment, which facilitates subsequent similarity search in the vector database.
[0034] The embedding model can be a word2vec model, an OpenAI ada-002 model, a voyage-code-2 model, or other models that can realize information vectorization.
[0035] If the initial semantic vector is , for the i-th component in the initial semantic vector v , for the i-th component The normalization formula used for normalization can be: ; in, is the i-th component The normalized result is It is the Lp norm, and p can be taken as 2, that is, the L2 norm can be used to perform normalization operation on the initial semantic vector.
[0036] The normalized results of each component in the initial semantic vector can form the target semantic vector.
[0037] Then, step S2 is performed to search for candidate semantic vectors of multiple candidate text blocks in a vector database using the target semantic vector.
[0038] The vector database may store semantic vectors of text blocks of different project files, and the project files may include text contents such as codes and documents. When determining the semantic vectors of text blocks of different project files, different types of text contents in each project file may be first divided into blocks to obtain text blocks in each project file.
[0039] Since codes and documents of different scales correspond to different semantic functions, the codes can be segmented at the function level and the documents can be segmented at the chapter level. In some special cases, the length of document chapters and code functions is too long, which will exceed the input length limit of the large language model. Assuming that the input length of the large language model is L tokens, the sliding window strategy can be used to segment the text content. The length of the sliding window can be the same as the input length of the large language model, both of which are Ltokens, and the step length of the sliding window can be L / 2 tokens.
[0040] Then, the text blocks in the same project file obtained by segmentation can be converted into semantic vectors through the embedding model and normalized to obtain the semantic vectors of the text blocks in the same project file. Furthermore, the semantic vectors of the text blocks of different project files can be obtained by processing different project files separately through the embedding model.
[0041] When searching for candidate semantic vectors in the vector database, the similarity between the target semantic vector and the semantic vectors of the text blocks of each project file in the vector database can be calculated first, and then a plurality of corresponding text blocks with high similarity are selected from the text blocks of each project file as candidate text blocks. Here, the number of candidate text blocks can be a preset number, that is, the first preset number of text blocks with high similarity are selected as candidate text blocks. The preset number can be set as needed and is not specifically limited here.
[0042] Among them, the similarity between semantic vectors can be measured by distances such as cosine distance and Jaccard distance. The text block of the semantic vector with a smaller angle with the target semantic vector is closer to the current code block in terms of semantics. Thus, a preset number of candidate text blocks are selected for subsequent completion of the current code block.
[0043] Then, step S3 is performed to sample multiple candidate text blocks using the similarity between each candidate semantic vector and the target semantic vector to obtain a target number of reference text blocks. Here, the target number can be set as needed, for example, it can be set to a value such as 3 or 5.
[0044] When sampling each candidate text block, we can first use the similarity between each candidate semantic vector and the target semantic vector to determine the sampling probability of each candidate text block, and then use the sampling probability of each candidate text block to perform a target number of times on multiple candidate text blocks without replacement. Each sampling can obtain a reference text block, and a total of a target number of reference text blocks can be obtained.
[0045] Here, the similarity between each candidate semantic vector and the target semantic vector may be normalized, and then the normalized results corresponding to each candidate semantic vector are used as the sampling probabilities of each candidate text block.
[0046] During each sampling, a random number may be generated first, and then the random number may be compared with the sampling probability of each candidate text block, and the candidate text block with the sampling probability closest to the random number may be selected as the reference text block obtained by sampling.
[0047] Here, sampling is performed by determining the sampling probability, so that the randomness of the sampling can be guaranteed, and thus the diversity of the obtained reference text blocks can be improved.
[0048] Finally, step S4 is performed to complete the current code block using the reference text block and context information and applying the large language model. Here, the reference text block and context information can be input into the large language model, and the large language model automatically generates a code snippet after the mouse cursor position in the current code block, that is, a code snippet at the completion position in the current code block, to complete the current code block.
[0049] The code completion retrieval enhancement method provided in the embodiment of the present invention first obtains the target semantic vector of the context information of the current code block; then based on the target semantic vector, the candidate semantic vectors of multiple candidate text blocks are searched in the vector database; the vector database is used to store the semantic vectors of text blocks of different project files; thereafter, based on the similarity between the multiple candidate semantic vectors and the target semantic vector, multiple candidate text blocks are sampled to determine the target number of reference text blocks; finally, based on the reference text blocks and the context information, a large language model is applied to complete the current code block. The method can obtain external relevant knowledge for code completion by searching in the vector database, which helps to improve the accuracy of the code completion results and improve the code completion and generation performance of the large language model. By sampling multiple candidate text blocks based on the similarity between multiple candidate semantic vectors and the target semantic vector, the target number of reference text blocks is determined, which can alleviate the problem of high repetition and small difference between the candidate text blocks, and ensure the diversity of the reference text blocks while meeting the input size limit of the large language model, which helps to improve the user adoption rate of the code completion results. This method can provide simple and powerful support for the developer's development process through automatic code completion, improve programming efficiency, and help developers focus on more important business, thereby creating greater value.
[0050] On the basis of the above embodiment, the step of determining the sampling probability of each candidate text block based on the similarity between the plurality of candidate semantic vectors and the target semantic vector includes: Calculating a radial basis function value of each candidate semantic vector based on the similarity between each candidate semantic vector and the target semantic vector; Based on the radial basis function value of each candidate semantic vector, the sampling probability of each candidate text block is determined.
[0051] Specifically, when determining the sampling probability of each candidate text block, the radial basis function value of each candidate semantic vector can be calculated by using the similarity between each candidate semantic vector and the target semantic vector. The similarity between a preset number of candidate semantic vectors and the target semantic vector can be expressed as: ; Where n is the preset number, is the similarity between the i-th candidate semantic vector and the target semantic vector.
[0052] Here, the e-index of the radial basis function can be determined according to the similarity. The similarity can be directly used as the e-index of the radial basis function, or the square or negative square of the similarity can be used as the e-index of the radial basis function, which is not specifically limited here.
[0053] After that, the radial basis function value of each candidate semantic vector can be used to determine the sampling probability of each candidate text block. For example, the radial basis function value of each candidate semantic vector can be normalized, and the normalized result of the radial basis function value of each candidate semantic vector is used as the sampling probability of each candidate text block. That is: ; in, is the radial basis function value of the i-th candidate semantic vector, is the sampling probability of the i-th candidate text block.
[0054] In addition, the radial basis function values of each candidate semantic vector can be multiplied according to the influence parameters of other candidate semantic vectors on each candidate semantic vector, and then the normalized result of the product is used as the sampling probability of each candidate text block. That is, ; in, is the influence parameter of the j-th candidate semantic vector on the ith candidate semantic vector, which can be set in advance or calculated according to actual conditions and is not specifically limited here.
[0055] The sampling probabilities of a preset number of candidate text blocks can constitute a sampling probability set , through normalization, The sum of the values in each dimension is 1.
[0056] In the embodiment of the present invention, by calculating the radial basis function value of each candidate semantic vector and determining the sampling probability of each candidate text block, the calculation of the sampling probability can be made more reasonable. Moreover, by determining the sampling probability of each candidate text block through the normalized result of the radial basis function value of each candidate semantic vector, the sum of the sampling probabilities of the candidate text blocks can be guaranteed to be 1, thereby ensuring the rationality and randomness of the sampling probability and facilitating the implementation and calculation of the sampling operation.
[0057] On the basis of the above embodiment, the step of determining the sampling probability of each candidate text block based on the radial basis function value of each candidate semantic vector further includes: For any candidate semantic vector among the plurality of candidate semantic vectors, based on the product of the similarities between the candidate semantic vector and any other candidate semantic vector, calculating an influence parameter of the other candidate semantic vector on the candidate semantic vector; Calculate the product of the influence parameter of any other candidate semantic vector on any candidate semantic vector and the radial basis function value of any candidate semantic vector; Based on the normalized result of the multiplication result, the sampling probability of the candidate text block corresponding to any candidate semantic vector is determined.
[0058] Specifically, for the ith candidate semantic vector, the jth candidate semantic vector is the other candidate semantic vectors, and the influence parameter of the jth candidate semantic vector on the ith candidate semantic necklace can be calculated by multiplying the similarity corresponding to the ith candidate semantic vector and the jth candidate semantic vector, that is, ; in, is the similarity product of the i-th candidate semantic vector and the j-th candidate semantic vector, It is a hyperparameter and can take a value of -1.
[0059] After that, the influence parameter of the jth candidate semantic vector on the ith candidate semantic vector is calculated The radial basis function value of the i-th candidate semantic vector The product of .
[0060] After that, the sampling probability of the i-th candidate text block can be determined by using the normalized result of the product result: .
[0061] In the embodiment of the present invention, by introducing the influence parameters of other candidate semantic vectors, the sampling probability of each candidate text block based on popularity can be determined, which can further improve the diversity of the reference text blocks obtained by sampling.
[0062] On the basis of the above embodiment, the method of applying a large language model based on the reference text block and the context information to complete the current code block includes: Constructing prompt word information based on the reference text block and the context information; The prompt word information is input into the large language model to obtain a code snippet of the completion position in the current code block output by the large language model.
[0063] Specifically, when a large language model is applied, the reference text block and context information may be used to construct prompt information. Here, the reference text block and context information may be filled into a pre-constructed prompt template to obtain prompt information.
[0064] Afterwards, the prompt word information can be input into the large language model, and the code snippet of the completion position in the current code can be obtained through the powerful generation capability of the large language model.
[0065] It is understandable that a large language model can use a heuristic graph search algorithm such as beam search to determine multiple optional code snippets, and select the best one from the multiple optional code snippets for output.
[0066] In an embodiment of the present invention, by constructing prompt word information, sufficient guiding knowledge can be provided for a large language model, so that the large language model can quickly output code snippets that are more easily adopted by users, which not only improves the output efficiency of the code snippets, but also improves the user adoption rate of the code snippets.
[0067] On the basis of the above embodiment, the prompt word information is input into the large language model to obtain the code snippet of the completion position in the current code block output by the large language model, and then includes: receiving user feedback information regarding the code snippet; If the feedback information is negative feedback information, the large language model re-outputs a new code snippet.
[0068] Specifically, after obtaining the code snippet at the completion position, the user can give feedback information for the code snippet after viewing the code snippet. Here, the code snippet at the completion position can be directly displayed, and a feedback button can also be set on the display interface. The feedback button can include a positive feedback button and a negative feedback button. The positive feedback button is used to feedback positive feedback information, which can be triggered when the user is satisfied with the code snippet and accepts it. The negative feedback button is used to feedback negative feedback information, which can be triggered when the user is dissatisfied with the code snippet and does not accept it.
[0069] If the feedback information is negative feedback information, the large language model can be used to re-use the heuristic graph search algorithm to determine multiple optional code snippets, and the optimal optional code snippet is selected from the multiple optional code snippets for output. When regenerating, the large language model will try to avoid generating the same code snippet as the previously generated code snippet to avoid giving users a bad experience.
[0070] like Figure 2As shown, based on the above embodiment, an embodiment of the present invention provides a code completion retrieval enhancement device, including: An acquisition module 21 is used to acquire a target semantic vector of context information of a current code block; A retrieval module 22, used to search for candidate semantic vectors of multiple candidate text blocks in a vector database based on the target semantic vector; the vector database is used to store semantic vectors of code segments of different project files; A sampling module 23, configured to sample a plurality of the candidate text blocks based on similarities between the plurality of the candidate semantic vectors and the target semantic vector, and determine a target number of reference text blocks; The completion module 24 is used to complete the current code block by applying a large language model based on the reference text block and the context information.
[0071] Based on the above embodiment, the sampling module is specifically used for: Determining a sampling probability of each of the candidate text blocks based on similarities between the plurality of candidate semantic vectors and the target semantic vector; Based on the sampling probability of each candidate text block, a plurality of candidate text blocks are sampled to determine the reference text block.
[0072] Based on the above embodiment, the sampling module is specifically used for: Calculating a radial basis function value of each candidate semantic vector based on the similarity between each candidate semantic vector and the target semantic vector; Based on the radial basis function value of each candidate semantic vector, the sampling probability of each candidate text block is determined.
[0073] Based on the above embodiment, the sampling module is specifically used for: Based on the normalized result of the radial basis function value of each candidate semantic vector, the sampling probability of each candidate text block is determined.
[0074] Based on the above embodiment, the sampling module is specifically used for: For any candidate semantic vector among the plurality of candidate semantic vectors, based on the product of the similarities between the candidate semantic vector and any other candidate semantic vector, calculating an influence parameter of the other candidate semantic vector on the candidate semantic vector; Calculate the product of the influence parameter of any other candidate semantic vector on any candidate semantic vector and the radial basis function value of any candidate semantic vector; Based on the normalized result of the multiplication result, the sampling probability of the candidate text block corresponding to any candidate semantic vector is determined.
[0075] Based on the above embodiment, the completion module is specifically used for: Constructing prompt word information based on the reference text block and the context information; The prompt word information is input into the large language model to obtain a code snippet of the completion position in the current code block output by the large language model.
[0076] Based on the above embodiment, a feedback module is further included, which is used for: receiving user feedback information regarding the code snippet; If the feedback information is negative feedback information, the large language model re-outputs a new code snippet.
[0077] Specifically, the functions of each module in the code completion retrieval enhancement device provided in the embodiment of the present invention correspond one-to-one to the operation flow of each step in the above method embodiment, and the effects achieved are also consistent. Please refer to the above embodiment for details, which will not be repeated in the embodiment of the present invention.
[0078] Figure 3 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 3 As shown, the electronic device may include: a processor 310, a communication interface 320, a memory 330 and a communication bus 340, wherein the processor 310, the communication interface 320 and the memory 330 communicate with each other through the communication bus 340. The processor 310 may call the logic instructions in the memory 330 to execute the code completion retrieval enhancement method provided in the above embodiments.
[0079] In addition, the logic instructions in the above-mentioned memory 330 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.
[0080] On the other hand, the present invention also provides a computer program product, which includes a computer program. The computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the code completion retrieval enhancement method provided in the above embodiments.
[0081] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented when the computer program is executed by a processor to execute the code completion retrieval enhancement method provided in the above embodiments.
[0082] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0083] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0084] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A code completion retrieval enhancement method, characterized in that: include: Get the target semantic vector of the context information of the current code block; Based on the target semantic vector, searching for candidate semantic vectors of multiple candidate text blocks in a vector database; The vector database is used to store semantic vectors of text blocks of different project files; Based on the similarity between the plurality of candidate semantic vectors and the target semantic vector, sampling the plurality of candidate text blocks to determine a target number of reference text blocks; Based on the reference text block and the context information, a large language model is applied to complete the current code block.
2. The code completion retrieval enhancement method according to claim 1, characterized in that: The step of sampling the plurality of candidate text blocks based on the similarities between the plurality of candidate semantic vectors and the target semantic vector to determine a target number of reference text blocks includes: Determining a sampling probability of each of the candidate text blocks based on similarities between the plurality of candidate semantic vectors and the target semantic vector; Based on the sampling probability of each candidate text block, a plurality of candidate text blocks are sampled to determine the reference text block.
3. The code completion retrieval enhancement method according to claim 2, characterized in that: The step of determining the sampling probability of each candidate text block based on the similarity between the plurality of candidate semantic vectors and the target semantic vector comprises: Calculating a radial basis function value of each candidate semantic vector based on the similarity between each candidate semantic vector and the target semantic vector; Based on the radial basis function value of each candidate semantic vector, the sampling probability of each candidate text block is determined.
4. The code completion retrieval enhancement method according to claim 3, characterized in that: The step of determining the sampling probability of each candidate text block based on the radial basis function value of each candidate semantic vector comprises: Based on the normalized result of the radial basis function value of each candidate semantic vector, the sampling probability of each candidate text block is determined.
5. The code completion retrieval enhancement method according to claim 3, characterized in that: The step of determining the sampling probability of each candidate text block based on the radial basis function value of each candidate semantic vector further includes: For any candidate semantic vector among the plurality of candidate semantic vectors, based on the product of the similarities between the candidate semantic vector and any other candidate semantic vector, calculating an influence parameter of the other candidate semantic vector on the candidate semantic vector; Calculate the product of the influence parameter of any other candidate semantic vector on any candidate semantic vector and the radial basis function value of any candidate semantic vector; Based on the normalized result of the multiplication result, the sampling probability of the candidate text block corresponding to any candidate semantic vector is determined.
6. The code completion retrieval enhancement method according to any one of claims 1 to 5, characterized in that: The applying a large language model based on the reference text block and the context information to complete the current code block includes: Constructing prompt word information based on the reference text block and the context information; The prompt word information is input into the large language model to obtain a code snippet of the completion position in the current code block output by the large language model.
7. The code completion retrieval enhancement method according to claim 6, characterized in that: The step of inputting the prompt word information into the large language model to obtain a code snippet of the completion position in the current code block output by the large language model comprises: receiving user feedback information regarding the code snippet; If the feedback information is negative feedback information, the large language model re-outputs a new code snippet.
8. A code completion retrieval enhancement device, characterized in that: include: An acquisition module is used to obtain a target semantic vector of the context information of the current code block; A retrieval module, used to search for candidate semantic vectors of multiple candidate text blocks in a vector database based on the target semantic vector; the vector database is used to store semantic vectors of code segments of different project files; A sampling module, configured to sample a plurality of the candidate text blocks based on similarities between the plurality of the candidate semantic vectors and the target semantic vector, and determine a target number of reference text blocks; A completion module is used to complete the current code block by applying a large language model based on the reference text block and the context information.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the code completion retrieval enhancement method according to any one of claims 1 to 7 is implemented.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the code completion retrieval enhancement method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Method for constructing semantic code search model in large-scale candidate set and related device
CN114896368A
Data service recommendation result diversification method based on determinant point process
CN117194977A
Question and answer model-based code completion method, code completion terminal, medium and system
CN118170354A
Code generation method and device, equipment, storage medium and program product
CN118502768A
Cited By
Code search method and device based on large model agent and medium
CN119829746A
Method and device for performing context retrieval based on code repository
CN119883276A
Method and apparatus for performing context retrieval on basis of code repository
WO2026138158A1
Multi-source retrieval augmented code generation
WO2026150256A1
