Retrieval-enhanced code generation method, device, electronic device and storage medium

By constructing a word vector database based on cosine similarity and a positive and negative feedback training mechanism, the accuracy of code generation is improved, and the problem of insufficient code generation capabilities of general large models in the vertical field is solved, and the generated code is more in line with user needs.

CN118778941BActive Publication Date: 2024-11-26LONGSHINE TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411261230.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-10
Publication Date
2024-11-26
Estimated Expiration
2044-09-10

AI Technical Summary

Technical Problem

The general model cannot learn business project code, resulting in low code generation accuracy in projects in vertical fields and cannot effectively provide code assistance to vertical fields.

Method used

By entering the question text data into the word embedding model, a problem word vector is generated, and the word vectors in the code warehouse and development documents are filtered based on cosine similarity, a database is constructed, and a text combination input code is used to generate a large language model, combining positive and negative feedback matching pairs Carry out model training, update model parameters, and improve the accuracy of code generation.

Benefits of technology

It realizes effective retrieval of vertical domain problems, reduces the cost of manually labeling data, and the generated code is more in line with user programming preferences, and improves the accuracy and user satisfaction of code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118778941B_ABST
    Figure CN118778941B_ABST
Patent Text Reader

Abstract

The present invention provides a code generation method, device, electronic device and storage medium for retrieval enhancement, which relates to the field of computer technology, wherein the method comprises: inputting question text data into a word embedding model to obtain a question word vector of the question text data; based on cosine similarity, screening the word vectors in a first database and the word vectors in a second database to determine a preset number of retrieval contents similar to the question word vector; text combination of the preset number of retrieval contents to obtain input text, and inputting the input text into a code generation large language model to obtain an output code fragment. By screening the word vectors in the first database and the word vectors in the second database to determine the preset number of retrieval contents similar to the question word vector, retrieval enhancement of the question text data is achieved, the cost of manually annotating data is reduced, the accuracy of the generated code is improved, and the generated code is more in line with the user's programming preferences.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a retrieval-enhanced code generation method, device, electronic device and storage medium. Background Art

[0002] In the field of code generation, many general large models have emerged. Based on these general large models, there are many code assistants in the form of plug-ins for developers to use, so that developers can automatically obtain output code based on input problems.

[0003] Since general large models cannot learn business project codes, they cannot provide code assistance for vertical projects. Even if some companies provide private deployment services, based on code and natural language retrieval and recall combined with large models for question and answer, due to the strong uniqueness of the business code field, the ability to retrieve and generate business problems in real scenarios is very poor, resulting in low accuracy of the generated code. Summary of the invention

[0004] The present invention provides a retrieval-enhanced code generation method, device, electronic device and storage medium, which are used to improve the accuracy of code generation.

[0005] The present invention provides a retrieval-enhanced code generation method, comprising the following steps:

[0006] Inputting the question text data into the word embedding model to obtain the question word vector of the question text data;

[0007] Based on cosine similarity, word vectors in a first database and word vectors in a second database are screened to determine a preset number of search contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in a code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in development documents into the word embedding model;

[0008] Text combination is performed on the preset number of search contents and the question text data to obtain input text, and the input text is input into a code-generated large language model to obtain an output code fragment output by the code-generated large language model.

[0009] According to a search-enhanced code generation method provided by the present invention, the text combination of the preset number of search contents and the question text data to obtain the input text includes:

[0010] Based on the prompt engineering method, a code generation prompt word template is constructed, wherein the code generation prompt word template is used to generate output in the form of a thought chain from multiple inputs;

[0011] Based on the code-generated prompt word template, the preset number of search contents and the question text data are combined to obtain an input text in the form of a thought chain.

[0012] A retrieval-enhanced code generation method according to the present invention further includes:

[0013] Collecting model input and model output of the code-generated large language model, and determining user feedback information on each model output;

[0014] Associating the model output with its corresponding feedback information to obtain positive and negative feedback matching pairs of the code generation large language model;

