A method and apparatus for generating warehouse-level code

CN123011210APending Publication Date: 2026-10-30BEIHANG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610886042.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-06-18
Publication Date
2026-10-30

AI Technical Summary

Technical Problem

[0003]本发明提供了一种生成仓库级代码的方法及装置,以解决现有不能高效/快速且高质量地生成仓库级代码的问题

Benefits of technology

本发明是在仓库级代码生成过程中,从候选上下文中高效筛选出真正有效的代码片段,在提升生成质量的同时显著降低计算开销与推理时间,从而高效/快读且高质量地生成仓库级代码。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123011210A_ABST
    Figure CN123011210A_ABST
Patent Text Reader

Abstract

The application discloses a method and device for generating warehouse-level code, wherein in the warehouse-level code generation process, a truly effective code segment is efficiently screened out from a candidate context, the calculation cost and reasoning time are significantly reduced while the generation quality is improved, and thus the warehouse-level code is quickly and high-quality generated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of large language model technology, and in particular to a method and apparatus for generating repository-level code. Background Technology

[0002] In recent years, large language models have demonstrated strong capabilities in code generation tasks. However, in more complex repository-level code generation scenarios, models rely on the internal code context of the repository to generate correct results. Existing methods typically use retrieval-enhanced generation techniques, inputting multiple candidate code snippets as context into the model. However, this approach often results in a large amount of semantically similar but functionally irrelevant redundant information in the retrieved code context. This noisy context interferes with the model's inference process, making it difficult for the model to accurately capture truly useful information, thus reducing the correctness of the generated code. Furthermore, when increasing the number of retrievals to improve recall, the proportion of invalid context rises simultaneously, leading to a "performance improvement followed by a decline" phenomenon. Meanwhile, redundant context significantly increases input length, resulting in higher computational costs and inference latency, reducing overall system efficiency. Therefore, how to generate repository-level code simply and efficiently has become an urgent problem to be solved. Summary of the Invention

[0003] This invention provides a method and apparatus for generating repository-level code, in order to solve the problem that existing methods cannot generate repository-level code efficiently / quickly and with high quality.

[0004] In a first aspect, the present invention provides a method for generating repository-level code, the method comprising: The code context selection phase includes: performing static analysis on the code repository to obtain a code context set library, and based on the code context set library, obtaining a candidate code context set according to the received code generation request; grouping the candidate code context set, and using a draft model to generate draft code sequences {D1, ..., D} for each group of candidate contexts. n}, and apply the structure-aware draft to guide the preset context selection algorithm to select the effective code context set G*, where n is a natural number; The target code generation stage includes: constructing a generation prompt based on the effective code context set G* and the code generation request; generating the final code based on the draft code sequence and the prompt using a draft-aware parallel decoding method with a code generation model.

[0005] Optionally, the step of performing static analysis on the code repository to obtain the code context collection library includes: The source code file is constructed using the code parsing tool Tree-sitter. The AST nodes are traversed, and the code text of functions and classes is extracted as code units C. Each code unit is saved as a code context object. After parsing, the code context set {C1, ..., C} is... N Save it as a JSONL file or database table, and create an index for retrieval to obtain the code context collection library.

[0006] Optionally, based on the code context set library, a candidate code context set is obtained according to the received code generation request, including: The received code generation request is used as query Q. The BM25 algorithm is used for keyword retrieval to extract the top-K candidate contexts from the code context set, resulting in the K most relevant code text fragments {C1, ..., C}. K}, which is the candidate code context set.

[0007] Optionally, the candidate code context set is grouped, and a draft code sequence {D1, ..., D} is generated for each group of candidate contexts using a draft model. n}, and applies a structure-aware draft-guided pre-defined context selection algorithm to select a set of valid code contexts G*, including: Candidate contexts are sorted in descending order of similarity score with query Q, and the Top-K contexts are divided into n groups G according to a preset grouping size M. i n is K / M, rounded down, for each context group G i Concatenate the original query Q with a separate hint P. i ; Multiple P i Input a draft model, and generate multiple corresponding code drafts D. i API matching and logical similarity filtering are performed to obtain the final valid context set G*.

