Method and electronic device for code generation based on large language model

By dynamically updating template weights through a programming intelligence agent and selecting Top-K templates using the UCB optimization module, and coordinating with a large language model to generate code, this approach solves the problem of insufficient optimization of template usage in traditional code generation schemes, thereby improving the success rate and accuracy of code generation.

CN121092153BActive Publication Date: 2026-02-17BEIJING FEISHU TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202511639571.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-11-10
Publication Date
2026-02-17
Estimated Expiration
2045-11-10

AI Technical Summary

Technical Problem

In existing technologies, traditional code generation solutions lack dynamic feedback and optimization of template usage effects, making it difficult to improve code generation success rate and efficiency. Furthermore, existing RAG solutions fail to effectively balance the relationship between high-success-rate templates and potentially high-quality templates.

Method used

The template information is dynamically updated through a programming intelligence agent, the template weights are adjusted based on user feedback, the Top-K templates are selected in conjunction with the UCB optimization module, code is generated in collaboration with a large language model, and the template performance is dynamically iterated and optimized.

Benefits of technology

It improved the success rate and accuracy of online user code generation, and achieved dynamic iterative optimization of template performance and a self-learning closed loop for the model.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121092153B_ABST
    Figure CN121092153B_ABST
Patent Text Reader

Abstract

Embodiments of the present disclosure relate to a method for code generation based on a large language model and an electronic device. The method for code generation based on the large language model comprises: receiving a user query, the user query comprising a user requirement description; selecting at least one candidate template corresponding to the user query from a template library based on template information of a plurality of candidate templates in the template library, wherein the template library stores the plurality of candidate templates and a template weight of each template in the plurality of candidate templates, and the template weight is associated with a number of times of using the corresponding template and a result of generating code using the corresponding template; and inputting the user query and the at least one candidate template into the large language model to generate a code response corresponding to the user query. In this way, the success rate and accuracy of online user code generation are improved.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present disclosure relates generally to the field of computers, and more particularly to a method for code generation based on large language models and an electronic device. BACKGROUND

[0002] With the increasing demand for software development, online users have a surge in demand for code generation based on specific technology stacks (such as platform application programming interfaces (APIs), front-end templates, domain-specific languages (DSLs)). Traditional code generation schemes rely on fixed template matching or single large language model (LLM) generation, lack dynamic feedback and optimization of template usage effectiveness, and cannot iteratively improve code generation success rate. Some high-potential but low-usage templates are long ignored (insufficient exploration), while some low-success-rate templates are repeatedly used (over-exploitation).

[0003] Retrieval-augmented generation (RAG) technology can improve relevance, but existing RAG schemes are mostly based on static similarity sorting and do not combine historical performance and exploration needs of templates, making it difficult to balance the relationship between “exploiting high-success-rate templates” and “exploring potential high-quality templates”, resulting in difficulty in further improving code generation success rate and efficiency. SUMMARY

[0004] According to an example embodiment of the present disclosure, a method, apparatus, electronic device, computer-readable storage medium, and computer program product for code generation based on a large language model are provided. The usage effectiveness of the template can be dynamically fed back and optimized, thereby improving the success rate and accuracy of online user code generation.

[0005] In a first aspect of the present disclosure, a method for code generation based on a large language model is provided, comprising: receiving a user query, the user query including a user demand description; selecting at least one candidate template corresponding to the user query from a template library based on template weights of a plurality of candidate templates in the template library, wherein the template library stores a plurality of candidate templates and a template weight of each template in the plurality of candidate templates, and the template weight is associated with a usage frequency of the corresponding template and a result of generating code using the corresponding template; and inputting the user query and the at least one candidate template into a large language model to generate a code response corresponding to the user query.

[0006] In a second aspect of this disclosure, an electronic device is provided, comprising: at least one processing unit; and at least one memory coupled to the at least one processing unit and storing instructions for execution by the at least one processing unit, the instructions causing the electronic device to perform the method described in the first aspect of this disclosure when executed by the at least one processing unit.

[0007] In a third aspect of this disclosure, an apparatus for code generation based on a large language model is provided, comprising: a receiving unit configured to receive a user query, the user query including a user requirement description; a selection unit configured to select at least one candidate template corresponding to the user query from a template library based on template weights of a plurality of candidate templates in the template library, wherein the template library stores a plurality of candidate templates and a template weight of each of the plurality of candidate templates, wherein the template weight is associated with the number of times the corresponding template is used and the result of generating code using the corresponding template; and a generation unit that inputs the user query and at least one candidate template into the large language model to generate a code response corresponding to the user query.

