Zero sample code search method based on large model enhanced optimization and reordering

By enhancing and re-ranking large models to optimize zero-sample code search, the problems of semantic deviation generated by large models and difficulty in identifying similar codes are solved, achieving more accurate code search.

CN120596592APending Publication Date: 2025-09-05SICHUAN UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510733388.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-04
Publication Date
2025-09-05

AI Technical Summary

Technical Problem

In existing zero-sample code search methods, the enhanced semantics generated by large models deviate from the original semantics, and the pre-trained model has difficulty in accurately distinguishing similar codes, resulting in a decrease in search performance.

Method used

A large language model is used to generate multiple enhanced codes of the original query and code, which are then encoded using a pre-trained code model and average pooled. The search results are optimized by combining similarity smoothing and large model reranking.

Benefits of technology

Improved the accuracy of code search and the ability to identify subtle differences, and improved the performance of zero-sample code search.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120596592A_ABST
    Figure CN120596592A_ABST
Patent Text Reader

Abstract

The invention discloses a zero sample code search method based on large model enhanced optimization and reordering, which comprises the following steps of: firstly, generating a plurality of equivalent enhanced codes of an original query statement and an original code snippet by using a large model, encoding by a pre-training model, and aggregating into an enhanced embedded vector; respectively calculating cross-modal similarities of the embedded vectors before and after enhancement; smoothing the similarity change before and after enhancement so as to reduce the excessive influence of the generation preference of the large model on the real semantics; and then based on a two-stage search strategy of large model reordering, reordering the first k codes in the preliminary search result according to the semantic matching degree, accurately distinguishing target codes from similar codes, and assisting the model to improve the accuracy of the search result.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of code search technology, and in particular to a zero-sample code search method based on large model enhanced optimization and reordering. Background Art

[0002] Code search is a key research task in software engineering. Its goal is to quickly retrieve semantically relevant code snippets using natural language queries. In recent years, with the rapid development of deep learning technology, significant progress has been made in code representation learning. In particular, techniques based on large-scale pre-trained models have significantly improved code search performance. These models draw on pre-training techniques from the field of natural language processing (NLP). They capture the semantic information of code through self-supervised pre-training on large-scale, multi-language programming data.

[0003] The goal of the zero-shot code search task is to achieve accurate matching and retrieval of natural language queries with code snippets in the code base without the need for specific training samples. Previous work typically uses a pre-trained code model without fine-tuning, directly encoding the query and code separately to obtain embeddings, and then sorting the search results based on similarity. This approach leverages the prior knowledge of the pre-trained model, resulting in acceptable search performance. However, when migrating to code bases and datasets that have not been seen in the pre-training phase, there is still significant room for improvement. Recent research has begun to attempt to use large language models (LLMs) to enhance the performance of zero-shot code search, but there are still some limitations: (1) The enhanced semantics generated by the large model deviate to a certain extent from the original semantics. The original query intent in the dataset is ambiguous, and its focus is difficult to accurately determine. There may be multiple codes that meet its intent. Similarly, the semantics of the original code are also diverse, and its focus is not unique. There may be multiple ways to interpret its function or purpose. The content generated by the large model based on the original query or code depends on the model's prior knowledge and is affected by multiple factors such as the corpus and training method during its training. It has certain semantic preferences, which may lead to possible semantic deviations in the generated content. In this case, although the method of generating multiple responses and performing semantic fusion by the large model can increase semantic diversity and thus alleviate the deviation, these multiple responses are still subject to the influence of its preferences as a whole.

[0004] (2) Encoder-based code pre-training models have difficulty distinguishing between codes with extremely similar semantics. These pre-training models can sort the searched code results according to the degree of conformity with the query intent. When the semantics of the target code are highly distinguishable from other codes, the target code is often placed first, achieving accurate search. However, when the semantic similarity between the target code and some other codes is high and there are only slight differences, the pre-training model may find it difficult to distinguish and mistakenly rank similar codes higher, resulting in reduced search performance. Summary of the Invention

[0005] The present invention provides a zero-sample code search method based on large-model enhanced optimization and reordering to solve the problems existing in existing zero-sample search methods, such as the deviation between the enhanced semantics generated by the large model and the original semantics to a certain extent, which fails to accurately reflect the true intention of the code, and the difficulty of pre-trained models in identifying similar codes.

