Code retrieval methods, systems, equipment, and media for repository-level NELs

By constructing a NEL retrieval framework and utilizing an asymmetric dual encoder and a LoRA adapter, efficient retrieval across file edits is achieved by updating only the query encoder while keeping the code block encoder frozen. This solves the problems of cross-file editing and high-cost deployment in existing technologies, and improves accuracy and efficiency.

CN121636585BActive Publication Date: 2026-04-03UNIV OF SCI & TECH OF CHINA
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-02-03
Publication Date
2026-04-03

AI Technical Summary

Technical Problem

Existing Next Edit Suggestion (NES) techniques are limited to single-file editing and cannot meet the needs of cross-file editing in the real world. Furthermore, they require the entire codebase to be recoded and indexed during frequent updates, resulting in high storage and deployment costs.

Method used

By analyzing code commit history, a training dataset is constructed and a NEL retrieval framework with an asymmetric dual encoder and LoRA adapter is adopted. Only the query encoder is updated while the code block encoder is kept frozen, enabling efficient retrieval across file edits and reducing storage and deployment costs.

Benefits of technology

It improves the accuracy and efficiency of cross-file editing, reduces storage and deployment costs, solves the challenges of cross-file editing in existing technologies, and provides the first public benchmark focused on this task.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121636585B_ABST
    Figure CN121636585B_ABST
Patent Text Reader

Abstract

This invention discloses a code retrieval method, system, device, and medium for repository-level NEL (Network Elastic Compute Service) code retrieval. These are corresponding solutions. The solutions involve: automatically mining and verifying cross-file code editing dependencies by analyzing code commit history to form a NEL benchmark dataset; designing an NEL retrieval framework to address the needs of index reuse and efficient deployment. Specifically, a task-specific low-rank adaptive adapter is designed, employing an asymmetric dual encoder. During training, only partial parameters are used to update the query encoder while keeping the block encoder used for index construction frozen; subsequently, using the current edit content in the repository as the query, the most likely edited code block can be accurately located and returned, improving development efficiency. Furthermore, the solution provided by this invention eliminates the need for recoding and indexing the entire codebase, reducing storage and deployment costs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of code retrieval technology, and in particular to a code retrieval method, system, device and medium for repository-level NELs. Background Technology

[0002] With the rapid development of large language models (LLMs) for code, Next Edit Suggestion (NES) technology has been widely used in various development tools, such as Cursor and GitHub Copilot (both are integrated development environment tools for AI-assisted code writing and editing). NES aims to predict and automate a developer's next action by analyzing their historical editing behavior.

[0003] However, existing NES methods are typically limited to editing within a single file and cannot meet the cross-file requirements common in real-world software development. For example, in Figure 1 In the scenario shown, when a developer modifies the code in the core file oauth2.py, they usually have to update the corresponding schema definition in the test file test_tutorial003.py simultaneously. This is a typical cross-file editing scenario. Figure 1 In the scenario examples shown, the paths to the core files and test files, as well as the code content, are all examples. Furthermore, all the code symbols involved in the code content are in a general form, such as username for username and title for title, etc., which will not be elaborated here.

[0004] An intuitive approach to scaling this up is to feed the entire code repository as context into a large language model. However, this approach is not only costly and inefficient, but it can also exceed the context window limitations of large language models. To address this issue, a more practical approach is to design a repository-level NES workflow consisting of retrieval and generation phases. Figure 1 As shown, the developer's current action (code differences) is first used as a query and input into a retrieval tool. The retrieval tool identifies and retrieves the top k most likely code blocks to be edited. Then, the retrieved code blocks are provided as context to the generator to produce specific edit suggestions. In this workflow, the quality of the retrieved code blocks directly affects the quality of the final suggestions; this retrieval process is defined as the repository-level Next Edit Location (NEL) task.

[0005] Performing NEL tasks faces two major challenges:

[0006] (1) Data and evaluation gaps: How to construct effective training data and evaluation methods. NEL needs to capture complex cross-file dependencies and development workflow patterns, while existing code editing benchmarks are mainly focused on the single-file level and lack systematic collection and annotation of cross-file editing patterns.

[0007] (2) Real-world deployment constraints: In real-world development scenarios, NES-related models need to be updated frequently because developers' adoption or rejection behavior provides clear optimization signals. This frequent update cycle makes related indexing, storage, and deployment costs major issues.

[0008] In view of this, the present invention is hereby proposed. Summary of the Invention

[0009] The purpose of this invention is to provide a code retrieval method, system, device, and medium for repository-level NELs, which can meet the needs of cross-file editing in the real world and reduce storage and deployment costs.

[0010] The objective of this invention is achieved through the following technical solution:

[0011] A code retrieval method for repository-level NELs includes:

[0012] By analyzing code commit history and mining code editing dependencies, a training dataset and a NEL benchmark dataset are constructed. NEL represents the location of the next edit. In the NEL benchmark dataset, a single data pair contains a query and a positive sample. The query is the difference in the code, and the positive sample is the code block corresponding to the query.