[0015] The code generation large language model is trained based on the positive and negative feedback matching pairs, the model input, and the model output, and model parameters of the code generation large language model are updated based on the training results.

[0016] According to a retrieval-enhanced code generation method provided by the present invention, the code generation large language model is trained based on the positive and negative feedback matching pairs, the model input and the model output, and the model parameters of the code generation large language model are updated based on the training results, including:

[0017] Based on the target reward model, the advantage of the positive and negative feedback matching pairs, the model input and the model output are estimated to obtain a reward value;

[0018] Inputting the positive and negative feedback matching pairs, model input and model output into the code generation large language model to obtain generation likelihood;

[0019] Based on the KL divergence between the original code-generated large language model and the code-generated large language model, the reward value, and the generation likelihood, a loss is determined, and backpropagation is performed based on the loss to update model parameters of the code-generated large language model.

[0020] According to a retrieval-enhanced code generation method provided by the present invention, the model parameters of the target reward model are updated by back-propagation training based on the distance values ​​determined by the positive and negative feedback matching pairs as losses.

[0021] According to a retrieval-enhanced code generation method provided by the present invention, the training process of the word embedding model includes:

[0022] Collecting model input and model output of the code-generated large language model, and determining user feedback information on each model output;

[0023] Based on the model output and the feedback information corresponding to the model, and the question text data, determine the positive sample of the code-generated large language model and the negative sample of the code-generated large language model;

[0024] Based on contrastive learning, the word embedding model is trained according to the positive samples and the negative samples to update model parameters of the word embedding model.

[0025] The present invention also provides a retrieval-enhanced code generation device, comprising the following modules:

[0026] A word vector generation module, used to input question text data into a word embedding model to obtain a question word vector for the question text data;

[0027] A retrieval module, configured to screen the word vectors in the first database and the word vectors in the second database based on cosine similarity, and determine a preset number of retrieval contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in the code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in the development documents into the word embedding model;

[0028] The code generation module is used to perform text combination on the preset number of search contents and the question text data to obtain input text, and input the input text into the code generation large language model to obtain an output code fragment output by the code generation large language model.

[0029] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the retrieval-enhanced code generation method described above is implemented.

[0030] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements any of the retrieval-enhanced code generation methods described above.

[0031] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the retrieval-enhanced code generation method described above is implemented.

[0032] The retrieval-enhanced code generation method, device, electronic device and storage medium provided by the present invention, through the first database constructed by code snippets in the code warehouse and the second database pre-constructed by text snippets in the development document, based on cosine similarity, the word vectors in the first database and the word vectors in the second database are screened, and a preset number of retrieval contents similar to the question word vector are determined, thereby realizing retrieval enhancement of question text data, solving the problem of difficulty in collecting preference training data, reducing the cost of manually annotating data, and improving the accuracy of generated code. At the same time, based on this method, user feedback can be collected online, converted into training data, and the model can be iterated online, so that the generated code is more in line with the user's programming preferences. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] In order to more clearly illustrate the technical solutions in the present invention or 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 some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0034] Figure 1 It is a flowchart of the retrieval-enhanced code generation method provided by the present invention.

[0035] Figure 2 It is a schematic diagram of the construction process of the second database provided by the present invention.

[0036] Figure 3 It is a schematic diagram of the code generation process for retrieval enhancement provided by the present invention.

[0037] Figure 4 It is a schematic diagram of the training process of the code-generated large language model provided by the present invention.

[0038] Figure 5 It is a schematic diagram of the device structure of the retrieval-enhanced code generation device provided by the present invention.

[0039] Figure 6 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0040] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings 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 creative work are within the scope of protection of the present invention.

[0041] Figure 1 : is a flow chart of the retrieval enhanced code generation method provided by the present invention, such as Figure 1 As shown, the method includes the following:

[0042] Step 110, inputting the question text data into a word embedding model to obtain a question word vector of the question text data;

[0043] Step 120, based on cosine similarity, the word vectors in the first database and the word vectors in the second database are screened to determine a preset number of search contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in the code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in the development document into the word embedding model;