[0006] According to a first aspect, an embodiment provides a zero-sample code search method based on large model enhanced optimization and reordering, the method comprising: Get the original query statement and the original code snippet in the code library; Based on the large language model, generating enhanced codes for the original query statement and the original code snippet respectively; Based on the code pre-training model, the original query statement and the original code snippet are respectively encoded to obtain corresponding embedding vectors, and the enhanced code of the original query statement and the original code snippet are respectively encoded to obtain corresponding embedding vectors; Calculate the similarity between the embedding vectors of the original query statement and the original code snippet before enhancement, and the similarity between the embedding vectors of the original query statement and the enhanced code snippet after enhancement; According to the change of similarity before and after enhancement, the enhanced similarity is smoothed to obtain the final similarity, and the preliminary code search results are obtained according to the final similarity sorting; Based on the large language model, the first k code snippets of the preliminary code search results are reordered according to the matching degree to obtain the optimal code search result.

[0007] Furthermore, based on the code pre-training model, the original query statement and the original code snippet are respectively encoded to obtain corresponding embedding vectors, specifically including: The original query statement is , the original code snippet is , the encoding process is expressed as: ; ; in, represents the embedding vector of the original query sentence, Represents the embedding vector of the original code snippet, and Encoder represents the encoding process of the code pre-training model.

[0008] Furthermore, based on the large language model, enhanced codes for the original query statement and the original code snippet are generated respectively, specifically including: For the original query: Build-based prompt templates for generating code , generate enhanced code: ; in, Indicates that the original query statement The generated An enhancement code, Indicates the large model generation process, prompt template is , the number of enhanced code generation is ; For the original code snippet: First, based on the prompt template for summary generation , using a large language model to generate natural language summaries: ; in, represents the generated summary; Based on the built prompt template for generating code Generate enhanced code: ; in, Representation based on the original code snippet The generated An enhancement code, Indicates the large model generation process, prompt template is , the number of enhanced code generation is .

[0009] Furthermore, based on the code pre-training model, the original query statement and the enhanced code of the original code snippet are respectively encoded to obtain corresponding embedding vectors, specifically including: The generated multiple enhanced codes are first encoded through the code pre-training model, and then aggregated and normalized through average pooling to obtain the corresponding embedding vectors: ; ; in, Indicates the first Enhancement Code The embedding vector of Indicates the first Enhancement Code The embedding vector of Encoder represents the encoding process of the code pre-training model, and the number of enhanced code generation is , .

[0010] Furthermore, the similarity between the embedding vectors corresponding to the original query statement and the original code snippet before enhancement and the similarity between the embedding vectors corresponding to the enhanced code of the original query statement and the original code snippet after enhancement are calculated, specifically including: Assume that there are M original code snippets in the code base, record The embedding vector of the original code snippet is , the embedding vector of the corresponding enhanced code is , ; ; ; in, Embedding vector representing the enhanced code corresponding to the original query With the The embedding vector of the original code snippet corresponding to the enhanced code The similarity of Embedding vector representing the original query sentence With the Embedding vectors of the original code snippets similarity; Due to coexistence codes, so we get The enhanced similarity and Similarity before enhancement: ; ; in, is the enhanced similarity vector, is the similarity vector before enhancement.

[0011] Furthermore, the enhanced similarity is smoothed according to the change in similarity before and after enhancement to obtain a final similarity, and preliminary code search results are obtained according to the final similarity sorting, specifically including: Calculate the similarity difference before and after enhancement: ; Then, extract The positive and negative parts of : ; ; in, Represents a vector Each element in takes a larger value than 0; The final similarity vector after correction is defined as: ; in, and It is a hyperparameter, which is set to negative values ​​to adjust the weights of the positive and negative parts, weaken the relative changes, and thus achieve smoothing operations; Will As the basis of the search results, the values ​​of each element in the vector are sorted in descending order to obtain a preliminary search result list.

[0012] Furthermore, based on the large language model, the first k code snippets of the preliminary code search results are reordered according to the matching degree to obtain the optimal code search results, specifically including: Based on the constructed prompt template for reordering, the selected front matter is analyzed using a large language model. The semantics of each code and the degree of matching with the original query statement are used to output fine-grained ranking results.

