Code generation method and apparatus
By generating code sequences by matching the current question vector in the code vector library and querying the target cache information in the cache sequence, the problem of low cache hit rate and degraded inference quality in retrieval enhancement generation system with long context input is solved, achieving more efficient cache utilization and improved system performance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- LENOVO (BEIJING) LTD
- Filing Date
- 2026-03-26
- Publication Date
- 2026-06-09
AI Technical Summary
Existing retrieval-enhanced generation systems suffer from low cache hit rates and degraded inference quality when handling long contextual inputs, primarily due to their inability to recognize semantically equivalent code fragments, leading to redundant computations and insufficient cache utilization.
By matching the current problem vector in the code vector library, a code sequence is generated, and the target cache information is queried in the cache sequence. The code information is generated using the positional information of the target code, ensuring that the positional relationship between the cache information and the code sequence corresponds, thereby improving the cache hit rate and accuracy.
While ensuring the accuracy of code generation, the cache hit rate was improved, redundant calculations were reduced, and system performance and cache utilization were enhanced.
Smart Images

Figure CN122173613A_ABST
Abstract
Description
Technical Field
[0001] This application relates to electronic information technology, and more particularly to a code generation method and apparatus. Background Technology
[0002] Currently, Retrieval-Augmented Generation (RAG) systems suffer from low cache hit rates and degraded inference quality when handling long contextual inputs. Specifically, caching techniques in related technologies either maintain accuracy while achieving low cache utilization, or improve cache utilization while reducing inference quality. This is because caching techniques in related technologies cannot recognize semantically equivalent code fragments, leading to a large amount of avoidable redundant computation, which limits the improvement of cache utilization and system performance. Summary of the Invention
[0003] In view of this, embodiments of this application provide at least one code generation method and apparatus.
[0004] The technical solution of this application embodiment is implemented as follows: In a first aspect, embodiments of this application provide a code generation method, the method comprising: In response to the user's current query request, determine the current question vector corresponding to the current query request; Based on the current problem vector, a matching operation is performed in the code vector library to obtain a code sequence that includes at least two current code snippets; Based on each current code and its first position information in the code sequence, a query is performed in multiple cache sequences including at least two cache information items to obtain target cache information for the target code in the at least two current codes. The cache sequence is obtained by sorting the cache information according to the degree of matching between each historical code and its corresponding historical query request. Each historical code is obtained by matching the historical query request in a code vector library. The cache information is obtained by pre-filling the historical code. The positional relationship of each target cache information item in the cache sequence corresponds to the positional relationship of each target code in the code sequence. Determine the target position information based on the first position information of the target code in the code sequence; Based on the target cache information of each target code, the code information corresponding to the current query request is generated according to the target position information.
[0005] Secondly, embodiments of this application provide a code generation apparatus, the apparatus comprising: The first determining module is used to determine the current question vector corresponding to the current query request in response to the user's current query request; The matching module is used to perform matching in the code vector library based on the current problem vector to obtain a code sequence that includes at least two current codes; A query module is used to query multiple cache sequences, each including at least two cache information items, based on each current code and the first order information of each current code in the code sequence, to obtain target cache information of the target code in the at least two current codes; wherein, the cache sequence is obtained by sorting each cache information item according to the matching degree between each historical code and the corresponding historical query request; each historical code is obtained by matching the historical query request in a code vector library; the cache information is obtained by pre-filling the historical code; the positional relationship of each target cache information item in the cache sequence corresponds to the positional relationship of each target code in the code sequence; The second determining module is used to determine the target position information according to the first position information of the target code in the code sequence; The generation module is used to generate the code information corresponding to the current query request based on the target cache information of each target code and according to the target position information.
[0006] It should be understood that the above general description and the following detailed description are merely exemplary and explanatory, and are not intended to limit the technical solutions of this application. Attached Figure Description
[0007] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with this application and, together with the specification, serve to explain the technical solutions of this application.
[0008] Figure 1 A schematic diagram of the implementation flow of a code generation method provided in this application embodiment. Figure 1 ; Figure 2 A schematic diagram of the implementation flow of a code generation method provided in this application embodiment. Figure 2 ; Figure 3 A schematic diagram of the implementation flow of a code generation method provided in this application embodiment. Figure 3 ; Figure 4 A schematic diagram illustrating the generation of code vectors provided in an embodiment of this application; Figure 5 A schematic diagram illustrating the reasoning process of the code generation model provided in this application embodiment; Figure 6This is a schematic diagram of the composition structure of a code generation device provided in an embodiment of this application; Figure 7 This is a schematic diagram of a hardware entity of the code generation device provided in an embodiment of this application. Detailed Implementation
[0009] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application are further described in detail below with reference to the accompanying drawings and embodiments. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0010] In the following description, references are made to “some embodiments,” which describe a subset of all possible embodiments. However, it is understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0011] The terms “first / second / third” are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that “first / second / third” may be interchanged in a specific order or sequence where permitted, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.
[0012] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application pertains. The terminology used herein is for descriptive purposes only and is not intended to limit the scope of this application.
[0013] Currently, RAG systems suffer from low cache hit rates and degraded inference quality when handling long context inputs. Specifically, caching techniques in related technologies either maintain accuracy while achieving low cache utilization, or improve cache utilization while reducing inference quality. This is because caching techniques in related technologies cannot recognize semantically equivalent code fragments, leading to a large amount of avoidable redundant computation, which limits the improvement of cache utilization and system performance.
[0014] Current research challenges lie in improving cache hit rate under long contextual inputs without sacrificing inference accuracy. Furthermore, RAG caching systems in related technologies exhibit significant limitations in scenarios such as code generation, completion, and understanding. They typically rely on exact / prefix matching of document identifiers (e.g., file paths, code snippet IDs) or text strings to achieve cache reuse. However, software development involves numerous functionally equivalent code patterns with different textual expressions.
[0015] Related technical solutions include: 1. Precise prefix matching methods, such as RadixCache, LMCache, and RAGCache, which have high cache hit rates but low accuracy; 2. Approximate key-value cache matching methods, such as CacheBlend and PromptCache, which improve cache utilization but reduce inference quality.
[0016] To address the technical problems in related technologies, embodiments of this application provide a code generation method. This code generation method can be applied to electronic devices. Exemplarily, the electronic devices include, but are not limited to, smartphones, tablets, wearable devices, personal computers (PCs), netbooks, etc. The implementation form of this application is not fixed or limited. The technical solution of this application will be described in detail below with reference to embodiments, such as... Figure 1 As shown, this code generation method can be implemented through steps S101 to S103: Step S101: In response to the user's current query request, determine the current question vector corresponding to the current query request.
[0017] Here, the current query request is used to instruct the code generation model to generate the corresponding code information.
[0018] In this embodiment, key information can be extracted from the current query request, semantic information of the current query request can be generated based on the key information, and then a current question vector corresponding to the current query request can be generated based on the semantic information. In some embodiments, the semantic information of the current query request can also be directly determined, and then a current question vector corresponding to the current query request can be generated based on the semantic information.
[0019] Step S102: Match the code vectors in the code vector library based on the current problem vector to obtain a code sequence that includes at least two current codes.
[0020] Here, a code sequence refers to a sequence obtained by arranging at least two current codes in a predetermined order. This predetermined order can be determined based on the similarity between the current question vector and the current codes. For example, after matching the current question vector in a code vector library to obtain current code 1, current code 2, and current code 3, if current code 3 has the highest similarity to the current question vector, current code 2 has the lowest similarity, and current code 1 has the lowest similarity, then the code sequence is [current code 3, current code 2, current code 1].
[0021] In this embodiment of the application, the code vector library includes code vectors corresponding to each code. The code vectors are determined based on the attribute information of the code. After determining the current problem vector, the current problem vector can be matched with each code vector in the code vector library, and the vector with similarity that meets the predetermined conditions is determined as the current code.
[0022] Step S103: Based on each current code and the first order information of each current code in the code sequence, query in multiple cache sequences including at least two cache information to obtain the target cache information of the target code in at least two current codes.
[0023] Here, the cache sequence is obtained by sorting each cache information according to the degree of matching between each historical code and the corresponding historical query request; each historical code is obtained by matching historical query requests in the code vector library; and the cache information is obtained by pre-populating the historical code.
[0024] Understandably, for a given historical query request, at the corresponding historical moment, a query must first be performed in the code vector library based on the historical query request to obtain a historical code sequence containing at least two historical codes. This historical code sequence refers to a sequence obtained by arranging at least two historical codes in a predetermined order. Then, when the code generation model generates code corresponding to the historical query request based on the historical code sequence, it needs to pre-fill at least two historical codes sequentially according to the order of the historical codes in the historical code sequence to obtain cache information corresponding to each historical code. The cache information is then sorted according to the order of the historical codes (i.e., the degree of matching between each historical code and the corresponding historical query request) to obtain the cache sequence. In other words, each historical query request corresponds to a cache sequence generated at a historical moment.
[0025] For example, based on historical query requests, a historical code sequence [historical code 3, historical code 2, historical code 1] is obtained from the code vector library. Historical code 3 is pre-populated to obtain cache information 3, historical code 2 is pre-populated to obtain cache information 2, and historical code 1 is pre-populated to obtain cache information 1. The cache sequence [cache information 3, cache information 2, cache information 1] is obtained according to the order of historical code 3, historical code 2, and historical code 1.
[0026] In some embodiments, historical query requests can be requests initiated by users within a preset time range. For example, query requests sent by users within the past 3 days are identified as historical query requests.
[0027] In this embodiment of the application, the first order information refers to the information used to sort each current code. The first order information can represent the degree of matching between each current code and the current problem vector. The smaller the first order information of the current code, the higher the degree of matching between the current code and the current problem vector.
[0028] In this embodiment, the matching status of cached information and current code can be determined based on the second position information of each cached information in the cache sequence and the first position information of each current code in the code sequence. The successfully matched cached information is then identified as the target cached information for the current code. The positional relationship of each target cached information in the cache sequence corresponds to the positional relationship of each target code in the code sequence. It should be noted that each target cached information refers to cached information within the same cache sequence. For example, in the cache sequence [cache information 3, cache information 2, cache information 1], cache information 2 and cache information 1 can serve as target cached information for two target codes in at least two current codes. In this case, the two target codes are matched with the historical codes corresponding to cache information 2 and cache information 1, respectively. In other words, whether cached information and current code match successfully refers to whether the historical code corresponding to the cached information matches the current code. If a match is successful, and the positional relationship of each cached information in the cache sequence corresponds to the positional relationship of each target code in the code sequence, then the cached information is identified as the target cached information for the target code.
[0029] It is understandable that the order of each target cached information in the cache sequence corresponds to the order of each target code in the code sequence. This means that the order of each target cached information in the cache sequence corresponds to the order of each target code in the code sequence. For example, consider a cache sequence of [5, 4, 2, 1] and a code sequence of [6, 5, 1, 4]. Because cached information 5 matches current code 5, and cached information 4 matches current code 4, and the order of cached information 5 and cached information 4 in the cache sequence is cached information 5 first, followed by cached information 4, and the order of current code 5 and current code 4 in the code sequence is also current code 5 first, followed by current code 4, the order of cached information 5 and cached information 4 in the cache sequence corresponds to the order of current code 5 and current code 4 in the code sequence. Therefore, cached information 5 is the target cached information for current code 5, and cached information 4 is the target cached information for current code 4.
[0030] Step S104: Determine the target position information according to the first position information of the target code in the code sequence.
[0031] In this embodiment of the application, it is determined whether the target code is all the code in the code sequence. If so, the target position information is determined according to the first position information of the target code in the code sequence. If the target code is not all the code in the code sequence, the target position information is determined according to the first position information of the target code in the code sequence and the first position information of the other codes in the code sequence.
[0032] For example, in the case of cache sequence [5, 4, 2, 1] and code sequence [6, 5, 1, 4], the target cache hit information is [5, 4]. Therefore, the code that did not hit the cache in [6, 5, 1, 4] includes [6, 1]. So the target bit sequence information is [5, 4, 6, 1].
[0033] Step S105: Based on the target cache information of each target code, generate the code information corresponding to the current query request according to the target position information.
[0034] In this embodiment of the application, if the target code is all the code in the code sequence, then the target cache information is decoded according to the target position information to obtain the code information corresponding to the current query request; if the target code is not all the code in the code sequence, then the other codes besides the target code are pre-filled first, and then the target cache information and the caches corresponding to other codes are decoded according to the target position information to obtain the code information corresponding to the current query request.
[0035] In this embodiment, in response to the user's current query request, the current question vector corresponding to the current query request is first determined. Based on the current question vector, a code vector library is used for matching to obtain a code sequence including at least two current codes. Based on each current code and its first-order information within the code sequence, a query is performed in multiple cache sequences including at least two cache information entries to obtain target cache information for the target code in the at least two current codes. Finally, target position information is determined according to the first-order information of the target code in the code sequence. Based on the target cache information of each target code, code information corresponding to the current query request is generated according to the target position information. In this way, by matching the current question vector in the code vector library to obtain the corresponding code sequence, historical caches can be safely reused while ensuring the accuracy of code generation. Simultaneously, by searching for the target cache information of the target code in the cache information corresponding to each historical query request in the at least two current codes, target cache information corresponding to the positional relationship of each target code in the code sequence can be found, thereby further improving the cache hit rate. This solves the technical problem in related technologies where the inability to reuse semantically equivalent code fragments leads to a large amount of avoidable redundant computation, limiting cache utilization and system performance improvement.
[0036] In some embodiments, such as Figure 2 As shown, step S103 can be achieved through steps S201 and S202: Step S201: For each of the multiple cache sequences, according to the second and first order information of at least two cache information in the cache sequence, the historical code corresponding to each cache information is matched sequentially based on at least two current codes to obtain the initial cache information of the target code corresponding to the cache sequence.
[0037] In this embodiment, it can be first determined whether the current code in the first position of the code sequence matches the historical code corresponding to the cache information in the first position of the cache sequence. If they cannot match, it can be determined whether the current code in the second position of the code sequence matches the historical code corresponding to the cache information in the first position of the cache sequence. If none of the current codes in the code sequence match the historical code corresponding to the cache information in the first position of the cache sequence, it can be determined that there is no initial cache information in the cache sequence.
[0038] If the current code in the first position of the code sequence matches the historical code corresponding to the cache information in the first position of the cache sequence, then the cache information in the first position is determined as the initial cache information of the current code. Then, it is determined whether the current code in the second position of the code sequence matches the historical code corresponding to the cache information in the second position of the cache sequence. If they do not match, it is determined whether the current code in the third position of the code sequence matches the historical code corresponding to the cache information in the second position of the cache sequence. If none of the other current codes in the code sequence, except the current code in the first position, match the historical code corresponding to the cache information in the first position of the cache sequence, then it is determined that there is only one initial cache information (i.e., the cache information in the first position) in the cache sequence.
[0039] If the current code in the first position of the code sequence does not match the historical code corresponding to the cache information in the first position of the cache sequence, then determine whether the current code in the second position of the code sequence matches the historical code corresponding to the cache information in the first position of the cache sequence. If they match, then the cache information in the first position of the cache sequence is determined as the cache information of the current code in the second position of the code sequence.
[0040] In this embodiment, the initial cache information of the target code corresponding to all cache sequences is determined according to the method described above. For example, cache sequence 1 includes the initial cache information of two target codes, cache sequence 2 has no initial cache information, and cache sequence 1 includes the initial cache information of one target code.
[0041] Step S202: Determine the number of initial cache information corresponding to each cache sequence, and determine the initial cache information with the largest number as the target cache information of at least two target codes in the current code.
[0042] In this embodiment, the size relationship between the number of initial cache information corresponding to each cache sequence can be determined, and the initial cache information with the largest number can be determined as the target cache information of at least two target codes in the current code. For example, if cache sequence 1 includes initial cache information of two target codes, cache sequence 2 has no initial cache information, and cache sequence 1 includes initial cache information of one target code, then the two initial cache information in cache sequence 1 can be determined as the target cache information of two target codes.
[0043] In this embodiment, for each of the multiple cache sequences, based on the second and first order information of at least two cache pieces of information in the cache sequence, the historical code corresponding to each cache piece of information is matched sequentially based on at least two current codes to obtain the initial cache information of the target code corresponding to the cache sequence. The number of initial cache pieces of information corresponding to each cache sequence is determined, and the initial cache piece of information with the largest number is determined as the target cache information of the target code in the at least two current codes. In this way, by determining the initial cache information of the target code corresponding to the cache sequence based on the second order information of each cache piece of information in the cache sequence and the first order information of each current code in the code sequence, the prefix similarity between the two sequences (cache sequence and code sequence) can be accurately measured, thereby improving the cache hit rate.
[0044] In some embodiments, such as Figure 3 As shown, the step S201 above, "based on the second and first order information of at least two cached information in the cache sequence, matching the historical code corresponding to each cached information sequentially based on at least two current codes to obtain the initial cache information of the target code corresponding to the cache sequence," can be achieved through steps S301 and S302: Step S301: For the first cache information located in the first position in the cache sequence, match the first historical code corresponding to the first cache information with the first current code located in the first position in the code sequence to obtain the first matching result.
[0045] In this embodiment, the first historical code corresponding to the first cached information in the first order is matched first, and the first current code in the first order of the code sequence is matched first. For example, if the cache sequence is [5, 4, 2, 1] and the code sequence is [6, 5, 1, 4], then cache "5" and code "6" are matched first.
[0046] Step S302: Based on the first matching result, determine the matching object in the historical code corresponding to at least two cached information and the first other code other than the first current code among at least two current codes.
[0047] In this embodiment of the application, based on the matching relationship between the first historical code corresponding to the first cache information in the first order of the cache sequence and the first current code in the first order of the code sequence, matching objects are determined in the historical codes corresponding to at least two cache information and the first other codes other than the first current code in at least two current codes.
[0048] In some embodiments, step S302 can be implemented by steps S401 and S402: Step S401: If the first matching result indicates that the first historical code matches the first current code, the second cache information in the second position of the cache sequence and the second current code in the second position of the code sequence are determined as matching objects.
[0049] In this embodiment of the application, when the first historical code matches the first current code, the second cached information in the second position of the cache sequence and the second current code in the second position of the code sequence can both be determined as matching objects. For example, if the cache sequence is [5, 4, 2, 1] and the code sequence is [5, 6, 1, 4], and the first historical code corresponding to cache "5" matches the first current code "5", then the second historical code corresponding to cache "4" and the second current code "6" can be determined as matching objects.
[0050] In this embodiment of the application, when the first matching result indicates that the first historical code matches the first current code, the cache information corresponding to the first historical code can be determined as the initial cache information of the first current code, and the first current code is the target code.
[0051] Step S402: If the first matching result indicates that the first historical code and the first current code do not match, the first historical code corresponding to the first cache information and the second current code located in the second position in the code sequence are determined as matching objects.
[0052] In this embodiment of the application, when the first historical code and the first current code do not match, both the first historical code corresponding to the first cache information and the second current code located in the second position in the code sequence can be determined as matching objects. For example, if the cache sequence is [5, 4, 2, 1] and the code sequence is [6, 5, 1, 4], and the first historical code corresponding to cache "5" does not match the first current code "6", then the second historical code corresponding to cache "5" and the second current code "5" can be determined as matching objects.
[0053] Step S303: Match the matching objects until the matching of the cache information located at the last position in the cache sequence is completed, and the initial cache information of each target code is obtained.
[0054] For example, in the case of a cache sequence of [5, 4, 2, 1] and a code sequence of [6, 5, 1, 4], the matching steps include: Step 1. Match the historical code corresponding to cached code "5" with the current code "6"; Step 2. Because the historical code corresponding to cache "5" does not match the current code "6", the current code "5" in the next position of the current code "6" is used to match the historical code corresponding to cache "5" again. Step 3. Since the historical code corresponding to cache "5" matches the current code "5", the historical code corresponding to cache "4" is matched with the current code "1".
[0055] Since cache "5" has already been hit, the historical code corresponding to cache "4" in the next order of cache "5" needs to be matched. Since the current code "6" has already been matched, the current code "1" in the next order of current code "6" is matched with the historical code corresponding to cache "4".
[0056] Step 4. Because the historical code corresponding to cache "4" does not match the current code "1", the historical code corresponding to cache "4" is matched with the current code "4".
[0057] In this case, because the historical code corresponding to cache "4" does not match the current code "1", cache "4" is not hit, and it still needs to be matched. However, the current code "1" has already been matched, so the current code "4" in the next position of the current code "1" is matched with the historical code corresponding to cache "4". At this time, the current code "4" matches the historical code corresponding to cache "4", so cache "4" is hit. That is to say, for the cache sequence [5, 4, 2, 1], cache [5, 4] is hit.
[0058] Understandably, related technologies require a strict correspondence between the cache sequence and the code sequence for a cache hit. For example, a cache hit of [6, 5, 1, 4] is only possible if both the code sequence and the cache sequence are [6, 5, 1, 4]. Alternatively, a cache hit is only possible if the positional order of the initial cache information in the cache sequence perfectly matches the positional order of the target code in the code sequence. For example, a cache hit of [6, 5] is only possible if both the code sequence and the cache sequence are [6, 5, 7, 8]. However, this application achieves a cache hit of [5, 4] even with a cache sequence of [5, 4, 2, 1] and a code sequence of [6, 5, 1, 4]. This not only preserves the positional relationship between the target code within the code sequence but also allows for "non-contiguous" flexibility, enabling the identification of a portion of the cache sequence that is consecutively consistent with the beginning of the code sequence, thereby improving the cache hit rate.
[0059] In this embodiment, for the first cache information in the first position of the cache sequence, the first historical code corresponding to the first cache information is matched with the first current code in the first position of the code sequence to obtain a first matching result. Based on the first matching result, matching objects are determined from the first other codes other than the first current code among the historical codes corresponding to at least two cache information pieces and at least two current codes. Matching is performed on the matching objects until the matching of the cache information in the last position of the cache sequence is completed, thereby obtaining the initial cache information of each target code. In this way, only the positional relationship between target codes in the code sequence can be preserved, while allowing the flexibility of "non-continuous" sequences, so that the part that is continuous and consistent with the beginning of the code sequence can be determined from the cache sequence, thereby improving the cache hit rate.
[0060] In some embodiments, step S202 can be implemented by steps S501 and S502: Step S501: If there are at least two target cache sequences among multiple cache sequences, determine the number of tokens corresponding to the initial cache information in each target cache sequence; the target cache sequence includes the largest number of initial cache information.
[0061] It is understandable that if multiple cache sequences contain multiple target cache sequences with the largest number of initial cache information, then it is necessary to determine the cache sequence containing the target cache information from among the multiple target cache sequences. In this embodiment, the number of tokens in all initial cache information of each target cache sequence can be determined.
[0062] For example, cache sequence 1 includes 2 initial cache information, and cache sequence 2 also includes 2 initial cache information. Since both cache sequence 1 and cache sequence 2 include the largest number of initial cache information, both cache sequence 1 and cache sequence 2 are target cache sequences. At this time, it is necessary to determine the number of tokens of all initial cache information in cache sequence 1 and the number of tokens of all initial cache information in cache sequence 2.
[0063] Step S502: The initial cache information with the most tokens in each target cache sequence is determined as the target cache information.
[0064] In this embodiment, the initial cache information with the largest number of tokens can be determined as the target cache information. For example, cache sequence 1 includes 2 initial cache information, and cache sequence 2 also includes 2 initial cache information. If the number of tokens in the 2 initial cache information in cache sequence 1 is greater than the number of tokens in the 2 initial cache information in cache sequence 2, then the 2 initial cache information in cache sequence 1 is determined as the target cache information.
[0065] In this embodiment, when at least two target cache sequences exist among multiple cache sequences, the number of tokens corresponding to the initial cache information in each target cache sequence is determined; the target cache sequence includes the initial cache information with the largest number of tokens; the initial cache information with the largest number of tokens in each target cache sequence is determined as the target cache information. In this way, by using the number of tokens corresponding to the initial cache information in each target cache sequence, more accurate cache information can be filtered out from at least two target cache sequences, thereby improving the accuracy of code generation.
[0066] In some embodiments, the above method can also be implemented by step S601, and step S104 can be implemented by step S602: Step S601: If the number of target codes is less than the number of current codes, pre-fill the second other codes other than the target codes in at least two current codes to obtain the cache information of each second other code.
[0067] For example, if the cache sequence is [5, 4, 2, 1] and the code sequence is [6, 5, 1, 4], and the target code that hits the cache is [5, 4], and the second other code is [6, 1], then [6, 1] needs to be pre-filled first to obtain the cache information of [6, 1].
[0068] Step S603: Based on the target cache information of each target code and the cache information of each second other code, generate the code information corresponding to the current query request according to the target position information.
[0069] In this embodiment of the application, the target cache information of each target code and the cache information of each second other code are decoded according to the target position information to obtain the code information corresponding to the current query request.
[0070] For example, if the cache sequence is [5, 4, 2, 1] and the code sequence is [6, 5, 1, 4], and the target code that is hit in the cache is [5, 4], then the target cache information of [5, 4] and the cache information of [6, 1] are decoded in the order of [5, 4, 6, 1] to obtain the code information corresponding to the current query request.
[0071] In this embodiment, when the number of target codes is less than the number of current codes, pre-filling processing is performed on at least two current codes other than the target codes to obtain cache information for each second other code. Based on the target cache information of each target code and the cache information of each second other code, code information corresponding to the current query request is generated according to the target order information. In this way, pre-filling processing is only required for the second other codes that have not been cached, thereby solving the technical problem in related technologies where the inability to reuse semantically equivalent code fragments leads to a large amount of avoidable redundant computation, which limits the improvement of cache utilization and system performance, and improves the efficiency of code generation.
[0072] In some embodiments, step S603 can be implemented by steps S701 and S702: Step S701: Determine the inference priority of at least two current codes based on the first order information of at least two current codes in the code sequence.
[0073] In this embodiment of the application, prompt words for the code generation model can be generated based on the first order information of at least two current codes in the code sequence, and input into the code generation model. The code generation model can determine the inference priority of at least two current codes based on the prompt words.
[0074] For example, in the case of a code sequence of [6, 5, 1, 4], the prompt "When answering the question, please read the context in the following priority order: code 6 > code 5 > code 1 > code 4" can be identified as the prompt word for the code generation model.
[0075] Step S702: Correct the target position information based on the inference priority, and decode the target cache information of each target code and the cache information of each second other code according to the corrected target position information to obtain the code information corresponding to the current query request.
[0076] In this embodiment of the application, when the target position information is different from the inference priority, the target position information can be corrected based on the inference priority, and the target cache information of each target code and the cache information of each second other code can be decoded according to the corrected target position information.
[0077] For example, with a cache sequence of [5, 4, 2, 1] and a code sequence of [6, 5, 1, 4], the target cache hit is [5, 4]. Therefore, the code that did not hit the cache in [6, 5, 1, 4] includes [6, 1], so the target order is [5, 4, 6, 1]. Since the inference priority is code 6 > code 5 > code 1 > code 4, the target order can be corrected to code 6 > code 5 > code 1 > code 4 based on code 6 > code 5 > code 1 > code 4.
[0078] In this embodiment, the inference priority of at least two current codes is first determined based on their first-order information in the code sequence. Then, the target order information is corrected based on the inference priority, and the target cache information of each target code and the cache information of each second other code are decoded according to the corrected target order information to obtain the code information corresponding to the current query request. This allows for sequential model training, improving the model's utilization of unordered fragment information and thus increasing the accuracy of code generation.
[0079] In some embodiments, the above method can also be implemented through steps S801 and S802, and step S102 can be implemented through steps S803 and S804. Step S801: Perform vector transformation on each piece of code in the code library to obtain the code vector for each piece of code.
[0080] Here, code vectors are used to represent the topological structure of the code.
[0081] In this embodiment of the application, for each code, the above step S801 can be implemented by at least one of steps S8011 to S8014: Step S8011: Parse the abstract syntax tree of the code and perform vector transformation on the topological structure of the key nodes in the abstract syntax tree to obtain the feature vector of the code.
[0082] In this embodiment, the code in the codebase can be parsed into an Abstract Syntax Tree (AST), and the topological structure of the key nodes in the AST can be extracted. Finally, the topological structure of the key nodes is transformed into a feature vector. Specifically, after obtaining the topological structure of the key nodes, the topological structure can be traversed in a depth-first or breadth-first manner to obtain a linear sequence. Finally, the linear sequence is embedded into text to obtain the feature vector of the code.
[0083] In some embodiments, the abstract syntax tree can be used as an image input to a graph neural network (GNN) to obtain the feature vector of the code.
[0084] Step S8012: Construct a symbolic dependency graph of the code, and construct a dependency vector of the code based on the symbolic dependency graph.
[0085] In this embodiment of the application, core symbols can be obtained from the code, and then a symbol dependency graph can be constructed based on the core symbols; then, based on the symbol dependency graph, the dependency paths in the symbol dependency graph are concatenated to obtain an ordered text path, and the ordered text path is embedded to obtain a dependency vector.
[0086] Step S8013: Use a code model to perform semantic encoding on the code to obtain the code's embedding vector.
[0087] In this embodiment, code can be input into a trained code model, which can then output the code's embedding vector. For example, this code model can be CodeBERT.
[0088] In this embodiment, an abstract syntax tree can be input into a code model to obtain a code structure vector, and the code can be input into the code model to obtain a code semantic vector. Finally, the structure vector and the semantic vector are fused to obtain the code embedding vector.
[0089] Step S8014: Determine the data flow information and / or control flow information of the code, and perform vector transformation on the data flow information and / or control flow information of the code to obtain the code vector.
[0090] In this embodiment of the application, the data flow information and / or control flow information can be data that can be structured into code. By performing vector transformation on the data flow information and / or control flow information, the code vector of the code can be obtained.
[0091] Step S802: Construct a code vector library based on the code vector of each code.
[0092] Step S803: Perform similarity matching between the current question vector and each code vector to obtain at least two target code vectors; the similarity between the target code vector and the current question vector is greater than or equal to the similarity threshold.
[0093] In this embodiment of the application, the current problem vector can be matched with each code vector to obtain the similarity between the current problem vector and each code vector, and the code vector with a similarity greater than or equal to the similarity threshold is determined as the target code vector.
[0094] Step S804: Sort the current code corresponding to each target code vector according to the similarity between each target code vector and the current question vector to obtain a code sequence including at least two current codes.
[0095] In this embodiment of the application, after obtaining at least two target code vectors, the similarity between each target code vector and the current problem vector can be sorted to obtain the sequence of current code corresponding to each target code vector, that is, to obtain a code sequence including at least two current codes.
[0096] For example, if the similarity of target code vector 1 is 80%, the similarity of target code vector 2 is 81%, and the similarity of target code vector 3 is 79%, then the code sequence is [2, 1, 3].
[0097] In this embodiment, each code in the codebase is vectorized to obtain a code vector representing the topological structure of the code. The current question vector is then matched with each code vector for similarity, resulting in at least two target code vectors with a similarity greater than or equal to a similarity threshold. Finally, the current codes corresponding to each target code vector are sorted according to their similarity to the current question vector, resulting in a code sequence including at least two current codes. This method, by transforming the code vectors, enables semantic awareness of the code, improving the accuracy of selecting relevant code from the codebase. Simultaneously, sorting the current codes according to their similarity to the current question vector enhances the code generation model's utilization of unordered fragment information.
[0098] In some embodiments, the application of the code generation method provided in this application in real-world scenarios is described below.
[0099] Retrieval-enhanced generation systems suffer from low cache hit rates and degraded inference quality when handling long contextual inputs. Specifically, existing caching techniques either maintain accuracy while achieving low cache utilization, or improve cache utilization while reducing inference quality.
[0100] Current research challenges lie in: 1. How to improve cache hit rate under long contextual input without sacrificing inference accuracy. 2. Existing RAG caching systems have significant limitations in scenarios such as code generation, completion, and understanding. They typically rely on exact / prefix matching of document identifiers (such as file paths, code snippet IDs) or text strings to achieve cache reuse. However, software development involves numerous functionally equivalent code patterns with different textual expressions.
[0101] In related technologies, renaming variables / functions—such as `calculate_sum(a,b)` and `compute_total(x,y)`—may achieve the same addition functionality. Syntactic sugar or API variants: using a `for` loop and using a `map` function to perform the same traversal operation. Logically equivalent code blocks: two different algorithms implementing the same sorting function. However, existing methods fail to recognize these semantically equivalent code snippets, leading to a large amount of avoidable redundant computation, limiting cache utilization and system performance improvements.
[0102] To address the aforementioned technical challenges, related solutions include: 1. Precise prefix matching methods, such as RadixCache, LMCache, and RAGCache, which offer high cache hit rates but low accuracy. 2. Approximate key-value (KV) cache matching methods, such as CacheBlend and PromptCache, which improve cache utilization but degrade inference quality. 3. Existing RAG caching systems often rely on textual descriptions of code for vector retrieval, necessitating the development of descriptions from code.
[0103] This application provides a semantically aware cache reuse mechanism for code generation, which includes: 1. Code semantic awareness (offline process, pre-converting code in the code library into vectors): a cache hit method that uses semantic matching to replace traditional key-value exact matching, improving cache hit rate.
[0104] 2. Unordered cache reuse: An unordered retrieval strategy that breaks the restrictions of cache order and makes full use of cached information.
[0105] 3. Generation-based sequential model training: Improves the model's ability to utilize information from unordered fragments.
[0106] In this embodiment, firstly, in the cache hit stage, it not only relies on text or identifier matching, but more importantly, it analyzes the deep semantic features of the code (such as the Abstract Syntax Tree (AST) structure, API dependencies, data flow, etc.) to determine whether two code contexts are "equivalent" or "highly similar," thereby safely reusing the KV cache while ensuring generation accuracy. Then, a set-based longest prefix matching algorithm is used to achieve unordered cache reuse, further improving the cache hit rate. Finally, a sequence-based model training method can be used to enhance the model's utilization of unordered fragment information.
[0107] The advantages of the semantically aware cache reuse mechanism for code generation provided in this application embodiment are: 1. Performance improvement: Avoids redundant pre-padding calculations for the entire code segment, significantly reducing first-word latency (TimeToFirst Token, TTFT).
[0108] 2. Accuracy Guarantee: Through precise semantic prompts, the Large Language Model (LLM) clearly understands the functional equivalence and syntactic differences of the code, and will not misunderstand the function due to syntactic differences, thus generating more accurate answers.
[0109] 3. High cache hit rate: Expanding the scope of cache reuse from "completely identical text" to "semantic functional equivalence", combined with unordered cache reuse technology, greatly improves cache utilization.
[0110] In this embodiment of the application, code semantic awareness can be achieved through formula (1): Formula (1); in, For code vectors; It is a structural representation based on an abstract syntax tree; API / symbolic dependency graph representation; Representation of data flow / control flow; For lightweight code semantic embedding (e.g., [CLS] vectors for CodeBERT / CodeT5).
[0111] In this embodiment of the application, the above-mentioned This can be achieved through the following steps: parse the code in the codebase into an abstract syntax tree (AST), extract the topological structure of key nodes (such as function definitions, calls, and control flow), and transform the topological structure into feature vectors.
[0112] In this embodiment of the application, the above-mentioned This can be achieved through the following steps: API / symbolic dependency graph: Identify the key library functions, classes, and methods called in the code, and build a dependency vector.
[0113] In this embodiment of the application, the above-mentioned This can be achieved by using the following steps: generating embedding vectors for code snippets using a specialized code model (such as CodeBERT).
[0114] In some embodiments, code semantic awareness can also be achieved through at least one of the following steps: 1) encoding based on serialized AST (DFS, depth-first search), inputting it into a sequence model; 2) encoding based on graph structure (GNN); 3) hybrid method: AST + source code token combination.
[0115] For example, such as Figure 4 As shown, code 401 is encoded to obtain data stream structure 403 and / or abstract syntax tree 402. The data stream structure 403 and / or abstract syntax tree 402 are processed to obtain embedded representation 404, which is the code vector.
[0116] like Figure 5 As shown, the inference process 500 of the code generation model includes prefix caching 501, pre-filling 502, and decoding 503. Among them, the unordered cache reuse in the semantic-aware cache reuse mechanism for code generation can be applied to prefix caching 501, thereby achieving the effect of fully utilizing cached information.
[0117] In this embodiment, unordered cache reuse is a set-based longest prefix matching algorithm that can find the longest consecutive prefix at the beginning of a target sequence (existing order) based on the relative order in an existing sequence. The pseudocode for the set-based longest prefix matching algorithm is shown below: # Compute k_i in Eq.(10): longest prefix of X that appears in C_i asan in-order subsequence # Inputs: # X = [x1, x2, ..., xm] # C = [c1, c2, ..., cn] # (this is C_i) # id(.) returns the block identifier (hash / fingerprint / semantic-ID) # Output: # k = max k st x1..xk can be matched in C in increasing positions # pos = matched positions [p1, p2, ..., pk] (optional, useful for debugging / reuse slicing) function unordered_longest_prefix_match(X, C): m ← length(X) n ← length(C) j ← 1 # pointer in X (next prefix block we want to match) pos ← empty_list # store matched indices in C (optional) for p from 1 to n: if j>m: break if id(C[p]) == id(X[j]): # or match(C[p], X[j]) if you usesemantic gating append(pos, p) j ← j + 1 k ← j - 1 return (k, pos) In this embodiment of the application, the implementation process of the above pseudocode is as follows: 1. Initialization Phase First, the function will obtain basic information about the two input sequences and initialize key variables: Calculate the length of sequence X and assign it to variable m (X has m elements: X1 to X2). m ); Calculate the length of sequence C and assign it to variable n (C has n elements: C1 to C2). m ); Initialize pointer j=1: j is used to point to the next prefix element in X that needs to be matched (initially, the first element x1 of X needs to be matched); Initialize an empty list pos: Used to record the position (index) of the element that matches the X prefix in C, which is convenient for debugging or subsequent slicing.
[0118] 2. Traversal and Matching Phase The function starts from the first element of C and iterates through each element in order (position p ranges from 1 to n). The core logic is as follows: Early termination check: Before each traversal, check if j>m is true. If true, it means that all elements of X have been matched, so jump out of the loop directly and do not need to continue traversing C. Element matching judgment: If j≤m (there are still elements of X that need to be matched), then use the id() function to determine whether the current C[p] and X[j] match (id(C[p])==id(X[j]), which can also be replaced with semantic matching or other custom logic); Successful match handling: If the id matches successfully, add the current position p in C to the pos list, and increment j by 1 (indicating that the next element to be matched is the next element of X, for example, if it was originally matching x1, now it is matching x2). Match failure handling: If the id does not match, do nothing and directly traverse the next element of C (p is automatically incremented by 1).
[0119] 3. Result Calculation and Return Stage When C completes its iteration (from p to n) or exits the loop prematurely: Calculate the length k of the longest matching prefix: k = j - 1 (because j points to the "next element to be matched", for example, j = 3 means that x1 and x2 have been matched, so the number of matches is 2, i.e., j - 1). Return result: tuple (k, pos), where k is the longest prefix length that X can match in order, and pos is the list of corresponding matching positions in C (for example, pos=[2,5] means that the first two elements of X match the 2nd and 5th positions in C respectively).
[0120] For example, taking X=[5,4,2,1] and C=[6,5,1,4] as examples, the implementation process of the above pseudocode is explained: 1. Initialize variables The length of X is m=4 (the elements are 5, 4, 2, 1 in sequence); The length of C is n=4 (the elements are 6, 5, 1, 4 in sequence); Pointer j = 1 (initially matching the first element 5 of X); The list of matching positions is pos = [] (an empty list).
[0121] 2. Iterate through each element of C (p from 1 to 4). p=1 (C[1]=6): id(6) ≠ id(5) (X[j=1] is 5), no operation, continue to the next element; p=2 (C[2]=5): id(5)=id(5), matching successful, add p=2 to pos→pos=[2], add j to 1→j=2 (now we need to match the second element 4 of X); p=3 (C[3]=1): id(1) ≠ id(4) (X[j=2] is 4), no operation, continue to the next element; p=4 (C[4]=4): id(4)==id(4), matching successful. Add p=4 to pos→pos=[2,4], add j to 1→j=3 (now we need to match the third element 2 of X). At this point, C has been traversed (from p to 4), and the loop terminates.
[0122] 3. Calculation Results The longest prefix length k = j-1 = 3-1 = 2; Matching position pos=[2,4].
[0123] Final result The function returns the result: (k=2,pos=[2,4]).
[0124] The meaning of this result is: the longest prefix of X (which can be a subsequence of C in order) is the first two elements [5,4], where 5 matches the second position of C and 4 matches the fourth position of C; while the third element of X, 2, cannot be found in C (there is no 2 in C), so the prefix length stops at 2.
[0125] In this embodiment of the application, a specific example is used to illustrate Generation-based sequential model training: Input the prompt "You are a helpful intelligent assistant that can generate code based on its own knowledge and, where possible, combine relevant code knowledge information" into the model; User input: The following is code knowledge information: [item 1]4 … [item 2]1 … [item 3]6 … [item 4] 5… Please generate code based on this context information: Note: When answering questions, please read the context in the following order of priority: [item3]>[item4]>[item2]>[item1].
[0126] Figure 6 This is a schematic diagram of the composition structure of a code generation device provided in an embodiment of this application, such as... Figure 6 As shown, the code generation device 600 includes: a first determining module 601, a matching module 602, a query module 603, a second determining module 604, and a generation module 605; wherein, The first determining module 601 is used to determine the current question vector corresponding to the current query request in response to the user's current query request; Matching module 602 is used to perform matching in the code vector library based on the current problem vector to obtain a code sequence that includes at least two current code snippets; The query module 603 is used to query multiple cache sequences, each containing at least two cache information items, based on each current code and the first order information of each current code in the code sequence, to obtain the target cache information of the target code in at least two current codes. The cache sequence is obtained by sorting the cache information items according to the degree of matching between each historical code and the corresponding historical query request. Each historical code is obtained by matching historical query requests in a code vector library. The cache information is obtained by pre-filling historical code. The positional relationship of each target cache information item in the cache sequence corresponds to the positional relationship of each target code in the code sequence. The second determining module 604 is used to determine the target position information according to the first position information of the target code in the code sequence; The generation module 605 is used to generate the code information corresponding to the current query request based on the target cache information of each target code and according to the target position information.
[0127] In some embodiments, the query module 603 is further configured to, for each of the multiple cache sequences, match the historical code corresponding to each cache information according to the second order information and the first order information of at least two cache information in the cache sequence, based on at least two current codes, to obtain the initial cache information of the target code corresponding to the cache sequence; determine the number of initial cache information corresponding to each cache sequence, and determine the initial cache information with the largest number as the target cache information of the target code in at least two current codes.
[0128] In some embodiments, the query module 603 is further configured to match the first historical code corresponding to the first cache information in the first position of the cache sequence with the first current code in the first position of the code sequence to obtain a first matching result; based on the first matching result, determine the matching object in the first other code other than the first current code among the historical codes corresponding to at least two cache information and at least two current codes respectively; match the matching object until the matching of the cache information in the last position of the cache sequence is completed to obtain the initial cache information of each target code.
[0129] In some embodiments, the query module 603 is further configured to, when the first matching result indicates that the first historical code and the first current code match, determine the second cache information in the second position of the cache sequence and the second current code in the second position of the code sequence as matching objects; and when the first matching result indicates that the first historical code and the first current code do not match, determine the first historical code corresponding to the first cache information and the second current code in the second position of the code sequence as matching objects.
[0130] In some embodiments, the query module 603 is further configured to determine the number of tokens corresponding to the initial cache information in each target cache sequence when there are at least two target cache sequences in multiple cache sequences; the target cache sequence includes the initial cache information with the largest number of tokens; and the initial cache information with the largest number of tokens in each target cache sequence is determined as the target cache information.
[0131] In some embodiments, the above apparatus further includes a pre-filling processing module; the pre-filling processing module is used to pre-fill at least two current codes other than the target code when the number of target codes is less than the number of current codes, to obtain cache information of each second other code; the above generation module 605 is used to generate code information corresponding to the current query request according to the target position information based on the target cache information of each target code and the cache information of each second other code.
[0132] In some embodiments, the generation module 605 is further configured to determine the inference priority of at least two current codes based on the first order information of at least two current codes in the code sequence; correct the target order information based on the inference priority; and decode the target cache information of each target code and the cache information of each second other code according to the corrected target order information to obtain the code information corresponding to the current query request.
[0133] In some embodiments, the above apparatus further includes a vector generation module; the vector generation module is used to perform vector transformation on each code in the code library to obtain a code vector for each code; the code vector is used to represent the topological structure of the code; a code vector library is constructed based on the code vector of each code; the matching module 602 is further used to perform similarity matching between the current question vector and each code vector to obtain at least two target code vectors; the similarity between the target code vector and the current question vector is greater than or equal to a similarity threshold; the current codes corresponding to each target code vector are sorted according to the similarity between each target code vector and the current question vector to obtain a code sequence including at least two current codes.
[0134] In some embodiments, the vector generation module described above is further configured to parse the abstract syntax tree of the code, and perform vector transformation on the topological structure of key nodes in the abstract syntax tree to obtain the feature vector of the code; construct the symbolic dependency graph of the code, and construct the dependency vector of the code based on the symbolic dependency graph; perform semantic encoding on the code using a code model to obtain the embedding vector of the code; determine the data flow information and / or control flow information of the code, and perform vector transformation on the data flow information and / or control flow information of the code to obtain the code vector of the code.
[0135] The descriptions of the apparatus embodiments above are similar to those of the method embodiments above, and have similar beneficial effects. In some embodiments, the functions or modules included in the apparatus provided in this application can be used to perform the methods described in the method embodiments above. For technical details not disclosed in the apparatus embodiments of this application, please refer to the descriptions of the method embodiments of this application for understanding.
[0136] It should be noted that, in the embodiments of this application, if the above-described code generation method is implemented as a software functional module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiments of this application, or the part that contributes to the related technology, can be embodied in the form of a software product. This software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, mobile hard drives, read-only memory (ROM), magnetic disks, or optical disks. Thus, the embodiments of this application are not limited to any specific hardware, software, or firmware, or any combination of hardware, software, and firmware.
[0137] This application provides a computer device including a memory and a processor. The memory stores a computer program that can run on the processor. When the processor executes the program, it implements some or all of the steps in the above-described method.
[0138] This application provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements some or all of the steps in the above-described method. The computer-readable storage medium can be transient or non-transient.
[0139] This application provides a computer program including computer-readable code. When the computer-readable code is run in a computer device, the processor in the computer device performs some or all of the steps in the above-described method.
[0140] This application provides a computer program product, which includes a non-transitory computer-readable storage medium storing a computer program. When the computer program is read and executed by a computer, it implements some or all of the steps in the above-described method. This computer program product can be implemented specifically through hardware, software, or a combination thereof. In some embodiments, the computer program product is specifically embodied as a computer storage medium; in other embodiments, the computer program product is specifically embodied as a software product, such as a software development kit (SDK), etc.
[0141] It should be noted that the descriptions of the various embodiments above tend to emphasize the differences between them, while their similarities or commonalities can be referred to interchangeably. The descriptions of the above embodiments of the device, storage medium, computer program, and computer program product are similar to the descriptions of the above method embodiments and have similar beneficial effects. For technical details not disclosed in the embodiments of the device, storage medium, computer program, and computer program product of this application, please refer to the descriptions of the method embodiments of this application for understanding.
[0142] Figure 7 This is a schematic diagram of a hardware entity of the code generation device in the embodiments of this application, such as... Figure 7 As shown, the hardware entity of the code generation device 700 includes: a processor 701, a communication interface 702, and a memory 703, wherein: The processor 701 typically controls the overall operation of the code generation device 700, which may implement the code generation method provided in the embodiments of this application.
[0143] The communication interface 702 enables the code generation device 700 to communicate with other terminals or servers via a network.
[0144] The memory 703 is configured to store instructions and applications executable by the processor 701, and can also cache data to be processed or already processed (e.g., image data, audio data, voice communication data, and video communication data) in the processor 701 and the code generation device 700. It can be implemented using flash memory or random access memory (RAM). Data transfer between the processor 701, the communication interface 702, and the memory 703 can be performed via bus 704.
[0145] This application provides a computer storage medium that stores one or more programs, which can be executed by one or more processors to implement the steps of the code generation method as described in any of the above embodiments.
[0146] It should be noted that the descriptions of the storage medium and device embodiments above are similar to those of the method embodiments above, and have similar beneficial effects. For technical details not disclosed in the storage medium and device embodiments of this application, please refer to the descriptions of the method embodiments of this application for understanding.
[0147] The aforementioned processor can be at least one of the following: Application Specific Integrated Circuit (ASIC), Digital Signal Processor (DSP), Digital Signal Processing Device (DSPD), Programmable Logic Device (PLD), Field Programmable Gate Array (FPGA), Central Processing Unit (CPU), Controller, Microcontroller, and Microprocessor. It is understood that other electronic devices can also implement the functions of the aforementioned processor, and this application does not specifically limit the specific implementation.
[0148] The aforementioned computer storage media / memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), magnetic random access memory (FRAM), flash memory, magnetic surface memory, optical disc, or compact disc read-only memory (CD-ROM), etc.; or it can be various terminals that include one or any combination of the above-mentioned memories, such as mobile phones, computers, tablet devices, personal digital assistants, etc.
[0149] It should be understood that the phrase "one embodiment" or "an embodiment" throughout the specification means that a specific feature, structure, or characteristic related to the embodiment is included in at least one embodiment of this application. Therefore, "in one embodiment" or "in an embodiment" appearing throughout the specification does not necessarily refer to the same embodiment. Furthermore, these specific features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. It should be understood that in the various embodiments of this application, the sequence numbers of the above steps / processes do not imply a sequential order of execution; the execution order of each step / process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application. The sequence numbers of the above embodiments of this application are merely descriptive and do not represent the superiority or inferiority of the embodiments.
[0150] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element.
[0151] The above are merely embodiments of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application.
Claims
1. A code generation method, the method comprising: In response to the user's current query request, determine the current question vector corresponding to the current query request; Based on the current problem vector, a matching operation is performed in the code vector library to obtain a code sequence that includes at least two current code snippets; Based on each current code and its first position information in the code sequence, a query is performed in multiple cache sequences including at least two cache information items to obtain target cache information for the target code in the at least two current codes. The cache sequence is obtained by sorting the cache information according to the degree of matching between each historical code and its corresponding historical query request. Each historical code is obtained by matching the historical query request in a code vector library. The cache information is obtained by pre-filling the historical code. The positional relationship of each target cache information item in the cache sequence corresponds to the positional relationship of each target code in the code sequence. Determine the target position information based on the first position information of the target code in the code sequence; Based on the target cache information of each target code, the code information corresponding to the current query request is generated according to the target position information.
2. The method according to claim 1, characterized in that, The step of querying multiple cache sequences, each containing at least two cache information, based on each current code and the first order information of each current code in the code sequence, to obtain the target cache information of the target code in the at least two current codes, includes: For each of the multiple cache sequences, according to the second position information and the first position information of the at least two cache information in the cache sequence, the historical code corresponding to each cache information is matched sequentially based on the at least two current codes to obtain the initial cache information of the target code corresponding to the cache sequence; Determine the number of initial cache information corresponding to each cache sequence, and determine the initial cache information with the largest number as the target cache information of the target code in the at least two current codes.
3. The method according to claim 2, characterized in that, The step of matching the historical code corresponding to each cached information in the cache sequence according to the second position information and the first position information of the at least two cached information, based on the at least two current codes, to obtain the initial cache information of the target code corresponding to the cache sequence includes: For the first cache information located in the first order of the cache sequence, the first historical code corresponding to the first cache information is matched with the first current code located in the first order of the code sequence to obtain a first matching result; Based on the first matching result, a matching object is determined from the historical code corresponding to the at least two cached information and the first other code other than the first current code among the at least two current codes; The matching objects are matched until the matching of the cache information located at the last position in the cache sequence is completed, so as to obtain the initial cache information of each target code.
4. The method according to claim 3, characterized in that, The step of determining the matching object based on the first matching result, from the historical code corresponding to the at least two cached information items and from the first other code items other than the first current code among the at least two current codes, includes: If the first matching result indicates that the first historical code matches the first current code, then the second cache information in the second position of the cache sequence and the second current code in the second position of the code sequence are determined as the matching objects; If the first matching result indicates that the first historical code and the first current code do not match, the first historical code corresponding to the first cache information and the second current code located in the second position in the code sequence are determined as the matching objects.
5. The method according to claim 2, characterized in that, The step of determining the initial cache information with the largest quantity as the target cache information of the target code in the at least two current code snippets includes: In the case where there are at least two target cache sequences among multiple cache sequences, the number of tokens corresponding to the initial cache information in each target cache sequence is determined; the target cache sequence includes the initial cache information with the largest number. The initial cache information with the largest number of tokens in each of the target cache sequences is determined as the target cache information.
6. The method according to claim 1, characterized in that, The method further includes: If the number of target codes is less than the number of current codes, pre-filling is performed on the second other codes other than the target codes in the at least two current codes to obtain cache information for each of the second other codes; The step of generating code information corresponding to the current query request based on the target cache information of each target code and according to the target position information includes: Based on the target cache information of each of the target codes and the cache information of each of the second other codes, the code information corresponding to the current query request is generated according to the target position information.
7. The method according to claim 6, characterized in that, The step of generating code information corresponding to the current query request based on the target cache information of each of the target codes and the cache information of each of the second other codes, according to the target position information, includes: Based on the first order information of the at least two current codes in the code sequence, the inference priority of the at least two current codes is determined; Based on the inference priority, the target position information is corrected, and the target cache information of each target code and the cache information of each of the second other codes are decoded according to the corrected target position information to obtain the code information corresponding to the current query request.
8. The method according to any one of claims 1 to 7, characterized in that, The method further includes: Each piece of code in the codebase is vectorized to obtain a code vector for each piece of code; the code vector is used to represent the topological structure of the code. Based on the code vector of each piece of code, the code vector library is constructed; The process involves matching the current problem vector against a pre-defined code vector library to obtain a code sequence containing at least two current code snippets, including: The current question vector is matched with each of the code vectors to obtain at least two target code vectors; the similarity between the target code vectors and the current question vector is greater than or equal to a similarity threshold. Based on the similarity between each target code vector and the current question vector, the current codes corresponding to each target code vector are sorted to obtain a code sequence that includes at least two current codes.
9. The method according to claim 8, characterized in that, For each piece of code, the vector transformation of each piece of code in the codebase to obtain the code vector for each piece of code includes at least one of the following: The abstract syntax tree of the code is parsed, and the topological structure of the key nodes in the abstract syntax tree is transformed into vectors to obtain the feature vector of the code. Construct a symbolic dependency graph of the code, and construct a dependency vector of the code based on the symbolic dependency graph; The code is semantically encoded using a code model to obtain the embedding vector of the code; Determine the data flow information and / or control flow information of the code, and perform vector transformation on the data flow information and / or control flow information of the code to obtain the code vector of the code.
10. A code generation apparatus, the apparatus comprising: The first determining module is used to determine the current question vector corresponding to the current query request in response to the user's current query request; The matching module is used to perform matching in the code vector library based on the current problem vector to obtain a code sequence that includes at least two current codes; A query module is used to query multiple cache sequences, each including at least two cache information items, based on each current code and the first order information of each current code in the code sequence, to obtain target cache information of the target code in the at least two current codes; wherein, the cache sequence is obtained by sorting each cache information item according to the matching degree between each historical code and the corresponding historical query request; each historical code is obtained by matching the historical query request in a code vector library; the cache information is obtained by pre-filling the historical code; the positional relationship of each target cache information item in the cache sequence corresponds to the positional relationship of each target code in the code sequence; The second determining module is used to determine the target position information according to the first position information of the target code in the code sequence; The generation module is used to generate the code information corresponding to the current query request based on the target cache information of each target code and according to the target position information.