[0044] Step 130, text combination is performed on the preset number of search contents and the question text data to obtain input text, and the input text is input into a code-generated large language model to obtain an output code fragment output by the code-generated large language model.

[0045] The execution subject of the retrieval enhanced code generation method provided by the present invention can be an electronic device, a component in an electronic device, an integrated circuit, or a chip. The electronic device can be a mobile electronic device or a non-mobile electronic device. Exemplarily, the mobile electronic device can be a mobile phone, a tablet computer, a notebook computer, a palmtop computer, an ultra-mobile personal computer (UMPC), a netbook, or a personal digital assistant (PDA), etc., and the non-mobile electronic device can be a server, a network attached storage (NAS), or a personal computer (PC), etc., which is not specifically limited by the present invention.

[0046] The following takes the computer executing the retrieval enhanced code generation method provided by the present invention as an example to explain the technical solution of the present invention in detail.

[0047] In step 110, the question text data is input into a word embedding model to obtain a question word vector of the question text data.

[0048] The question text data is based on any question that the user needs to generate code for. The generated text data may be, for example, "generate a code snippet to establish a TCP connection."

[0049] The word embedding model is a technique in natural language processing (NLP) that aims to map words or phrases in the vocabulary into a high-dimensional vector space so that these vectors can capture the semantic relationship between words. These vectors are usually called word vectors. Among them, word embedding models can include Word2Vec models, BERT models, etc.

[0050] The question text data is input into the word embedding model, and the question word vector of the question text data is determined to obtain the semantic representation of the question text data.

[0051] In step 120, based on cosine similarity, the word vectors in the first database and the word vectors in the second database are screened to determine a preset number of search contents similar to the problem word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in the code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in the development documents into the word embedding model.

[0052] The first database and the second database are pre-built. The code snippets in the code repository are input into the word embedding model to obtain a vector representation of each code snippet, and the first database is built based on the obtained multiple code snippet word vectors.

[0053] Among them, the code repository is based on a pre-collected data set containing a large number of code snippets, which can be a data set built from the internal code of a company.

[0054] Optionally, the specific construction process of the first database can be: first, the code in the code repository is divided into code snippets with a fixed number of lines according to rules, and then each code snippet is input into a word embedding model (the word embedding model converts the code snippet into a fixed-length digital sequence representation, which serves as the semantic representation of the code snippet) to obtain the code snippet word vector, and finally each code snippet word vector is stored in the first database.

[0055] The text fragments in the development document are input into the word embedding model to obtain the vector representation of each text fragment, and a second database is constructed based on the obtained multiple development document word vectors.

[0056] The development document is based on a pre-collected data set containing program development information, and may be a data set constructed based on internal development document data of a company.

[0057] Optionally, the specific construction process of the second database can be as follows: Figure 2 The schematic diagram of the construction process of the second database provided by the present invention is shown as follows:

[0058] First, the text in the development document is segmented into text segments with a fixed number of words based on preset rules, and then each text segment is input into the word embedding model to obtain the development document word vector with the corresponding semantic representation, and finally each development document word vector is stored in the second database.

[0059] For each word vector in the database (whether it is a code snippet word vector or a development document word vector), calculate the cosine similarity between it and the question word vector. Cosine similarity is an indicator that measures the degree of similarity between two vectors in direction. The value range is [-1, 1], where 1 means completely similar, -1 means completely opposite, and 0 means orthogonal (i.e., neither similar nor opposite).

[0060] According to the calculated cosine similarity, the word vectors in the database are sorted, and a preset number of word vectors with the highest similarity are selected as the search results. The code snippets or development document text snippets corresponding to these word vectors are the search content most relevant to the user's question.

[0061] Optionally, the question word vector can be used to calculate cosine similarity (cosine similarity represents the distance between two high-dimensional vectors in their respective high-dimensional spaces) with each word vector representation stored in the first database and the second database. This similarity is used to measure the relevance between the user's question and the code snippet word vector stored in the first database or the text snippet word vector stored in the second database. Then, the retrieved code snippet word vectors and text snippet word vectors are sorted in descending order of relevance, and finally the top five sorted snippets are used as the retrieved content.