[0008] Optionally, the step of performing API matching filtering and logical similarity filtering to obtain the final effective context set G* includes: In the API matching and filtering process, the Tree-sitter tool is used to parse the draft code and the candidate context code, extract the external API call set in the draft, and extract the internal API set and external API set in the candidate context. It is then calculated whether there is an intersection between the external API call set and all API sets of the candidate context. If there is an intersection, the code context is retained. In logical similarity screening, the calculation is performed for each draft D. i Its corresponding context group G i Each candidate context {C1,...,C} M The similarity between the API matching context and the logical similarity context is calculated, and the L code contexts with the highest scores are selected, where L is a preset hyperparameter. The union of the contexts selected by API matching and the contexts selected by logical similarity is taken to obtain the final effective context set G*.

[0009] Optionally, based on the draft code sequence and the aforementioned prompts, the final code is generated using a draft-aware parallel decoding method via a code generation model, including: Using a code generation model, the code generation request is concatenated with the valid context set G* to form a prompt, which is then converted into a token sequence. Here, the token is the basic lexical unit for the code generation model to process text and code, and the final code is generated based on the token sequence.

[0010] Optionally, generating the final code based on the token sequence control code generation model further includes: From the draft code sequence {D1, ..., D n A draft code is selected as a candidate sequence. This draft code is then converted into a token sequence by the code generation model's tokenizer. Multiple draft tokens are input into the code generation model in parallel. The code generation model calculates the predicted probability distribution of all positions through a single forward inference. If a draft token matches the token predicted by the code generation model at the corresponding position, the token is accepted, and subsequent tokens are verified. If they do not match, the current continuous verification is stopped, and the position is replaced with the token predicted by the code generation model. Finally, the final code is generated.

[0011] Optionally, the method further includes: searching for potential usable code segments from the draft code based on whether the suffix of the currently generated code is a prefix of the code segment in the draft code; if a potential usable code segment is found, continuing to use draft-aware parallel decoding for parallel verification; otherwise, continuing autoregressive generation until the code generation model generates an end symbol and stops generating and outputs the final code.

[0012] Secondly, the present invention provides an apparatus for generating repository-level code, the method comprising: The code context selection module performs static analysis on the code repository to obtain a code context set library, and based on the code context set library, obtains a candidate code context set according to the received code generation request; it groups the candidate code context set, and uses a draft model to generate draft code sequences {D1, ..., D} for each group of candidate contexts. n}, and apply the structure-aware draft to guide the preset context selection algorithm to select the effective code context set G*, where n is a natural number; The target code generation module is used to construct a generation prompt based on the effective code context set G* and the code generation request, and generate the final code using a draft-aware parallel decoding method based on the draft code sequence and the prompt.

[0013] Thirdly, the present invention provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the pre-planned cluster leader election method for rapid network construction of UAV swarm-oriented networks as described above.

[0014] The beneficial effects of this invention are as follows: This invention efficiently filters out truly effective code snippets from candidate contexts during the repository-level code generation process, significantly reducing computational overhead and inference time while improving generation quality, thereby generating repository-level code efficiently / fast and with high quality.

[0015] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and in order to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description

[0016] Various other advantages and benefits will become apparent to those skilled in the art upon reading the following detailed description of preferred embodiments. The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Furthermore, the same reference numerals denote the same parts throughout the drawings. In the drawings: Figure 1 This is a flowchart illustrating a method for generating repository-level code according to an embodiment of the present invention; Figure 2 This is a flowchart illustrating a method for generating repository-level code according to an embodiment of the present invention; Figure 3 This is a schematic diagram of the structure of an apparatus for generating repository-level code according to an embodiment of the present invention. Detailed Implementation

[0017] The present invention will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and do not limit the scope of the invention.

