Programming code searching method and device and medium

The large language model is used to perform annotation encoding and similarity calculation on the code base, which solves the problems of automatic code search model in matching accuracy and long code processing, realizes efficient and accurate code search function, and significantly saves resource costs.

CN118568192APending Publication Date: 2024-08-30UNITED AUTOMOTIVE ELECTRONICS SYST
View PDF 0 Cites 4 Cited by

Patent Information

Application Number
CN202410195719.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-02-22
Publication Date
2024-08-30

AI Technical Summary

Technical Problem

The existing code automatic search model has low accuracy in matching query questions and code base codes, and has limitations in processing long codes, which affects the accuracy of the search.

Method used

Use a large language model to annotate and code the code in the code base, and use the coding model to calculate the similarity between the query statement and the code, select the code that meets the preset requirements as the search target, and use the large general language model and coding model to combine user questions and The code is translated into the same encoding to implement the code search function.

Benefits of technology

Significantly improves the accuracy and efficiency of code search, saves time and computing power costs in model construction and data training, improves the effect of annotation generation through the combination of prompt words and example explanations, and uses a deep neural network model with a self-attention mechanism Improved coding accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118568192A
    Figure CN118568192A
Patent Text Reader

Abstract

The invention relates to the technical field of field programming, in particular to a programming code searching method and device and a medium. The method comprises the following steps of: respectively annotating each section of code in a code library by adopting a general language large model; annotating and coding each section of code by adopting a coding model to obtain a first code; encoding the query statement of the to-be-searched code by adopting an encoding model to obtain a second code; and calculating the similarity between the second code and the plurality of first codes, and selecting a code corresponding to the first code meeting a preset requirement as a search target. According to the method, an existing language large model is utilized, the model construction and data training process is omitted, and the time cost and the computing power cost are remarkably saved; problems and code annotations proposed by a user are translated into the same code through a general language large model and a coding model, matching is completed through coding similarity calculation, and finally the code searching function is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of programming technology, and in particular to a programming code search method, device and medium. Background Art

[0002] In order to support software developers to quickly query program codes in existing databases, a variety of automatic code search models have been proposed, such as CodeBert, GraphCodeBert, etc. However, they all have some problems: First, these models directly encode the query questions raised by users and the codes in the code base. However, since the query questions and code structures are very different, the matching accuracy is not high. In addition, these encoders also have restrictions on the length of the input code. For codes that exceed the limit length, they can only be divided into blocks before encoding, which greatly affects the accuracy of code search.

[0003] In recent years, the emergence of large language models (LLMs), such as ChatGPT, MOSS, and ChatGLM, has greatly improved the performance of natural language processing tasks and is regarded as a revolutionary advancement in the field of natural language processing. It also provides a new idea for automatic code search. These large language pre-trained models often have more than 150 billion parameters. They are first pre-trained on a huge corpus using a simple, autoregressive, next token prediction form, and then fine-tuned for downstream tasks. Multiple examples have proven that large language models that have only been pre-trained can achieve good generalization results on many tasks, including code-related tasks. Summary of the invention

[0004] The invention discloses a programming code search method, device and medium, which realizes the automatic code search function through a large language model.

[0005] To achieve the above purpose, on the one hand, a programming code search method is provided, and the specific method is as follows:

[0006] Use a general language large model to annotate each piece of code in the code base;

[0007] Using the coding model to encode each code segment annotation to obtain a first code;

[0008] Encode the query statement of the code to be searched using the encoding model to obtain a second code;

[0009] The similarity between the second code and several first codes is calculated, and the code corresponding to the first code that meets the preset requirements is selected as the search target.

[0010] The advantage of this embodiment is that it utilizes the existing language big model, saves the model construction and data training process, and significantly saves time and computing costs; the questions raised by the user and the code comments are translated into the same encoding through the general language big model and the encoding model respectively, and then the matching is completed through the encoding similarity calculation, and finally the code search function is realized.

[0011] Optionally, the general language large model is ChatGLM or ChatGPT.