[0062] In step 130, text combination is performed on the preset number of search contents and the question text data to obtain input text, and the input text is input into a code generation large language model to obtain an output code fragment output by the code generation large language model.

[0063] Text combination is the process of combining multiple retrieved contents (whether code snippets or development document text snippets, as well as user questions) into a coherent input text. The key to this step is how to effectively organize and present this information so that the code generation language model can understand it and generate useful code based on it.

[0064] Optionally, add some contextual hints at the beginning of the input text to help the model understand the user's intent and needs. These hints can include a description of the problem, an overview of the target function, or example input / output. The input text can also be organized into a more structured form, such as using Markdown, code comments, or task-specific templates. This helps the model better understand the input information and generate code that meets expectations. Before submitting the final input text to the model, perform a thorough cleaning and correction to ensure that there are no grammatical errors, spelling errors, or unnecessary redundant information in the text.

[0065] The constructed input text is input into the code generation language model, and the model will generate the corresponding output code snippet based on the content and context information of the input text.

[0066] Optionally, the code generation process to implement the retrieval enhancement can be as follows Figure 3 The schematic diagram of the retrieval-enhanced code generation process provided by the present invention is shown as follows. Input the question text data into the word embedding model to obtain the semantic representation of the user question; calculate the correlation between the semantic representation of the user question and the semantic representation of each code snippet in the first database; calculate the correlation between the semantic representation of the user question and the semantic representation of each text snippet in the second database; select the five snippets with the highest correlation; put the user question and the five snippets with the highest correlation into the prompt word template to obtain a complete prompt word; input the complete prompt word into the code generation large language model; the code generation large language model generates content similar to "unable to generate code" or generates a code snippet.

[0067] The model first needs to deeply understand and analyze the input text to identify key information, requirements, and constraints. Based on the understanding and analysis results of the input text, the model will generate multiple candidate code snippets. These candidate code snippets may cover different implementation methods, algorithm selections, and optimization strategies. The model will evaluate and compare these candidate code snippets and select the code snippet that best meets the intent and requirements of the input text as the final output.

[0068] Optionally, based on the way of constructing the code assistant, a retrieval-enhanced code generation method can be integrated into the code assistant to realize the automatic code generation process.

[0069] The retrieval-enhanced code generation method provided by the present invention uses a first database constructed by code snippets in a code warehouse and a second database pre-constructed by text snippets in development documents, and based on cosine similarity, screens the word vectors in the first database and the word vectors in the second database, determines a preset number of retrieval contents similar to the question word vector, realizes retrieval enhancement of question text data, solves the problem of difficulty in collecting preferred training data, reduces the cost of manually annotating data, and improves the accuracy of generated code. At the same time, based on this method, user feedback can be collected online, converted into training data, and the model can be iterated online, so that the generated code is more in line with the user's programming preferences.

[0070] In one embodiment, the preset number of search contents and the question text data are combined to obtain input text, including: based on the prompt engineering method, constructing a code generation prompt word template, the code generation prompt word template is used to generate output in the form of a thinking chain from multiple inputs; based on the code generation prompt word template, the preset number of search contents and the question text data are combined to obtain the input text in the form of a thinking chain.

[0071] Clarify the specific goals and application scenarios of code retrieval, such as finding the implementation of a specific algorithm, a code snippet of a specific function, or optimizing existing code, etc. This helps determine the key information and logical structure that the prompt word template needs to cover.

[0072] After constructing an effective code generation prompt word template, it can be applied to a preset number of search contents. By combining the search contents and the user's questions in a text combination manner, input text in the form of a thought chain is generated.

[0073] The final output should be prompt words that stimulate the code generation model to think and analyze. Such input text helps the code generation model better understand user needs, thereby providing more accurate and relevant code generation results.