[0018] Existing Top-K retrieval methods, which select the top K contexts based solely on similarity without further filtering, inevitably introduce a large number of semantically related but functionally irrelevant code snippets as the K value increases. This results in an increased proportion of noise in the input, interfering with the attention distribution of large code generation models, making it difficult for the model to accurately locate key contexts during generation, ultimately leading to a decrease in the accuracy of the generated code. Reinforcement learning-based retrieval optimization methods (such as RL-Coder) rely on specific training data distributions, and their learned retrieval strategies are highly task-dependent. When applied to different code repositories or new scenarios, the retrieval strategies struggle to maintain stable performance, leading to fluctuations in context selection quality and affecting the accuracy of the generated code. Lightweight model-based context control methods (such as RepoFormer and CodeFilter) lack in-depth modeling of the internal structure of the code (such as function call relationships and semantic dependencies), thus only performing coarse-grained filtering and failing to accurately identify code snippets truly critical to the current task, resulting in insufficient filtering accuracy. Furthermore, context-compression-based methods (such as LongCodeZip) assess context importance by calculating the perplexity of code snippets, thus achieving fine-grained filtering. However, this method requires multiple forward computations on multiple candidate contexts, resulting in a complex computational process and a significant increase in overall inference time. In practical applications, especially in low-latency scenarios such as code generation tasks, these methods struggle to balance filtering effectiveness with computational efficiency. In general, existing methods suffer from two main problems: firstly, simple retrieval or coarse-grained filtering methods struggle to effectively filter noisy contexts, limiting generation accuracy; secondly, while refined filtering methods can improve context quality, they typically incur high computational overhead, making it difficult to meet the efficiency requirements of practical applications. Therefore, existing methods struggle to achieve a balance between "context quality" and "computational efficiency."

[0019] To address the above problems, embodiments of the present invention provide a method for generating repository-level code, see [link to relevant documentation]. Figure 1 The method includes: S101, Code Context Selection; Specifically, the code context selection in this embodiment of the invention includes: S1. Perform static analysis on the code repository to obtain a code context collection library; specifically, in this embodiment of the invention, an abstract syntax tree (AST) is constructed from the source code files using a code parsing tool, the AST nodes are traversed, the code text of functions and classes is extracted as code units C, and each code unit is saved as a code context object. After parsing, the code context collection {C1, ..., C...} is... N Save it as a JSONL file or database table, and create an index for retrieval, thus obtaining the code context collection library for subsequent retrieval.

[0020] S2. Based on the code context set library, obtain a candidate code context set according to the received code generation request; It is understood that, in this embodiment of the invention, the received code generation request is used as query Q, and the top-K candidate contexts are retrieved from the code context set. The BM25 algorithm is then used for keyword retrieval to obtain the K code text fragments {C1, ..., C...} most relevant to the code generation request. K}, which is the candidate code context set.

[0021] Specifically, the system then receives user input, which can be the function signature to be completed or a natural language description of the requirements. The system uses this input as query Q and retrieves the Top-K candidate contexts from the code context set. K is a configurable hyperparameter that can be set according to the model's context length or the use case. In practice, the BM25 algorithm is implemented using BM25Okapi from the open-source rank_bm25 library to perform keyword retrieval and return the K most relevant code text fragments {C1, ..., C} to the user input. K}

[0022] S3. Group the candidate code context set, and use the draft model to generate draft code sequences {D1, ..., D} for each group of candidate contexts. n}; S4. The application of structure-aware drafts guides the selection of a pre-defined context selection algorithm to select a set of valid code contexts G*. Specifically, in this embodiment of the invention, the candidate contexts are sorted in descending order of their similarity scores with query Q, and the Top-K contexts are divided into n groups G according to a preset grouping size M. i n is K / M, rounded down, for each context group G i Concatenate the original query Q with a separate hint P. i ; to combine multiple P i Input a draft model, and generate multiple corresponding code drafts D. iAPI matching and logical similarity filtering are performed to obtain the final valid context set G*.

