Subfunction code generation method and device
By receiving code completion instructions, using function types and context code, combined with the trained code completion model, we determine and complete undefined subfunctions, and solve the problem of program operation failure caused by undefined subfunctions in the code completion model, and improve the accuracy and operability of code completion.
Patent Information
- Application Number
- CN202411160366.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-22
- Publication Date
- 2025-08-26
- Estimated Expiration
- 2044-08-22
AI Technical Summary
There may be undefined subfunctions in the complete code output from the existing code completion model, resulting in the problem of failure of the program.
By receiving code completion instructions, using function type and context code, combined with the trained code completion model, determine the target completion code of undefined subfunctions and complete it until all subfunctions are defined, ensuring that the generated complete code is complete.
Improves the accuracy and runability of code completion, and reduces compilation or run errors caused by undefined subfunctions.
Smart Images

Figure CN119536693B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the technical field of large language models, and in particular to a method and device for generating code for a sub-function. Background Art
[0002] With the development of large language model technology, more and more people are beginning to use large models to solve code completion tasks in software development scenarios.
[0003] The completed code output by the existing code completion model may contain undefined sub-functions. If an undefined sub-function is called during code execution, the program may fail to run.
[0004] Therefore, there is an urgent need for a sub-function code generation method to solve the above technical problems. Summary of the Invention
[0005] In view of this, the present disclosure proposes a method and device for generating sub-function codes to solve the problem in related technologies that program execution fails due to the presence of undefined sub-functions in the completed code.
[0006] The first embodiment of the present disclosure provides a method for generating code for a sub-function, the method comprising:
[0007] receiving a code completion instruction, the code completion instruction including a function type and a context code of a target sub-function; the target sub-function represents at least one undefined sub-function in a function to be completed;
[0008] Determining a target completion code for the target subfunction according to the function type, the context code, and the first code completion model;
[0009] The target completion code is used to complete the function to be completed, and a first completion code is obtained.
[0010] The disclosed embodiment determines the target completion code of the undefined sub-function according to the function type, context code and first code completion model of the undefined sub-function, and uses the target completion code to complete the function to be completed to obtain the first completion code. This ensures that the generated completion code is complete, reduces compilation or runtime errors caused by undefined sub-functions, and greatly improves the accuracy of code completion and the runnability of the code.
[0011] In the embodiment of the present disclosure, after obtaining the first completion code, the method further includes:
[0012] Detecting whether there is an undefined new sub-function in the target sub-function completed by the first completion code;
[0013] If the undefined new sub-function exists, the undefined new sub-function is determined as a new target sub-function, and the operation of receiving the code completion instruction is performed again.
[0014] The disclosed embodiment checks whether the first completed code obtained through completion is defined, thereby ensuring that all sub-functions are correctly defined, reducing errors of undefined function calls during code execution, and thereby greatly improving the accuracy of code completion and the operability of the code.
[0015] In an embodiment of the present disclosure, determining a target completion code for the target sub-function according to the function type, the context code, and the first code completion model includes:
[0016] Extracting semantic features of the context code of the target sub-function;
[0017] The first code completion model is called to determine a target completion code for the target sub-function according to the semantic feature and the function type.
[0018] In an embodiment of the present disclosure, the method further includes: training to obtain the first code completion model, wherein the model training step of the first code completion model includes:
[0019] Acquire a plurality of first sample codes; each first sample code includes at least one function code fragment, and each function code fragment includes at least one sub-function call;
[0020] For any one first sample code among the plurality of first sample codes, delete the definition of any sub-function called in any function in the first sample code to obtain a second sample code;
[0021] The initial model is trained based on the function information of the deleted sub-function in the second sample code, and the preamble code and postamble code corresponding to the deleted sub-function to obtain the first code completion model; the function information includes a function type and a function name.
[0022] In an embodiment of the present disclosure, before receiving the code completion instruction, the method further includes:
[0023] Get the source code to be completed;
[0024] Inputting the source code to be completed into the trained second code completion model, and outputting the second completed code;
[0025] Determine whether there is an undefined sub-function in the second completion code;
[0026] If no undefined sub-function exists in the second completion code, completing the source code to be completed using the second completion code;
[0027] If there is an undefined sub-function in the second completion code, an operation of receiving a code completion instruction is performed.
[0028] In the embodiment of the present disclosure, after obtaining the first completion code, the method further includes:
[0029] If there is no undefined sub-function in the first completion code, the source code to be completed is completed using the first completion code to obtain an executable source code.
[0030] In an embodiment of the present disclosure, the second code completion model is obtained through training, and the model training steps of the second code completion model include:
[0031] Obtaining a plurality of third sample codes;
[0032] For any one of the plurality of third sample codes, convert the third sample code into a corresponding first abstract syntax tree, thereby obtaining a plurality of first abstract syntax trees corresponding one-to-one to the plurality of third sample codes; wherein any first abstract syntax tree includes a plurality of branch structures, each branch structure including any one node in the first abstract syntax tree except a root node, and all child nodes of the any one node;
[0033] For any one of the plurality of first abstract syntax trees, delete any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree;
[0034] The second initial model is trained according to a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees to obtain the second code completion model.
[0035] In the embodiment of the present disclosure, a second abstract syntax tree is obtained by deleting any branch structure in the first abstract syntax tree; an initial model is trained based on multiple second abstract syntax trees that correspond one-to-one to the multiple first abstract syntax trees to obtain a second code completion model. The code completion model obtained by this model training method can ensure the structural integrity of the code completion (here, structural integrity refers to the integrity of the overall framework of the code and does not conflict with undefined sub-functions), thereby achieving the purpose of ensuring the normal operation of the completed source code.
[0036] In an embodiment of the present disclosure, training a second initial model according to a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees to obtain the second code completion model includes:
[0037] For any second abstract syntax tree among the plurality of second abstract syntax trees, convert the second abstract syntax tree into a corresponding target character sequence;
[0038] Determine a target position of the deleted branch structure of the second abstract syntax tree in the target character sequence;
[0039] Determining a first character sequence and a second character sequence from the target character sequence according to the target position; the first character sequence represents a character sequence preceding the target position of the target character sequence, and the second character sequence represents a character sequence following the target position of the target character sequence;
[0040] The initial model is trained to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence.
[0041] The disclosed embodiment determines the first character sequence and the second character sequence from the target character sequence through the target position, and trains the initial model to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence. This enables the model to consider the preceding and subsequent context of the code to better understand the logical flow and structure of the code, thereby optimizing the model performance and achieving the purpose of improving the prediction accuracy of the completion structure.
[0042] In an embodiment of the present disclosure, after deleting any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree, the method further includes:
[0043] Filtering out a first abstract syntax tree corresponding to the second abstract syntax tree from the plurality of first abstract syntax trees;
[0044] A branch structure in the first abstract syntax tree corresponding to the deleted branch structure in the second abstract syntax tree is marked.
[0045] An embodiment of the second aspect of the present disclosure provides a sub-function code generation device, including:
[0046] An instruction receiving module is configured to receive a code completion instruction, wherein the code completion instruction includes a function type and a context code of a target sub-function; the target sub-function represents at least one undefined sub-function in a function to be completed;
[0047] a target completion code determination module, configured to determine a target completion code for the target sub-function according to the function type, the context code, and a first code completion model;
[0048] The code completion module is used to complete the to-be-completed function using the target completion code to obtain a first completion code.
[0049] An embodiment of the third aspect of the present disclosure provides an electronic device, which includes a memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the code generation method of the sub-function described in the first aspect above by executing the computer instructions.
[0050] An embodiment of the fourth aspect of the present disclosure provides a computer-readable storage medium, on which computer instructions are stored, and the computer instructions are used to enable a computer to execute the code generation method of the sub-function described in the first aspect.
[0051] Additional aspects and advantages of the present disclosure will be given in part in the following description and in part will become apparent from the following description or learned through practice of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] Various other advantages and benefits will become apparent to those skilled in the art upon reading the detailed description of the preferred embodiment below. The accompanying drawings are for illustration purposes only and are not to be considered as limiting the present disclosure. The same reference symbols are used throughout the drawings to represent the same components. In the drawings:
[0053] Figure 1 A schematic flow chart of a method for generating code for a sub-function provided by an embodiment of the present disclosure is shown;
[0054] Figure 2 A schematic structural diagram of a code generation device for a sub-function provided by an embodiment of the present disclosure is shown;
[0055] Figure 3 A schematic structural diagram of an electronic device provided by an embodiment of the present disclosure is shown;
[0056] Figure 4 A schematic diagram of a storage medium provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION
[0057] Exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments described herein. Rather, these embodiments are provided to enable a more thorough understanding of the present disclosure and to fully convey the scope of the present disclosure to those skilled in the art.
[0058] It should be noted that, unless otherwise specified, the technical or scientific terms used in the present disclosure should have the common meanings understood by those skilled in the art to which the present disclosure belongs.
[0059] According to an embodiment of the present disclosure, an embodiment of a code generation method for a sub-function is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0060] In this embodiment, a method for generating code for a sub-function is provided. Figure 1 is a flowchart of a method for generating code for a sub-function according to an embodiment of the present disclosure, such as Figure 1 As shown, the process includes the following steps:
[0061] Step S101: receiving a code completion instruction.
[0062] Specifically, a code completion instruction includes the function type and context code of the target subfunction. The target subfunction represents at least one undefined subfunction within the function to be completed. The function to be completed can be understood as the completed code output by a code completion tool (e.g., a code completion model) in a code completion scenario, including the function to be completed. The function to be completed typically contains one or more subfunctions, each of which can be understood as a functional module, written through function calls.
[0063] More specifically, an undefined sub-function can be understood as a function module that formally exists in the code, but lacks necessary dependencies, preventing the module from executing during code execution. This issue may occur because the code completion model is constrained by limited computing resources and insufficient memory, which limits the length of the code sequence it outputs. This can lead to some functions / function modules in the code being left undefined.
[0064] Step S102 : determining a target completion code for the target sub-function according to the function type, the context code, and a first code completion model.
[0065] Specifically, functions can be classified according to various conditions. For example, according to their purpose, functions can be divided into calculation functions, operation functions, acquisition functions, and setting functions; according to their scope, functions can be divided into global functions (that is, defined and accessed in the global scope of the code) and local functions (defined inside a function or code block and can only be used in the local scope).
[0066] In some specific embodiments, the target completion code of the target subfunction can be determined based on the trained first code completion model, the function type and function name of the target subfunction, and the context code of the target subfunction.
[0067] In some specific embodiments, the above step S102 includes steps S1021 and S1022:
[0068] Step S1021, extracting semantic features of the context code of the target sub-function;
[0069] Step S1022: Call the first code completion model to determine a target completion code for the target sub-function according to the semantic features and the function type.
[0070] In some specific embodiments, the method further includes: training to obtain the first code completion model;
[0071] The model training steps of the first code completion model include steps a1 to a3:
[0072] Step a1: Acquire multiple first sample codes.
[0073] Specifically, each first sample code includes at least one function code segment, and the functions in the segment can be understood as a function module; each function can call at least one sub-function, and the sub-function can be understood as a sub-function unit in the function module.
[0074] Step a2: for any first sample code among the plurality of first sample codes, delete the definition of any sub-function called in any function in the first sample code to obtain a second sample code.
[0075] Specifically, you can select the code starting from the end position of the previous function and ending at the start position of the next function, and delete the code of this part of the function.
[0076] Step a3: training the initial model according to the function information of the deleted sub-function in the second sample code, and the pre-order code and post-order code corresponding to the deleted sub-function, to obtain the first code completion model.
[0077] Specifically, the function information includes a function type and a function name.
[0078] In some specific embodiments, the preamble code can be understood as the code of the preamble part of the position where the deleted subfunction is located in the source code, and the code corresponding to the preset start symbol in the preamble part of the target position (i.e., the position where the above-mentioned deleted subfunction is located) in the source code can be used as the preamble code; more specifically, the preset start symbol can be understood as the start symbol that is closest to the target position and is located in front of the target position, and the first character sequence can be understood as the code between the preset start symbol and the target position.
[0079] In some specific embodiments, the post-sequence code can be understood as the code in the post-sequence part of the position of the deleted sub-function in the source code, and the code corresponding to the preset separator in the post-sequence part of the target position (i.e., the position of the deleted sub-function) in the source code can be used as the post-sequence code; more specifically, the preset separator can be understood as being selected according to actual needs, for example, selecting the Nth separator located post-sequence to the target position can be understood as: the code between the preset separator and the target position is used as the post-sequence code.
[0080] Step S103: Completing the function to be completed by using the target completion code to obtain a first completion code.
[0081] Specifically, the target completion code can replace the code of the undefined sub-function in the function to be completed, thereby achieving the purpose of redefining the undefined sub-function in the function to be completed, and then completing the code of the function to be completed to obtain the first completion code.
[0082] In some specific embodiments, after step S103, the method further includes:
[0083] Step b1: Detect whether there is any undefined new sub-function in the target sub-function completed by the first completion code.
[0084] Step b2: If the undefined new sub-function exists, determine the undefined new sub-function as a new target sub-function, and perform the operation of receiving the code completion instruction again.
[0085] In the above steps b1-b2, when it is detected that there is an undefined new sub-function in the first completed code, the above steps S101-S103 are continued to be executed until there is no undefined sub-function in the completed code, and the completed code can be used as executable source code.
[0086] In some specific embodiments, before receiving the code completion instruction, the method further includes:
[0087] Step c1: Obtain the source code to be completed.
[0088] Step c2: input the source code to be completed into the trained second code completion model, and output the second completed code.
[0089] Specifically, the model training steps of the second code completion model are described in detail in the following embodiments and will not be elaborated here.
[0090] Step c3: determine whether there is any undefined sub-function in the second completion code.
[0091] Step c4: If there is no undefined sub-function in the second completion code, the source code to be completed is completed using the second completion code.
[0092] Step c5: If there is an undefined sub-function in the second completion code, perform an operation of receiving a code completion instruction.
[0093] In the above steps c3 to c5, the second completion code can be understood as the function to be completed in the above step S101. When there is an undefined sub-function in the second completion code, the target completion code of the sub-function will be determined according to the function type of the sub-function, the context code of the sub-function, and the first code completion model; the code corresponding to the undefined sub-function in the second completion code is replaced by the target completion code to obtain the completed second completion code, and then the source code to be completed is completed by the completed second completion code to obtain executable source code.
[0094] In some specific embodiments, after obtaining the first completion code, the method further includes:
[0095] If there is no undefined sub-function in the first completion code, the source code to be completed is completed using the first completion code to obtain an executable source code.
[0096] In some specific embodiments, the method further includes: training to obtain the second code completion model, and the model training step of the second code completion model includes:
[0097] Step d1: Acquire multiple third sample codes.
[0098] Specifically, the multiple third sample codes obtained are not specifically limited here, and the corresponding third sample codes can be selected according to actual needs. For example: the multiple third sample codes obtained can come from different technical fields or application scenarios, respectively, so that the second code completion model finally trained has universality, that is, the second code completion model can perform code completion operations on source code to be completed in different technical fields or application scenarios. For another example: the multiple third sample codes obtained can belong to the same technical field or application scenario, so that the code completion model finally trained has domain specificity, that is, the code completion model can better understand and predict code patterns and structures in a specific technical field, thereby improving the accuracy of code completion.
[0099] Step d2: for any one of the plurality of third sample codes, convert the third sample code into a corresponding first abstract syntax tree, to obtain a plurality of first abstract syntax trees corresponding one-to-one to the plurality of third sample codes.
[0100] In some specific embodiments, in the process of converting the third sample code into the corresponding first abstract syntax tree, the third sample code can be first decomposed into multiple characters, such as keywords, identifiers, operators, etc., to form a character sequence through the multiple characters; then, the character sequence is converted into a nested syntax structure, namely, an abstract syntax tree (AST), through a syntax analyzer, wherein the syntax analyzer constructs the AST according to the syntax rules of the programming language.
[0101] In some specific embodiments, any first abstract syntax tree includes multiple branch structures, each branch structure includes any node except the root node in the first abstract syntax tree, and all child nodes of the any node.
[0102] Step d3: For any first abstract syntax tree among the multiple first abstract syntax trees, delete any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree.
[0103] Specifically, the first abstract syntax tree refers to the abstract syntax tree corresponding to the source code whose code is complete and does not require code completion. The second syntax tree refers to the abstract syntax tree corresponding to the source code whose code is incomplete and requires code completion, and the abstract syntax tree lacks any branch structure.
[0104] Through the above steps d1 to d3, the embodiment of the present disclosure can obtain multiple first abstract syntax trees and multiple second abstract syntax trees, which are in a one-to-one correspondence. For example, the first abstract syntax tree 1 corresponds to the second abstract syntax tree 1, the first abstract syntax tree 2 corresponds to the second abstract syntax tree 2, and the first abstract syntax tree 3 corresponds to the second abstract syntax tree 3. Among them, there is a marked branch structure in each second abstract syntax tree, and the deleted branch structure in the first abstract syntax tree corresponding to the above second abstract syntax tree corresponds to the marked branch structure. For example, the first abstract syntax tree 1 corresponds to the second abstract syntax tree 1, and the deleted branch structure N in the first abstract syntax tree 1 corresponds to the marked branch structure N in the second abstract syntax tree 1.
[0105] Step d4: training the second initial model according to the plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees to obtain the second code completion model.
[0106] In some specific embodiments, the above step d4 further includes:
[0107] Step d41 : For any second abstract syntax tree among the plurality of second abstract syntax trees, convert the second abstract syntax tree into a corresponding target character sequence.
[0108] Specifically, the tree structure can be converted into a character sequence using a corresponding preset algorithm based on the structure of the syntax tree. For example, if the syntax tree represents an expression, the conversion algorithm will generate a corresponding mathematical expression string.
[0109] In some specific embodiments, each character sequence includes multiple characters, and the characters include at least one of the following: keywords, identifiers, constants, string literals, and special symbols.
[0110] Step d42: Determine the target position of the deleted branch structure of the second abstract syntax tree in the target character sequence.
[0111] Step d43: determining a first character sequence and a second character sequence from the target character sequence according to the target position.
[0112] Specifically, the first character sequence represents a character sequence of a preceding portion of a target position of the target character sequence, and the second character sequence represents a character sequence of a succeeding portion of the target position of the target character sequence;
[0113] Step d44: training the initial model to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence.
[0114] Specifically, the initial model can select a large language model suitable for sequence-to-sequence tasks, such as a Transformer-based model such as GPT (Generative Pre-trained Transformer) and BERT (Bidirectional Encoder Representations from Transformers).
[0115] In some specific embodiments, during the model training process, a validation set can be used to evaluate the model's performance using appropriate evaluation metrics, such as BLEU score, ROUGE score, or custom evaluation metrics. The model can then be fine-tuned based on the evaluation results, which may include adjusting the network structure, optimizing the training process, or using more data for training.
[0116] In some specific embodiments, after step d3, the method further includes:
[0117] Step 301: Filter out a first abstract syntax tree corresponding to the second abstract syntax tree from the multiple first abstract syntax trees;
[0118] Step 302: Mark the branch structure in the first abstract syntax tree that corresponds to the deleted branch structure in the second abstract syntax tree.
[0119] In steps 301 and 302 above, for any second abstract syntax tree, it is necessary to screen out a first abstract syntax tree corresponding to the second abstract syntax tree from multiple first abstract syntax trees; then, based on the location of the deleted branch structure in the second abstract syntax tree, the corresponding branch structure in the first abstract syntax tree is annotated. By annotating the branch structure corresponding to the deleted branch structure in the first abstract syntax tree, the present application facilitates evaluating the output results of the code completion model after model training through the annotated branch structure of the first abstract syntax tree, so as to evaluate the completion effect of the code completion model.
[0120] In some specific embodiments, after step d44, the method further includes:
[0121] Step S501: Input a first character sequence and a second character sequence corresponding to any second abstract syntax tree into the initial model to obtain a character sequence of a completed branch structure output by the initial model.
[0122] Step S502: If the character sequence of the completion branch structure is the same as the character sequence of the marked branch structure in the target first abstract syntax tree, the initial model is determined as the code completion model.
[0123] Specifically, the target first abstract syntax tree represents a first abstract syntax tree among the multiple first abstract syntax trees corresponding to any one of the second abstract syntax trees.
[0124] Step S503: If the character sequence of the completed branch structure is different from the character sequence of the marked branch structure in the target first abstract syntax tree, the initial model is optimized according to multiple second abstract syntax trees corresponding one-to-one to the multiple first abstract syntax trees.
[0125] The disclosed embodiment can effectively evaluate the code completion effect of the model by comparing the character sequence of the completed branch structure output by the initial model with the character sequence of the annotated branch structure in the target first abstract syntax tree, so as to ensure the output accuracy of the completed code; and, under the condition that the character sequence of the completed branch structure output by the model is different from the character sequence of the annotated branch structure in the target first abstract syntax tree, the model parameters are adjusted through an optimization process so that the model outputs the completed code that meets the expected requirements.
[0126] Corresponding to the implementation of the code generation method of the above sub-function, the embodiment of the present disclosure also provides a specific embodiment for illustration:
[0127] The present invention also solves the technical problem that the existing code completion model has its own limitations, such as limited computing resources, insufficient memory, etc., which leads to a limit on the length of the completed code output by the code completion model, resulting in the possibility of undefined sub-functions in the completed code output. If an undefined sub-function is called during the code execution process, the program may fail to run.
[0128] To address the issue of undefined sub-functions in the above-mentioned completion code, the present invention uses the function type and context code of the undefined sub-function, as well as a trained first code completion model, to determine the target completion code for the undefined sub-function, and then uses the target completion code to complete the above-mentioned completion code to obtain the completed completion code. After obtaining the completed completion code, it will be checked to see if the sub-function is defined. If there is an undefined sub-function in the completed completion code, the trained first code completion model will continue to output the target completion code corresponding to the undefined sub-function until there is no undefined sub-function in the final completed code.
[0129] Corresponding to the implementation of the above sub-function code generation method, the embodiment of the present disclosure also provides a sub-function code generation device for executing the above Figure 1 The code generation method of the sub-function described in any embodiment of the present invention is shown. Figure 2 As shown, the code generation device of the sub-function includes:
[0130] An instruction receiving module is configured to receive a code completion instruction, wherein the code completion instruction includes a function type and a context code of a target sub-function; the target sub-function represents at least one undefined sub-function in a function to be completed;
[0131] a target completion code determination module, configured to determine a target completion code for the target sub-function according to the function type, the context code, and a first code completion model;
[0132] The code completion module is used to complete the to-be-completed function using the target completion code to obtain a first completion code.
[0133] Optionally, the device also includes: a sub-function detection module, used to detect whether there is an undefined new sub-function in the target sub-function completed by the first completion code; if the undefined new sub-function exists, the undefined new sub-function is determined as a new target sub-function, and the operation of receiving the code completion instruction is executed again.
[0134] Optionally, the target completion code determination module is further used to: extract semantic features of the context code of the target sub-function; and call the first code completion model to determine the target completion code of the target sub-function according to the semantic features and the function type.
[0135] Optionally, the device also includes: a first code completion model training module, used to obtain multiple first sample codes; each first sample code includes at least one function code fragment, and each function code fragment includes at least one sub-function call; for any first sample code among the multiple first sample codes, the definition of any sub-function called in any function in the first sample code is deleted to obtain a second sample code; according to the function information of the deleted sub-function in the second sample code, and the pre-order code and post-order code corresponding to the deleted sub-function, the initial model is trained to obtain the first code completion model; the function information includes the function type and function name.
[0136] Optionally, the device also includes: a code completion module, used to obtain the source code to be completed; input the source code to be completed into a trained second code completion model, and output a second completion code; determine whether there is an undefined sub-function in the second completion code; if there is no undefined sub-function in the second completion code, complete the source code to be completed using the second completion code; if there is an undefined sub-function in the second completion code, execute the operation of receiving the code completion instruction.
[0137] Optionally, the second code completion model training module is used to: obtain multiple third sample codes; for any one third sample code among the multiple third sample codes, convert the third sample code into a corresponding first abstract syntax tree to obtain multiple first abstract syntax trees corresponding one-to-one to the multiple third sample codes; any first abstract syntax tree includes multiple branch structures, each branch structure includes any one node in the first abstract syntax tree except the root node, and all child nodes of the any one node; for any one first abstract syntax tree among the multiple first abstract syntax trees, delete any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree; train the second initial model according to the multiple second abstract syntax trees corresponding one-to-one to the multiple first abstract syntax trees to obtain the second code completion model.
[0138] Optionally, the second code completion model training module is also used to: convert any one of the multiple second abstract syntax trees into a corresponding target character sequence; determine the target position of the deleted branch structure of the second abstract syntax tree in the target character sequence; determine a first character sequence and a second character sequence from the target character sequence based on the target position; the first character sequence represents a character sequence of the preceding part of the target position of the target character sequence, and the second character sequence represents a character sequence of the following part of the target position of the target character sequence; train the initial model to predict the character sequence of the deleted branch structure based on the first character sequence and the second character sequence.
[0139] The sub-function code generation device provided by the above-mentioned embodiment of the present disclosure and the sub-function code generation method provided by the embodiment of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by the application programs stored therein.
[0140] The present disclosure also provides an electronic device to execute the code generation method of the above sub-function. Figure 3 , which shows a schematic diagram of an electronic device provided by some embodiments of the present disclosure. Figure 3 As shown, the electronic device 3 includes: a processor 300, a memory 301, a bus 302 and a communication interface 303, wherein the processor 300, the communication interface 303 and the memory 301 are connected via the bus 302; the memory 301 stores a computer program that can be run on the processor 300, and the processor 300 executes the aforementioned Figure 1 The code generation method of the sub-function provided in any of the illustrated embodiments.
[0141] Memory 301 may include high-speed random access memory (RAM) and may also include non-volatile memory, such as at least one disk storage device. Communication between the system network element and at least one other network element is achieved through at least one communication interface 303 (which may be wired or wireless), and may utilize the Internet, a wide area network, a local area network, a metropolitan area network, or the like.
[0142] The bus 302 can be an ISA bus, a PCI bus or an EISA bus. The bus can be divided into an address bus, a data bus, a control bus, etc. Among them, the memory 301 is used to store programs. After receiving the execution instruction, the processor 300 executes the program. Figure 1 The code generation method for the sub-function disclosed in any of the illustrated embodiments may be applied to the processor 300 or implemented by the processor 300 .
[0143] The processor 300 may be an integrated circuit chip with signal processing capabilities. During implementation, each step of the above method can be completed by hardware integrated logic circuits in the processor 300 or by software instructions. The processor 300 may be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it may also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the various methods, steps, and logic block diagrams disclosed in the embodiments of the present disclosure. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the method disclosed in conjunction with the embodiments of the present disclosure may be directly implemented and executed by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. The software module may be located in a storage medium mature in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, etc. The storage medium is located in the memory 301 , and the processor 300 reads the information in the memory 301 and completes the steps of the above method in combination with its hardware.
[0144] The electronic device provided by the embodiment of the present disclosure and the code generation method of the sub-function provided by the embodiment of the present disclosure are based on the same inventive concept and have the same beneficial effects as the methods adopted, run or implemented by them.
[0145] The present disclosure also provides a computer-readable storage medium corresponding to the code generation method of the sub-function provided in the above embodiment. Figure 4 The computer-readable storage medium shown is a CD 30 on which a computer program (ie, a program product) is stored. When the computer program is run by a processor, the code generation method of the sub-function provided by any of the aforementioned embodiments is executed.
[0146] It should be noted that examples of the computer-readable storage medium may also include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other optical or magnetic storage media, which are not listed here one by one.
[0147] The computer-readable storage medium provided by the above-mentioned embodiment of the present disclosure and the sub-function code generation method provided by the embodiment of the present disclosure are based on the same inventive concept and have the same beneficial effects as the method adopted, run or implemented by the application program stored therein.
[0148] It should be noted that:
[0149] In the description provided herein, numerous specific details are described. However, it is understood that embodiments of the present disclosure can be practiced without these specific details. In some instances, well-known structures and technologies are not shown in detail so as not to obscure the understanding of this description.
[0150] Similarly, it should be understood that in order to streamline the present disclosure and aid in understanding one or more of the various inventive aspects, in the above description of exemplary embodiments of the present disclosure, various features of the present disclosure are sometimes grouped together into a single embodiment, figure, or description thereof. However, this disclosed approach should not be interpreted as reflecting that the claimed disclosure requires more features than are expressly recited in each claim. Rather, as reflected in the claims below, inventive aspects lie in less than all the features of the individual embodiments disclosed above. Accordingly, the claims that follow the detailed description are hereby expressly incorporated into this detailed description, with each claim standing on its own as a separate embodiment of the present disclosure.
[0151] Furthermore, those skilled in the art will appreciate that although some embodiments described herein include certain features included in other embodiments but not other features, combinations of features from different embodiments are intended to be within the scope of this disclosure and to form different embodiments. For example, in the claims below, any of the claimed embodiments may be used in any combination.
[0152] The above description is merely a preferred embodiment of the present disclosure, but the scope of protection of the present disclosure is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this disclosure should be included in the scope of protection of the present disclosure. Therefore, the scope of protection of the present disclosure should be based on the scope of protection of the claims.
Claims
1. A method for generating code for a sub-function, characterized in that: The method comprises: receiving a code completion instruction, the code completion instruction including a function type and a context code of a target sub-function; the target sub-function represents at least one undefined sub-function in a function to be completed; Determining a target completion code for the target subfunction according to the function type, the context code, and the first code completion model; Completing the to-be-completed function using the target completion code to obtain a first completion code; The model training step of the first code completion model includes: Acquire a plurality of first sample codes; each first sample code includes at least one function code fragment, and each function code fragment includes at least one sub-function call; For any one first sample code among the plurality of first sample codes, delete the definition of any sub-function called in any function in the first sample code to obtain a second sample code; Training the initial model based on function information of the deleted sub-function in the second sample code, and the pre-order code and post-order code corresponding to the deleted sub-function, to obtain the first code completion model; the function information includes a function type and a function name; Before receiving the code completion instruction, the method further includes: Get the source code to be completed; Inputting the source code to be completed into the trained second code completion model, and outputting the second completed code; Determine whether there is an undefined sub-function in the second completion code; If no undefined sub-function exists in the second completion code, completing the source code to be completed using the second completion code; If there is an undefined sub-function in the second completion code, executing an operation of receiving a code completion instruction; The model training step of the second code completion model includes: Obtaining a plurality of third sample codes; Converting the plurality of third sample codes into corresponding first abstract syntax trees; the first abstract syntax tree includes a plurality of branch structures, each branch structure includes any one node except a root node in the first abstract syntax tree, and all child nodes of the any one node; Deleting any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree; Converting the second abstract syntax tree into a corresponding target character sequence; Determining a first character sequence and a second character sequence from the target character sequence; the first character sequence represents a character sequence of a preceding portion of a target position of the target character sequence, and the second character sequence represents a character sequence of a succeeding portion of the target position of the target character sequence; The initial model is trained to predict a character sequence of a deleted branch structure based on the first character sequence and the second character sequence.
2. The method according to claim 1, characterized in that After obtaining the first completion code, the method further includes: Detecting whether there is an undefined new sub-function in the target sub-function completed by the first completion code; If the undefined new sub-function exists, the undefined new sub-function is determined as a new target sub-function, and the operation of receiving the code completion instruction is performed again.
3. The method according to claim 1 or 2, characterized in that Determining a target completion code for the target sub-function according to the function type, the context code, and the first code completion model includes: Extracting semantic features of the context code of the target sub-function; The first code completion model is called to determine a target completion code for the target sub-function according to the semantic feature and the function type.
4. The method according to claim 1, wherein After obtaining the first completion code, the method further includes: If there is no undefined sub-function in the first completion code, the source code to be completed is completed using the first completion code to obtain an executable source code.
5. The method according to claim 1, wherein After deleting any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree, the method further includes: Filtering out a first abstract syntax tree corresponding to the second abstract syntax tree from the plurality of first abstract syntax trees; A branch structure in the first abstract syntax tree corresponding to the deleted branch structure in the second abstract syntax tree is marked.
6. A code generation device for a sub-function, characterized in that: The device comprises: An instruction receiving module is configured to receive a code completion instruction, wherein the code completion instruction includes a function type and a context code of a target sub-function; the target sub-function represents at least one undefined sub-function in a function to be completed; a target completion code determination module, configured to determine a target completion code for the target sub-function according to the function type, the context code, and a first code completion model; A code completion module, configured to complete the to-be-completed function using the target completion code to obtain a first completion code; The model training step of the first code completion model includes: Acquire a plurality of first sample codes; each first sample code includes at least one function code fragment, and each function code fragment includes at least one sub-function call; For any one first sample code among the plurality of first sample codes, delete the definition of any sub-function called in any function in the first sample code to obtain a second sample code; Training the initial model based on function information of the deleted sub-function in the second sample code, and the pre-order code and post-order code corresponding to the deleted sub-function, to obtain the first code completion model; the function information includes a function type and a function name; Before receiving the code completion instruction, it also includes: Get the source code to be completed; Inputting the source code to be completed into the trained second code completion model, and outputting the second completed code; Determine whether there is an undefined sub-function in the second completion code; If no undefined sub-function exists in the second completion code, completing the source code to be completed using the second completion code; If there is an undefined sub-function in the second completion code, executing an operation of receiving a code completion instruction; The model training step of the second code completion model includes: Obtaining a plurality of third sample codes; Converting the plurality of third sample codes into corresponding first abstract syntax trees; the first abstract syntax tree includes a plurality of branch structures, each branch structure includes any one node except a root node in the first abstract syntax tree, and all child nodes of the any one node; Deleting any branch structure in the first abstract syntax tree to obtain a second abstract syntax tree; Converting the second abstract syntax tree into a corresponding target character sequence; Determining a first character sequence and a second character sequence from the target character sequence; the first character sequence represents a character sequence of a preceding portion of a target position of the target character sequence, and the second character sequence represents a character sequence of a succeeding portion of the target position of the target character sequence; The initial model is trained to predict a character sequence of a deleted branch structure based on the first character sequence and the second character sequence.
Citation Information
Patent Citations
Code completion method and device, storage medium and electronic device
CN110502227A
Intelligent contract code completion method based on deep neural network and global cache
CN118092889A