[0074] The retrieval-enhanced code generation method provided by the present invention uses thought chain prompt engineering technology to guide the analysis of retrieved reference content and then generate answers, so that the generated thought chain simulates the human thinking process, improves the quality of input text, and provides traceability and explainability for subsequently generated content.

[0075] In one embodiment, the method further includes: collecting model input and model output of the code generation large language model, and determining user feedback information on each model output; associating the model output with its corresponding feedback information to obtain a positive and negative feedback matching pair of the code generation large language model; training the code generation large language model based on the positive and negative feedback matching pairs, the model input and the model output, and updating the model parameters of the code generation large language model based on the training results.

[0076] During the use of the large language model for code generation, the large language model for code generation is continuously iterated and upgraded.

[0077] Specifically, the model input and model output of the code-generated large language model can be collected, and based on the user's rating or evaluation of the output results, the user's feedback information on the model output can be determined.

[0078] Associate each model output with its corresponding user feedback information to ensure that each output has a corresponding feedback record. Construct positive and negative feedback matching pairs based on the positivity of user feedback (such as positive evaluation, negative evaluation).

[0079] The positive and negative feedback matching pairs, model input and model output are used as training data to train the code generation large language model, so as to realize the continuous optimization and update process of the model parameters of the code generation large language model.

[0080] In one embodiment, the code generation large language model is trained based on the positive and negative feedback matching pairs, the model input and the model output, and the model parameters of the code generation large language model are updated based on the training results, including: based on a target reward model, the positive and negative feedback matching pairs, the model input and the model output are estimated to obtain a reward value; the positive and negative feedback matching pairs, the model input and the model output are input into the code generation large language model to obtain a generation likelihood; based on the KL divergence between the original code generation large language model and the code generation large language model, the reward value and the generation likelihood, a loss is determined, and back propagation is performed based on the loss to update the model parameters of the code generation large language model.

[0081] The model parameters of the target reward model are updated by back-propagation training based on the distance values ​​determined by the positive and negative feedback matching pairs as losses.

[0082] The target reward model is an independent model that can evaluate the quality of generated code. It learns how to score different code outputs based on user feedback (especially positive and negative feedback). This score represents the degree of goodness of the generated code relative to the expected target. Training the target reward model: First, use the collected positive and negative feedback matching pairs to train the reward model. Positive feedback (such as high satisfaction scores or unmodified code) should be given higher reward values, while negative feedback (such as low satisfaction scores or heavily modified code) should be given lower reward values. A distance value is calculated based on each positive and negative feedback pair sample in each batch, and the target reward model parameters are updated based on the average distance value obtained for each batch as the loss backpropagation of this batch of data.

[0083] The advantage estimate is used to put the current input and the current model output into the target reward model to get a reward value. If it is a positive feedback matching pair, the advantage estimate is higher, and if it is a negative feedback matching pair, the advantage estimate is lower.

[0084] For each positive and negative feedback matching pair, calculate its corresponding advantage value. Positive feedback matching pairs usually have higher advantage values ​​because they represent high-quality code generated by the model; while negative feedback matching pairs may have lower or even negative advantage values.

[0085] The code generates a large language model training process diagram as follows Figure 4 The training process of the code-generated large language model provided by the present invention is shown in the schematic diagram.

[0086] The model input and model output of the code-generated large language model are collected, and user feedback information on each model output is determined.

[0087] The model output is associated with its corresponding feedback information to obtain positive and negative feedback matching pairs of the code generation large language model.

[0088] When training a large language model for code generation, a loss function needs to be designed to guide the model parameter update. This loss function should comprehensively consider the generative likelihood of the original model, the KL (Kullback-Leibler Divergence) divergence (which measures the difference between two probability distributions), and the reward value.

[0089] Generation likelihood: measures the probability that a model will generate a given output sequence. In code generation tasks, a higher generation likelihood usually means that the model is more likely to generate human-readable, syntactically correct code.

