Reviewer-based iterative self-enhancement generation method, system, device and storage medium
By building a vector retrieval library and introducing a reviewer module, the problem of unstable quality of AI-generated code was solved, the robustness of the generator module was improved, and high-quality code generation was achieved.
Patent Information
- Application Number
- CN202410467600.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-18
- Publication Date
- 2025-09-19
- Estimated Expiration
- 2044-04-18
AI Technical Summary
In existing technologies, the quality of AI-generated code is unstable, it easily relies on low-quality retrieval code, and there is a model hallucination problem, which makes the generation results uncontrollable.
By building a vector retrieval library, using the output of the generator module for self-enhancement training, adding a reviewer module to simulate the role of a code reviewer, replacing erroneous codes, enhancing the robustness of the generator module and avoiding local optimal solutions.
It improves the quality of AI-generated code, reduces reliance on low-quality retrieved code, and enhances the robustness of the generator module, helping it find the global optimal solution and generate high-quality code.
Smart Images

Figure CN118312169B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of natural language processing and artificial intelligence technology, and in particular relates to a reviewer-based iterative self-enhancement generation method, system, computer device and storage medium. Background Art
[0002] In today's society, with the rapid development of information technology and the explosive growth of internet data, artificial intelligence (AI) technology has made tremendous progress. People have higher expectations for AI, such as generating code from text. However, directly enabling AI to generate code based on text is difficult. To address this problem, researchers have proposed the retrieval enhancement paradigm. Just as human programmers might consult GitHub for similar solutions when faced with a problem, AI can be equipped with a search engine to retrieve relevant code. Feeding the AI with both the required text and the code helps it better generate the correct, relevant code.
[0003] However, the traditional retrieval-augmentation paradigm presents numerous problems. AI trained using this paradigm relies heavily on the quality of the retrieved code. However, the relevance of the retrieved content to the question fluctuates, resulting in inconsistent quality of the output answer. Furthermore, humans often feed multiple snippets of retrieved code along with the text into the AI model. This introduces more valid information but also more noise, reducing the proportion of valid information and interfering with model generation. Furthermore, the model itself may suffer from hallucinations, making it prone to falling into local optimal solutions. Summary of the Invention
[0004] To address the shortcomings of the above-mentioned prior art, the present invention provides a reviewer-based iterative self-enhancing generation method, system, computer device, and storage medium. By adding the output of the generator module as high-quality retrieval code to the training set, the generator module can receive retrieval codes with different quality distributions for the same problem, enhancing its robustness and enabling it to learn to flexibly apply the retrieval codes without over-reliance on the quality of the retrieval codes. Furthermore, a reviewer module is added to simulate the role of a real-world code reviewer, replacing code it deems incorrect with a mask. This provides the generator module with prompts to help it overcome illusions and escape from local optimal solutions. This invention ultimately effectively improves the quality of code generation and has excellent practical application value.
[0005] The first object of the present invention is to provide a reviewer-based iterative self-enhancement generation method.
[0006] The second object of the present invention is to provide an iterative self-enhancing generation system based on reviewers.
[0007] A third object of the present invention is to provide a computer device.
[0008] A fourth object of the present invention is to provide a storage medium.
[0009] The first object of the present invention can be achieved by adopting the following technical solutions:
[0010] A reviewer-based iterative self-enhancement generation method, the method comprising:
[0011] Based on the acquired data set, a vector retrieval library is constructed; the samples in the data set include text and corresponding codes;
[0012] In the vector retrieval library, the corresponding code is retrieved according to the text;
[0013] The text and the corresponding search code are concatenated as input data, the code corresponding to the text is used as the first output data, and the input data and the first output data are used as samples in the training set of the generator module; the second output data is obtained according to the search code corresponding to the text and the corresponding code, and the input data and the second output data are used as samples in the training set of the reviewer module;
[0014] The generator module is trained using the generator module training set; the input data in the generator module training set is input into the trained generator module to obtain a first output code; the first output code and the text corresponding to the generator module training set, as well as the code corresponding to the text, are added as samples into the generator module training set, and the generator module is trained again using the expanded generator module training set; the input data in the expanded generator module training set is input into the retrained generator module to obtain a subsequent output code; the search code in the expanded generator module training set is replaced with the corresponding subsequent output code to obtain a new generator module training set;
[0015] The reviewer module is trained using the reviewer module training set, and the input data in the reviewer module training set is input into the trained reviewer module to obtain a masked output code;
[0016] The masked output code, the corresponding text in the reviewer module training set, and the code corresponding to the text are added as samples to the new generator module training set to obtain the final training set; the generator module is trained using the final training set to obtain a trained generator module;
[0017] Based on the trained generator module and the trained reviewer module, the code corresponding to the requirement text is generated.
[0018] Furthermore, the method of searching for corresponding codes based on text in the vector search library includes:
[0019] Use the text encoder to encode special characters [CLS] and text in the dataset into embedding vectors:
[0020]
[0021] Where, Represents text i The first vector in the corresponding embedding vector, text i Represents the i-th paragraph of text;
[0022] Will All vector search libraries Do the inner product of the vector and remove the text i After finding the corresponding code, take out the code with the highest internal score as the corresponding code.
[0023] Furthermore, constructing a vector retrieval library based on the acquired data set includes:
[0024] Use the code encoder to encode the special characters [CLS] and the codes in the dataset into embedding vectors:
[0025]
[0026] Where, Code i The first vector in the corresponding embedding vector, Code i Represents the i-th section of code;
[0027] Will The corresponding codes are stored in the database as a vector retrieval library.
[0028] Furthermore, before building the vector retrieval library, the dataset is processed, including:
[0029] Filter out invalid information in the data set, remove the code that is wrong and cannot run, and the text that does not match at all or is too short.
[0030] Furthermore, obtaining the second output data according to the search code corresponding to the text and the corresponding code includes:
[0031] The longest common subsequence is obtained based on the retrieval code corresponding to the text and the corresponding code, and the corresponding positions of the retrieval code and the longest common subsequence are set to fixed values, and the other positions are set to 0 as the second output data.
[0032] Furthermore, the generator module consists of an encoder and a decoder of a transformer architecture, each of which contains a learnable embedding matrix and multiple layers of transformer blocks with a self-attention mechanism.
[0033] Furthermore, the reviewer module consists of an encoder in the transformer architecture and an additional linear output layer.
[0034] Furthermore, the code corresponding to the requirement text is generated based on the trained generator module and the trained reviewer module, including:
[0035] Input the required text into the trained generator module to obtain the code;
[0036] The requirement text and code are spliced and input into the trained reviewer module to obtain the masked code;
[0037] The required text and masked code are concatenated and input into the trained generator module to generate high-quality code.
[0038] The second object of the present invention can be achieved by adopting the following technical solutions:
[0039] A reviewer-based iterative self-enhancing generation system, comprising:
[0040] A vector retrieval library construction module is used to construct a vector retrieval library based on the acquired data set; the samples in the data set include text and corresponding codes;
[0041] Code retrieval module, used to retrieve corresponding codes based on text in the vector retrieval library;
[0042] The training set construction module takes the text and the corresponding search code as input data, the code corresponding to the text as first output data, and uses the input data and the first output data as samples in the generator module training set; obtains the second output data based on the search code corresponding to the text and the corresponding code, and uses the input data and the second output data as samples in the reviewer module training set;
[0043] A training module is used to train the generator module using the generator module training set; input the input data in the generator module training set into the trained generator module to obtain the first output code; the first output code and the text corresponding to the generator module training set, as well as the code corresponding to the text are added as samples to the generator module training set, and the generator module is trained again using the expanded generator module training set; the input data in the expanded generator module training set is input into the retrained generator module to obtain subsequent output codes; the retrieval code in the expanded generator module training set is replaced with the corresponding subsequent output code to obtain a new generator module training set; the reviewer module is trained using the reviewer module training set, and the input data in the reviewer module training set is input into the trained reviewer module to obtain a masked output code; the masked output code and the text corresponding to the reviewer module training set, as well as the code corresponding to the text are added as samples to the new generator module training set to obtain a final training set; the generator module is trained using the final training set to obtain a trained generator module;
[0044] The code generation module is used to generate code corresponding to the requirement text based on the trained generator module and the trained reviewer module.
[0045] The third object of the present invention can be achieved by adopting the following technical solutions:
[0046] A computer device includes a processor and a memory for storing a program executable by the processor. When the processor executes the program stored in the memory, the above-mentioned reviewer-based iterative self-enhancement generation method is implemented.
[0047] The fourth object of the present invention can be achieved by adopting the following technical solutions:
[0048] A storage medium stores a program, which, when executed by a processor, implements the above-mentioned reviewer-based iterative self-enhancement generation method.
[0049] The present invention has the following beneficial effects compared to the prior art:
[0050] 1. This method uses the generator module's own output to automatically construct an enhanced dataset with diverse retrieval code quality distributions and continuously trains the generator module. This training results in a highly robust generator module, effectively alleviating the problem in traditional retrieval enhancement paradigms where the model relies on the retrieval code, resulting in uncontrollable code generation quality. This method is simple and effective, does not require the collection of additional data, and has good cost-effectiveness and application value.
[0051] 2. The present invention imitates the role of code reviewers in real software development teams, adds a reviewer module, and gives some prompts to the generator module, alleviating the "model illusion" problem existing in traditional language models, helping the generator module to move out of the local optimal solution and find the global optimal solution, and finally generate high-quality code. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the structures shown in these drawings without paying any creative work.
[0053] Figure 1 Flowchart of the reviewer-based iterative self-enhancement generation method according to embodiment 1 of the present invention;
[0054] Figure 2 Schematic diagram of constructing a vector search library and a training set according to Example 1 of the present invention;
[0055] Figure 3 Schematic diagram of the principle of the reviewer-based iterative self-enhancement generation method according to Example 1 of the present invention;
[0056] Figure 4 This is a flowchart of the training model of Example 1 of the present invention;
[0057] Figure 5 This is a flow chart of the generator module according to embodiment 1 of the present invention;
[0058] Figure 6 Schematic diagram of a subsequent training generator module according to embodiment 1 of the present invention;
[0059] Figure 7 This is a flow chart of the reviewer module according to Example 1 of the present invention;
[0060] Figure 8 A schematic diagram of text-to-code generation using the reviewer-based iterative self-enhancing generation method according to Example 1 of the present invention;
[0061] Figure 9 This is a structural block diagram of a reviewer-based iterative self-enhancement generation system according to embodiment 2 of the present invention;
[0062] Figure 10 This is a structural block diagram of a computer device according to embodiment 3 of the present invention. DETAILED DESCRIPTION
[0063] To make the purpose, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention. It should be understood that the specific implementation described is only used to explain this application and is not used to limit this application.
[0064] Example 1:
[0065] To better serve humans through AI and enhance the accuracy of text-to-code generation, this embodiment proposes a new retrieval enhancement paradigm. Previous retrieval enhancement paradigms caused the model to rely on retrieval codes of unknown quality, resulting in unstable output quality. This embodiment proposes incorporating the model's output into a training set, constructing a training set with a diverse distribution of retrieval code quality. This increases the model's robustness and reduces its reliance on retrieval code. Furthermore, the inclusion of a reviewer helps the model overcome illusions and errors to a certain extent.
[0066] like Figure 1 As shown, the reviewer-based iterative self-enhancement generation method provided in this embodiment includes the following steps:
[0067] S101. Based on the obtained public dataset, construct a vector retrieval library and a generator module training set and a reviewer module training set.
[0068] Furthermore, step S101 includes:
[0069] (1) Obtain the public datasets Hearthstone and Magic and process them.
[0070] In this example, the Hearthstone and Magic datasets were collected for training the model. These datasets contain code and text describing the code's functionality, also known as requirement text. The code and its corresponding text are collectively referred to as a text-code pair.
[0071] First, we filter out invalid information from the collected public datasets Hearthstone and Magic, removing the code that is inherently wrong and cannot run, as well as text segments that are completely mismatched or too short.
[0072] (2) Use the processed data sets Hearthstone and Magic to build a vector retrieval library.
[0073] The datasets mentioned below include Hearthstone and Magic.
[0074] like Figure 2 As shown, the code encoder is used to encode the codes in the special characters [CLS]+ dataset into embedding vectors, which are defined as follows:
[0075]
[0076] Among them, Code i Represents the i-th section of code, and takes out the vector representing [CLS] from all embedded vectors, which is the first vector.
[0077] Will It is stored in the database together with its corresponding code as a vector retrieval library.
[0078] (3) Using the processed data sets Hearthstone and Magic and the vector retrieval library, construct the generator module training set and the reviewer module training set.
[0079] In this embodiment, the code encoder is the encoder part of any fine-tuned public transformer architecture on the Internet.
[0080] The text encoder is used to encode the special characters [CLS] + the required text in the dataset into an embedding vector, which is defined as follows:
[0081]
[0082] Among them, text i Represents the i-th paragraph of text and takes out the first vector.
[0083] Will All vector search libraries Do the inner product of the vectors, remove the actual code corresponding to the text, and then take out the other code with the highest inner product score as the retrieval code.
[0084] The text and the search code are concatenated as input, and the correct target code corresponding to the text is used as the target output to form the generator module training set;
[0085] The longest common subsequence of the retrieval code and the correct code corresponding to the text is calculated, and the corresponding positions of the retrieval code and the longest common subsequence are set to 1, and the other positions are set to 0 as the target output. The text and the retrieval code are concatenated as input to form the reviewer module training set.
[0086] S102. Train the text-to-code generation model using the generator module training set and the reviewer module training set.
[0087] like Figure 3As shown in Figure 2, the text-to-code generation model consists of a generator module and a reviewer module. The generator module consists of a transformer-based encoder and decoder, each containing a learnable embedding matrix and multiple layers of transformer blocks with a self-attention mechanism. The reviewer module consists of the transformer-based encoder and an additional linear output layer.
[0088] Further, such as Figure 4 As shown, step S102 includes:
[0089] S1021. Train the generator module using the generator module training set; input the input data in the generator module training set into the trained generator module to obtain the first output code.
[0090] Further, such as Figure 5 As shown, step S1021 includes:
[0091] (1) The input (the concatenation of text and retrieval code) is tokenized by the tokenizer and the learnable embedding matrix W in the encoder is used. P ∈R n×d Get the embedding vector representation of the input text and code:
[0092] V i =u i W P
[0093] Where d represents the dimension of the embedding vector, u i Represents the number of the i-th word. V i Represents the embedding vector representation of the i-th word, and is also the input of the multi-layer transformer block model.
[0094] (2) For the above embedding vector V i , through multiple layers of encoding and decoding of transformer blocks using self-attention mechanism, its nonlinearity is increased, and then the code representation is output.
[0095] Assume that the positive sample is the correct feasible code corresponding to the text, and take the text and code concatenated as input. Let the generator module predict the next word based on the input, and get the code through autoregressive prediction:
[0096]
[0097] Y i (k) Represents the i-th segment of code generated by the generator module for the kth time; Represents the xth word of this code;
[0098] According to Y i (k) and the true answer Y i , use the cross entropy function to calculate the error value, and adjust the parameters of the generator module through gradient back propagation. The formula is as follows:
[0099]
[0100] S1022. Add the text plus the first output code to the generator module training set, input the training set to the generator model for continuous training and inference to obtain subsequent output codes, and replace the text plus the previous code with the text plus the subsequent code to continue training.
[0101] like Figure 6 As shown, the text and the first output code of the corresponding output are spliced as input, and the code corresponding to the text is used as output, and added to the generator module training set to obtain the expanded generator module training set; the generator module is trained using the expanded generator module training set; the input data in the expanded generator module training set is input into the trained generator module for continuous training and output to obtain subsequent output codes; the subsequent output codes are used to replace the retrieval codes in the input of the corresponding expanded generator module training set to obtain a new generator module training set.
[0102] S1023. Input the reviewer module dataset to the reviewer module, train and infer to obtain a masked output code.
[0103] The reviewer module is trained using the reviewer module training set; the input data in the reviewer module training set is input into the trained reviewer module to obtain a masked output code.
[0104] Further, such as Figure 7 As shown, step S1023 includes:
[0105] (1) Take the text and code concatenation as input and let the reviewer module output the feature vector of each word based on the input:
[0106] [X′,Y′ R ]=Reviewer(X,Y R )
[0107] Y′ R =(y′ R1 ,y′ R2 ,y′ R3 …y′ Rn )
[0108] Where X is the text, Y Ris the corresponding retrieval code, which is encoded by the reviewer module to obtain the corresponding feature vector of each word [X′, Y′ R ];y′ Rj Represents Y R The feature vector corresponding to the j-th word in .
[0109] (2) The feature vector Y′ of the retrieval code R The input is fed into the linear layer and then passed through a sigmoid activation function to obtain the probability p of whether each word needs to be covered (whether it is valid) j :
[0110] p j =sigmoid(W m y′ Rj +b m )
[0111] The probability distribution output for each code segment is as follows:
[0112] P i =(p1,p2...p n )
[0113] (3) The target answer Y is trained in the reviewer module i With P i The error value is calculated by the cross entropy function, and the reviewer module is adjusted by gradient backpropagation, including the parameters in the embedding matrix, transformer block and linear layer in the module. The formula is as follows:
[0114] Y i =(0,1,1,1,0…1)
[0115] Loss2(Y i ,P i )=-∑[y j log(p j )+(1-y j )log(1-p j )]
[0116] Y i The target obtained by processing the above retrieval code and the longest common subsequence is: the corresponding position is set to 1, and the other positions are set to 0.
[0117] Finally, a trained reviewer module is obtained.
[0118] S1024. Add the output code with text and mask to the generator module training set, and input the training set to the generator for final training.
[0119] The text and the corresponding masked output code are concatenated as input, and the correct target code corresponding to the text is used as the target output. They are added to the new generator module training set to obtain the final training set; the generator module is trained using the final training set to obtain a trained generator module.
[0120] S103. Generate code for the required text in real scenarios using the trained text-to-code generation model.
[0121] The trained text-to-code generation model is the trained generator module and the trained reviewer module obtained in step S102.
[0122] Further, such as Figure 8 As shown, step S103 includes three stages:
[0123] (1) Provide the demand text alone or the demand text combined with the retrieval code as input to the generator module, which then outputs the code;
[0124] (2) The code output by the demand text splicing generator module is provided as input to the reviewer module, which outputs the masked code;
[0125] (3) The required text is concatenated with the masked code as input and provided to the generator module, which then outputs the final code.
[0126] The requirement text refers to the textual description of the functionality that the user wants the code automatically generated by the model to have.
[0127] Those skilled in the art will appreciate that all or part of the steps in the method for implementing the above embodiments may be completed by instructing related hardware through a program, and the corresponding program may be stored in a computer-readable storage medium.
[0128] It should be noted that although the method operations of the above embodiments are described in a particular order in the accompanying drawings, this does not require or imply that the operations must be performed in this particular order, or that all of the illustrated operations must be performed to achieve the desired results. Rather, the depicted steps may be performed in a different order. Additionally or alternatively, certain steps may be omitted, multiple steps may be combined into a single step, and / or a single step may be broken down into multiple steps.
[0129] Example 2:
[0130] like Figure 9 As shown, this embodiment provides a reviewer-based iterative self-enhancement generation system, which includes a vector retrieval library construction module 901, a code retrieval module 902, a training set construction module 903, a training module 904, and a code generation module 905, wherein:
[0131] The vector retrieval library construction module 901 is used to construct a vector retrieval library based on the acquired data set; the samples in the data set include text and corresponding codes;
[0132] The code retrieval module 902 is used to retrieve the corresponding code according to the text in the vector retrieval library;
[0133] The training set construction module 903 takes the text and the corresponding search code as input data, the code corresponding to the text as first output data, and uses the input data and the first output data as samples in the training set of the generator module; obtains second output data based on the search code corresponding to the text and the corresponding code, and uses the input data and the second output data as samples in the training set of the reviewer module;
[0134] The training module 904 is used to train the generator module using the generator module training set; input the input data in the generator module training set into the trained generator module to obtain the first output code; the first output code and the text corresponding to the generator module training set, as well as the code corresponding to the text, are added as samples to the generator module training set, and the generator module is trained again using the expanded generator module training set; the input data in the expanded generator module training set is input into the retrained generator module to obtain subsequent output codes; the retrieval code in the expanded generator module training set is replaced with the corresponding subsequent output code to obtain a new generator module training set; the reviewer module is trained using the reviewer module training set, and the input data in the reviewer module training set is input into the trained reviewer module to obtain a masked output code; the masked output code and the text corresponding to the reviewer module training set, as well as the code corresponding to the text, are added as samples to the new generator module training set to obtain a final training set; the generator module is trained using the final training set to obtain a trained generator module;
[0135] The code generation module 905 is used to generate codes corresponding to the requirement text according to the trained generator module and the trained reviewer module.
[0136] The specific implementation of each module in this embodiment can be found in the above-mentioned embodiment 1, and will not be described one by one here; it should be noted that the system provided in this embodiment is only illustrated by the division of the above-mentioned functional modules. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure can be divided into different functional modules to complete all or part of the functions described above.
[0137] Example 3:
[0138] This example introduces a computer device, which can be a computer, such as Figure 10 As shown, it includes a system bus 1001 of a computer. Also connected to the bus are a processor 1002, a memory, a network interface 1003, an input device 1004, and a display 1005. Among them, the processor is responsible for calculation and control, and the memory includes an internal memory 1006 and a non-volatile storage medium 1007. The internal memory 1006 provides an operating environment for the operating system and computer programs, and the non-volatile storage medium 1007 stores the operating system, computer programs, and databases. When the processor 1002 executes the computer program stored in the memory, it implements the reviewer-based iterative self-enhancement generation method in the above-mentioned embodiment 1, as follows:
[0139] Based on the acquired data set, a vector retrieval library is constructed; the samples in the data set include text and corresponding codes;
[0140] In the vector retrieval library, the corresponding code is retrieved according to the text;
[0141] The text and the corresponding search code are concatenated as input data, the code corresponding to the text is used as the first output data, and the input data and the first output data are used as samples in the training set of the generator module; the second output data is obtained according to the search code corresponding to the text and the corresponding code, and the input data and the second output data are used as samples in the training set of the reviewer module;
[0142] The generator module is trained using the generator module training set; the input data in the generator module training set is input into the trained generator module to obtain a first output code; the first output code and the text corresponding to the generator module training set, as well as the code corresponding to the text, are added as samples into the generator module training set, and the generator module is trained again using the expanded generator module training set; the input data in the expanded generator module training set is input into the retrained generator module to obtain a subsequent output code; the search code in the expanded generator module training set is replaced with the corresponding subsequent output code to obtain a new generator module training set;
[0143] The reviewer module is trained using the reviewer module training set, and the input data in the reviewer module training set is input into the trained reviewer module to obtain a masked output code;
[0144] The masked output code, the corresponding text in the reviewer module training set, and the code corresponding to the text are added as samples to the new generator module training set to obtain the final training set; the generator module is trained using the final training set to obtain a trained generator module;
[0145] Based on the trained generator module and the trained reviewer module, the code corresponding to the requirement text is generated.
[0146] Example 4:
[0147] This embodiment provides a storage medium, which is a computer-readable storage medium and stores a computer program. When the computer program is executed by a processor, the reviewer-based iterative self-enhancement generation method of the above embodiment 1 is implemented as follows:
[0148] Based on the acquired data set, a vector retrieval library is constructed; the samples in the data set include text and corresponding codes;
[0149] In the vector retrieval library, the corresponding code is retrieved according to the text;
[0150] The text and the corresponding search code are concatenated as input data, the code corresponding to the text is used as the first output data, and the input data and the first output data are used as samples in the training set of the generator module; the second output data is obtained according to the search code corresponding to the text and the corresponding code, and the input data and the second output data are used as samples in the training set of the reviewer module;
[0151] The generator module is trained using the generator module training set; the input data in the generator module training set is input into the trained generator module to obtain a first output code; the first output code and the text corresponding to the generator module training set, as well as the code corresponding to the text, are added as samples into the generator module training set, and the generator module is trained again using the expanded generator module training set; the input data in the expanded generator module training set is input into the retrained generator module to obtain a subsequent output code; the search code in the expanded generator module training set is replaced with the corresponding subsequent output code to obtain a new generator module training set;
[0152] The reviewer module is trained using the reviewer module training set, and the input data in the reviewer module training set is input into the trained reviewer module to obtain a masked output code;
[0153] The masked output code, the corresponding text in the reviewer module training set, and the code corresponding to the text are added as samples to the new generator module training set to obtain the final training set; the generator module is trained using the final training set to obtain a trained generator module;
[0154] Based on the trained generator module and the trained reviewer module, the code corresponding to the requirement text is generated.
[0155] It should be noted that the computer-readable storage medium of the present embodiment may be a computer-readable signal medium or a computer-readable storage medium or any combination thereof. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or component, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to, an electrical connection having one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.
[0156] In summary, the reviewer-based iterative self-enhanced generation method, system, computer device and storage medium provided by the present invention collect data from public datasets to construct a vector retrieval library, a generator training set and a reviewer training set based on the longest common subsequence; use the output of the generator module and the code in the vector retrieval library to construct a training set with diverse retrieval code quality distribution; add additional reviewer modules to provide feedback to the generator module to help it reduce hallucinations and escape from local optimal solutions; use the trained generator module and reviewer module to generate code for demand text in real scenarios to obtain the answers that users want.
[0157] The above is only a preferred embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any technician familiar with the technical field can make equivalent replacements or changes based on the technical solution and inventive concept of the present invention within the scope disclosed by the present invention, which falls within the scope of protection of the present invention.
Claims
1. A reviewer-based iterative self-enhancement generation method, characterized in that The method comprises: Based on the acquired data set, a vector retrieval library is constructed; the samples in the data set include text and corresponding codes; In the vector retrieval library, the corresponding code is retrieved according to the text; The text and the corresponding search code are concatenated as input data, the code corresponding to the text is used as the first output data, and the input data and the first output data are used as samples in the training set of the generator module; the second output data is obtained according to the search code corresponding to the text and the corresponding code, and the input data and the second output data are used as samples in the training set of the reviewer module; The generator module is trained using the generator module training set; the input data in the generator module training set is input into the trained generator module to obtain a first output code; the first output code and the text corresponding to the generator module training set, as well as the code corresponding to the text, are added as samples into the generator module training set, and the generator module is trained again using the expanded generator module training set; the input data in the expanded generator module training set is input into the retrained generator module to obtain a subsequent output code; the search code in the expanded generator module training set is replaced with the corresponding subsequent output code to obtain a new generator module training set; The reviewer module is trained using the reviewer module training set, and the input data in the reviewer module training set is input into the trained reviewer module to obtain a masked output code; The masked output code, the corresponding text in the reviewer module training set, and the code corresponding to the text are added as samples to the new generator module training set to obtain the final training set; the generator module is trained using the final training set to obtain a trained generator module; Based on the trained generator module and the trained reviewer module, the code corresponding to the requirement text is generated.
2. The iterative self-enhancement generation method according to claim 1, characterized in that: In the vector search library, the corresponding code is searched according to the text, including: Use the text encoder to encode special characters [CLS] and text in the dataset into embedding vectors: Where, Represents text i The first vector in the corresponding embedding vector, text i Represents the i-th paragraph of text; Will All vector search libraries Do the inner product of the vector and remove the text i After finding the corresponding code, take out the code with the highest internal score as the corresponding code.
3. The iterative self-enhancement generation method according to any one of claims 1 to 2, characterized in that: The step of constructing a vector search library based on the acquired data set includes: Use the code encoder to encode the special characters [CLS] and the codes in the dataset into embedding vectors: Where, Code i The first vector in the corresponding embedding vector, Code i Represents the i-th section of code; Will The corresponding codes are stored in the database as a vector retrieval library.
4. The iterative self-enhancement generation method according to any one of claims 1 to 2, characterized in that: Before building the vector retrieval library, the dataset is processed, including: Filter out invalid information in the data set, remove the code that is wrong and cannot run, and the text that does not match at all or is too short.
5. The iterative self-enhancement generation method according to any one of claims 1 to 2, characterized in that: The obtaining of the second output data according to the search code corresponding to the text and the corresponding code includes: The longest common subsequence is obtained based on the retrieval code corresponding to the text and the corresponding code, and the corresponding positions of the retrieval code and the longest common subsequence are set to fixed values, and the other positions are set to 0 as the second output data.
6. The iterative self-enhancement generation method according to any one of claims 1 to 2, characterized in that: The generator module consists of a transformer-based encoder and decoder, each of which contains a learnable embedding matrix and multiple layers of transformer blocks with a self-attention mechanism.
7. The iterative self-enhancement generation method according to any one of claims 1 to 2, characterized in that: The reviewer module consists of an encoder in the transformer architecture and an additional linear output layer.
8. The iterative self-enhancement generation method according to any one of claims 1 to 2, characterized in that: The code corresponding to the requirement text is generated according to the trained generator module and the trained reviewer module, including: Input the required text into the trained generator module to obtain the code; The requirement text and code are spliced and input into the trained reviewer module to obtain the masked code; The required text and masked code are concatenated and input into the trained generator module to generate high-quality code.
9. A reviewer-based iterative self-enhancing generation system, characterized by: The system comprises: A vector retrieval library construction module is used to construct a vector retrieval library based on the acquired data set; the samples in the data set include text and corresponding codes; Code retrieval module, used to retrieve corresponding codes based on text in the vector retrieval library; The training set construction module takes the text and the corresponding search code as input data, the code corresponding to the text as the first output data, and uses the input data and the first output data as samples in the generator module training set; obtains the second output data based on the search code corresponding to the text and the corresponding code, and uses the input data and the second output data as samples in the reviewer module training set; A training module is used to train the generator module using the generator module training set; input the input data in the generator module training set into the trained generator module to obtain the first output code; the first output code and the text corresponding to the generator module training set, as well as the code corresponding to the text are added as samples to the generator module training set, and the generator module is trained again using the expanded generator module training set; the input data in the expanded generator module training set is input into the retrained generator module to obtain subsequent output codes; the retrieval code in the expanded generator module training set is replaced with the corresponding subsequent output code to obtain a new generator module training set; the reviewer module is trained using the reviewer module training set, and the input data in the reviewer module training set is input into the trained reviewer module to obtain a masked output code; the masked output code and the text corresponding to the reviewer module training set, as well as the code corresponding to the text are added as samples to the new generator module training set to obtain a final training set; the generator module is trained using the final training set to obtain a trained generator module; The code generation module is used to generate code corresponding to the requirement text based on the trained generator module and the trained reviewer module.
10. A storage medium storing a program, characterized in that: When the program is executed by a processor, the iterative self-enhancement generation method according to any one of claims 1 to 8 is implemented.
Citation Information
Patent Citations
Code generation method and device, electronic equipment and storage medium
CN113434136A
Code generation method and device, storage medium and electronic equipment
CN116166271A