Code generation method and device, electronic equipment, computer readable storage medium and computer program product
By retrieving and utilizing sample data from a sample database to enhance the reasoning process of the code model, the problem of errors in code generation by neural network models is solved, and higher quality code generation is achieved.
Patent Information
- Application Number
- CN202510655495.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-20
- Publication Date
- 2026-11-20
AI Technical Summary
The code generated by existing neural network models often contains errors or does not meet user requirements, resulting in low code quality.
By retrieving sample data corresponding to the target problem from the sample database, using the code model for model inference to generate target code, and combining the sample problem and sample code for data augmentation, the accuracy of code generation is improved.
The generated code more accurately matches the user's intent, improving code quality.
Smart Images

Figure CN123140202A_ABST
Abstract
Description
Technical Field
[0001] This application relates to artificial intelligence technology, and more particularly to a code generation method, apparatus, electronic device, computer-readable storage medium, and computer program product. Background Technology
[0002] Currently, it is possible to generate code data using artificial intelligence-based network models. For example, a user can input prompts or questions into a neural network model, requesting the model to generate computer programming language code to implement a certain function; the neural network model then performs model inference based on the user's prompts or questions, generating and outputting the corresponding code.
[0003] However, the code generated by current neural network models often contains errors or does not meet user requirements, thus reducing code quality. Summary of the Invention
[0004] This application provides a code generation method, apparatus, electronic device, computer-readable storage medium, and computer program product, which can improve the code quality generated based on user questions.
[0005] The technical solution of this application embodiment is implemented as follows:
[0006] This application provides a code generation method, the method comprising:
[0007] Based on the user's target question, a search is performed in the sample database to determine at least one target sample data corresponding to the target question;
[0008] Using a code model, model reasoning is performed based on at least one target sample data and the target question to generate target code corresponding to the target question.
[0009] This application provides a code generation apparatus, the apparatus comprising:
[0010] The retrieval module is used to search the sample database based on the user's target question and determine at least one target sample data corresponding to the target question; the sample data in the sample database includes sample questions and sample codes corresponding to the sample questions;
[0011] The generation module is used to generate target code corresponding to the target problem by performing model reasoning based on the at least one target sample data and the target problem through a code model.
[0012] Optionally, the sample data in the sample database includes sample questions and sample codes corresponding to the sample questions; the code generation device further includes: a construction module; the construction module is used to obtain at least one sample question and sample codes corresponding to each sample question before searching the sample database according to the user's target question to determine at least one target sample data corresponding to the target question; and to construct the sample database based on the at least one sample question and the sample codes corresponding to each of the at least one sample question.
[0013] Optionally, the construction module is further configured to generate a sample vector representation corresponding to each sample problem; use each sample problem and the sample code corresponding to each sample problem as sample data, and use the sample vector representation corresponding to each sample problem as an index corresponding to the sample data to construct the sample database.
[0014] Optionally, the retrieval module is further configured to generate a target vector representation corresponding to the target question; perform similarity matching between the target vector representation and the sample vector representation in the sample database to determine at least one target sample vector representation corresponding to the target vector representation; and use the sample data corresponding to the at least one target sample vector representation as the at least one target sample data.
[0015] Optionally, the generation module is further configured to generate prompt information based on the at least one target sample data and the target question; the prompt information is used to prompt the code model to refer to the sample questions and sample codes in the target sample data and generate code for the target question; and the code model performs model reasoning for the code generation task based on the prompt information to generate the target code.
[0016] Optionally, the prompt information includes: prompt information generated based on multiple target sample data; the generation module is further configured to use the code model to refer to the sample questions and sample codes in each of the multiple target sample data, and perform model reasoning for the code generation task with the goal of generating the code for the target question, so as to obtain the target code.
[0017] Optionally, the code generation device further includes a verification module, which is used to perform at least one verification on the target code and determine the verification result; the generation module is also used to update the target code by performing model reasoning again based on the at least one target sample data and the target problem through the code model if the verification result fails.
[0018] Optionally, the verification module is further configured to verify the execution result of the target code, and / or to verify the code logic of the target code, and determine the verification result.
[0019] Optionally, the inspection module is further configured to determine the inspection result as failed if any one of the at least one inspection fails, and to determine the inspection result as passed if all at least one inspection passes.
[0020] Optionally, the code generation device further includes an update module, which is used to take the target problem and the target code as new sample data, combine the new sample data with the target vector representation corresponding to the target problem, and add it to the sample database.
[0021] This application provides an electronic device, the electronic device comprising:
[0022] Memory is used to store executable instructions for a computer;
[0023] The processor, when executing computer-executable instructions stored in the memory, implements the code generation method provided in the embodiments of this application.
[0024] This application provides a computer-readable storage medium storing a computer program or computer-executable instructions, which, when executed by a processor, implements the code generation method provided in this application.
[0025] This application provides a computer program product, including a computer program or computer executable instructions, which, when executed by a processor, implements the code generation method provided in this application.
[0026] The embodiments of this application have the following beneficial effects:
[0027] The system matches the target question against a sample database to identify at least one target sample data point. This sample database contains sample questions and their corresponding sample codes. Using a code model, model inference is performed based on this target sample data and the target question to generate the target code. In this way, for a user-submitted target question requiring code generation, at least one corresponding target sample data point can be retrieved from the sample database. The sample questions and their corresponding sample codes within this database are then used to augment the code model's inference. This allows the code model to reference the sample questions and their corresponding sample codes to perform model inference for the code generation task, resulting in more accurate target code that meets the user's requirements. Therefore, the quality of the code generated based on the user's question is improved. Attached Figure Description
[0028] Figure 1 This is a schematic diagram of an optional flowchart of the code generation method provided in the embodiments of this application;
[0029] Figure 2 This is a schematic diagram of an optional flowchart of the code generation method provided in the embodiments of this application;
[0030] Figure 3 This is a schematic diagram of an optional process of the code generation method provided in the embodiments of this application;
[0031] Figure 4 This is a schematic diagram illustrating an optional process of applying the code generation method provided in this application embodiment to a real-world scenario;
[0032] Figure 5 This is a schematic diagram of an optional structure of the code generation apparatus provided in the embodiments of this application;
[0033] Figure 6 This is a schematic diagram of an optional structure of the electronic device provided in the embodiments of this application. Detailed Implementation
[0034] To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0035] In the following description, references are made to “some embodiments,” which describe a subset of all possible embodiments. However, it is understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0036] In the following description, the terms "first, second, third" are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first, second, third" may be interchanged in a specific order or sequence where permitted, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.
[0037] In this application embodiment, the terms "module" or "unit" refer to a computer program or part of a computer program that has a predetermined function and works with other related parts to achieve a predetermined goal, and can be implemented wholly or partially using software, hardware (such as processing circuitry or memory), or a combination thereof. Similarly, a processor (or multiple processors or memory) can be used to implement one or more modules or units. Furthermore, each module or unit can be part of an overall module or unit that includes the functionality of that module or unit.
[0038] Unless otherwise defined, all technical and scientific terms used in the embodiments of this application have the same meaning as commonly understood by one of ordinary skill in the art. The terminology used in the embodiments of this application is for the purpose of describing the embodiments of this application only and is not intended to limit this application.
[0039] In the implementation of this application, the collection and processing of relevant data should strictly comply with the requirements of relevant national laws and regulations, obtain the informed consent or separate consent of the personal information subject, and carry out subsequent data use and processing within the scope of laws and regulations and the authorization of the personal information subject.
[0040] Currently, when using neural network models, such as language models, to generate code data, users typically describe their problems. For example, users can describe the desired code functionality and other requirements, inputting these descriptions as prompts into the neural network model, which then generates the corresponding code. However, user-described prompts are often quite simple. For instance, a user might input, "You need to generate suitable Python code according to problem A." Such simple prompts can easily lead to errors and mismatches in the generated code, resulting in low-quality code.
[0041] This application provides a code generation method, apparatus, electronic device, computer-readable storage medium, and computer program product, which can generate multi-sample prompt words using a sample database, enabling the code model to produce more accurate code that better matches the user's intent, and improving the quality of code generated by the neural network model.
[0042] The code generation method provided in this application can be applied to electronic devices. In some embodiments, the electronic device may include a terminal or a server. For example, the terminal may include a personal portable mobile device (such as a mobile phone, tablet, laptop, etc.) or an in-vehicle terminal; the server may include an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, network services, and big data and artificial intelligence platforms. The specific selection depends on the actual situation, and this application does not limit it.
[0043] refer to Figure 1 , Figure 2 This is a schematic diagram of an optional flowchart of the code generation method provided in an embodiment of this application. For example... Figure 1 As shown, the code generation method provided in this application embodiment can be implemented by executing the process S101-S102, as follows:
[0044] S101. Based on the user's target question, search the sample database to determine at least one target sample data corresponding to the target question.
[0045] In this embodiment, the target problem includes the code generation requirements described by the user in natural language. The target problem can be data in text form; for example, it can include text data such as the function the user wants the code to implement, the programming language used, and the input / output requirements of the code. The user can input the target problem through an interactive interface with the code model to generate corresponding code data.
[0046] In this embodiment, the code model includes a neural network model for implementing the code generation task. Exemplarily, the code model can be obtained through pre-training. Through model training, the code model can learn the structure and logic of code from a large amount of code data and understand the user's intent in the target problem. Therefore, in the model application stage, it can perform text-to-code model reasoning according to the user's intent and generate the corresponding code. Exemplarily, the code model can include a large language model, or it can include a neural network model with a Transformer architecture, a Sequence-to-Sequence (Seq2Seq) architecture, etc., depending on the actual situation. This embodiment does not limit the choice.
[0047] In this embodiment, the code model is connected to or has a built-in sample database. The sample database includes at least one sample data. Each sample data includes at least one sample question and the corresponding sample code. When the code model receives the target question input by the user, it first searches the sample database to identify sample data that is highly relevant to the target question. This data can then be used as augmenting information for the code model during reasoning, assisting it in generating high-quality code for the target question.
[0048] In some embodiments, the sample problem may include a text problem described in natural language that can be solved using code, and the sample code corresponding to the sample problem includes code for solving the text problem. For example, a sample database can be constructed by pre-collecting various types of user-generated code requirement problems and the high-quality code corresponding to those requirement problems, thereby obtaining at least one sample problem and its corresponding sample code.
[0049] In some embodiments, based on a pre-built sample database, the target question currently raised by the user can be searched among at least one sample question contained in the sample database to determine the sample question whose matching degree with the target question meets the target matching condition as the target sample question, and the sample code corresponding to the target sample question in the sample database is taken as the target sample code, thereby determining at least one target sample data, each target sample data including a target sample question and its corresponding target sample code.
[0050] In some embodiments, the semantics and / or keywords of the target question can be used to retrieve at least one sample question from the sample database. For example, semantic matching of the target question with each sample question is performed to obtain the semantic matching degree of the target question for each sample question; and / or keyword matching of the target question with each sample question is performed to obtain the keyword matching degree of the target question for each sample question. Based on the semantic matching degree and / or keyword matching degree, the matching degree of the target question for each sample question is determined, and sample questions with a matching degree greater than a target matching degree threshold (equivalent to meeting the target matching condition) are designated as target sample questions. Furthermore, the target sample code corresponding to the target sample question is determined.
[0051] In some embodiments, both the target problem and the sample problem can be converted into feature vector representations. The similarity between the feature vector representations of the target problem and the sample problem is calculated to determine the sample problem whose similarity is greater than a target similarity threshold (equivalent to meeting the target matching condition). Alternatively, the feature distance between the feature vector representations of the target problem and the sample problem can be calculated to determine the sample problem whose feature distance is less than a target distance threshold (equivalent to meeting the target matching condition). Then, the target sample code corresponding to the target sample problem is determined. Alternatively, the above retrieval methods can be arbitrarily combined without conflict, and the specific choice depends on the actual situation; this application embodiment does not limit this.
[0052] S102. Using a code model, perform model reasoning based on at least one target sample data and the target question to generate the target code corresponding to the target question.
[0053] In this embodiment of the application, based on at least one target sample data obtained in the above steps, the at least one target sample data and the target question can be used together to obtain the input data for the code model to use for model reasoning. In this way, during the reasoning process, the code model can use at least one target sample question and its corresponding target sample code as a reference to perform more accurate model reasoning for the target question. Under the guidance of at least one target sample question and its corresponding target sample code, more accurate code generation is performed, thereby obtaining the target code corresponding to the target question.
[0054] It is understandable that, for a target problem that requires code generation proposed by a user, the embodiments of this application can first retrieve at least one corresponding target sample data from the sample database, and use the sample problem and its corresponding sample code in the at least one target sample data to perform data augmentation on the model reasoning of the code model. This allows the code model to refer to the sample problem and its corresponding sample code to perform model reasoning for the code generation task of the target problem, generating more accurate target code that meets the user's requirements. Therefore, the code quality generated based on the user's problem is improved.
[0055] In some embodiments, before retrieving data from the sample database based on the user's target question and determining at least one target sample data corresponding to the target question, at least one sample question and sample code corresponding to each of the at least one sample question may be obtained; and a sample database may be constructed based on the at least one sample question and the sample code corresponding to each of the at least one sample question.
[0056] For example, each sample problem in at least one sample problem, and the sample code corresponding to that sample problem, can be used as a sample data to obtain at least one sample data, and a sample database can be constructed based on the at least one sample data.
[0057] In some embodiments, this can be achieved by performing, such as Figure 2 The method shown below constructs a sample database as follows:
[0058] S001. Obtain at least one sample problem and the sample code corresponding to each sample problem.
[0059] In some embodiments, sample questions and their corresponding sample codes can be obtained by collecting historical interaction data between users and the code model. For example, question text input by users during their historical use of the code model can be collected as sample questions, and high-quality code generated by the code model for that question text can be selected as sample code, thereby obtaining at least one sample question and at least one sample code. Alternatively, sample questions and sample codes can be collected using public datasets or code platforms. Alternatively, data augmentation techniques (such as instruction rewriting, code variant generation, etc.) can be used to expand the original sample questions and sample codes to obtain new sample questions and sample codes. The specific selection depends on the actual situation, and this application embodiment does not limit the choice.
[0060] In some embodiments, the collected sample questions and their sample codes can be cleaned and preprocessed. For example, the sample questions and sample codes can be cleaned to remove noisy data such as HTML tags, special characters, and duplicate samples. The sample questions and sample codes can also be labeled and aligned to ensure that each natural language instruction corresponds to a correct code and to avoid ambiguity. This application does not specifically limit the implementation of the data cleaning and preprocessing.
[0061] S002. Generate the sample vector representation corresponding to each sample problem.
[0062] In this embodiment of the application, for each sample question, a text vector model or a preset vector encoding algorithm can be used to generate a semantic vector representation of the text corresponding to the sample question, which serves as the sample vector representation of the sample question.
[0063] S003. Take each sample problem and its corresponding sample code as sample data, and take the sample vector representation corresponding to each sample problem as the index of the sample data to construct a sample database.
[0064] In this embodiment, a sample question and its corresponding sample code can be considered as sample data, and the sample vector representation of the sample question can be used as the index of the sample data. This allows for the processing of at least one sample question and its corresponding sample code to construct a sample database. In other words, in the sample database, the index of the sample data is the vector representation of the sample question within that sample data. Thus, the index of the sample data is equivalent to the index of the database and can be applied to the retrieval of the target question.
[0065] For example, high-quality (problem, code) data pairs can be collected, such as (problem, code) data pairs can be represented as {(q1, c1), (q2, c2), ..., (q n ,cn )}. Where, q n Let c represent the nth natural language problem text (sample problem) that can be solved using code. n This is the corresponding code text that can be used to solve the problem (sample code corresponding to the sample problem); the problem q is represented using a text vector model. i (i∈[1,n]) vectorized, as (q) i ,c i The sample database is constructed by indexing the data pairs. The contents stored in the sample database can be represented as: {{v1:(q1,c1)},{v2:(q2,c2)}…,{v n :(q n ,c n )}},v n =V(q) n Where V represents the text vector model, q n Let c represent the text of the nth question. n v represents the nth code text. n The question text q represents the problem text. n The vector representation obtained through the text vector model.
[0066] It should be noted that, in some embodiments, the sample database can also collect self-developed code or unique code corresponding to the question text, or other target-customized types of code, as sample code, according to the user's needs. In this way, the sample code in the sample database can be used to provide corresponding code generation references for the customized code questions raised by the user, ensuring that the target code generated by the code model based on the sample code meets the user's customization requirements.
[0067] It is understandable that by pre-acquiring sample questions and sample code, generating sample vector representations corresponding to the sample questions, and combining the sample vector representations, sample questions, and sample code to construct a sample database, the sample database can be used to help the code model produce more accurate code data and improve the quality of code generation.
[0068] based on Figure 1 and Figure 2 In some embodiments of the process shown, the process in S101 of searching the sample database based on the user's target question to determine at least one target sample data corresponding to the target question can be as follows: Figure 3 As shown, it includes:
[0069] S201. Generate the target vector representation corresponding to the target problem.
[0070] In this process, a text vector model or a preset vector encoding algorithm can be used to generate a semantic vector representation of the text corresponding to the target problem, which serves as the target vector representation.
[0071] S202. Perform similarity matching between the target vector representation and the sample vector representation in the sample database to determine at least one target sample vector representation corresponding to the target vector representation.
[0072] In this embodiment, the index of at least one sample data in the sample database is at least one sample vector representation. The target vector representation is matched with the at least one sample vector representation for similarity. The sample vector representation that satisfies the similarity matching condition is determined as the target sample vector representation corresponding to the target vector representation, thereby obtaining at least one target sample vector representation.
[0073] In some embodiments, the process of performing similarity matching between a target vector representation and at least one sample vector representation to obtain at least one target sample vector representation may include: calculating the similarity between the target vector representation and each of the at least one sample vector representations to obtain at least one similarity between the target vector representation and the at least one sample vector representation; and taking the sample vector representation corresponding to the similarity that satisfies the similarity matching condition among the at least one similarity as the target sample vector representation corresponding to the target vector representation, thereby obtaining at least one target sample vector representation.
[0074] For example, similarity matching conditions may include: a similarity higher than a target similarity threshold, and / or at least the top N similarities with the highest numerical values (N is greater than or equal to 1 and less than or equal to the number of sample vector representations). For instance, the similarity between the target vector representation and M sample vector representations in the sample database is calculated to obtain M similarities; the similarities among these M similarities that are greater than the target similarity threshold are determined to obtain N similarities; or, the top N similarities with the highest numerical values among the M similarities are determined; or, the top N similarities with the highest numerical values among the M similarities are determined, and all of the top N similarities are greater than the target similarity threshold. Furthermore, the N sample vector representations corresponding to the determined N similarities are used as the N target sample vector representations corresponding to the target problem.
[0075] S203. Represent at least one target sample vector as the corresponding sample data, and use it as at least one target sample data.
[0076] Here, at least one target sample vector is equivalent to at least one index, and each index corresponds to a sample data in the sample database. The sample data corresponding to each target sample vector is taken as the target sample data, thereby determining at least one target sample data.
[0077] For example, cosine similarity can be used to match the similarity between the target vector representation and the sample vector representation. For instance, for a given question text q (equivalent to the target question), the text vector model V is used to obtain its corresponding vector representation v (equivalent to the target vector representation): v = V(q). Using formulas (1) and (2), v can be compared with the n sample vector representations {v1, v2, ..., v...} in the sample database. n The similarity was calculated separately as follows:
[0078]
[0079] Wherein, CosineSimilarity(v,v i ) represents the computation of vectors v and v i Cosine similarity between them, v T This represents the transpose of vector v, v T v i Representing vectors v and v i The sum of element-wise products; ||v|| denotes the norm of vector v, ||v i || represents vector v i The norm of v is used to normalize v. T v i The calculation results yield vectors v and v i Cosine similarity s between i , which represents the similarity between the question text q and the i-th sample question in the sample database.
[0080] Thus, using formula (1), we can calculate the n similarities between the question text q and the n sample questions in the sample database. Then, from these n similarities, we determine the k most similar (k is greater than or equal to 1 and less than n). Each of these k similarities is then mapped to a sample question and its corresponding sample code in the sample database, thereby obtaining k target sample data. In other words, by using the target vector representation and the n sample vector representations, we retrieve k target sample data. The above process can be illustrated by formula (2) as follows:
[0081] {(q i ,c i )|i∈argmax k (s1,s2,…,s n )}Formula (2)
[0082] Among them, (q) i ,c i ) indicates the target sample data for recall.
[0083] It should be noted that the similarity matching between the target vector representation and the sample vector representation in the sample database can also be performed by calculating the Euclidean distance or Hamming distance between the target vector representation and the sample vector representation to obtain at least one target sample vector representation. The specific choice depends on the actual situation, and this application embodiment does not limit it.
[0084] Understandably, by using similarity matching, at least one target sample problem similar to the target problem can be identified from the sample database, and then at least one target sample code corresponding to the at least one target sample problem can be identified to guide the code model in generating code for the target problem. This achieves targeted data augmentation for the target problem and improves the quality of code generation.
[0085] In some embodiments, based on the above embodiments, the process in S102 of generating target code corresponding to the target problem by performing model inference based on at least one target sample data and the target problem using a code model may include:
[0086] Based on at least one target sample data and a target question, a prompt message is generated. The prompt message is used to prompt the code model to refer to the sample questions and sample codes in the target sample data and generate code for the target question. Through the code model, the model inference of the code generation task is performed based on the prompt message to generate the target code.
[0087] In other words, guided by prompts, the code model can refer to the relationship between the sample questions in each target sample data and the sample codes corresponding to those sample questions, and perform model reasoning for the code generation task for the target question raised by the current user, thereby generating the code corresponding to the target question.
[0088] It is understandable that by generating prompts based on at least one target sample data and the target question, and inputting them into the code model to generate target code, data augmentation of the target question can be achieved using at least one target sample data, thereby improving the reasoning effect of the code model on the target question and thus improving the code quality of the target code generated by the code model.
[0089] In some embodiments, the prompt information includes prompt information generated based on multiple target sample data. That is, multiple target sample data can be retrieved from a sample database based on the user's target question, and prompt information for inputting into the code model is generated based on these multiple target sample data. Thus, through the code model, the model can refer to the sample questions and sample codes in each of the multiple target sample data, and perform model reasoning for the code generation task with the goal of generating code for the target question, thereby obtaining the target code.
[0090] For example, based on the recalled k target sample data Given the question text q, construct multiple code sample hint words (equivalent to hint messages). Use these hint words as input to the code model to generate the code corresponding to the question text. For example, the hint messages generated for the code model can be as follows:
[0091] ##Role
[0092] ##introduce
[0093] As a professional code expert, please refer to the relationship between the problem text and its corresponding [sample code] in the sample data to generate code for a new [problem text].
[0094] ##Sample Data
[0095] Sample data 1:
[0096] [Question Text] q1
[0097] [Sample Code]c1
[0098] Sample data 2:
[0099] [Question Text] q2
[0100] [Sample Code]c2
[0101] …
[0102] Sample data k:
[0103] [Question Text] q k
[0104] [Sample Code]c k
[0105] ## Generate code based on the question text
[0106] [Question Text] q
[0107] [target code]
[0108] By parsing the above prompts, the code model understands that its role is that of a professional code expert, and the reasoning task it needs to perform is "generating code based on the problem text." The problem text to be generated is q, and it needs to refer to the relationship between the problem text (equivalent to a sample problem) and the sample code in each sample data set from sample data 1 to sample data k (e.g., the relationship between q1 and c1, the relationship between q2 and c2, and q...). k With c kThe code model learns the relationships between the question text and sample code in each sample to generate target code for the question text q. This enhances the understanding of the user's intent regarding the question text q and improves code generation, resulting in more accurate and logically consistent code data as target code.
[0109] It is understandable that generating prompts based on multiple target sample data and inputting them into the code model to generate target code can enrich the information that the code model can refer to when performing model inference, enhance the data augmentation effect on the model input, and thus further improve the reasoning effect of the code model on the target problem, thereby improving the quality of the generated target code.
[0110] In some embodiments, after the code model generates the target code, at least one test can be performed on the target code to determine the test result; if the test result fails, the code model can be used to perform model reasoning again based on at least one target sample data and the target problem to update the target code.
[0111] In other words, in this embodiment of the application, the target code generated by the code model will be tested in at least one way. If the test passes, the target code can be adopted; if the test fails, the randomness of the code model generation result will be utilized to perform another model inference based on at least one target sample data and the target question to generate new target code.
[0112] Furthermore, in some embodiments, at least one test can be performed on the new target code. If the test fails, model reasoning is performed again based on at least one target sample data and the target problem to generate new target code again, until the generated target code can pass at least one test.
[0113] In some embodiments, at least one inspection may include inspection of at least one inspection dimension such as the execution result of the target code, code logic, and code completion. The at least one inspection dimension can be customized according to actual needs, and this application embodiment does not impose specific limitations. By performing at least one inspection on the target code, the inspection result corresponding to each of the at least one inspection can be obtained, that is, at least one inspection result can be obtained; then, the final inspection result corresponding to the target code can be determined by combining the at least one inspection result.
[0114] In some embodiments, the target code can be subjected to execution result verification and / or code logic verification to determine the verification result. For example, for the execution result dimension verification, a code interpreter can be used to verify whether the target code can be executed successfully, thus obtaining the execution result dimension verification result. For instance, if the target code encounters an error while running in the code interpreter, the execution dimension verification result is "failed"; if the target code runs successfully in the code interpreter, the execution dimension verification result is "passed".
[0115] For example, for the verification of code logic, a pre-trained neural network model can be used to check whether the code logic of the target code is correct and whether it meets human expectations, thereby obtaining the verification results of the code logic dimension. For instance, a code logic verification model can be trained using a large amount of human code data. Through the code logic verification model, the target code is checked for code logic, and the verification results of the target code in the code logic dimension are output as pass or fail.
[0116] For example, for the code completion dimension verification, a pre-trained code functionality verification model can be used to perform inference based on the target code and output the completion degree of the target code. The completion degree characterizes the extent to which the target code completes the functionality required by the target problem. Furthermore, based on the completion degree of the target code, the verification result for the target code in the code completion dimension can be determined. For example, if the completion degree is higher than or equal to the target completion degree threshold (e.g., 98%), the target code is determined to have passed the code completion dimension verification; if the completion degree is lower than the target completion degree threshold, the target code is determined to have failed the code completion dimension verification.
[0117] It should be noted that at least one of the checks in the embodiments of this application can be arbitrarily combined without conflict, and can be executed sequentially or in parallel in any order. The embodiments of this application do not impose specific limitations on the combination of check methods or the execution order.
[0118] In some embodiments, if any one of the at least one tests fails, the test result is determined to be failed; if all at least one test passes, the test result is determined to be passed.
[0119] In other words, if at least one of the test results contains a failing result, the final test result is failing; only if all at least one test result passes, the final test result is passing.
[0120] It should be noted that the strategy of determining the final test result based on the test results of at least one test can also be configured according to actual needs. For example, corresponding weights can be preset for various test results, and then the test result can be weighted using the weights corresponding to each test result, and the final test result can be obtained by combining at least one weighted test result. The specific selection should be based on the actual situation, and the embodiments of this application do not impose specific limitations.
[0121] For example, for the code text generated for question q (equivalent to target code), the code is input into a code interpreter to check if it can run normally in the interpreter. If it runs normally, the execution result dimension verification is considered successful; if it reports an error or fails, the execution result dimension verification is considered unsuccessful. Furthermore, the code text is input into a pre-trained code logic verification model. The model performs reasoning based on the code text to evaluate whether the code logic is correct and whether it meets human expectations, outputting the code logic accuracy. If the code logic accuracy is higher than or equal to the target accuracy threshold, the code logic dimension verification is considered successful; if it is lower than the target accuracy threshold, the code logic dimension verification is considered unsuccessful. If both the execution result dimension verification and the code logic dimension verification pass, this code text is adopted as the corresponding code for solving question q. If either the execution result dimension verification or the code logic dimension verification fails, the code model performs another round of model reasoning based on question q and the samples 1 to k mentioned above, outputting new code text.
[0122] Understandably, by performing at least one verification on the target code, the quality of the target code can be verified from at least one dimension. If the quality does not meet the requirements, the target code can be regenerated, thereby reducing the outflow of low-quality code and further improving the quality of the final generated code.
[0123] In some embodiments, after the code model generates the target code, or after the target code is subjected to at least one test and the test result is determined to be passed, the target problem and the target code can be used as new sample data, and the new sample data can be combined with the target vector representation corresponding to the target problem and added to the sample database.
[0124] In other words, the target code and its corresponding target problem can be added to the sample database. Furthermore, the target vector representation corresponding to the target problem can be used as an index for the data pair (target problem, target code) and added to the sample database. This allows for continuous expansion of the sample database during user use, enabling continuous construction of sample data. As the sample data expands, the quality of the code produced by the code model will further improve.
[0125] This application provides a code generation method applicable to real-world scenarios, such as... Figure 4 As shown, it includes:
[0126] Step 1: Seed Database Construction. The seed database is equivalent to the sample database described above. The seed database construction process can be referred to in steps S001-S003 above, and will not be repeated here.
[0127] Step 2: Semantic Vector-Based Code Sample Recall. For the input user question (equivalent to the target question), a target vector representation corresponding to the user question is generated. Using a vector-based retrieval method, a search is performed on at least one sample vector representation in the seed database. Sample data matching is then performed based on semantic similarity to obtain at least one recalled sample corresponding to the user question (equivalent to at least one target sample data mentioned above).
[0128] Step 3: Code generation based on multiple code sample prompts. This involves generating multiple code sample prompts (equivalent to hints) based on at least one recalled sample and the user question. These prompts are then input into a language model (equivalent to a code model). The language model performs inference based on the prompts and outputs the target code corresponding to the user question.
[0129] Step 4: Data Filtering. Input the target code into the code interpreter. If the target code is executable, it can be used as executable code. The code logic verification model is then used to check its code logic. If the code logic check passes, the executable code is adopted, and Step 5: Data Backflow is executed. If the code logic check fails, the process returns to the code model. The target code is regenerated based on the prompts from multiple code samples, and Step 4: Data Filtering is executed again for the regenerated target code until the code logic check passes.
[0130] Step 5: Data Feedback. Based on the executable code that has passed the code logic check and its corresponding issue text, new sample data is generated and added to the seed database.
[0131] It is understood that the embodiments of this application implement a code generation scheme based on multiple code sample prompts from a seed database, which can produce more accurate code data. Furthermore, a closed-loop sample code data construction flywheel is constructed, which can continuously build sample code data, and as the seed database expands, the quality of the generated code will become increasingly better, thus further improving code quality.
[0132] This application provides a code generation apparatus, such as... Figure 5 As shown, the code generation device 1 may include:
[0133] The retrieval module 11 is used to search the sample database according to the user's target question and determine at least one target sample data corresponding to the target question;
[0134] The generation module 12 is used to generate target code corresponding to the target problem by performing model reasoning based on the at least one target sample data and the target problem through a code model.
[0135] In some embodiments, the sample data in the sample database includes sample questions and sample codes corresponding to the sample questions; the code generation device 1 further includes: a construction module; the construction module is configured to obtain at least one sample question and sample codes corresponding to each sample question before searching the sample database according to the user's target question to determine at least one target sample data corresponding to the target question; and to construct the sample database based on the at least one sample question and the sample codes corresponding to each of the at least one sample question.
[0136] In some embodiments, the construction module is further configured to generate a sample vector representation corresponding to each sample problem; and to construct the sample database by using each sample problem and the sample code corresponding to each sample problem as sample data, and using the sample vector representation corresponding to each sample problem as an index corresponding to the sample data.
[0137] In some embodiments, the retrieval module 11 is further configured to generate a target vector representation corresponding to the target question; perform similarity matching between the target vector representation and the sample vector representation in the sample database to determine at least one target sample vector representation corresponding to the target vector representation; and use the sample data corresponding to the at least one target sample vector representation as the at least one target sample data.
[0138] In some embodiments, the generation module 12 is further configured to generate prompt information based on the at least one target sample data and the target problem; the prompt information is used to prompt the code model to refer to the sample problems and sample codes in the target sample data and generate code for the target problem; and the code model performs model reasoning for the code generation task based on the prompt information to generate the target code.
[0139] In some embodiments, the code generation device 1 further includes a verification module, which is used to perform at least one verification on the target code and determine the verification result; the generation module 12 is further used to update the target code by performing model reasoning again based on the at least one target sample data and the target problem through the code model if the verification result fails.
[0140] In some embodiments, the prompt information includes: prompt information generated based on multiple target sample data; the generation module 12 is further configured to, through the code model, refer to the sample questions and sample codes in each of the multiple target sample data, perform model reasoning for the code generation task with the goal of generating the code for the target question, and obtain the target code.
[0141] In some embodiments, the verification module is further configured to perform execution result verification on the target code, and / or perform code logic verification on the target code, and determine the verification result.
[0142] In some embodiments, the inspection module is further configured to determine the inspection result as failed if any one of the at least one inspection fails, and to determine the inspection result as passed if all at least one inspection passes.
[0143] In some embodiments, the code generation device 1 further includes an update module, which is used to take the target problem and the target code as new sample data, combine the new sample data with the target vector representation corresponding to the target problem, and add it to the sample database.
[0144] It should be noted that the description of the above device embodiments is similar to the description of the above method embodiments, and has similar beneficial effects. For technical details not disclosed in the device embodiments of this application, please refer to the description of the method embodiments of this application for understanding.
[0145] This application provides an electronic device, such as... Figure 6 As shown, the electronic device 3 may include: a memory 32 and a processor 33. The memory 32 and the processor 33 are connected via a communication bus 34; the memory 32 is used to store executable instructions; the processor 33 is used to implement the code generation method provided in this application embodiment when executing the executable instructions stored in the memory 32.
[0146] This application provides a computer-readable storage medium storing executable instructions, wherein the executable instructions are stored and when executed by a processor, they will cause the processor to execute any of the code generation methods provided in this application.
[0147] In some embodiments, the computer-readable storage medium may be a memory such as FRAM, ROM, PROM, EPROM, EEPROM, flash memory, magnetic surface memory, optical disk, or CD-ROM; or it may be a variety of devices including one or any combination of the above-mentioned memories.
[0148] In some embodiments, executable instructions may take the form of a program, software, software module, script, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a standalone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.
[0149] As an example, executable instructions may, but do not necessarily, correspond to files in the file system. They may be stored as part of a file that holds other programs or data, for example, in one or more scripts in a Hyper Text Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple co-located files (e.g., a file that stores one or more modules, subroutines, or code sections).
[0150] As an example, executable instructions can be deployed to execute on a single computing device, or on multiple computing devices located in one location, or on multiple computing devices distributed across multiple locations and interconnected via a communication network.
[0151] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, and improvements made within the spirit and scope of this application are included within the scope of protection of this application.
Claims
1. A code generation method, characterized in that, The method includes: Based on the user's target question, a search is performed in the sample database to determine at least one target sample data corresponding to the target question; Using a code model, model reasoning is performed based on at least one target sample data and the target question to generate target code corresponding to the target question.
2. The method according to claim 1, characterized in that, The sample data in the sample database includes sample questions and corresponding sample codes; before retrieving the sample database based on the user's target question to determine at least one target sample data corresponding to the target question, the method further includes: Obtain at least one sample question and the sample code corresponding to each sample question; The sample database is constructed based on the at least one sample problem and the sample code corresponding to each sample problem in the at least one sample problem.
3. The method according to claim 2, characterized in that, The step of constructing the sample database based on the at least one sample problem and the sample code corresponding to each sample problem in the at least one sample problem includes: Generate a sample vector representation corresponding to each sample problem; The sample database is constructed by using each sample problem and its corresponding sample code as sample data, and the sample vector representation corresponding to each sample problem as the index of the sample data.
4. The method according to any one of claims 1-3, characterized in that, The step of retrieving data from a sample database based on the user's target question to determine at least one target sample data corresponding to the target question includes: Generate the target vector representation corresponding to the target problem; The target vector representation is matched with the sample vector representations in the sample database to determine at least one target sample vector representation corresponding to the target vector representation; The sample data corresponding to the at least one target sample vector is used as the at least one target sample data.
5. The method according to any one of claims 1-4, characterized in that, The step of generating target code corresponding to the target problem by performing model inference based on the at least one target sample data and the target problem through a code model includes: Based on the at least one target sample data and the target question, a prompt message is generated; the prompt message is used to prompt the code model to refer to the sample questions and sample codes in the target sample data and generate code for the target question; Using the code model, the target code is generated by performing model reasoning for the code generation task based on the prompt information.
6. The method according to claim 5, characterized in that, The prompt information includes: prompt information generated based on multiple target sample data; the step of generating the target code through model inference of the code generation task based on the prompt information using the code model includes: Using the code model, and referring to the sample questions and sample codes in each of the multiple target sample data, the model reasoning for the code generation task is performed with the goal of generating the code for the target question, and the target code is obtained.
7. The method according to any one of claims 1-6, characterized in that, The method further includes: Perform at least one test on the target code and determine the test result; If the test result fails, the target code is updated by performing model reasoning again based on the at least one target sample data and the target question using the code model.
8. The method according to claim 7, characterized in that, The step of performing at least one test on the target code and determining the test result includes: The target code is subjected to execution result verification, and / or the target code is subjected to code logic verification, and the verification result is determined.
9. A code generation device, characterized in that, The device includes: The retrieval module is used to search the sample database based on the user's target question and determine at least one target sample data corresponding to the target question; the sample data in the sample database includes sample questions and sample codes corresponding to the sample questions; The generation module is used to generate target code corresponding to the target problem by performing model reasoning based on the at least one target sample data and the target problem through a code model.
10. An electronic device, characterized in that, The electronic device includes: Memory is used to store executable instructions for a computer; A processor, when executing computer-executable instructions stored in the memory, implements the method according to any one of claims 1 to 8.
11. A computer-readable storage medium storing computer-executable instructions, characterized in that, When the computer-executable instructions or computer program are executed by a processor, they implement the method described in any one of claims 1 to 8.
12. A computer program product comprising computer-executable instructions, characterized in that, When the computer-executable instructions or computer program are executed by a processor, they implement the method according to any one of claims 1 to 8.