[0023] In this embodiment of the invention, API matching and logical similarity filtering are performed to obtain the final effective context set G*, which includes: In the API matching and filtering process, the Tree-sitter tool is used to parse the draft code and the candidate context code, extract the external API call set in the draft, and extract the internal API set and external API set in the candidate context. It is then calculated whether there is an intersection between the external API call set and all API sets of the candidate context. If there is an intersection, the code context is retained. In logical similarity screening, the calculation is performed for each draft D. i Its corresponding context group G i Each candidate context {C1,...,C} M The similarity between the API matching context and the logical similarity context is calculated, and the L code contexts with the highest scores are selected, where L is a preset hyperparameter. The union of the contexts selected by API matching and the contexts selected by logical similarity is taken to obtain the final effective context set G*.

[0024] It should be noted that in embodiments D1, ..., D of the present invention n For different draft code orders, n is a natural number, specifically set according to the number of draft code orders.

[0025] In specific implementation, this embodiment of the invention involves the system grouping the retrieved candidate contexts, allowing small draft models to generate draft code for each, and applying a structure-aware draft-guided context selection algorithm to select the effective code context. Specifically, the system sorts the candidate contexts in descending order of their similarity score to query Q, and divides the Top-K contexts into n groups G according to a preset grouping size M. i n is K / M, rounded down. For example, when K=10 and M=4, the system assigns the 1st to 4th contexts to group 1 G1, the 5th to 8th contexts to group 2 G2, and the 9th to 10th contexts to group 3 G3. Each context group G... i Concatenate the original query Q to form a separate hint P. i The prompt word templates are shown in Table 1: Table 1. Prompt Word Templates Next, the system calls a lightweight draft model to generate code drafts. The draft model can be a code-to-large-code generation model with a small parameter size, such as Qwen2.5-Coder-1.5B-Instruct, DeepSeek-Coder-1.3B-Instruct, or other similar open-source code models. In practice, the vLLM framework can be used to load the model and tokenizer and perform batch inference to accelerate the process. The system inputs multiple Pis into the draft model and generates multiple code drafts D in parallel. i During generation, a temperature of 0 can be set to reduce randomness and ensure stable draft code results. This allows for more accurate judgment and generation by reducing the context length of the draft model. The process supports parallelism, thus reducing inference time. On the widely used academic benchmark CoderEval dataset, with K=10, M=4, and four NVIDIA 4090 graphics cards, DeepSeek-Coder 1.3B-Instruct takes a maximum of only 1.3 seconds. Subsequently, the system applies a structure-aware draft-guided context selection algorithm. This step includes API call matching and logical similarity filtering. API stands for Application Programming Interface, which in this invention mainly refers to the function, class, method, or module interface in the code repository. In specific implementation, the system again uses the Tree-sitter tool to parse the draft code and candidate context code, extracting the external API call set from the draft, such as function call names and class method call names; simultaneously, it extracts the internal and external API sets from the candidate context. Internal APIs refer to functions, classes, or methods defined within the candidate context itself and callable by other code. External APIs refer to functions, classes, or methods called internally by the candidate context but defined elsewhere. The system calculates whether there is an intersection between the draft's external API set and the candidate context API set. If an intersection exists, it indicates that the context may provide the interface definition required by the draft or a similar interface usage pattern, and therefore it is retained. In the logical similarity filtering, the system calculates D for each draft. i Its corresponding context group G i Each candidate context {C1, ..., C} MThe similarity between the two is calculated. In specific implementation, BM25Okapi can still be used, with draft code as the query and candidate context code within the group as the document. BM25 similarity scores are calculated, and the top-L code contexts with the highest scores are selected. L is a preset hyperparameter, and L is less than the group size M. This invention defaults to 1. Finally, the system takes the union of the API matching filtering results and the logical similarity filtering results to obtain the final effective context set G*, thereby improving the generated code quality of the target large code model. On the publicly available benchmark CoderEval dataset commonly used in academia, the code quality generated using the DeepSeek-Coder-6.7B-Instruct model can be improved by up to 52.7% compared to not using this invention, and by up to 31.3% compared to the best baseline LongCodeZip; using the Qwen2.5-Coder-7B-Instruct model can be improved by up to 19.2%, and by up to 9.9% compared to LongCodeZip.