[0012] Furthermore, when the general language large model is used to annotate each code segment in the code base, the input statement includes: prompt words, example descriptions and code base codes.

[0013] The advantage of this embodiment is that the effect of the large language model code comment generation task is significantly improved by combining the prompt words with example descriptions.

[0014] Furthermore, the method for selecting the prompt word is as follows:

[0015] Preset several alternative prompt words;

[0016] Use each prompt word as a guide for the general language model, enter the sample code to obtain comments;

[0017] Calculate the cosine similarity between the acquired annotations and the sample annotations;

[0018] Select the prompt word with the highest similarity.

[0019] The advantage of this embodiment is that, through the prompt word selection method, preferred prompt words can be obtained, which significantly improves the accuracy of annotation translation.

[0020] Furthermore, the prompt word content includes: Please generate a short comment in onesentence for the following function: <code>.

[0021] Furthermore, the example description includes more than one annotation instance, and each annotation includes a code to be annotated and a code annotation.

[0022] The advantage of this embodiment is that the general large language model can be trained into the required annotation translation model through annotation examples, thereby improving the accuracy of annotation translation.

[0023] Furthermore, the encoding model is a deep neural network model with a self-attention mechanism.

[0024] Furthermore, the encoding model is a GraphCodeBert model.

[0025] Further, the GraphCodeBert model includes a masked language modeling pre-training task and a replacement token detection pre-training task;

[0026] The masked language modeling pre-training task randomly masks one or more annotation languages ​​and predicts the masked annotation languages ​​based on the annotation language context;

[0027] The replacement token detection pre-training task determines whether the prediction result is the same as before occlusion.

[0028] The advantage of this embodiment is that the ability and accuracy of the Embedding model to convert encoding can be improved through masking language training.

[0029] Furthermore, the similarity calculation method between the first code and any second code is as follows:

[0030] Calculate the cosine similarity between each encoding segment of the first encoding and all encoding segments of the second encoding respectively;

[0031] A similarity matrix is ​​constructed using a number of cosine similarities, the maximum values ​​in each row and each column are selected respectively, and the average value of the maximum values ​​is calculated as the quantization score of the first code.

[0032] The advantage of this embodiment is that there are several ways to express the query statement, no matter which expression method includes coded keywords, by comparing the first code with the second code in turn, it can be ensured that no keyword is missed, which significantly improves the comparison accuracy.

[0033] Furthermore, the code corresponding to the first code that meets the preset requirements is selected as the search target, and the specific method is as follows:

[0034] The quantization scores of several first codes are sorted from high to low, and the code corresponding to the first code of the preset sorting is selected as the search target.

[0035] Optionally, the code in the code library is Python, C, C++ or Java.

[0036] To achieve the above object, on the other hand, a programming code search device is provided, comprising: an annotation module, a first encoding module, a second encoding module and a calculation module;

[0037] The annotation module uses a general language large model to annotate each code segment in the code base;

[0038] The first encoding module uses the encoding model to encode each code segment annotation to obtain a first code;

[0039] The second encoding module encodes the query statement of the code to be searched using the encoding model to obtain a second code;

[0040] The calculation module calculates the similarity between the second code and a plurality of first codes, and selects the code corresponding to the first code that meets the preset requirements as the search target.

[0041] To achieve the above object, on the other hand, a storage medium is provided, which stores a plurality of instructions, and a processor loads the plurality of instructions to execute the above programming code search method.

[0042] It should be noted that the terms "first", "second" and similar terms used in this article are only for describing the various components in the technical solution, and do not constitute a limitation of the technical solution, nor can they be understood as an indication or suggestion of the importance of the corresponding elements; an element with terms such as "first", "second" and similar terms indicates that the corresponding technical solution contains at least one of the element. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] In order to more clearly illustrate the technical solution of the present invention and facilitate further understanding of the technical effects, technical features and purposes of the present invention, the present invention is described in detail below in conjunction with the accompanying drawings. The accompanying drawings constitute an essential part of the specification and are used together with the embodiments of the present invention to illustrate the technical solution of the present invention, but do not constitute a limitation to the present invention.

