Code editing method and device, equipment, medium and program product
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-28
- Publication Date
- 2026-03-10
AI Technical Summary
The prior art is difficult to effectively solve the problems of the accuracy of developers' intentions during code editing and the accuracy of code editing suggestions, especially in complex development scenarios.
By obtaining the code snippets and prompt word information to be edited, calling the trained machine learning model, determining the editing operation and generating code editing results, thereby achieving efficient and accurate code editing suggestions.
It realizes efficient and accurate code editing suggestions, which can better match the developer's intentions and improve the accuracy and efficiency of code editing.
Smart Images

Figure CN121646757A_ABST
Abstract
Description
Code editing method, device, equipment, medium and program product Technical Field
[0001] Example embodiments of the present disclosure generally relate to the field of computers, and more particularly, to a method, apparatus, electronic device, computer-readable storage medium, and computer program product for code editing. Background Art
[0002] Compared to writing new code, developers often spend more time maintaining and modifying existing code. Given the diversity of code edits and the difficulty of accurately understanding developer intent, designing effective code edit prediction methods has been a challenging research area. With the development of computer technology, generative artificial intelligence technology has been applied to various aspects of our lives. For example, some code development applications (such as integrated development platforms (IDEs)) provide code edit suggestion features to improve the efficiency of developers in completing code refactoring, defect repair, and other tasks.
[0003] Summary of the Invention
[0004] In a first aspect of the present disclosure, a code editing method is provided. The method includes: obtaining a first code snippet to be edited; invoking a trained machine learning model based on prompt word information and the first code snippet to obtain a model output of the machine learning model, wherein the prompt word information is used to guide the machine learning model to determine an editing operation for the code snippet; and generating a code editing result based on the determined editing operation; and determining a second edited code snippet based on the model output obtained from the machine learning model.
[0005] In a second aspect of the present disclosure, a device for code editing is provided. The device includes: a code acquisition module configured to acquire a first code snippet to be edited; a model calling module configured to call a trained machine learning model based on prompt word information and the first code snippet to obtain a model output of the machine learning model, wherein the prompt word information is used to guide the machine learning model to determine an editing operation for the code snippet, and to generate a code editing result based on the determined editing operation; and a code determination module configured to determine an edited second code snippet based on the model output obtained from the machine learning model.
[0006] In a third aspect of the present disclosure, an electronic device is provided. The device includes at least one processing unit; and at least one memory coupled to the at least one processing unit and storing instructions for execution by the at least one processing unit. When executed by the at least one processing unit, the instructions cause the device to perform the method of the first aspect.
[0007] In a fourth aspect of the present disclosure, a computer-readable storage medium is provided, wherein a computer program is stored on the computer-readable storage medium, and the computer program can be executed by a processor to implement the method of the first aspect.
[0008] In a fifth aspect of the present disclosure, a computer program product is provided, which includes computer-executable instructions, which, when executed by a processor, implement the method according to the first aspect of the present disclosure.
[0009] It should be understood that the content described in this summary section is not intended to limit the key features or important features of the embodiments of the present disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. In the accompanying drawings, the same or similar reference numerals represent the same or similar elements, wherein:
[0011] FIG1 shows a schematic diagram of an example environment in which some embodiments of the present disclosure can be implemented;
[0012] FIG2 shows a flowchart of a code editing process according to some embodiments of the present disclosure;
[0013] FIG3 illustrates an example model training process according to some embodiments of the present disclosure;
[0014] FIG4 shows a schematic structural block diagram of an example apparatus for code editing according to some embodiments of the present disclosure; and
[0015] FIG5 illustrates a block diagram of an electronic device capable of implementing one or more embodiments of the present disclosure. DETAILED DESCRIPTION
[0016] The following describes embodiments of the present disclosure in more detail with reference to the accompanying drawings. Although certain 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 construed as limited to the embodiments described herein. Rather, these embodiments are provided to provide a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are for illustrative purposes only and are not intended to limit the scope of protection of the present disclosure.
[0017] It should be noted that the titles of any section / subsection provided herein are not limiting. Various embodiments are described throughout this document, and any type of embodiment may be included under any section / subsection. Furthermore, the embodiments described in any section / subsection may be combined in any manner with any other embodiments described in the same section / subsection and / or in different sections / subsections.
[0018] In the description of the embodiments of the present disclosure, the term "including" and similar terms should be understood as open inclusion, that is, "including but not limited to". The term "based on" should be understood as "based at least in part on". The term "one embodiment" or "the embodiment" should be understood as "at least one embodiment". The term "some embodiments" should be understood as "at least some embodiments". Other explicit and implicit definitions may be included below. The terms "first", "second", etc. may refer to different or the same objects. Other explicit and implicit definitions may be included below.
[0019] The embodiments of the present disclosure may involve user data, data acquisition and / or use, etc. These aspects shall comply with the corresponding laws, regulations and relevant provisions. In the embodiments of the present disclosure, all data collection, acquisition, processing, processing, forwarding, use, etc. are carried out on the premise that the user is aware of and confirms them. Accordingly, when implementing the various embodiments of the present disclosure, the types, scope of use, and usage scenarios of the data or information that may be involved should be informed to the user and the user's authorization should be obtained in an appropriate manner in accordance with the relevant laws and regulations. The specific notification and / or authorization method may vary according to the actual situation and application scenario, and the scope of the present disclosure is not limited in this respect.
[0020] If this specification and the solutions in the examples involve the processing of personal information, such processing will be done only with a legitimate basis (such as with the consent of the subject of personal information or as necessary for the performance of a contract) and only within the prescribed or agreed scope. A user's refusal to process personal information other than that required for basic functions will not affect the user's use of basic functions.
[0021] As used herein, the term "model" can learn the association between corresponding inputs and outputs from training data, so that after training is completed, corresponding outputs can be generated for given inputs. The generation of the model can be based on machine learning technology. Deep learning is a machine learning algorithm that processes inputs and provides corresponding outputs by using multiple layers of processing units. A neural network model is an example of a model based on deep learning. In this article, "model" may also be referred to as "machine learning model", "learning model", "machine learning network" or "learning network", and these terms are used interchangeably in this article.
[0022] A "neural network" is a machine learning network based on deep learning. A neural network is capable of processing inputs and providing corresponding outputs. It typically includes an input layer, an output layer, and one or more hidden layers between the input and output layers. Neural networks used in deep learning applications typically include many hidden layers, thereby increasing the depth of the network. The layers of a neural network are connected in sequence so that the output of the previous layer is provided as input to the next layer, where the input layer receives the input of the neural network and the output of the output layer serves as the final output of the neural network. Each layer of a neural network includes one or more nodes (also called processing nodes or neurons), each of which processes the input from the previous layer.
[0023] Generally speaking, machine learning can be roughly divided into three stages, namely the training stage, the testing stage, and the application stage (also called the inference stage). In the training stage, a given model can be trained using a large amount of training data, and the parameter values are continuously updated iteratively until the model can obtain consistent inferences that meet the expected goals from the training data. Through training, the model can be considered to be able to learn the association between input and output (also called input-to-output mapping) from the training data. The parameter values of the trained model are determined. In the testing stage, the test input is applied to the trained model to test whether the model can provide the correct output, thereby determining the performance of the model. The testing stage can sometimes be integrated into the training stage. In the application or inference stage, the trained model can be used to process the actual model input based on the parameter values obtained through training to determine the corresponding model output.
[0024] In code development environments, code editing suggestion features are expected to better meet developers' intentions and provide more accurate suggestions. With the development of artificial intelligence (AI) technology, it has been proposed that models can be used to assist with code editing within development applications. However, such solutions still struggle to meet these requirements in practice.
[0025] Various example implementations of this solution are described in detail below in conjunction with the accompanying drawings.
[0026] FIG1 shows a schematic diagram of an example environment 100 in which embodiments of the present disclosure can be implemented. As shown in FIG1 , the example environment 100 may include a terminal device 110 .
[0027] In this example environment 100, a terminal device 110 may run an application 120 that supports interface interaction. Application 120 may be any suitable type of application for interface interaction, examples of which may include, but are not limited to, code editing applications, applications integrated with an integrated development environment (IDE), or other suitable applications such as a browser. User 140 may interact with application 120 via terminal device 110 and / or its attached devices.
[0028] In the environment 100 of FIG1 , if the application 120 is active, the terminal device 110 may present an interface 150 for supporting interface interaction through the application 120. For example, a user may perform code development and other related operations via the interface 150.
[0029] In some embodiments, during the code editing process, the machine learning model 140 may be called to generate code editing suggestions. The call to the machine learning model 140 may be performed by the terminal device 110 or by the server device 130. In the case of being performed by the server device 130, the terminal device 110 may send a code editing request to the server device 130, and in response to the request, the server device 130 may call the machine learning model 140. In some embodiments, the server device 130 may also monitor and determine whether to provide code editing suggestions to the terminal device 110.
[0030] In some embodiments, the machine learning model 140 can be constructed based on a language model (LM). The machine learning model used is a content generation model that can generate corresponding outputs based on model inputs. In some embodiments, the machine learning model based on the language model can accept model inputs in textual modalities (e.g., natural language and / or machine language) and / or model inputs in non-textual modalities (e.g., images, voice, video, etc.), and can generate desired outputs based on the model inputs and prompt words. The prompt words here are used to guide the machine learning model to generate user needs that can solve the user needs indicated by the model inputs.
[0031] In some embodiments, the terminal device 110 communicates with the server device 130 to enable the provision of services for the application 120. The terminal device 110 can be any type of mobile terminal, fixed terminal or portable terminal, including a mobile phone, a desktop computer, a laptop computer, a notebook computer, a netbook computer, a tablet computer, a media computer, a multimedia tablet, a handheld computer, a portable game terminal, a VR / AR device, a personal communication system (PCS) device, a personal navigation device, a personal digital assistant (PDA), an audio / video player, a digital camera / camcorder, a positioning device, a television receiver, a radio broadcast receiver, an e-book device, a gaming device or any combination thereof, including accessories and peripherals of these devices or any combination thereof. In some embodiments, the terminal device 110 can also support any type of interface for the user (such as a "wearable" circuit, etc.).
[0032] The server device 130 can be a standalone physical server, a server cluster or distributed system consisting of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content distribution networks, and big data and artificial intelligence platforms. The server device 130 can include, for example, a computing system / server such as a mainframe, an edge computing node, a computing device in a cloud environment, etc. The server device 130 provides backend services for supporting applications 120 in the terminal device 110.
[0033] A communication connection may be established between the server device 130 and the terminal device 110. The communication connection may be established via a wired or wireless method. The communication connection may include, but is not limited to, a Bluetooth connection, a mobile network connection, a Universal Serial Bus (USB) connection, a Wireless Fidelity (WiFi) connection, etc., and the embodiments of the present disclosure are not limited in this respect. In the embodiments of the present disclosure, the server device 130 and the terminal device 110 may implement signaling interaction through the communication connection between the two.
[0034] It should be understood that the structure and function of the various elements in the environment 100 are described for illustrative purposes only and do not imply any limitation on the scope of the present disclosure.
[0035] When using machine learning models to edit code, the way the model is called will affect the accuracy of the code generated by the model. In addition, the training of the machine learning model, especially the construction of training data, will also affect the performance of the machine learning model.
[0036] Currently, for code editing tasks, code commit data is often used as model training data. However, directly using submitted modification data as a training set for code editing does not match the code editing scenarios of real developers. This is because the development process is full of variables and the context is complex. In addition, directly training the model with modified code makes it difficult for the model to learn the extent of the code modification.
[0037] The embodiments of the present disclosure propose an automated code editing technology solution based on chain of thought (COT), which realizes efficient, accurate, and user-intended automated predictive code editing suggestions, and updates the code snippets to be edited, including adding, deleting, and modifying code snippets.
[0038] In the code editing solution proposed in the embodiments of the present disclosure, for a first code snippet to be edited, a trained machine learning model is invoked based on the prompt word information and the first code snippet to obtain a model output from the machine learning model. The prompt word information here is used to guide the machine learning model in determining an editing operation for the code snippet and generating a code editing result based on the determined editing operation. Based on the model output obtained from the machine learning model, a second edited code snippet is determined.
[0039] In this way, when a model is called to assist with code editing, embodiments of the present disclosure can prompt the machine learning model to first determine a specific editing operation, then perform the code editing based on the determined editing operation, and provide the edited code as an editing suggestion. By guiding the model's generation process in this way, the model can be constrained to generate code more accurately.
[0040] Some example embodiments of the present disclosure will be described below with continued reference to the accompanying drawings.
[0041] 2 shows a flow chart of an example process 200 for code editing according to some embodiments of the present disclosure. The process 200 may be implemented at the terminal device 110 or the server device 130.
[0042] In block 210 , the terminal device 110 or the server device 130 obtains a first code segment to be edited.
[0043] Assume that the user performs code development or code editing on the terminal device 110. In some embodiments, the request for code editing suggestions can be manually triggered by the user. For example, the user can select a portion of the code snippet to request automatic editing of the code snippet. For another example, the user can trigger the code snippet being edited (for example, the code block where the cursor is currently located, or several lines of code near the cursor position) by triggering a predetermined control. In some embodiments, the code of the current interface can be monitored, and the code block being edited by the user can be determined as the code snippet to be edited. In this way, code editing suggestions can be monitored in real time and provided to the user.
[0044] At block 220 , the terminal device 110 or the server device 130 invokes the trained machine learning model 140 based on the prompt word information and the first code snippet to obtain a model output of the machine learning model 140. The prompt word information is used to guide the machine learning model 140 in determining an editing operation for the code snippet and generating a code editing result based on the determined editing operation.
[0045] Conventional code edit prediction training schemes are based on code submission data and fine-tuning open-source models. However, directly using the submitted modifications as the model's learning target makes it difficult to distinguish the extent of the code changes required during actual training. Furthermore, code editing scenarios typically use code text as input, and the instructions of thought chains can disrupt the code formatting of the data. During code edit training, if the model is directly trained on edited data, it will have difficulty distinguishing the extent of additions, deletions, and modifications.
[0046] Therefore, in the embodiments of this disclosure, a new code editing thought chain solution is adopted. Without destroying the structure of the code data, the model is asked to first determine what operation to perform on the current code to be edited (add, delete, modify, or keep unchanged), and then generate code based on the result of the determination. This allows the machine learning model to more accurately grasp the extent of code editing and modification.
[0047] In some embodiments, the model output of the machine learning model 140 may include an editing operation determined for the first code snippet from multiple candidate editing operations. In some embodiments, the model output of the machine learning model 140 also includes a second code snippet. The multiple candidate editing operations may include one or more of the following: a code addition operation, a code deletion operation, a code replacement operation, and a non-modification operation. A code addition operation refers to adding new code to a code snippet; a code deletion operation refers to deleting part of the code in a code snippet; and a code replacement operation refers to replacing all or part of the code in a code snippet with other code. A non-modification operation refers to determining that no modification is required to the current code snippet. For a code snippet, one or more editing operations may need to be performed. In some embodiments, the machine learning model 140 may generate the code after each edit, step by step, until the final code snippet is generated. Of course, only example operations in a code editing scenario are listed here. In specific applications, other different operations can be defined at different operation granularities based on actual conditions, as long as these operations are applicable to code editing.
[0048] In some embodiments, the prompt word information may require the machine learning model 140 to first output the editing operation of how to edit the code snippet, and then output the edited code. In some embodiments, an example of the model output of the machine learning model 140 is shown in Table 1:
[0049] Table 1
[0050] In Table 1, "#Next, how to edit the code" is the output requirement for machine learning model 140 in the prompt information, guiding the model to determine the editing operation to be performed on the code. "Replace" instructs machine learning model 140 to determine whether to perform a "code replacement operation" on the first code snippet to be edited. Based on this operation, machine learning model 140 further determines the code snippet obtained by performing the code replacement operation on the first code snippet.
[0051] In some embodiments, when the machine learning model 140 supports streaming output, directing the machine learning model 140 to perform step-by-step operations can also facilitate the execution of post-processing strategies. For example, in some cases, if you want to adjust the machine learning model 140 to not display operations that do not modify the code, or to reduce the display of deletion operations, you can determine whether the machine learning model 140 needs to continue reasoning after receiving part of the machine learning model output.
[0052] Specifically, after providing the first code snippet and prompt word information as model input to the trained machine learning model 140, a first model output of the machine learning model 140 (e.g., a partial streaming output result of the model) can be obtained. If the first model output indicates that the edit operation determined for the first code snippet is a non-modification operation, a stop indication is sent to the machine learning model 140 to instruct the machine learning model 140 to stop processing. In this way, an output indication can be directly generated to indicate that the edited code snippet is the same as the first code snippet without the help of subsequent reasoning of the machine learning model.
[0053] In some embodiments, in addition to the no-modification operation, other operations that require the model to stop processing can be configured according to application needs (for example, a "code deletion operation" can be configured).
[0054] At block 230, the terminal device 110 or the server device 130 determines the edited second code snippet based on the model output obtained from the machine learning model 140. In some embodiments, the model output of the machine learning model 140 includes the second code snippet. In some embodiments, as described above, if the model output of the machine learning model 140 indicates a non-modification operation, it can be determined that the edited second code snippet is the same as the first code snippet before editing.
[0055] In some embodiments, since the machine learning model 140 needs to determine the operation of the code snippet to be edited from multiple candidate code editing operations, in the training data of the machine learning model 140, the real editing operation corresponding to the sample can be labeled for each training sample. In order to maintain the machine learning model 140's understanding of the original semantics of the operation, the real editing operation can be directly labeled by the corresponding natural language. For example, the labels corresponding to the code addition operation, code deletion operation, code replacement operation and non-modification operation are "Add", "Delete", "Replace" and "Same" in English respectively, and will not be processed as special tokens (tokens). This can utilize the original semantic understanding ability of the language model. Of course, in addition to English labels, labels of other natural languages can also be marked.
[0056] In some embodiments, because the edit code predicted by the machine learning model 140 is highly correlated with the COT's operational considerations, if the machine learning model 140 predicts an incorrect edit operation for a code segment to be edited, the subsequent predicted code edits will also be incorrect. Therefore, in some embodiments, the weight of the training loss corresponding to the edit operation is increased during the training of the machine learning model 140.
[0057] Specifically, during the training process of the machine learning model 140, a sample code snippet and prompt word information are provided to the machine learning model 140 to be trained, and the sample code snippet is annotated with a real editing operation and a real edited code snippet obtained after applying the real editing operation to the sample code snippet. Then, a model output is obtained from the machine learning model 140 to be trained, and the model output includes a predicted editing operation and a predicted edited code snippet. Based on the first difference between the predicted editing operation and the real editing operation and the second difference between the predicted edited code snippet and the real edited code snippet, the parameters of the machine learning model 140 to be trained are updated.
[0058] During parameter updating, the first difference and the second difference are weighted using a first weight and a second weight, respectively, to determine a training loss value. The first weight applied to the first difference (i.e., the training loss corresponding to the editing operation) is greater than the second weight applied to the second difference (i.e., the training loss corresponding to the code editing result). For example, the first weight may be twice or another multiple of the second weight. Then, the parameters of the machine learning model 140 to be trained are updated based on the determined training loss value. This allows the machine learning model 140 to better update parameters to accurately determine the editing operation on the code, which in turn naturally improves the accuracy of code editing.
[0059] In some embodiments, the training process of the machine learning model 140 depends on the training data set. In some embodiments, the training of the machine learning model 140 can be implemented on the server side, for example, it can be implemented at the server device 130 or other server devices.
[0060] Generally, directly collecting data from online users will be noisy and costly, and conventional code editing training set construction schemes are mostly based on code submission data as data for predicting code editing suggestions. The construction of training samples based on code submission data can obtain a large amount of code editing data efficiently and at a low cost, but there is a certain deviation from the actual editing situation. For example, the information to be edited collected from the submission data are all code blocks that clearly need to be modified, but in the actual scenario, it is not known whether the area to be edited needs to be modified, the number of lines of modified content, and the degree of modification. In some embodiments of the present disclosure, the real online editing situation is analyzed and the real behavior of users is simulated based on the code submission data, so that the model can more accurately predict the developer's intention when editing.
[0061] FIG3 illustrates an example model training process 300 according to some embodiments of the present disclosure.
[0062] As shown in FIG3 , code submission data 310 during the code editing process is first obtained. A training dataset 330 for the machine learning model 140 is generated from the code submission data 310 according to predefined simulated code editing behaviors. Model training 340 is performed on the machine learning model 140 using the training dataset 330. Model training 340 may include multiple stages of training, including a pre-training stage 342 and a SFT stage 344. The following will first discuss the construction of the training dataset 330, followed by a discussion of the multi-stage model training.
[0063] In some embodiments, after obtaining the code submission data, at least one line of code adjacent to the code snippet can be selected from the code where the submitted code snippet is located, and then based on the code submission data and the selected at least one line of code, at least one training sample is generated according to the definition of the simulated code editing behavior.
[0064] In actual scenarios, it may be difficult to know whether the current code under the cursor needs to be modified, nor whether the code to be modified is a single line or multiple lines. Therefore, this solution searches for several lines above and / or below the edited code line involved in the code submission data. For example, you can search 2 lines above and 3 lines below the cursor position to form the initial code submission data of the training sample.
[0065] In some embodiments, when searching for nearby code lines, certain code lines may be skipped and not selected. Code lines that may be skipped include, but are not limited to, blank lines, lines of code that have been modified in the most recent history edit, lines of code that contain only comments, lines of code whose last character is "{}():", and lines of code that begin with a specific keyword (e.g., "if", "while", "for", "try", "else").
[0066] In some embodiments, when constructing training samples, it is also possible to simulate non-modification behaviors in real scenarios. This is because the code submission data may always contain data on modification behaviors. However, in real scenarios, it is possible that the current edit has been completed, or the content to be edited is already very good and does not need to be modified. At this time, forcing the model to modify may result in random modifications. Therefore, adding training samples corresponding to non-modification behaviors to the training data set of the machine learning model 140 can further enhance the code editing capabilities of the trained machine learning model 140. Specifically, in the training data set of the machine learning model 140, such training samples can be constructed, which include a code snippet before editing, a code snippet after editing, and a label for non-modification operations, and the code snippet before editing is the same as the code snippet after editing.
[0067] In some embodiments, alternatively or additionally, the number of code prefixes and code suffixes can also be simulated. In the code submission data, the number of lines of the prefix and the number of lines of the suffix are generally sufficient. However, in the actual code editing scenario, the prefix may be very short or even 0. Therefore, when constructing the training sample, based on the upper limit requirements for the prefix and the upper limit requirements for the suffix, the code editing area prefix and the code editing area suffix are divided from the code submission data (as well as the additional selected code lines). The upper limit requirement for the prefix indicates the upper limit of the number of code lines of the editing area prefix to be extracted, and the upper limit requirement for the suffix indicates the upper limit of the number of code lines of the editing area suffix to be extracted.
[0068] When determining the pre-edited code snippet and the post-edited code snippet, training samples can be generated based on the preceding and following contexts of the code editing area, the pre-edited code snippet, and the post-edited code snippet. In this scenario, the model input of the machine learning model 140 includes the pre-edited code snippet and the preceding and following contexts of the code editing area corresponding to the code snippet. These preceding and following code lines are used to provide contextual information for the code snippet to be edited.
[0069] In the training samples, the symbol "prefix" can be used to identify the code information before the edit area, and the symbol "suffix" can be used to identify the code information after the edit area. In the training set, two special tokens are used to enclose each, and the explanations of other special tokens are added to the annotation information. Table 2 below shows an example of such a training sample:
[0070] Table 2
[0071] In Table 2, the symbol “<|begin|>” indicates the beginning of the training data sample, and the symbol “ <ctxedits>"Symbols that identify historical editing operations, two symbols before and after" <edit>"Japanese"< / edit> " is used to frame a historical editing operation, corresponding to a training sample. A training sample includes the code path, the code editing area before the text, the code editing area after the text, the code editing area (including the code snippet before editing) and the code snippet after the editing of the editing area. In addition, the symbol " <path> "sum"< / path> "Paths and symbols used to frame code" <prefix> "sum"< / prefix> "Used to frame the text before the code editing area; symbol" <suffix> "sum"< / suffix> "Used to frame the text after the code editing area, symbol" <before> "sum"< / before> "Used to frame the code snippet before editing, symbol" <after> "sum"< / after> " is used to frame the edited code snippet.
[0072] Typically, the content to be edited collected by the code submission data may be the edit content corresponding to the entire code block, but in actual scenarios, it may be part of the code in the code block that needs to be edited, which can easily lead to inconsistency between the training data and the actual scenario. In some embodiments, it is also proposed to simulate the modification behavior in the code block. Multiple lines of content to be edited in the code block are cut, and only part of it is taken as the content to be edited. Specifically, when generating at least one training sample, the code block corresponding to the code submission data can be divided into multiple code fragments. Then, a partial code fragment is selected from the multiple code fragments to generate at least one training sample.
[0073] In some embodiments, training samples can also be constructed by simulating the user's line modification behavior. In the actual code writing process, the user may have edited half of the line, but the information collected from the code submission data is either unedited or edited. Therefore, when constructing a training sample, the code difference between the code snippet before editing and the code snippet after editing can be determined. The code snippet before editing is modified based on the code difference to obtain the code snippet in editing, and at least one training sample is generated based on the code snippet before editing, the code snippet in editing, and the code snippet after editing. For example, the difference between the unedited code and the edited code of a certain code line can be taken, and part of the difference can be randomly extracted and updated to obtain the intermediate code line. In this way, the code modification process can be constructed, such as "aaa"->"baa"->"bbb".
[0074] In some embodiments, because the editing operations in the code submission data do not have time information, the data is submitted once. However, in actual code editing, there will be historical editing behaviors, that is, historical code submission data associated with the code submission data. Therefore, when constructing training samples, based on the current code submission data, historical code submission data associated with the code submission data can be found to jointly construct training samples. In some embodiments, a simulation strategy of historical editing behavior can be adopted to construct historical editing data, and code submission data corresponding to several historical editing operations can be randomly selected. In some embodiments, historical code submission data may include context in the same file, or code data corresponding to similar editing operations in other files, and / or code data corresponding to editing operations with adjacent submission times. In some embodiments, these historical code submission data can be represented in training samples with corresponding symbols (for example, <ctxedits>< / ctxedits> ) to frame it.
[0075] In some embodiments, it is also possible to find situations where the preceding or following text in the relevant code editing data contains the correct answer. Since the code submission data is collected, the edited results of the code to be edited may be included in other related edits, which will cause the answer to be leaked. Therefore, in some embodiments, such information can be filtered. Specifically, perturbation processing can be performed on the code submission data to obtain the perturbed code submission data, and at least one training sample can be generated based on the perturbed code submission data. The perturbation processing may include performing one or more of the following random processing on the code submission data: randomly sampling non-relevant historical editing data, deleting situations where there may be real editing results in other editing histories, and adding situations where relevant historical editing is limited to the same submission.
[0076] In some embodiments, similar codes may be added to the training samples, for example, codes with a code similarity exceeding a similarity threshold with the code submission data are obtained, and then training samples are further constructed based on the similar codes. Extract the function name from the code to be edited based on regular matching, and then extract the function definition or usage code from the repository code based on the function name. Using a third-party code search library can help the model better predict editing suggestions. In some embodiments, similar codes can be represented by corresponding symbols in the training samples (for example, <related>< / related> ) to frame it.
[0077] In some embodiments, historical related edits can also be considered in the training samples. When constructing training samples, for code submission data, historical code editing behaviors in code editing are also considered. Taking into account that there may be a strong correlation between the code that currently needs to be edited and the user's previous editing behavior, the following categories of historical editing information can be added. In one example, contextual historical editing can be supplemented. Usually, historical edits that occur in the front or back of the code have the greatest relevance to the edits to be predicted for the current code snippet, are of the highest importance, and can be collected first. In one example, single-file historical editing can also be supplemented, that is, searching for codes corresponding to other historical editing behaviors that occur in the context from a single code file.
[0078] In some embodiments, historical edits of other files other than the current file may be searched, and codes corresponding to historical edit behaviors taught by similarity may be introduced based on text similarity of the codes.
[0079] According to the construction of the training data set mentioned in the above embodiment, through the analysis of real online editing situations, the user's real editing behavior is simulated based on the code submission data, making the training data more in line with the real scene, solving the problem that directly using code submission data for training cannot fit the real editing scene, and realizing a more accurate and efficient solution for predicting the developer's editing intentions.
[0080] Because code is a low-entropy language, and most of the content in code editing tasks is pre-edited, direct training using conventional training schemes will result in low loss and slow fitting. In some embodiments of the present disclosure, a more efficient training method is adopted, using multi-stage supervised fine-tuning (SFT) training, to enable the model to better fit the code editing training set.
[0081] Since the amount of code editing data is slightly larger than that of ordinary SFT, and since code is a low-entropy language, especially in code editing tasks, most of the content is a copy of the code before editing, and the model output content is relatively short, generally only a few dozen tokens. Therefore, only calculating the training loss of the model output will result in a small loss value and slow fitting. In some embodiments of the present disclosure, the model training 340 of the machine learning model 140 adopts a two-stage training, including the pre-training stage 342 and the SFT stage 344 as shown in Figure 4.
[0082] In the pre-training phase 342, the machine learning model 140 to be trained is pre-trained using a first training dataset. The first training dataset includes a first model input and a first model output. Pre-training involves optimizing the model's loss for the input and output data sequences in the entire training dataset. Specifically, the model optimizes the loss for both instructions (pre-edit code and prompt input) and responses (post-edit code and labels of edit operations). Considering that historical edits contain valuable code information, the first phase calculates the loss for all data.
[0083] During pre-training phase 342, machine learning model 140 is used to predict the first model input and the first model output in the first dataset, and the parameters of machine learning model 140 are updated based on the prediction errors for the first model input and the first model output. In other words, during pre-training phase 342, the training loss for both the model input (including the prompt word information) and the model output is calculated simultaneously. Because the model input contains historical edit data in a format similar to the current edit data, this allows for faster model training convergence.
[0084] In the SFT phase 344, supervised fine-tuning training is performed on the pre-trained machine learning model 140 using a second training dataset. The second training dataset includes a second model input and a second model output. In some embodiments, the second training dataset can be the same as or partially the same as the first training dataset used in the pre-training phase.
[0085] In the SFT stage 344, the machine learning model 140 is used to process the second model input and perform a prediction on the second model output and the machine learning model 140 is updated based on the prediction error of the second model output. In the SFT stage 344, only the training loss of the model output is calculated, allowing the model to be fine-tuned for the model output. Fine-tuning only the model output allows the model to better fit the code editing operation and the edited code. Therefore, supervised fine-tuning training means that the model only optimizes the loss of the target (that is, the answer or response after the instruction), rather than the loss of the entire sequence (including the instruction and the answer). The second stage only calculates the loss of the answer, allowing the model to better learn the edited code.
[0086] Embodiments of the present disclosure also provide corresponding apparatuses for implementing the above-described methods or processes. FIG4 shows a schematic structural block diagram of an example apparatus 400 for code editing according to certain embodiments of the present disclosure. Apparatus 400 may be implemented as or included in an electronic device. Each module / component in apparatus 400 may be implemented by hardware, software, firmware, or any combination thereof.
[0087] As shown in Figure 4, the device 400 includes a code acquisition module 410, which is configured to obtain a first code snippet to be edited. The device 400 also includes a model calling module 420, which is configured to call a trained machine learning model based on the prompt word information and the first code snippet to obtain the model output of the machine learning model, the prompt word information is used to guide the machine learning model to determine the editing operation on the code snippet, and generate a code editing result based on the determined editing operation. The device 400 also includes a code determination module 430, which is configured to determine the edited second code snippet based on the model output obtained from the machine learning model.
[0088] In some embodiments, the model output includes: an edit operation determined for the first code snippet from a plurality of candidate edit operations, and the second code snippet. In some embodiments, the plurality of candidate edit operations include one or more of the following: a code addition operation, a code deletion operation, a code replacement operation, and an unmodified operation.
[0089] In some embodiments, the model calling module 420 is further configured to: provide the first code snippet and prompt word information as model input to the trained machine learning model to obtain a first model output of the machine learning model; in response to the first model output indicating that the editing operation determined for the first code snippet is a non-modification operation, send a stop indication to the machine learning model to instruct the machine learning model to stop processing; and generate an output indication to indicate that the second code snippet is the same as the first code snippet.
[0090] In some embodiments, the machine learning model is trained in the following manner: providing sample code snippets and prompt word information to the machine learning model to be trained, the sample code snippets being annotated with real editing operations and real edited code snippets obtained after applying the real editing operations to the sample code snippets; obtaining model output from the machine learning model to be trained, the model output including predicted editing operations and predicted edited code snippets; updating the parameters of the machine learning model to be trained based on a first difference between the predicted editing operation and the real editing operation and a second difference between the predicted edited code snippet and the real edited code snippet.
[0091] In some embodiments, updating the parameters of the machine learning model to be trained includes: determining a training loss value by weighting the first difference and the second difference using a first weight and a second weight, respectively, the first weight applied to the first difference being greater than the second weight applied to the second difference; and updating the parameters of the machine learning model to be trained based on the determined training loss value.
[0092] In some embodiments, the training samples in the training data set of the machine learning model are constructed in the following manner: in response to obtaining code submission data during the code editing process, at least one line of code adjacent to the code snippet is selected from the code where the submitted code snippet is located; based on the code submission data and the selected at least one line of code, at least one training sample is generated according to the definition of the simulated code editing behavior.
[0093] In some embodiments, the second training sample in the training data set of the machine learning model includes a code snippet before editing, a code snippet after editing, and a label of an unmodified operation, and the code snippet before editing and the code snippet after editing are the same.
[0094] In some embodiments, generating at least one training sample includes: based on the upper limit requirements for the previous text and the upper limit requirements for the subsequent text, dividing the code editing area previous text and the code editing area subsequent text from the code submission data and the selected at least one line of code respectively; and generating a training sample based on the code editing area previous text, the code editing area subsequent text, the pre-editing code snippet and the post-editing code snippet.
[0095] In some embodiments, generating at least one training sample includes: dividing a code block corresponding to the code submission data into a plurality of code segments; and selecting some code segments from the plurality of code segments to generate at least one training sample.
[0096] In some embodiments, generating at least one training sample includes: determining a pre-editing code snippet and a post-editing code snippet from code submission data and at least one selected line of code; determining a code difference between the pre-editing code snippet and the post-editing code snippet; modifying the pre-editing code snippet based on the code difference to obtain an editing code snippet; and generating at least one training sample based on the pre-editing code snippet, the editing code snippet, and the post-editing code snippet.
[0097] In some embodiments, generating at least one training sample further includes: generating at least one training sample based on at least one of the following: historical code submission data associated with the code submission data, code whose code similarity with the code submission data exceeds a similarity threshold, or historical code editing behavior in code editing.
[0098] In some embodiments, generating at least one training sample includes: performing perturbation processing on the code submission data to obtain perturbed code submission data; and generating at least one training sample based on the perturbed code submission data.
[0099] In some embodiments, the training process of the machine learning model includes: performing pre-training on the machine learning model to be trained using a first training dataset, wherein the first training dataset includes a first model input and a first model output, and in the pre-training, using the machine learning model to perform predictions on the first model input and the first model output in the first dataset, respectively, and updating the parameters of the machine learning model based on the prediction errors of the first model input and the first model output; and performing supervised fine-tuning training on the pre-trained machine learning model using a second training dataset.
[0100] In some embodiments, the second training dataset includes a second model input and a second model output, and the second model input is processed using the machine learning model in supervised fine-tuning training and predictions are performed on the second model output and the machine learning model is updated based on the prediction error of the second model output.
[0101] FIG5 illustrates a block diagram of an electronic device 500 in which one or more embodiments of the present disclosure may be implemented. It should be understood that the electronic device 500 shown in FIG5 is merely exemplary and should not be construed as limiting the functionality and scope of the embodiments described herein. The electronic device 500 shown in FIG5 may be implemented as or included in the terminal device 110 or the server device 130 in FIG1 .
[0102] As shown in FIG5 , electronic device 500 is a general-purpose electronic device. Components of electronic device 500 may include, but are not limited to, one or more processors or processing units 510, memory 520, storage device 530, one or more communication units 540, one or more input devices 550, and one or more output devices 560. Processing unit 510 may be a real or virtual processor and is capable of performing various processes according to programs stored in memory 520. In a multi-processor system, multiple processing units execute computer-executable instructions in parallel to enhance the parallel processing capabilities of electronic device 500.
[0103] The electronic device 500 typically includes a plurality of computer storage media. Such media can be any accessible media that can be obtained by the electronic device 500, including but not limited to volatile and non-volatile media, removable and non-removable media. The memory 520 can be a volatile memory (e.g., registers, cache, random access memory (RAM)), a non-volatile memory (e.g., read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory), or some combination thereof. The storage device 530 can be a removable or non-removable medium and can include a machine-readable medium, such as a flash drive, a disk, or any other medium that can be used to store information and / or data and can be accessed within the electronic device 500.
[0104] The electronic device 500 may further include additional removable / non-removable, volatile / non-volatile storage media. Although not shown in FIG. 5 , a disk drive for reading from or writing to a removable, non-volatile disk (e.g., a “floppy disk”) and an optical drive for reading from or writing to a removable, non-volatile optical disk may be provided. In these cases, each drive may be connected to a bus (not shown) by one or more data media interfaces. The memory 520 may include a computer program product 525 having one or more program modules configured to perform various methods or actions of various embodiments of the present disclosure.
[0105] The communication unit 540 enables communication with other electronic devices via a communication medium. Additionally, the functions of the components of the electronic device 500 can be implemented in a single computing cluster or multiple computing machines that can communicate via a communication connection. Thus, the electronic device 500 can operate in a networked environment using a logical connection with one or more other servers, a network personal computer (PC), or another network node.
[0106] Input device 550 may be one or more input devices, such as a mouse, keyboard, or trackball. Output device 560 may be one or more output devices, such as a display, a speaker, or a printer. Electronic device 500 may also communicate with one or more external devices (not shown) via communication unit 540 as needed, such as a storage device, a display device, or the like, with one or more devices that allow a user to interact with electronic device 500, or with any device that allows electronic device 500 to communicate with one or more other electronic devices (e.g., a network card, a modem, etc.). Such communication may be performed via an input / output (I / O) interface (not shown).
[0107] According to an exemplary implementation of the present disclosure, a computer-readable storage medium is provided, on which computer-executable instructions are stored, wherein the computer-executable instructions are executed by a processor to implement the method described above. According to an exemplary implementation of the present disclosure, a computer program product is also provided, which is tangibly stored on a non-transitory computer-readable medium and includes computer-executable instructions, and the computer-executable instructions are executed by a processor to implement the method described above.
[0108] Various aspects of the present disclosure are described herein with reference to flowcharts and / or block diagrams of methods, apparatuses, devices, and computer program products implemented according to the present disclosure. It should be understood that each block of the flowcharts and / or block diagrams, and combinations of blocks in the flowcharts and / or block diagrams, can be implemented by computer-readable program instructions.
[0109] These computer-readable program instructions can be provided to a processing unit of a general-purpose computer, a special-purpose computer, or other programmable data processing device, thereby producing a machine such that, when these instructions are executed by the processing unit of the computer or other programmable data processing device, a device is generated that implements the functions / actions specified in one or more blocks in the flowcharts and / or block diagrams. These computer-readable program instructions can also be stored in a computer-readable storage medium, where these instructions cause the computer, programmable data processing device, and / or other device to operate in a specific manner. Thus, the computer-readable medium storing the instructions comprises an article of manufacture that includes instructions for implementing various aspects of the functions / actions specified in one or more blocks in the flowcharts and / or block diagrams.
[0110] Computer-readable program instructions can be loaded onto a computer, other programmable data processing apparatus, or other device so that a series of operational steps are performed on the computer, other programmable data processing apparatus, or other device to produce a computer-implemented process, thereby causing the instructions executed on the computer, other programmable data processing apparatus, or other device to implement the functions / actions specified in one or more boxes in the flowchart and / or block diagram.
[0111] The flow charts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the systems, methods and computer program products according to multiple implementations of the present disclosure. In this regard, each box in the flow chart or block diagram can represent a part for a module, program segment or instruction, and a part for a module, program segment or instruction comprises one or more executable instructions for realizing the logical function of the specification. In some alternative implementations, the functions marked in the box can also occur in a sequence different from that marked in the accompanying drawings. For example, two continuous boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flow chart, and the combination of the boxes in the block diagram and / or flow chart can be realized by a special hardware-based system that performs the function or action of the specification, or can be realized by a combination of special hardware and computer instructions.
[0112] While various implementations of the present disclosure have been described above, the foregoing description is intended to be illustrative, not exhaustive, and not limited to the disclosed implementations. Many modifications and variations will be apparent to those skilled in the art without departing from the scope and spirit of the described implementations. The terminology used herein is selected to best explain the principles of the implementations, their practical applications, or improvements to existing technologies, or to enable others skilled in the art to understand the various implementations disclosed herein.< / ctxedits>
Claims
1. A code editing method, comprising: Get the first code snippet to be edited; Calling a trained machine learning model based on the prompt word information and the first code snippet to obtain a model output of the machine learning model, wherein the prompt word information is used to guide the machine learning model to determine an editing operation on the code snippet, and to generate a code editing result based on the determined editing operation; as well as Based on a model output obtained from the machine learning model, an edited second code snippet is determined.
2. The method of claim 1, wherein the model output comprises: an editing operation determined for the first code snippet from a plurality of candidate editing operations, and the second code snippet, and The multiple candidate editing operations include one or more of the following: a code adding operation, a code deleting operation, a code replacing operation, and a non-modifying operation.
3. The method according to claim 2, wherein calling a trained machine learning model based on prompt word information and the first code snippet comprises: Providing the first code snippet and prompt word information as model input to a trained machine learning model to obtain a first model output of the machine learning model; In response to the first model output indicating that the editing operation determined for the first code snippet is the non-modification operation, sending a stop indication to the machine learning model to instruct the machine learning model to stop processing; as well as An output indication is generated to indicate that the second code snippet is the same as the first code snippet.
4. The method of claim 1, wherein the machine learning model is trained by: Providing a sample code snippet and the prompt word information to a machine learning model to be trained, wherein the sample code snippet is annotated with a real editing operation and a real edited code snippet obtained after applying the real editing operation to the sample code snippet; Obtaining a model output from the machine learning model to be trained, wherein the model output includes a predicted editing operation and a predicted edited code snippet; Based on a first difference between the predicted editing operation and the actual editing operation and a second difference between the predicted edited code fragment and the actual edited code fragment, the parameters of the machine learning model to be trained are updated.
5. The method according to claim 4, wherein updating the parameters of the machine learning model to be trained comprises: Determine a training loss value by weighting the first difference and the second difference using a first weight and a second weight, respectively, the first weight applied to the first difference being greater than the second weight applied to the second difference; as well as The parameters of the machine learning model to be trained are updated based on the determined training loss value.
6. The method according to claim 1, wherein the training samples in the training data set of the machine learning model are constructed by: In response to obtaining code submission data during the code editing process, selecting at least one line of code adjacent to the code snippet from the code where the submitted code snippet is located; At least one training sample is generated according to a definition of simulated code editing behavior based on the code submission data and the selected at least one line of code.
7. The method according to claim 1, wherein the second training sample in the training data set of the machine learning model includes a code snippet before editing, a code snippet after editing, and a label of an unmodified operation, and the code snippet before editing and the code snippet after editing are the same.
8. The method according to claim 6, wherein generating at least one training sample comprises: Based on the upper limit requirement of the preceding text and the upper limit requirement of the succeeding text, the preceding text of the code editing area and the succeeding text of the code editing area are respectively divided from the code submission data and the selected at least one line of code; as well as A training sample is generated based on the preceding text of the code editing area, the following text of the code editing area, the code fragment before editing, and the code fragment after editing.
9. The method according to claim 6, wherein generating at least one training sample comprises: Dividing the code block corresponding to the code submission data into multiple code fragments; and Some code snippets are selected from the multiple code snippets to generate the at least one training sample.
10. The method according to claim 6, wherein generating at least one training sample comprises: Determine a pre-editing code segment and a post-editing code segment from the code submission data and the selected at least one line of code; Determine a code difference between the code snippet before editing and the code snippet after editing; Modifying the code fragment before editing according to the code difference to obtain the code fragment in editing; as well as At least one training sample is generated based on the pre-editing code segment, the editing code segment, and the post-editing code segment.
11. The method according to claim 6, wherein generating at least one training sample further comprises: At least one training sample is also generated based on at least one of the following: historical code submission data associated with the code submission data, Codes whose code similarity with the code submission data exceeds the similarity threshold, or Historical code editing behavior in said code editing.
12. The method according to claim 6, wherein generating at least one training sample comprises: Performing a disturbance process on the code submission data to obtain disturbed code submission data; as well as At least one training sample is generated based on the perturbed code submission data.
13. The method according to claim 1, wherein the training process of the machine learning model comprises: Pre-training the machine learning model to be trained using a first training data set, The first training data set includes a first model input and a first model output, and in the pre-training, the machine learning model is used to perform predictions on the first model input and the first model output in the first data set, respectively, and the parameters of the machine learning model are updated based on the prediction errors of the first model input and the first model output; as well as Performing supervised fine-tuning training on the pre-trained machine learning model using a second training data set, The second training data set includes a second model input and a second model output, and in the supervised fine-tuning training, the second model input is processed by the machine learning model and prediction is performed on the second model output and the machine learning model is updated based on the prediction error of the second model output.
14. A device for code editing, comprising: A code acquisition module is configured to acquire a first code segment to be edited; a model calling module configured to call a trained machine learning model based on prompt word information and the first code snippet to obtain a model output of the machine learning model, wherein the prompt word information is used to guide the machine learning model to determine an editing operation on the code snippet, and to generate a code editing result based on the determined editing operation; as well as The code determination module is configured to determine an edited second code snippet based on a model output obtained from the machine learning model.
15. An electronic device, comprising: at least one processing unit; as well as At least one memory, the at least one memory being coupled to the at least one processing unit and storing instructions for execution by the at least one processing unit, the instructions causing the electronic device to perform the method according to any one of claims 1 to 13 when executed by the at least one processing unit.
16. A computer-readable storage medium having a computer program stored thereon, wherein the computer program can be executed by a processor to implement the method according to any one of claims 1 to 13.
17. A computer program product comprising computer executable instructions, wherein the computer executable instructions, when executed by a processor, implement the method according to any one of claims 1 to 13.