[0013] A NEL retrieval framework is constructed using an asymmetric dual encoder and a retrieval unit, and trained using a training dataset. The asymmetric dual encoder consists of a query encoder and a code block encoder. The query encoder encodes the query to obtain a query vector, and the code block encoder encodes the positive samples corresponding to the query to obtain code block vectors. The retrieval unit calculates the similarity between the query vector and the code block vectors, and the calculated similarity is used to construct a contrastive learning loss. The query encoder is then optimized using a LoRA adapter, where the LoRA adapter is a low-rank adaptive adapter.

[0014] After training, the NEL benchmark dataset is used for evaluation. When the evaluation results meet the requirements, the code block encoder encodes and indexes each code block in the repository. The query encoder uses the differences between the code in the repository as queries and encodes them. The retrieval unit calculates the similarity between the query vector and the code block vectors in the repository, and retrieves the indexes of the top k code blocks with the highest similarity.

[0015] A code retrieval system for repository-level NELs, comprising:

[0016] The training dataset and the NEL benchmark dataset are used to analyze code commit history and mine code editing dependencies, thereby constructing the training dataset and the NEL benchmark dataset. NEL is the location of the next edit. In the NEL benchmark dataset, a single data pair contains a query and a positive sample. The query is the difference in the code, and the positive sample is the code block corresponding to the query.

[0017] The NEL retrieval framework construction and training unit is used to construct the NEL retrieval framework using an asymmetric dual encoder and a retrieval unit, and to train it using a training dataset. The asymmetric dual encoder includes a query encoder and a code block encoder. The query encoder encodes the query to obtain a query vector, and the code block encoder encodes the positive samples corresponding to the query to obtain a code block vector. The retrieval unit calculates the similarity between the query vector and the code block vector, and combines the calculated similarity to construct a contrastive learning loss. The query encoder is then optimized using a LoRA adapter, where the LoRA adapter is a low-rank adaptive adapter.

[0018] The evaluation and code retrieval unit is used to evaluate the code using the NEL benchmark dataset after training. When the evaluation results meet the requirements, the code block encoder encodes and indexes each code block in the repository. The query encoder uses the differences in the code in the repository as queries and encodes them. The retrieval unit calculates the similarity between the query vector and the code block vectors in the repository and retrieves the indexes of the top k code blocks with the highest similarity.

[0019] A processing device includes: one or more processors; and a memory for storing one or more programs;

[0020] When the one or more programs are executed by the one or more processors, the one or more processors implement the aforementioned method.

[0021] A readable storage medium storing a computer program that, when executed by a processor, implements the aforementioned method.

[0022] As can be seen from the technical solution provided by this invention, to address the problem of missing task data, the system analyzes code submission history to automatically mine and verify cross-file code editing dependencies, forming a dataset. To address the needs of index reuse and efficient deployment, a NEL retrieval framework is designed. Specifically, a task-specific LoRA (Low-rank Adaptation) adapter is designed, employing an asymmetric dual encoder. During training, only a subset of parameters are used to update the query encoder, while the block encoder used to build the index remains frozen. Then, based on the current edited content in the repository (i.e., code differences) as the query, the system can accurately locate and return the most likely edited code block, improving development efficiency. Furthermore, the solution provided by this invention does not require recoding and indexing the entire codebase, reducing storage and deployment costs. Attached Figure Description

[0023] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the following description of the embodiments will be briefly introduced. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0024] Figure 1 A schematic diagram illustrating a scenario of an existing NES method provided as background information for this invention.

[0025] Figure 2 A flowchart of a code retrieval method for repository-level NELs provided in an embodiment of the present invention.

[0026] Figure 3 This is a schematic diagram illustrating the NEL benchmark dataset construction process provided in an embodiment of the present invention.

[0027] Figure 4 A schematic diagram of the NEL retrieval framework and its training scheme provided in an embodiment of the present invention.

[0028] Figure 5 This is a schematic diagram of a code retrieval system for repository-level NELs provided in an embodiment of the present invention.

[0029] Figure 6 This is a schematic diagram of a processing device provided in an embodiment of the present invention. Detailed Implementation

[0030] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the protection scope of the present invention.

[0031] First, the following explanations are provided for the terms that may be used in this article:

[0032] The term "and / or" means that either or both can be achieved simultaneously. For example, X and / or Y means that it includes both "X" or "Y" as well as the three cases of "X and Y".

[0033] The terms "comprising," "including," "containing," "having," or other similar semantic descriptions should be interpreted as non-exclusive inclusion. For example, including a technical feature element (such as raw material, component, ingredient, carrier, dosage form, material, size, part, component, mechanism, device, step, process, method, reaction conditions, processing conditions, parameter, algorithm, signal, data, product or article of manufacture, etc.) should be interpreted as including not only the expressly listed technical feature element, but also other technical feature elements that are not expressly listed and are well-known in the art.

