Code generation

By building a custom retrieval library and retrieving the code context of the target sub-fragments for the unfinished code, high-quality retrieval results and prompt information are generated, which solves the problem of code generation quality under limited computing resources and realizes efficient code generation under small-scale models.

WO2025191348A1PCT designated stage Publication Date: 2025-09-18CLOUD INTELLIGENCE ASSETS HOLDING (SINGAPORE) PTE LTD

Patent Information

Application Number
PCT/IB2025/050682
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2024-03-13
Filing Date
2025-01-23
Publication Date
2025-09-18

AI Technical Summary

Technical Problem

With limited computing resources, existing technologies cannot effectively utilize large language models for high-quality code generation, resulting in poor code generation quality.

Method used

Build a custom retrieval library containing sub-segments cut from reference code snippets. Use the custom retrieval library to retrieve target sub-segments that meet the preset similarity requirements for the unfinished code, and obtain their code context in the reference code snippets. After generating the retrieval results, build a prompt information input code generation model.

Benefits of technology

Under limited computing resources, the code generation quality is improved, making the quality of the code generation model comparable to that of large language models, and effectively reducing memory usage and alleviating memory pressure.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure IB2025050682_18092025_PF_FP_ABST
    Figure IB2025050682_18092025_PF_FP_ABST
Patent Text Reader

Abstract

Embodiments of the present disclosure provide a code generation method, a device, and a storage medium. In the embodiments of the present disclosure, a custom retrieval library can be constructed, wherein the custom retrieval library comprises sub-snippets segmented from reference code snippets; on this basis, a target sub-snippet meeting a preset similarity requirement can be retrieved for an uncompleted code from the custom retrieval library; and then a code context of the target sub-snippet can be acquired to generate a retrieval result. Since the custom retrieval library is at the sub-snippet level, the code diversity in the custom retrieval library can be effectively enhanced, and various code variants can be covered, so that the retrieval precision can be effectively improved, effectively improving the quality of retrieval results. Moreover, prompt information can be constructed on the basis of the generated retrieval result and input into a code generation model.
Need to check novelty before this filing date? Find Prior Art

Description

[0001] Code Generation Technology Field

[0002]

[0001] The present disclosure relates to the field of artificial intelligence technology, and in particular to code generation.

[0003] For programmers, writing high-quality and efficient code is an important task in the software development process. Along with the complexity of software development systems and the increase of code volume, programmers are often faced with the challenge of writing cumbersome and lengthy code.

[0004] Currently, some large language models (LLMs) can provide code generation capabilities. In this solution, unfinished code is input into the LLM, and the LLM performs code completion to achieve code generation. The generated code can be provided to programmers for reference, thereby effectively improving code writing efficiency.

[0005]

[0004] Since code generation technology based on large language models has high requirements for computing resources (such as GPUs, etc.), when computing resources are limited, it is impossible to use large language models and only smaller models can be used for code generation, resulting in poor code generation quality.

[0006]

[0005] Various aspects of the present disclosure provide a code generation method, device, and storage medium for improving the quality of code generation under limited computing resources.

[0007]

[0006] An embodiment of the present disclosure provides a code generation method, comprising: in response to a code generation instruction, searching for a target sub-segment that meets a preset similarity requirement for an unfinished code from a custom search library, wherein the custom search library includes sub-segments segmented from a reference code segment; obtaining a code context corresponding to the target sub-segment in the corresponding reference code segment to generate a search result; and constructing prompt information based on the search result and inputting the prompt information into a code generation model, so that the code generation model generates code for the unfinished code with reference to the prompt information.

[0008]

[0007] Furthermore, the process of constructing the custom search library may include: obtaining reference code snippets from an open source code library; extracting method bodies from the obtained reference code snippets based on abstract syntax tree technology; and dividing the extracted method bodies into sub-segments to construct the custom search library.

[0009]

[0008] Further, dividing the extracted method body into sub-segments may include: if the number of word tokens contained in the target method body exceeds a preset word number threshold, dividing the target method body into multiple sub-segments according to the preset word number threshold, and the number of words contained in each of the divided sub-segments does not exceed the preset word number threshold; wherein the target method body is any extracted method body.

[0010]

[0009] Further, obtaining the code context corresponding to the target sub-segment in the corresponding reference code segment to generate a search result may include: obtaining the method body to which the target sub-segment belongs as the search result.

[0011]

[0010] Furthermore, obtaining reference code snippets from an open source code library may include: filtering out code snippets in the open source code library that do not contain method bodies and / or cannot be parsed into abstract syntax trees, and using the remaining code snippets as reference code snippets; and normalizing variable names and / or constant names contained in the reference code snippets.

[0012]

[0011] Furthermore, the method may further include: searching for the unfinished code from the custom search library according to string similarity to determine K candidate sub-segments; respectively calculating vector similarities between the unfinished code and the K candidate sub-segments; and sorting the K candidate sub-segments based on string similarity and vector similarity to select the target sub-segment.

[0013]

[0012] Further, sorting the K candidate sub-segments based on the string similarity and the vector similarity to determine the target sub-segment may include: recording the first ranking corresponding to the sorting according to the string similarity for each of the K candidate sub-segments; recording the second ranking corresponding to the sorting according to the vector similarity for each of the K candidate sub-segments; performing a weighted combination of the corresponding first ranking and second ranking for a single candidate sub-segment to obtain a final ranking corresponding to the single candidate sub-segment; and sorting the K candidate sub-segments according to their respective final rankings to select the target sub-segment.

[0014]

[0013] Further, constructing prompt information based on the retrieval results and inputting it into a code generation model may include: performing a quality assessment on the retrieval results; if the retrieval results meet the quality assessment requirements, adding the retrieval results to a preset template to generate the prompt information; splicing the prompt information and the unfinished code into input information to input into the code generation model.

[0015]

[0014] Further, performing quality assessment on the search result may include: calculating the vector similarity between the search result and the unfinished code; if the vector similarity exceeds a preset standard, determining that the search result meets the quality assessment requirement.

[0016]

