Subfunction code generation method and device

By receiving code completion instructions and using the code completion model to determine the target completion code, and completing the completion function, the problem of undefined subfunctions in the output of the code completion model is solved, and the accuracy and operability of code completion are improved.

CN119536693AActive Publication Date: 2025-02-28BEIJING GUIXIN TECHNOLOGY CO LTD
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202411160366.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-22
Publication Date
2025-02-28
Estimated Expiration
2044-08-22

AI Technical Summary

Technical Problem

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.

Method used

By receiving code completion instructions, determine the target completion code of the target subfunction based on the function type, context code and code completion model, and use the target completion code to complete the complete function to ensure that the generated complete code is complete.

Benefits of technology

Ensure that the generated complete code is complete, reduce compilation or operation errors caused by undefined subfunctions, and greatly improve the accuracy of code completion and the code runability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119536693A_ABST
    Figure CN119536693A_ABST
Patent Text Reader

Abstract

The invention provides a sub-function code generation method and device, and the method comprises the steps: receiving a code completion instruction which comprises a function type and a context code of a target sub-function; the target sub-function represents at least one undefined sub-function in the to-be-complemented function; determining a target completion code of the target sub-function according to the function type, the context code and a first code completion model; and complementing the to-be-complemented function through the target complementing code to obtain a first complementing code. According to the embodiment of the invention, by detecting and complementing the undefined sub-functions, the generated complementation codes can be ensured to be complete, compiling or running errors caused by the undefined sub-functions are reduced, and the code complementation accuracy and the code operability are greatly improved.
Need to check novelty before this filing date? Find Prior Art

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 codes 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, a sub-function code generation method is urgently needed 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 the related art that the program fails to run due to the existence of undefined sub-functions in the completion code.

[0006] The first aspect of the present disclosure provides a method for generating code for a sub-function, the method comprising:

[0007] 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;