[0034] The term "composed of" excludes any technical features not expressly listed. When used in a claim, it closes the claim to exclude all technical features other than those expressly listed, except for associated conventional impurities. If the term appears only in a clause of a claim, it limits the claim to the elements expressly listed in that clause; elements recited in other clauses are not excluded from the overall claim.

[0035] The following provides a detailed description of a code retrieval method, system, device, and medium for repository-level NELs provided by this invention. Contents not described in detail in the embodiments of this invention are prior art known to those skilled in the art. Where specific conditions are not specified in the embodiments of this invention, they are performed according to conventional conditions in the art or conditions recommended by the manufacturer. Where the manufacturers of the instruments used in the embodiments of this invention are not specified, they are all conventional products that can be purchased commercially.

[0036] Example 1

[0037] like Figure 2 The diagram shows a flowchart of a code retrieval method for repository-level NELs, which mainly includes the following steps:

[0038] Step 1: Construct the training dataset and the NEL benchmark dataset.

[0039] In this embodiment of the invention, code submission history is analyzed to mine code editing dependencies and obtain the NEL benchmark dataset; wherein, NEL is the location of the next edit, and a single data pair in the NEL benchmark dataset contains a query and a positive sample, the query being the difference in code, and the positive sample being the code block corresponding to the query.

[0040] The preferred implementation method for this step is as follows:

[0041] (1) Analyze the code commit history and filter out the code commit history with the number of code differences within a set range; denote the differences of the code as diff; for each code commit history that is filtered out, analyze the relationship between different diffs and construct a dependency graph; wherein, the same code commit history contains modifications to several code files, each code file contains several diffs, the nodes in the dependency graph are diffs, and there is a dependency edge between diffs that have a dependency relationship.

[0042] (2) Traverse the dependency graph and convert each cross-file dependency edge into a query positive sample pair to form the initial dataset; wherein, the dependency edge points from the source node to the target node, the content of the diff corresponding to the source node is used as the query, and the code block before the diff is edited corresponding to the target node is used as the positive sample; the number of positive samples corresponding to a query is one or more.

[0043] (3) Divide the initial dataset into a training dataset and a test dataset, filter the test dataset, and obtain the NEL benchmark dataset.

[0044] In this embodiment of the invention, each query-positive sample pair in the training dataset contains one positive sample corresponding to the query. When there are multiple positive samples corresponding to each query, the corresponding query-positive sample pair will be split into multiple pairs. The number of positive samples corresponding to a query in the NEL benchmark dataset is one or more.

[0045] Step 2: Build and train the NEL retrieval framework.

[0046] In this embodiment of the invention, an NEL retrieval framework is constructed using an asymmetric dual encoder and a retrieval unit, and trained using a training dataset. The asymmetric dual encoder includes a query encoder and a code block encoder. The query encoder encodes the query to obtain a query vector, and the code block encoder encodes the positive samples corresponding to the query to obtain a code block vector. The retrieval unit calculates the similarity between the query vector and the code block vector, and the calculated similarity is used to construct a contrastive learning loss. The query encoder is then optimized using a LoRA adapter. The LoRA adapter is a low-rank adaptive adapter.

[0047] In this embodiment of the invention, the similarity between the query vector and the code block vector is calculated by the retrieval tool, and the contrastive learning loss is constructed by combining the calculated similarity. This includes: in a training batch of size N, given a set of data pairs... , will query The query vector is represented as , This represents a query encoder, where f(.) is the pooling and normalization function, which encodes the query... Corresponding positive samples The code block vector is represented as , This indicates a code block encoder.

[0048] For query vectors Its corresponding code block vector As a positive example, all other code block vectors in the same batch As a negative example, by calculating the similarity and constructing the contrastive learning loss, it is expressed as:

[0049] ;

[0050] in, To compare learning loss, This is a similarity measurement function.

[0051] In this embodiment of the invention, the optimization of the query encoder using the LoRA adapter includes: during training, optimizing the LoRA adapter parameters based on the loss function. Optimization is performed by applying the optimized LoRA adapter to the self-attention module of the query encoder, and fine-tuning is achieved by optimizing the weight parameters of the self-attention module.

[0052] Step 3: Evaluation and code retrieval of repository-level NELs.

[0053] After training, the NEL benchmark dataset is used for evaluation. When the evaluation results meet the requirements, the code block encoder encodes each code block in the repository and builds an index. The query encoder uses the differences in the code in the repository as queries and encodes them. The retrieval unit calculates the similarity between the query vector and the code block vectors in the repository and retrieves the indexes of the top k code blocks with the highest similarity.