[0013] Furthermore, the method further comprises: The processing process of the large language model includes: first using the DeepSeek-R1 model to generate a few-shot examples, then building an efficient prompt template based on the few-shot examples, and then inputting it into the DeepSeek-V3 model to generate the final result.

[0014] Furthermore, the method further comprises: The zero-shot code search method was verified under different code base datasets and different code pre-training models. The evaluation indicators included average reciprocal rank and Top-k recall. The average reciprocal rank was used to measure the average rank of the target code in the retrieval list, and the Top-k recall was used to measure the proportion of queries with the target code in the top k search results.

[0015] According to a second aspect, an embodiment provides a zero-sample code search system based on large model enhanced optimization and reordering, the system comprising: The data preparation module is used to obtain the original query statements and original code snippets in the code library; An enhanced code generation module, configured to generate enhanced codes for the original query statement and the original code snippet based on a large language model; An encoding module, configured to encode the original query statement and the original code snippet based on a code pre-training model to obtain corresponding embedding vectors, and to encode the enhanced code of the original query statement and the original code snippet to obtain corresponding embedding vectors; A similarity calculation module is used to calculate the similarity of the embedding vectors corresponding to the original query statement and the original code snippet before enhancement, and the similarity of the embedding vectors corresponding to the enhanced code of the original query statement and the original code snippet after enhancement; a smoothing processing module, configured to smooth the enhanced similarity according to the change in similarity before and after enhancement to obtain a final similarity, and to obtain preliminary code search results according to the final similarity sorting; The reordering module is used to reorder the first k code snippets of the preliminary code search results according to the matching degree based on the large language model to obtain the optimal code search results.

[0016] The present invention provides a zero-sample code search method based on large model enhanced optimization and reordering, which has the following beneficial effects: (1) The present invention encodes the original query statement and code snippet separately, and then generates multiple enhanced rewritten codes of the original query statement and code snippet through a large model. After encoding these rewritten codes, they are aggregated through average pooling. The generation capability of the large model is used to enhance the search capability of the code pre-training model in multiple aspects, achieving more accurate code search without fine-tuning training.

[0017] (2) The present invention smoothes the enhanced similarity based on the change in similarity before and after enhancement, taking into account the relative change in similarity and possible semantic deviation brought about by the enhanced content generated by the large model, thereby reducing the noise introduced by the large model's own preferences.

[0018] (3) This paper proposes a two-stage search strategy based on large model re-ranking, which uses the large model to finely rank the top k codes of the initial search results. This is simple but efficient, and greatly improves the model's ability to distinguish subtle differences in codes.

[0019] (4) Compared with the existing large model enhancement method and non-enhancement method, the zero-sample search method proposed in this paper achieves better accuracy in zero-sample code search tasks. BRIEF DESCRIPTION OF THE DRAWINGS

[0020] Figure 1 A flowchart of a zero-sample code search method based on large model enhanced optimization and reordering provided by one embodiment of the present invention; Figure 2 A schematic diagram of the logical structure of a zero-sample code search system based on large model enhanced optimization and reordering is provided in one embodiment of the present invention. DETAILED DESCRIPTION

[0021] The present invention will be further described in detail below by means of specific embodiments in conjunction with the accompanying drawings. Similar elements in different embodiments are numbered with associated similar elements. In the following embodiments, many detailed descriptions are provided to enable the present invention to be better understood. However, those skilled in the art will readily appreciate that some of the features may be omitted under different circumstances, or may be replaced by other elements, materials, or methods. In some cases, some operations related to the present invention are not shown or described in the specification. This is to avoid the core of the present invention being overwhelmed by excessive descriptions, and for those skilled in the art, it is not necessary to describe these related operations in detail. They can fully understand the related operations based on the description in the specification and the general technical knowledge in the art.

[0022] In addition, the features, operations, or characteristics described in the specification may be combined in any appropriate manner to form various embodiments. Furthermore, the steps or actions in the method description may be reordered or adjusted in a manner readily apparent to those skilled in the art. Therefore, the various sequences in the specification and drawings are provided solely for the purpose of clearly describing a particular embodiment and are not intended to be mandatory, unless otherwise specified.

[0023] The first embodiment of the present invention provides a zero-sample code search method based on large model enhancement optimization and reordering, which is combined with Figure 1 Provide detailed explanation.