[0015] Further, the method may further include: if the search result does not meet the quality assessment requirement, using a preset template that does not include the search result as the prompt information.

[0017]

[0016] Further, the preset template includes a search result filling area and a prompt field for isolating the search results from the unfinished code.

[0018]

[0017] An embodiment of the present disclosure further provides a computing device, which may include a memory, a processor, and a communication component; the memory is used to store one or more computer instructions; the processor is coupled to the memory and the communication component, and is used to execute the one or more computer instructions to perform the aforementioned code generation method.

[0019]

[0018] The embodiment of the present disclosure may also provide a computer-readable storage medium storing a computer program, which, when executed by one or more processors, causes the one or more processors to execute the aforementioned code generation method.

[0020]

[0019] The embodiments of the present disclosure may also provide a computer program product, which may include a computer program / instructions, wherein when the computer program is executed by a processor, the processor is caused to implement the aforementioned code generation method.

[0021]

[0020] In an embodiment of the present disclosure, a new code generation solution is proposed. A custom search library can be constructed, which contains sub-segments cut from reference code fragments. Based on this, a target sub-segment that meets the preset similarity requirements can be retrieved from the custom search library for the unfinished code. The code context of the target sub-segment can then be obtained to generate a search result. Because the custom search library is at the sub-segment level, it can effectively expand the code diversity in the custom search library and cover various code variants. This can effectively improve the search accuracy and thus effectively improve the quality of the search results. Based on the generated search results, prompt information can be constructed and input into a code generation model. In this way, when computing resources are limited, a model of appropriate scale can be used to undertake code generation work, and the code generation quality of the model can be improved by inputting better prompt information into the model.

[0022] The accompanying drawings described herein are intended to provide a further understanding of the present disclosure and constitute a part of the present disclosure. The exemplary embodiments of the present disclosure and their descriptions are intended to explain the present disclosure and do not constitute an improper limitation of the present disclosure. In the accompanying drawings:

[0023] FIG1 is a flow chart of a code generation method provided by an exemplary embodiment of the present disclosure;

[0024] FIG2 is a logic diagram of a code generation method provided by an exemplary embodiment of the present disclosure;

[0025] FIG3 is a flow chart of an exemplary custom search library construction solution provided by an exemplary embodiment of the present disclosure;

[0026] FIG4 is a flow chart of an exemplary retrieval scheme provided by an exemplary embodiment of the present disclosure;

[0027] FIG5 is a logic diagram of an exemplary code generation solution in an exemplary application scenario provided by an exemplary embodiment of the present disclosure;

[0028]

[0027] FIG6 is a schematic diagram of the structure of a computing device provided by another exemplary embodiment of the present disclosure.

[0029] To make the objectives, technical solutions, and advantages of the present disclosure more clear, the technical solutions of the present disclosure will be clearly and completely described below in conjunction with specific embodiments of the present disclosure and corresponding drawings. Obviously, the described embodiments are only some of the embodiments of the present disclosure, and are not all of the embodiments. All other embodiments obtained by persons of ordinary skill in the art based on the embodiments of the present disclosure without creative effort are within the scope of protection of the present disclosure.

[0030]

[0029] Before describing in detail the technical solutions provided by the various embodiments of the present disclosure, some of the technical concepts involved in the present disclosure are briefly described as follows.

[0031] Code generation, also known as code completion, can be understood as completing the code context for an unfinished code segment to generate a more complete code. For example, a user only needs to enter 5 lines of code, and after code completion, 100 lines of code context can be generated for these 5 lines of code, without the user having to manually write these code contexts. Of course, the numbers here are for illustrative purposes only. Clearly, code generation can effectively improve the user's code writing efficiency.

[0032] A code generation model refers to a neural network model or a machine learning model with a code completion function. The code generation model can be a large language model or a smaller model. The scale here can refer to the parameter scale or the number of layers. The smaller the model, the less computing resources are usually required.

[0033] During their research, the inventors discovered that large language models can provide better code generation quality. However, the parameter scale of large language models typically needs to reach billions or even tens of billions to achieve relatively good code generation quality. At this scale, large language models require a large amount of computing resources. This makes it impossible to use large language models for code generation when computing resources are limited.

[0034]

[0033] To this end, the embodiments of the present disclosure propose an innovative code generation solution that can be applied in situations where computing resources are limited and can ensure the quality of code generation.

[0035]

[0034] The following describes in detail the technical solutions provided by various embodiments of the present disclosure in conjunction with the accompanying drawings.

[0036] FIG1 is a flow chart illustrating a code generation method according to an exemplary embodiment of the present disclosure, and FIG2 is a logic diagram illustrating a code generation method according to an exemplary embodiment of the present disclosure. The method may be performed by a code generation device, which may be implemented as software, hardware, or a combination of software and hardware, and may be integrated into a computing device. Referring to FIG1 , the method may include steps 100 to 102.

[0037]

[0036] Step 100: In response to a code generation instruction, a target sub-segment that meets a preset similarity requirement is retrieved for the unfinished code from a custom search library, where the custom search library contains sub-segments segmented from a reference code segment.

[0038]

[0037] Step 101: For a target sub-segment, obtain its corresponding code context in the corresponding reference code segment to generate a search result.

[0039]

[0038] Step 102: Construct prompt information based on the search results and input it into a code generation model, so that the code generation model can refer to the prompt information to generate code for the incomplete code.

[0039] The code generation method provided in this embodiment can be applied to various scenarios requiring code completion. This embodiment does not limit the application scenario. The code generation device provided in this embodiment can be used as a functional module in an existing computer program product, for example, as a platform function in a code development platform; it can also be used as an independent computer program product, for example, providing an independent code generation call port to provide code generation services to external parties. This embodiment also does not limit the product form of the code generation device.

[0040]

[0040] Referring to FIG. 1 , in step 100, in response to a code generation instruction, unfinished code may be obtained. Unfinished code may be understood as code that requires code completion. Unfinished code is typically provided by a user (e.g., a programmer). In practical applications, it may be several lines of code input by the user. In terms of data form, the unfinished code obtained in step 100 is a string. Here, the unfinished code may be included in the code generation instruction, allowing the code generation device in this embodiment to parse the unfinished code from the code generation instruction. In step 100, this embodiment also proposes a custom search library and describes the contents of the custom search library. As previously described, the custom search library in this embodiment contains sub-segments segmented from a reference code segment.