[0090] KL divergence: Used to prevent the model from deviating too far from the distribution of the original code generation large language model during the optimization process. This helps maintain the stability and generalization ability of the model.

[0091] Reward value: Incorporate reward value into the loss function to encourage the model to generate code with higher reward value. This helps the model to continuously optimize its generation ability based on user feedback.

[0092] After obtaining the loss function, the loss value is propagated back to each layer of the model through the back propagation algorithm to calculate the gradient of the parameters of each layer.

[0093] Parameter update: Use an optimization algorithm (such as gradient descent) to update the model's parameters based on the calculated gradients. This process aims to minimize the loss function, thereby improving the quality of the model-generated code and user satisfaction.

[0094] In one embodiment, the training process of the word embedding model includes: collecting model input and model output of the code generation large language model, and determining user feedback information on each model output; determining positive samples of the code generation large language model and negative samples of the code generation large language model based on the model output and the feedback information corresponding to the model output, and the question text data; based on contrastive learning, training the word embedding model according to the positive samples and the negative samples to update the model parameters of the word embedding model.

[0095] Based on user feedback, the model output is divided into positive samples and negative samples. Positive samples are usually those code outputs that are highly rated by users, perform well in real applications, or meet specific quality standards; while negative samples are those code outputs that are low rated, contain errors, or perform poorly in real applications.

[0096] Contrastive learning is a method of learning representations by comparing the similarities between pairs of samples. In the training of word embedding models, contrastive learning frameworks such as Siamese networks and Triplet networks can be used. These frameworks usually contain two or three parallel neural network branches to process paired or grouped samples and optimize model parameters by comparing their outputs.

[0097] The positive and negative sample pairs are input into the contrastive learning framework, the loss value is calculated through forward propagation, and the parameters of the word embedding model are updated using the backpropagation algorithm. During the training process, the gradient descent method or its variants can be used to optimize the loss function and gradually adjust the weights and bias terms of the model.

[0098] The training process is an iterative optimization process. In each iteration cycle, new sample pairs are used to update the model parameters and evaluate the performance of the model on the validation set. Based on the validation results, hyperparameters such as learning rate, batch size, and training rounds can be adjusted to further optimize model performance.

[0099] The retrieval-enhanced code generation device provided by the present invention is described below. The retrieval-enhanced code generation device described below and the retrieval-enhanced code generation method described above can be referenced to each other.

[0100] like Figure 5 As shown, the device comprises:

[0101] A word vector generation module 510 is used to input question text data into a word embedding model to obtain a question word vector for the question text data;

[0102] A retrieval module 520 is used to screen the word vectors in the first database and the word vectors in the second database based on cosine similarity to determine a preset number of retrieval contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in the code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in the development documents into the word embedding model;

[0103] The code generation module 530 is used to perform text combination on the preset number of search contents and the question text data to obtain input text, and input the input text into the code generation large language model to obtain an output code fragment output by the code generation large language model.

[0104] The retrieval-enhanced code generation device provided by the present invention uses a first database constructed by code snippets in a code warehouse and a second database pre-constructed by text snippets in development documents, and based on cosine similarity, screens the word vectors in the first database and the word vectors in the second database, determines a preset number of retrieval contents similar to the question word vector, realizes retrieval enhancement of question text data, solves the problem of difficulty in collecting preference training data, reduces the cost of manually annotating data, and improves the accuracy of generated code. At the same time, based on this method, user feedback can be collected online, converted into training data, and the model can be iterated online, so that the generated code is more in line with the user's programming preferences.

[0105] In one embodiment, the code generation module 530 is specifically used to:

[0106] The step of combining the preset number of search contents and the question text data to obtain the input text comprises:

[0107] Based on the prompt engineering method, a code generation prompt word template is constructed, wherein the code generation prompt word template is used to generate output in the form of a thought chain from multiple inputs;

[0108] Based on the code-generated prompt word template, the preset number of search contents and the question text data are combined to obtain an input text in the form of a thought chain.

[0109] In one embodiment, the code generation module 530 is further specifically used for:

[0110] Collecting model input and model output of the code-generated large language model, and determining user feedback information on each model output;

[0111] Associating the model output with its corresponding feedback information to obtain positive and negative feedback matching pairs of the code generation large language model;

[0112] The code generation large language model is trained based on the positive and negative feedback matching pairs, the model input, and the model output, and model parameters of the code generation large language model are updated based on the training results.

[0113] In one embodiment, the code generation module 530 is further specifically used for:

[0114] The step of training the code generation large language model based on the positive and negative feedback matching pairs, the model input, and the model output, and updating the model parameters of the code generation large language model based on the training results, includes:

[0115] Based on the target reward model, the advantage of the positive and negative feedback matching pairs, the model input and the model output are estimated to obtain a reward value;

[0116] Inputting the positive and negative feedback matching pairs, model input and model output into the code generation large language model to obtain generation likelihood;

[0117] Based on the KL divergence between the original code-generated large language model and the code-generated large language model, the reward value, and the generation likelihood, a loss is determined, and backpropagation is performed based on the loss to update model parameters of the code-generated large language model.

[0118] In one embodiment, the code generation module 530 is further specifically used for:

[0119] The model parameters of the target reward model are updated by back-propagation training based on the distance values ​​determined by the positive and negative feedback matching pairs as losses.

[0120] In one embodiment, the word vector generation module 510 is specifically used for:

[0121] The training process of the word embedding model includes:

[0122] Collecting model input and model output of the code-generated large language model, and determining user feedback information on each model output;

[0123] Based on the model output and the feedback information corresponding to the model, and the question text data, determine the positive sample of the code-generated large language model and the negative sample of the code-generated large language model;

[0124] Based on contrastive learning, the word embedding model is trained according to the positive samples and the negative samples to update model parameters of the word embedding model.

[0125] Figure 6 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 6 As shown, the electronic device may include: a processor 610, a communication interface 620, a memory 630 and a communication bus 640, wherein the processor 610, the communication interface 620 and the memory 630 communicate with each other through the communication bus 640. The processor 610 may call the logic instructions in the memory 630 to execute the code generation method for retrieval enhancement, the method comprising: inputting question text data into a word embedding model to obtain a question word vector of the question text data;

[0126] Based on cosine similarity, word vectors in a first database and word vectors in a second database are screened to determine a preset number of search contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in a code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in development documents into the word embedding model;

[0127] Text combination is performed on the preset number of search contents and the question text data to obtain input text, and the input text is input into a code-generated large language model to obtain an output code fragment output by the code-generated large language model.

[0128] In addition, the logic instructions in the above-mentioned memory 630 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0129] On the other hand, the present invention also provides a computer program product, the computer program product includes a computer program, the computer program can be stored in a non-transitory computer-readable storage medium, when the computer program is executed by a processor, the computer can execute the retrieval enhancement code generation method provided by the above methods, the method includes: inputting question text data into a word embedding model to obtain a question word vector of the question text data;

[0130] Based on cosine similarity, word vectors in a first database and word vectors in a second database are screened to determine a preset number of search contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in a code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in development documents into the word embedding model;

[0131] Text combination is performed on the preset number of search contents and the question text data to obtain input text, and the input text is input into a code-generated large language model to obtain an output code fragment output by the code-generated large language model.

[0132] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented when the computer program is executed by a processor to perform the code generation method for retrieval enhancement provided by the above methods, the method comprising: inputting question text data into a word embedding model to obtain a question word vector of the question text data;

[0133] Based on cosine similarity, word vectors in a first database and word vectors in a second database are screened to determine a preset number of search contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in a code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in development documents into the word embedding model;

[0134] Text combination is performed on the preset number of search contents and the question text data to obtain input text, and the input text is input into a code-generated large language model to obtain an output code fragment output by the code-generated large language model.