[0024] The method architecture proposed in this embodiment mainly includes large model enhanced encoding, similarity smoothing search and large model Top-k re-ranking modules: (1) Large model enhanced encoding module: Encode the original query and code separately, then generate multiple enhanced rewritten codes of the original query and code through the large model, and then encode these rewritten codes and aggregate them through average pooling.

[0025] (2) Similarity smoothing search module: First, the enhanced similarity and the original similarity are calculated separately, and then the difference between the two is obtained. Different weights are assigned according to the positive or negative value of these differences and added to the enhanced similarity to obtain the final similarity, thereby obtaining the preliminary search results.

[0026] (3) Large model Top-k re-ranking module: prompts the large model to analyze and re-rank the first k codes of the preliminary search results to obtain the final search results.

[0027] like Figure 1As shown, in step S100, the original query statement and the original code snippet in the code library are obtained.

[0028] In this embodiment, the goal of the zero-sample code search task is to achieve accurate matching and retrieval of natural language queries and code snippets in the code library without the need for specific training samples.

[0029] like Figure 1 As shown, in step S200, enhanced codes of the original query statement and the original code snippet are generated respectively based on the large language model.

[0030] Existing searches that rely solely on embedding vectors of the original query and original code are affected by the semantic misalignment between the natural language and code modalities. This is because the original query and original code are different modalities and have different semantic expressions. Even if a query-code pair is clearly matched, the encoder may not be able to accurately represent it using a dense embedding vector due to the different external forms and semantic expressions. Furthermore, queries and code contain different amounts of information: queries are typically brief, while code contains more implementation details. A single query may only focus on one functional aspect of the code, while ignoring other aspects. Furthermore, both the query and code may contain various noises that interfere with their true intent. In these cases, the encoded embedding vectors will be less effective in search. This embodiment leverages the powerful code understanding and generation capabilities of the large model to enhance the encoding process. Code contains more implementation details, so converting the original query and code into code uniformly through the large model can, to a certain extent, bridge the gap between the modalities and further align their semantics.

[0031] Because the large model excels at code generation, the original query statement can be treated as a code description, prompting the large model to generate code that meets the description requirements. Multiple generation is employed, generating multiple implementations for the same query. This approach explicitly increases the diversity of generated content, exploring different implementation options and covering as many potential code features as possible, thereby increasing the probability of finding the target code.

[0032] Specifically, for the original query statement: Build-based prompt templates for generating code , generate enhanced code: ; in, Indicates that the original query statement The generated An enhancement code, Indicates the large model generation process, prompt template is , the number of enhanced code generation is , in this embodiment, the actual setting .

[0033] Similarly, for the original code snippet: First, based on the prompt template for summary generation , using a large language model to generate natural language summaries: ; in, represents the generated summary; Based on the built prompt template for generating code Generate enhanced code: ; in, Representation based on the original code snippet The generated An enhancement code, Indicates the large model generation process, prompt template is , the number of enhanced code generation is .

[0034] Thus, the embedding vector of the original query sentence is obtained , the embedding vector of the original code snippet , the embedding vector of the original query sentence corresponding to the enhanced code And the embedding vector of the original code snippet corresponding to the enhanced code .

[0035] Furthermore, in this embodiment, the processing process of the large language model includes: first using the DeepSeek-R1 model to generate a few-sample examples, then building an efficient prompt template based on the few-sample examples, and then inputting it into the DeepSeek-V3 model to generate the final result.

[0036] In this embodiment, the large language models used are the DeepSeek-V3 model and the DeepSeek-R1 model.

[0037] (1) DeepSeek-V3 is a powerful Mixture of Experts (MoE) language model, not an inference model. It has 671 billion total parameters, but only activates 37 billion parameters for each token. Based on the DeepSeekMoE architecture and combined with multi-head latent attention, this model has high efficiency advantages in training and inference performance. DeepSeek-V3 has designed an innovative load balancing strategy, characterized by no auxiliary loss strategy and the use of multi-token prediction training targets. DeepSeek-V3 has excellent code capabilities among open source models, and its inference speed and cost are better than DeepSeek-R1.

[0038] (2) DeepSeek-R1 is an open-source reasoning model based on the research and improvements of DeepSeek-V3. This model explores the practice of using reinforcement learning to improve reasoning capabilities. The key difference between DeepSeek-R1 and DeepSeek-V3 in generating responses is that it can think for a certain period of time beforehand, showing long thought chain behavior. The former shows expert-level performance in code-related tasks, but its response speed is relatively slow and the API call cost is higher.