[0041] The reference code snippet may be a public code snippet obtained from a public channel. For example, it may be a code snippet corresponding to a commonly used functional module accumulated within a company. Another example may be a code snippet uploaded by other users and crawled from the Internet. This embodiment does not limit the source of the reference code snippet. In terms of storage format, the reference code snippet is typically stored as a code file. Preferably, a public code snippet that is relatively complete in logic and longer than the unfinished code can be selected as the reference code snippet, but this embodiment is not limited to this.

[0042]

[0042] Based on this, this embodiment proposes that sub-segments can be segmented from a reference code snippet, and a custom search library can be constructed based on the sub-segments. During research, the inventors discovered that, through segmentation, a complete and long reference code snippet can be broken down into several shorter sub-segments. From the perspective of a single sub-segment, it represents a small portion of the string within the reference code snippet. This allows the sub-segment to participate independently in search and comparison, without being affected by other strings within the reference code snippet to which it belongs. During research, the inventors also discovered that users often use code variants during code writing, resulting in significant differences between the unfinished code provided by the user and the reference code snippet. The sub-segment-level custom search library proposed in this embodiment can effectively increase code diversity, allowing even unfinished code corresponding to code variants to generate high-quality search results in the custom search library of this embodiment. The code variant can be understood as the code generated by deforming the regular code. Obviously, the code variant is generally less similar to the reference code snippet. Accordingly, the unfinished code entered by the user for the code variant is also less similar to the reference code snippet as a whole.

[0043] For example, an unfinished code contains 10 lines of code, of which lines 1-5 conform to conventional code, but lines 6-10 are unconventional code. If a search is performed directly based on the reference code segments, the similarity between the unfinished code and each reference code segment is relatively low due to the significant difference between lines 6-10 and conventional code. However, the custom search library provided in this embodiment may contain sub-segments corresponding to lines 1-5. Thus, when searching the unfinished code, the sub-segment corresponding to lines 1-5 in the custom search library can be found based on the unfinished code. It can be seen that when searching using a sub-segment as the search unit, the search is not affected by other sub-segments in the reference code segment to which it belongs. Therefore, even if the unfinished code contains unconventional code, the appropriate sub-segment can be accurately found for the unfinished code.

[0044] In this embodiment, in step 100, an optimization search may be performed for the unfinished code in the custom search library to identify target sub-segments that meet a preset similarity requirement. The preset similarity requirement can be flexibly set as needed and is not limited in this embodiment. An exemplary search scheme and an exemplary preset similarity requirement will be provided later, and will not be described in detail here.

[0045] It is worth noting that, in this embodiment, the sub-segments are stored in the form of character strings in the custom search library. Moreover, in this embodiment, only this one search library needs to be constructed to obtain excellent search results, which can effectively reduce the memory resource usage of the search library in the aforementioned computing device and alleviate memory pressure.

[0046] Continuing with FIG. 1 , in step 101, the code context corresponding to the target sub-segment in the corresponding reference code segment can be obtained for the target sub-segment to generate a search result. As previously described, in this embodiment, the target sub-segment is retrieved from the custom search library. However, during research, the inventors discovered that obtaining the code context for the target sub-segment can provide more reference for the subsequent code generation model. The code context refers to the code portion adjacent to the target sub-segment in the reference code segment.

[0047] In an exemplary solution, during the aforementioned process of segmenting the reference code segment, the order relationship between the segmented sub-segments can be recorded and stored in the custom search library of this embodiment. Thus, in step 101, by querying this order relationship, the preceding and / or subsequent sub-segments of the target sub-segment in the corresponding reference code segment can be determined, thereby obtaining the code context corresponding to the target sub-segment.

[0048]

[0048] The target sub-segment and its corresponding code context may be used as the search result generated in step 101. Thus, the search result generated in step 101 may be part or all of the code in the reference code segment to which the target sub-segment belongs.

[0049]

[0049] The search results generated by searching with sub-segments as the search units are obviously of higher quality than the search results that can be obtained by directly searching the reference code segments.

[0050]

[0050] On this basis, in step 102 of this embodiment, it is proposed that prompt information can be constructed based on the search results and input into the code generation model, so that the code generation model can refer to the prompt information for unfinished code to perform code generation.

[0051] It is worth noting that this embodiment abandons the large language model and instead adopts a code generation model that can be supported by limited computing resources. Although the scale of this code generation model is smaller than that of the large language model, because this embodiment provides high-quality prompt information for this code generation model, it can effectively improve the code generation quality of this code generation model. During the research process, the inventors found that based on the prompt information provided by this embodiment, the code generation quality of the code generation model is comparable to that of the large language model.

[0052] In the present embodiment, the code generation logic in the code generation model is not limited. The general principle of the code generation model in the present embodiment is: extract features from the prompt information, as additional context information, and write code context for the unfinished code with reference to the context information to produce output results. This is only exemplary, and the present embodiment is not limited thereto.

[0053] The inventors discovered during the research process that high-quality prompt information can effectively improve the code generation quality of the code generation model. In this regard, in a preferred implementation, it is further proposed that: the search results are subjected to quality assessment; if the search results meet the quality assessment requirements, the search results are provided to the code generation model through the prompt information; and if the search results do not meet the quality assessment requirements, the search results are no longer provided to the code generation model.

[0054]

[0054] In this way, it can be ensured that only high-quality search results are provided to the code generation model, while search results of insufficient quality will be discarded to avoid misleading the code generation model and affecting the code generation quality of the code generation model.

