Code generation method and device, electronic equipment and storage medium
By acquiring and merging prompts to enhance instructions, generating code using a pre-trained large language model, and correcting errors when execution fails, the problem of insufficient code generation accuracy in existing technologies is solved, achieving high accuracy in code generation.
Patent Information
- Application Number
- CN202511013849.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-22
- Publication Date
- 2025-11-21
AI Technical Summary
Existing code generation methods suffer from insufficient accuracy, algorithm-driven approaches support limited conversion types and lack complex semantic understanding, while large language model-based approaches suffer from poor interpretability and insufficient generalization ability.
By obtaining the hints from the first code, the associated hint enhancement instructions are determined, and after merging and processing with the first hint, they are input into a pre-trained large language model to generate the second code. When the execution fails, the error information is used to correct the code and improve its accuracy.
By enhancing the understanding of prompts and correcting generated code, the accuracy of code generation was significantly improved, errors were reduced, and the final quality of the code was enhanced.
Smart Images

Figure CN120994202A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a code generation method, apparatus, electronic device, and storage medium. Background Technology
[0002] Currently, code generation mainly relies on two methods: algorithm-driven and large language model-based code generation. Algorithm-driven methods require writing different algorithms for different types of transformations, have limited support for various transformation types, and lack understanding of complex semantics and context during transformations, resulting in poor accuracy of the generated code. While large language model-based code generation has the advantage of low cost, it also has significant drawbacks: when using implicit reasoning large language models, the lack of interpretability makes the generated code difficult to debug and verify, leading to low accuracy; when using explicit reasoning large language models, overfitting often results in insufficient generalization ability, which also affects the accuracy of code generation. Summary of the Invention
[0003] The main objective of this application is to provide a code generation method, apparatus, electronic device, and storage medium, which aims to improve the accuracy of the generated code.
[0004] To achieve the above objectives, a first aspect of this application proposes a code generation method, the method comprising:
[0005] Get the first hint for the first code, which is used to perform data transformation;
[0006] Based on the first prompt, determine the prompt enhancement instruction that is related to the first prompt;
[0007] The first prompt and the prompt enhancement instruction are merged to obtain a second prompt for the first code;
[0008] The second prompt is input into the pre-trained first language model to obtain the second code corresponding to the second prompt;
[0009] The second code is run to obtain a first execution result. The first execution result is used to indicate whether the second code has run successfully. If the second code fails to run, the first execution result is also used to indicate an error message.
[0010] If the first execution result indicates that the second code failed to run, then the second code is corrected according to the execution error information to obtain the first code.
[0011] To achieve the above objectives, a second aspect of this application provides a code generation apparatus, the apparatus comprising:
[0012] The acquisition module is used to acquire the first prompt for the first code, which is used for data conversion.
[0013] The determining module is used to determine, based on the first prompt, a prompt enhancement instruction that is associated with the first prompt;
[0014] The merging module is used to merge the first prompt and the prompt enhancement instruction to obtain a second prompt for the first code;
[0015] The generation module is used to input the second prompt into the pre-trained first large language model to obtain the second code corresponding to the second prompt;
[0016] The running module is used to run the second code and obtain a first running result of the second code. The first running result is used to indicate whether the second code ran successfully. If the second code fails to run, the first running result is also used to indicate running error information.
[0017] The correction module is used to correct the second code according to the running error information if the first running result indicates that the second code has failed to run, so as to obtain the first code.
[0018] To achieve the above objectives, a third aspect of this application provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the code generation method described in the first aspect.
[0019] To achieve the above objectives, a fourth aspect of the present application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the code generation method described in the first aspect.
[0020] The code generation method, apparatus, electronic device, and storage medium proposed in this application obtain a first prompt for a first code, then determine a prompt enhancement instruction related to the first prompt based on the first prompt, and then merge the first prompt and the prompt enhancement instruction to obtain a second prompt for the first code. The second prompt is then input into a pre-trained first language model to obtain a second code corresponding to the second prompt. This strengthens the first language model's understanding of the input prompt, thereby improving the accuracy of the second code generated by the first language model. Then, the second code is run to obtain a first execution result. If the first execution result indicates that the second code has failed, the second code is corrected based on the execution error information indicated by the first execution result to obtain the first code. Thus, in the event of a second code failure, correcting the second code based on the relevant execution error information can further reduce errors in the code. Therefore, through the aforementioned two stages (i.e., prompt enhancement and second code correction), the accuracy of the final obtained code can be improved. Attached Figure Description
[0021] Figure 1 This is a flowchart of the code generation method provided in the embodiments of this application;
[0022] Figure 2 This is a schematic diagram of the structure of the code generation apparatus provided in the embodiments of this application;
[0023] Figure 3 This is a schematic diagram of the hardware structure of the electronic device provided in the embodiments of this application. Detailed Implementation
[0024] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0025] It should be noted that although functional modules are divided in the device schematic diagram and a logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than the module division in the device or the order in the flowchart. The terms "first," "second," etc., in the specification, claims, and the aforementioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0026] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.
[0027] To address the problems of the prior art, embodiments of this application provide a code generation method, apparatus, electronic device, and storage medium, aiming to improve the accuracy of the generated code.
[0028] The recommended methods, apparatuses, electronic devices, and storage media provided in this application are specifically described through the following embodiments. First, the code generation method in this application is described.
[0029] The embodiments of this application can acquire and process relevant data based on artificial intelligence technology. Artificial intelligence (AI) refers to the theories, methods, technologies, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, perceive the environment, acquire knowledge, and use that knowledge to obtain optimal results.
[0030] Foundational technologies for artificial intelligence generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing, operating / interactive systems, and mechatronics. AI software technologies mainly encompass computer vision, robotics, biometrics, speech processing, natural language processing, and machine learning / deep learning.
[0031] The code generation method provided in this application relates to the field of computer technology. The code generation method provided in this application can be applied to a terminal, a server, or software running on either a terminal or a server. In some embodiments, the terminal can be a smartphone, tablet, laptop, desktop computer, etc.; the server can be configured as an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be an application implementing the code generation method, but is not limited to the above forms.
[0032] This application can be used in a wide variety of general-purpose or special-purpose computer system environments or configurations. Examples include: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, and distributed computing environments including any of the above systems or devices. This application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc., that perform specific tasks or implement specific abstract data types. This application can also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communication network. In distributed computing environments, program modules can reside in local and remote computer storage media, including storage devices.
[0033] Figure 1 This is a flowchart of the code generation method provided in the embodiments of this application. Please refer to it. Figure 1 . Figure 1 The method may include, but is not limited to, steps 101 to 106.
[0034] Step 101: Obtain the first hint for the first code, which is used for data conversion;
[0035] Step 102: Based on the first prompt, determine the prompt enhancement instruction that is related to the first prompt;
[0036] Step 103: Merge the first prompt and the prompt enhancement instruction to obtain a second prompt for the first code;
[0037] Step 104: Input the second prompt into the pre-trained first large language model to obtain the second code corresponding to the second prompt;
[0038] Step 105: Run the second code to obtain the first running result of the second code. The first running result is used to indicate whether the second code ran successfully. If the second code fails to run, the first running result is also used to indicate running error information.
[0039] Step 106: If the first running result indicates that the second code failed to run, then the second code is corrected according to the running error information to obtain the first code.
[0040] The first code is used for data transformation, that is, to convert data from one format, structure, or value to another. The first prompt can be a suggestion for the first code based on the input of a related object. It can contain information such as the target data to be transformed, a data transformation example, or the transformation target. For example, the first prompt could be "Write a function to transform the input data into the desired output." Furthermore, the data transformation example supports instance-level examples (e.g., specific input-output pairs) and abstract-level examples (e.g., metadata or semantic information). Typically, first prompts suffer from unclear logic, non-standard expression, or vague description. Therefore, the first prompt based on the input of a related object can be enhanced; that is, based on the first prompt, prompt enhancement instructions that are related to the first prompt can be determined. For example, for the first prompt mentioned above, a lightweight large language model can be used to process the first prompt to obtain prompt enhancement instructions, such as "continuous mapping type; input data: Excel data; desired output: corresponding date and time format." Here, "continuous mapping type" is the predicted operation type, "input data" and "desired output" are the information to be replaced, and "Excel data" and "corresponding date and time format" are the replacement information. Then, the first suggestion and the suggestion enhancement instruction can be merged to obtain a second suggestion for the first code, such as "Continuous mapping type: convert the given Excel data to the corresponding date and time format". Here, the first suggestion and the suggestion enhancement instruction can be merged through a large language model, or it can be merged based on preset syntax rules.
[0041] Subsequently, the second prompt can be input into the pre-trained first language model to obtain the second code corresponding to the second prompt. The first language model can be pre-trained using historical prompts and corresponding historical codes. During training, historical prompts can be input into the first language model to obtain the corresponding code to be compared. Based on a predetermined loss function (e.g., cross-entropy loss function), historical codes, and the code to be compared, the loss value of the first language model can be calculated, and the model parameters of the first language model can be adjusted based on the loss value of the first language model through backpropagation. The training of the first language model is completed when the loss value of the first language model is less than the first preset loss value, or when the number of execution rounds of the aforementioned training process reaches the first preset number of rounds. The first preset loss value and the first preset number of rounds are set according to the actual situation.
[0042] After generating the second code, it can be run to obtain the first execution result. The first execution result indicates whether the second code ran successfully. If the second code fails to run, the first execution result also indicates an error message, such as a value error (ValueError) or a syntax error (SyntaxError). Therefore, if the first execution result indicates that the second code ran successfully, it is used as the first code. If the first execution result indicates that the second code failed to run, it is corrected according to the error message to obtain the first code. For example, the error type indicated by the error message (e.g., value error or syntax error) can be matched with multiple first preset types in a preset relationship to obtain correction suggestion information corresponding to the error type. Thus, the second code can be corrected according to the correction suggestion information to obtain the first code. Alternatively, the second prompt and correction suggestion information can be input together into the first language model to obtain the first code output by the first language model.
[0043] Steps 101 to 106, as illustrated in this embodiment, involve obtaining a first hint for the first code, determining a hint enhancement instruction related to the first hint based on the first hint, merging the first hint and the hint enhancement instruction to obtain a second hint for the first code, and inputting the second hint into a pre-trained first language model to obtain the second code corresponding to the second hint. This strengthens the first language model's understanding of the input hint, thereby improving the accuracy of the second code generated by the first language model. Then, the second code is run to obtain a first running result. If the first running result indicates that the second code failed, the second code is corrected based on the running error information indicated by the first running result to obtain the first code. Thus, in the event of a second code failure, correcting the second code based on the relevant running error information can further reduce errors in the code. Therefore, through the aforementioned two stages (i.e., strengthening the hint and correcting the second code), the accuracy of the final obtained code can be improved.
[0044] In some embodiments, the prompting enhancement instruction includes conversion task description information and target conversion type. The conversion task description information is used to describe the data conversion operation performed on the target data, and the target conversion type is any one of formatting type, extraction type, discrete mapping type, continuous mapping type, or unit conversion type.
[0045] The enhanced prompts include a conversion task description and a target conversion type. The conversion task description describes the data conversion operation performed on the target data. The target conversion type can be any one of the following: formatting, extraction, discrete mapping, continuous mapping, or unit conversion. A formatting type converts data to a preset format; for example, formatting name initials or dates is a formatting type. An extraction type extracts partial information from data; for example, extracting the top-level domain from a Uniform Resource Locator (URL) is an extraction type. A discrete mapping type converts data to a finite number of discontinuous output values; for example, mapping hexadecimal color codes to RGB values is a discrete mapping type. A continuous mapping type converts data to a continuous set; for example, time zone conversion is a continuous mapping type. A unit conversion type converts the data's unit of measurement to a preset unit of measurement; for example, converting data from Celsius to Fahrenheit is a unit conversion type. Other conversion types may also be included.
[0046] Continuing with the example of the enhanced prompt instruction being "Continuous mapping type; Input data: Excel data; Required output: corresponding date and time format", the conversion task description information is "Input data: Excel data; Required output: corresponding date and time format", and the target conversion type is continuous mapping type.
[0047] In some embodiments, determining a prompt enhancement instruction associated with the first prompt based on the first prompt includes:
[0048] The first prompt is input into the pre-trained second language model to obtain the prompt enhancement instruction.
[0049] Here, a lightweight large language model can be used as the second large language model. When determining the prompt enhancement instruction that is related to the first prompt, the first prompt can be input into the pre-trained second large language model to obtain the prompt enhancement instruction output by the second large language model. Since the lightweight large language model has fewer model parameters, the computational load during the reasoning process is smaller, thus improving the reasoning speed for prompt enhancement instructions.
[0050] In some embodiments, the second large language model is trained according to the following process:
[0051] Acquire sample data, which includes sample prompts and sample instructions;
[0052] The sample prompt is input into the second large language model to obtain the instruction to be compared.
[0053] Based on the instruction to be compared and the sample instruction, the loss value of the second large language model is determined, and the second large language model is trained based on the loss value.
[0054] The second language model can be pre-trained using sample data through supervised fine-tuning. The sample data includes sample prompts and sample instructions. Here, the sample data can be obtained from multiple datasets and constructed in conjunction with the actual application scenario, using a data pair format similar to "(sample prompt, sample instruction)" to ensure the sample data contains clear information. After inputting the sample prompts into the second language model, the output instruction to be compared is obtained. Subsequently, based on a predetermined loss function (e.g., cross-entropy loss function), the sample instruction, and the instruction to be compared, the loss value of the second language model can be calculated, and the model parameters of the second language model can be adjusted according to the loss value. The training of the second language model is completed when the loss value of the second language model is less than a second preset loss value, or when the number of execution rounds of the aforementioned training process reaches the second preset number of rounds. The second preset loss value and the second preset number of rounds are both set according to the actual situation. In this way, the trained second language model can subsequently process the first prompt to generate prompt enhancement instructions, improving the generation efficiency of prompt enhancement instructions.
[0055] In some embodiments, the second code is corrected based on the runtime error information to obtain the first code, including:
[0056] Determine the error type corresponding to the runtime error message;
[0057] The error type is matched with multiple first preset types in a preset relationship to obtain correction suggestion information corresponding to the error type. The preset relationship includes multiple first preset types and correction suggestion information corresponding to each first preset type.
[0058] The second code is corrected based on the correction suggestion information to obtain the third code;
[0059] The third code is run to obtain a second execution result, which indicates whether the third code has run successfully.
[0060] If the second execution result indicates that the third code has executed successfully, then the first code is determined based on the third code.
[0061] If the second code fails to run, it can be corrected based on the runtime error information to obtain the first code. Specifically, first, the error type corresponding to the runtime error information can be determined, such as a value error or syntax error. Then, the error type can be matched with multiple first preset types in a preset relationship to obtain correction suggestion information corresponding to the error type. Here, the preset relationship includes multiple first preset types and correction suggestion information corresponding to each first preset type. For example, assuming the error type is a syntax error, after matching with multiple first preset types, correction suggestion information such as "The error message indicates a syntax error related to the use of single parentheses in variable names. Single parentheses cannot be used in variable names" can be obtained. Subsequently, the second code can be corrected based on the correction suggestion information to obtain the third code. For example, based on the correction suggestion information in the aforementioned example, the single parentheses in the variable names in the second code can be modified to obtain the third code.
[0062] After obtaining the third code, it can be run to obtain a second execution result. This second result indicates whether the third code ran successfully. If the second result indicates successful execution, the first code is determined based on the third code; for example, the third code is used as the first code. In this way, the process of correcting the second code can be completed, resulting in a more accurate first code.
[0063] In some embodiments, the method further includes:
[0064] If the second execution result indicates that the third code failed to run, then the number of times the second code needs to be corrected is determined.
[0065] When the number of processing times reaches a threshold, at least one first text information related to the first query information is retrieved from the database based on the first query information. The first query information includes at least one of the second code and the runtime error information.
[0066] The first text information and the second prompt are merged to obtain a third prompt for the first code;
[0067] The third prompt is input into the first large language model to obtain the first code.
[0068] If the second execution result indicates that the third code failed to run, the number of times the second code needs to be corrected is determined, and then compared with a threshold number. The threshold number is a value set based on actual conditions. If the number of processing attempts is less than the threshold, the process of correcting the second code based on the correction suggestion information to obtain the third code can continue. If the number of processing attempts reaches the threshold, lazy retrieval enhancement generation can be triggered, i.e., retrieving at least one first text information related to the first query information from the database. The first query information includes at least one of the second code and runtime error information. The database can be an external vector database. The database stores application programming interface (API) documents, code snippets, or best practice information, and the API documents, code snippets, or best practice information are vectorized and indexed. When multiple text information related to the first query information are obtained, a portion of the relevant text information can be selected as the first text information based on the similarity between the relevant text information and the first query information. Then, the first text information and the second suggestion can be merged to obtain the third suggestion for the first code. In other words, the first text information is added as additional context to the second prompt to obtain the third prompt. Here, the merging process can be either fusing the first text information and the second prompt, or combining them into a data pair. Thus, after inputting the third prompt into the first language model, the first code is obtained. When the number of times the second code is corrected reaches a threshold, relevant information is retrieved from an external database using the above steps. This information guides the first language model in generating the first code, improving the processing capability for complex data transformation tasks.
[0069] In some embodiments, after determining the error type corresponding to the runtime error information, the method further includes:
[0070] The error type is matched with multiple second preset types to obtain matching results;
[0071] Determining the first code based on the third code includes:
[0072] The third code is identified to obtain reference information in the third code, and the reference information is used to indicate whether the third code references at least one of an external database and an application programming interface.
[0073] If the preset conditions are met, at least one second text information related to the second query information is obtained from the database based on the second query information; the second query information includes at least one of the third code and the runtime error information, and the preset conditions are that the matching result indicates that the error type is successfully matched with any of the multiple second preset types, or the reference information indicates that the third code references at least one of the external database and the application programming interface;
[0074] The second text information and the second prompt are merged to obtain a fourth prompt for the first code;
[0075] The fourth prompt is input into the first large language model to obtain the first code.
[0076] For some more complex data transformation tasks, lazy retrieval can be used to enhance the accuracy of the generated first code. Specifically, after determining the error type corresponding to the runtime error message, the error type can be matched with multiple second preset types to obtain matching results. The matching results indicate whether the error type successfully matches any of the multiple second preset types. The multiple second preset types include types such as importing unknown modules and calling unknown functions. Furthermore, after running the third code obtained after correcting the second code, if the second execution result of the third code indicates that the third code ran successfully, the third code can be identified to obtain reference information. The reference information indicates whether the third code references at least one of the following: an external database and an application programming interface.
[0077] If the matching result indicates that the error type matches any of the multiple second preset types, or if the reference information indicates that the third code references at least one of an external database and an application programming interface, then the preset conditions are considered met. In this case, at least one second text information related to the second query information is retrieved from the database based on the second query information. The second query information includes at least one of the third code and runtime error information. When multiple text information related to the second query information are retrieved, a portion of the relevant text information can be selected as the second text information based on the similarity between the relevant text information and the second query information.
[0078] Subsequently, the second text information and the second prompt can be merged to obtain a fourth prompt for the first code. This fourth prompt is then input into the first language model to obtain the first code. Here, the merging process can be either fusing the second text information and the second prompt, or combining them into a data pair. For second code containing specific errors, or specific external database references or application programming interface references, obtaining relevant information from the database and using this information to guide the first language model in generating the first code can improve the processing capability for complex data transformation tasks or data transformation tasks requiring specific database support.
[0079] Figure 2 This is a schematic diagram of the structure of the code generation apparatus provided in the embodiments of this application. Please refer to it. Figure 2 This application also provides a code generation apparatus 200 that can implement the above-described code generation method. The apparatus 200 includes:
[0080] The acquisition module 201 is used to acquire the first prompt for the first code, which is used for data conversion;
[0081] The determining module 202 is used to determine, based on the first prompt, a prompt enhancement instruction that is associated with the first prompt;
[0082] The merging module 203 is used to merge the first prompt and the prompt enhancement instruction to obtain a second prompt for the first code;
[0083] The generation module 204 is used to input the second prompt into the pre-trained first large language model to obtain the second code corresponding to the second prompt;
[0084] The running module 205 is used to run the second code and obtain a first running result of the second code. The first running result is used to indicate whether the second code runs successfully. If the second code fails to run, the first running result is also used to indicate running error information.
[0085] The correction module 206 is used to correct the second code according to the running error information if the first running result indicates that the second code has failed to run, so as to obtain the first code.
[0086] In some embodiments, the correction module 206 includes:
[0087] The first determining submodule is used to determine the error type corresponding to the runtime error information;
[0088] The first matching submodule is used to match the error type with multiple first preset types in a preset relationship to obtain correction suggestion information corresponding to the error type. The preset relationship includes multiple first preset types and correction suggestion information corresponding to each first preset type.
[0089] The correction submodule is used to correct the second code according to the correction suggestion information to obtain the third code;
[0090] The running submodule is used to run the third code and obtain a second running result of the third code. The second running result is used to indicate whether the third code has run successfully.
[0091] The second determining submodule is used to determine the first code based on the third code if the second running result indicates that the third code has run successfully.
[0092] In some embodiments, the correction module 206 further includes:
[0093] The third determining submodule is used to determine the number of times to correct the second code if the second running result indicates that the third code has failed to run.
[0094] The acquisition submodule is used to, when the number of processing times reaches a threshold, retrieve at least one first text information related to the first query information from the database based on the first query information, wherein the first query information includes at least one of the second code and the runtime error information;
[0095] The merging submodule is used to merge the first text information and the second prompt to obtain a third prompt for the first code;
[0096] The first input submodule is used to input the third prompt into the first large language model to obtain the first code.
[0097] In some embodiments, the correction module 206 further includes:
[0098] The second matching submodule is used to match the error type with multiple second preset types to obtain a matching result;
[0099] The second determination submodule includes:
[0100] An identification unit is configured to identify the third code and obtain reference information in the third code, wherein the reference information is used to indicate whether the third code references at least one of an external database and an application programming interface;
[0101] The acquisition unit is configured to, if a preset condition is met, acquire at least one second text information related to the second query information from the database based on the second query information; the second query information includes at least one of the third code and the runtime error information, and the preset condition is that the matching result indicates that the error type is successfully matched with any second preset type among a plurality of second preset types, or the reference information indicates that the third code references at least one of an external database and an application programming interface;
[0102] A merging unit is used to merge the second text information and the second prompt to obtain a fourth prompt for the first code;
[0103] The input unit is used to input the fourth prompt into the first large language model to obtain the first code.
[0104] In some embodiments, the determining module 202 includes:
[0105] The second input submodule is used to input the first prompt into the pre-trained second language model to obtain the prompt enhancement instruction.
[0106] In some embodiments, the second large language model is trained according to the following process:
[0107] Acquire sample data, which includes sample prompts and sample instructions;
[0108] The sample prompt is input into the second large language model to obtain the instruction to be compared.
[0109] Based on the instruction to be compared and the sample instruction, the loss value of the second large language model is determined, and the second large language model is trained based on the loss value.
[0110] In some embodiments, the prompting enhancement instruction includes conversion task description information and target conversion type. The conversion task description information is used to describe the data conversion operation performed on the target data, and the target conversion type is any one of formatting type, extraction type, discrete mapping type, continuous mapping type, or unit conversion type.
[0111] The specific implementation of the code generation device 200 is basically the same as the specific implementation of the code generation method described above, and will not be repeated here.
[0112] This application also provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the above-described code generation method. This electronic device can be any smart terminal, including tablet computers, in-vehicle computers, etc.
[0113] Figure 3 This is a schematic diagram of the hardware structure of the electronic device provided in the embodiments of this application. Please refer to it. Figure 3 Electronic devices include:
[0114] The processor 301 can be implemented using a general-purpose CPU (Central Processing Unit), microprocessor, application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this application.
[0115] The memory 302 can be implemented as a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 302 can store the operating system and other applications. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 302 and is called and executed by the processor 301 using the code generation method of the embodiments of this application.
[0116] Input / output interface 303 is used to implement information input and output;
[0117] The communication interface 304 is used to enable communication and interaction between this device and other devices. Communication can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.).
[0118] Bus 305 transmits information between various components of the device (e.g., processor 301, memory 302, input / output interface 303, and communication interface 304);
[0119] The processor 301, memory 302, input / output interface 303, and communication interface 304 are connected to each other within the device via bus 305.
[0120] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the above-described code generation method.
[0121] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
[0122] The code generation method, apparatus, electronic device, and storage medium provided in this application obtain a first prompt for a first code, then determine a prompt enhancement instruction related to the first prompt based on the first prompt, and then merge the first prompt and the prompt enhancement instruction to obtain a second prompt for the first code. The second prompt is input into a pre-trained first language model to obtain a second code corresponding to the second prompt. In this way, the understanding of the input prompt by the first language model can be strengthened, thereby improving the accuracy of the second code generated by the first language model. Then, the second code is run to obtain a first running result of the second code. If the first running result indicates that the second code has failed to run, the second code is corrected according to the running error information indicated by the first running result to obtain the first code. In this way, in the case of failure of the second code, the second code is corrected according to the running error information related to the second code, which can further reduce the errors in the code. Therefore, through the aforementioned two stages (i.e., the enhancement of the prompt and the correction of the second code), the accuracy of the final code can be improved.
[0123] The embodiments described in this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided by the embodiments of this application. As those skilled in the art will know, with the evolution of technology and the emergence of new application scenarios, the technical solutions provided by the embodiments of this application are also applicable to similar technical problems.
[0124] Those skilled in the art will understand that the technical solutions shown in the figures do not constitute a limitation on the embodiments of this application, and may include more or fewer steps than shown, or combine certain steps, or different steps.
[0125] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0126] Those skilled in the art will understand that all or some of the steps in the methods disclosed above, as well as the functional modules / units in the systems and devices, can be implemented as software, firmware, hardware, or suitable combinations thereof.
[0127] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “having,” and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0128] It should be understood that in this application, "at least one (item)" means one or more, and "more than" means two or more. "And / or" is used to describe the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent three cases: only A exists, only B exists, and both A and B exist simultaneously, where A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. "At least one (item) of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one (item) of a, b, or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, and c can be single or multiple.
[0129] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of the units described above is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.
[0130] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0131] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0132] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes multiple instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing programs, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0133] The preferred embodiments of the present application have been described above with reference to the accompanying drawings, but this does not limit the scope of the claims of the present application. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and substance of the embodiments of the present application shall be within the scope of the claims of the present application.
Claims
1. A code generation method, characterized in that, The method includes: Get the first hint for the first code, which is used to perform data transformation; Based on the first prompt, determine the prompt enhancement instruction that is related to the first prompt; The first prompt and the prompt enhancement instruction are merged to obtain a second prompt for the first code; The second prompt is input into the pre-trained first language model to obtain the second code corresponding to the second prompt; The second code is run to obtain a first execution result. The first execution result is used to indicate whether the second code has run successfully. If the second code fails to run, the first execution result is also used to indicate an error message. If the first execution result indicates that the second code failed to run, then the second code is corrected according to the execution error information to obtain the first code.
2. The method according to claim 1, characterized in that, The second code is corrected based on the runtime error information to obtain the first code, including: Determine the error type corresponding to the runtime error message; The error type is matched with multiple first preset types in a preset relationship to obtain correction suggestion information corresponding to the error type. The preset relationship includes multiple first preset types and correction suggestion information corresponding to each first preset type. The second code is corrected based on the correction suggestion information to obtain the third code; The third code is run to obtain a second execution result, which indicates whether the third code has run successfully. If the second execution result indicates that the third code has executed successfully, then the first code is determined based on the third code.
3. The method according to claim 2, characterized in that, The method further includes: If the second execution result indicates that the third code failed to run, then the number of times the second code needs to be corrected is determined. When the number of processing times reaches a threshold, at least one first text information related to the first query information is retrieved from the database based on the first query information. The first query information includes at least one of the second code and the runtime error information. The first text information and the second prompt are merged to obtain a third prompt for the first code; The third prompt is input into the first large language model to obtain the first code.
4. The method according to claim 2, characterized in that, After determining the error type corresponding to the runtime error information, the method further includes: The error type is matched with multiple second preset types to obtain matching results; Determining the first code based on the third code includes: The third code is identified to obtain reference information in the third code, and the reference information is used to indicate whether the third code references at least one of an external database and an application programming interface. If the preset conditions are met, at least one second text information related to the second query information is obtained from the database based on the second query information; the second query information includes at least one of the third code and the runtime error information, and the preset conditions are that the matching result indicates that the error type is successfully matched with any of the multiple second preset types, or the reference information indicates that the third code references at least one of the external database and the application programming interface; The second text information and the second prompt are merged to obtain a fourth prompt for the first code; The fourth prompt is input into the first large language model to obtain the first code.
5. The method according to claim 1, characterized in that, The step of determining, based on the first prompt, a prompt enhancement instruction that is associated with the first prompt includes: The first prompt is input into the pre-trained second language model to obtain the prompt enhancement instruction.
6. The method according to claim 5, characterized in that, The second major language model is trained according to the following process: Acquire sample data, which includes sample prompts and sample instructions; The sample prompt is input into the second large language model to obtain the instruction to be compared. Based on the instruction to be compared and the sample instruction, the loss value of the second large language model is determined, and the second large language model is trained based on the loss value.
7. The method according to claim 1, characterized in that, The enhanced prompt instruction includes conversion task description information and target conversion type. The conversion task description information is used to describe the data conversion operation performed on the target data. The target conversion type is any one of formatting type, extraction type, discrete mapping type, continuous mapping type, or unit conversion type.
8. A code generation device, characterized in that, The device includes: The acquisition module is used to acquire the first prompt for the first code, which is used for data conversion. The determining module is used to determine, based on the first prompt, a prompt enhancement instruction that is associated with the first prompt; The merging module is used to merge the first prompt and the prompt enhancement instruction to obtain a second prompt for the first code; The generation module is used to input the second prompt into the pre-trained first large language model to obtain the second code corresponding to the second prompt; The execution module is used to run the second code and obtain a first execution result of the second code. The first execution result is used to indicate whether the second code has run successfully. If the second code fails to run, the first execution result is also used to indicate an error message. The correction module is used to correct the second code according to the running error information if the first running result indicates that the second code has failed to run, so as to obtain the first code.
9. An electronic device, characterized in that, The electronic device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the code generation method according to any one of claims 1 to 7.
10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the code generation method according to any one of claims 1 to 7.