[0135] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0136] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0137] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A retrieval-enhanced code generation method, characterized in that: The method comprises: Inputting the question text data into the word embedding model to obtain the question word vector of the question text data; Based on cosine similarity, word vectors in a first database and word vectors in a second database are screened to determine a preset number of search contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in a code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in development documents into the word embedding model; Performing text combination on the preset number of search contents and the question text data to obtain input text, and inputting the input text into a code generation large language model to obtain an output code fragment output by the code generation large language model, wherein the code generation large language model is used to output a code fragment corresponding to the text data based on the input text data; Also includes: Collecting model input and model output of the code-generated large language model, and determining user feedback information on each model output; Associating the model output with its corresponding feedback information to obtain positive and negative feedback matching pairs of the code generation large language model; The code generation large language model is trained based on the positive and negative feedback matching pairs, the model input, and the model output, and model parameters of the code generation large language model are updated based on the training results.

2. The retrieval-enhanced code generation method according to claim 1, characterized in that: The step of combining the preset number of search contents and the question text data to obtain the input text comprises: Based on the prompt engineering method, a code generation prompt word template is constructed, wherein the code generation prompt word template is used to generate output in the form of a thought chain from multiple inputs; Based on the code-generated prompt word template, the preset number of search contents and the question text data are combined to obtain an input text in the form of a thought chain.

3. The retrieval-enhanced code generation method according to claim 1, characterized in that: The step of training the code generation large language model based on the positive and negative feedback matching pairs, the model input, and the model output, and updating the model parameters of the code generation large language model based on the training results, includes: Based on the target reward model, the advantage of the positive and negative feedback matching pairs, the model input and the model output are estimated to obtain a reward value; Inputting the positive and negative feedback matching pairs, model input and model output into the code generation large language model to obtain generation likelihood; Based on the KL divergence between the original code-generated large language model and the code-generated large language model, the reward value, and the generation likelihood, a loss is determined, and backpropagation is performed based on the loss to update model parameters of the code-generated large language model.

4. The retrieval-enhanced code generation method according to claim 3, characterized in that: The model parameters of the target reward model are updated by back-propagation training based on the distance values ​​determined by the positive and negative feedback matching pairs as losses.

5. The retrieval-enhanced code generation method according to claim 1, characterized in that: The training process of the word embedding model includes: Collecting model input and model output of the code-generated large language model, and determining user feedback information on each model output; Based on the model output and the feedback information corresponding to the model, and the question text data, determine the positive sample of the code-generated large language model and the negative sample of the code-generated large language model; Based on contrastive learning, the word embedding model is trained according to the positive samples and the negative samples to update model parameters of the word embedding model.

6. A retrieval-enhanced code generation device, used to implement the retrieval-enhanced code generation method according to any one of claims 1 to 5, characterized in that: include: A word vector generation module, used to input question text data into a word embedding model to obtain a question word vector for the question text data; A retrieval module, configured to screen the word vectors in the first database and the word vectors in the second database based on cosine similarity, and determine a preset number of retrieval contents similar to the question word vector, wherein the first database is constructed based on a plurality of code snippet word vectors obtained by inputting code snippets in the code repository into the word embedding model, and the second database is constructed based on a plurality of development document word vectors obtained by inputting text snippets in the development documents into the word embedding model; A code generation module, used for performing text combination on the preset number of search contents and the question text data to obtain input text, and inputting the input text into a code generation large language model to obtain an output code fragment output by the code generation large language model, wherein the code generation large language model is used to output a code fragment corresponding to the text data based on the input text data; Also includes: Collecting model input and model output of the code-generated large language model, and determining user feedback information on each model output; Associating the model output with its corresponding feedback information to obtain positive and negative feedback matching pairs of the code generation large language model; The code generation large language model is trained based on the positive and negative feedback matching pairs, the model input, and the model output, and model parameters of the code generation large language model are updated based on the training results.

7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the retrieval-enhanced code generation method according to any one of claims 1 to 5 is implemented.

8. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the retrieval-enhanced code generation method according to any one of claims 1 to 5 is implemented.

9. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the retrieval-enhanced code generation method according to any one of claims 1 to 5 is implemented.