A code generation method, apparatus, device, and storage medium
By generating code in segments and managing variables using a variable pool, the problem of inaccurate generation in large-scale pre-trained models when the context exceeds the window size is solved, thus achieving simplification and accuracy in code generation.
Patent Information
- Application Number
- CN202311188756.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-14
- Publication Date
- 2026-11-17
- Estimated Expiration
- 2043-09-14
AI Technical Summary
Existing large-scale pre-trained models suffer from inaccurate or non-functional code generation when the input context exceeds the window size, impacting the code generation performance of developers and teams.
By generating code in segments and using a variable pool to manage variables, control the context size, and store key information, more accurate code can be generated.
It simplifies code generation complexity, effectively controls context size, manages data through a variable pool, and generates more accurate code.
Smart Images

Figure CN117311719B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a code generation method, apparatus, device, and storage medium, belonging to the field of software technology. Background Technology
[0002] In modern software development, developers often use code generators, such as large-scale pre-trained models, to automate parts of programming tasks, improving efficiency and accuracy. However, a problem arises when the input context exceeds the window size of the large-scale pre-trained model. For example, if a developer wants to generate a complex class or function, they may need to provide a large amount of contextual information, including but not limited to class definitions, method definitions, and variable definitions. If this information exceeds the window size of the large-scale pre-trained model, the generated code may be inaccurate or fail to function.
[0003] This issue affects a large number of developers and teams who use code generators, including but not limited to large software companies, open-source projects, and independent developers. They may encounter situations where these tools require a significant amount of contextual information, leading to suboptimal code generation results. Summary of the Invention
[0004] Objective: In view of at least one of the above technical problems, the present invention provides a code generation method, apparatus, device and storage medium that generates code in segments and manages variables through a variable pool to solve the problem of input context exceeding window size.
[0005] Technical solution: To solve the above technical problems, the technical solution adopted by the present invention is as follows:
[0006] In a first aspect, the present invention provides a code generation method, the method comprising:
[0007] Obtain a text description that includes the user's requirements;
[0008] Based on the text description including user requirements, a corresponding step description for implementing user requirements is generated, wherein the step description includes a step description with multiple sub-steps.
[0009] Based on the step descriptions of each sub-step, the code for each sub-step is generated sequentially.
[0010] In some embodiments, based on the text description including user requirements, a corresponding step description for implementing user requirements is generated, including:
[0011] The text description, including the user's needs, is input into a large-scale pre-trained model;
[0012] Large-scale pre-trained models break down user requirements into multiple sub-steps, thereby generating step descriptions for each sub-step.
[0013] In some embodiments, the process of generating a corresponding step description for fulfilling user needs using a large-scale pre-trained model based on the text description including user needs further includes:
[0014] A preset first prompt word is input into a large-scale pre-trained model, wherein the first prompt word is used to help the large-scale pre-trained model generate corresponding step descriptions.
[0015] In some embodiments, based on the step description of each sub-step, the code for each sub-step is generated sequentially, including:
[0016] Extract the important variables corresponding to the code of each sub-step and store them in a preset variable pool. The important variables are the variables that need to be used in the code of the corresponding sub-step.
[0017] Based on the step description of the next sub-step and the important variables stored in the variable pool, the corresponding code is generated using a large-scale pre-trained model.
[0018] In some embodiments, when generating code for sub-steps other than the first sub-step, the process of generating corresponding code using a large-scale pre-trained model based on the step description of the next sub-step and important variables stored in the variable pool further includes:
[0019] A pre-defined second prompt word is input into a large-scale pre-trained model, wherein the second prompt word is used to describe the meaning of the important input variables.
[0020] In some embodiments, the variable pool also stores text descriptions corresponding to important variables, wherein the method for obtaining the text descriptions corresponding to the important variables includes:
[0021] The code generated in the current sub-step and the important variables in the code are input into a large-scale pre-trained model to generate text descriptions corresponding to the important variables. The text descriptions corresponding to the important variables are text information used to describe the function and / or role of the important variables.
[0022] In some embodiments, the process of inputting the code generated in the current sub-step and the important variables in the code into a large-scale pre-trained model to generate text descriptions corresponding to the important variables further includes:
[0023] A preset third prompt word is input into the large-scale pre-trained model, wherein the third prompt word is used to help the large-scale pre-trained model generate textual descriptions of important variables in the sub-step code.
[0024] In some embodiments, the method for determining important variables in the code of a sub-step includes:
[0025] Store all variables from the code in the previous sub-step into the variable pool;
[0026] Once the code for the next step is generated, the variables in the code for the next step are compared with the variables stored in the variable pool. Unused variables are removed from the variable pool, and only the reused variables are kept. All variables in the code for the next step are then stored in the variable pool.
[0027] In some embodiments, the variable pool stores in the following formats: substep index, variable, text description, and corresponding weight; wherein the substep index represents the identifier of the first substep in which the variable appears.
[0028] Whenever a sub-step's code is generated, the variables and corresponding text descriptions of the sub-step's code are extracted and stored in the variable pool, and the variables are assigned initial weights.
[0029] During the generation of sub-step code, the variable pool is updated, specifically including: updating the weight of variables based on their reuse status; if the updated weight is lower than the threshold, the corresponding variable is deleted from the variable pool; if a variable is reused once, its weight is reset to the initial weight; and if a new variable is stored, it is assigned an initial weight.
[0030] In some embodiments, during the generation of code for each sub-step, the step description of the sub-step, the variables stored in the variable pool, and the textual description of the variables are input, and a large-scale pre-trained model is invoked to understand and summarize the code of all previous sub-steps, specifically including:
[0031] Based on the window size of the large-scale pre-trained model, the code of all previous sub-steps is divided into blocks to obtain multiple code blocks that can be accommodated in the window, and a corresponding text description is generated for each code block. The text description is text information used to describe the function and / or role of the code block.
[0032] Merge the text descriptions of all code blocks to obtain the total text description;
[0033] During the process of generating code for each sub-step, the step description of the sub-step, the variables stored in the variable pool, the text description of the variables, and the total text description are input into a large-scale pre-trained model to generate more coherent code.
[0034] In a second aspect, the present invention provides a code generation apparatus, the apparatus comprising:
[0035] The acquisition module is used to acquire text descriptions that include user requirements;
[0036] The first generation module is used to generate a corresponding step description for realizing the user's needs based on the text description including the user's needs, wherein the step description includes a step description of multiple sub-steps.
[0037] The second generation module generates the code for each sub-step sequentially based on the step description of each sub-step.
[0038] Thirdly, the present invention provides an apparatus comprising,
[0039] Memory;
[0040] processor;
[0041] as well as
[0042] Computer programs;
[0043] The computer program is stored in the memory and configured to be executed by the processor to implement the method described in the first aspect above.
[0044] Fourthly, the present invention provides a storage medium having a computer program stored thereon, which, when executed by a processor, implements the method described in the first aspect.
[0045] Beneficial Effects: The code generation method, apparatus, device, and storage medium provided by this invention have the following advantages: This invention provides a code generation method, apparatus, device, and storage medium that generates code in segments and manages variables using a variable pool to solve the problem of input context exceeding the window size. Since only one code segment is generated each time, the complexity of code generation is simplified, the context size can be effectively controlled, and data management through a variable pool leads to better results. Attached Figure Description
[0046] Figure 1 This is a schematic diagram illustrating an application scenario of a code generation method according to an embodiment of the present invention;
[0047] Figure 2 This is a schematic diagram of a code generation method according to an embodiment of the present invention;
[0048] Figure 3 This is a schematic diagram of a data processing apparatus according to an embodiment of the present invention;
[0049] Figure 4 This is a schematic diagram of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0050] The present invention will be further described below with reference to the accompanying drawings and embodiments. The following embodiments are only used to more clearly illustrate the technical solutions of the present invention, and should not be used to limit the scope of protection of the present invention.
[0051] like Figure 1 As shown, before detailing the embodiments of the present invention, a specific scenario example is given to illustrate the scenarios in which the technical solution of the present invention can be used. The method of the present invention can be configured in... Figure 1 The server in the system can communicate with multiple clients.
[0052] The client application provides users with a visual interface for code generation and design. It also includes an input interface where users can enter text information, such as, "Please compile a list of invoice numbers issued today." This text information is then transmitted to the server over the network.
[0053] The server, equipped with a large-scale pre-trained model, interprets user requirements from input text descriptions and breaks them down into multiple sub-steps, generating step descriptions for each sub-step. These sub-steps then fulfill the user requirements. Code for each sub-step is generated sequentially based on its description, with the next step's code only generated after the previous one is complete. In addition to the step descriptions, a pre-defined variable pool controls the generation process. During generation, key variables from each sub-step are extracted and stored in the pool. When generating code for the next sub-step, the code is generated based on the step description and these key variables. This approach, within the limited context window of the large-scale pre-trained model, utilizes the variable pool to store crucial information during code generation, such as the aforementioned key variables, resulting in more accurate code.
[0054] 1. Context: In code generation, context refers to the information used to guide code generation, such as class definitions, method definitions, and variable definitions. There is a direct relationship between context and the generated code; that is, context directly affects the quality of the generated code.
[0055] 2. Window Size: In this problem, the window size refers to the maximum length of contextual information that a large-scale pre-trained model such as ChatGPT can consider. If the contextual information exceeds the window size, the excess information will not be considered by ChatGPT, which may affect the quality of the generated code.
[0056] 3. Summary: In this problem, summarizing refers to the process of simplifying the context to fit within the window size. However, the summarizing process may result in the loss of a significant amount of detailed information, thus affecting the quality of the generated code.
[0057] When using ChatGPT to generate code, the required context can be so extensive that it cannot fit within the model's window size. One approach is to first summarize the context and then use this summary as context to guide code generation. However, because the code contains a great deal of detail, this summarization method can easily lose a significant amount of detail.
[0058] To address this technical problem, the method of this invention generates code in segments and manages variables using a variable pool to solve the issue of input context exceeding the window size. Since only one code segment is generated each time, the complexity of code generation is simplified, the context size can be effectively controlled, and data management through a variable pool leads to better results.
[0059] The aforementioned server can be an electronic device with a certain computing power. For example, a server can be a distributed system server, or a system with multiple processors, memory, network communication modules, etc., working together. A server can also be a cloud server, or an intelligent cloud computing server or intelligent cloud host with artificial intelligence technology. A server can also be a server cluster formed by several servers. Alternatively, with the development of science and technology, a server can also be a new technological means capable of realizing the corresponding functions of the implementation method described in the specification. For example, it can be a new form of "server" based on quantum computing.
[0060] The aforementioned client can be an electronic device with network access capabilities. Specifically, for example, the terminal can be a desktop computer, tablet computer, laptop computer, smartphone, etc. Alternatively, the terminal can also be software that can run on the electronic device.
[0061] The aforementioned network can be any type of network, which can use any of the various available protocols (including but not limited to TCP / IP, SNA, IPX, etc.) to support data communication. One or more networks can be a Local Area Network (LAN), an Ethernet-based network, a Token Ring network, a Wide Area Network (WAN), the Internet, a Virtual Network, a Virtual Private Network (VPN), an Intranet, an Extranet, a Public Switched Telephone Network (PSTN), an Infrared Network, a Wireless Network (e.g., Bluetooth, Wi-Fi), and / or any combination of these and / or other networks.
[0062] Example 1
[0063] Firstly, such as Figure 2As shown, this embodiment provides a code generation method, including:
[0064] Step S1: Obtain a text description that includes the user's requirements;
[0065] In this embodiment, the aforementioned text description including user requirements is a piece of text information. This user requirement can be related to the company's business; in this case, the text description could be "Help me compile a list of invoice numbers issued today." Alternatively, the user requirement can be completely unrelated to the company's business; in this case, the text description could be "Help me compile a list of ticket prices for all flights from City A to City B on [Date] at all times." In short, this text description including user requirements is a text description of the user's expectation that one or more functions will be implemented through code.
[0066] Step S2: Based on the text description including user requirements, generate a corresponding step description for implementing user requirements, wherein the step description includes a step description of multiple sub-steps;
[0067] In this embodiment, the text description containing user requirements can be input into a large-scale pre-trained model, which then generates corresponding step descriptions. This large-scale pre-trained model can be a GPT series model, an LLaMA series model, an ERNIE series model, or so on.
[0068] In this embodiment, the above-mentioned step description is also a piece of text information. It is generated by a large-scale pre-trained model that, upon receiving a text description containing user needs, understands the user needs and breaks them down into multiple sub-steps, thus generating step descriptions for each sub-step. Correspondingly, these multiple sub-steps can fulfill the aforementioned user needs. For example, the text description containing user needs, "Help me calculate the ticket prices for all flights from city A to city B on [date]," can be broken down into the following steps: Step 1: Open a browser; Step 2: Enter the URL of a flight search website; Step 3: Enter the date [date], city A, and city B in the corresponding location on the flight search website to perform the search; Step 4: Extract the search results.
[0069] Understandably, in this step, in addition to inputting the text description containing user requirements received in step S1 into the large-scale pre-trained model, a preset first prompt word can also be input to help the large-scale pre-trained model generate the corresponding step description. For example, the prompt word could be: "Please generate the corresponding step description based on the text description I provide."
[0070] Step S3: Based on the step descriptions of each sub-step, generate the code for each sub-step in sequence.
[0071] In this implementation, code for each sub-step is generated sequentially, with the next sub-step's code only generated after the previous step has been completed. Furthermore, in addition to the step descriptions for each sub-step, a pre-defined variable pool controls the generation process. During generation, key variables corresponding to each sub-step are extracted and placed into the variable pool. When generating code for the next sub-step, the corresponding code is generated based on the step description and key variables of that next sub-step. The advantage of this approach is that, under the constraint of a limited context window in a large-scale pre-trained model, storing crucial information from the code generation process, such as the aforementioned key variables, in the variable pool makes the generated code more accurate.
[0072] Correspondingly, when generating sub-steps other than the first sub-step, in addition to inputting the step description of the sub-step and the variables stored in the variable pool into the large-scale pre-trained model, a preset second prompt word can also be input to help the large-scale pre-trained model input the meaning of the variables. For example, the prompt word could be "In the process of generating code, please use the variables in the variable pool".
[0073] In this implementation, important variables are those that need to be used in the code corresponding to subsequent sub-steps, while non-important variables are temporary variables used in the current code.
[0074] In this embodiment, the variable pool can store not only important variables but also their corresponding text descriptions, which are also textual information. These text descriptions are generated by a large-scale pre-trained model that understands the code generated in the current sub-step and the important variables within that code. The descriptions can be functional or descriptive. In this embodiment, a pre-set third prompt can also be used to enable the large-scale pre-trained model to generate textual descriptions of important variables in the sub-step code. For example, it could be "Please describe the role or effect of the important variable in the corresponding code of this sub-step."
[0075] In this implementation, a specific method is given to determine important and unimportant variables in the code, specifically:
[0076] In this implementation, the code for the sub-step is generated first, and then the code for the next sub-step is generated. In other words, before generating the code for the next sub-step, it is not clear which variables will be used in the next sub-step. Therefore, by counting the number of times the variables in the code of the previous sub-step are used, or by performing variable scope analysis, it is not possible to determine which variables in the previous sub-step are important variables. Variables that are used frequently in the previous step may just be temporary variables and will not be passed to the code of the next sub-step.
[0077] Therefore, this implementation provides a conservative strategy for determining important variables. That is, it is assumed that all variables generated in the previous sub-step will be reused in the next sub-step and stored in the variable pool. Only after the code for the next step is generated is the variable in the code of the next step compared with the variable stored in the variable pool. The unused variable is deleted from the variable pool, and only the reused variable is kept.
[0078] To describe the above process using a specific scenario, for example, the text description of user requirements can be A, and the corresponding sub-steps can be A1, A2, and A3. In the process of generating code1 corresponding to A1, code1 can be generated based on the text description of A1, or it can be generated based on the initial variables pre-set in the variable pool.
[0079] After code1 is generated, the large-scale pre-trained model can generate text descriptions of each variable in code1. Then, the variables and their corresponding text descriptions in code1 are stored in the variable pool to generate code2 corresponding to A2.
[0080] Large-scale pre-trained models can generate code2 based on the variables in the current variable pool, the text descriptions of the variables, and the text descriptions of A2.
[0081] After generating code2, the variables generated in code2 are compared with the variables extracted from code1 stored in the current variable pool. Unused variables in code1 are removed from the variable pool, and all variables in code2 are extracted and stored in the variable pool. Correspondingly, a large-scale pre-trained model can be called to generate text descriptions of each variable in code2 and then stored in the variable pool.
[0082] Similarly, the same steps can be followed to generate code3, thus obtaining all the code for A.
[0083] It is worth noting that this method of determining important variables based on a conservative strategy does not take into account the cross-step propagation of important variables. In other words, a variable might be generated in code 1, not reused in code 2, but reused in code 3. Therefore, to address this issue, this embodiment proposes a method for determining important variables based on the aforementioned method of determining important variables using a conservative strategy, including:
[0084] The variable pool can adopt the following structure:
[0085] Sub-step index variable Text description Weight code1 a xxxx 1 code1 b xxxxx 1
[0086] Note: The sub-step index represents the identifier of the first sub-step in which the variable appears. In the variable pool mentioned above, whenever the code for a sub-step is generated, the corresponding variable is extracted and stored in the variable pool, and a corresponding text description is also generated. It should be noted that large-scale pre-trained models can also be called to generate text descriptions of variables. It is important to note that the initial weight of all variables can be 1 (or other values). During the generation of sub-step code, the variable pool is continuously updated, that is, the weights of the variables are continuously updated. Specifically, when a variable is not reused, its weight is reduced, for example, by multiplying it by 0.9. When the weight falls below a certain threshold, such as 0.5, the variable is removed from the variable pool. Conversely, when a variable is reused once, its weight is reset to the initial weight. When a new variable is stored, it uses the initial weight.
[0087] This method of dynamically updating weights, taking into account the propagation of variables across sub-steps, preserves more important variable information within the limited window of a large-scale pre-trained model, thus helping the large-scale pre-trained model generate more accurate code.
[0088] In this embodiment, a method for code continuation is also provided to help the model generate logically coherent and fluent code. This method may be:
[0089] In generating the code for each of the above sub-steps, in addition to inputting the step descriptions of the sub-steps, the variables stored in the variable pool, and the text descriptions of the variables, a large-scale pre-trained model can be invoked to understand and summarize the code of all previous sub-steps. This step of invoking the large-scale pre-trained model to understand and summarize the code of all previous sub-steps can include: based on the window size of the existing large-scale pre-trained model, dividing the code of all previous sub-steps into blocks, obtaining multiple code blocks that can all fit within the window, and generating a corresponding text description for each code block. This text description is used to describe the function and role of the code block. Next, the text descriptions of all code blocks are merged to obtain a total text description. In the process of generating the code for each of the above sub-steps, the step descriptions of the sub-steps, the variables stored in the variable pool, the text descriptions of the variables, and the total text description of each code block are input into the model to generate more coherent code. Through this code continuation method, the generation process of the large-scale pre-trained model can be better controlled, thereby generating more coherent and fluent code.
[0090] In summary, this invention provides a code generation method that addresses the issue of input context exceeding the window size by generating code in segments and managing variables using a variable pool. Since only one code segment is generated each time, the complexity of code generation is simplified, the context size is effectively controlled, and data management through a variable pool ensures better results. The method also simplifies code generation complexity by generating code in segments, effectively controls the context size, and ensures consistency of variables across different sub-steps.
[0091] Example 2
[0092] Secondly, based on Example 1, such as Figure 3 As shown, this embodiment provides a code generation apparatus, including:
[0093] The acquisition module is used to acquire text descriptions that include user requirements;
[0094] The first generation module is used to generate a corresponding step description for realizing the user's needs based on the text description including the user's needs, wherein the step description includes a step description of multiple sub-steps.
[0095] The second generation module generates the code for each sub-step sequentially based on the step description of each sub-step.
[0096] Example 3
[0097] Thirdly, based on Example 1, such as Figure 4As shown, this embodiment provides a device, including,
[0098] Memory;
[0099] processor;
[0100] as well as
[0101] Computer programs;
[0102] The computer program is stored in the memory and configured to be executed by the processor to implement the method described in Embodiment 1.
[0103] Example 4
[0104] Fourthly, based on Embodiment 1, this embodiment provides a storage medium on which a computer program is stored, and when the computer program is executed by a processor, it implements the method described in Embodiment 1.
[0105] Example 5
[0106] Fifthly, based on Embodiment 1, this embodiment proposes a hardware system that applies a code generation method from Embodiment 1. This method is applied to the server side.
[0107] A terminal can be an electronic device with network access capabilities. For example, a terminal can be a desktop computer, tablet computer, laptop computer, smartphone, digital assistant, shopping guide terminal, television set, etc.
[0108] A server can be an electronic device with a certain computing power. For example, a server can be a distributed system server, or a system with multiple processors, memory, network communication modules, etc., working together. A server can also be a cloud server, or an intelligent cloud computing server or intelligent cloud host with artificial intelligence technology. A server can also be a server cluster formed by several servers. Alternatively, with the development of science and technology, a server can also be a new technological means capable of realizing the corresponding functions of the implementation method described in the manual. For example, it can be a new form of "server" based on quantum computing.
[0109] Example 6:
[0110] Sixthly, based on Embodiment 1, this embodiment also provides a computer program product containing instructions that, when executed by a computer, cause the computer to perform a code generation method as described in Embodiment 1.
[0111] It is understood that the specific examples in this document are only intended to help those skilled in the art better understand the embodiments described herein, and are not intended to limit the scope of the invention.
[0112] It is understood that in the various embodiments described in this specification, the sequence number of each process does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments described in this specification.
[0113] It is understood that the various implementation methods described in this specification can be implemented individually or in combination, and the implementation methods in this specification are not limited in this respect.
[0114] Unless otherwise stated, all technical and scientific terms used in the embodiments of this specification have the same meaning as commonly understood by one of ordinary skill in the art. The terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the scope of this specification. The term "and / or" as used in this specification includes any and all combinations of one or more of the associated listed items. The singular forms "a," "the," and "the" as used in the embodiments of this specification and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise.
[0115] It is understood that the processor in the embodiments of this specification can be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method embodiments can be completed by the integrated logic circuits in the processor's hardware or by instructions in software form. The processor can be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in the embodiments of this specification. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the embodiments of this specification can be directly implemented by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software modules can reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. This storage medium is located in memory; the processor reads information from the memory and, in conjunction with its hardware, completes the steps of the above methods.
[0116] It is understood that the memory in the embodiments of this specification may be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. Non-volatile memory may be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Volatile memory may be random access memory (RAM). It should be noted that the memory in the systems and methods described herein is intended to include, but is not limited to, these and any other suitable types of memory.
[0117] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this specification.
[0118] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can be referred to the corresponding processes in the aforementioned method implementations, and will not be repeated here.
[0119] In the several embodiments provided in this specification, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0120] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment, depending on actual needs.
[0121] In addition, the functional units in the various embodiments of this specification can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0122] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of this specification, in essence, or the parts that contribute to the prior art, or parts of the technical solutions, can be embodied in the form of software products. These computer software products are stored in a storage medium and include 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 steps of the methods described in the various embodiments of this specification. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0123] The above description is only a preferred embodiment of the present invention. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the technical principles of the present invention, and these improvements and modifications should also be considered within the scope of protection of the present invention.
Claims
1. A code generation method characterized by, The method includes: Obtain a text description that includes the user's requirements; Based on the text description including user requirements, a corresponding step description for implementing user requirements is generated, wherein the step description includes a step description of multiple sub-steps. Based on the step descriptions of each sub-step, the code for each sub-step is generated sequentially, including: extracting the important variables corresponding to the code of each sub-step and storing them in a preset variable pool, wherein the important variables are the variables that need to be used in the corresponding code of subsequent sub-steps; generating the corresponding code using a large-scale pre-trained model according to the step description of the next sub-step and the important variables stored in the variable pool; wherein the method for determining the important variables in the code of the sub-step includes: storing all variables in the code of the previous sub-step into the variable pool; after the code of the next step is generated, comparing the variables in the code of the next step with the variables stored in the variable pool, deleting unused variables from the variable pool, and only keeping the reused variables; and storing all variables in the code of the next step into the variable pool.
2. The code generation method according to claim 1, characterized in that, The process of generating a corresponding step description for fulfilling user needs using a large-scale pre-trained model, based on the textual description including user requirements, also includes: A preset first prompt word is input into a large-scale pre-trained model, wherein the first prompt word is used to help the large-scale pre-trained model generate corresponding step descriptions.
3. The code generation method according to claim 1, characterized in that, When generating code for sub-steps other than the first sub-step, the process of generating corresponding code using a large-scale pre-trained model based on the step description of the next sub-step and the important variables stored in the variable pool also includes: A pre-defined second prompt word is input into a large-scale pre-trained model, wherein the second prompt word is used to describe the meaning of the important input variables.
4. The code generation method according to claim 1 or 3, characterized in that, The variable pool also stores text descriptions corresponding to important variables, wherein the method for obtaining the text descriptions corresponding to the important variables includes: The code generated in the current sub-step and the important variables in the code are input into a large-scale pre-trained model to generate text descriptions corresponding to the important variables. The text descriptions corresponding to the important variables are text information used to describe the function and / or role of the important variables.
5. The code generation method according to claim 4, characterized in that, The process of inputting the code generated in the current sub-step and the important variables in the code into a large-scale pre-trained model to generate text descriptions corresponding to the important variables also includes: A preset third prompt word is input into the large-scale pre-trained model, wherein the third prompt word is used to help the large-scale pre-trained model generate textual descriptions of important variables in the sub-step code.
6. The code generation method according to claim 1, characterized in that, The variable pool stores information in the following formats: substep index, variable, text description, and corresponding weight; where the substep index represents the identifier of the first substep in which the variable appears. Whenever a sub-step's code is generated, the variables and corresponding text descriptions of the sub-step's code are extracted and stored in the variable pool, and the variables are assigned initial weights. During the generation of sub-step code, the variable pool is updated, specifically including: updating the weight of variables based on their reuse status; if the updated weight is lower than the threshold, the corresponding variable is deleted from the variable pool; if a variable is reused once, its weight is reset to the initial weight; and if a new variable is stored, it is assigned an initial weight.
7. The code generation method according to claim 1, characterized in that, During the generation of code for each sub-step, the input includes the step description of the sub-step, the variables stored in the variable pool, and the text description of the variables. A large-scale pre-trained model is then invoked to understand and summarize the code of all previous sub-steps, specifically including: Based on the window size of the large-scale pre-trained model, the code of all previous sub-steps is divided into blocks to obtain multiple code blocks that can be accommodated in the window, and a corresponding text description is generated for each code block. The text description is text information used to describe the function and / or role of the code block. Merge the text descriptions of all code blocks to obtain the total text description; During the process of generating code for each sub-step, the step description of the sub-step, the variables stored in the variable pool, the text description of the variables, and the total text description are input into a large-scale pre-trained model to generate more coherent code.
8. A code generation device, characterized in that, The device includes: The acquisition module is used to acquire text descriptions that include user requirements; The first generation module is used to generate a corresponding step description for realizing the user's needs based on the text description including the user's needs, wherein the step description includes a step description of multiple sub-steps. The second generation module, based on the step descriptions of each sub-step, sequentially generates the code for each sub-step. Specifically, it is used to: extract the important variables corresponding to the code of each sub-step and store them in a preset variable pool, wherein the important variables are the variables that need to be used in the corresponding code of subsequent sub-steps; generate the corresponding code using a large-scale pre-trained model according to the step description of the next sub-step and the important variables stored in the variable pool; wherein the method for determining the important variables in the code of the sub-step includes: storing all variables in the code of the previous sub-step in the variable pool; after the code of the next step is generated, comparing the variables in the code of the next step with the variables stored in the variable pool, deleting unused variables from the variable pool, and only keeping the reused variables; and storing all variables in the code of the next step in the variable pool.