[0044] The same reference numerals in the accompanying drawings represent the same components, specifically:

[0045] Figure 1 This is the overall framework diagram of the search algorithm.

[0046] Figure 2 Schematic diagram of the code flow for the general language large model annotation.

[0047] Figure 3 This is a schematic diagram of the Embedding model structure.

[0048] Figure 4 Schematic diagram of experimental verification.

[0049] Figure 5 This is a schematic diagram of the search interface.

[0050] Figure 6 This is a schematic diagram of the process in Example 1. DETAILED DESCRIPTION

[0051] The present invention will be further described in detail below in conjunction with the accompanying drawings and embodiments. Of course, the specific embodiments described below are only intended to explain the technical solutions of the present invention, rather than to limit the present invention. In addition, the parts described in the embodiments or the accompanying drawings are only examples of the relevant parts of the present invention, rather than the entirety of the present invention.

[0052] Embodiment 1:

[0053] A programming code search method uses the excellent performance of a general large language model in code-related tasks. First, each piece of code in the code base is put into the large language model to generate corresponding comments. The comments are descriptions of the overall function of each piece of code. Then, the Embedding model is used to independently encode the query question raised by the user and the comments generated in the previous step. Finally, the similarity score between the query question encoding and the encoding of each comment is calculated, and the candidate codes in the code base are sorted, so as to finally give the Top K related codes. The overall framework is as follows: Figure 1 shown.

[0054] like Figure 6 As shown, the specific steps are as follows:

[0055] S1. Use a general language large model to annotate each code segment in the code base.

[0056] Specifically, the codes in the code library may be in Python, C, C++, Java, or other programming languages.

[0057] Specifically, the general language big model is ChatGLM, and can also be ChatGPT, and of course can also be other general language big models.

[0058] When using the general language large model to annotate each code segment in the code base, the input statement includes: prompt word Task Prompt, example description Demonstrations and code base code Query. The above contents are merged together and input into LLM to obtain the corresponding comment for the code in Query, namely Target Comment.

[0059] Specifically, the steps for selecting the prompt word are as follows:

[0060] S111. Preset several alternative prompt words.

[0061] S112, using each prompt word as a guide for the general language model, input sample code to obtain annotations.

[0062] S113, calculating the cosine similarity between the acquired annotation and the sample annotation.

[0063] S114: Select the prompt word with the highest similarity.

[0064] After several experiments, the prompt word is selected as Please generate a short comment in onesentence for the following function: <code>This prompt can make full use of LLM's excellent ability in code comment generation tasks and guide LLM to produce concise and accurate answers.

[0065] Specifically, the example description includes more than one annotation instance, and each annotation includes the code to be annotated and the code annotation. Instruction Tuning uses formatted natural language prompts, which are composed of task prompts and demonstrations composed of several task examples. The flowchart of Instruction Tuning is as follows: Figure 2 As shown. The selection of the number of task instances in Demonstrations will also affect the final LLM output results. Too few instances are not conducive to LLM learning the rules, while too many instances will lead to catastrophic forgetting of the model. In the present invention, this embodiment uses 10 instances because this selection enables LLM to provide the best performance in less time.

[0066] In step S1, different prompts play a vital role in the output of LLM. Therefore, how to design a good prompt so that LLM can output concise and accurate answers is our focus. Using ChatGLM as LLM, 30 code segments are randomly selected from the CodeSearchNet dataset, and different prompts are used to input LLM to generate results. Finally, it is found that: "Please generate a short comment in one sentencefor the following function: <code>" as a prompt, not only can LLM output concise annotation descriptions, but the output results also have a higher matching accuracy with the Ground Truth. The matching accuracy is measured by CodeBertScore. The higher the value, the higher the matching accuracy.

[0067] Examples are shown in Tables 1 and 2:

[0068] Table 1. Examples of codes and their corresponding reference annotations

[0069]

[0070] Table 2. Examples of the impact of different prompts on ChatGLM output results

[0071]

[0072]