[0054] The solution provided by the embodiments of this invention significantly improves the accuracy of locating cross-file editing contexts. Its greatest advantage lies in maintaining the static nature of the code index, allowing the model to be updated and iterated without recoding the entire codebase. This enables the invention to share a unified index across multiple different code retrieval tasks, greatly reducing storage and computational overhead in actual deployments and solving industry deployment challenges. Furthermore, this invention provides the first publicly available benchmark focused on this task, promoting research in related fields.

[0055] To more clearly demonstrate the technical solution and its effects provided by the present invention, the method provided by the embodiments of the present invention will be described in detail below with reference to specific examples.

[0056] I. Overall Overview of the Plan

[0057] This invention provides a code retrieval method for repository-level NELs to address the following technical problems: overcoming the limitation of existing next-edit suggestions technology, which is limited to single-file editing and cannot support cross-file editing needs in the real world; and simultaneously solving the problem that existing code retrieval models require recoding and indexing of the entire codebase during frequent updates and iterations, resulting in high storage and deployment costs. In practical applications, a codebase index is often already created by other code retrieval tasks (such as Text2Code, Code2Code, etc.). This invention aims to enable the NEL retrieval engine to reuse shared codebase indexes, and its own updates should not cause any changes to the index. Text2Code uses natural language to retrieve code, while Code2Code uses code snippets to retrieve similar code snippets.

[0058] The specific technical implementation route of this invention is as follows:

[0059] (1) Construction of NEL benchmark dataset.

[0060] To effectively evaluate repository-level edit point location capabilities, this invention first constructs a high-quality evaluation benchmark. This benchmark analyzes code commit history, automatically mines and manually verifies cross-file code edit dependencies, forming a dataset for model training and evaluation.

[0061] (2) NEL retrieval framework design.

[0062] This invention proposes a novel retrieval framework. The core feature of this framework is its asymmetric design: it comprises a query encoder for understanding the user's current edit (query) and a block encoder for encoding and indexing the codebase. During training, only the query encoder is updated, while the block encoder remains frozen.

[0063] (3) Training program.

[0064] This invention employs a contrastive learning approach for training. The training objective is to enable the NEL retrieval framework to spatially narrow the vector representation of the user's current edited content to the vector representation of the actual "next edit location," while simultaneously distancing it from other irrelevant code.

[0065] (3) Application scheme.

[0066] In practical applications, the entire code repository only needs to be encoded and indexed once using a frozen code block encoder. When a user makes an edit, the updated query encoder is used to encode the query and efficiently retrieve it from the static index to find the most relevant next edit location.

[0067] II. Detailed introduction of the plan.

[0068] 1. Problem definition and formalization.

[0069] This invention formally defines the warehouse-level next edit point location task as follows:

[0070] Given a code repository R, ​​it can be represented as a set of code blocks. Each 'c' represents a code block, and M is the number of code blocks. Given a query 'q', which represents the user's current edit, typically represented as a code diff.

[0071] The objective of this invention is to train a retrieval model (i.e., the NEL retrieval framework described below) that can retrieve one or more target code blocks, which constitute a positive sample set. , This is the location where the next edit is most likely to occur. The model needs to learn to... The code block in the code is ranked highly, so that it ranks as high as possible among all candidate code blocks in the repository.

[0072] 2. Construct the training dataset and the NEL benchmark dataset.

[0073] To systematically evaluate the capabilities of NEL retrieval frameworks on NEL tasks, this invention constructs a new evaluation benchmark. This dataset is derived from real-world development activities in popular open-source projects; for example, it may also cover four programming languages ​​(Python, Java, C++, Rust) and 18 representative repositories.

[0074] The data construction process of this invention follows a strict three-stage process, such as... Figure 3 As shown.

[0075] (1) Stage I: Dependency graph generation.

[0076] This invention first analyzes code commit history from the code repository. To focus on meaningful cross-file edits, this invention filters code commit history containing 2 to 10 diff files. For each qualifying code commit history, this invention uses a large language model (LLM) to analyze the relationships between diffs and generate a dependency graph connecting the related diffs.

[0077] Those skilled in the art will understand that each code commit history contains modifications to several code files, and each code file contains several diffs. Therefore, a corresponding dependency graph is built for each code commit history, and the nodes in the dependency graph are diffs. There is a dependency edge between diffs that have a dependency relationship.

[0078] (2) Phase II: Data Construction.

[0079] This invention traverses the generated dependency graph, and identifies each cross-file dependency edge in the graph (in...). Figure 3 The text (indicated by bold arrows) is transformed into a (query-positive sample) pair. Specifically:

[0080] Query: In the dependent edge, the diff content of the source node is used as the query q.

[0081] Positive samples: In dependency edges, the code block content of the target node before editing is used as a positive sample. A query It may correspond to multiple positive samples For example, through this process, the present invention constructed approximately 26,000 positive query sample pairs, forming the initial dataset.

[0082] The initial training data is divided into a training dataset and a test dataset. The training dataset is used for subsequent training processes, while the test dataset is processed into the NEL benchmark dataset through Phase III and used for subsequent evaluation.