[0039] Considering that the large models used by AOR are DeepSeek-R1 and DeepSeek-V3, their official APIs do not support generating multiple responses by inputting a single prompt. If this is achieved by repeated input, additional input token consumption will be incurred. Here, it is assumed that the average input token consumption of each prompt is , the average token consumption of outputting a single code is , then a single query generates The total number of tokens required for each code is In view of the above situation, this embodiment designs a token-efficient prompt, which explicitly requires the large model to output in one response There are some differences in the code, which avoids repeated calls and reduces the number of tokens consumed to , significantly optimizing the cost of calling large models, as follows: 1) In the specific design of large-model prompts, few-shot prompts can provide concrete examples for the large model to complete the task, improving the accuracy of generated content. Therefore, this embodiment first uses the large model to construct multiple query-code pairs as few-shot examples. The large model selected here is the DeepSeek-R1 model, which is more expensive and slower but has stronger reasoning capabilities. This is because this process is called infrequently and requires high-quality examples to guide subsequent code generation. During the formal code generation phase, the lower-cost and faster DeepSeek-V3 model is used. The few-shot examples are combined with the original query to form the input prompt, i.e., a token-efficient prompt. The few-shot construction and code generation stages have different characteristics: the former is small-scale and requires high-quality examples, while the latter is large-scale and requires efficient generation speed. Therefore, different large models are used to suit their respective characteristics.

[0040] 2) In the enhancement step of the original code snippet, the process of converting the code into a natural language summary also requires first generating a few-shot example through the DeepSeek-R1 model, then combining the few-shot example and the original code into an input prompt, and finally formally generating a summary through the DeepSeek-V3 model.

[0041] like Figure 1 As shown, in step S300, based on the code pre-training model, the original query statement and the original code snippet are respectively encoded to obtain corresponding embedding vectors, and the enhanced codes of the original query statement and the original code snippet are respectively encoded to obtain corresponding embedding vectors.

[0042] Specifically, the original query statement is , the original code snippet is , the encoding process is expressed as: ; ; in, represents the embedding vector of the original query sentence, Represents the embedding vector of the original code snippet, and Encoder represents the encoding process of the code pre-training model.

[0043] In this embodiment, after obtaining the enhanced codes generated by the large model, these enhanced codes are encoded by the same Encoder, and then aggregated and normalized by average pooling to obtain the corresponding embedding vectors.

[0044] Specifically, the generated multiple enhanced codes are first encoded through the code pre-training model, and then aggregated and normalized through average pooling to obtain the corresponding embedding vectors: ; ; in, Indicates the first Enhancement Code The embedding vector of Indicates the first Enhancement Code The embedding vector of Encoder represents the encoding process of the code pre-training model, and the number of enhanced code generation is , .

[0045] like Figure 1 As shown, in step S400, the similarity between the embedding vectors corresponding to the original query statement before enhancement and the original code snippet and the similarity between the embedding vectors corresponding to the enhanced code of the original query statement after enhancement and the original code snippet are calculated.

[0046] Specifically, suppose there are M original code snippets in the code base, record the The embedding vector of the original code snippet is , the embedding vector of the corresponding enhanced code is , ; ; ; in, Embedding vector representing the enhanced code corresponding to the original query With the The embedding vector of the original code snippet corresponding to the enhanced code The similarity of Embedding vector representing the original query sentence With the Embedding vectors of the original code snippets similarity; Due to coexistence codes, so we get The enhanced similarity and Similarity before enhancement: ; ; in, is the enhanced similarity vector, is the similarity vector before enhancement.

[0047] The enhanced similarities can better represent the degree of semantic matching between the query and the code as a whole, but they also have some problems. Compared with the similarity before enhancement, the enhanced similarity may show a large change, which is reflected in the fact that the similarity corresponding to some codes is excessively improved, but they are not actually the codes that best meet the query intent, and the similarity of the target code may be mistakenly reduced. This is because the content generated by the large model contains its own preferences and noise of irrelevant content, which deviates from the original semantics in these aspects. To correct this problem, this embodiment considers the enhanced similarity and the pre-enhancement similarity at the same time, and calculates the relative change between them.