[0008] In a fourth aspect of this disclosure, a computer-readable storage medium is provided having machine-executable instructions stored thereon, which, when executed by a device, cause the device to perform the method described in the first aspect of this disclosure.

[0009] In a fifth aspect of this disclosure, a computer program product is provided, including computer-executable instructions, wherein the computer-executable instructions, when executed by a processor, implement the method described in the first aspect of this disclosure.

[0010] In a sixth aspect of this disclosure, an electronic device is provided, comprising: processing circuitry configured to perform the method described in the first aspect of this disclosure.

[0011] The summary section is provided to introduce a series of concepts in a simplified form, which will be further described in the detailed description below. The summary section is not intended to identify key or essential features of this disclosure, nor is it intended to limit the scope of this disclosure. Other features of this disclosure will become readily apparent from the following description. Attached Figure Description

[0012] The above and other features, advantages, and aspects of the embodiments of this disclosure will become more apparent from the accompanying drawings and the following detailed description. In the drawings, the same or similar reference numerals denote the same or similar elements, wherein:

[0013] Figure 1 Schematic diagrams of systems according to some embodiments of the present disclosure are shown;

[0014] Figure 2 A schematic flowchart of a method for code generation based on a large language model according to some embodiments of the present disclosure is shown;

[0015] Figure 3 A schematic diagram illustrating a code generation process based on a large language model according to some embodiments of the present disclosure is shown;

[0016] Figure 4 Block diagrams of example apparatuses according to some embodiments of the present disclosure are shown; and

[0017] Figure 5 A block diagram of an example device that can be used to implement embodiments of the present disclosure is shown. Detailed Implementation

[0018] Embodiments of this disclosure will now be described in more detail with reference to the accompanying drawings. While some embodiments of this disclosure are shown in the drawings, it should be understood that this disclosure can be implemented in various forms and should not be construed as limited to the embodiments set forth herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of this disclosure. It should be understood that the accompanying drawings and embodiments of this disclosure are for illustrative purposes only and are not intended to limit the scope of protection of this disclosure.

[0019] The large language model in this embodiment can be applied to a variety of application scenarios, such as writing articles, generating dialogues, creating poems, generating content for social media / news, generating code / comments / documents, generating teaching materials, and assisting language learning.

[0020] In practical applications, code generation can be achieved using a single language model or by relying on fixed template matching. However, traditional code generation methods lack dynamic feedback and optimization of template usage effects, making it impossible to iteratively improve code generation success rates. Existing RAG solutions also fail to incorporate historical template performance and exploration needs, hindering further improvements in code generation success rates and efficiency.

[0021] To address the aforementioned problems and other potential issues, embodiments of this disclosure provide a code generation method based on a large language model. In these embodiments, the programming agent can dynamically update template information, such as the number of times a template is used and its weight, based on user feedback on the functional performance of the generated code. Furthermore, the programming agent of this disclosure also explores templates with high potential but low usage frequency. In this way, the success rate and accuracy of online user code generation are further improved.

[0022] Figure 1A schematic diagram of a system 100 according to some embodiments of the present disclosure is shown. The system 100 includes a large language model (LLM) 101 and a programming agent 102, wherein the programming agent 102 includes a template library module 103, an embedding generation module 104, a similarity calculation module 105, an upper confidence bound (UCB) selection module 106, a RAG generation module 107, and a feedback update module 108.

[0023] like Figure 1 As shown, LLM 101 and the programming agent 102 can receive and transmit information to each other and collaborate to complete related tasks. LLM 101 is capable of deep understanding and processing of natural language, and can perform various complex tasks such as code generation and knowledge question answering based on neural networks. After receiving prompts, LLM 101 can process the prompts and convert them into vector representations.

[0024] It's understandable that user input prompts are mostly presented in natural language. The prompts input into the LLM 101 large language model can be a piece of text used to guide the model in generating specific outputs. For example, the prompt can be a question, a sentence fragment, an instruction, or any other form of text. LLM 101 can predict the next content based on the prompt. For instance, in a code generation task, if the prompt is "You are a professional code generation assistant, and you need to generate directly executable code based on user needs and provided reference templates…", LLM 101 will output code that meets the user's needs based on the input prompt.