[0026] S102, Target code generation, that is, constructing a generation prompt based on the effective code context set G* and the code generation request, and generating the final code based on the draft code sequence and the prompt using a draft-aware parallel decoding method of the code generation model.

[0027] Specifically, in this embodiment of the invention, a code generation model is used to concatenate the code generation request with the valid context set G* to form a prompt, and then convert it into a token sequence. Here, the token is the basic lexical unit for the code generation model to process text and code, and the final code is generated based on the token sequence.

[0028] That is, the embodiments of the present invention are derived from the draft code sequence {D1, ..., D...} n A draft code is selected as a candidate sequence. This draft code is then converted into a token sequence by the code generation model's tokenizer. Multiple draft tokens are input into the code generation model in parallel. The code generation model calculates the predicted probability distribution of all positions through a single forward inference. If a draft token matches the token predicted by the code generation model at the corresponding position, the token is accepted, and subsequent tokens are verified. If they do not match, the current continuous verification is stopped, and the position is replaced with the token predicted by the code generation model. Finally, the final code is generated.

[0029] In specific implementation, this embodiment of the invention selects a draft code as a candidate sequence from the draft code sequence {D1, D2, ..., Dn} generated in stage S4. The default selection strategy is to select the draft with the highest semantic similarity to the current code generation request, i.e., the draft D1 with the highest similarity score. The similarity calculation is based on the BM25 similarity used in the context retrieval stage in step S101. If the user specifies or the system determines that other strategies need to be adopted based on the task type, a different strategy may be adopted. After selecting a draft, the system converts the draft code into a draft token sequence through the word segmenter of the code generation model. Then, the system concatenates the code generation request with the effective code context set to generate a hint, and uses draft-aware parallel inference to accelerate the process: the model calculates the predicted probability distribution corresponding to each position in the draft token sequence in parallel through one forward inference, i.e., the model predicts the probability distribution of the token that should be generated at that position based on the hint and the draft prefix. The specific verification process is as follows: starting from the first token in the draft sequence, the probability distribution predicted by the model at the corresponding position is obtained, and the token corresponding to the highest probability is taken as the predicted token. If the predicted token matches the token at the corresponding position in the draft sequence, the draft token is accepted, appended to the output sequence as a generated token, and the verification of the draft token at the next position continues. If they do not match, the current continuous verification process is immediately stopped, the draft token at that position is replaced with the token predicted by the model, the replaced token is appended to the output sequence, and the current parallel verification is terminated. When the parallel verification process is interrupted, the system does not directly revert to the fully autoregressive mode, but instead initiates a string matching mechanism to attempt to continue utilizing reusable fragments from the draft code. Let the currently generated code sequence be the current generated sequence. The system checks whether the string suffix of the current generated sequence matches the prefix of a fragment in the original draft code. The longest common substring algorithm is used for matching, and a minimum matching length threshold is set to the string length corresponding to three tokens to avoid overly fragmented matching. If a matching fragment that meets the threshold condition is found, the system continues to use all subsequent tokens starting from the end position of the matching fragment in the draft code as new parallel verification candidate sequences. Through the above parallel inference process, the generation efficiency of the target model is further improved. On the widely used academic benchmark CoderEval dataset, the code generation time using the DeepSeek-Coder-6.7B-Instruct model can be reduced by up to 48.6% compared to not using this invention, and by up to 48.6% compared to the best baseline RepoFormer; the inference time using the Qwen2.5-Coder-7B-Instruct model can be reduced by up to 52.1%, and by up to 45% compared to RL-Coder.