[0048] like Figure 1 As shown, in step S500, the enhanced similarity is smoothed according to the change of similarity before and after enhancement to obtain a final similarity, and preliminary code search results are obtained according to the final similarity sorting.

[0049] Specifically, calculate the similarity difference before and after enhancement: ; Then, extract The positive and negative parts of : ; ; in, Represents a vector Each element in takes a larger value than 0; The final similarity vector after correction is defined as: ; in, and is a hyperparameter used to adjust the weights of the positive and negative parts, both of which are set to negative values. Used to reduce the degree of relative change to achieve smooth operation; Will As the basis of the search results, the values ​​of each element in the vector are sorted in descending order to obtain a preliminary search result list.

[0050] like Figure 1 As shown, in step S600, based on the large language model, the first k code snippets of the preliminary code search results are reordered according to the matching degree to obtain the optimal code search result.

[0051] Specifically, based on the constructed prompt template for reordering, the selected previous The semantics of each code and the degree of matching with the original query statement are used to output fine-grained ranking results.

[0052] Based on the above steps, a preliminary search result list is obtained, which includes Ideally, the model should place the target code first in the list. In reality, encoder-based code pre-training models have difficulty distinguishing between semantically very similar codes, inevitably ranking other similar non-target codes higher. This is difficult to completely overcome because the semantics of queries and codes are diverse and complex, and encoder models cannot fully capture all the nuances.

[0053] To alleviate this problem, this embodiment introduces a large model reordering strategy. Considering the high computational cost of reordering all codes, and after the optimization of the aforementioned large model enhanced encoding and similarity smoothing search, the preliminary search results have a certain degree of accuracy, and the target code is often placed in the front position. Therefore, this embodiment only selects the first Then use the big model to analyze this The algorithm considers the semantics of the code and its match with the query, outputting refined ranking results. This two-stage reranking strategy ensures search performance while reducing computational costs. The specific prompt still uses the few-shot prompting method, first prompting the DeepSeek-R1 model to generate a few-shot example following a fixed answer format. These examples are then combined with the actual samples to be reranked, and the DeepSeek-V3 model generates the final ranking result.

[0054] Furthermore, the method also includes: verifying the zero-sample code search method under different code base datasets and different code pre-training models, and the evaluation indicators include mean reciprocal rank (MRR) and top-k recall rate (R@k), where mean reciprocal rank is used to measure the average ranking of the target code in the retrieval list, and top-k recall rate is used to measure the proportion of queries with the target code in the top k search results.

[0055] Specifically, the experimental datasets used in this example include CoNaLa, APPS, MBPP, and MBJP. These four commonly used public datasets are widely used to evaluate code tasks such as code generation and code search. Since this application focuses on improving code search models in the zero-shot setting, only the test set data is used.

[0056] Evaluation metrics include Mean Reciprocal Rank (MRR) and Top-k Recall (R@k): ; ; in Indicates the The ranking of the target code of the query statement, Q is the total number of queries, is an indicator function if If the target code is the first in the search result list, it returns 1, otherwise it returns 0. R@1 measures the proportion of queries with the target code ranked first in the search result list. Compared with R@5 and R@10, it can better reflect the actual search accuracy, so R@1 is only used as a supplementary indicator besides MRR.

[0057] The pre-training model used in this embodiment is as follows, which can effectively evaluate the improvements provided by the method proposed in this embodiment: (1) Contriever is an unsupervised dense information retrieval model based on contrastive learning training, which does not require labeled data. It generates positive sample pairs by randomly cropping documents and expands negative samples using the MoCo framework to learn dense representations of documents and queries.

[0058] (2) CodeT5+ adopts an encoder-decoder architecture and improves code comprehension and generation capabilities through various pre-training tasks (such as cross-modal contrastive learning and causal language modeling).

[0059] (3) UniXcoder is a unified multimodal pre-training model that uses abstract syntax trees and code comments as input, is pre-trained through masked language modeling, unidirectional language modeling, and denoising objectives, and additionally utilizes two multimodal tasks to learn code representations.

[0060] (4) Based on UniXcoder, CoCoSoDa continues pre-training on the code search dataset using soft data augmentation and multimodal momentum contrastive learning, and achieves very competitive results.