[0073] S2. Use the coding model to encode each code segment annotation to obtain a first code.

[0074] S3. Encode the query statement of the code to be searched using the encoding model to obtain a second code.

[0075] In step S2 and step S3, the encoding model is a deep neural network model with a self-attention mechanism. In this embodiment, the GraphCodeBert model is selected.

[0076] Large pre-trained models have brought significant improvements to almost every NLP task in the past few years by training deep neural networks on large-scale plain text with self-supervised learning objectives. One of the most representative neural structures is Transformer, and the Embedding model used in this patent also follows this structure, such as Figure 3 It contains multiple self-attention layers and can perform conventional learning through gradient decomposition.

[0077] Specifically, the GraphCodeBert model includes a masked language modeling pre-training task and a replacement token detection pre-training task;

[0078] S31, the masked language modeling pre-training task, randomly masking one or more annotation languages, and predicting the masked annotation languages ​​according to the annotation language context;

[0079] S32, the token replacement detection pre-training task is performed to determine whether the prediction result is the same as before occlusion.

[0080] The Embedding model includes two pre-training tasks, namely Masked Language Modeling and Replaced Token Detection. Among them, 1. Masked Language Modeling randomly masks one or more words in the Mask natural language and code, allowing the model network to predict the original word token at the masked part based on the context; 2. Replaced Token Detection first uses the natural language generator NL Generator and the code generator Code Generator to predict the Mask part respectively, and then puts the predicted result into the natural language-code discriminator NL-Code Discriminator to determine whether the predicted result is the same as the original Token. The trained Embedding model can be used as an encoder for natural language and code. This embodiment uses the Embedding model as a natural language encoder to encode user questions and code comments.

[0081] S4. Calculate the similarity between the second code and several first codes, and select the code corresponding to the first code that meets the preset requirements as the search target.

[0082] After obtaining the encoded user questions and code comments, for each code comment encoding, it is necessary to solve the similarity between it and the user question encoding and give a quantitative score, which will be used as a metric for subsequent sorting.

[0083] Specifically, the similarity calculation method between the first code and any second code is as follows:

[0084] S41, respectively calculating the cosine similarity between each coding segment of the first coding and all coding segments of the second coding.

[0085] Comment code for each code Where i represents the i-th code segment in the code base, and m is the number of codes in the i-th code segment annotation. Where n is the cosine similarity of each Encoded Token between the number of encoded questions of the user:

[0086] S42. Constructing a similarity matrix using several cosine similarities The maximum value in each row and each column is selected respectively, and the average value of several maximum values ​​is calculated as the quantization score of the first code.

[0087] S43, repeat the above two steps for N code segments and user query questions in the code base respectively to obtain N quantitative scores. Select the top K scores, where K is an adjustable parameter, and index the corresponding top K code segments in the code base as the final output.

[0088] Test verification:

[0089] A code base is constructed that contains multiple different code segments, some of which are similar to each other. Figure 4 Some test examples are shown. In this test experiment, we set the parameter K to 3. When the user enters a query question at Query, the algorithm will return the three codes with the highest similarity to the question and give the corresponding similarity scores. From the experimental results, we can see that even if the three codes are similar to each other, the algorithm can understand the user's question and put the code that best meets the query question requirements in the first place.

[0090] Based on LangChain, a corresponding code automatic search interface ChatBot was developed, such as Figure 5 As shown. The user enters the question they want to query in the chat box and clicks the submit button. ChatBot will search for the most relevant code in the existing code library according to the algorithm proposed in this patent, and finally output the code the user wants to query.

[0091] Embodiment 2:

[0092] A programming code search device includes: an annotation module, a first encoding module, a second encoding module and a calculation module;

[0093] The annotation module uses a general language large model to annotate each code segment in the code base;

[0094] The first encoding module uses the encoding model to encode each code segment annotation to obtain a first code;

[0095] The second encoding module encodes the query statement of the code to be searched using the encoding model to obtain a second code;

[0096] The calculation module calculates the similarity between the second code and a plurality of first codes, and selects the code corresponding to the first code that meets the preset requirements as the search target.