[0030] In summary, this invention introduces a draft generation mechanism into the code generation process, enabling the model to pre-evaluate the actual contribution of different candidate contexts to the generated results under relatively small-scale context conditions. Since the draft results reflect the effectiveness of each context combination, code fragments that substantially contribute to the generation can be prioritized during subsequent filtering, effectively reducing interference from irrelevant contexts. Furthermore, by combining a structure-aware filtering strategy based on API call relationships and semantic similarity, this invention can perform refined analysis of the context from both code structure and logic perspectives. Compared to methods relying solely on similarity or simple decision mechanisms, it can more accurately identify key contexts, thereby improving the correctness and stability of code generation. Simultaneously, because this invention effectively compresses the context before generation, the information input into the large code generation model is more refined, thus reducing the computational overhead caused by input length. In addition, by introducing a draft-based parallel decoding mechanism, multiple generated tokens can be verified in a single forward computation, reducing the number of computations required for step-by-step generation and significantly reducing inference latency. Therefore, this invention improves the accuracy of context selection while effectively reducing computational complexity, achieving a synergistic improvement in code generation quality and system efficiency, and better meeting the dual requirements of high precision and low latency in practical application scenarios.

[0031] Additionally, embodiments of the present invention also provide an apparatus for generating repository-level code, see [link to relevant documentation]. Figure 3 The method includes: The code context selection module performs static analysis on the code repository to obtain a code context set library, and based on the code context set library, obtains a candidate code context set according to the received code generation request; it groups the candidate code context set, and uses a draft model to generate draft code sequences {D1, ..., D} for each group of candidate contexts. n}, and apply a structure-aware draft to guide a pre-defined context selection algorithm to select a set of valid code contexts G*; The target code generation module is used to construct a generation prompt based on the effective code context set G* and the code generation request, and generate the final code using a draft-aware parallel decoding method based on the draft code sequence and the prompt.

[0032] Specifically, in the embodiments of the present invention, the code context selection module and the target code generation module efficiently filter out truly effective code fragments from candidate contexts during the repository-level code generation process, thereby improving the generation quality while significantly reducing computational overhead and inference time, thus generating repository-level code simply and with high quality.

[0033] Meanwhile, embodiments of the present invention also provide a computer-readable storage medium storing a computer program, which, when executed by a processor, implements any of the above-described methods for rapid network establishment of UAV swarm-oriented networks.

[0034] The relevant content of the device embodiment and storage medium embodiment of the present invention can be understood by referring to the method embodiment of the present invention, and will not be discussed in detail here.

[0035] Although preferred embodiments of the invention have been disclosed for illustrative purposes, those skilled in the art will recognize that various modifications, additions, and substitutions are possible, and therefore the scope of the invention should not be limited to the embodiments described above.

Claims

1. A method for generating repository-level code, characterized in that, The method includes: code context selection and target code generation, wherein, The code context selection includes: performing static analysis on the code repository to obtain a code context set library, and based on the code context set library, obtaining a candidate code context set according to the received code generation request; grouping the candidate code context set, and using a draft model to generate draft code sequences {D1, ..., D} for each group of candidate contexts. n }, and apply the structure-aware draft to guide the preset context selection algorithm to select the effective code context set G*, where n is a natural number; The target code generation includes: constructing a generation prompt based on the effective code context set G* and the code generation request; generating the final code based on the draft code sequence and the prompt using a draft-aware parallel decoding method with a code generation model.

2. The method according to claim 1, characterized in that, The static analysis of the code repository to obtain the code context collection library includes: The source code file is constructed using the code parsing tool Tree-sitter. The AST nodes are traversed, and the code text of functions and classes is extracted as code units C. Each code unit is saved as a code context object. After parsing, the code context set {C1, ..., C} is... N Save it as a JSONL file or database table, and create an index for retrieval to obtain the code context collection library.