[0061] All experiments were conducted on a 24GB NVIDIA GeForce RTX 4090 with CUDA version 12.4. The experimental code is based on the PyTorch framework, with a fixed random seed of 123456 and a learning rate of 2e-5. In addition, the similarity smoothing weights were adjusted based on the experimental results. Set to -0.35, = -1.0. Validation experiments were conducted on four pre-trained models and four datasets. Ablation experiments were also conducted to explore the contribution of the various modules included in the method of this embodiment (primarily the large-model enhanced encoding, similarity smoothing search, and large-model top-k reranking modules) to performance improvement. The experimental results show that the method of this embodiment achieves optimal performance on all four datasets, verifying its effectiveness. The experimental results on four datasets and four pre-trained models demonstrate the method's high efficiency and general applicability.

[0062] Corresponding to the above-disclosed zero-sample code search method based on large model enhancement optimization and reordering, the embodiment of the present invention also discloses a zero-sample code search system based on large model enhancement optimization and reordering, such as Figure 2 As shown, it specifically includes: The data preparation module is used to obtain the original query statements and original code snippets in the code library; An enhanced code generation module, configured to generate enhanced codes for the original query statement and the original code snippet based on a large language model; An encoding module, configured to encode the original query statement and the original code snippet based on a code pre-training model to obtain corresponding embedding vectors, and to encode the enhanced code of the original query statement and the original code snippet to obtain corresponding embedding vectors; A similarity calculation module is used to calculate the similarity of the embedding vectors corresponding to the original query statement and the original code snippet before enhancement, and the similarity of the embedding vectors corresponding to the enhanced code of the original query statement and the original code snippet after enhancement; a smoothing processing module, configured to smooth the enhanced similarity according to the change in similarity before and after enhancement to obtain a final similarity, and to obtain preliminary code search results according to the final similarity sorting; The reordering module is used to reorder the first k code snippets of the preliminary code search results according to the matching degree based on the large language model to obtain the optimal code search results.

[0063] It should be noted that for a detailed description of a zero-sample code search system based on large model enhanced optimization and reordering provided in an embodiment of the present invention, reference can be made to the relevant description of a zero-sample code search method based on large model enhanced optimization and reordering provided in an embodiment of the present invention, which will not be repeated here.

[0064] The above examples are used to illustrate the present invention, which are only used to help understand the present invention and are not intended to limit the present invention. Those skilled in the art can make several simple deductions, modifications or substitutions based on the concept of the present invention.

Claims

1. A zero-sample code search method based on large model enhanced optimization and reordering, characterized in that: The method comprises: Get the original query statement and the original code snippet in the code library; Based on the large language model, generating enhanced codes for the original query statement and the original code snippet respectively; Based on the code pre-training model, the original query statement and the original code snippet are respectively encoded to obtain corresponding embedding vectors, and the enhanced code of the original query statement and the original code snippet are respectively encoded to obtain corresponding embedding vectors; Calculate the similarity between the embedding vectors corresponding to the original query statement and the original code snippet before enhancement, and the similarity between the embedding vectors corresponding to the enhanced code of the original query statement and the original code snippet after enhancement; According to the change of similarity before and after enhancement, the enhanced similarity is smoothed to obtain the final similarity, and the preliminary code search results are obtained according to the final similarity sorting; Based on the large language model, the first k code snippets of the preliminary code search results are reordered according to the matching degree to obtain the optimal code search result.

2. The zero-sample code search method based on large model enhanced optimization and reordering according to claim 1, characterized in that: Based on the large language model, enhanced codes for the original query statement and the original code snippet are generated respectively, specifically including: For the original query: Build-based prompt templates for generating code , generate enhanced code: ; in, Indicates that the original query statement The generated An enhancement code, Indicates the large model generation process, prompt template is , the number of enhanced code generation is ; For the original code snippet: First, based on the prompt template for summary generation , using a large language model to generate natural language summaries: ; in, represents the generated summary; Based on the built prompt template for generating code Generate enhanced code: ; in, Representation based on the original code snippet The generated An enhancement code, Indicates the large model generation process, prompt template is , the number of enhanced code generation is .

3. The zero-sample code search method based on large model enhanced optimization and reordering according to claim 1, characterized in that: Based on the code pre-training model, the original query statement and the original code snippet are respectively encoded to obtain corresponding embedding vectors, specifically including: The original query statement is , the original code snippet is , the encoding process is expressed as: ; ; in, represents the embedding vector of the original query sentence, Represents the embedding vector of the original code snippet, and Encoder represents the encoding process of the code pre-training model.