[0055] In an exemplary quality assessment scheme, vector similarity between the search results and the unfinished code can be calculated; if the vector similarity exceeds a preset standard, the search results are determined to meet the quality assessment requirements. The MPNet model can be used to calculate the vector similarity between the search results and the unfinished code. MPNet is a language embedding model trained on source code that converts string-based code into numeric vectors and calculates vector similarity through vector comparison. The preset standard can be set as needed. Thus, by performing a quality assessment on the search results, search results with vector similarity below the preset standard can be discarded to prevent these search results from misleading the code generation model.

[0056] It should be understood that, for the code generation model, even if the search results provided by the present embodiment are not obtained, the code generation for the unfinished code can be completed by the internal code generation logic. In other words, not providing the search results to the code generation model will not affect the normal operation of the code generation model.

[0057]

[0057] In a further optimization scheme, this embodiment further proposes: if the retrieval result meets the quality assessment requirements, the retrieval result can be added to the preset template to generate prompt information; and if the retrieval result does not meet the quality assessment requirements, the preset template that does not include the retrieval result can be used as prompt information. Among them, a preset template corresponding to the prompt information is provided in the optimization scheme, and independent prompt information can be packaged through the preset template, so that the code generation model can recognize the prompt information and avoid the code generation model confusing the prompt information with the unfinished code. Preferably, the preset template can include a retrieval result filling area and a prompt field for isolating the retrieval result from the unfinished code. Among them, the aforementioned retrieval results that can be provided to the code generation model can be added to the retrieval result filling area in the preset template; and if there is no retrieval result that meets the quality assessment requirements, the retrieval result filling area in the preset model can be empty. In addition, the prompt field in the preset template can be a string that the code generation model can understand, for example, <aug>< / aug> or <special_token>, etc., to prompt the code generation model that this part of the information is a prompt. In actual application, in the preset template, prompt fields can be set at the front and back ends of the search result filling area, as shown in the following example:

[0058] <aug>< / aug> Search results filling area

[0059] <aug>< / aug>

[0060] In this way, the hint field can be used to isolate the search results, thereby avoiding confusion with the unfinished code. In this example, "aug" is used to prompt the code generation module and that this part of the information will perform retrieval augmentation on the unfinished code. It should be understood that this is merely exemplary and the present embodiment is not limited thereto.

[0061] Referring to FIG. 2 , in step 102, the prompt information and the unfinished code may be combined into input information for input into the code generation model. The aforementioned preset template may be used to separate the search results from the unfinished code in the prompt information, thereby enabling the code generation model to more accurately distinguish between the search results and the unfinished code, thereby improving the code generation quality of the code generation model.

[0062] In summary, this embodiment proposes a new code generation solution that constructs a custom search library containing sub-segments segmented from reference code snippets. Based on this, the custom search library can be searched for target sub-segments that meet preset similarity requirements for unfinished code. The code context of the target sub-segment can then be obtained to generate search results. Because the custom search library is at the sub-segment level, it effectively expands the code diversity within the custom search library and can cover various code variants. This effectively improves search accuracy and thus the quality of search results. Based on the generated search results, prompt information can be constructed and input into a code generation model. In this way, when computing resources are limited, a model of appropriate scale can be used to perform code generation tasks, and the code generation quality of the model can be improved by inputting better prompt information into the model.

[0063] In the above or following embodiments, a variety of construction schemes can be used to construct a custom search library. FIG3 is a flow chart of an exemplary custom search library construction scheme provided by an exemplary embodiment of the present disclosure. Referring to FIG3 , this exemplary construction scheme may include steps 300 to 302.

[0064]

[0062] Step 300: Obtain a reference code snippet from an open source code library.

[0063] Step 301: Extract a method body from the obtained reference code snippet based on the abstract syntax tree technology.

[0065]

[0064] Step 302: Divide the extracted method body into sub-segments to construct a custom search library.

[0066]

[0065] The open source code base can be a relatively comprehensive code base that is self-built or provided through public channels. In this exemplary construction scheme, reference code snippets can be obtained from the open source code base. Preferably, in step 300, the code snippets in the open source code base can first be converted based on the abstract syntax tree technology. On this basis, the open source code base can be cleaned to filter out code snippets that do not contain method bodies and / or cannot be parsed into abstract syntax trees. The remaining code snippets are used as reference code snippets in this embodiment. The abstract syntax tree (AST) is an abstract representation of the grammatical structure of the source code. It represents the grammatical structure of the programming language in a tree-like form, and each node on the tree represents a structure in the source code. The technical principles of the abstract syntax tree will not be elaborated here, and reference can be made to the records in the public materials. However, two concepts in the abstract syntax tree used in this embodiment are explained here:

[0067] One is the method body. The method body is a block of statements following the method declaration. It is a code area used to implement specific functionality. When a method is called, the code within the method body is executed to complete the method's task. The method body is typically enclosed by a pair of curly braces {}. The method body is usually preceded by a series of indices, such as 'public', 'private', and 'static', indicating the method type and access rights.

[0068] Another is a token. In the field of compiler theory, a token is the smallest unit in a high-level programming language. A token is the basic, indivisible unit that constitutes a source program. Abstract syntax tree technology includes a lexical parsing step, in which all tokens contained in the code can be parsed.

[0069] In addition to cleaning the open source codebase, preferably, in step 300, the variable names and / or constant names contained in the filtered reference code snippets may also be normalized. The inventors discovered during their research that the filtered reference code snippets may contain some variable names and / or constant names customized by the code authors. Here, it is proposed to normalize these customized variable names and / or constant names to ensure that the variable names corresponding to the same variable remain consistent across all reference code snippets, and the constant names corresponding to the same constant remain consistent across all reference code snippets. This can effectively improve the accuracy of the search process and avoid missing appropriate sub-segments due to variable and / or constant names.

[0070] Continuing with FIG. 3 , in step 301, a method body can be extracted from the reference code snippet obtained in step 300 based on the abstract syntax tree technology. Based on the previous explanation of the method body, it can be seen that in step 301, the reference code snippet is essentially segmented into one or more method bodies.

[0071] In step 302, the segmentation process continues, dividing the extracted method body into sub-segments to construct a custom search library. That is, in this exemplary construction scheme, the segmented sub-segments are shorter than or equal to the method body to which they belong. Furthermore, in step 302, the order relationship between the sub-segments extracted from a single method body may also be recorded.