3. The method according to claim 2, characterized in that, Based on the code context set library, a candidate code context set is obtained according to the received code generation request, including: The received code generation request is used as query Q. The BM25 algorithm is used for keyword retrieval to extract the top-K candidate contexts from the code context set, resulting in the K most relevant code text fragments {C1, ..., C}. K }, which is the candidate code context set.

4. The method according to claim 3, characterized in that, The candidate code context set is grouped, and a draft code sequence {D1, ..., D} is generated for each group of candidate contexts using a draft model. n }, and applies a structure-aware draft-guided pre-defined context selection algorithm to select a set of valid code contexts G*, including: Candidate contexts are sorted in descending order of similarity score with query Q, and the Top-K contexts are divided into n groups G according to a preset grouping size M. i n is K / M, rounded down, for each context group G i Concatenate the original query Q with a separate hint P. i ; Multiple P i Input a draft model, and generate multiple corresponding code drafts D. i API matching and logical similarity filtering are performed to obtain the final valid context set G*.

5. The method according to claim 4, characterized in that, The process of performing API matching and logical similarity filtering to obtain the final effective context set G* includes: In the API matching and filtering process, the Tree-sitter tool is used to parse the draft code and the candidate context code, extract the external API call set in the draft, and extract the internal API set and external API set in the candidate context. It is then calculated whether there is an intersection between the external API call set and all API sets of the candidate context. If there is an intersection, the code context is retained. In logical similarity screening, the calculation is performed for each draft D. i Its corresponding context group G i Each candidate context {C1,...,C M The similarity between the API matching context and the logical similarity context is calculated, and the L code contexts with the highest scores are retained. L is a preset hyperparameter. The union of the contexts filtered by API matching and the contexts filtered by logical similarity is taken to obtain the final effective context set G*.

6. The method according to any one of claims 1-5, characterized in that, Based on the draft code sequence and the aforementioned hints, the final code is generated using a draft-aware parallel decoding method through a code generation model, including: Using a code generation model, the code generation request is concatenated with the valid context set G* to form a prompt, which is then converted into a token sequence. Here, the token is the basic lexical unit for the code generation model to process text and code, and the final code is generated based on the token sequence.

7. The method according to claim 6, characterized in that, The generation of the final code based on the token sequence control code generation model also includes: From the draft code sequence {D1, ..., D n A draft code is selected as a candidate sequence. This draft code is then converted into a token sequence by the code generation model's tokenizer. Multiple draft tokens are input into the code generation model in parallel. The code generation model calculates the predicted probability distribution of all positions through a single forward inference. If a draft token matches the token predicted by the code generation model at the corresponding position, the token is accepted, and subsequent tokens are verified. If they do not match, the current continuous verification is stopped, and the position is replaced with the token predicted by the code generation model. Finally, the final code is generated.

8. The method according to claim 7, characterized in that, The method further includes: The system searches for potential usable code snippets in the draft code based on whether the suffix of the currently generated code is a prefix of the code snippet in the draft code. If a potential usable code snippet is found, the system continues to use draft-aware parallel decoding for parallel verification. Otherwise, the system continues to perform autoregressive generation until the code generation model generates an end symbol, at which point it stops generating and outputs the final code.

9. An apparatus for generating repository-level code, characterized in that, The method includes: The code context selection module performs static analysis on the code repository to obtain a code context set library, and based on the code context set library, obtains a candidate code context set according to the received code generation request; it groups the candidate code context set, and uses a draft model to generate draft code sequences {D1, ..., D} for each group of candidate contexts. n }, and apply the structure-aware draft to guide the preset context selection algorithm to select the effective code context set G*, where n is a natural number; The target code generation module is used to construct a generation prompt based on the effective code context set G* and the code generation request, and generate the final code using a draft-aware parallel decoding method based on the draft code sequence and the prompt.

10. A computer-readable storage medium storing a computer program that, when executed by a processor, implements the method for generating repository-level code according to any one of claims 1-8.