Code generation method and device based on pre-trained model, equipment and medium
By extracting contextual code information and automatically generating code using a pre-trained model, the problem of natural language models relying on manual input of descriptive information is solved, thereby improving the accuracy and efficiency of code generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- BEIJING VOLCANO ENGINE TECH CO LTD
- Filing Date
- 2024-02-20
- Publication Date
- 2026-05-19
AI Technical Summary
In existing technologies, natural language models rely on developers to input descriptive information when generating code, making it difficult to guarantee the accuracy and efficiency of code generation.
By extracting contextual code information from the target generation location, corresponding code hints are generated, and pre-trained code generation models are used to automatically generate code, reducing reliance on human input.
It improves the accuracy and efficiency of code generation, reduces human error, and enhances the development efficiency of software code.
Smart Images

Figure CN117971185B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, specifically to a code generation method, apparatus, device, and medium based on a pre-trained model. Background Technology
[0002] During software coding, developers typically write code based on requirements and their understanding of relevant components. However, when encountering unfamiliar areas or components, developers often resort to search engines for solutions, which is inefficient. With the continuous development of natural language models, code generation tools can automatically generate relevant code based on language prompts or partial code snippets, significantly improving development efficiency and quality.
[0003] However, natural language models can only generate or optimize code based on the descriptive information input by developers. Developers need to input a lot of descriptive information to ensure the accuracy of the generated or optimized software code. However, relying on the descriptive information input by developers makes it difficult to guarantee the accuracy and efficiency of the generated software code. Summary of the Invention
[0004] In view of this, this disclosure provides a code generation method, apparatus, device, and medium based on a pre-trained model to solve the problems of low accuracy and efficiency in software code generation.
[0005] In a first aspect, this disclosure provides a code generation method based on a pre-trained model, comprising: obtaining the target generation position of the code to be generated; extracting context code information corresponding to the target generation position, and generating code hint information that matches the context code information; inputting the code hint information into a preset code generation model, and generating target code corresponding to the target generation position through the preset code generation model; wherein, the preset code generation model is pre-trained based on the hint information samples.
[0006] Secondly, this disclosure provides a code generation device based on a pre-trained model, comprising: an acquisition module for acquiring the target generation location of the code to be generated; a prompt information generation module for extracting context code information corresponding to the target generation location and generating code prompt information matching the context code information; and a code generation module for inputting the code prompt information into a preset code generation model and generating target code corresponding to the target generation location through the preset code generation model, wherein the preset code generation model is pre-trained based on prompt information samples.
[0007] Thirdly, this disclosure provides a computer device, including: a memory and a processor, which are communicatively connected to each other. The memory stores computer instructions, and the processor executes the computer instructions to perform the code generation method based on a pre-trained model as described in the first aspect or any corresponding embodiment.
[0008] Fourthly, this disclosure provides a computer-readable storage medium storing computer instructions for causing a computer to execute the code generation method based on a pre-trained model according to the first aspect or any corresponding embodiment thereof.
[0009] The code generation method based on a pre-trained model provided in this disclosure extracts contextual code information from the target generation location to generate corresponding code hints, thus achieving automatic generation of code hints without relying on manual input of descriptive information. Furthermore, combining contextual code information with the generated code hints ensures greater accuracy. Subsequently, a preset code generation model generates the target code according to the code hints, thereby improving the accuracy and efficiency of target code generation and further enhancing software code development efficiency. Attached Figure Description
[0010] To more clearly illustrate the technical solutions in the specific embodiments of this disclosure or the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0011] Figure 1 This is a flowchart illustrating a code generation method based on a pre-trained model according to an embodiment of the present disclosure;
[0012] Figure 2 This is a flowchart illustrating another code generation method based on a pre-trained model according to an embodiment of the present disclosure;
[0013] Figure 3 This is a specific schematic diagram generated from code based on embodiments of this disclosure;
[0014] Figure 4 This is a schematic diagram of assembling a template based on an embodiment of this disclosure;
[0015] Figure 5 This is a flowchart illustrating another code generation method based on a pre-trained model according to an embodiment of the present disclosure;
[0016] Figure 6This is a structural block diagram of a code generation apparatus based on a pre-trained model according to an embodiment of the present disclosure;
[0017] Figure 7 This is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present disclosure. Detailed Implementation
[0018] To make the objectives, technical solutions, and advantages of the embodiments of this disclosure clearer, the technical solutions of the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this disclosure, and not all embodiments. Based on the embodiments of this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.
[0019] In the automatic generation of software code, related technologies primarily utilize Large Language Models (LLMs) to generate corresponding code or optimize existing code based on prompt text input by developers. To ensure the accuracy of code generation or optimization, developers need to input a large amount of prompt text. However, since the input of prompt text depends on the developers, even with extensive input, errors due to human factors are difficult to avoid, thus compromising the correctness of the generated code.
[0020] The technical solution disclosed herein generates corresponding code hints based on code context, execution context, and process context, so that the code generation model can automatically generate the corresponding target code according to the code hints, effectively reducing the dependence on developers, reducing personnel usage costs, and improving code generation efficiency and accuracy.
[0021] According to an embodiment of this disclosure, a code generation method based on a pre-trained model is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0022] This embodiment provides a code generation method based on a pre-trained model, which can be used on computer devices such as computers and laptops. Figure 1 This is a flowchart of a code generation method based on a pre-trained model according to an embodiment of this disclosure, such as... Figure 1 As shown, the process includes the following steps:
[0023] Step S101: Obtain the target generation location for the code to be generated.
[0024] The code to be generated is the code to be written according to the functions required by the software. Specifically, the computer device is equipped with code development tools (such as a Notebook). These tools have corresponding code editing pages, allowing developers to use them to develop software code and implement specific functions. The code to be generated can be in any programming language, such as Python or SQL code.
[0025] The target generation location is the position of the code to be generated. Specifically, when developers encounter unfamiliar areas or components while writing code through the code editing page, an automatic code generation process is triggered to ensure software development efficiency. At this time, the computer device can record the position where the developer stops code development and triggers the automatic code generation command, and determine that position as the target generation location of the code to be generated.
[0026] Step S102: Extract the context code information corresponding to the target generation location, and generate code hint information that matches the context code information.
[0027] Context code information refers to the code information in the units above and below the target generation location, such as variable types, variable contents, and output results. Specifically, based on the target generation location, the code content in the unit above the target generation location is parsed to obtain the code information in that unit; simultaneously, the code content in the unit below the target generation location is parsed to obtain the code information in that unit.
[0028] It should be noted that if the target generation location does not have a lower unit, the above code information corresponding to the target generation location is extracted.
[0029] Code hints are key text information used to describe the key text information required to generate code. Combined with the contextual code information, the code type, code variable information, code execution information, etc. required for the code to be generated can be determined. The key text information (such as key variable text, key execution result text, etc.) required for the code to be generated is extracted according to the code type, code variable information, code execution information, etc. The extracted key text information is used to describe the code to be generated in order to obtain the code hint information corresponding to the code to be generated.
[0030] Step S103: Input the code hint information into the preset code generation model, and generate the target code corresponding to the target generation position through the preset code generation model.
[0031] The preset code generation model is pre-trained based on the prompt information samples.
[0032] The default code generation model is a model trained using prompt information samples as input and code as output, such as a natural language model, deep learning model, or machine learning model. The specific training process is as follows:
[0033] Step 1: Obtain a sample set of prompt messages. This sample set includes multiple prompt message samples. The prompt message sample set is a pre-collected set of prompt messages specific to the code implementing a particular function. This set includes multiple prompt messages described in different ways, and these prompt messages are used as the prompt message samples. Specifically, this prompt message sample set can be obtained manually by developers or collected online; the method of obtaining the prompt message sample set is not specifically limited here.
[0034] Step 2: For any target prompt information sample in the prompt information sample set, extract the description sample for code generation from the target prompt information sample, and process the description sample using a preset code generation model to output the predicted code corresponding to the description sample.
[0035] Specifically, for any target prompt information sample in the prompt information sample set, semantic recognition and semantic segmentation are performed to extract key text information of the target prompt information sample. This key text information is then used to describe the code to be generated, producing a description sample corresponding to that code. Subsequently, the description sample is input into a preset code generation model for prediction processing, outputting the predicted code corresponding to the description sample.
[0036] Step 3: Compare the actual code corresponding to the predicted code and the target prompt information sample, and generate error information based on the comparison results, so as to correct the preset code generation model through the error information.
[0037] Specifically, the predicted code output by the preset code generation model is compared with the actual code corresponding to the target prompt information sample to generate a comparison result. The error information between the predicted code and the actual code is determined by the comparison result. The preset code generation model is then iteratively trained based on the error information to correct the preset code generation model and make its output more accurate.
[0038] Therefore, by training a pre-defined code generation model in conjunction with code suggestion information, the system was able to automatically generate corresponding code based on the code suggestion information.
[0039] The target code is generated based on code hints and is used to connect with the code in the preceding and following units at the target generation location to achieve the corresponding specific function. Specifically, the trained pre-defined code generation model is deployed on a computer device. After receiving code hints, the computer device inputs these hints into the pre-defined code generation model to request automatic code generation. Accordingly, the pre-defined code generation model executes the code generation process according to the code hints and generates the corresponding target code.
[0040] The code generation method based on a pre-trained model provided in this embodiment extracts contextual code information from the target generation location to generate corresponding code hints, thus achieving automatic generation of code hints without relying on manual input of descriptive information. Furthermore, combining contextual code information with the generated code hints ensures greater accuracy. Subsequently, a preset code generation model generates the target code according to the code hints, thereby improving the accuracy and efficiency of target code generation and further enhancing software code development efficiency.
[0041] This embodiment provides a code generation method based on a pre-trained model, which can be used on computer devices such as computers and laptops. Figure 2 This is a flowchart of a code generation method based on a pre-trained model according to an embodiment of this disclosure, such as... Figure 2 As shown, the process includes the following steps:
[0042] Step S201: Obtain the target generation location for the code to be generated. For details, please refer to the relevant descriptions of the corresponding steps in the above embodiments, which will not be repeated here.
[0043] Step S202: Extract the context code information corresponding to the target generation location, and generate code hint information that matches the context code information.
[0044] Specifically, step S202 above may include:
[0045] Step S2021: Obtain the code text of the upper and lower units corresponding to the target generation position, and extract the code context information corresponding to the target generation position from the code text of the upper and lower units.
[0046] The code text represents the code written in each unit above and below the target generation position, while the code context information is the code extracted from the code text of each unit above and below. Specifically, a content retrieval component is deployed in the code development tool to extract contextual information from the currently written code. This content retrieval component includes a unit content retrieval subcomponent, which extracts the corresponding code context information from the units above and below the target generation position.
[0047] like Figure 3 As shown, if the target generation location is A on the code editing page, then the cell above the target generation location is B, and the cell below it is C. The cell content retrieval sub-component can then extract the corresponding code context information from the cell above B and the cell below C.
[0048] Step S2022: Obtain the execution results of the code text of the upper and lower units, and extract the execution context information corresponding to the target generation position from the execution results.
[0049] The execution result represents the output information after the code text has been executed. This result helps determine if there are any execution errors in the code text within the preceding and following units. The execution context information is textual information extracted from the execution result to describe the execution outcome.
[0050] Specifically, such as Figure 3 As shown, the content retrieval component includes an execution content retrieval subcomponent, which extracts the corresponding execution context information from the execution results of the preceding and following units.
[0051] If the target generation location is A on the code editing page, the cell above the target generation location is B, and the cell below it is C, then the execution content retrieval sub-component can extract the execution text information for the cell above B from the execution result of the cell above B, and extract the execution text information for the cell below C from the execution result of the cell below C.
[0052] Step S2023: Obtain the variable information corresponding to the execution result, and extract the process context information corresponding to the target generation location from the variable information.
[0053] Variable information represents the information of each variable obtained after the code in the preceding and following units has been executed. Specifically, this includes the name, type, and stored data of each variable. After the code in the preceding and following units has finished executing, the variable information it generates resides in memory space, which is the space pre-allocated by the computer device for code execution. Process context information is textual information extracted from memory space to describe the variables generated during code execution.
[0054] Specifically, such as Figure 3 As shown, the content retrieval component includes an execution process content retrieval subcomponent. This process content retrieval subcomponent retrieves variable information corresponding to the execution results of the preceding and following units from the memory space, so as to extract the corresponding process context information from the variable information.
[0055] For example, if the target generation location is A on the code editing page, the unit above the target generation location is B, and the unit below it is C, then the process content retrieval subcomponent can retrieve the variable information generated after the code execution of the unit above B from the memory space, and extract the variable text information generated after the code execution of the unit above B from the variable information. Simultaneously, it can retrieve the variable information generated after the code execution of the unit below C from the memory space, and extract the variable text information generated after the code execution of the unit below C from the variable information. The extracted variable text information generated after the code execution of the unit above B and the variable text information generated after the code execution of the unit below C are then defined as the process context information.
[0056] Step S2024: Obtain the preset information assembly template and determine the filling position of the context code information in the information assembly template.
[0057] The information assembly template is a pre-defined information filling template for context code information. This template configures corresponding placeholders for the context information, allowing different context code information to be assembled together. The filling position indicates the location of the context code information within the information assembly template; this position can be determined using placeholders.
[0058] like Figure 4 Taking the information assembly template shown as an example, this template includes a preceding code information placement area and a following code information placement area. The preceding code information placement area is located above the following code information placement area. This area includes a code preceding sub-area, an execution preceding sub-area, and a process preceding sub-area. Each preceding sub-area has a corresponding unique identifier and a placeholder. The unique identifier and placeholder can be on the same line or on adjacent different lines. Similarly, the following code information placement area includes a code following sub-area, an execution following sub-area, and a process following sub-area. Each following sub-area also has a corresponding unique identifier and a placeholder. These unique identifiers and placeholders can be on the same line or on adjacent different lines.
[0059] In a specific example, this unique identifier can be formed by combining text and preset symbols, such as... Figure 4The unique identifiers and their corresponding placeholders shown in the examples "#code above", "#execution above", "#process above", "#code below", "#execution below", and "#process below" are located in adjacent lines above and below each other.
[0060] As an optional implementation, the code information area can also be placed above the code information area above it, for example, "#code below", "#execution below", "#process below", "#code above", "#execution above", "#process above"; or the same type of context information can be arranged sequentially, such as "#code above", "#code below", "#execution above", "#execution below", "#process above", "#process below", etc. There is no specific limitation on the information assembly template here, and those skilled in the art can set it according to actual needs.
[0061] Step S2025: Fill the context code information into the information assembly template according to the filling position, assemble the context code information according to the information assembly template, and generate code prompt information.
[0062] After extracting the context code information, it is placed in the information assembly template according to its position, completing the assembly of the context code information. The assembled information describes the contextual requirements that the code to be generated must meet; that is, the assembled information serves as code hints for code generation. This code hint is then input into a preset code generation model to assist developers in automatically generating the target code.
[0063] In some optional implementations, when generating code hints, step S202 may further include:
[0064] Step a1: Obtain code generation description information and determine the first assembly position of the code generation description information in the information assembly template.
[0065] Step a2: In response to the code function description operation, generate code function description information and determine the second assembly position of the code function description information in the information assembly template.
[0066] Step a3: Fill the information assembly template with code generation description information, code function description information, and context code information according to the first assembly position, the second assembly position, and the filling position.
[0067] Step a4: Assemble the code generation description information, code function description information, and context code information according to the information assembly template to generate code hint information.
[0068] Code generation description information is the text description information generated when automatic code generation is triggered, such as "#Start automatic code generation process" or "#Start automatic code generation assistant". The first assembly position is the location of the code generation description information within the information assembly template; for example, the first assembly position is above the context code information. Figure 4 As shown, the information assembly template has a first description information area for placing code generation description information, and the code generation description information is located in the first description information area.
[0069] Specifically, a prompt information generation system is deployed in the computer device. This prompt information generation system is connected to the code generation tool via a communication interface, allowing the code generation tool to transmit the context code information extracted by the content retrieval component to the prompt information generation system through the communication interface, such as... Figure 3 As shown. Accordingly, when the prompt message generation system receives the context code information, it starts the automatic code generation process, triggers the prompt message generation system to generate the corresponding text description information, and identifies this text description information as the code generation description information.
[0070] The code function description operation is an operation performed by developers to describe the function of the code to be generated. This code function description operation can be triggered by input devices connected to the computer device, such as keyboard and mouse, or by voice components deployed in the computer device. Of course, it can also be triggered by a virtual keyboard. There are no specific restrictions on the form of triggering the code function description operation.
[0071] The code function description information is textual information used to describe the specific function that the generated code needs to achieve, such as "#Generate a piece of code to implement quicksort function". The second assembly position is the location of the code function description information in the information assembly template, for example, the second assembly position is below the context code information. Figure 4 As shown, the information assembly template includes a second description information area for placing code function description information. Specifically, after a developer triggers a code function description operation, the computer device can respond to the developer's operation and generate the corresponding code function description information on the code editing page.
[0072] The information assembly template sets up areas for placing code generation description information and code function description information, such as... Figure 4As shown. After obtaining the code generation description information and the code function description information, the code generation description information is placed in the code generation description information placement area of the information assembly template according to the first assembly position, and the code function description information is placed in the code function description information placement area according to the second assembly position. At the same time, context code information is placed in the filling position, thereby realizing the assembly of the code generation description information, the code function description information, and the context code information to generate code prompt information for the target generation position.
[0073] In the above embodiments, the code function description information, code generation description information, and context code information are assembled using an information assembly template, making the description information used to generate code suggestion information more comprehensive. Subsequently, code suggestion information is generated based on the assembled information, greatly improving the accuracy of code suggestion information generation.
[0074] Step S203: Input the code hint information into the preset code generation model, and generate target code corresponding to the target generation location through the preset code generation model. The preset code generation model is pre-trained based on the hint information samples. For details, please refer to the relevant descriptions of the corresponding steps in the above embodiments, which will not be repeated here.
[0075] The code generation method based on a pre-trained model provided in this embodiment extracts more comprehensive contextual code information by considering the code context information of the preceding and following units, execution context information, and process context information. This ensures the accuracy of contextual code information extraction for the target generation location, thereby improving the accuracy of code suggestion generation. Furthermore, by combining the extracted code context information of the preceding and following units, execution context information, and process context information with an information assembly template, the method achieves ordered assembly of contextual code information, avoiding chaotic assembly that could affect code suggestion generation and further improving the accuracy of code suggestion generation.
[0076] This embodiment provides a code generation method based on a pre-trained model, which can be used on computer devices such as computers and laptops. Figure 5 This is a flowchart of a code generation method based on a pre-trained model according to an embodiment of this disclosure, such as... Figure 5 As shown, the process includes the following steps:
[0077] Step S301: Obtain the target generation location for the code to be generated.
[0078] Specifically, step S301 above may include:
[0079] Step S3011: Detect whether a code generation instruction has been triggered at the current code editing location.
[0080] The current code editing location is the developer's real-time editing location. Code generation commands are instructions triggered by the developer to automatically generate code. These commands can be triggered via keyboard shortcuts, by the developer entering code function descriptions, or by the code generation icon (label) on the code editing page. No specific limitations are placed on the method of triggering code generation commands here; those skilled in the art can determine the appropriate method based on actual needs.
[0081] When developers struggle to complete development of specific functionalities at the current code editing location, a code generation command can be triggered at that location to initiate an automatic code generation process, ensuring the software development progress. Therefore, during code development using the code generation tool's code editing interface, the system can detect in real-time whether a code generation command has been triggered at the current editing location. If a command is detected, step S3012 is executed; otherwise, the code development detection continues.
[0082] Step S3012: If a code generation instruction is triggered at the current code editing position, the current code editing position is determined as the target generation position.
[0083] If a code generation command is triggered at the current code editing location, it means that the developer needs to execute the automatic code generation process at the current code editing location, and the current code editing location where automatic code generation is required is set as the target generation location for the code to be generated.
[0084] Step S302: Extract the context code information corresponding to the target generation location, and generate code hint information that matches the context code information. For details, please refer to the relevant descriptions of the corresponding steps in the above embodiments, which will not be repeated here.
[0085] Step S303: Input the code hint information into the preset code generation model, and generate target code corresponding to the target generation location through the preset code generation model. The preset code generation model is pre-trained based on the hint information samples.
[0086] Step S304: Check whether the execution result of the target code is correct.
[0087] After the target code is generated, the execution process begins to produce execution results, which may include code execution logs, warnings, and error messages. The execution results determine the execution status of the target code. By parsing the information contained in the execution results, it can be determined whether the target code's execution is correct. If the target code's execution result is incorrect, step S305 is executed; if the target code's execution result is correct, this target code is used as the final code connecting the preceding and following units to achieve a specific function.
[0088] Step S305: When the execution result of the target code is incorrect, extract the execution error information of the target code.
[0089] Execution error messages are textual descriptions of code execution errors. When the execution result of the target code is incorrect, it indicates that the currently generated target code contains errors, meaning the target code needs further optimization. In this case, in order to optimize the target code so that the preset code generation model can generate correct target code, execution error messages can be extracted from the execution result.
[0090] Step S306: Update the code prompt information based on the execution error information to obtain the updated target prompt information.
[0091] The target suggestion information is a textual description used to generate code. Specifically, execution error information is used as supplementary information to the code suggestion information. This supplementary information is then updated to generate corresponding target suggestion information to prevent the preset code generation model from generating incorrect target code again.
[0092] Step S307: Regenerate the target code according to the target prompt information using the preset code generation model.
[0093] The target hint information is input into the preset code generation model to request code generation based on the target hint information. Specifically, during the process of regenerating the target code based on the target hint information, the target code that executes incorrectly is input into the preset code generation model as target hint information, so that the preset code generation model optimizes the target code that executes incorrectly according to the target hint information and generates code that matches the target hint information.
[0094] In some alternative implementations, the above method may further include:
[0095] Step b1: Detect the number of errors in the execution result.
[0096] Step b2: When the number of errors reaches a preset number, in response to the adjustment operation for the target code, the adjusted target code is generated.
[0097] The error count is the number of consecutive errors that occur when the preset code generation model executes target code designed to achieve the same specific function. Specifically, each time the preset code generation model outputs target code, the target code is executed once, and the execution result of the target code is used to count the number of execution errors.
[0098] The preset number of errors is a pre-defined maximum allowed number of execution errors, such as 3. There is no specific limit to this preset number. Each time the target code encounters an error, the error count is accumulated. The error count generated by the current automatic code generation is compared with the preset number to determine if the preset number has been reached. When the preset number of errors is reached, it indicates that the preset code generation model is unable to automatically generate the correct target code that matches the target generation location. In this case, a warning message can be generated to alert developers to intervene and adjust the automatically generated target code to obtain the required target code. When the preset number of errors is not reached, the generated target code can be optimized based on the execution error information to make the generated target code more accurate.
[0099] In the above implementation, when the number of errors in the execution result reaches a preset number, it indicates that there is a problem with the generation of the target code. At this time, the target code can be adjusted by the developers so that the error information of the target code can be processed in a timely manner, so as to avoid affecting the development and execution of subsequent software code.
[0100] In some optional implementations, after generating the target code corresponding to the target generation position, the method may further include: filling the target generation position with the target code generated by the preset code generation model. Specifically, after the preset code generation model outputs the target code, the target code output by the preset code generation model is placed at the target generation position, thereby achieving the connection between the target code and the code of the preceding and following units based on the implementation of a specific function through the target code.
[0101] After generating the target code, the target code is directly filled into the target location to connect the code of the preceding and following units, thus ensuring the integrity of the software code.
[0102] In some optional implementations, after generating the target code corresponding to the target generation location, the above method may further include: creating a new code generation location and filling the newly created code generation location with the target code generated by the preset code generation model.
[0103] The code generation location is a designated place for the target code. When the automatic code generation process for the target generation location is started, a corresponding code generation location is created, and the target code output by the preset code generation model is placed in the newly created code generation location. After confirming that the target code's execution result is correct, the target code from the code generation location is then placed back into the target generation location.
[0104] After generating the target code, the target code is filled into the newly created code generation location. After confirming that the target code execution result is correct, the target code is then placed in the target generation location. This avoids the direct insertion of target code with execution errors into the target generation location, which would affect the execution of other unit code. Thus, it is possible to simultaneously ensure the accuracy of target code generation and the integrity of software code.
[0105] The code generation method based on a pre-trained model provided in this embodiment combines the triggering of code generation instructions to determine the corresponding target generation location, thus achieving effective determination of the target generation location. After generating the target code, the execution result of the target code is detected to optimize the code suggestion information based on the execution result, thereby achieving optimization of the target code and significantly improving the optimization accuracy of the target code.
[0106] This embodiment also provides a code generation apparatus based on a pre-trained model, which is used to implement the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can be a combination of software and / or hardware that performs a predetermined function. Although the apparatus described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0107] This embodiment provides a code generation device based on a pre-trained model, such as... Figure 6 As shown, it includes:
[0108] The acquisition module 401 is used to obtain the target generation location of the code to be generated.
[0109] The prompt message generation module 402 is used to extract the context code information corresponding to the target generation location and generate code prompt information that matches the context code information.
[0110] The code generation module 403 is used to input code prompt information into a preset code generation model and generate target code corresponding to the target generation position through the preset code generation model. The preset code generation model is pre-trained based on the prompt information samples.
[0111] In some optional implementations, the acquisition module 401 described above may include:
[0112] The detection unit is used to detect whether a code generation instruction has been triggered at the current code editing location.
[0113] The location determination unit is used to determine the current code editing location as the target generation location if a code generation instruction is triggered at the current code editing location.
[0114] In some optional implementations, the above-mentioned prompt information generation module 402 may include:
[0115] The code content extraction unit is used to obtain the code text of the units above and below the target generation position, and extract the code context information of the target generation position from the code text of the units above and below.
[0116] The execution content extraction unit is used to obtain the execution results of the code text of the preceding and following units, and to extract the execution context information corresponding to the target generation position from the execution results.
[0117] The process content extraction unit is used to obtain variable information corresponding to the execution result and extract process context information corresponding to the target generation location from the variable information.
[0118] In some optional implementations, the above-mentioned prompt information generation module 402 may further include:
[0119] The fill position determination unit is used to obtain a preset information assembly template and determine the fill position of the context code information in the information assembly template.
[0120] The assembly unit is used to fill the context code information into the information assembly template according to the filling position, assemble the context code information according to the information assembly template, and generate code hint information.
[0121] In some optional implementations, the above-mentioned prompt information generation module 402 may further include:
[0122] The generation description information acquisition unit is used to acquire code generation description information and determine the first assembly position of the code generation description information in the information assembly template.
[0123] The function description information generation unit is used to generate code function description information in response to the code function description operation and determine the second assembly position of the code function description information in the information assembly template.
[0124] The filling unit is used to fill the information assembly template with code generation description information, code function description information and context code information according to the first assembly position, the second assembly position and the filling position.
[0125] The information assembly unit is used to assemble code generation description information, code function description information, and context code information according to the information assembly template, and generate code prompt information.
[0126] In some alternative embodiments, the above-described apparatus may further include:
[0127] The execution detection module is used to check whether the execution result of the target code is correct.
[0128] The error message extraction module is used to extract execution error messages from the target code when the execution result of the target code is incorrect.
[0129] The update module is used to update the code prompt information based on the execution error information, and obtain the updated target prompt information.
[0130] The optimization module is used to generate a model from preset code and regenerate the target code according to the target prompt information.
[0131] In some alternative embodiments, the above-described apparatus may further include:
[0132] The error count acquisition module is used to detect the number of errors in the execution result.
[0133] The adjustment module is used to generate adjusted target code in response to adjustment operations on the target code when the number of errors reaches a preset number.
[0134] In some alternative embodiments, the above-described apparatus may further include:
[0135] The target code filling module is used to fill the target code generated by the preset code generation model into the target generation location, or to fill the target code generated by the preset code generation model into the newly created code generation location.
[0136] In this embodiment, the code generation device based on the pre-trained model is presented in the form of a functional unit. Here, a unit refers to an ASIC circuit, a processor and memory that execute one or more software or fixed programs, and / or other devices that can provide the above functions.
[0137] Further functional descriptions of the above modules and units are the same as those in the corresponding embodiments described above, and will not be repeated here.
[0138] The code generation device based on a pre-trained model provided in this embodiment extracts contextual code information from the target generation location and generates corresponding code hints based on this contextual information. This achieves automatic generation of code hints without relying on manual input of descriptive information. Furthermore, combining contextual code information with the generated code hints ensures greater accuracy. Subsequently, the target code is generated according to the code hints using a preset code generation model, thereby improving the accuracy and efficiency of target code generation and further enhancing software code development efficiency.
[0139] This disclosure also provides a computer device having the above-described features. Figure 6 The code generation device shown is based on a pre-trained model.
[0140] Please see Figure 7 , Figure 7 This is a schematic diagram of the structure of a computer device provided in an optional embodiment of this disclosure, such as... Figure 7 As shown, the computer device includes one or more processors 10, memory 20, and interfaces for connecting the components, including high-speed interfaces and low-speed interfaces. The components communicate with each other via different buses and can be mounted on a common motherboard or otherwise installed as needed. The processors can process instructions executed within the computer device, including instructions stored in or on memory to display graphical information of a GUI on external input / output devices (such as display devices coupled to the interfaces). In some alternative implementations, multiple processors and / or multiple buses can be used with multiple memories and multiple memory modules, if desired. Similarly, multiple computer devices can be connected, each providing some of the necessary operations (e.g., as a server array, a group of blade servers, or a multiprocessor system). Figure 7 Take a processor 10 as an example.
[0141] Processor 10 may be a central processing unit, a network processor, or a combination thereof. Processor 10 may further include a hardware chip. The hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. The programmable logic device may be a complex programmable logic device (CAMP), a field-programmable gate array (FPGA), a general-purpose array logic (GDA), or any combination thereof.
[0142] The memory 20 stores instructions executable by at least one processor 10 to cause the at least one processor 10 to perform the method shown in the above embodiments.
[0143] The memory 20 may include a program storage area and a data storage area. The program storage area may store the operating system and applications required for at least one function; the data storage area may store data created based on the use of the computer device as shown by a landing page for an app. Furthermore, the memory 20 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 alternative embodiments, the memory 20 may optionally include memory remotely located relative to the processor 10, which can be connected to the computer device 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.
[0144] The memory 20 may include volatile memory, such as random access memory; the memory may also include non-volatile memory, such as flash memory, hard disk or solid-state drive; the memory 20 may also include a combination of the above types of memory.
[0145] The computer device also includes an input device 30 and an output device 40. The processor 10, memory 20, input device 30, and output device 40 can be connected via a bus or other means. Figure 7 Taking the example of a connection between China and Israel via a bus.
[0146] Input device 30 can receive input numerical or character information, and generate key signal inputs related to user settings and function control of the computer device, such as a touchscreen, keypad, mouse, trackpad, touchpad, joystick, one or more mouse buttons, trackball, joystick, etc. Output device 40 may include display devices, auxiliary lighting devices (e.g., LEDs), and haptic feedback devices (e.g., vibration motors). The aforementioned display devices include, but are not limited to, liquid crystal displays, light-emitting diodes, displays, and plasma displays. In some alternative embodiments, the display device may be a touchscreen.
[0147] The computer device also includes a communication interface for communicating with other devices or communication networks.
[0148] This disclosure also provides a computer-readable storage medium in which the methods described in this disclosure can be implemented in hardware or firmware, or implemented as recordable on a storage medium, or implemented as computer code originally stored on a remote storage medium or a non-transitory machine-readable storage medium and subsequently stored on a local storage medium after being downloaded over a network. Thus, the methods described herein can be processed by software stored on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. The storage medium may be a magnetic disk, optical disk, read-only memory, random access memory, flash memory, hard disk, or solid-state drive, etc.; further, the storage medium may also include combinations of the above types of memory. It is understood that computers, processors, microprocessor controllers, or programmable hardware include storage components capable of storing or receiving software or computer code that, when accessed and executed by the computer, processor, or hardware, implements the methods shown in the above embodiments.
[0149] Although embodiments of the present disclosure have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the present disclosure, and such modifications and variations all fall within the scope defined by the appended claims.
Claims
1. A code generation method based on a pre-trained model, characterized in that, include: Obtain the target generation location for the code to be generated; Extract the context code information corresponding to the target generation location, and generate code hint information that matches the context code information. The code hint information includes code generation description information, code function description information, and the context code information. The code generation description information is text description information generated when automatic code generation is triggered. The text description information is used to characterize the start of the automatic code generation operation. The code function description information is text information used to describe the specific function that the code to be generated needs to achieve. The step of extracting the context code information corresponding to the target generation location and generating code hint information that matches the context code information includes: Obtain the code text of the upper and lower units corresponding to the target generation position, and extract the code context information corresponding to the target generation position from the code text of the upper and lower units; The execution results of the code text in the upper and lower units are obtained, and the execution context information corresponding to the target generation position is extracted from the execution results. The execution results are used to determine whether there are execution errors in the code text in the upper and lower units. Obtain the variable information corresponding to the execution result, and extract the process context information corresponding to the target generation position from the variable information. The variable information is the variable information corresponding to the execution result of the upper and lower units. The code suggestion information is generated based on the code context information, the execution context information, and the process context information; The code hint information is input into a preset code generation model, and the preset code generation model generates target code corresponding to the target generation location. Check whether the execution result of the target code is correct; When the execution result of the target code is incorrect, extract the execution error information of the target code; The code prompt information is updated based on the execution error information to obtain the updated target prompt information; The target code is regenerated according to the target prompt information using the preset code generation model; wherein, during the process of regenerating the target code according to the target prompt information, the target code that has an execution error is input into the preset code generation model as part of the target prompt information, so that the preset code generation model optimizes the target code that has an execution error according to the target prompt information; The number of errors in the execution result is detected, where the number of errors is the number of consecutive errors made by the preset code generation model when executing target code used to achieve the same specific function; When the number of errors reaches a preset number, in response to the adjustment operation for the target code, the adjusted target code is generated; The preset code generation model is pre-trained based on the prompt information samples.
2. The method according to claim 1, characterized in that, The step of obtaining the target generation location of the code to be generated includes: Detect whether a code generation instruction has been triggered at the current code editing location; If a code generation instruction is triggered at the current code editing location, the current code editing location is determined as the target generation location.
3. The method according to claim 1, characterized in that, The generation of code suggestion information that matches the context code information includes: Obtain a preset information assembly template and determine the filling position of the context code information in the information assembly template; The context code information is filled into the information assembly template according to the filling position, and the context code information is assembled according to the information assembly template to generate the code prompt information.
4. The method according to claim 3, characterized in that, Also includes: Obtain the code generation description information and determine the first assembly position of the code generation description information in the information assembly template; In response to the code function description operation, the code function description information is generated, and the second assembly position of the code function description information in the information assembly template is determined; According to the first assembly position, the second assembly position, and the filling position, the code generation description information, the code function description information, and the context code information are filled into the information assembly template; The code generation description information, the code function description information, and the context code information are assembled according to the information assembly template to generate the code prompt information.
5. The method according to claim 1, characterized in that, After generating the target code corresponding to the target generation location using the preset code generation model, the method further includes: The target code generated by the preset code generation model is filled into the target generation position; And / or, Create a new code generation location and fill the newly created code generation location with the target code generated by the preset code generation model.
6. A code generation device based on a pre-trained model, characterized in that, include: The acquisition module is used to obtain the target generation location of the code to be generated; The prompt information generation module is used to extract the context code information corresponding to the target generation position, and generate code prompt information that matches the context code information. The code prompt information includes code generation description information, code function description information, and the context code information. The code generation description information is text description information generated when automatic code generation is triggered, and the text description information is used to characterize the initiation of the automatic code generation operation. The code function description information is text information used to describe the specific function that the code to be generated needs to achieve. The module also obtains the code text of the units above and below the target generation position, and extracts the code context information corresponding to the target generation position from the code text of the units above and below. The execution results of the code text in the upper and lower units are obtained, and the execution context information corresponding to the target generation position is extracted from the execution results. The execution results are used to determine whether there are execution errors in the code text in the upper and lower units. Obtain variable information corresponding to the execution result, extract process context information corresponding to the target generation position from the variable information, wherein the variable information is the variable information corresponding to the execution result of the preceding and following units; generate the code hint information based on the code context information, the execution context information, and the process context information; The code generation module is used to input the code hint information into a preset code generation model, and generate target code corresponding to the target generation position through the preset code generation model. An execution detection module is used to detect whether the execution result of the target code is correct; An error information extraction module is used to extract execution error information of the target code when the execution result of the target code is incorrect; The update module is used to update the code prompt information based on the execution error information to obtain the updated target prompt information; An optimization module is used to regenerate target code according to the target prompt information using the preset code generation model; wherein, during the process of regenerating target code according to the target prompt information, the target code that executes incorrectly is input into the preset code generation model as part of the target prompt information, so that the preset code generation model optimizes the target code that executes incorrectly according to the target prompt information; The error count acquisition module is used to detect the number of errors in the execution result, wherein the error count is the number of consecutive errors made by the preset code generation model when executing target code used to achieve the same specific function; An adjustment module is used to generate adjusted target code in response to an adjustment operation on the target code when the number of errors reaches a preset number. The preset code generation model is pre-trained based on the prompt information samples.
7. A computer device, characterized in that, include: A memory and a processor are communicatively connected, the memory stores computer instructions, and the processor executes the computer instructions to perform the code generation method based on a pre-trained model as described in any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing the computer to execute the code generation method based on the pre-trained model as described in any one of claims 1 to 5.