[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 function to be completed is completed by using the target completion code to obtain a first completion code.

[0010] The disclosed embodiment determines the target completion code of the undefined sub-function according to the function type, context code and the 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 operability 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 can ensure that all sub-functions are correctly defined by checking whether the first completed code obtained through completion is defined, thereby reducing the error of undefined function calls that occurs when the code is running, 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 of 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 the embodiment of the present disclosure, the method further includes: obtaining the first code completion model through training, wherein the model training step of the first code completion model includes:

[0019] Acquire a plurality of first sample codes; each of the first sample codes includes at least one function code fragment, and each function code fragment includes at least one sub-function call;

[0020] For any one of 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 according to 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 there is no undefined sub-function in the second completion code, completing the source code to be completed by 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 by 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 by 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, so as to obtain a plurality of first abstract syntax trees corresponding one to the plurality of third sample codes; any 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;

[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] The disclosed embodiment obtains a second abstract syntax tree by deleting any branch structure in the first abstract syntax tree; and trains an initial model according to multiple second abstract syntax trees corresponding 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 (the structural integrity here refers to the integrity of the overall framework of the code, and does not conflict with the undefined sub-functions), so as to achieve the purpose of 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 the deleted branch structure of the second abstract syntax tree at a target position of the target character sequence;

[0039] According to the target position, a first character sequence and a second character sequence are determined from the target character sequence; the first character sequence represents a character sequence of a preceding portion of the 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;

[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 contexts 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 the embodiment of the present disclosure, after deleting any branch structure in the first abstract syntax tree to obtain the 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] The embodiment of the second aspect of the present disclosure provides a sub-function code generation device, including:

[0046] An instruction receiving module, 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 the first code completion model;

[0048] The code completion module is used to complete the to-be-completed function by 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 above.

[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 be learned through practice of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0052] By reading the detailed description of the preferred embodiment below, various other advantages and benefits will become clear to those of ordinary skill in the art. The accompanying drawings are only used for the purpose of illustrating the preferred embodiment and are not considered to be limitations of the present disclosure. In addition, the same reference symbols are used to represent the same components throughout the accompanying drawings.

[0053] In the attached picture:

[0054] 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;

[0055] Figure 2 A schematic diagram showing the structure of a code generation device for a sub-function provided by an embodiment of the present disclosure is shown;

[0056] Figure 3 A schematic diagram of the structure of an electronic device provided by an embodiment of the present disclosure is shown;

[0057] Figure 4 A schematic diagram of a storage medium provided by an embodiment of the present disclosure is shown. DETAILED DESCRIPTION

[0058] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the 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. On the contrary, these embodiments are provided in order 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.

[0059] It should be noted that, unless otherwise specified, the technical terms 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.

[0060] 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.

[0061] 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:

[0062] Step S101, receiving a code completion instruction.

[0063] Specifically, the code completion instruction includes the function type and context code of the target sub-function, and the target sub-function represents at least one undefined sub-function in the function to be completed. Among them, the function to be completed can be understood as the completion code containing the function to be completed output by the code completion tool (such as the code completion model) in the code completion scenario. The function to be completed generally contains one or more sub-functions, each of which can be understood as a functional module and is written in the form of function calls.

[0064] More specifically, an undefined sub-function can be understood as a function module that exists in the form of the code, but the necessary dependencies are missing inside the function module, which results in the function module being unable to be executed during the code running process. This problem may be caused by the fact that the code completion model is limited by limited computing resources, insufficient memory, etc., which results in the code sequence output by the code completion model not being too long, which in turn results in some functions / function modules in the code not being defined.

[0065] Step S102: determining a target completion code of the target sub-function according to the function type, the context code and the first code completion model.

[0066] Specifically, functions can be classified according to various conditions. For example, functions can be classified according to their purpose into calculation functions, operation functions, acquisition functions, and setting functions; functions can be classified according to their scope into global functions (defined and accessed within the global scope of the code) and local functions (defined within a function or code block and can only be used within the local scope).

[0067] In some specific embodiments, the target completion code of the target sub-function may be determined according to the trained first code completion model based on the function type and function name of the target sub-function and the context code of the target sub-function.

[0068] In some specific embodiments, the above step S102 includes steps S1021 and S1022:

[0069] Step S1021, extracting semantic features of the context code of the target sub-function;

[0070] Step S1022: Call the first code completion model to determine a target completion code for the target sub-function according to the semantic feature and the function type.

[0071] In some specific embodiments, the method further includes: training to obtain the first code completion model;

[0072] The model training steps of the first code completion model include steps a1 to a3:

[0073] Step a1, obtaining a plurality of first sample codes.

[0074] Specifically, each first sample code includes at least one function code fragment, and the functions in the fragment 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.

[0075] 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.

[0076] Specifically, you can select to start from the end position of the previous function and end at the start position of the next function, and delete the code of this part of the function.

[0077] Step a3: training the initial model according to 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.

[0078] Specifically, the function information includes a function type and a function name.

[0079] In some specific embodiments, the preamble code can be understood as the code in 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.

[0080] In some specific embodiments, the post-order code can be understood as the code in the post-order part of the position where the deleted sub-function is located in the source code, and the code corresponding to the preset separator in the post-order part of the target position (i.e., the position where the above-mentioned deleted sub-function is located) in the source code can be used as the post-order code; more specifically, the preset separator can be understood as being selected according to actual needs, for example, selecting the Nth separator located post-order to the target position can be understood as: the code between the preset separator and the target position is used as the post-order code.

[0081] Step S103, completing the function to be completed by using the target completion code to obtain a first completion code.

[0082] Specifically, the target completion code can replace the code of the undefined sub-function in the function to be completed, so as to achieve the purpose of redefining the undefined sub-function in the function to be completed, and then complete the code of the function to be completed to obtain the first completion code.

[0083] In some specific embodiments, after the above step S103, the method further includes:

[0084] Step b1, detecting whether there is an undefined new sub-function in the target sub-function completed by the first completion code.

[0085] 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.

[0086] 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.

[0087] In some specific embodiments, before receiving the code completion instruction, the method further includes:

[0088] Step c1, obtaining the source code to be completed.

[0089] Step c2: input the source code to be completed into the trained second code completion model, and output a second completed code.

[0090] 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.

[0091] Step c3, determining whether there is any undefined sub-function in the second completion code.

[0092] Step c4: If there is no undefined sub-function in the second completion code, the source code to be completed is completed by using the second completion code.

[0093] Step c5: If there is an undefined sub-function in the second completion code, execute the operation of receiving the code completion instruction.

[0094] 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 an executable source code.

[0095] In some specific embodiments, after obtaining the first completion code, the method further includes:

[0096] If there is no undefined sub-function in the first completion code, the source code to be completed is completed by using the first completion code to obtain an executable source code.

[0097] 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:

[0098] Step d1, obtaining a plurality of third sample codes.

[0099] 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 universal applicability, 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, and improve the accuracy of code completion.

[0100] Step d2: for any one of the plurality of third sample codes, convert the third sample code into a corresponding first abstract syntax tree, so as to obtain a plurality of first abstract syntax trees corresponding one-to-one to the plurality of third sample codes.

[0101] 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 multiple characters; then, through a syntax analyzer, the character sequence is converted into a nested syntax structure, namely, an abstract syntax tree (AST), wherein the syntax analyzer constructs the AST according to the syntax rules of the programming language.

[0102] 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.

[0103] 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.

[0104] 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, and 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.

[0105] Through the above steps d1 to d3, the disclosed embodiment 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, wherein 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.

[0106] Step d4: training the 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.

[0107] In some specific embodiments, the above step d4 further includes:

[0108] 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.

[0109] Specifically, the tree structure can be converted into a character sequence using a corresponding preset algorithm according to 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.

[0110] In some specific embodiments, each character sequence includes a plurality of characters, and the characters include at least one of the following: a keyword, an identifier, a constant, a string literal, and a special symbol.

[0111] Step d42: determining the target position of the deleted branch structure of the second abstract syntax tree in the target character sequence.

[0112] Step d43, determining a first character sequence and a second character sequence from the target character sequence according to the target position.

[0113] Specifically, the first character sequence represents a character sequence of a preceding part of a target position of the target character sequence, and the second character sequence represents a character sequence of a succeeding part of a target position of the target character sequence;

[0114] 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.

[0115] 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), BERT (Bidirectional Encoder Representations from Transformers), etc.