[0025] For example, the template library module 103 can store template information for multiple candidate templates. Taking template X (the first template) as an example, the template information of template X can include: the identification information of template X, the number of times template X has been used, and the template weight of template X. The template weight of template X is related to: the number of times template X has been used within a preset historical time period and the result of generating code using template X. For example, the number of times template X has been used can be a positive integer (the number of times it has been called) or 0 (it has not been called). For example, the result of generating code using template X can include the result of generating code each time X is used. The result of a particular instance can be any of the following: the code generated based on template X is adopted directly without modification; the code generated based on template X is adopted after one or more modifications; or the code generated based on template X is not adopted.

[0026] Optionally, for template X, the template weight of template X can be determined based on the historical results of code generation using template X. Optionally, for template X, the number of times template X is used (e.g., n) and the template weight (e.g., s) within a preset historical time period can be stored.

[0027] In the embodiments of this disclosure, the template weight may also be referred to as the template score, score, etc., and is used to characterize the success rate of the code of the corresponding template being adopted. For example, the “score” mentioned below can be replaced by the template weight.

[0028] In embodiments of this disclosure, the template library module 103 stores multiple verified templates T and associates each template with a historical buffer. , and These represent the number of times the template was used within a preset historical time period, and the template's score, respectively. For example, the score of a stored template is related to its historical usage, such as whether the template was used in the past and the generated code was successfully accepted (or accepted after modification, or not accepted even after modification). Assume that the template library module 103 contains m (m is an integer greater than or equal to 1) templates, where a template refers to a set of verification test examples containing "user query-code" pairs, and the set of all templates is denoted as […]. Each template From query (User Requirements Description) and Code (Corresponding to the correct code) are composed of all functionally verified, where i is an integer greater than or equal to 1 and less than or equal to m.

[0029] For example, 10,000 query-code pairs based on code templates are collected, manually verified, and then stored in Redis. .

[0030] In embodiments of this disclosure, the embedding generation module 104, based on an embedding language model, combines the user query Q with a template. This is converted into an embedding vector to capture semantic information from the text. The term "embedding vector" is also known as a low-dimensional dense vector, feature vector, etc.