[0097] It should be noted that the above embodiments are only for more clearly illustrating the technical solutions of the present invention. Those skilled in the art will understand that the implementation methods of the present invention are not limited to the above contents, and obvious changes, replacements or substitutions based on the above contents do not exceed the scope of the technical solutions of the present invention; other implementation methods will also fall within the scope of the present invention without departing from the concept of the present invention.< / code> < / code> < / code>

Claims

1. A programming code search method, characterized in that: The specific method is as follows: Use a general language large model to annotate each piece of code in the code base; Using the coding model to encode each code segment annotation to obtain a first code; Encode the query statement of the code to be searched using the encoding model to obtain a second code; The similarity between the second code and several first codes is calculated, and the code corresponding to the first code that meets the preset requirements is selected as the search target.

2. The programming code search method according to claim 1, characterized in that: The general language model is ChatGLM or ChatGPT.

3. The programming code search method according to claim 1, characterized in that: When using the general language large model to annotate each code segment in the code base, the input statement includes: prompt words, example descriptions, and code base codes.

4. The programming code search method according to claim 3, characterized in that: The method for selecting the prompt word is as follows: Preset several alternative prompt words; Use each prompt word as a guide for the general language model, enter the sample code to obtain comments; Calculate the cosine similarity between the acquired annotations and the sample annotations; Select the prompt word with the highest similarity.

5. The programming code search method according to claim 4, characterized in that: The prompt words include: Please generate a short comment in one sentence for the following function: <code>。< / code> <code> 6. The programming code search method according to claim 3, characterized in that: The example description includes more than one annotation instance, and each annotation includes a code to be annotated and a code comment.

7. The programming code search method according to claim 1, characterized in that: The encoding model is a deep neural network model with a self-attention mechanism.

8. The programming code search method according to claim 7, characterized in that: The encoding model is a GraphCodeBert model.

9. The programming code search method according to claim 8, characterized in that: The GraphCodeBert model includes a masked language modeling pre-training task and a replacement token detection pre-training task; The masked language modeling pre-training task randomly masks one or more annotation languages ​​and predicts the masked annotation languages ​​based on the annotation language context; The replacement token detection pre-training task determines whether the prediction result is the same as before occlusion.

10. The programming code search method according to claim 1, characterized in that: The similarity calculation method of the first code and any second code is as follows: Calculate the cosine similarity between each encoding segment of the first encoding and all encoding segments of the second encoding respectively; A similarity matrix is ​​constructed using a number of cosine similarities, the maximum values ​​in each row and each column are selected respectively, and the average value of the maximum values ​​is calculated as the quantization score of the first code.

11. The programming code search method according to claim 10, characterized in that: Select the code corresponding to the first code that meets the preset requirements as the search target. The specific method is as follows: The quantization scores of several first codes are sorted from high to low, and the code corresponding to the first code of the preset sorting is selected as the search target.

12. The programming code search method according to claim 1, characterized in that: The code in the code library is in Python, C, C++ or Java.

13. A programming code search device, characterized in that: include: An annotation module, a first encoding module, a second encoding module and a calculation module; The annotation module uses a general language large model to annotate each code segment in the code base; The first encoding module uses the encoding model to encode each code segment annotation to obtain a first code; The second encoding module encodes the query statement of the code to be searched using the encoding model to obtain a second code; The calculation module calculates the similarity between the second code and a plurality of first codes, and selects the code corresponding to the first code that meets the preset requirements as the search target.

14. A storage medium, characterized in that: A plurality of instructions are stored, and a processor loads the plurality of instructions to execute the programming code searching method according to any one of claims 1 to 12. < / code>

Citation Information

Cited By

  • Code reverse analysis method and system, electronic equipment, medium and product

    CN118778971A

  • Code generation method and device, storage medium and electronic equipment

    CN120447880A

  • Task code segment-oriented large model cue word optimization method and system

    CN122152359A

  • Method and system for generating coding guide on basis of large language model

    WO2026111402A1