[0116] In some specific embodiments, during the model training process, a validation set can be used to evaluate the performance of the model using appropriate evaluation indicators, such as BLEU score, ROUGE score, or a custom evaluation indicator. The model can also 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.

[0117] In some specific embodiments, after the above step d3, the method further includes:

[0118] Step 301, selecting a first abstract syntax tree corresponding to the second abstract syntax tree from the plurality of first abstract syntax trees;

[0119] 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.

[0120] In the above steps 301-302, 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, according to the location of the deleted branch structure in the second abstract syntax tree, the corresponding branch structure in the above first abstract syntax tree is annotated. The present application annotates the branch structure corresponding to the deleted branch structure in the first abstract syntax tree, which helps to evaluate the output result of the code completion model through the annotated branch structure of the first abstract syntax tree after model training, so as to evaluate the completion effect of the code completion model.

[0121] In some specific embodiments, after step d44, the method further comprises:

[0122] 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.

[0123] 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.

[0124] 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.

[0125] 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 a plurality of second abstract syntax trees corresponding one-to-one to the plurality of first abstract syntax trees.

[0126] 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 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.

[0127] 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:

[0128] The present invention also solves a technical problem that the existing code completion model has its own limitations, such as limited computing resources, insufficient memory, etc., which leads to a limitation on the length of the completion code output by the code completion model, resulting in the presence of undefined sub-functions in the completion code output. If an undefined sub-function is called during the code running process, the program may fail to run.

[0129] In order to solve the problem that there may be undefined sub-functions in the above-mentioned completion code, the present invention determines the target completion code of the undefined sub-function through the function type and context code of the undefined sub-function and the trained first code completion model, and then completes the above-mentioned completion code through the target completion code to obtain the completed completion code. After obtaining the completed completion code, it will be checked whether 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 completion code.

[0130] 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 of the embodiments shown in the figure. Figure 2 As shown, the code generation device of the sub-function includes:

[0131] An instruction receiving module, 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;

[0132] 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 the first code completion model;

[0133] The code completion module is used to complete the to-be-completed function by using the target completion code to obtain a first completion code.

[0134] 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 performed again.

[0135] 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.

[0136] 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 preamble code and postamble 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 the function name.

[0137] Optionally, the device also includes: a code completion module, used to obtain 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 are undefined sub-functions in the second completion code; if there are no undefined sub-functions in the second completion code, complete the source code to be completed by using the second completion code; if there are undefined sub-functions in the second completion code, execute the operation of receiving the code completion instruction.

[0138] Optionally, the second code completion model training module is used to: obtain multiple third sample codes; for any one of 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 of 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.

[0139] Optionally, the second code completion model training module is also used to: for any one of the multiple second abstract syntax trees, convert the second abstract syntax tree 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; according to the target position, determine 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 part of the target position of the target character sequence, and the second character sequence represents a character sequence of a post-order 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.

[0140] The sub-function code generation device provided in the above-mentioned embodiment of the present disclosure and the sub-function code generation method provided in 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.

