Code search method, system and device based on large language model classification and prototype integration
Through the method of large language model classification and prototype integration, the semantic ambiguity problem of ambiguous queries in code search is solved, the search accuracy and robustness are improved, and it is suitable for code search needs in complex scenarios.
Patent Information
- Application Number
- CN202510438207.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-09
- Publication Date
- 2025-09-12
- Estimated Expiration
- 2045-04-09
AI Technical Summary
Existing code search technologies have difficulty in accurately understanding the semantic intent of queries and code when dealing with ambiguous manually written queries, resulting in performance degradation, especially poor search results on large-scale open source platforms.
A method based on large language model classification and prototype integration is adopted to improve the matching accuracy between queries and codes through data cleaning, multimodal hard negative sample loss training, coarse-grained screening and fine-grained integration.
It improves the accuracy and relevance of code search, enhances the robustness and generalization ability of the model, supports flexible adaptation to complex scenarios, and accelerates training convergence efficiency, making it suitable for real-time search of large-scale code bases.
Smart Images

Figure CN119961381B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of code semantic analysis, and in particular to a code search method, system and device based on large language model classification and prototype integration. Background Art
[0002] To help developers retrieve relevant code snippets from open source platforms such as GitHub, Stack Overflow, etc., researchers have proposed many methods, called code search. This requires developers to input natural language from websites during the development process to retrieve semantically matching code snippets.
[0003] Existing code search techniques can be roughly divided into three categories: information retrieval (IR)-based methods, deep learning methods without fusion, and deep learning methods with fusion strategies. Research in the first category primarily focuses on traditional information retrieval techniques to extract relevant code from software repositories. These methods rely heavily on regular expression matching, making it difficult to capture the deeper semantic relationships between queries and code. With the emergence of deep learning, a growing number of studies have turned to using deep learning models to learn features from code. These methods, belonging to the second category, typically use a single model. While deep learning has effectively improved code search performance, these methods remain challenging due to the semantic gap between query language and code language. This gap leads to performance bottlenecks, which have only been partially alleviated with the introduction of pre-trained models. Furthermore, some pre-trained model-based methods employ techniques such as contrastive learning to improve code semantic understanding and search performance. For example, the momentum contrastive learning algorithm CoCoSoDa is introduced into the pre-trained model UniXcoder to learn better feature representations using more negative samples. This method demonstrates compelling performance on the large-scale benchmark dataset CodeSearchNet. However, these methods still struggle when dealing with semantically ambiguous, human-written queries. A third category of methods is multimodal code search that employs fusion strategies. These methods combine multiple representations or results from pre-trained models to improve performance. However, not all techniques achieve good performance on their own.
[0004] Current approaches primarily leverage pre-trained models and feature representation techniques such as contrastive learning to narrow the semantic distance between matching query-code pairs, and improve performance through fusion strategies. However, in real-world search scenarios, manually written queries are often ambiguous and may not accurately reflect the core intent of the code. Existing approaches often suffer from performance degradation when faced with such ambiguous queries. Recently, large language model techniques have been widely used in generative tasks such as code summarization and test generation, and have demonstrated superior performance to traditional pre-trained models. We hypothesize that large language models, with their large parameter size, abundant training data, advanced architecture, and multi-task learning capabilities, can also outperform existing models in understanding ambiguous code queries. However, large language models have not yet been widely used in code search, and previous studies using retrieval-augmented generation techniques have had limited success in applying large language models to code search. Summary of the Invention
[0005] The purpose of the present invention is to provide a code search method, system and device based on large language model classification and prototype integration, which can effectively narrow the semantic gap between queries and codes, solve the problem of semantic ambiguity that may be caused by ambiguous queries, and thus improve the performance of code search.
[0006] To achieve the above objectives, the present invention provides a code search method based on large language model classification and prototype integration, the steps of which are as follows:
[0007] S01. After performing data cleaning on a corpus segment to be processed containing multiple "query-code" pairs, the query source token and the code source token are extracted to obtain cleaned "query-code" source token pairs.
[0008] S02. Classify the “query-code” source Token pairs using a large language model to obtain “query-code” source Token pairs of different categories;
[0009] S03, inputting the "query-code" source token pairs of different categories into multiple pre-trained models, and performing model training using a multimodal hard negative sample loss based on category characteristics to obtain trained expert models of different categories;
[0010] S04. Use the trained expert models of different categories to perform code searches and obtain preliminary search results;
[0011] S05. Filter the preliminary search results using a coarse-grained large language model classification and screening module to obtain filtered code search results;
[0012] S06. Using a fine-grained multimodal integration module, the filtered code search results are integrated to obtain a final search result.
[0013] Preferably, in S01, the data cleaning includes lowercase conversion, underscore splitting, camel case splitting and lemmatization.
[0014] Preferably, in S03, a multimodal hard negative sample loss based on category characteristics is used for model training to obtain trained expert models of different categories, including:
[0015] 1) Calculate the loss value of the model output using multimodal hard negative sample loss based on category characteristics;
[0016] 2) Based on the loss value, update the parameters in the improved model by backpropagating the output result;
[0017] 3) Repeat 1) and 2) until the parameters converge to obtain the trained improved TranS0former model.
[0018] Preferably, the multimodal hard negative sample loss based on category characteristics includes focal loss and triple multimodal loss; the expression of the multimodal hard negative sample loss based on category characteristics is:
[0019] ,
[0020] Where, represents the focal loss, represents the triple multimodal loss, represents the weight of the focal loss, represents the weight of triplet loss;
[0021] The focal loss improves the convergence speed by focusing on the hard negative samples in the training data; the expression of the focal loss is:
[0022] ,
[0023] Where, Indicates the prediction The probability that a query matches a code, Indicates that the The labels of the training samples are converted to one-hot encoding. is the amount of data to be predicted, is the adjustment coefficient for the number of positive and negative samples, The adjustment coefficient for classifying difficult and easy samples;
[0024] The triple multimodal loss includes inter-modal loss and intra-modal loss; the inter-modal loss represents the loss between code and query, and the intra-modal loss represents the loss between code and code, and query and query; the triple multimodal loss is expressed as:
[0025] ,
[0026] ,
[0027] ,
[0028] ,
[0029] ,
[0030] Where, Represents two features and The cosine distance between Representation and Code Anchors Positive code samples with the same category, Representation and Code Anchors There are negative samples of codes of different categories, Representation and Query Anchors Positive query samples with the same category, Representation and Query Anchors have represents the specific training category of the model, and are the minimum distances that need to be maintained between positive and negative pairs, and Greater than m.
[0031] Preferably, in S03, during the model training process, after the expert model training is completed, the enhanced expert model is retrained for the difficult samples where the expert model of each category performs poorly, and the expression is:
[0032] ,
[0033] Where, and Indicates the The more difficult samples in the data where the expert model performs poorly, and It represents the feature representation that the model is continuously optimized through the loss function during the training process. Represents an enhanced expert model.
[0034] Preferably, in S05, the coarse-grained large language model classification and screening module uses the large language model to predict a category for each query and code, and assigns a higher confidence to search results predicted to be of the same category. Codes and queries of the same category should have a relatively high similarity score, and codes and queries of different categories should have a lower similarity score. The expression of the similarity score is:
[0035] ,
[0036] Where, Indicates the query samples, Indicates the Code samples, Indicates the confidence coefficient added when the predictions are exactly the same category, Indicates the confidence coefficient added when there is an intersection between the predicted categories. and They represent the first query samples and Classification labels for code samples.
[0037] Preferably, in S06, the fine-grained multimodal integration module integrates the preliminary search results of expert models of different categories using a prototype-based integration method, which is expressed as:
[0038] ,
[0039] Where, Indicates the query samples, Indicates the Code samples, Indicates the The code search results after screening by category experts, represents an ensemble method;
[0040] Ensemble methods The purpose is to accurately select the best expert based on the characteristics of the input data, generate a probability distribution of the expert's prediction accuracy based on the input query, and the final output is the weighted sum of all expert outputs, which is expressed as:
[0041] ,
[0042] Where, Indicates the The query characteristics, Indicates the The prototype of the class query data, represents the cosine similarity, Represents the first The classification labels of query samples, Indicates the predicted category With category When matching, add the confidence coefficient;
[0043] Among them, the prototype It represents a representative feature vector that can capture the essence of a class of data. Assuming that query samples in the same category will show similar features after training, these features are defined as prototypes. Features of different categories have different prototypes. The prototype of a category is represented by taking the average of all features of the trained category:
[0044] ,
[0045] Where, represents the first The query characteristics, Indicates training category The total number of query samples.
[0046] A code search system based on large language model classification and prototype integration is applied to any of the above methods, the system comprising:
[0047] The data processing module is used to clean the data of the corpus to be processed containing multiple "query-code" pairs and extract the query source token and code source token;
[0048] A data classification module, configured to classify the query-code source token pairs using a large language model;
[0049] Code search module for:
[0050] Inputting the query-code source token pairs of different categories into multiple pre-trained models, and training the models using a multimodal hard negative loss based on category characteristics to obtain trained expert models of different categories; the multimodal hard negative loss based on category characteristics includes focal loss and triple multimodal loss;
[0051] Code searches are performed using trained expert models of different categories to obtain preliminary search results; the preliminary search results are filtered using a coarse-grained large language model classification and screening module to obtain code search results after filtering the preliminary search results of experts of different categories; the coarse-grained large language model classification and screening module uses a large language model to predict a category for the query and the code respectively, and assigns a higher confidence level to search results predicted to be of the same category; the filtered code search results are integrated using a fine-grained multimodal integration module to obtain a final search result; the fine-grained multimodal integration module uses a prototype-based integration method to integrate the preliminary search results of expert models of different categories.
[0052] An electronic device comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements any one of the above-mentioned methods when executing the computer program.
[0053] According to the specific embodiments provided by the present invention, the present invention discloses the following technical effects:
[0054] (1) Improve search accuracy and relevance
[0055] Using a large language model to perform fine-grained classification of query-code pairs, we train expert models for different categories, allowing each model to focus on domain-specific features and reduce cross-domain noise. Combining a focal loss (to address sample imbalance) with a triple multimodal loss (to optimize cross-modal alignment) enhances the model's ability to distinguish difficult samples and improves query-code matching accuracy. By integrating the outputs of different expert models through prototype weighting and integrating multi-category features, we avoid bias in a single model and further optimize the final results.
[0056] (2) Enhance model robustness and generalization
[0057] By unifying the representation of code and queries through operations like lowercase and camelCase splitting, we reduce the impact of grammatical differences on the model and improve its adaptability to diverse inputs. We retrain an enhanced model for samples where the expert model underperforms, preventing overfitting and improving coverage of edge cases. A triple loss simultaneously constrains inter-modality (code-query) and intra-modality (code-code, query-query) similarity, enhancing the model's ability to capture semantic consistency.
[0058] (3) Accelerate training convergence efficiency
[0059] By adjusting the weights of easy and difficult samples (parameter γ) and the class balance coefficient (α), the model accelerates learning of key samples and shortens convergence time. The coarse-grained classification and screening module quickly filters low-confidence results, reducing subsequent computational overhead; the fine-grained integration module performs weighted fusion only on highly relevant candidates, improving overall efficiency.
[0060] (4) Support flexible adaptation of complex scenarios
[0061] Dynamically integrating text and code features through prototype representations, it's suitable for complex code search needs across languages and frameworks. Expert models can be expanded on demand (e.g., by adding new categories), and the system's modular design facilitates feature iteration and maintenance.
[0062] (5) Practical application value
[0063] Based on pre-trained models and the Transformer architecture, it can be adapted for GPU acceleration to meet the real-time search requirements of large-scale code bases. Classification tags provide interpretability for search results, helping developers understand the matching logic.
[0064] The technical solution of the present invention is further described in detail below through the accompanying drawings and embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0065] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0066] Figure 1 A flowchart of a code search method based on large language model classification and prototype integration according to an embodiment of the subject name of the present invention;
[0067] Figure 2 This is a schematic diagram of model training and actual prediction after training according to an embodiment of the present invention;
[0068] Figure 3 A schematic diagram of the structure of a code search system according to an embodiment of the present invention;
[0069] Figure 4 Schematic diagram of the electronic device structure according to an embodiment of the present invention.
[0070] Reference numerals
[0071] 100. Electronic device; 110. Memory; 120. Processor. DETAILED DESCRIPTION
[0072] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only 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 making creative efforts are within the scope of protection of the present invention.
[0073] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments. Example
[0074] like Figure 1 As shown in the figure, the code search method based on large language model classification and prototype integration has the following steps:
[0075] S01. After performing data cleaning on a corpus segment to be processed containing multiple "query-code" pairs, query source tokens and code source tokens are extracted to obtain cleaned "query-code" source token pairs.
[0076] In the application, you can use Python toolkits and specific cleaning methods to clean the data and extract tokens from the corpus. Examples of data cleaning methods include lowercase, underscore splitting, camel case splitting, and lemmatization.
[0077] Data cleaning can remove noise data such as comments, blank lines, and non-ASCII characters in the code to ensure the purity of the source tokens. The cleaned code is segmented to generate a source token sequence. At the same time, a target token sequence in the corresponding target language (such as natural language) can be prepared as a summary reference.
[0078] During the training process, the corpus segments to be processed are the sub-datasets of six code languages in the CodeSearchNet dataset, which can include Ruby dataset, JavaScript dataset, Java dataset, Go dataset, Php dataset and Python dataset; the Ruby dataset contains a total of 27,588 data instances, 24,927 instances are used for training, and 1,400 instances are used for verification; the Ruby dataset contains a total of 27,588 data instances, 24,927 instances are used for training, and 1,400 instances are used for verification; the JavaScript dataset contains a total of 65,201 data instances, 5 8,025 instances are used for training and 3,885 instances are used for validation; the Java dataset contains 181,061 data instances, of which 164,923 instances are used for training and 5,183 instances are used for validation; the Go dataset contains a total of 182,735 data instances, of which 167,288 instances are used for training and 7,325 instances are used for validation; the Php dataset contains a total of 268,237 data instances, of which 241,241 instances are used for training and 12,982 instances are used for validation; the Python dataset contains a total of 280,652 data instances, of which 251,820 instances are used for training and 13,914 instances are used for validation.
[0079] When performing data cleaning on the corpus to be processed, the following operations can be performed:
[0080] Tokenize the code snippets in the corpus to be processed and convert each token to lowercase;
[0081] Use the NLTK library to split each identifier based on camel case and underscores; for example, the identifier "IndexOutOfBoundError" will be broken down into "index", "out", "of", "bound", and "error";
[0082] Use spacy2 to lemmatize each word and standardize its form to improve uniformity.
[0083] S02. Use a large language model to classify the "query-code" source Token pairs to obtain "query-code" source Token pairs of different categories.
[0084] S03. Input the "query-code" source token pairs of different categories into multiple pre-trained models, and use multimodal hard negative sample loss based on category characteristics to train the models to obtain trained expert models of different categories. This includes:
[0085] 1) Calculate the loss value of the model output using multimodal hard negative sample loss based on category characteristics;
[0086] 2) Based on the loss value, update the parameters in the improved model by backpropagating the output result;
[0087] 3) Repeat 1) and 2) until the parameters converge to obtain a trained improved TranSformer model.
[0088] like Figure 2 As shown in the upper part of the figure, the classified source tokens are input into multiple models for training. The right side shows the multimodal hard negative sample loss based on category characteristics used in the training process. The model trained in this way can be regarded as a code search expert that only focuses on a certain category.
[0089] The multimodal hard negative sample loss based on category characteristics includes focal loss and triple multimodal loss. The expression of the multimodal hard negative sample loss based on category characteristics is:
[0090] ,
[0091] Where, represents the focal loss, represents the triple multimodal loss, represents the weight of the focal loss, represents the weight of triplet loss;
[0092] The focal loss improves the convergence speed by focusing on the hard negative samples in the training data; the expression of the focal loss is:
[0093] ,
[0094] Where, Indicates the prediction The probability that a query matches a code, Indicates that the The labels of the training samples are converted to one-hot encoding. is the amount of data to be predicted, is the adjustment coefficient for the number of positive and negative samples, The adjustment coefficient for classifying difficult and easy samples;
[0095] The triple multimodal loss includes inter-modal loss and intra-modal loss; the inter-modal loss represents the loss between code and query, and the intra-modal loss represents the loss between code and code, and query and query; the triple multimodal loss is expressed as:
[0096] ,
[0097] ,
[0098] ,
[0099] ,
[0100] ,
[0101] Where, Represents two features and The cosine distance between Representation and Code Anchors Positive code samples with the same category, Representation and Code Anchors There are negative samples of codes of different categories, Representation and Query Anchors Positive query samples with the same category, Representation and Query Anchors have represents the specific training category of the model, and are the minimum distances that need to be maintained between positive and negative pairs, and Greater than m.
[0102] After completing the training of the expert model, for the more difficult samples where the expert model of each category performs poorly, the enhanced expert model is retrained. The expression is:
[0103] ,
[0104] Where, and Indicates the The more difficult samples in the data where the expert model performs poorly, and It represents the feature representation that the model is continuously optimized through the loss function during the training process. Represents an enhanced expert model.
[0105] S04. Use the trained expert models of different categories to perform code searches and obtain preliminary search results.
[0106] S05, using the coarse-grained large language model classification and screening module to screen the preliminary search results to obtain screened code search results; Figure 2 The initial search results are then coarsely filtered through a large language model classification and filtering module (CCR). This module uses the large language model to predict a category for each query and code, assigning a higher confidence level to search results that predict the same category. Code and query of the same category should have a relatively high similarity score, while code and query of different categories should have a lower similarity score. The similarity score is expressed as:
[0107] ,
[0108] Where, Indicates the query samples, Indicates the Code samples, Indicates the confidence coefficient added when the predictions are exactly the same category, shows the confidence coefficient added when there is an intersection between the predicted categories, and They represent the first query samples and Classification labels for code samples.
[0109] S06. Using a fine-grained multimodal integration module, the filtered code search results are integrated to obtain the final search results. The filtered code search results are passed through a fine-grained multimodal integration module FMPI to obtain the final result. This module uses a prototype-based integration method to integrate the preliminary search results of different categories of expert models, and its expression is:
[0110] ,
[0111] Where, Indicates the query samples, Indicates the Code samples, Indicates the The code search results after screening by category experts, Represents an ensemble method.
[0112] Ensemble methods The purpose is to accurately select the best expert based on the characteristics of the input data, generate a probability distribution of the expert's prediction accuracy based on the input query, and the final output is the weighted sum of all expert outputs, which is expressed as:
[0113] ,
[0114] Where, Indicates the The query characteristics, Indicates the The prototype of the class query data, represents the cosine similarity, Represents the first The classification labels of query samples, Indicates the predicted category With category When matching, add a confidence factor.
[0115] Among them, the prototype It represents a representative feature vector that can capture the essence of a class of data. Assuming that query samples in the same category will show similar features after training, these features are defined as prototypes. Features of different categories have different prototypes. The prototype of a category is represented by taking the average of all features of the trained category:
[0116] ,
[0117] Where, represents the first The query characteristics, Indicates training category k The total number of query samples.
[0118] Through the above process, accurate code search can be achieved. Corresponding to the above application function implementation method embodiment, the present invention also provides a code search system, device and corresponding embodiment based on large language model classification and prototype integration. Figure 3 shown.
[0119] The code search system includes:
[0120] The data processing module is used to clean the corpus to be processed (containing multiple "query-code" pairs) and then extract the query source token and code source token;
[0121] A data classification module is used to classify the query-code source token pairs using a large language model:
[0122] Code search module for:
[0123] Inputting the query-code source token pairs of different categories into multiple pre-trained models, and training the models using a multimodal hard negative loss based on category characteristics to obtain trained expert models of different categories; the multimodal hard negative loss based on category characteristics includes focal loss and triple multimodal loss;
[0124] Code searches are performed using trained expert models of different categories to obtain preliminary search results; the preliminary search results are filtered using a coarse-grained large language model classification and screening module to obtain code search results after filtering the preliminary search results of experts of different categories; the coarse-grained large language model classification and screening module uses a large language model to predict a category for the query and the code respectively, and assigns a higher confidence level to search results predicted to be of the same category; the filtered code search results are integrated using a fine-grained multimodal integration module to obtain a final search result; the fine-grained multimodal integration module uses a prototype-based integration method to integrate the preliminary search results of expert models of different categories.
[0125] Regarding the system in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated again here.
[0126] An embodiment of the present invention further provides an electronic device, such as Figure 4 As shown, the electronic device 100 includes a memory 110 and a processor 120 .
[0127] The processor 120 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.
[0128] Memory 110 may include various types of storage units, such as system memory, read-only memory (ROM), and permanent storage. ROM may store static data or instructions required by processor 120 or other computer modules. Permanent storage may be a readable and writable storage device. Permanent storage may be a non-volatile storage device that retains stored instructions and data even when the computer is powered off. In some embodiments, the permanent storage device utilizes a mass storage device (e.g., a magnetic or optical disk, flash memory). In other embodiments, the permanent storage device may be a removable storage device (e.g., a floppy disk, optical drive). System memory may be a readable and writable storage device or a volatile readable and writable storage device, such as dynamic random access memory (DRAM). System memory may store some or all instructions and data required by the processor during operation. Furthermore, memory 110 may include any combination of computer-readable storage media, including various types of semiconductor memory chips (e.g., DRAM, SRAM, SDRAM, flash memory, programmable read-only memory), as well as magnetic disks and / or optical disks. In some embodiments, the memory 110 may include a readable and / or writable removable storage device, such as a compact disc (CD), a read-only digital versatile disc (e.g., DVD-ROM, double-layer DVD-ROM), a read-only Blu-ray disc, an ultra-density optical disc, a flash memory card (e.g., SD card, mini SD card, Micro-SD card, etc.), a magnetic floppy disk, etc. Computer-readable storage media do not contain carrier waves and transient electronic signals transmitted wirelessly or by wire.
[0129] The memory 110 stores executable codes. When the executable codes are processed by the processor 120 , the processor 120 may execute part or all of the above-mentioned methods.
[0130] The remaining technical features of the above embodiments can be flexibly selected by those skilled in the art to meet specific practical needs. However, it will be apparent to those skilled in the art that these specific details are not required to practice the present invention. In other instances, to avoid obscuring the present invention, well-known components, structures, or parts are not described in detail, and are therefore within the scope of protection of the technical solutions claimed in the claims.
[0131] Modifications and variations made by those skilled in the art without departing from the spirit and scope of the present invention are intended to be within the scope of the claims appended hereto. In the foregoing description, numerous specific details are set forth to provide a thorough understanding of the present invention. However, it will be apparent to those skilled in the art that these specific details are not necessarily required to practice the present invention. In other instances, well-known techniques, such as specific construction details, operating conditions, and other technical requirements, are not described in detail to avoid obscuring the present invention.
[0132] This document uses specific examples to illustrate the principles and implementation methods of the present invention. The above examples are only intended to help understand the method and core concept of the present invention. At the same time, those skilled in the art will find that the specific implementation methods and application scopes may vary based on the concept of the present invention. In summary, the contents of this specification should not be construed as limiting the present invention.
Claims
1. A code search method based on large language model classification and prototype integration, characterized in that: Here are the steps: S01. After performing data cleansing on a corpus segment to be processed containing multiple "query-code" pairs, the query source token and the code source token are extracted to obtain cleaned "query-code" source token pairs. S02. Classify the “query-code” source Token pairs using a large language model to obtain different categories of “query-code” source Token pairs; S03. Input the "query-code" source token pairs of different categories into multiple pre-trained models, and use the multimodal hard negative sample loss based on category characteristics to train the models to obtain trained expert models of different categories; the multimodal hard negative sample loss based on category characteristics includes focal loss and triple multimodal loss; the expression of the multimodal hard negative sample loss based on category characteristics is: , Where, represents the focal loss, represents the triple multimodal loss, represents the weight of the focal loss, represents the weight of triplet loss; S04. Use the trained expert models of different categories to perform code searches and obtain preliminary search results; S05. Filter the preliminary search results using a coarse-grained large language model classification and screening module to obtain filtered code search results. The coarse-grained large language model classification and screening module uses a large language model to predict a category for each query and code, and assigns a higher confidence level to search results that are predicted to be of the same category. Codes and queries of the same category should have a relatively high similarity score, and codes and queries of different categories should have a lower similarity score. The similarity score is expressed as: , Where, Indicates the query samples, Indicates the Code samples, Indicates the confidence coefficient added when the predictions are exactly the same category, Indicates the confidence coefficient added when there is an intersection between the predicted categories. and They represent the first query samples and Classification labels of code samples; S06. Using a fine-grained multimodal integration module, the filtered code search results are integrated to obtain a final search result. The fine-grained multimodal integration module integrates the preliminary search results of expert models of different categories using a prototype-based integration method, which is expressed as: , Where, Indicates the query samples, Indicates the Code samples, Indicates the The code search results after screening by category experts, represents an ensemble method; Ensemble methods The purpose is to accurately select the best expert based on the characteristics of the input data, generate a probability distribution of the expert's prediction accuracy based on the input query, and the final output is the weighted sum of all expert outputs, which is expressed as: , Where, Indicates the The characteristics of the query, Indicates the The prototype of the class query data, represents the cosine similarity, Represents the first The classification labels of query samples, Indicates the predicted category With category When matching, add the confidence coefficient; Among them, the prototype It represents a representative feature vector that can capture the essence of a class of data. Assuming that query samples in the same category will show similar features after training, these features are defined as prototypes. Features of different categories have different prototypes. The prototype of a category is represented by taking the average of all features of the trained category: , Where, Indicates training category Middle The characteristics of the query, Indicates training category The total number of query samples.
2. The code search method based on large language model classification and prototype integration according to claim 1, characterized in that: In S01, the data cleaning includes lowercase conversion, underscore splitting, camel case splitting and lemmatization.
3. The code search method based on large language model classification and prototype integration according to claim 1, characterized in that: In S03, we use the multimodal hard negative sample loss based on category characteristics to train the model and obtain trained expert models of different categories, including: 1) Calculate the loss value of the model output using multimodal hard negative sample loss based on category characteristics; 2) Based on the loss value, update the parameters in the improved model by backpropagating the output result; 3) Repeat 1) and 2) until the parameters converge to obtain a trained improved TranSformer model.
4. The code search method based on large language model classification and prototype integration according to claim 3 is characterized in that: The focal loss improves the convergence speed by focusing on the hard negative samples in the training data; the expression of the focal loss is: , Where, Indicates the prediction The probability that a query matches a code, Indicates that the The labels of the training samples are converted to one-hot encoding. is the amount of data to be predicted, is the adjustment coefficient for the number of positive and negative samples, The adjustment coefficient for classifying difficult and easy samples; The triple multimodal loss includes inter-modal loss and intra-modal loss; the inter-modal loss represents the loss between code and query, and the intra-modal loss represents the loss between code and code, and query and query; the triple multimodal loss is expressed as: , Where, Represents two features and The cosine distance between Representation and Code Anchors Positive code samples with the same category, Representation and Code Anchors There are negative samples of codes of different categories, Representation and Query Anchors Positive query samples with the same category, Representation and Query Anchors have represents the specific training category of the model, and are the minimum distances that need to be maintained between positive and negative pairs, and Greater than .
5. The code search method based on large language model classification and prototype integration according to claim 4 is characterized in that: In S03, during the model training process, after the expert model training is completed, the enhanced expert model is retrained for the difficult samples where the expert model of each category performs poorly. The expression is: , Where, and Indicates the The more difficult samples in the data where the expert model performs poorly, and It represents the feature representation that the model is continuously optimized through the loss function during the training process. Represents an enhanced expert model.
6. A code search system based on large language model classification and prototype integration, characterized by: The method according to any one of claims 1 to 5, wherein the system comprises: The data processing module is used to clean the data of the corpus to be processed containing multiple "query-code" pairs and extract the query source token and code source token; A data classification module, configured to classify the query-code source token pairs using a large language model; Code search module for: Input the query-code source token pairs of different categories into multiple pre-trained models, and train the models using a multimodal hard negative loss based on category characteristics to obtain trained expert models of different categories; the multimodal hard negative loss based on category characteristics includes focal loss and triple multimodal loss; Code searches are performed using trained expert models of different categories to obtain preliminary search results; the preliminary search results are filtered using a coarse-grained large language model classification and screening module to obtain code search results after filtering the preliminary search results of experts of different categories; the coarse-grained large language model classification and screening module uses a large language model to predict a category for the query and the code respectively, and assigns a higher confidence level to search results predicted to be of the same category; the filtered code search results are integrated using a fine-grained multimodal integration module to obtain a final search result; the fine-grained multimodal integration module uses a prototype-based integration method to integrate the preliminary search results of expert models of different categories.
7. An electronic device, characterized in that: The method comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the method according to any one of claims 1 to 5 when executing the computer program.
Citation Information
Patent Citations
Code searching method based on annotation semantic information
CN112507065A
Code search method based on post-interaction mechanism
CN117112851A