4. The zero-sample code search method based on large model enhanced optimization and reordering according to claim 1, characterized in that: Encoding the original query statement and the enhanced code of the original code snippet to obtain corresponding embedding vectors, specifically including: The generated multiple enhanced codes are first encoded through the code pre-training model, and then aggregated and normalized through average pooling to obtain the corresponding embedding vectors: ; ; in, Indicates the first Enhancement Code The embedding vector of Indicates the first Enhancement Code The embedding vector of Encoder represents the encoding process of the code pre-training model, and the number of enhanced code generation is , .

5. The zero-sample code search method based on large model enhanced optimization and reordering according to claim 1, characterized in that: Calculating the similarity between the embedding vectors corresponding to the original query statement and the original code snippet before enhancement, and the similarity between the embedding vectors corresponding to the enhanced code of the original query statement and the original code snippet after enhancement, specifically includes: Assume that there are M original code snippets in the code base, record The embedding vector of the original code snippet is , the embedding vector of the corresponding enhanced code is , ; ; ; in, Embedding vector representing the enhanced code corresponding to the original query With the The embedding vector of the original code snippet corresponding to the enhanced code The similarity of Embedding vector representing the original query sentence With the Embedding vectors of the original code snippets similarity; Due to coexistence codes, so we get The enhanced similarity and Similarity before enhancement: ; ; in, is the enhanced similarity vector, is the similarity vector before enhancement.

6. The zero-sample code search method based on large model enhanced optimization and reordering according to claim 5, characterized in that: The enhanced similarity is smoothed according to the change in similarity before and after enhancement to obtain the final similarity. The preliminary code search results are obtained according to the final similarity sorting, including: Calculate the similarity difference before and after enhancement: ; Then, extract The positive and negative parts of : ; ; in, Represents a vector Each element in takes a larger value than 0; The final similarity vector after correction is defined as: ; in, and It is a hyperparameter, which is set to negative values ​​to adjust the weights of the positive and negative parts, weaken the relative changes, and thus achieve smoothing operations; Will As the basis of the search results, the values ​​of each element in the vector are sorted in descending order to obtain a preliminary search result list.

7. The zero-sample code search method based on large model enhanced optimization and reordering according to claim 1, characterized in that: Based on the large language model, the first k code snippets in the preliminary code search results are re-ranked according to the matching degree to obtain the optimal code search results, specifically including: Based on the constructed prompt template for reordering, the selected front matter is analyzed using a large language model. The semantics of each code and the degree of matching with the original query statement are used to output fine-grained ranking results.

8. The zero-sample code search method based on large model enhanced optimization and reordering according to claim 1, characterized in that: The method further comprises: The processing process of the large language model includes: first using the DeepSeek-R1 model to generate a few-shot examples, then building an efficient prompt template based on the few-shot examples, and then inputting it into the DeepSeek-V3 model to generate the final result.

9. The zero-sample code search method based on large model enhanced optimization and reordering according to claim 1, characterized in that: The method further comprises: The zero-shot code search method was verified under different code base datasets and different code pre-training models. The evaluation indicators included average reciprocal rank and Top-k recall. The average reciprocal rank was used to measure the average rank of the target code in the retrieval list, and the Top-k recall was used to measure the proportion of queries with the target code in the top k search results.

10. A zero-sample code search system based on large model enhanced optimization and reordering, characterized in that: The system comprises: The data preparation module is used to obtain the original query statements and original code snippets in the code library; An enhanced code generation module, configured to generate enhanced codes for the original query statement and the original code snippet based on a large language model; An encoding module, configured to encode the original query statement and the original code snippet based on a code pre-training model to obtain corresponding embedding vectors, and to encode the enhanced code of the original query statement and the original code snippet to obtain corresponding embedding vectors; A similarity calculation module is used to calculate the similarity of the embedding vectors corresponding to the original query statement and the original code snippet before enhancement, and the similarity of the embedding vectors corresponding to the enhanced code of the original query statement and the original code snippet after enhancement; a smoothing processing module, configured to smooth the enhanced similarity according to the change in similarity before and after enhancement to obtain a final similarity, and to obtain preliminary code search results according to the final similarity sorting; The reordering module is used to reorder the first k code snippets of the preliminary code search results according to the matching degree based on the large language model to obtain the optimal code search results.