[0072] During research, the inventors discovered that different segmentation lengths in step 302 resulted in different code generation quality. Therefore, this exemplary construction scheme proposes a preferred segmentation method: a suitable token number threshold can be preset. In step 302, if the number of tokens contained in the target method body exceeds the preset token number threshold, the target method body is segmented into multiple sub-segments according to the preset token number threshold, with the number of tokens contained in each of the segmented sub-segments not exceeding the preset token number threshold. The target method body can be any method body extracted in step 301. In this preferred segmentation method, a uniform segmentation length (i.e., the preset token number threshold) is used to segment each method body extracted in step 301. If the length of a method body is greater than the segmentation length, the method body is segmented into multiple sub-segments. If the length of the method body is less than or equal to the segmentation length, the method body does not need to be segmented. The lengths of the sub-fragments obtained under this preferred segmentation method will not exceed the segmentation length.

[0073]

[0072] In this preferred segmentation method, multiple word quantity thresholds can be enumerated, and at each enumerated word quantity threshold, the code generation quality is tested according to the code generation scheme provided in this embodiment. Then, based on the tested code generation quality, a final word data threshold is optimized from the enumerated multiple word quantity thresholds as the word data threshold used in this preferred segmentation method.

[0074] Thus, the custom search library constructed in step 302 will contain a number of sub-segments. As mentioned above, the custom search library can also store the sequential relationship between the sub-segments contained in the method body on a method body basis. This can provide a rich, diverse, and concise search basis for subsequent search steps.

[0075] Based on the exemplary construction scheme shown in FIG3 , in this embodiment, during the search phase (e.g., step 102 in FIG1 ), the method body to which the target sub-segment belongs can be obtained as a search result. The sub-segments located before and / or after the target sub-segment in the method body to which the target sub-segment belongs can be used as the code context corresponding to the target sub-segment. It should be understood that if the method body described in the target sub-segment also includes sub-segments located before the target sub-segment, these sub-segments can also be included in the search results. The inventors have discovered during their research that these sub-segments can generally provide positive information to the code generation model, helping to improve code generation quality.

[0076]

[0075] It should be understood that, in addition to the exemplary construction scheme shown in FIG. 3 , other construction schemes may be adopted in this embodiment. For example, different segmentation lengths may be adopted for different types of method bodies, or reference code fragments may be segmented based on manual experience. Other exemplary construction schemes will not be described in detail herein.

[0077] In summary, this embodiment provides an optimization solution for the construction phase of a custom search library. By performing pre-processing operations such as cleaning and normalization, the quality of the reference code snippets is improved. By adopting a reasonable segmentation scheme, the reference code snippets can be segmented into sub-segments of appropriate lengths. Sub-segments of appropriate lengths can effectively improve the quality of the search results, thereby improving the code generation quality of the code generation model.

[0078] In the above or following embodiments, a variety of implementations may be used to retrieve the target sub-segment. FIG4 is a flowchart illustrating an exemplary retrieval solution provided in accordance with an exemplary embodiment of the present disclosure. Referring to FIG4 , the exemplary retrieval solution may include steps 400 to 402.

[0079]

[0078] Step 400: Optimize the unfinished code from the custom search library according to the string similarity to determine K candidate sub-segments.

[0080]

[0079] Step 401: Calculate the vector similarities between the unfinished code and the K candidate sub-segments respectively.

[0081]

[0080] Step 402: Sort the K candidate sub-segments based on the string similarity and the vector similarity to select the target sub-segment.

[0082]

[0081] Referring to FIG. 4 , as mentioned above, the data in the custom search library in this embodiment is in the form of strings. Therefore, in step 400, the unfinished code can be optimized from the custom search library based on string similarity to determine K candidate sub-segments. In step 400, the custom search library can preferably be implemented using an ElasticSearch database. The ElasticSearch database is a distributed, highly scalable, and highly real-time search and data analysis engine. It conveniently enables the search, analysis, and exploration of large amounts of data. Leveraging the horizontal scalability of the ElasticSearch database can make data more valuable in production environments. Exemplarily, the implementation principle of the ElasticSearch database may include the following steps: First, the user submits data to the ElasticSearch database. The corresponding sentence is then segmented by a segmentation controller, and its weights and segmentation results are stored in the database. When the user searches the data, the results are ranked and scored based on the weights, and the returned results are presented to the user. Thus, in step 400, the search capabilities provided by the Elasticsearch database, combined with some string-level search algorithms, can be used to quickly retrieve K candidate sub-segments from the custom search library. Here, string-level search algorithms may include the BM25 search algorithm, etc., and this search algorithm is not limited here. The BM25 search algorithm, which stands for Best Matching 25, is an algorithm used to evaluate the relevance between search content and documents during information retrieval. The principles of this search algorithm will not be elaborated on here. Of course, in addition to the ElasticSearch database, other types of databases can also be used to implement the custom search library in this embodiment. In this embodiment, based on the search capabilities provided by these databases and combined with some string-level search algorithms, the aforementioned step 400 can be implemented smoothly. Therefore, this embodiment does not limit the implementation of the custom search library.

[0082] It should be understood that in step 400, the K candidate sub-segments are optimized based on string similarity. The value of K can be flexibly adjusted as needed. Optionally, the value of K may be dynamically determined according to the actual code length, and further examples are not given here.

[0083] Referring to FIG. 4 , in step 401, vector similarities between the unfinished code and the K candidate sub-segments can be calculated separately. First, in step 401, further evaluating the similarity between the K candidate sub-segments and the unfinished code from the perspective of vector similarity can more reasonably select the final target sub-segment. Second, in step 401, only the vector similarities between the K candidate sub-segments and the unfinished code need to be calculated, rather than the vector similarities between all sub-segments in the custom search library and the unfinished code, effectively reducing the computational effort.

[0084] In step 401, the MPNet model or the like may be used to respectively calculate the vector similarities between the unfinished code and the K candidate sub-segments. The algorithm model and algorithm principle used in calculating the vector similarity are not limited herein, and an appropriate algorithm model may be selected as needed in the embodiments.