[0083] (3) Stage III: Quality filtration.

[0084] The automatically generated (query-positive sample) pairs contain noise. To ensure data quality, this invention designs a filtering process that includes LLM filtering and manual annotation.

[0085] LLM Filtering: This invention uses a powerful LLM (e.g., Qwen3-235B-A22B) to perform initial screening of automatically generated sample pairs. This LLM is responsible for evaluating whether the edits represented by each sample pair are logically relevant. Here, Qwen is the model name (i.e., "Qianwen"), 3 is the version number, 235B is the total number of model parameters, A22B is the number of activation parameters, and B represents billions. The LLM involved in different steps of this invention can be the same or different LLMs; the specific choice is determined by the user based on actual circumstances, and this invention does not impose any restrictions.

[0086] Manual annotation: After LLM screening, this invention extracts multiple (e.g., 50) instances for each repository as a test set and submits them to multiple (e.g., three) experts (e.g., experienced software engineers) for final review. Experts judge acceptance or rejection based on a series of criteria. This invention employs multi-round annotation and majority voting to ensure annotation consistency. For example, the annotation criteria are as follows:

[0087] (3.1) Synchronization of implementation and testing: Determine whether the code editing has synchronously updated the implementation code (such as function bodies and class methods) and its corresponding test code (such as unit test cases). When the core business logic or algorithm changes, the test cases used to verify its correctness must also be modified accordingly to ensure the effectiveness and coverage of the tests.

[0088] (3.2) Synchronization between interface and implementation: Pay attention to the consistency between the declaration definition of a function or class (i.e., the "interface", such as function signature and parameter list) and its specific implementation code. When the interface definition changes (e.g., adding or deleting parameters or modifying the type), all code that implements the interface or calls the function needs to be adjusted accordingly to match the new interface constraints.

[0089] (3.3) Synchronization of Configuration and Feature Updates: Identify configuration changes that occur with the development of new features or updates to existing features. Systems typically rely on configuration files (such as YAML, JSON, etc.) or build scripts to manage their behavior. When features are introduced or modified, these configuration files need to be modified synchronously to enable, disable, or adjust the new features.

[0090] (3.4) Code refactoring: Marks edits that aim to improve the internal quality of the code (such as readability and maintainability) without changing its external functionality. Typical refactoring operations include function extraction (extracting complex logic into new functions), function migration (moving function positions), variable renaming, or splitting and merging classes / modules.

[0091] (3.5) Synchronization of Data Model and View / Interface: Pay attention to whether the front-end user interface or application programming interface is updated accordingly when the back-end data structure (such as database schema) changes. For example, when a new field is added to a database table, the relevant interface response definition and front-end display page also need to be modified to correctly process and display the new data.

[0092] (3.6) Synchronization of Documentation and Code: Determine whether changes to code logic are accompanied by corresponding documentation updates. This includes comments within the code (such as function docstrings), project documentation (such as README files), or dedicated interface documentation. The goal is to ensure that the documentation accurately reflects the latest code logic and usage methods.

[0093] (3.6) Adaptation for dependency library updates: Identify code modifications made to ensure compatibility with version updates of external dependency libraries (third-party libraries, frameworks). When a new version of a dependency library is released, especially if it contains incompatible updates, the project's own code needs to be adapted (e.g., calling new interfaces, modifying deprecated usages) to ensure that the program can compile and run correctly.

[0094] (3.7) Synchronous modification of cloned code: Consistent modifications to duplicate or "cloned" code (i.e., code with similar implementation and functionality) existing in the codebase. When a developer fixes a defect that occurs repeatedly in multiple places, or optimizes a piece of copied logic, all relevant clone instances should be found and the same changes applied.

[0095] 3. NEL search framework.

[0096] (1) NEL retrieval framework architecture.

[0097] The core objective of this invention is to keep the code block index statically unchanged when the model is iteratively updated due to a task (such as the NEL task).

[0098] To achieve this goal, the present invention employs a searcher, and as... Figure 4 The diagram illustrates an asymmetric dual encoder architecture, incorporating a task-specific LoRA adapter. The core idea of ​​LoRA is to freeze the original weight matrix of the pre-trained model. And inject an increment matrix consisting of a product of low-rank matrices. .

[0099] In this embodiment of the invention, the query encoder and the code block encoder can adopt the same model structure, for example, both adopt the Transformer (transformer neural network) architecture, wherein the trapezoidal structure in the query encoder is an additional LoRA adapter.

[0100] Those skilled in the art will understand that the Transformer architecture mainly includes an Embedding layer, a Transformer Block layer, a Self-attention module, and an FFN (Feedforward Neural Network) module. The Self-attention module contains four parameter matrices (i.e., query projection weight matrix, key projection weight matrix, value projection weight matrix, and output projection weight matrix). LoRA operates on the four parameter matrices of the query encoder. Referencing the original weight matrix of each parameter matrix, calculate Then, it is added to the original weight matrix to update the weight matrix: , where W is the updated weight matrix.

