Code generation method and device and storage medium
By building a custom retrieval library and obtaining code context as input into the code generation model, the problem of poor code generation quality under limited computing resources is solved and efficient code generation is achieved.
Patent Information
- Application Number
- CN202410289015.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-13
- Publication Date
- 2025-09-16
AI Technical Summary
With limited computing resources, the code generation quality of existing large language models is poor and cannot effectively improve code writing efficiency.
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, obtain their code context, and build a prompt information input code generation model based on the retrieval results.
With limited computing resources, the quality of code generation is improved, comparable to the generation effect of large language models, and code writing efficiency is improved.
Smart Images

Figure CN120653230A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a code generation method, device, and storage medium. Background Art
[0002] For programmers, writing high-quality and efficient code is an important task in the software development process. With the increasing complexity and code size of software development systems, programmers often face the challenge of writing tedious and lengthy code.
[0003] Currently, some large language models (LLMs) offer code generation capabilities. In this approach, unfinished code is fed into the LLM, which then completes it and generates the code. The generated code is then provided to programmers as a reference, effectively improving coding efficiency.
[0004] Since code generation technology based on large language models has high requirements for computing resources (such as GPUs), when computing resources are limited, large language models cannot be used and only smaller-scale models can be used for code generation, resulting in poor code generation quality. Summary of the Invention
[0005] Various aspects of the present application provide a code generation method, device, and storage medium to improve the quality of code generation when computing resources are limited.
[0006] The present invention provides a method for generating a code, including:
[0007] 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, wherein the custom search library includes sub-segments segmented from the reference code segment;
[0008] 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;
[0009] 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.
[0010] Furthermore, the process of constructing the custom search library may include:
[0011] Get reference code snippets from open source repositories;
[0012] Extract method bodies from the obtained reference code snippets based on abstract syntax tree technology;
[0013] The extracted method body is divided into sub-segments to construct the custom search library.
[0014] Furthermore, the extracted method body is divided into sub-segments, which may include:
[0015] If the number of tokens contained in the target method body exceeds a preset token number threshold, the target method body is divided into multiple sub-segments according to the preset token number threshold, and the number of tokens contained in each of the divided sub-segments does not exceed the preset token number threshold;
[0016] The target method body is any extracted method body.
[0017] Furthermore, obtaining a code context corresponding to the target sub-segment in the corresponding reference code segment to generate a search result may include:
[0018] The method body to which the target sub-segment belongs is obtained as the search result.
[0019] Furthermore, reference code snippets can be obtained from open source code repositories, including:
[0020] After removing code snippets in the open source code library 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;
[0021] Normalize variable and / or constant names contained in reference code snippets.
[0022] Furthermore, the method may further include:
[0023] According to the string similarity, optimizing the unfinished code from the custom search library to determine K candidate sub-segments;
[0024] Calculating vector similarities between the unfinished code and the K candidate sub-segments respectively;
[0025] Based on the string similarity and the vector similarity, the K candidate sub-segments are sorted to select the target sub-segment.
[0026] Furthermore, sorting the K candidate sub-segments based on the string similarity and the vector similarity to determine the target sub-segment may include:
[0027] Record the first ranking corresponding to the K candidate sub-segments after being sorted according to string similarity;
[0028] Record the second rankings corresponding to the K candidate sub-segments after being sorted according to vector similarity;
[0029] Under a single candidate sub-segment, weighted merging of the corresponding first and second rankings is performed to obtain the final ranking corresponding to the single candidate sub-segment;
[0030] The K candidate sub-segments are sorted according to their respective final rankings to select the target sub-segment.
[0031] Furthermore, constructing prompt information based on the search results and inputting a code generation model may include:
[0032] Performing a quality assessment on the search results;
[0033] If the search result meets the quality assessment requirements, the search result is added to the preset template to generate the prompt information;
[0034] The prompt information and the unfinished code are spliced into input information to input into the code generation model.
[0035] Furthermore, performing a quality assessment on the search results may include:
[0036] Calculating the vector similarity between the search result and the unfinished code;
[0037] If the vector similarity exceeds a preset standard, it is determined that the search result meets the quality assessment requirement.
[0038] Furthermore, the method may further include:
[0039] 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.
[0040] Furthermore, the preset template includes a search result filling area and a prompt field for separating the search results from the unfinished code.
[0041] The present application also provides a computing device that may include a memory, a processor, and a communication component;
[0042] The memory is used to store one or more computer instructions;
[0043] The processor is coupled to the memory and the communication component, and is configured to execute the one or more computer instructions to perform the aforementioned code generation method.
[0044] An embodiment of the present application 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.
[0045] The embodiments of the present application 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.
[0046] In an embodiment of the present application, a new code generation scheme is proposed, which can build a custom search library, which contains sub-segments cut from reference code fragments. Based on this, the target sub-segments that meet the preset similarity requirements can be retrieved from the custom search library for the unfinished code, and then the code context of the target sub-segment can be obtained to generate the search results. Since 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, which 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 the code generation model. In this way, when computing resources are limited, a model of appropriate size can be used to undertake the code generation work, and the code generation quality of the model can be improved by inputting better prompt information into the model. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0048] Figure 1 A flowchart of a code generation method provided by an exemplary embodiment of the present application;
[0049] Figure 2 A logic diagram of a code generation method provided by an exemplary embodiment of the present application;
[0050] Figure 3 A flowchart of an exemplary solution for building a custom search library provided in an exemplary embodiment of the present application;
[0051] Figure 4 A flowchart of an exemplary search solution provided for an exemplary embodiment of the present application;
[0052] Figure 5 A logical diagram of an exemplary code generation solution in an exemplary application scenario provided by an exemplary embodiment of the present application;
[0053] Figure 6 A schematic structural diagram of a computing device provided as another exemplary embodiment of the present application. DETAILED DESCRIPTION
[0054] To make the purpose, technical solutions, and advantages of this application more clear, the technical solutions of this application will be clearly and completely described below in conjunction with the specific embodiments of this application and the corresponding drawings. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0055] Before describing in detail the technical solutions provided by the embodiments of the present application, some of the technical concepts involved in the present application are briefly described as follows.
[0056] Code generation, also known as code completion, is the process of adding context to an unfinished piece of code to generate a more complete code. For example, a user only needs to enter 5 lines of code, and code completion can generate 100 lines of code context for these 5 lines, eliminating the need for the user to manually enter the context. Of course, these numbers are for illustrative purposes only. Clearly, code generation can significantly improve user coding efficiency.
[0057] A code generation model refers to a neural network model or machine learning model with code completion capabilities. This model can be a large language model or a smaller model. The scale here can refer to parameters or layers. Smaller models generally require fewer computing resources.
[0058] During their research, the inventors discovered that while large language models can provide good code generation quality, achieving this quality requires a parameter scale of billions or even tens of billions. At this scale, large language models consume a significant amount of computing resources. This makes it difficult to use large language models for code generation when computing resources are limited.
[0059] To this end, the embodiments of the present application propose an innovative code generation solution that can be applied when computing resources are limited and can ensure the quality of code generation.
[0060] The following describes in detail the technical solutions provided by various embodiments of the present application in conjunction with the accompanying drawings.
[0061] Figure 1 A flowchart of a code generation method provided by an exemplary embodiment of the present application is provided. Figure 2 This is a logic diagram of a code generation method provided by an exemplary embodiment of the present application. The method can be executed by a code generation device, which can be implemented as software, hardware, or a combination of software and hardware. The code generation device can be integrated into a computing device. Figure 1, the method may include:
[0062] Step 100: In response to the 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 includes sub-segments segmented from a reference code segment;
[0063] Step 101: Obtain the code context corresponding to the target sub-segment in the corresponding reference code segment to generate a search result;
[0064] Step 102: construct prompt information based on the search result 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 unfinished code.
[0065] 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 the outside world. This embodiment also does not limit the product form of the code generation device.
[0066] refer to Figure 1 , in step 100, in response to the code generation instruction, unfinished code can be obtained. Among them, the unfinished code can be understood as the code that needs to be completed. The unfinished code is usually provided by the user (such as a programmer, etc.). In actual applications, it can be a few lines of code entered by the user. In terms of data form, the unfinished code obtained in step 100 is a character string. Here, the unfinished code can be carried in the code generation instruction so that the code generation device in this embodiment can parse the unfinished code from the code generation instruction. In step 100, this embodiment also proposes a custom retrieval library and describes the content contained in the custom retrieval library. As mentioned above, the custom retrieval library in this embodiment contains sub-segments cut out from the reference code fragment.
[0067] 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. For another example, it 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 form, the reference code snippet is usually 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. Of course, this embodiment is not limited to this.
[0068] Based on this, this embodiment proposes that sub-segments can be segmented from a reference code snippet and a custom search library constructed based on these sub-segments. During research, the inventors discovered that by segmenting, 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 comparisons 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 the unfinished code they provide being significantly different from the reference code snippet overall. The sub-segment-level custom search library proposed in this embodiment effectively increases code diversity, allowing even unfinished code corresponding to code variants to generate high-quality search results within the custom search library of this embodiment. Code variants can be understood as codes generated by modifying regular code. Obviously, code variants generally have a lower degree of similarity to the reference code snippet. Accordingly, the unfinished code entered by users for code variants will also have a lower degree of similarity to the reference code snippet overall.
[0069] 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 fragment, the similarity between the unfinished code and each reference code fragment 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 for the unfinished code, the sub-segment corresponding to lines 1-5 in the custom search library can be hit based on the code lines 1-5 in the unfinished code. It can be seen that when searching using a sub-segment as the search unit, it will not be affected by other sub-segments in the reference code fragment to which it belongs. Therefore, even if the unfinished code contains unconventional code, the appropriate sub-segment can be accurately hit for the unfinished code.
[0070] In this embodiment, in step 100, the unfinished code can be optimized in the custom search library to find target sub-segments that meet the 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, but will not be described in detail here.
[0071] It is worth noting that in this embodiment, the sub-segments are stored as strings in the custom search library. Moreover, in this embodiment, only this one search library is needed 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.
[0072] Continue to refer Figure 1 In step 101, the code context corresponding to the target sub-segment in the corresponding reference code segment can be obtained to generate search results. As previously mentioned, 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.
[0073] In an exemplary embodiment, during the aforementioned segmentation of the reference code fragment, the sequential 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 sequential relationship, the preceding and / or subsequent sub-segments of the target sub-segment within the corresponding reference code fragment can be determined, thereby obtaining the code context corresponding to the target sub-segment.
[0074] 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.
[0075] The retrieval results generated by searching based on sub-segments are obviously of higher quality than the retrieval results that can be obtained by directly searching the reference code snippets.
[0076] 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 to generate code for the unfinished code.
[0077] It is worth noting that this embodiment abandons the large language model and instead uses 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, the high-quality prompt information provided by this embodiment effectively improves the code generation quality of this code generation model. During research, 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.
[0078] In this embodiment, the code generation logic in the code generation model is not limited. The general principle of the code generation model in this embodiment is to extract features from the prompt information as additional context information, and reference this context information to write code context for the unfinished code to generate output results. This is merely exemplary, and this embodiment is not limited to this.
[0079] During their research, the inventors discovered that high-quality prompt information can effectively improve the code generation quality of the code generation model. In a preferred implementation, they further propose: performing a quality assessment on the search results; if the search results meet the quality assessment requirements, then providing the search results to the code generation model through prompt information; otherwise, the search results are no longer provided to the code generation model.
[0080] In this way, it can be ensured that only high-quality retrieval results are provided to the code generation model, while retrieval 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.
[0081] In an exemplary quality assessment scheme, the vector similarity between the retrieval results and the unfinished code can be calculated; if the vector similarity exceeds a preset standard, the retrieval results are determined to meet the quality assessment requirements. Among them, the MPNet model can be used to calculate the vector similarity between the retrieval results and the unfinished code. MPNet: is a language embedding model trained with source code, which is used to convert code in string form into numerical vector form, and can calculate vector similarity through vector comparison. Among them, the preset standard here can be set as needed. In this way, by performing a quality assessment on the retrieval results, the retrieval results with vector similarity lower than the preset standard can be discarded to prevent these retrieval results from misleading the code generation model.
[0082] It should be understood that, for the code generation model, even if the search results provided in this embodiment are not obtained, it can still complete code generation for the unfinished code through its 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.
[0083] In a further optimization scheme, this embodiment also proposes: if the retrieval results meet the quality assessment requirements, the retrieval results can be added to the preset template to generate prompt information; and if the retrieval results do not meet the quality assessment requirements, the preset template that does not contain the retrieval results 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 from confusing the prompt information with the unfinished code. Preferably, the preset template may include a retrieval result filling area and a prompt field for separating the retrieval results 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 may be empty. In addition, the prompt field in the preset template may 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 prompt information. 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:
[0084] <aug>< / aug>
[0085] Search results filling area
[0086] <aug>< / aug>
[0087] In this way, the prompt 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 to perform augmentation on the unfinished code along with this partial information. It should be understood that this is merely exemplary and the present embodiment is not limited thereto.
[0088] refer to Figure 2 In step 102, the prompt information and the unfinished code can be combined into input information for input into the code generation model. The aforementioned preset template can be used to separate the search results from the unfinished code in the prompt information, allowing 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.
[0089] In summary, this embodiment proposes a new code generation solution that can construct a custom search library containing sub-segments cut from reference code snippets. Based on this, the target sub-segments that meet 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 search results. 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 the quality of the search results. Based on the generated search results, prompt information can be constructed and input into the code generation model. In this way, when computing resources are limited, a model of appropriate size can be used to undertake the code generation work, and the code generation quality of the model can be improved by inputting better prompt information into the model.
[0090] In the above or below embodiments, a variety of construction schemes can be used to construct a custom search library. Figure 3 A flowchart of an exemplary custom search library construction solution provided by an exemplary embodiment of the present application is provided. Figure 3 , this exemplary construction scheme may include:
[0091] Step 300: Obtain reference code snippets from an open source code library;
[0092] Step 301: Extract the method body from the obtained reference code snippet based on the abstract syntax tree technology;
[0093] Step 302: Divide the extracted method body into sub-segments to construct a custom search library.
[0094] Among them, the open source code base can be a relatively comprehensive code base that is self-built or provided by 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 be first 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, and 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 the form of a tree, 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 information. However, two concepts in the abstract syntax tree used in this embodiment are explained here:
[0095] The first is the method body. The method body is the block of statements following the method declaration. It's the code area used to implement a specific function. 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 in a pair of curly braces {}. The method body is usually preceded by a series of introductory characters, such as `public`, `private`, and `static`, indicating the method's type and access rights.
[0096] The other is the 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 makes up a source program. Abstract syntax tree technology includes a lexical parsing step, which parses all tokens contained in the code.
[0097] 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 can also be normalized. During the research process, the inventors discovered that the filtered reference code snippets may contain some variable names and / or constant names customized by the code author. 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 in all reference code snippets remain consistent, and the constant names corresponding to the same constant in all reference code snippets remain consistent. This can effectively improve the accuracy of the search process and avoid missing the appropriate sub-segment due to variable names and / or constant names.
[0098] Continue to refer Figure 3 In step 301, the method body can be extracted from the reference code snippet obtained in step 300 based on the abstract syntax tree technology. Following the previous explanation of the method body, it can be seen that in step 301, the reference code snippet is actually segmented into one or more method bodies.
[0099] In step 302, the extracted method body is segmented into sub-segments to construct a custom search library. Specifically, 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 of the sub-segments extracted from a single method body can also be recorded.
[0100] During the research, the inventors found that in step 302, the quality of code generation caused by different segmentation lengths was different. For this reason, a preferred segmentation method is proposed in this exemplary construction scheme: a suitable word number threshold can be preset. In step 302, if the number of word tokens contained in the target method body exceeds the preset word number threshold, the target method body is segmented into multiple sub-segments according to the preset word number threshold, and the number of words contained in each sub-segment does not exceed the preset word number threshold. Among them, the target method body can be any method body extracted in step 301. In this preferred segmentation method, a unified segmentation length (that is, the preset word number threshold) is used to segment each method body extracted in step 301. When the length of the method body is greater than the segmentation length, the method body will be segmented into multiple sub-segments, and 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-segments segmented under this preferred segmentation method will not exceed the segmentation length.
[0101] In this preferred segmentation method, multiple word quantity thresholds can be enumerated, and under each enumerated word quantity threshold, the code generation quality can be tested according to the code generation scheme provided in this embodiment. Then, based on the tested code generation quality, the final word data threshold is optimized from the enumerated multiple word quantity thresholds as the word data threshold used in this preferred segmentation method.
[0102] 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 order relationship between the sub-segments contained in the method body as a unit. This can provide a rich, diverse, and concise search basis for subsequent search steps.
[0103] exist Figure 3 Based on the exemplary construction scheme shown in FIG, in this embodiment, in the search link (such as Figure 1 In step 102), 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 contains sub-segments located before the target sub-segment, these sub-segments can also be placed in the search results. The inventors found in the research process that these sub-segments can usually bring positive prompt information to the code generation model, which helps to improve the quality of code generation.
[0104] It should be understood that, in this embodiment, in addition to the above Figure 3In addition to the exemplary construction scheme shown, other construction schemes can also be adopted in this embodiment. For example, different segmentation lengths can be used for different types of method bodies, or reference code fragments can be segmented according to manual experience, etc. Other exemplary construction schemes are not described in detail here.
[0105] In summary, in this embodiment, an optimization scheme is provided for the construction stage of the custom retrieval library. The quality of the reference code snippets is improved through preprocessing operations such as cleaning and normalization. By adopting a reasonable segmentation scheme, the reference code snippets can be divided into sub-segments of appropriate lengths. Sub-segments of appropriate lengths can effectively improve the quality of the retrieval results, thereby improving the code generation quality of the code generation model.
[0106] In the above or below embodiments, multiple implementations may be used to retrieve the target sub-segment. Figure 4 A flowchart of an exemplary search solution provided for an exemplary embodiment of the present application. Figure 4 , this exemplary search scheme may include:
[0107] Step 400: Optimize the unfinished code from the custom search library according to the string similarity to determine K candidate sub-segments;
[0108] Step 401: Calculate the vector similarities between the unfinished code and the K candidate sub-segments respectively;
[0109] Step 402: Sort the K candidate sub-segments based on the string similarity and the vector similarity to select the target sub-segment.
[0110] refer to Figure 4As 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 particular, in step 400, the custom search library can preferably be implemented using the ElasticSearch database. The Elasticsearch database is a distributed, highly scalable, and highly real-time search and data analysis engine. It can easily enable the search, analysis, and exploration of large amounts of data. Taking full advantage of the horizontal scalability of the Elasticsearch database can make data more valuable in a production environment. Exemplarily, the implementation principle of the Elasticsearch database may include the following steps: first, the user submits data to the Elasticsearch database, then the corresponding sentence is segmented by the word segmentation controller, and its weight and the word segmentation result are stored in the data. When the user searches the data, the results are ranked and scored according to the weight, and the returned results are presented to the user. In this way, in step 400, the retrieval capability provided by the Elasticsearch database can be used in combination with some string-level search algorithms to quickly retrieve K candidate sub-fragments from the custom retrieval library. Here, the string-level search algorithm may include the BM25 search algorithm, etc., and the search algorithm is not limited here. Among them, the BM25 search algorithm: the full English name is Best Matching 25, which is an algorithm used to evaluate the relevance between search content and documents during information retrieval. The principle of this search algorithm will not be elaborated here. Of course, in addition to the ElasticSearch database, other types of databases can also be used in this embodiment to implement the custom retrieval library. In this embodiment, based on the retrieval capabilities provided by these databases, combined with some string-level search algorithms, the aforementioned step 400 can be implemented without obstacles. Therefore, in this embodiment, the implementation form of the custom retrieval library is not limited.
[0111] It should be understood that in step 400, K candidate sub-segments are selected based on string similarity. The value of K can be flexibly adjusted as needed. Alternatively, the value of K can be dynamically determined based on the actual code length. Further examples are not provided here.
[0112] refer to Figure 4In step 401, the vector similarities between the unfinished code and the K candidate sub-segments can be calculated separately. First, in step 401, the similarity between the K candidate sub-segments and the unfinished code is further evaluated from the perspective of vector similarity, which 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, which effectively reduces the amount of calculation.
[0113] In step 401, the MPNet model or the like may be used to 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 the embodiments may select a suitable algorithm model as needed.
[0114] 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.
[0115] 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; under a single candidate sub-segment, performing a weighted combination of the corresponding first ranking and second ranking to obtain the 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.
[0116] This preferred ranking scheme normalizes string and vector similarity, so that both similarities influence the final ranking of the K candidate sub-segments using a unified dimension. This effectively avoids illogical rankings caused by inconsistent dimensions between the two similarities, ensuring the accuracy and rationality of the selected target sub-segments and improving code generation quality.
[0117] In summary, this embodiment provides an optimized solution for the search phase. Based on a custom search library, K candidate sub-segments are initially screened based on string similarity. These pre-screened candidate sub-segments are then further evaluated based on vector similarity. This allows for a comprehensive assessment of the similarity between these K candidate sub-segments and the unfinished code, using both string and vector similarity, to select the appropriate target sub-segment. This approach not only ensures the accuracy and rationality of the selected target sub-segment but also reduces the computational and search effort required, effectively improving retrieval quality and efficiency during the search phase.
[0118] Figure 5This is a logical diagram of an exemplary code generation solution for an exemplary application scenario provided by an exemplary embodiment of the present application. In this exemplary application scenario, a user can enter a section of code (i.e., unfinished code) into the code compiler. By triggering a submit control (e.g., clicking a submit button), a code generation instruction can be generated, and the code generation instruction can carry the unfinished code.
[0119] refer to Figure 5 , the code generation instruction can reach the code generation device provided by this embodiment. Figure 5 , the upper left corner shows the unfinished code carried in the code generation instruction. The following presents the code generation solution provided by this embodiment in this exemplary application scenario in three stages:
[0120] (1) Retrieval library construction stage.
[0121] During the retrieval library construction phase, a high-quality sub-fragment-level code retrieval library can be constructed based on the abstract syntax tree and manually designed rules:
[0122] 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.
[0123] 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 unified special identifiers; 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).
[0124] 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 (e.g., Figure 5 The custom search library is stored in the ElasticSearch database.
[0125] (2) Retrieval and sorting stage.
[0126] In the retrieval and sorting stage, a sorting algorithm can be used to obtain the code snippet that best matches the unfinished code:
[0127] First, based on the string-level BM25 search algorithm, the top K candidate sub-segments can be retrieved from the custom retrieval library; the Ratcliff algorithm can also be used to roughly sort the K candidate sub-segments according to string similarity, and the rank of each candidate sub-segment can be recorded.
[0128] Next, the MPNet model is used to calculate the vector similarity between the K candidate sub-segments and the unfinished code, and the K candidate sub-segments are sorted according to the vector similarity, and the rank of each candidate sub-segment is recorded. In this way, each candidate sub-segment will have two ranks. Based on the two ranks of each candidate sub-segment, the inverse rank rearrangement is performed, and finally, the target sub-segment with the highest matching degree with the unfinished code can be obtained (refer to Figure 5 ).
[0129] Finally, based on the "sequence relationship" of the above records, the code context of the target sub-segment is obtained. The target sub-segment and the code context are spliced together to obtain the search results (refer to Figure 5 The search result code contains the information below).
[0130] (3) Prompt generation stage.
[0131] During the prompt generation phase, prompt information can be constructed based on the search results and prompt templates to guide the model to generate code that meets the context specifications:
[0132] 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 the 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 and concatenated with the code to be completed before being input into the CodeGPT code generation model.
[0133] 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 a prompt to the user. After browsing, the user can decide whether to apply the result code to the compilation interface. These can be customized in the code compiler and will not be described in detail here.
[0134] In summary, in this exemplary application scenario, the optimization innovation of the above three stages can be realized based on the code generation solution provided in this embodiment, so that excellent code generation quality can be achieved by using a small-scale code generation model under limited computing resources.
[0135] It should be noted that in some of the processes described in the above embodiments and the accompanying drawings, multiple operations that appear in a specific order are included, but it should be clearly understood that these operations may not be executed in the order in which they appear in this article or may be executed in parallel. The sequence numbers of the operations, such as 101, 102, etc., are only 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 descriptions such as "first" and "second" in this article are used to distinguish different ranking types, etc., and do not represent the order of precedence, nor do they limit "first" and "second" to being different types.
[0136] Figure 6 This is a schematic diagram of a computing device provided by another exemplary embodiment of the present application. Figure 6 As shown, the computing device includes a memory 60 , a processor 61 and a communication component 62 .
[0137] The processor 61 is coupled to the memory 60 and is configured to execute the computer program in the memory 60 to:
[0138] 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, wherein the custom search library includes sub-segments segmented from the reference code segment;
[0139] 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;
[0140] 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.
[0141] In an optional embodiment, when constructing the custom search library, the processor 61 may be specifically configured to:
[0142] Get reference code snippets from open source repositories;
[0143] Extract method bodies from the obtained reference code snippets based on abstract syntax tree technology;
[0144] The extracted method body is divided into sub-segments to construct the custom search library.
[0145] In an optional embodiment, when the processor 61 divides the extracted method body into sub-segments, it may be specifically configured to:
[0146] If the number of tokens contained in the target method body exceeds a preset token number threshold, the target method body is divided into multiple sub-segments according to the preset token number threshold, and the number of tokens contained in each of the divided sub-segments does not exceed the preset token number threshold;
[0147] The target method body is any extracted method body.
[0148] 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 configured to:
[0149] The method body to which the target sub-segment belongs is obtained as the search result.
[0150] In an optional embodiment, when the processor 61 obtains the reference code snippet from the open source code library, it may be specifically configured to:
[0151] After removing code snippets in the open source code library 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;
[0152] Normalize variable and / or constant names contained in reference code snippets.
[0153] In an optional embodiment, the processor 61 may also be configured to:
[0154] According to the string similarity, optimizing the unfinished code from the custom search library to determine K candidate sub-segments;
[0155] Calculating vector similarities between the unfinished code and the K candidate sub-segments respectively;
[0156] Based on the string similarity and the vector similarity, the K candidate sub-segments are sorted to select the target sub-segment.
[0157] 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 may be specifically configured to:
[0158] Record the first ranking corresponding to the K candidate sub-segments after being sorted according to string similarity;
[0159] Record the second rankings corresponding to the K candidate sub-segments after being sorted according to vector similarity;
[0160] Under a single candidate sub-segment, weighted merging of the corresponding first and second rankings is performed to obtain the final ranking corresponding to the single candidate sub-segment;
[0161] The K candidate sub-segments are sorted according to their respective final rankings to select the target sub-segment.
[0162] In an optional embodiment, when the processor 61 constructs prompt information based on the search results and inputs the code generation model, it can be specifically used to:
[0163] Performing a quality assessment on the search results;
[0164] If the search result meets the quality assessment requirements, the search result is added to the preset template to generate the prompt information;
[0165] The prompt information and the unfinished code are spliced into input information to input into the code generation model.
[0166] In an optional embodiment, when performing quality assessment on the search results, the processor 61 may be specifically configured to:
[0167] Calculating the vector similarity between the search result and the unfinished code;
[0168] If the vector similarity exceeds a preset standard, it is determined that the search result meets the quality assessment requirement.
[0169] In an optional embodiment, the processor 61 may also be configured to:
[0170] 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.
[0171] 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.
[0172] Further, if Figure 6 As shown, the computing device also includes: a power supply component 63 and other components. Figure 6 Only some components are shown schematically, and it does not mean that the computing device only includes Figure 6 Components shown.
[0173] It is worth noting that the technical details in the above-mentioned embodiments of the computing device can be referred to the relevant description in the aforementioned method embodiment. In order to save space, they will not be repeated here, but this should not cause any loss of the scope of protection of this application.
[0174] Accordingly, an embodiment of the present application further provides a computer-readable storage medium storing a computer program, which, when executed, can implement the steps performed in the above method embodiment.
[0175] Accordingly, an embodiment of the present application also provides a computer program product, which, when executed, can implement the steps performed in the above method embodiment.
[0176] above Figure 6 The memory in the computing platform 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, pictures, videos, etc. The memory can be implemented by any type of volatile or non-volatile storage device or a combination of them, 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.
[0177] above Figure 6 The communication component in is configured to facilitate wired or wireless communication between the device where the communication component is located and other devices. The device where the communication component is located can access a wireless network based on a communication standard, such as WiFi, 2G, 3G, 4G / LTE, 5G and other mobile communication networks, or a combination thereof. In an exemplary embodiment, the communication component receives a broadcast signal or broadcast-related information from an external broadcast management system via a broadcast channel. In an exemplary embodiment, the communication component also 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 and other technologies.
[0178] above Figure 6 The power supply component in a device provides power to various components of the device in which the power supply component is located. The power supply component 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 component is located.
[0179] Those skilled in the art will appreciate that the embodiments of the present application can be provided as methods, systems, or computer program products. Therefore, the present application can adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application can adopt 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.) that contain computer-usable program code.
[0180] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart 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 generate instructions for implementing the steps in the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0181] 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 work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0182] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0183] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.
[0184] 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 application 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 relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0185] The foregoing is merely an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application should all be included in the protection scope of the present application.
Claims
1. A code generation method, characterized in that: include: 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, wherein the custom search library includes sub-segments segmented from the reference code segment; 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; 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, characterized in that The process of building the custom search library includes: Get reference code snippets from open source repositories; Extract method bodies from the obtained reference code snippets based on abstract syntax tree technology; The extracted method body is divided into sub-segments to construct the custom search library.
3. The method according to claim 2, characterized in that The extracted method body is divided into sub-segments, including: If the number of tokens contained in the target method body exceeds a preset token number threshold, the target method body is divided into multiple sub-segments according to the preset token number threshold, and the number of tokens contained in each of the divided sub-segments does not exceed the preset token number threshold; The target method body is any extracted method body.
4. The method according to claim 2, characterized in that Obtaining the code context corresponding to the target sub-segment in the corresponding reference code segment to generate a search result, including: The method body to which the target sub-segment belongs is obtained as the search result.
5. The method according to claim 2, characterized in that Get reference code snippets from open source repositories, including: After removing code snippets in the open source code library 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; Normalize variable and / or constant names contained in reference code snippets.
6. The method according to claim 1, characterized in that Also includes: 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, characterized in that Sorting the K candidate sub-segments based on string similarity and vector similarity to determine the target sub-segment includes: Record the first ranking corresponding to the K candidate sub-segments after being sorted according to string similarity; Record the second rankings corresponding to the K candidate sub-segments after being sorted according to vector similarity; Under a single candidate sub-segment, weighted merging of the corresponding first and second rankings is performed to obtain the final ranking corresponding to the single candidate sub-segment; The K candidate sub-segments are sorted according to their respective final rankings to select the target sub-segment.
8. The method according to claim 1, characterized in that Constructing prompt information based on the search results and inputting into a code generation model includes: Performing a quality assessment on the search results; If the search result meets the quality assessment requirements, the search result is added to the preset template to generate the prompt information; The prompt information and the unfinished code are spliced into input information to input into the code generation model.
9. The method according to claim 8, characterized in that Performing a quality assessment on the search results, including: Calculating the vector similarity between the search result and the unfinished code; If the vector similarity exceeds a preset standard, it is determined that the search result meets the quality assessment requirement.
10. The method according to claim 8, characterized in that Also includes: 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, characterized in that 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, characterized in that including memory, processor, and communication components; The memory is used to store one or more computer instructions; The processor is coupled to the memory and the communication component, and is configured to execute the one or more computer instructions 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, characterized in that: 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, characterized in that The method comprises a computer program / instruction, 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.