[0085]

[0085] Based on this, in step 402, the K candidate sub-segments may be sorted based on the string similarity and the vector similarity to select the target sub-segment.

[0086]

[0086] In step 402, a preferred sorting scheme may be: recording the first ranking corresponding to the K candidate sub-segments after sorting according to string similarity; recording the second ranking corresponding to the K candidate sub-segments after sorting according to vector similarity; for a single candidate sub-segment, performing a weighted combination of the corresponding first and second rankings to obtain a final ranking corresponding to the single candidate sub-segment; and sorting the K candidate sub-segments according to their respective final rankings to select the target sub-segment.

[0087] In this preferred ranking scheme, the string similarity and vector similarity are normalized. Thus, the two similarities influence the final ranking of the K candidate sub-segments according to a unified dimension. This effectively avoids the problem of unreasonable ranking caused by the inconsistent dimensions of the two similarities, thereby ensuring the accuracy and rationality of the selected target sub-segments, thereby improving the quality of code generation.

[0088] In summary, this embodiment provides an optimized solution for the search phase. Based on a custom search library, K candidate sub-segments can be initially screened based on string similarity. These initially screened candidate sub-segments are then further evaluated based on vector similarity. In this way, the similarity between the K candidate sub-segments and the unfinished code can be comprehensively evaluated based on both string similarity and vector similarity, thereby selecting a suitable target sub-segment. This not only ensures the accuracy and rationality of the selected target sub-segment, but also reduces the computational and search workload, effectively improving the search quality and efficiency during the search phase.

[0089] FIG5 is a logical diagram of an exemplary code generation solution in an exemplary application scenario provided by an exemplary embodiment of the present disclosure. In this exemplary application scenario, a user may enter a section of code (i.e., unfinished code) into a code compiler. By triggering a submit control (e.g., clicking a submit button), a code generation instruction may be generated. The code generation instruction may include the unfinished code.

[0090] With reference to Figure 5, this code generation instruction can arrive the code generation device that the present embodiment provides.With reference to Figure 5, the upper left corner shows the unfinished code carried in the code generation instruction.The following three stages are presented in this exemplary application scenario The code generation scheme that the present embodiment provides:

[0091] (1) Search library construction stage

[0092]

[0091] During the retrieval library construction phase, a high-quality sub-segment level code retrieval library can be constructed based on the abstract syntax tree and manually designed rules.

[0093]

[0092] First, the open source code base can be cleaned to filter out code snippets that do not contain valid methods (for example, unit test code, code that does not include method implementation, etc.) and code snippets that cannot be parsed into abstract syntax trees.

[0094] Next, the method body is extracted from the code snippet based on the abstract syntax tree, and the user-defined variable names and constants in the method are normalized into a unified special identifier; and the segmentation length is determined based on the distribution of the number of tokens in the method-level code (i.e., the method body).

[0095] Finally, the method-level code is segmented into multiple sub-segments within a specified length range (i.e., less than or equal to the segmentation length determined above), and the order relationship between the sub-segments segmented from the same code segment (i.e., a single method body) is marked, thereby constructing a custom search library (such as the segment-level search library shown in FIG5 ). The custom search library is then stored in the ElasticSearch database.

[0096] (2) Retrieval and sorting stage

[0097]

[0095] In the search and sorting stage, a sorting algorithm can be used to obtain the code fragment that best matches the unfinished code.

[0098]

[0096] First, the top K candidate sub-segments can be retrieved from the custom search library based on the string-level BM25 search algorithm. The K candidate sub-segments can also be roughly sorted according to string similarity using the Ratcliff algorithm, and the rank of each candidate sub-segment can be recorded.

[0099] Next, the MPNet model is used to calculate vector similarities between the K candidate sub-segments and the unfinished code. The K candidate sub-segments are then ranked according to the vector similarities, and the rank of each candidate sub-segment is recorded. Each candidate sub-segment thus has two ranks. Based on the two ranks of each candidate sub-segment, an inverse rank reordering is performed to ultimately obtain the target sub-segment with the highest match to the unfinished code (see the retrieved segment in FIG5 ).

[0100] Finally, based on the aforementioned recorded “sequence relationship,” the code context of the target sub-segment is obtained. The target sub-segment and the code context are then concatenated to obtain the search result (refer to the search result code in FIG. 5 , which contains the following information).

[0101] (3) Prompt generation stage

[0102]

[0099] In the prompt generation stage, prompt information can be constructed based on the search results and prompt template to guide the model to generate code that meets the context specifications.

[0103] The MPNet model can be used to calculate the similarity between the unfinished code and the search results. A filtering threshold can also be set, and search results with a similarity higher than the filtering threshold are added to a pre-designed prompt template, which is then concatenated with the code to be completed and input into a code generation model, such as the CodeGPT model. If the similarity of the search results is lower than the filtering threshold, they can be discarded, and only an empty prompt template is added to the code to be completed and concatenated, and then input into the CodeGPT code generation model.

[0104] In this exemplary application scenario, the result code generated by the code generation model can be returned to the code compiler. If the user sets the code compiler to direct completion mode, the code compiler can directly write code on behalf of the user according to the received result code. If the user sets the code compiler to completion prompt mode, the code compiler can display the received result code as prompt content to the user. After browsing, the user can decide whether to apply the result code to the compilation interface. These can be personalized in the code compiler and will not be described in detail here.

[0105]

[0102] In summary, in this exemplary application scenario, the code generation solution provided in this embodiment can be used to achieve the optimization innovation of the above three stages, so that under the condition of limited computing resources, excellent code generation quality can be achieved by using a small-scale code generation model.

[0106]

[0103] It should be noted that, in some of the processes described in the above embodiments and the accompanying drawings, multiple operations are included that appear in a specific order. However, it should be clearly understood that these operations may not be executed in the order in which they appear in this document or may be executed in parallel. Operation sequence numbers such as 101, 102, etc. are merely used to distinguish between different operations, and the sequence numbers themselves do not represent any execution order. In addition, these processes may include more or fewer operations, and these operations may be executed in sequence or in parallel. It should be noted that the descriptions "first" and "second" herein are used to distinguish different ranking types, etc., and do not represent a sequential order, nor do they limit "first" and "second" to different types.