[0101] In this embodiment of the invention, the asymmetric dual encoder architecture includes a query encoder. With code block encoder Among them: code block encoder Used to encode positive samples during training, and after training, to encode all code blocks in the repository to build vector indices. The code block encoder is used in all tasks and all model iterations. The weights are always kept frozen. The query encoder is used to encode queries during training.

[0102] To balance computational efficiency and task specificity, this invention provides... A task-specific LoRA adapter has been introduced. That is, for the specific task NEL... This invention will be associated with a separate LoRA adapter. When training the NEL retrieval framework, only LoRA adapter parameters It will be updated, and The base weights (i.e., the weight matrices other than the four parameter matrices) and All weights remain unchanged.

[0103] (2) Training program.

[0104] This invention employs InfoNCE loss (a contrastive learning loss function used to learn high-quality feature representations in unlabeled data by distinguishing positive samples from multiple negative samples) for contrastive learning. In a training batch of size N, given a set of training pairs... ,in It is the first One query, It is its corresponding positive sample (the actual next editing position).

[0105] Use the query encoder and the code block encoder to obtain their vector representations respectively:

[0106] ;

[0107] ;

[0108] Where f(.) is the pooling and normalization function.

[0109] For query vectors Its corresponding code block vector This is a positive example, while all other code block vectors in the batch... All are considered negative examples. The contrastive learning loss is defined as:

[0110] ;

[0111] in, , Represents the query vector and code block vectors Cosine similarity between them This is a temperature hyperparameter, typically ranging from 0.01 to 1.0; in this embodiment, it can be set to 1.0. The model minimizes the objective loss function. To conduct training.

[0112] (3) Application scheme.

[0113] After training, the NEL benchmark dataset is used for evaluation. Once the performance meets the standards, it can be used for actual deployment. Specifically, the user's current operation (code differences) is taken as a query. The code block that is similar to the query is found in the indexed codebase and returned to the generator. The generator then generates the corresponding edited content, improving the user's development efficiency.