[0031] For example, the embedding vector dimension is denoted as d, and the template... The embedding vector is denoted as ( (where d is the set of real numbers and d is the vector dimension), and the embedded vector corresponding to the user query is denoted as . .

[0032] Optionally, the embedded language model uses a model structure MM to transform a string into a 768-dimensional embedding vector. It should be understood that other embedded language models, such as Gemini Embeddings, can also be used, and the vector dimension can also be other dimensions; this disclosure is not limited in this regard.

[0033] For example, when a user enters a query, such as "React-based login page code (using XY components)", the Model Structure (MM) generates an embedding vector for that user query.

[0034] In embodiments of this disclosure, the similarity calculation module 105 calculates the cosine similarity between the user query embedding vector and the embedding vectors of each template. This cosine similarity can characterize the semantic relevance between the user query and each template. For example, the cosine similarity between the aforementioned user query embedding vector and 10,000 templates is calculated.

[0035] In the embodiments of this disclosure, the UCB selection module 106 calculates the UCB score for each template based on similarity, template historical performance, and exploration factor, and selects Top-K templates. Template historical performance includes the number of times the template was used within a preset historical time period. And the score of the template. For example, an array that records the usage of each template can be used in a history buffer. Including the number of times used and cumulative performance score The UCB selection module 106 selects the K highest-scoring templates from the candidate templates based on UCB scores, i.e., the Top-K templates.

[0036] Introducing an exploration factor into the algorithm for calculating UCB scores can uncover some high-potential but rarely used templates, while also taking into account the template scores. The exploration factor is used to balance exploration and template utilization, thereby improving the accuracy and efficiency of code generation. The term "exploration factor" may also be referred to as a balancing factor, exploration-utilization balancing factor, exploration coefficient, exploration parameter, exploration-utilization parameter, exploration intensity, or others; this disclosure is not limited thereto.

[0037] For example, the UCB optimization module 106 calculates the UCB score for each template (e.g., the aforementioned 10,000 templates), and selects the 5 templates with the highest scores, i.e., the Top-5 templates. For instance, these 5 selected templates all have a similarity to the user query greater than or equal to 0.75, with 2 of them used less than or equal to 3 times. Understandably, although the two templates mentioned above are used infrequently among the 10,000 templates, using UCB scores to filter the Top-5 templates can identify templates with low usage but high potential, thus increasing the utilization rate of high-potential templates.

[0038] In the embodiments of this disclosure, the RAG generation module 107 inputs the Top-K template as a search result into the LLM 101, and the two work together to generate target code. For example, inputting the aforementioned Top-5 template into the LLM 101 generates React login page code containing XY components.

[0039] In embodiments of this disclosure, the feedback update module 108 updates the historical buffer of the corresponding template based on user feedback on the generated code. For example, if a user reports "the code needs to be modified to run successfully" (modification count t=1), the Top-5 template will be updated. (Add 1 to each) and (For example, add 1.2 to each). For instance, if a user reports "the button style has been modified 10 times but it still fails" (number of modifications t=10), update the usage count of the Top-5 template. (Add 1 to each) and the score of the template (For example, subtract 1 from each). It should be understood that the above addition and subtraction of scores on the template are merely exemplary and are not limited in this disclosure. For example, the added or subtracted scores could be functions related to the number of times the code has been modified.

[0040] Figure 2 A schematic flowchart of a method 200 for code generation based on a large language model according to some embodiments of the present disclosure is shown. In block 202, a user query is received, the user query including a user requirement description. In block 204, based on the template weights of multiple candidate templates in a template library, at least one candidate template corresponding to the user query is selected from the template library, wherein the template library stores multiple candidate templates and the template weight of each of the multiple candidate templates, wherein the template weight is associated with the number of times the corresponding template is used and the result of generating code using the corresponding template. In block 206, the user query and at least one candidate template are input into the large language model to generate a code response corresponding to the user query.

[0041] In some embodiments of this disclosure, a collaborative working mode between the programming agent 102 and the LLM 101 is employed to process prompt information more efficiently. The programming agent 102 can filter out Top-K templates that meet the requirements and also discover templates that are used less frequently but have high potential. The same process is executed for all subsequent online user queries, further realizing dynamic iterative optimization of template performance and a self-learning closed loop of the model.

[0042] For example, the user's requirement description can be text, voice, etc. For example, the requirement description can be entered through a human-computer interaction interface.

[0043] In some embodiments of this disclosure, user feedback on code responses is received; and based on the feedback, the usage count and template weight of at least one candidate template in the template library are updated. Taking the first template among multiple candidate templates as an example, the template weight of the first template is related to: the usage count of the first template within a preset historical time period. And the results of generating code using the first template.

[0044] For example, the result of using the first template to generate code includes any of the following: the code generated based on the first template is adopted directly without modification, the code generated based on the first template is adopted after one or more modifications, or the code generated based on the first template is not adopted.

[0045] Optionally, the score of the first template can also be calculated / updated based on the results of the generated code using the first template. It should be understood that when a template is first stored in the history buffer, the number of uses and the score can be initialized, for example... This ensures the accuracy of template information even after the template has been used multiple times.

[0046] For example, after a user uses the generated code, they can provide feedback on the functionality. The feedback update module 108 then updates the number of times each template is used and its score in the historical buffer of each template in Top-K based on the feedback.

[0047] For example, the feedback information may include one or more types. For instance, if the feedback information indicates that the user did not modify the code response (i.e., the code generated based on at least one candidate template was adopted directly without modification), then the feedback update module 108 will increment the usage count of each candidate template among the K templates by 1 and add a first preset value to the score. For example, assuming the first preset value is 1, then...

[0048] (1)

[0049] In equation (1) This indicates an assignment update, meaning the left-hand parameter is updated to the calculation result on the right.

[0050] For example, the feedback information indicates that the code response functions correctly after being modified by the user (i.e., the code generated based on at least one candidate template is adopted after one or more modifications), and the updated template information includes: incrementing the usage count of each candidate template in the at least one candidate template group by 1, and adding a second preset value to the score. For example, assuming the second preset value is 1.2, then...

[0051] (2)

[0052] Optionally, the second preset value is greater than the first preset value, thereby increasing the usage rate of the template corresponding to the modified and normally functioning code in subsequent user query processes.

[0053] In some embodiments of this disclosure, the feedback information indicates that the code response still fails to function correctly after being modified by the user (i.e., the code generated based on at least one candidate template is not adopted), and the updating of template information includes: incrementing the usage count of each candidate template in the at least one candidate template by 1, and subtracting a specific value from the score, the specific value being associated with the number of modifications to the code response and a third preset value. For example, assuming the third preset value is 1, then...

[0054] (3)

[0055] In equation (3) Number of modifications allowed for the user It is an integer greater than 1. Indicates taking The smaller value in 1, the constant It can be flexibly configured according to specific circumstances (such as computing resources, response speed, etc.). For example, 'a' can be set to 10, thereby avoiding excessive impact on template performance evaluation due to too few failures in modification attempts. For example, a template will not be considered to have no exploration value just because of a single modification failure.

[0056] As discussed above, updating template information may involve a first preset value, a second preset value, and a third preset value. It should be understood that the embodiments of this disclosure do not limit the size of the first / second / third preset values. For example, these three can be unequal, or two of them can be equal, etc. For instance, the value size can be set based on an application strategy, where the application strategy can be multiple candidate templates that recommend the number of times the application is used, or suggestions for templates that the application has not used, etc.

[0057] In some embodiments of this disclosure, the UCB selection module 106 selects at least one candidate template by: determining the similarity between the user query and each of the multiple candidate templates; determining the upper bound of the confidence level UCB score between the user query and each template based on the multiple similarities between the user query and the multiple candidate templates and the template information of the multiple candidate templates; and selecting at least one candidate template based on the multiple UCB scores between the user query and the multiple candidate templates.

[0058] For example, for each template Calculate its semantic similarity with the user query Q, for example, represented as:

[0059] (4)

[0060] Where the function , For vectors and dot product, For vectors The length of the mold, For vectors The modulus length. In equation (4), Equivalent to the function , Equivalent to a function .

[0061] For example, for each template The UCB selection module 106 calculates the UCB score. In some embodiments, the UCB score is also based on a balancing factor, which indicates the template selection strategy. Optionally, the UCB calculation formula can be:

[0062] (5)

[0063] In equation (5) template The average score of experience, if ,but Alternatively, besides Division can also be a relationship between two factors. and The function, i.e. , where the function Follow Increasing, following Decreasing. If ,but (Indicates that the initial performance is unknown). In equation (5) The total number of times all templates are used. That is, the total number of times all templates are used.

[0064] In equation (5) As a balance factor, ,For example For example, the UCB score is also based on a balance factor, which indicates the template selection strategy. The larger the size, the more inclined one is to explore templates that are used less frequently; The smaller the value, the more likely it is to use templates with high average scores.

[0065] Alternatively, the UCB calculation formula can also be:

[0066] (6)

[0067] In equation (6), parameter A is an adjustable hyperparameter, such as A = 10, 20, 30, or others. In equations (5) and (6) It is the natural logarithm function. It should be understood that formulas (5) and (6) are merely exemplary and are not limited thereto in this disclosure.

[0068] It should be understood that the term "balance factor" may also be referred to as exploration factor, exploration-utilization balance factor, exploration coefficient, exploration parameter, exploration-utilization parameter, exploration intensity, or others; this disclosure does not limit it.

[0069] In some embodiments of this disclosure, at least one candidate template is the K templates with the highest corresponding UCB scores among a plurality of candidate templates, where K is a predefined positive integer. For example, the UCB scores of all templates can be calculated using formula (5) or (6), and the 5 templates with the highest scores are selected, i.e., the Top-5 templates.

[0070] In some embodiments of this disclosure, the UCB optimization module 106 inputs the Top-K template as the search result into the LLM 101, and finally the RAG generation module 107 generates the target code. Simultaneously, the feedback update module 108 continues to receive user feedback on the code response and dynamically updates the template information.

[0071] It should be understood that some modules (such as the embedded generation module 104, RAG generation module 107, etc.) or all modules in the embodiments of this disclosure may be integrated into LLM 101, and this disclosure does not limit this.

[0072] As an example, when the programming agent 102 interacts with the LLM 101, it can adopt a four-part structure: "System Prompt → Context → User Query → Output Requirement," as shown in Table 1.

[0073] Table 1

[0074]

[0075] Figure 3 A schematic flowchart of a code generation process 300 based on a large language model according to some embodiments of the present disclosure is shown. Figure 3 As shown, at the initial stage of the entire system operation, the usage count and score of all templates are initialized with 301, that is... The user query processing 302 is then performed, for example, converting the user's requirement description in the query into a vector representation, i.e., an embedding vector, through the embedding generation module 104. Then, similarity calculation 303 is performed, for example, calculating the similarity between the user's code requirement description and all templates stored in Redis through the similarity calculation module 105. For example, using the UCB optimization module 106 and based on similarity, historical template performance (such as usage frequency and score), and a balance factor, the UCB score of each template is calculated (304), and the K templates with the highest scores are selected (305), i.e., the Top-K templates. In 306, the Top-K templates are input into the LLM, and then into the RAG code generation 307. Simultaneously, the user can judge the code's functional performance 308 and input feedback information. The system can read the functional performance from the feedback information and then update the template information based on the functional performance (309). Steps 302-309 are repeated for all subsequent online user queries, implementing dynamic iterative optimization of template performance.

[0076] It should be understood that in the embodiments of this disclosure, "first," "second," etc., are only used to indicate that multiple objects may be different, but at the same time, it does not exclude that two objects are the same, and should not be interpreted as any limitation on the embodiments of this disclosure.

[0077] It should also be understood that the manner, situation, category, and division of embodiments in the present disclosure are for the convenience of description only and should not constitute a special limitation. Various manners, categories, situations, and features in the embodiments can be combined with each other where logically consistent.

[0078] It should also be understood that the foregoing is merely to help those skilled in the art better understand the embodiments of this disclosure, and is not intended to limit the scope of the embodiments of this disclosure. Those skilled in the art can make various modifications, variations, or combinations based on the foregoing. Such modifications, variations, or combinations are also within the scope of the embodiments of this disclosure.

[0079] It should also be understood that the above description focuses on highlighting the differences between the various embodiments. Similarities or commonalities can be referenced or learned from each other, and for the sake of brevity, they will not be repeated here.

[0080] Figure 4A schematic block diagram of an example apparatus 400 according to some embodiments of the present disclosure is shown. Apparatus 400 may be implemented in software, hardware, or a combination of both. Figure 4 As shown, the device 400 includes a receiving unit 402, a selection unit 404, and a generating unit 406.

[0081] Receiving unit 402 is configured to receive user queries, which include a description of user needs. Selection unit 404 is configured to select at least one candidate template corresponding to the user query from a template library based on the template weights of multiple candidate templates in the library. The template library stores multiple candidate templates and the template weight of each candidate template, where the template weight is related to the number of times the corresponding template is used and the result of generating code using the corresponding template. Generation unit 406 is configured to input the user query and at least one candidate template into a large language model to generate a code response corresponding to the user query.

[0082] In some embodiments, the apparatus 400 may further include an updating unit. The receiving unit 402 is configured to receive user feedback information on the code response; and the updating unit is configured to update the template information of at least one candidate template in the template library based on the feedback information.

[0083] In some embodiments, the template weight of the first template among multiple candidate templates is related to: the number of times the first template is used within a preset historical time period, and the results of using the first template.

[0084] In some embodiments, the result of generating code using the first template includes any of the following: the code generated based on the first template is adopted directly without modification, the code generated based on the first template is adopted after being modified once or multiple times, or the code generated based on the first template is not adopted.

[0085] In some embodiments, the selection unit 404 is configured to: select at least one candidate template including: determining the similarity between the user query and each of the plurality of candidate templates; and determining the UCB score between the user query and each template based on the plurality of similarities between the user query and the plurality of candidate templates and the template information of the plurality of candidate templates.

[0086] In some embodiments, the UCB score is also based on a balance factor, which indicates the template selection strategy.

[0087] In some embodiments, at least one candidate template is the K highest-scoring corresponding UCB templates among a plurality of candidate templates, where K is a predefined positive integer.

[0088] In some embodiments, the corresponding UCB score of at least one candidate template exceeds a preset score.

[0089] In some embodiments, the feedback information indicates that the user has not modified the code response, and the update unit is configured to: increment the usage count of each candidate template in at least one candidate template by 1, and add a first preset value to the score.

[0090] In some embodiments, the feedback information indicates that the code response operates normally after being modified by the user, and the update unit is configured to: increment the usage count of each candidate template in at least one candidate template by 1, and add a second preset value to the score.

[0091] In some embodiments, the feedback information indicates that the code response still fails to function properly after being modified by the user, and the update unit is configured to: increment the usage count of each candidate template in at least one candidate template by 1, and subtract a specific value from the score, the specific value being associated with the number of modifications to the code response and a third preset value.

[0092] Figure 4 The device 400 can be used to achieve the above-mentioned combination. Figures 1 to 3 For the sake of brevity, the process described will not be repeated here.

[0093] The division of modules or units in the embodiments of this disclosure is illustrative and only represents one logical functional division. In actual implementation, there may be other division methods. Furthermore, the functional units in the disclosed embodiments may be integrated into one unit, exist as separate physical entities, or two or more units may be integrated into one unit. The integrated unit described above can be implemented in hardware or as a software functional unit.

[0094] Figure 5 A block diagram of an example device 500 that can be used to implement embodiments of the present disclosure is shown. It should be understood that... Figure 5 The device 500 shown is merely exemplary and should not be construed as limiting the functionality and scope of the implementation described herein. For example, device 500 can be used to perform the functions described above. Figures 1 to 3 The process described.

[0095] like Figure 5 As shown, device 500 is in the form of a general-purpose computing device. Components of computing device 500 may include, but are not limited to, one or more processors or processing units 510, memory 520, storage devices 530, one or more communication units 540, one or more input devices 550, and one or more output devices 560. Processing unit 510 may be a physical or virtual processor and is capable of performing various processes according to programs stored in memory 520. In a multiprocessor system, multiple processing units execute computer-executable instructions in parallel to improve the parallel processing capability of computing device 500.

[0096] Computing device 500 typically includes multiple computer storage media. Such media can be any available media accessible to computing device 500, including but not limited to volatile and non-volatile media, removable and non-removable media. Memory 520 can be volatile memory (e.g., registers, cache, random access memory (RAM)), non-volatile memory (e.g., read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory), or some combination thereof. Storage device 530 can be removable or non-removable media and can include machine-readable media, such as flash drives, disks, or any other media capable of storing information and / or data (e.g., training data for training) and accessible within computing device 500.

[0097] The computing device 500 may further include additional removable / non-removable, volatile / non-volatile storage media. Although not explicitly stated... Figure 5 As shown, disk drives for reading from or writing to removable, non-volatile disks (e.g., "floppy disks") and optical disk drives for reading from or writing to removable, non-volatile optical disks can be provided. In these cases, each drive can be connected to a bus (not shown) via one or more data media interfaces. Memory 520 may include computer program product 525 having one or more program modules configured to perform various methods or actions of various implementations of this disclosure.

[0098] The communication unit 540 enables communication with other computing devices via a communication medium. Additionally, the components of the computing device 500 can function as a single computing cluster or multiple computing machines capable of communicating via communication connections. Therefore, the computing device 500 can operate in a networked environment using logical connections to one or more other servers, network personal computers (PCs), or another network node.

[0099] Input device 550 can be one or more input devices, such as a mouse, keyboard, trackball, etc. Output device 560 can be one or more output devices, such as a monitor, speaker, printer, etc. Computing device 500 can also communicate as needed with one or more external devices (not shown) via communication unit 540. These external devices include storage devices, display devices, etc., and can communicate with one or more devices that enable user interaction with computing device 500, or with any device that enables computing device 500 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication can be performed via an input / output (I / O) interface (not shown).

[0100] According to an exemplary implementation of this disclosure, a computer-readable storage medium is provided that stores computer-executable instructions thereon, wherein the computer-executable instructions are executed by a processor to implement the methods described above. According to an exemplary implementation of this disclosure, a computer program product is also provided, which is tangibly stored on a non-transitory computer-readable medium and includes computer-executable instructions, which are executed by a processor to implement the methods described above. According to an exemplary implementation of this disclosure, a computer program product is provided that stores a computer program thereon, which, when executed by a processor, implements the methods described above.

[0101] Various aspects of this disclosure are described herein with reference to flowchart illustrations and / or block diagrams of methods, apparatuses, devices, and computer program products implemented according to this disclosure. It should be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer-readable program instructions.

[0102] These computer-readable program instructions can be provided to a processing unit of a general-purpose computer, a special-purpose computer, or other programmable data processing apparatus to produce a machine such that, when executed by the processing unit of the computer or other programmable data processing apparatus, they create means for implementing the functions / actions specified in one or more blocks of the flowchart and / or block diagram. These computer-readable program instructions can also be stored in a computer-readable storage medium that causes a computer, programmable data processing apparatus, and / or other device to operate in a particular manner. Thus, the computer-readable medium storing the instructions comprises an article of manufacture that includes instructions for implementing aspects of the functions / actions specified in one or more blocks of the flowchart and / or block diagram.

[0103] Computer-readable program instructions can be loaded onto a computer, other programmable data processing apparatus, or other device to cause a series of operational steps to be performed on the computer, other programmable data processing apparatus, or other device to produce a computer-implemented process, thereby causing the instructions that execute on the computer, other programmable data processing apparatus, or other device to perform the functions / actions specified in one or more boxes of a flowchart and / or block diagram.

[0104] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of an instruction, which contains one or more executable instructions for implementing the specified logical function. In some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutive blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, may be implemented using a dedicated hardware-based system that performs the specified function or action, or using a combination of dedicated hardware and computer instructions.

[0105] Various implementations of this disclosure have been described above. These descriptions are exemplary and not exhaustive, nor are they limited to the disclosed implementations. Many modifications and variations will be apparent to those skilled in the art without departing from the scope and spirit of the described implementations. The terminology used herein is chosen to best explain the principles, practical applications, or improvements to technology in the market, or to enable others skilled in the art to understand the various implementations disclosed herein.

Claims

1. A method for code generation based on a large language model, comprising: receiving a user query, the user query comprising a user requirement description; determining a similarity between the user query and each of a plurality of candidate code templates in a code template library, wherein the plurality of candidate code templates and template information of the plurality of candidate code templates are stored in the code template library, wherein the template information comprises a template weight of a corresponding code template, the template weight being associated with a historical usage number of the corresponding code template and a result condition of generating code using the corresponding code template, the template weight being used to represent a success rate of the corresponding code template being adopted, and wherein the each of the plurality of candidate code templates is a reference template for a task of generating code; determining an upper confidence bound (UCB) score between the user query and each of the plurality of candidate code templates based on the plurality of similarities between the user query and the plurality of candidate code templates, the template information of the plurality of candidate code templates, and a balancing factor, wherein the balancing factor indicates a template selection strategy; selecting at least one candidate code template corresponding to the user query from the code template library based on the plurality of UCB scores between the user query and the plurality of candidate code templates; and inputting the user query and the at least one candidate code template into a large language model to generate a code response corresponding to the user query.

2. The method of claim 1, further comprising: receiving feedback information of the code response from the user; and updating the usage number and the template weight of the at least one candidate code template in the code template library based on the feedback information.

3. The method of claim 1, wherein the template weight of a first template in the plurality of candidate code templates is associated with a usage number of the first template in a preset historical time period and a result condition of generating code using the first template.

4. The method of claim 3, wherein the result condition of generating code using the first template comprises any one of: the code generated based on the first template is directly adopted without modification, the code generated based on the first template is adopted after one or more modifications, or the code generated based on the first template is not adopted.

5. The method of claim 1, wherein the at least one candidate code template is K candidate code templates in the plurality of candidate code templates with the highest corresponding UCB scores, wherein K is a predefined positive integer.

6. The method of claim 1, wherein the corresponding UCB score of the at least one candidate code template exceeds a preset score value.

7. The method of claim 2, wherein the feedback information indicates that the user does not modify the code response, and wherein updating the usage number and the template weight comprises: adding 1 to the usage number and a first preset value to the template weight of each of the at least one candidate code template. ​ ​ 8.The method of claim 2, wherein the feedback information indicates that the code response is normally operated after being modified by the user, and wherein updating the usage times and the template weights comprises: adding 1 to the usage times and adding a second preset value to the template weights of each of the at least one candidate code template. 9.The method of claim 2, wherein the feedback information indicates that the code response is still not normally operated after being modified by the user, and wherein updating the usage times and the template weights comprises: adding 1 to the usage times and subtracting a specific value from the template weights of each of the at least one candidate code template, the specific value being associated with the number of times of modification of the code response and a third preset value. 10.An electronic device comprising: at least one processing unit; at least one memory coupled to the at least one processing unit and storing instructions for execution by the at least one processing unit, the instructions, when executed by the at least one processing unit, causing the electronic device to perform the method according to any one of claims 1 to 9. 11.An apparatus for code generation based on a large language model, comprising: a receiving unit configured to receive a user query, the user query comprising a user requirement description; a selecting unit configured to: determine a similarity between the user query and each of a plurality of candidate code templates in a code template library, wherein the plurality of candidate code templates and template information of the plurality of candidate code templates are stored in the code template library, wherein the template information comprises a template weight of a corresponding code template, the template weight being associated with a historical usage times of the corresponding code template and a result condition of a code generated by using the corresponding code template, the template weight being used to represent a success rate of the code of the corresponding code template being adopted, and wherein the each of the plurality of candidate code templates is a reference template for a code generation task; determine an upper confidence bound (UCB) score between the user query and each of the plurality of candidate code templates based on the plurality of similarities between the user query and the plurality of candidate code templates, the template information of the plurality of candidate code templates, and a balance factor, wherein the balance factor indicates a template selection strategy; select at least one candidate code template corresponding to the user query from the code template library based on the plurality of UCB scores between the user query and the plurality of candidate code templates; and a generating unit configured to input the user query and the at least one candidate code template to a large language model to generate a code response corresponding to the user query. 12.A computer readable storage medium having stored thereon a computer program, the program, when executed by a processor, implementing the method according to any one of claims 1 to 9. ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​

Citation Information

Patent Citations

  • Template generation method and device, electronic equipment and storage medium

    CN116842926A

  • Structured query language generation method and device, electronic equipment and storage medium

    CN118093621A