[0107]

[0104] FIG6 is a schematic diagram of the structure of a computing device provided by another exemplary embodiment of the present disclosure. As shown in FIG6, the computing device includes: a memory 60, a processor 61, and a communication component 62.

[0108]

[0105] The processor 61 is coupled to the memory 60 and is configured to execute a computer program in the memory 60, so as to: in response to a code generation instruction, retrieve a target sub-segment that meets a preset similarity requirement for the unfinished code from a custom search library, where the custom search library includes sub-segments segmented from reference code segments; obtain, for the target sub-segment, a code context corresponding to the target sub-segment in the corresponding reference code segment to generate a search result; and construct prompt information based on the search result and input the prompt information into a code generation model, so that the code generation model generates code for the unfinished code with reference to the prompt information.

[0109]

[0106] In an optional embodiment, when constructing the custom search library, the processor 61 may be specifically configured to: obtain reference code snippets from an open source code library; extract method bodies from the obtained reference code snippets based on abstract syntax tree technology; and divide the extracted method bodies into sub-segments to construct the custom search library.

[0110]

[0107] In an optional embodiment, when the processor 61 divides the extracted method body into sub-segments, it can be specifically used to: if the number of word tokens contained in the target method body exceeds a preset word number threshold, then divide the target method body into multiple sub-segments according to the preset word number threshold, and the number of words contained in a single divided sub-segment does not exceed the preset word number threshold; wherein the target method body is any extracted method body.

[0111]

[0108] In an optional embodiment, when the processor 61 obtains the code context corresponding to the target sub-segment in the corresponding reference code segment to generate the search result, it can be specifically used to: obtain the method body to which the target sub-segment belongs as the search result.

[0112]

[0109] In an optional embodiment, when the processor 61 obtains the reference code snippets from the open source code library, it can be specifically used to: filter out the code snippets in the open source code library that do not contain method bodies and / or cannot be parsed into abstract syntax trees, and use the remaining code snippets as reference code snippets; and normalize the variable names and / or constant names contained in the reference code snippets.

[0113]

[0110] In an optional embodiment, the processor 61 may further be configured to: optimize the unfinished code from the custom search library according to string similarity to determine K candidate sub-segments; respectively calculate vector similarities between the unfinished code and the K candidate sub-segments; and sort the K candidate sub-segments based on string similarity and vector similarity to select the target sub-segment.

[0114]

[0111] In an optional embodiment, when the processor 61 sorts the K candidate sub-segments based on the string similarity and the vector similarity to determine the target sub-segment, it can be specifically configured to: record the first ranking corresponding to the sorting according to the string similarity for each of the K candidate sub-segments; record the second ranking corresponding to the sorting according to the vector similarity for each of the K candidate sub-segments; perform a weighted combination of the corresponding first ranking and second ranking for a single candidate sub-segment to obtain a final ranking corresponding to the single candidate sub-segment; and sort the K candidate sub-segments according to their respective final rankings to select the target sub-segment.

[0115]

[0112] In an optional embodiment, when the processor 61 constructs prompt information based on the retrieval result and inputs the code generation model, it can be specifically used to: perform a quality assessment on the retrieval result; if the retrieval result meets the quality assessment requirements, add the retrieval result to a preset template to generate the prompt information; splice the prompt information and the unfinished code into input information to input the code generation model.

[0116]

[0113] In an optional embodiment, when performing quality assessment on the retrieval result, the processor 61 may be specifically configured to: calculate the vector similarity between the retrieval result and the unfinished code; and determine that the retrieval result meets the quality assessment requirement if the vector similarity exceeds a preset standard.

[0117]

[0114] In an optional embodiment, the processor 61 may further be configured to: if the retrieval result does not meet the quality assessment requirement, use a preset template that does not include the retrieval result as the prompt information.

[0118]

[0115] In an optional embodiment, the preset template includes a search result filling area and a prompt field for separating the search results from the unfinished code.

[0119]

[0116] Further, as shown in FIG6, the computing device further includes: a power supply component 63 and other components. FIG6 only schematically shows some components, which does not mean that the computing device only includes the components shown in FIG6.

[0120]

[0117] It is worth noting that the technical details of the above-mentioned embodiments of the computing device can refer to the relevant descriptions in the aforementioned method embodiments. In order to save space, they will not be repeated here, but this should not cause a loss of the protection scope of this disclosure.

[0121]

[0118] Accordingly, an embodiment of the present disclosure further provides a computer-readable storage medium storing a computer program, which can implement the steps performed in the above method embodiment when the computer program is executed.

[0122]

[0119] Accordingly, the embodiment of the present disclosure also provides a computer program product, which can implement the steps performed in the above method embodiment when the computer program is executed.

[0123]

[0120] The memory in FIG. 6 is used to store computer programs and can be configured to store various other data to support operations on the computing platform. Examples of such data include instructions for any application or method operating on the computing platform, contact data, phone book data, messages, images, videos, etc. The memory can be implemented by any type of volatile or non-volatile storage device, or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk, or optical disk.

[0124]

[0121] The communication component in FIG. 6 is configured to facilitate wired or wireless communication between the device in which the communication component resides and other devices. The device in which the communication component resides can access a wireless network based on a communication standard, such as WiFi, 2G, 3G, 4G / LTE, 5G, or other mobile communication networks, or a combination thereof. In one exemplary embodiment, the communication component receives broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In one exemplary embodiment, the communication component further includes a near-field communication (NFC) module to facilitate short-range communication. For example, the NFC module can be implemented based on radio frequency identification (RFID) technology, infrared data association (IrDA) technology, ultra-wideband (UWB) technology, Bluetooth (BT) technology, or other technologies.

[0125]

[0122] The power supply assembly in FIG. 6 provides power to various components of the device in which the power supply assembly is located. The power supply assembly may include a power management system, one or more power supplies, and other components associated with generating, managing, and distributing power to the device in which the power supply assembly is located.