[0114] Through the above description of the embodiments, those skilled in the art can clearly understand that the above embodiments can be implemented by software, or by using software plus necessary general-purpose hardware platforms. Based on this understanding, the technical solutions of the above embodiments can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, mobile hard drive, etc.), including several instructions to cause a computer device (such as a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments of the present invention.

[0115] Example 2

[0116] This invention also provides a code retrieval system for repository-level NELs, which is mainly used to implement the methods provided in the foregoing embodiments, such as... Figure 5 As shown, the system mainly includes:

[0117] The training dataset and the NEL benchmark dataset are used to analyze code commit history and mine code editing dependencies, thereby constructing the training dataset and the NEL benchmark dataset. NEL is the location of the next edit. In the NEL benchmark dataset, a single data pair contains a query and a positive sample. The query is the difference in the code, and the positive sample is the code block corresponding to the query.

[0118] The NEL retrieval framework construction and training unit is used to construct the NEL retrieval framework using an asymmetric dual encoder and a retrieval unit, and to train it using a training dataset. The asymmetric dual encoder includes a query encoder and a code block encoder. The query encoder encodes the query to obtain a query vector, and the code block encoder encodes the positive samples corresponding to the query to obtain a code block vector. The retrieval unit calculates the similarity between the query vector and the code block vector, and combines the calculated similarity to construct a contrastive learning loss. The query encoder is then optimized using a LoRA adapter, where the LoRA adapter is a low-rank adaptive adapter.

[0119] The evaluation and code retrieval unit is used to evaluate the code using the NEL benchmark dataset after training. When the evaluation results meet the requirements, the code block encoder encodes and indexes each code block in the repository. The query encoder uses the differences in the code in the repository as queries and encodes them. The retrieval unit calculates the similarity between the query vector and the code block vectors in the repository and retrieves the indexes of the top k code blocks with the highest similarity.

[0120] In this embodiment of the invention, the analysis of code submission history and the mining of code editing dependencies to construct a training dataset and a NEL benchmark dataset includes: analyzing code submission history and filtering out code submission history with a number of code differences within a set range; denoting the code differences as diffs; for each filtered code submission history, analyzing the relationships between different diffs and constructing a dependency graph, wherein the same code submission history contains modifications to several code files, each code file contains several diffs, the nodes in the dependency graph are diffs, and diffs with dependencies have a dependency edge; traversing the dependency graph, converting each cross-file dependency edge into a query positive sample pair to form the initial data; wherein the dependency edge points from the source node to the target node, the content of the diff corresponding to the source node is used as the query, and the code block before editing the diff corresponding to the target node is used as the positive sample; wherein the number of positive samples corresponding to a query is one or more; dividing the initial dataset into a training dataset and a test dataset, filtering the test dataset to obtain the NEL benchmark dataset.

[0121] In this embodiment of the invention, the step of calculating the similarity between the query vector and the code block vector using a retrieval device, and constructing a contrastive learning loss based on the calculated similarity, includes: in a training batch of size N, given a set of data pairs... , will query The query vector is represented as , This represents a query encoder, where f(.) is the pooling and normalization function, which encodes the query... Corresponding positive samples The code block vector is represented as , This indicates a block encoder; for query vectors Its corresponding code block vector As a positive example, all other code block vectors in the same batch As a negative example, by calculating the similarity and constructing the contrastive learning loss, it is expressed as:

[0122] ;

[0123] in, To compare learning loss, This is a similarity measurement function.

[0124] In this embodiment of the invention, the optimization of the query encoder using the LoRA adapter includes: during training, optimizing the LoRA adapter parameters based on the loss function. Optimization is performed by applying the optimized LoRA adapter to the self-attention module of the query encoder, and fine-tuning is achieved by optimizing the weight parameters of the self-attention module.

[0125] Those skilled in the art will understand that, for the sake of convenience and brevity, the above-described division of functional modules is used as an example. In practical applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the system can be divided into different functional modules to complete all or part of the functions described above.

[0126] Example 3

[0127] The present invention also provides a processing device, such as Figure 6 As shown, it mainly includes: one or more processors; a memory for storing one or more programs; wherein, when the one or more programs are executed by the one or more processors, the one or more processors implement the method provided in the foregoing embodiments.

[0128] Furthermore, the processing device also includes at least one input device and at least one output device; in the processing device, the processor, memory, input device, and output device are connected via a bus.

[0129] In this embodiment of the invention, the specific types of the memory, input device, and output device are not limited; for example:

[0130] Input devices can be touchscreens, image acquisition devices, physical buttons, or mice, etc.

[0131] The output device can be a display terminal;

[0132] The memory can be random access memory (RAM) or non-volatile memory, such as disk storage.

[0133] Example 4

[0134] The present invention also provides a readable storage medium storing a computer program that, when executed by a processor, implements the method provided in the foregoing embodiments.

[0135] In this embodiment of the invention, the readable storage medium is a computer-readable storage medium and can be disposed in the aforementioned processing device, for example, as a memory in the processing device. Furthermore, the readable storage medium can also be any medium capable of storing program code, such as a USB flash drive, portable hard drive, read-only memory (ROM), magnetic disk, or optical disk.

[0136] The above description is merely a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims. The information disclosed in the background section is intended only to enhance the understanding of the overall background technology of the present invention and should not be construed as an admission or implication in any way that such information constitutes prior art known to those skilled in the art.

Claims

1. A code retrieval method for repository-level NELs, characterized in that, include: By analyzing code commit history and mining code editing dependencies, a training dataset and a NEL benchmark dataset are constructed. NEL represents the location of the next edit. In the NEL benchmark dataset, a single data pair contains a query and a positive sample. The query is the difference in the code, and the positive sample is the code block corresponding to the query. A NEL retrieval framework is constructed using an asymmetric dual encoder and a retrieval unit, and trained using a training dataset. The asymmetric dual encoder consists of a query encoder and a code block encoder. The query encoder encodes the query to obtain a query vector, and the code block encoder encodes the positive samples corresponding to the query to obtain code block vectors. The retrieval unit calculates the similarity between the query vector and the code block vectors, and the calculated similarity is used to construct a contrastive learning loss. The query encoder is then optimized using a LoRA adapter, where the LoRA adapter is a low-rank adaptive adapter. After training, the NEL benchmark dataset is used for evaluation. When the evaluation results meet the requirements, the code block encoder encodes each code block in the repository and builds an index. The query encoder uses the differences in the code in the repository as queries and encodes them. The retrieval unit calculates the similarity between the query vector and the code block vectors in the repository and retrieves the indexes of the top k code blocks with the highest similarity.

2. The code retrieval method for repository-level NELs according to claim 1, characterized in that, The analysis of code commit history and the mining of code editing dependencies are used to construct the training dataset and the NEL benchmark dataset, which include: Analyze the code commit history and filter out code commit history with the number of code differences within a set range; denote the differences of the code as diffs; for each code commit history that is filtered out, analyze the relationship between different diffs and construct a dependency graph: the same code commit history contains modifications to several code files, each code file contains several diffs, the nodes in the dependency graph are diffs, and there is a dependency edge between diffs that have a dependency relationship. Traverse the dependency graph and convert each cross-file dependency edge into a query positive sample pair to form the initial dataset. The dependency edge points from the source node to the target node. The content of the diff corresponding to the source node is used as the query, and the code block before the diff is edited corresponding to the target node is used as the positive sample. The number of positive samples corresponding to a query can be one or more. The initial dataset is divided into a training dataset and a test dataset. The test dataset is then filtered to obtain the NEL benchmark dataset.

3. The code retrieval method for repository-level NELs according to claim 1, characterized in that, The step of calculating the similarity between the query vector and the code block vector using a retrieval tool, and constructing a contrastive learning loss based on the calculated similarity, includes: In a training batch of size N, given a set of data pairs , will query The query vector is represented as , This represents a query encoder, where f(.) is the pooling and normalization function, which encodes the query... Corresponding positive samples The code block vector is represented as , Indicates a block encoder; For query vectors Its corresponding code block vector As a positive example, all other code block vectors in the same batch As a negative example, by calculating the similarity and constructing the contrastive learning loss, it is expressed as: ; in, To compare learning loss, This is a similarity measurement function.

4. The code retrieval method for repository-level NELs according to claim 1, characterized in that, The optimization of the query encoder by combining the LoRA adapter includes: during training, optimizing the LoRA adapter parameters based on the loss function, applying the optimized LoRA adapter to the self-attention module of the query encoder, and fine-tuning by optimizing the weight parameters of the self-attention module.

5. A code retrieval system for repository-level NELs, characterized in that, include: The training dataset and the NEL benchmark dataset are used to analyze code commit history and mine code editing dependencies, thereby constructing the training dataset and the NEL benchmark dataset. NEL is the location of the next edit. In the NEL benchmark dataset, a single data pair contains a query and a positive sample. The query is the difference in the code, and the positive sample is the code block corresponding to the query. The NEL retrieval framework construction and training unit is used to construct the NEL retrieval framework using an asymmetric dual encoder and a retrieval unit, and to train it using a training dataset. The asymmetric dual encoder includes a query encoder and a code block encoder. The query encoder encodes the query to obtain a query vector, and the code block encoder encodes the positive samples corresponding to the query to obtain a code block vector. The retrieval unit calculates the similarity between the query vector and the code block vector, and combines the calculated similarity to construct a contrastive learning loss. The query encoder is then optimized using a LoRA adapter, where the LoRA adapter is a low-rank adaptive adapter. The evaluation and code retrieval unit is used to evaluate the code using the NEL benchmark dataset after training. When the evaluation results meet the requirements, the code block encoder encodes and indexes each code block in the repository. The query encoder uses the differences in the code in the repository as queries and encodes them. The retrieval unit calculates the similarity between the query vector and the code block vectors in the repository and retrieves the indexes of the top k code blocks with the highest similarity.

6. A code retrieval system for repository-level NELs according to claim 5, characterized in that, The analysis of code commit history and the mining of code editing dependencies are used to construct the training dataset and the NEL benchmark dataset, which include: Analyze the code commit history and filter out code commit history with the number of code differences within a set range; denote the differences of the code as diffs; for each code commit history that is filtered out, analyze the relationship between different diffs and construct a dependency graph: the same code commit history contains modifications to several code files, each code file contains several diffs, the nodes in the dependency graph are diffs, and there is a dependency edge between diffs that have a dependency relationship. Traverse the dependency graph and convert each cross-file dependency edge into a query positive sample pair to form the initial dataset. The dependency edge points from the source node to the target node. The content of the diff corresponding to the source node is used as the query, and the code block before the diff is edited corresponding to the target node is used as the positive sample. The number of positive samples corresponding to a query can be one or more. The initial dataset is divided into a training dataset and a test dataset. The test dataset is then filtered to obtain the NEL benchmark dataset.

7. A code retrieval system for repository-level NELs according to claim 5, characterized in that, The step of calculating the similarity between the query vector and the code block vector using a retrieval tool, and constructing a contrastive learning loss based on the calculated similarity, includes: In a training batch of size N, given a set of data pairs , will query The query vector is represented as , This represents a query encoder, where f(.) is the pooling and normalization function, which encodes the query... Corresponding positive samples The code block vector is represented as , Indicates a block encoder; For query vectors Its corresponding code block vector As a positive example, all other code block vectors in the same batch As a negative example, by calculating the similarity and constructing the contrastive learning loss, it is expressed as: ; in, To compare learning loss, This is a similarity measurement function.

8. A code retrieval system for repository-level NELs according to claim 5, characterized in that, The optimization of the query encoder by combining the LoRA adapter includes: during training, optimizing the LoRA adapter parameters based on the loss function, applying the optimized LoRA adapter to the self-attention module of the query encoder, and fine-tuning by optimizing the weight parameters of the self-attention module.

9. A processing device, characterized in that, include: One or more processors; Memory, used to store one or more programs; Wherein, when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement the method as described in any one of claims 1 to 4.

10. A readable storage medium storing a computer program, characterized in that, When a computer program is executed by a processor, it implements the method as described in any one of claims 1 to 4.

Citation Information

Patent Citations

  • Code generation method based on graph alignment coding large model and multi-agent collaboration

    CN120508285A

  • Unit test method for generating discrimination model based on retrieval

    CN120973684A