[0141] The disclosed embodiment 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 A code generation method for a sub-function provided by any of the illustrated embodiments.

[0142] The memory 301 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk memory. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 303 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used.

[0143] The bus 302 may be an ISA bus, a PCI bus, or an EISA bus. The bus may be divided into an address bus, a data bus, a control bus, etc. 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 a sub-function disclosed in any of the illustrated embodiments may be applied to the processor 300 or implemented by the processor 300 .

[0144] The processor 300 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 300. The above processor 300 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can 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 gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present disclosure can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor. The steps of the method disclosed in the embodiments of the present disclosure can be directly embodied as a hardware decoding processor to be executed, or the hardware and software modules in the decoding processor can be executed. The software module can be located in a mature storage medium in the field such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, 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.

[0145] 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.

[0146] 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 executed by a processor, the code generation method of the sub-function provided in any of the aforementioned embodiments will be executed.

[0147] 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.

[0148] 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.

[0149] It should be noted that:

[0150] In the description provided herein, a large number of specific details are described. However, it is understood that the 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.

[0151] 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 the exemplary embodiments of the present disclosure, the various features of the present disclosure are sometimes grouped together into a single embodiment, figure, or description thereof. However, this disclosed method should not be interpreted as reflecting that the claimed disclosure requires more features than those explicitly recited in each claim. More specifically, as reflected in the claims below, the inventive aspects lie in less than all the features of the individual embodiments disclosed above. Therefore, the claims that follow the specific embodiment are hereby expressly incorporated into the specific embodiment, with each claim itself serving as a separate embodiment of the present disclosure.

[0152] In addition, those skilled in the art will appreciate that, although some embodiments described herein include certain features included in other embodiments but not other features, the combination of features of different embodiments is meant to be within the scope of this disclosure and form different embodiments. For example, in the claims below, any one of the claimed embodiments may be used in any combination.

[0153] The above is only a preferred specific implementation of the present disclosure, but the protection scope of the present disclosure is not limited thereto. Any changes or substitutions that can be easily thought of by any technician familiar with the technical field within the technical scope disclosed in the present disclosure should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on the protection scope of the claims.

Claims

1. A method for generating code for a sub-function, characterized in that: The method comprises: 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; Determining a target completion code for the target subfunction according to the function type, the context code, and the first code completion model; The function to be completed is completed by using the target completion code to obtain a first completion code.

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 or 2, characterized in that: Also includes: The first code completion model is obtained by training, and the model training steps of the first code completion model include: Acquire a plurality of first sample codes; each of the first sample codes includes at least one function code fragment, and each function code fragment includes at least one sub-function call; For any one of 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; The initial model is trained according to 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.

5. The method according to claim 4, characterized in that 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 there is no undefined sub-function in the second completion code, completing the source code to be completed by using the second completion code; If there is an undefined sub-function in the second completion code, an operation of receiving a code completion instruction is performed.

6. The method according to claim 5, characterized in that 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 by using the first completion code to obtain an executable source code.

7. The method according to claim 5, characterized in that The second code completion model is obtained by training, and the model training steps of the second code completion model include: Obtaining a plurality of third sample codes; For any one of the plurality of third sample codes, convert the third sample code into a corresponding first abstract syntax tree, so as to obtain a plurality of first abstract syntax trees corresponding one to the plurality of third sample codes; any 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; 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; 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.

8. The method according to claim 7, characterized in that 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: 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; Determine the deleted branch structure of the second abstract syntax tree at a target position of the target character sequence; According to the target position, a first character sequence and a second character sequence are determined from the target character sequence; the first character sequence represents a character sequence of a preceding portion of the 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 the character sequence of the deleted branch structure based on the first character sequence and the second character sequence.

9. The method according to claim 7, characterized in that: 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.

10. A code generation device for a sub-function, characterized in that: The device comprises: An instruction receiving module, 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 the first code completion model; The code completion module is used to complete the to-be-completed function by using the target completion code to obtain a first completion code.

Citation Information

Patent Citations

  • Code completion method and device, storage medium and electronic device

    CN110502227A

  • Programming code loop segment completion method and device based on data flow analysis

    CN118034706A

  • Intelligent contract code completion method based on deep neural network and global cache

    CN118092889A

  • Bidirectional intelligent question and answer code supplementing method and system based on large language model

    CN118426825A

  • System and method for automatically detecting a security vulnerability in a source code using a machine learning model

    GB201917161D0