[0126]

[0123] Those skilled in the art will appreciate that embodiments of the present disclosure may be provided as methods, systems, or computer program products. Therefore, the present disclosure may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, the present disclosure may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0127] The present disclosure is described with reference to the flow chart and / or block diagram of the method, device (system), and computer program product according to the embodiment of the present disclosure. It should be understood that each flow process and / or box in the flow chart and / or block diagram and the combination of the flow process and / or box in the flow chart and / or block diagram can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device produce a device for implementing the function specified in one or more flow processes in the flow chart and / or one or more boxes in the block diagram.

[0128]

[0125] These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce a manufactured product including an instruction device that implements the functions specified in one or more processes in the flowchart and / or one or more boxes in the block diagram.

[0129]

[0126] These computer program instructions may also be loaded onto a computer or other programmable data processing device so that a series of operating steps are performed on the computer or other programmable device to produce a computer-implemented process, whereby the instructions executed on the computer or other programmable device provide steps for implementing the functions specified in one or more processes in the flowchart and / or one or more boxes in the block diagram.

[0130]

[0127] It should also be noted that the terms "comprise," "include," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus comprising a series of elements includes not only those elements but also other elements not explicitly listed, or elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprising a..." does not preclude the presence of other identical elements in the process, method, commodity, or apparatus comprising the element.

[0131]

[0128] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this disclosure are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and corresponding operation portals are provided for users to choose to authorize or refuse.

[0132] The above description is merely an embodiment of the present disclosure and is not intended to limit the present disclosure. It will be apparent to those skilled in the art that various modifications and variations may be made to the present disclosure. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present disclosure shall be included within the scope of protection of the present disclosure.

Claims

Claims 1. A code generation method, comprising: In response to the code generation instruction, searching for a target sub-segment that meets a preset similarity requirement for the unfinished code from a custom search library containing sub-segments segmented from a reference code segment; obtaining a code context corresponding to the target sub-segment in the corresponding reference code segment to generate a search result; Prompt information is constructed based on the search result and input into a code generation model, so that the code generation model generates code for the unfinished code with reference to the prompt information.

2. The method according to claim 1, wherein: The process of constructing the custom search library includes: obtaining reference code snippets from an open source code library; extracting method bodies from the obtained reference code snippets based on abstract syntax tree technology; and dividing the extracted method bodies into sub-segments to construct the custom search library.

3. The method according to claim 2, wherein: The extracted method body is divided into sub-segments, including: if the number of word tokens contained in the target method body exceeds a preset word number threshold, dividing the target method body into multiple sub-segments according to the preset word number threshold, and the number of words contained in each of the divided sub-segments does not exceed the preset word number threshold; wherein the target method body is any extracted method body.

4. The method according to claim 2, wherein: For the target sub-segment, obtaining a code context corresponding to the target sub-segment in the corresponding reference code segment to generate a search result includes: obtaining a method body to which the target sub-segment belongs as the search result.

5. The method according to claim 2, wherein: Obtaining reference code snippets from an open source code library includes: filtering out code snippets in the open source code library that do not contain method bodies and / or cannot be parsed into abstract syntax trees, and using the remaining code snippets as reference code snippets; and normalizing variable names and / or constant names contained in the reference code snippets.

6. The method according to claim 1, further comprising: According to the string similarity, optimizing the unfinished code from the custom search library to determine K candidate sub-segments; Calculating vector similarities between the unfinished code and the K candidate sub-segments respectively; Based on the string similarity and the vector similarity, the K candidate sub-segments are sorted to select the target sub-segment.

7. The method according to claim 6, wherein: Based on string similarity and vector similarity, Sorting K candidate sub-segments to determine the target sub-segment includes: recording the first ranking corresponding to the K candidate sub-segments after sorting according to string similarity; recording the second ranking corresponding to the K candidate sub-segments after sorting according to vector similarity; performing a weighted combination of the corresponding first ranking and second ranking for a single candidate sub-segment to obtain a final ranking corresponding to the single candidate sub-segment; and sorting the K candidate sub-segments according to their respective final rankings to select the target sub-segment.

8. The method according to claim 1, wherein: Constructing prompt information based on the search results and inputting it into a code generation model includes: performing a quality assessment on the search results; if the search results meet the quality assessment requirements, adding the search results to a preset template to generate the prompt information; and splicing the prompt information and the unfinished code into input information to input into the code generation model.

9. The method according to claim 8, wherein: Performing a quality assessment on the search result includes: calculating a vector similarity between the search result and the unfinished code; and determining that the search result meets a quality assessment requirement if the vector similarity exceeds a preset standard.

10. The method according to claim 8, further comprising: If the search result does not meet the quality assessment requirement, a preset template that does not include the search result is used as the prompt information.

11. The method according to claim 8, wherein: The preset template includes a search result filling area and a prompt field for isolating the search results from the unfinished code.

12. A computing device comprising a memory, a processor, and a communication component; wherein: The memory is used to store one or more computer instructions; the processor is coupled to the memory and the communication component, and is used to execute the one or more computer instructions, so as to execute the code generation method according to any one of claims 1 to 11.

13. A computer-readable storage medium storing a computer program, wherein: When the computer program is executed by one or more processors, the one or more processors are caused to execute the code generation method according to any one of claims 1 to 11.

14. A computer program product comprising a computer program / instructions, wherein: When the computer program is executed by a processor, the processor is caused to implement the code generation method according to any one of claims 1 to 11.

Citation Information

Patent Citations

  • Code annotation generation method and device

    CN116661855A

  • Example library construction method, example library use method and generated example code evaluation method

    CN117193733A

  • Hybrid front-end framework migration method based on AST and LLM

    CN117608656A

Cited By

  • Self-adaptive code processing method and device based on artificial intelligence and intelligent agent

    CN120909565A

  • Code adoption rate determination method and device, medium, electronic equipment and product

    CN121050713A

  • Migration operator generation method and device based on heterogeneous architecture and medium

    CN121935035A