Training method of code generation model based on cloud service and code generation method

By specifically training the code generation model on the cloud service, using function-level code snippets and natural language descriptions for two-stage training, and filtering unsafe data, the problems of low code generation accuracy and security risks in the existing technology are solved. Achieving higher accuracy and security.

CN118535135APending Publication Date: 2024-08-23HUAWEI CLOUD COMPUTING TECHNOLOGIES CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202310116008.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-02-14
Publication Date
2024-08-23

AI Technical Summary

Technical Problem

In the existing technology, code generation models have low accuracy in generating function-level code snippets, and there are security risks. In particular, because code files are directly used for training without targeted training of the model, the generated code snippets are unsafe.

Method used

Adopting a code generation model training method based on cloud services, using multiple function-level code snippets and natural language descriptions as samples, a two-stage training is performed, and the model is deployed on the cloud server to reduce the risk of privacy leakage, using data enhancement and causal language construction The model improves the generation accuracy and filters out code fragments and descriptions that do not meet security conditions.

Benefits of technology

It improves the accuracy of generating function-level code snippets, reduces the security risks of code snippets, and ensures the security of training data and the quality of generated code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118535135A
    Figure CN118535135A
Patent Text Reader

Abstract

The invention provides a training method of a code generation model based on cloud service and a code generation method, and belongs to the technical field of computers. The training method is applied to a cloud server, and comprises the following steps: training a code generation model based on a plurality of first samples; receiving a prompt word and a plurality of second samples sent by the terminal; and processing the plurality of second samples based on the cue through the code generation model, and updating the cue based on the processing result of the plurality of second samples and each second sample. According to the method, the training samples are the function-level code snippets and the natural language description, and the accuracy of generating the function-level code snippets can be improved; besides, the code generation model is deployed on the cloud server, and the prompt words customized by the user according to the target application scene and the second sample are sent by the terminal, so that decoupling of the online model and the private data of the user can be realized, and the risk of privacy disclosure is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a training method and a code generation method for a code generation model based on a cloud service. Background Art

[0002] Code generation refers to generating code in a specific programming language based on the requirements expressed by the user in natural language, thereby improving the user's programming efficiency.

[0003] In the related art, multiple code files are input into a code generation model for training, so that the code generation model can generate code snippets based on natural language descriptions in the code files or complete existing code snippets.

[0004] However, during program development, the most common scenario is the generation of function-level code snippets. In the above method, the code files are directly used as samples to train the code generation model, without specifically training the model's ability to generate function-level code snippets, resulting in a low accuracy rate for the generated function-level code snippets. In addition, each code file may have security vulnerabilities, resulting in a high security risk for the generated code snippets. Summary of the invention

[0005] The embodiment of the present application provides a code generation model training method and a code generation method based on cloud services, which can improve the accuracy of generating function-level code snippets and reduce the security risks of the generated code snippets. The technical solution is as follows.

[0006] In a first aspect, a training method for a code generation model based on a cloud service is provided, which is applied to a cloud server, and the method includes:

[0007] Based on multiple first samples, a code generation model is trained, wherein the first sample includes a first code snippet at a function level and a first natural language description corresponding to the first code snippet, and the code generation model includes multiple decoding layers; a prompt and multiple second samples sent by a receiving terminal are received, wherein the second sample includes a second code snippet at a function level in a target application scenario and a second natural language description corresponding to the second code snippet, and the prompt includes multiple tensors, each tensor corresponds to a decoding layer of the code generation model, and each tensor is used to be spliced ​​with the output of the previous layer of the decoding layer corresponding to the tensor to serve as the input of the corresponding decoding layer; through the code generation model, based on the prompt, the multiple second samples are processed, and the prompt is updated based on the processing results of the multiple second samples and each second sample.

[0008] In the above method, the training samples are function-level code snippets and natural language descriptions, which can improve the accuracy of generating function-level code snippets; in addition, since the code generation model is deployed on the cloud server, and the prompts and the second sample customized by the user according to the target application scenario are sent by the terminal, it is possible to achieve decoupling of the online model and the user's private data, thereby reducing the risk of privacy leakage.

[0009] Optionally, based on the plurality of first samples, training a code generation model comprises:

[0010] Based on multiple first samples, a first-stage training is performed on the code generation model, and the loss function used in the first-stage training is obtained based on causal language modeling of a third code snippet and a third natural language description output during the first-stage training; data enhancement is performed on the multiple first samples to obtain multiple third samples, and the third samples satisfy at least one of the length of the natural language description being within a first length range and the length of the code snippet being within a second length range; based on the multiple third samples, a second-stage training is performed on the code generation model after the first-stage training, and the loss function used in the second-stage training is obtained based on causal language modeling of a fourth code snippet output during the second-stage training.

[0011] In the above method, the code generation model is trained in two stages, and the samples used in the second stage training are obtained by enhancing the samples used in the first stage training, which can improve the code generation accuracy of the code generation model.

[0012] Optionally, the method further comprises at least one of the following:

[0013] Filter out code snippets and natural language descriptions that do not meet data security conditions from multiple code snippets to be screened and multiple natural language descriptions to be screened, obtain multiple first code snippets and multiple first natural language descriptions, and obtain multiple first samples based on the multiple first code snippets and multiple first natural language descriptions; determine multiple first code snippets from the multiple code snippets to be screened, determine multiple first natural language descriptions corresponding to the first code snippets from the multiple natural language descriptions to be screened, and obtain multiple first samples based on the multiple first code snippets and multiple first natural language descriptions; respectively compose each first code snippet and the first natural language description corresponding to each first code snippet into a sequence, normalize each sequence, and obtain multiple first samples.

[0014] Optionally, filtering out code snippets and natural language descriptions that do not meet data security conditions from the plurality of code snippets to be screened and the plurality of natural language descriptions to be screened to obtain a plurality of first code snippets and a plurality of first natural language descriptions includes at least one of the following:

[0015] Filter out a first code snippet and a first natural language description including a code open source certificate from multiple code snippets to be screened and multiple natural language descriptions to be screened; filter out code snippets and natural language descriptions including security vulnerabilities from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple first code snippets and multiple first natural language descriptions; filter out code snippets and natural language descriptions including sensitive information from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple first code snippets and multiple first natural language descriptions; filter out code snippets and natural language descriptions including semantic defects from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple first code snippets and multiple first natural language descriptions.

[0016] In the above method, the cloud server filters out code snippets and natural language descriptions that do not meet data security conditions from multiple code snippets to be screened and multiple natural language descriptions to be screened, which can ensure the security of training data from the source and avoid possible risks from the source of training data.

[0017] Optionally, determining a plurality of first code snippets from the plurality of code snippets to be screened includes any of the following:

[0018] Based on function definition rules of different programming languages, a first code snippet is identified from multiple code snippets to be screened; an abstract syntax tree analysis is performed on the multiple code snippets to be screened, and the first code snippet is identified from the multiple code snippets to be screened; and the first code snippet is identified from the multiple code snippets to be screened through a natural language processing method.

[0019] In the above method, the cloud server extracts function-level code snippets from multiple code snippets and natural language descriptions that meet data security conditions, and then can specifically train the code generation model to generate function-level code snippets, thereby improving the accuracy of generating function-level code snippets.

[0020] Optionally, determining a plurality of first natural language descriptions corresponding to the first code snippet from a plurality of natural language descriptions to be screened includes any of the following:

[0021] Based on annotation rules and language character rules, multiple first natural language descriptions corresponding to the first code snippet are determined from multiple natural language descriptions to be screened; abstract syntax tree analysis is performed on the multiple natural language descriptions to be screened, and multiple first natural language descriptions corresponding to the first code snippet are determined from the multiple natural language descriptions to be screened; through a natural language processing method, multiple first natural language descriptions corresponding to the first code snippet are determined from the multiple natural language descriptions to be screened.

[0022] Optionally, each sequence is normalized to obtain a plurality of first samples, including at least one of the following:

[0023] An abstract syntax tree analysis is performed on each sequence to obtain an analysis result of each sequence; based on the code syntax specification and each analysis result, the code syntax of the first code fragment in each sequence is normalized to obtain multiple first samples; based on the code format specification, the code format of the first code fragment in each sequence is normalized to obtain multiple first samples.

[0024] In the above method, by normalizing the code syntax and / or the code format of each sequence, the samples can be normalized, thereby improving the efficiency of model training and the accuracy of code generation.

[0025] In a second aspect, a cloud service-based code generation method is provided, which is applied to a cloud server, and the method includes:

[0026] A code generation request sent by a terminal is received, the code generation request carries a code generation task description and a prompt in a target scenario, the code generation task description includes a function signature and a natural language description of a code snippet to be generated, the prompt includes multiple tensors, each tensor corresponds to a decoding layer of a code generation model, and each tensor is used to be spliced ​​with the output of a layer above the decoding layer corresponding to the tensor to serve as an input to the corresponding decoding layer; through the code generation model, based on the prompt, the code generation task description is decoded to obtain at least one fifth code snippet; based on a code quality condition, at least one candidate code snippet is determined from the at least one fifth code snippet, and the candidate code snippet is sent to the terminal.

[0027] In the above method, after the cloud server receives the code generation task description and prompt sent by the terminal, the code generation model decodes the code generation task description based on the prompt and generates at least one code snippet, which can improve the accuracy of the generated function-level code snippet. In addition, since the prompt is updated and saved by the terminal side, the privatization of user weights and user data can be achieved, thereby improving user data security and avoiding the risk of code privacy leakage.

[0028] Optionally, the code generation request further carries a generation mode identifier, which indicates a generation mode of the code snippet. The code generation task description is decoded based on the prompt through the code generation model to obtain at least one fifth code snippet, including:

[0029] If the generation mode indicated by the generation mode identifier is function-level code generation, the code generation task description is decoded based on prompts through the code generation model, and the generated function-level code snippet is output; if the generation mode indicated by the generation mode identifier is non-function-level code generation, in the process of decoding the code generation task description based on prompts through the code generation model, if the number of output line breaks reaches the target number, the generated non-function-level code snippet is output.

[0030] In the above method, the code generation model generates function-level or non-function-level code snippets based on the generation pattern identifier, which can meet the different needs of users for code snippets, improve the flexibility of code generation while ensuring the accuracy of code generation, and is conducive to improving user experience.

[0031] Optionally, based on the code quality condition, at least one candidate code fragment is determined from the at least one fifth code fragment, including at least one of the following:

[0032] Filter out code fragments including security holes or sensitive information from at least one fifth code fragment to obtain at least one candidate code fragment; remove duplicate code fragments from at least one fifth code fragment to obtain at least one candidate code fragment.

[0033] In the above method, in the above method, the security of the generated code snippets is screened and deduplicated by using a security check method, which can further ensure the quality and security of the generated code snippets.

[0034] In a third aspect, a training device for a code generation model based on a cloud service is provided, the device comprising at least one functional module, and the at least one functional module is used to execute a training method for a code generation model based on a cloud service as provided in the first aspect or any possible implementation of the first aspect.

[0035] In a fourth aspect, a cloud service-based code generation device is provided, the device comprising at least one functional module, and the at least one functional module is used to execute the cloud service-based code generation method provided by the aforementioned second aspect or any possible implementation method of the second aspect.

[0036] In a fifth aspect, a computing device cluster is provided, the computing device cluster comprising at least one computing device, each computing device comprising a processor and a memory;

[0037] The processor of the at least one computing device is used to execute instructions stored in the memory of the at least one computing device, so that the computing device cluster executes the training method of the cloud service-based code generation model provided by the aforementioned first aspect or any possible implementation of the first aspect.

[0038] In a sixth aspect, a computing device cluster is provided, the computing device cluster comprising at least one computing device, each computing device comprising a processor and a memory;

[0039] The processor of the at least one computing device is used to execute instructions stored in the memory of the at least one computing device, so that the computing device cluster executes the cloud service-based code generation method provided by the aforementioned second aspect or any possible implementation of the second aspect.

[0040] In the seventh aspect, a computer program product comprising instructions is provided. When the instructions are executed by a computing device cluster, the computing device cluster executes a training method for a cloud service-based code generation model as provided in the first aspect or any possible implementation of the first aspect.

[0041] In an eighth aspect, a computer program product comprising instructions is provided. When the instructions are executed by a computing device cluster, the computing device cluster executes a cloud service-based code generation method as provided in the aforementioned second aspect or any possible implementation of the second aspect.

[0042] In a ninth aspect, a computer-readable storage medium is provided, which includes computer program instructions. When the computer program instructions are executed by a computing device cluster, the computing device cluster executes a training method for a cloud service-based code generation model provided in the aforementioned first aspect or any possible implementation of the first aspect.

[0043] In the tenth aspect, a computer-readable storage medium is provided, which includes computer program instructions. When the computer program instructions are executed by a computing device cluster, the computing device cluster executes the cloud service-based code generation method provided by the aforementioned second aspect or any possible implementation of the second aspect. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] Figure 1 It is a schematic diagram of an implementation environment of a cloud service-based code generation method provided in an embodiment of the present application;

[0045] Figure 2 It is a flowchart of a training method for a code generation model based on a cloud service provided in an embodiment of the present application;

[0046] Figure 3 It is a flowchart of a training method for a code generation model based on a cloud service provided in an embodiment of the present application;

[0047] Figure 4 is a schematic diagram of a first code snippet provided in an embodiment of the present application;

[0048] Figure 5 is a schematic diagram of a first sample provided in an embodiment of the present application;

[0049] Figure 6 It is a structural diagram of a code generation model provided in an embodiment of the present application;

[0050] Figure 7 It is a flow chart of a fine adjustment stage provided in an embodiment of the present application;

[0051] Figure 8 This is a schematic diagram of a prompt update process provided by an embodiment of the present application;

[0052] Fig. 9 is a flow chart of a code generation method based on cloud services provided in an embodiment of the present application;

[0053] Fig.10 is a schematic diagram of a code generation task description provided in an embodiment of the present application;

[0054] Fig.11 is a schematic diagram of a candidate code snippet provided in an embodiment of the present application;

[0055] Fig.12 It is a schematic diagram of generating a function-level code snippet provided by an embodiment of the present application;

[0056] Fig.13 is a schematic diagram of generating a single-line non-function-level code snippet provided by an embodiment of the present application;

[0057] Fig.14 is a schematic diagram of generating a multi-line non-function-level code snippet provided by an embodiment of the present application;

[0058] Fig.15 It is a flowchart of a code generation method based on cloud service provided in an embodiment of the present application;

[0059] Fig.16 It is a schematic diagram of the stages of a cloud service-based code generation method provided in an embodiment of the present application;

[0060] Fig.17 It is a schematic diagram of an implementation environment of another cloud service-based code generation method provided in an embodiment of the present application;

[0061] Fig.18 It is a structural block diagram of a training device for a code generation model based on a cloud service provided in an embodiment of the present application;

[0062] Fig.19 It is a structural block diagram of a cloud service-based code generation device provided in an embodiment of the present application;

[0063] Fig. 20 is a schematic diagram of the structure of a computing device provided in an embodiment of the present application;

[0064] Fig.21 is a schematic diagram of a computing device cluster provided in an embodiment of the present application;

[0065] Fig. 22 It is a schematic diagram of a possible implementation method of a computing device cluster provided in an embodiment of the present application. DETAILED DESCRIPTION

[0066] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.

[0067] The embodiments of the present application relate to artificial intelligence services and code generation technologies in the cloud field in the field of computer technology. For ease of understanding, the relevant concepts in artificial intelligence services and code generation technologies in the cloud field are introduced below.

[0068] First, we introduce the relevant concepts in artificial intelligence services in the cloud field.

[0069] 1. Artificial intelligence (AI): The basic principle of AI is to combine massive data with super-powerful computing and processing capabilities and intelligent algorithms to build an AI model that solves specific problems, so that the AI ​​model can automatically summarize and learn potential patterns or features from the data, thereby achieving a way of thinking close to that of humans. AI models are also AI algorithms (or AI operators), which are a general term for mathematical algorithms built on the principles of AI. They are also the basis for using AI to solve specific problems, such as deep learning (DL) models.

[0070] 2. Training and reasoning of AI models: Any AI model needs to be trained before solving specific technical problems. The training method of an AI model refers to the process of using a specified initial model to calculate the training data, and adjusting the parameters in the initial model using a certain method based on the calculation results, so that the model gradually learns certain rules and has specific functions. After training, an AI model with stable functions can be used for reasoning. The training of an AI model is the process of using a trained AI model to calculate the input data and obtain predicted reasoning results.

[0071] 3. AI Basic Development Platform: The AI ​​Basic Development Platform is a one-stop AI development platform for users. The AI ​​Basic Development Platform can provide various capabilities in the entire AI development process, including data preprocessing, model building and training, model management, model deployment, data optimization, and model optimization updates. Users can complete the development of AI models and the deployment and management of AI applications based on the AI ​​Basic Development Platform. The various capabilities in the AI ​​Basic Development Platform can be integrated for users to use throughout the AI ​​process, or they can provide users with independent functions separately.

[0072] 4. AI services in the cloud field: There are two mainstream types of AI services in the cloud field, one is the AI ​​basic development platform service of the platform-as-a-service (PaaS) type, and the other is the AI ​​application cloud service of the software-as-a-service (SaaS) type. For the first type of AI basic development platform service, public cloud service providers provide customers with AI basic development platforms with the support of sufficient underlying resources and upper-level AI algorithm capabilities. The AI ​​development framework and various AI algorithms built into the AI ​​basic development platform can be used by customers to quickly build and develop AI models or AI applications that meet personalized needs on the AI ​​basic development platform. For the second type of AI application cloud service, public cloud service providers provide ready-to-use, general AI application cloud services through cloud platforms, allowing customers to use AI capabilities in various application scenarios with zero barriers.

[0073] The following introduces the relevant concepts in code generation technology.

[0074] 1. Code generation: In software engineering (SE), code generation refers to the automatic generation of a specified program language sequence with a specific function or structure by a certain software program. In short, it is the generation of a program by a program. Code generation includes program synthesis and code completion. Among them, program synthesis is to generate corresponding code snippets from scratch according to a certain natural language description or preset functional description, which is code generation based on natural language (text2code); code completion is to continue to generate the following code snippet based on the previous code snippet.

[0075] 2. Language model: A language model refers to the modeling of a language sequence to predict the probability of occurrence of a language sequence, that is, to decode a language sequence to predict the probability of an element (character, word, phrase, etc.) appearing at each position in the language sequence. Language sequences include natural languages ​​and programming languages, etc. A common way to model a language sequence is to model the language sequence through a neural network. For example, by building a decoder network composed of multiple neural network computing units (transformers), the decoder predicts the element that may appear at the next position based on the existing context in the language sequence, and iterates until the entire sequence is predicted, that is, the decoding is completed.

[0076] 3. Corpora: Corpora is also called free text, which can be words, phrases, sentences, fragments, articles and their combination.

[0077] 4. Pre-training of language models: Pre-training of language models refers to training the corresponding language model through massive language sequence corpus, that is, samples, so that the language model has the ability to predict the probability of occurrence of a certain element at a certain position. This language model is also called a pre-trained language model (PLM).

[0078] 5. Code generation tools based on pre-training technology: Code generation tools based on pre-training technology are products that are created by combining technologies from the two fields of natural language processing (NLP) and programming language processing (PLP). Users can use the code generation tool to perform program synthesis or code completion. For example, the user first edits the natural language description, that is, writes code comments, and then the code generation tool decodes the natural language description to generate a code snippet corresponding to the function described by the natural language description, and presents the code snippet to the user in the form of a recommendation. The user decides to accept or reject the recommendation, or to further modify the code snippet after accepting it.

[0079] 6. Prompt: The essence of prompt is a set of text or vectors represented by tensors, which are used to indicate the application scenarios corresponding to the training samples. Each tensor corresponds to a decoding layer, and each tensor is used to concatenate with the output of the previous layer of the corresponding decoding layer as the input of the corresponding decoding layer.

[0080] 7. Prompt tuning: Prompt tuning refers to adding additional, trainable parameters to the pre-trained model and using these parameters to generate prompts for the target application scenario. Fine-tuning the model based on these parameters can improve the accuracy of code generation in customized scenarios.

[0081] The above introduces the relevant concepts of AI services and code generation technology in the cloud field. The following introduces the implementation environment of the embodiments of the present application.

[0082] Figure 1 Schematic diagram of the implementation environment of a cloud service-based code generation method provided in an embodiment of the present application. Figure 1 As shown, the implementation environment includes a terminal 101 and a server 102. The terminal 101 and the server 102 are connected to each other through a wired network or a wireless network.

[0083] Terminal 101 can be at least one of the devices such as desktop computers, laptops and laptop portable computers. Terminal 101 has a communication function and can access the Internet. Terminal 101 can refer to one of multiple terminals. The embodiment of the present application is only illustrated by terminal 101. Those skilled in the art can know that the number of the above-mentioned terminals can be more or less. Schematically, terminal 101 runs a code generation tool, which provides a code generation function and can generate the following code fragment based on the natural language description and the context of the code fragment edited by the user. For example, the code generation tool responds to the triggering operation of the code generation function, obtains the natural language description and code fragment for describing the code generation task, calls the code generation model deployed on the server 102 through the application programming interface (application program interface, API), and the server 102 decodes the code generation task through the code generation model, generates the corresponding code fragment, and sends the generated code fragment to the terminal 101. It should be noted that the code generation tool can be a functional module on a local or online code editor, a functional module on an integrated development environment (IDE) on a local or online editor, a tool or plug-in carried on a local or online code editor, a tool or plug-in carried on a local or online integrated development environment, a locally running software tool, an online running web page tool, etc., and the embodiments of the present application are not limited to this.

[0084] The server 102 may be an independent physical server, or a server cluster composed 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 delivery networks (CDNs), and big data and artificial intelligence platforms. Exemplarily, the server 102 is used to collect and process data to obtain samples required for code generation model training; the server 102 is also used to train the code generation model, deploy the trained code generation model, provide online code generation services for the terminal 101, send the generated code snippets to the terminal, and so on. It should be noted that server 102 can generally refer to one of multiple servers. The embodiment of the present application only uses server 102 as an example. Those skilled in the art can know that the various functions of the above-mentioned server 102 can be performed by different servers respectively. For example, server A is used to collect and process data to obtain samples required for code generation model training, server B is used to train the code generation model, and server C is used to deploy the trained code generation model, provide online code generation services for terminal 101, and send the generated code snippets to the terminal. The embodiment of the present application does not limit this. In some embodiments, server 102 is also associated with a data acquisition and processing device, which is used to collect and process data to obtain samples required for code generation model training. The embodiment of the present application does not limit this.

[0085] In some embodiments, the wireless network or wired network uses standard communication technology and / or protocol. The network is usually the Internet, but can also be any network, including but not limited to a local area network (LAN), a metropolitan area network (MAN), a wide area network (WAN), a mobile, wired or wireless network, a dedicated network or any combination of a virtual private network. In some embodiments, peer-to-peer (P2P) communication is implemented between the terminal 101 and the server 102 based on a remote procedure call protocol (RPC). In some embodiments, the terminal 101 and the server 102 use technologies and / or formats including hypertext markup language (HTML), extensible markup language (XML), etc. to represent data blocks exchanged through the network. In addition, conventional encryption technologies such as secure socket layer (SSL), transport layer security (TLS), virtual private network (VPN), and Internet protocol security (IPsec) can also be used to encrypt all or some links. In other embodiments, customized and / or dedicated data communication technologies can be used to replace or supplement the above-mentioned data communication technologies.

[0086] The implementation environment of the embodiment of the present application is introduced above. The training method and code generation method of the cloud service-based code generation model provided in the embodiment of the present application are introduced below.

[0087] First, the training method of the code generation model based on cloud service in an embodiment of the present application is introduced. Figure 2 is a flowchart of a training method for a code generation model based on a cloud service provided in an embodiment of the present application, such as Figure 2 As shown, taking the method executed by a cloud server as an example, the method includes the following steps 201 to 203.

[0088] 201. The cloud server trains a code generation model based on a plurality of first samples, wherein the first samples include a first code snippet at a function level and a first natural language description corresponding to the first code snippet, and the code generation model includes a plurality of decoding layers.

[0089] Among them, step 201 is also the training phase of the code generation model. The first sample is a sequence including multiple elements, each element corresponds to a position in the sequence, and each element can be a character, a word, or a word, etc., which is not limited in the embodiment of the present application. The sequence is divided into a first code snippet part and a first natural language description part. The first code snippet at the function level refers to a code snippet for implementing a fixed operation function. The first natural language description corresponding to the first code snippet is used to describe the function of the first code snippet, and can also be understood as a comment of the first code snippet. The first natural language description can be in Chinese or English, etc., which is not limited in the embodiment of the present application. The code generation model is a language model. In some embodiments, the code generation model includes a coding layer and multiple decoding layers. After the first sample is input into the code generation model, it is first encoded by the coding layer of the code generation model to obtain a vector corresponding to the first sample, and then the vector is decoded by multiple decoding layers of the code generation model to obtain a processing result of the first sample. In other embodiments, the cloud server encodes the first sample to obtain a vector corresponding to the first sample, inputs the vector into the code generation model, and decodes the vector through multiple decoding layers of the code generation model to obtain a processing result of the first sample. Among them, the multiple decoding layers of the code generation model can use an autoregressive method to predict the elements that may appear at each position of the subsequent text based on the existing previous text in the sequence, and output a sequence composed of the predicted elements. Schematically, the code generation model can predict the element with the highest probability of appearing at position n based on the existing elements at position 0 to position n-1 in the first sample, and then predict the element with the highest probability of appearing at position n+1 based on the existing elements at position 0 to position n, until the prediction of the elements at each position is completed, and output a sequence composed of the predicted elements, where n is a positive integer greater than or equal to 1.

[0090] In some embodiments, the first sample is obtained by processing a plurality of code snippets and natural language descriptions to be screened by the cloud server. For example, the cloud server filters out the code snippets and natural language descriptions that do not meet the data security conditions from the plurality of code snippets and natural language descriptions to be screened, and obtains a plurality of first samples, etc. The above processing process will be described in detail in the subsequent embodiments and will not be repeated here. Among them, the sources of the code snippets to be screened and the natural language descriptions to be screened include code engineering files, independent code snippets, script files, etc. The code snippets to be screened can be code snippets written in any programming language such as python and Java, and the natural language descriptions to be screened can be based on any natural language such as Chinese or English, which is not limited in the embodiments of the present application. In some embodiments, the number of the code snippets to be screened is at the code warehouse level, and the cloud server can obtain the above-mentioned files in the code warehouse through the uniform resource locator (URL) link of the code warehouse. Of course, the cloud server can also obtain the code snippets and natural language descriptions to be screened in other ways, which is not limited in the embodiments of the present application. In other embodiments, the cloud server is associated with a data acquisition and processing device, which is used to process multiple code snippets and natural language descriptions to be screened to obtain multiple first samples, and send the multiple first samples to the cloud server. The cloud server trains the code generation model based on the multiple first samples, thereby saving the computing resources of the cloud server and alleviating the computing pressure of the cloud server.

[0091] Schematically, the cloud server trains the code generation model based on multiple first samples, including: processing a first sample through the code generation model to obtain the probability of the elements that may appear at each position in the first sample; determining the loss value of this iteration based on the target elements that already exist at each position in the first sample and the obtained probability of the elements that may appear at each position, adjusting the parameters of the code generation model based on the loss value, and performing the next iteration based on the adjusted code generation model until the training end condition is met to obtain the trained code generation model, for example, the training end condition is that the probability value of the target element appearing at each position predicted by the code generation model obtains the maximum value. It should be noted that the above-mentioned process of training the code generation model is described by taking the code generation model as an example of performing one iteration based on a first sample. In some embodiments, the code generation model is performed based on multiple first samples. Of course, the cloud server can also train the code generation model based on other training methods, which is not limited in the embodiments of the present application.

[0092] 202. The cloud server receives a prompt and multiple second samples sent by the terminal, wherein the second sample includes a second code snippet at the function level in the target application scenario and a second natural language description corresponding to the second code snippet, and the prompt includes multiple tensors, each tensor corresponds to a decoding layer of the code generation model, and each tensor is used to be spliced ​​with the output of the previous layer of the decoding layer corresponding to the tensor to serve as the input of the corresponding decoding layer.

[0093] Among them, the second sample is obtained by the terminal processing the code snippet and natural language description to be screened in the target application scenario, that is, the second sample is customized by the user based on the target application scenario, and the process is the same as the process of obtaining the first sample in the above step 201, and will not be repeated. The prompt is a set of texts or vectors represented by tensors, which are used to prompt the target application scenario corresponding to the second sample, and each tensor corresponds to a decoding layer of the code generation model. In some embodiments, the number of tensors in the prompt is the same as the number of decoding layers of the code generation model, that is, each decoding layer has a corresponding tensor, wherein the first tensor is used to splice with the vector corresponding to the second sample as the input of the first decoding layer, and the mth tensor is used to splice with the output of the m-1th decoding layer of the code generation model as the input of the mth decoding layer, wherein m is a positive integer greater than or equal to 2. In other embodiments, the number of prompt tensors is less than the number of decoding layers of the code generation model, that is, only some decoding layers have corresponding tensors, wherein, for decoding layers with corresponding tensors, the input of the decoding layer is the concatenation of the output of the previous layer of the decoding layer and the tensor corresponding to the decoding layer, and for decoding layers without corresponding tensors, the input of the decoding layer is the output of the previous layer of the decoding layer. It should be noted that the above description of the quantitative relationship between tensors and decoding layers is only exemplary and can be customized according to actual needs, and the embodiments of the present application do not limit this.

[0094] In some embodiments, the terminal generates a probability distribution by setting the type of probability distribution (e.g., normal distribution, mean distribution, Poisson distribution, etc.), mean and variance and other parameters, and samples the probability distribution, and forms a tensor with the sampled values ​​to obtain a prompt. Among them, different tensors in the prompt can be obtained based on different probability distributions, that is, each tensor in the prompt is independent of each other, and the embodiments of the present application do not limit this. It should be noted that the above-mentioned method of generating prompts based on probability distribution is only exemplary, and prompts can also be obtained based on other methods, and the embodiments of the present application do not limit this.

[0095] 203. The cloud server generates a model through the code and processes the plurality of second samples based on the prompt, and updates the prompt based on the processing results of the plurality of second samples and each second sample.

[0096] Among them, step 203 is also the fine-tuning stage of the code generation model. Taking the case where the number of tensors in the prompt is the same as the number of decoding layers of the code generation model as an example, the cloud server processes the second sample based on the prompt, and the process of obtaining the processing result of the second sample includes: concatenating the first tensor in the prompt with the vector corresponding to the elements at position 0 to position i-1 in the second sample, and inputting the concatenated result into the first decoding layer of the code generation model to obtain the decoding result of the first decoding layer; then concatenating the second tensor with the decoding result of the first decoding layer, and inputting the concatenated result into the second decoding layer, and so on, until the decoding result of the last decoding layer is obtained, that is, the element with the highest probability of appearing at position i. The process of predicting elements at other positions is similar and will not be repeated. After the code generation model predicts the elements at each position, it outputs a sequence composed of the predicted elements, that is, the processing result of the second sample is obtained. It should be noted that the above process is described by taking the case where the number of tensors in the prompt is the same as the number of decoding layers as an example. In some embodiments, the number of tensors in the prompt is less than the number of decoding layers, and the processing process of the second sample by each decoding layer is the same as the above process. The difference is that for the decoding layer for which there is no corresponding tensor, the input of the decoding layer is the output of the previous layer of the decoding layer, and the similarities are not repeated here.

[0097] In some embodiments, the process of updating the prompt includes: the cloud server processes a second sample based on the prompt through the code generation model to obtain an output result; the cloud server obtains the loss of this iteration based on the output result and the second sample; the cloud server determines whether the update deadline condition is met based on the loss of this iteration and the update deadline condition; if the update deadline condition is met, the update is terminated, if the update deadline condition is not met, the prompt is updated based on the loss of this iteration to obtain a new prompt, and the next iteration is performed based on the new prompt until the update deadline condition is met. Wherein, updating the prompt refers to updating each tensor in the prompt. In some embodiments, each tensor in the prompt is obtained by sampling the probability distribution satisfied by the values ​​of the constituent tensors. Accordingly, the cloud server updates the prompt based on the iterative loss. The process includes: based on the iterative loss, using non-gradient optimization methods such as grid search and random search, the mean and variance of the probability distribution are updated to update the probability distribution, and then the updated probability distribution is sampled, and the sampled values ​​are composed of new tensors to obtain the updated prompt. It should be noted that the above-mentioned process of updating the prompt is explained by taking the example of the code generation model performing one iteration based on a second sample. In some embodiments, the code generation model performs one iteration based on multiple second samples. In addition, the above-mentioned process of updating each tensor in the prompt based on the iterative loss is only exemplary. In some embodiments, there are some parameters in the code generation model for controlling tensor generation. The cloud server updates these parameters based on the iterative loss through gradient optimization to update the various tensors in the prompt. The embodiments of the present application are not limited to this.

[0098] In the above-mentioned updating process, the cloud server treats the code generation model as a black box, that is, in the process of updating the prompt, the weight of the code generation model is not updated. In some embodiments, the cloud server sends the updated prompt to the terminal, which saves and updates it. In the code generation process under the target application scenario, the terminal sends the saved updated prompt to the cloud server, and the cloud server completes the code generation task based on the prompt. In other embodiments, the cloud server sends the processing results of the code generation model for multiple second samples to the terminal, and the terminal updates the prompt based on the multiple processing results and each second sample, and saves the updated prompt. The embodiments of the present application are not limited to this.

[0099] In the above method, the training samples are function-level code snippets and natural language descriptions, which can improve the accuracy of generating function-level code snippets; the cloud server receives the second sample and prompt under the target application scenario sent by the terminal, and fine-tunes the code generation model to obtain updated prompts. Since the code generation model is deployed on the cloud server, the second sample and prompt customized by the user according to the target application scenario are sent by the terminal, and only the prompt is updated in the fine-tuning stage. While ensuring the fine-tuning capability, the online model can be decoupled from the user's private data.

[0100] Above Figure 2 What is shown is only the basic process of the training method of the code generation model based on cloud service in the embodiment of the present application. The training method of the code generation model based on cloud service is further explained below. Figure 3 is a flowchart of a training method for a code generation model based on a cloud service provided in an embodiment of the present application, such as Figure 3 As shown, taking the method executed by a cloud server as an example, the method includes the following steps 301 to 308.

[0101] 301. The cloud server filters out code snippets and natural language descriptions that do not meet data security conditions from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtains multiple code snippets and natural language descriptions that meet data security conditions.

[0102] The code snippet to be screened and the natural language description to be screened are the same as those described above. Figure 2 The same is true for step 201 of the embodiment, which will not be described in detail.

[0103] The cloud server filters out code snippets and natural language descriptions that do not meet data security conditions from multiple code snippets and natural language descriptions to be screened, that is, performs security cleaning and filtering on the code snippets and natural language descriptions to be screened, so as to remove code snippets and natural language descriptions that may have security risks, privacy leaks or open source risks from the code snippets and natural language descriptions to be screened. This process includes at least one of the following methods.

[0104] The first method is to filter out multiple code snippets and natural language descriptions that meet data security conditions and include code open source certificates (licenses) from multiple code snippets to be screened and multiple natural language descriptions to be screened.

[0105] The second method is to filter out code snippets and natural language descriptions that include security vulnerabilities from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple code snippets and natural language descriptions that meet data security conditions.

[0106] The third method: filter out code snippets and natural language descriptions including sensitive information from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple code snippets and natural language descriptions that meet data security conditions.

[0107] The fourth method is to filter out code snippets and natural language descriptions including semantic defects from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple code snippets and natural language descriptions that meet data security conditions.

[0108] It should be noted that the above four methods are introduced by taking filtering data from the code snippet level as an example. In some embodiments, the cloud server filters out code repositories that do not meet data security conditions from multiple code repositories, and uses the code snippets and natural language descriptions in the filtered code repositories as code snippets and natural language descriptions that meet data security conditions. That is, identifying code snippets and natural language descriptions that meet data security conditions from the code repositories level can improve the efficiency of data security cleaning and filtering.

[0109] In some embodiments, the cloud server deduplicates the code snippets and natural language descriptions to be screened on a file basis, that is, removes duplicate files, thereby reducing the total amount of code snippets and natural language descriptions to be screened and reducing the repetition rate of samples, thereby improving the efficiency of data processing and model training.

[0110] In the above method, the cloud server filters out code snippets and natural language descriptions that do not meet data security conditions from multiple code snippets to be screened and multiple natural language descriptions to be screened, which can ensure the security of training data and avoid possible risks from the source of training data.

[0111] 302. The cloud server determines multiple function-level first code snippets from multiple code snippets that meet data security conditions, and determines multiple first natural language descriptions corresponding to the first code snippets from multiple natural language descriptions that meet data security conditions.

[0112] Among them, the cloud server determines multiple function-level first code snippets from multiple code snippets that meet the data security conditions, including: based on function definition rules of different programming languages, identifying the first code snippet from multiple code snippets that meet the data security conditions, or, performing abstract syntax tree (AST) analysis on multiple code snippets that meet the data security conditions, identifying the first code snippet from multiple code snippets that meet the data security conditions, or, through a natural language processing method, identifying the first code snippet from multiple code snippets that meet the data security conditions, for example, through a sequence labeling method, using a trained function labeler to annotate the function-level code snippets.

[0113] In some embodiments, the cloud server performs deduplication on each identified first code snippet, that is, removes duplicate first code snippets, thereby reducing the total amount of first code snippets and the repetition rate of samples, thereby improving the efficiency of data processing and model training. Figure 4 As shown, Figure 4 It is a schematic diagram of a first code snippet provided in an embodiment of the present application.

[0114] The cloud server determines multiple first natural language descriptions corresponding to the first code snippet from multiple natural language descriptions that meet data security conditions, including: based on annotation rules and language character rules, determining multiple first natural language descriptions corresponding to the first code snippet from multiple natural language descriptions that meet data security conditions, or performing abstract syntax tree analysis on multiple natural language descriptions that meet data security conditions, and determining multiple first natural language descriptions corresponding to the first code snippet from multiple natural language descriptions that meet data security conditions; or, through a natural language processing method, determining multiple first natural language descriptions corresponding to the first code snippet from multiple natural language descriptions that meet data security conditions.

[0115] In the above method, the cloud server extracts function-level code snippets and corresponding natural language descriptions from multiple code snippets and natural language descriptions that meet data security conditions, and then can specifically train the code generation model to generate function-level code snippets, thereby improving the accuracy of generating function-level code snippets.

[0116] 303. The cloud server respectively composes each first code snippet and the first natural language description corresponding to each first code snippet into a sequence, and normalizes each sequence to obtain a plurality of first samples.

[0117] Wherein, for any first code snippet, the cloud server forms a sequence with the first code snippet and the first natural language description corresponding to the first code snippet, normalizes the sequence, and obtains a first sample. Wherein, normalizing the sequence refers to unifying the code syntax and format in the sequence. In some embodiments, the cloud server forms a sentence pair of "natural language description-function implementation code" (natural language-program language) with each first code snippet and the first natural language description corresponding to each first code snippet. In some embodiments, the cloud server adds a natural language identifier before the first natural language description in each sequence, adds a code snippet identifier before the first code snippet, and adds a sample terminator at the end of the sequence, thereby marking the position of the natural language and code snippet in the sequence, which is conducive to improving the efficiency of model training. Wherein, the natural language identifier indicates the position of the natural language description in the sequence and the language type corresponding to the natural language description (for example, Chinese, English, etc.), and the code snippet identifier indicates the position of the code snippet in the sequence and the programming language corresponding to the code snippet. By adding the code snippet identifier before the first code snippet, the code generation model can be helped to quickly learn the grammatical rules of different programming languages, which is conducive to improving the efficiency of model training and the accuracy of code generation. The embodiment of the present application is not limited to this.

[0118] The cloud server normalizes each sequence to obtain multiple first samples, including at least one of the following two methods.

[0119] The first method is to perform an abstract syntax tree analysis on each sequence to obtain an analysis result of each sequence, and based on the code syntax specification and each analysis result, perform code syntax normalization on the first code fragment in each sequence to obtain multiple first samples.

[0120] The second method: based on the code format specification, the first code snippet in each sequence is normalized to obtain multiple first samples. In some embodiments, normalizing the first code snippet includes unifying spaces and indents in the code snippet, which is not limited in the embodiments of the present application.

[0121] Reference below Figure 5 , an example is given for the first sample obtained after normalization. Figure 5 As shown, Figure 5 is a schematic diagram of a first sample provided by an embodiment of the present application, wherein the first sample is a sequence consisting of a first code fragment and a first natural language description, and the natural language identifier is " <doc>",instruct" <doc>"The sequence after is a natural language description, and the code snippet identifier is " <python>",instruct" <python>"The sequence after is a code snippet and the programming language corresponding to the code snippet is Python. The sample end character is " <eod>", indicating the end of the sequence. It should be noted that the above Figure 5 The natural language identifier, code snippet identifier, and sample terminator shown are merely exemplary. In some embodiments, each of the above identifiers may be customized according to actual needs, which is not limited in the embodiments of the present application.

[0122] In the above method, by normalizing the code syntax and / or the code format of each sequence, the samples can be normalized, thereby improving the efficiency of model training and the accuracy of code generation.

[0123] In some embodiments, after obtaining multiple first samples, the cloud server also performs security detection on the multiple first samples through sensitive information detection, security information detection and other methods to further ensure the security of the training data.

[0124] It should be noted that the above is explained by sequentially executing steps 301 to 303 to obtain multiple first samples as an example, that is, first filter out the code snippets and natural language descriptions that meet the data security conditions from the code snippets and natural language descriptions to be screened, then identify the function-level code snippets and the natural language descriptions corresponding to the function-level code snippets from the code snippets and natural language descriptions that meet the data security conditions, and finally form a sequence of the identified function-level code snippets and the corresponding natural language descriptions, and then normalize the sequence to obtain the first sample. In some embodiments, the cloud server executes at least one of the above steps 301 to 303 on the code snippets and natural language descriptions to be screened, that is, the above three steps may not be fully executed. Taking the cloud server executing the above step 301 to obtain the first sample as an example, the cloud server filters out code snippets and natural language descriptions that do not meet data security conditions from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtains multiple first code snippets and multiple first natural language descriptions, and obtains multiple first samples based on the multiple first code snippets and multiple first natural language descriptions; taking the cloud server executing the above step 302 to obtain the first sample as an example, the cloud server determines multiple first code snippets from multiple code snippets to be screened, and determines multiple first natural language descriptions corresponding to the first code snippets from multiple natural language descriptions to be screened, and obtains multiple first samples based on the multiple first code snippets and multiple first natural language descriptions; taking the cloud server executing the above step 303 to obtain the first forget-origin as an example, the cloud server respectively composes each first code snippet and the first natural language description corresponding to each first code snippet into a sequence, normalizes each sequence, and obtains multiple first samples. The embodiments of the present application do not limit this.

[0125] 304. The cloud server performs a first phase training on the code generation model based on the multiple first samples, where the loss function used in the first phase training is obtained based on causal language modeling of a third code snippet and a third natural language description output during the first phase training.

[0126] Among them, the code generation model can be an encoder-decoder model based on transformer, or a pure decoder model. The pure decoder model is, for example, a generative pre-training (GPT) model, which is not limited in the embodiments of the present application. Causal language modeling is used to determine the loss of the code generation model based on the predicted probabilities of elements at each position of each first sample. In the first stage of training, after the code generation model processes the first sample, it outputs a third code snippet and a third natural language description, and performs causal language modeling (description-CLM+function-CLM) on the output third code snippet and the third natural language description to obtain the loss function used in the first stage of training, and then determines the loss of the code generation model based on the loss function, and performs the first stage of training on the code generation model.

[0127] Figure 6 is a schematic diagram of the structure of a code generation model provided in an embodiment of the present application, such as Figure 6 As shown, the length of the first sample is N, and there is one element at position 0 to position N-1 in the first sample (" <descr>""Return""a"..."\n"), N is a positive integer greater than 1, the code generation model includes multiple decoding layers and an output layer, wherein the multiple decoding layers are used to decode the first sample from left to right, and the output layer is used to output the decoding result of the decoding layer on the first sample, " <eoc>" is the output cutoff character, which is used to truncate the output of the model. It should be noted that Figure 6 The structure of the provided code generation model is only exemplary, and the structure of the code generation model can be determined according to actual needs, which is not limited in the embodiments of the present application.

[0128] In the above method, the loss of the first stage training is determined based on the probability of the elements predicted at each position in the natural language description of each first sample and the code snippet. It can be understood that both the natural language description and the code snippet are aimed at causal language modeling (CLM), which can simultaneously train the code generation model's ability to understand natural language descriptions and generate function-level code snippets.

[0129] 305. The cloud server performs data enhancement on the multiple first samples to obtain multiple third samples, where the third samples satisfy at least one of the following: the length of the natural language description is within the first length range and the length of the code snippet is within the second length range.

[0130] Among them, the cloud server performs data enhancement on multiple first samples to obtain multiple third samples, including at least one of the following: filtering out multiple third samples whose length of natural language description is in the first length range from multiple first samples; filtering out multiple third samples whose length of code snippets is in the second length range from multiple first samples. Among them, the first length range and the second length range can be determined according to the statistical characteristics of the code snippets and natural language descriptions in the multiple first samples. Taking the filtering out of multiple third samples whose length of natural language description is in the first length range from multiple first samples as an example, if the average length of the natural language description in the multiple first samples is 30, 25 to 35 is used as the first length range, and multiple third samples whose length of natural language description is in the range of 25 to 35 are filtered out from multiple first samples. The process of filtering out the length of code snippets in the second length range is similar and will not be repeated. It should be noted that the above description of determining the first length range and the second length range according to statistical characteristics is only exemplary, and the embodiments of the present application are not limited to this.

[0131] In some embodiments, the cloud server removes the first sample in which the difference between the length of the code snippet and the length of the natural language description in the multiple first samples is greater than or equal to the target value, and the size of the target value is determined according to the statistical characteristics of the code snippets and the natural language description in the multiple first samples. For example, the average length of the code snippets in the first sample is 100, and the average length of the natural language description in the first sample is 30, then the size of the target value is determined as the difference between the two, 70. It should be noted that the above description of determining the target value according to statistical characteristics is only exemplary, and the embodiments of the present application do not limit this.

[0132] In the above method, the multiple third samples used in the second stage training are obtained by performing data enhancement on the multiple first samples by the cloud server, which can improve the quality of the samples used in the second stage training, thereby improving the generation accuracy of the code generation model.

[0133] 306. The cloud server performs a second-stage training on the code generation model trained in the first stage based on the multiple third samples. The loss function used in the second-stage training is obtained based on causal language modeling of the fourth code snippet output during the second-stage training.

[0134] Among them, in the second stage training, after processing the third sample, the code generation model outputs a fourth code snippet and a fourth natural language description, and performs causal language modeling (description-NoCLM+function-CLM) on the output fourth code snippet to obtain the loss function used in the second stage training, and then determines the loss of the code generation model based on the loss function, and performs second stage training on the code generation model.

[0135] In the above method, in the second stage, the training is only based on the output code snippet for causal language modeling, that is, only the CLM training target of the code snippet part is retained, and there is no training target for the natural language description part, which can highlight the code generation capability of the code generation model.

[0136] 307. The cloud server receives a prompt and a plurality of second samples sent by the terminal, where the second sample includes a second code snippet at a function level in a target application scenario and a second natural language description corresponding to the second code snippet.

[0137] Among them, the prompt and multiple second samples correspond to the target application scenario. When the user authorizes the terminal to send the second sample, the user can determine the code snippet and natural language description to be screened required for the second sample by selecting a folder, a packaged code file, or specifying a code warehouse uniform resource locator, and refer to the same process as steps 301 to 303 above to process the code snippet and natural language description to be screened to obtain multiple second samples. The processing process is not repeated here. In some embodiments, the cloud server desensitizes the multiple second samples and prompts received to remove sensitive information that may exist in the second samples and prompts, thereby improving the security of the training data.

[0138] 308. The cloud server generates a model through the code, processes the plurality of second samples based on the prompt, and updates the prompt based on the processing results of the plurality of second samples and each second sample.

[0139] Among them, the process of the cloud server processing multiple second samples based on the prompt through the code generation model is the same as the above step 203, which will not be repeated here. In some embodiments, the cloud server can provide a basic prompt (base prompt), so that when the terminal side does not provide a prompt, the cloud server can process multiple second samples based on the basic prompt through the code generation model, that is, so that the code generation model has a basic fine-tuning capability. In the case where the prompt is provided on the terminal side, the cloud server can superimpose (add) the prompt sent by the terminal with the basic prompt to obtain the superimposed prompt, and the cloud server processes multiple second samples based on the superimposed prompt through the code generation model, and the embodiment of the present application does not limit this. Among them, the cloud server can use a derivative-free optimization method to update the prompt, so that it can also have the ability to update the prompt without obtaining the gradient information of the code generation model.

[0140] Figure 7 is a flow chart of a fine-tuning stage provided in an embodiment of the present application, such as Figure 7 As shown, user 1 corresponds to application scenario 1, and user 2 corresponds to application scenario 2. For user 1, user 1 sends input sample 1 (that is, the second sample) and prompt 1 to the cloud server. The cloud server generates a model through code and processes input sample 1 based on prompt 1 to obtain a processing result of input sample 1. The cloud server determines loss 1 based on true value 1 (that is, input sample 1) and the processing result of input sample 1, and updates prompt 1 based on loss 1 until the update cutoff condition is reached, wherein the determination method of loss 1 and the update cutoff condition are determined by user 1 according to application scenario 1. The process of user 2 updating prompt 2 is the same as that of user 1 and will not be repeated here. It should be noted that Figure 7 The splicing of the prompt before the input sample is only exemplary, and the splicing position of the prompt can be determined according to actual needs, and the embodiment of the present application does not limit this.

[0141] Figure 8 is a schematic diagram of a prompt update process provided in an embodiment of the present application, such as Figure 8 As shown, the code generation model performs the first iteration based on prompt 1 and obtains output result 1 (that is, Figure 7 ); the cloud server obtains the loss value of the first iteration based on the output result 1 and the true value 1, and determines whether the update deadline condition is met based on the loss value of the first iteration and the judgment standard (that is, the update deadline condition); if the judgment standard is met, the update is terminated; if the judgment standard is not met, the prompt is updated based on the loss of the first iteration to obtain an updated prompt, and the next iteration is performed based on the updated prompt until the judgment standard is met.

[0142] In the above method, the code generation model is fine-tuned through the second sample and prompts in the target application scenario, so that the code generation model can be adapted to different application scenarios with less data, which can improve the efficiency of model training and save computing resources.

[0143] It should be noted that the above step 308 is explained by taking the updating of the prompt by the cloud server as an example. In some embodiments, the cloud server sends the processing results of multiple second samples to the terminal, and the terminal updates the prompt, that is, the calculation of the loss for updating the prompt is performed on the user side, and the user can update the prompt belonging to him / her by himself / herself, so that the updating process of the prompt is invisible to the server side, which is conducive to protecting the user's data security and avoiding privacy leakage. Among them, the terminal can update the prompt by adopting methods such as gradient-free optimization, so that the user can also have the ability to update the prompt when the gradient information of the code generation model cannot be obtained. In addition, the generation method and optimization method of the prompt are determined by the target application scenario or the user, and the embodiments of the present application do not limit this.

[0144] It should be noted that the above steps 301 to 308 are achieved by performing two-stage training and fine-tuning on the code generation model, so that the code generation model has the ability to generate the following code snippet based on the previous code snippet and natural language description, that is, the code generation scenario. In some embodiments, similar to the above method, the model can be trained to have the ability to generate the following natural language description based on the previous code snippet, that is, the method provided in this application can be applied to text generation scenarios, for example, comment generation, test case generation, etc.

[0145] In summary, the quality control of training samples is achieved by performing security filtering on the screened code snippets and natural language descriptions, identifying function-level code snippets, and constructing sentence pair samples. In addition, the data input method is reorganized so that the samples used in the second stage of training are obtained by enhancing the samples used in the first stage of training, which can achieve code generation with higher accuracy and lower security and privacy risks. Furthermore, in customized scenarios, the user-customized prompts are decoupled from the online model, and a common code generation model is separated to avoid the leakage of user data caused by simultaneous fine-tuning of the online model by multiple users. In addition, since the computational complexity of updating the prompts is much smaller than that of the server-side model, it can save user computing resources and server-side computing resources. Overall, it will not cause code generation delays, and will not affect the user experience while ensuring the fine-tuning accuracy.

[0146] The above introduces the training method of the cloud service-based code generation model in the embodiment of the present application. The following introduces a cloud service-based code generation method provided by the embodiment of the present application. Fig. 9 is a flowchart of a code generation method based on cloud services provided in an embodiment of the present application, such as Fig. 9 As shown, the method is described by taking the execution of the method by a cloud server as an example. The method includes the following steps 901 to 903.

[0147] 901. The cloud server receives a code generation request sent by the terminal, and the code generation request carries a code generation task description and a prompt under the target application scenario. The code generation task description includes a function signature and a natural language description of the code snippet to be generated. The prompt includes multiple tensors, each tensor corresponds to a decoding layer of the code generation model, and each tensor is used to be spliced ​​with the output of the previous layer of the decoding layer corresponding to the tensor to serve as the input of the corresponding decoding layer.

[0148] Among them, the function signature is used to indicate the function name and parameters of the code snippet to be generated, and the natural language description is used to indicate the function of the code snippet to be generated. The cloud server provides code generation services, and the terminal side user can obtain the code generation services provided by the cloud server in the form of free, subscription and license, buyout, application program interface (API), cloud service, etc., which is not limited in the embodiments of the present application. In some embodiments, after the cloud server obtains the code generation task description, the function signature and natural language description in the code generation task description are organized in the form of a sentence pair of "natural language description-function implementation code", and the specific form is the same as step 303, which will not be repeated here. Fig.10 is a schematic diagram of a code generation task description provided by an embodiment of the present application, such as Fig.10 As shown, Fig.10 The code generation task description shown includes the function signature and function annotation of the code snippet to be generated (that is, the natural language description of the code snippet to be generated). It should be noted that Fig.10 The code generation task description shown in is only exemplary, and the embodiments of the present application do not limit the specific content of the code generation task description. In other embodiments, the code generation task description is converted by the terminal into a sentence pair form of "natural language description-function implementation code", and the code generation task description and prompt in the sentence pair form are sent to the cloud server, wherein the terminal obtains the code generation task description in response to the code generation task being triggered, and sends the code generation task description. In the above method, the terminal sends the converted code generation task description to the cloud server, which can reduce the computing pressure of the cloud server.

[0149] In some embodiments, the code generation task description includes the function signature, partial function implementation code and function annotation of the code snippet to be generated, that is, the user has manually edited part of the function implementation code. The code generation model can decode the code generation task description and generate subsequent function implementation code, that is, complete the code completion.

[0150] 902. The cloud server decodes the code generation task description based on the prompt through the code generation model to obtain at least one fifth code fragment.

[0151] Among them, the code generation model can predict the elements with the highest probability of appearing at each position of the following text based on the existing previous text in the code generation task description, and output a sequence composed of each element, that is, a code snippet. In some embodiments, the code generation model determines multiple elements with a probability of appearing in the first target number of digits at the target position, and uses any one of the multiple elements as the predicted output at the target position, and outputs a sequence composed of the predicted outputs at each position, that is, to obtain the fifth code snippet, so that for the same code generation task description, multiple code snippets can be obtained by decoding through the code generation model multiple times, and each code snippet obtained is random. In the above method, by randomly selecting an element with a higher probability of appearing as the output at the target position, multiple different code snippets can be generated for user selection while ensuring the accuracy of the generated code snippet.

[0152] In some embodiments, the code generation request also carries a generation mode identifier, which indicates the generation mode of the code snippet. The cloud server decodes the code generation task description based on the prompt through the code generation model to obtain at least one fifth code snippet, including: if the generation mode indicated by the generation mode identifier is function-level code generation, the code generation task description is decoded based on the prompt through the code generation model, and the generated function-level code snippet is output; if the generation mode indicated by the generation mode identifier is non-function-level code generation, in the process of decoding the code generation task description based on the prompt through the code generation model, if the number of output line breaks reaches the target number, the generated non-function-level code snippet is output. Among them, generating non-function-level code snippets includes generating single-line code snippets and generating multi-line code snippets, and the target number of lines can be determined according to actual needs, and the embodiment of the present application does not limit this. In the above method, the code generation model generates function-level or non-function-level code snippets based on the generation mode identifier, which can meet the different needs of users for the generated code snippets, while ensuring the accuracy of code generation, improving the flexibility of code generation, and helping to improve user experience.

[0153] 903. The cloud server determines at least one candidate code segment from the at least one fifth code segment based on the code quality condition, and sends the candidate code segment to the terminal.

[0154] Among them, the cloud server determines at least one candidate code snippet from at least one fifth code snippet based on the code quality condition, including: filtering out code snippets including security vulnerabilities or sensitive information from at least one fifth code snippet to obtain at least one candidate code snippet, or removing duplicate code snippets from at least one fifth code snippet to obtain at least one candidate code snippet, and so on. Fig.11 is a schematic diagram of a candidate code snippet provided in an embodiment of the present application, such as Fig.11 As shown, Fig.11 The code generation model is shown Fig.10 The code generation task description shown is processed and three candidate code snippets are obtained after screening based on code quality conditions.

[0155] It should be noted that in the above process of filtering out code snippets that include security vulnerabilities or sensitive information from at least one fifth code snippet, the security risk filtering rules used are only applicable to filtering the fifth code snippet, that is, only applicable to local code snippet scanning. In some embodiments, if a user develops an engineering project through a network cloud service, after the code snippet is generated, the cloud server scans all codes in the engineering project where the code snippet is located, that is, performs a global associated code scan to further reduce code security risks. This is not limited in the embodiments of the present application. In the above method, candidate code snippets are screened out based on code security conditions, that is, candidate screening of the security of the generated code snippets is performed using a security check method, which can further ensure the quality and security of the generated code snippets.

[0156] In some embodiments, the cloud server sends the candidate code snippets to the terminal. After the terminal receives the candidate code snippets, it displays the candidate code snippets. The user accepts or rejects the candidate code snippets displayed by the terminal by interacting with the terminal. In some embodiments, the cloud server sorts the candidate code snippets from high to low according to compilability, and sends the sorted code snippets to the terminal, which displays the sorted code snippets, thereby facilitating the user to select the generated code snippets. The user does not need to compare and accept multiple code snippets back and forth, which can reduce the user's operating costs and improve the user's operating experience. Figure 12 to Figure 14 This paper introduces the process of receiving candidate code snippets recommended by the terminal through interaction between the user and the terminal. Taking the code snippet generated by the function level as an example, Fig.12 is a schematic diagram of generating a function-level code snippet provided by an embodiment of the present application, such as Fig.12 As shown, there are 3 candidate code snippets in total. The terminal displays a code generation toolbar in the code editing interface and displays one of the 3 candidate code snippets to the user. According to the prompt of the code generation toolbar, if the user clicks "Alt+[", the terminal displays all 3 candidate code snippets in the code editing interface; if the user clicks "Alt+U", the terminal displays another candidate code snippet in the code editing interface; if the user clicks "Tab", the terminal receives the currently displayed candidate code snippet, which is a function-level code snippet. Take the generated model as a function-level code snippet as an example, Fig.13 is a schematic diagram of generating a single-line non-function-level code snippet provided by an embodiment of the present application, such as Fig.13 As shown, Fig.13 A candidate single-line non-function-level code snippet is shown; Fig.14 is a schematic diagram of generating a multi-line non-function-level code snippet provided by an embodiment of the present application, such as Fig.14 As shown, Fig.14 Displays a candidate multi-line non-function-level code snippet. User-based Fig.13 or Fig.14 The process of interaction between the interface and the terminal shown is similar to Fig.12 Similarly, I will not elaborate on it.

[0157] In some embodiments, the terminal stores the code snippet accepted by the user, and uses the complete code snippet and natural language description corresponding to the code snippet as samples in the model training fine-tuning stage (that is, the second sample), optimizes the samples in the fine-tuning stage, and thereby improves the code generation accuracy of the code generation model.

[0158] Below through Fig.15 The process shown in the above steps 901 to 903 is described by way of example, that is, the reasoning stage is described. Fig.15 is a flowchart of a code generation method provided by an embodiment of the present application, such as Fig.15 As shown, the user inputs the function signature and function comment through the code development interface on the IDE, the terminal obtains the function signature and function comment input by the user (that is, obtains the context) and the code generation mode identifier set by the user, and performs code generation (that is, code inference) through the code generation model deployed on the cloud server; the function signature, function comment, code generation mode identifier and prompt sent by the terminal are obtained through the cloud server, the function-level or non-function-level code snippet is generated through the code generation model, and the code snippet generated by the code generation model (that is, the original code / text generated by the model) is screened for security vulnerabilities and filtered for open source protocols to obtain safe code / text candidates (that is, candidate code snippets), and the generated candidate code snippets are sent to the terminal; the terminal displays the received candidate code snippets, and the user accepts any candidate code snippet by interacting with the terminal; the terminal stores the code snippet received by the user, and uses the code snippet to optimize the fine-tuning sample in the customized scenario (that is, local optimization data), and then further updates the prompt (that is, prompt fine-tuning).

[0159] Below through Figure 16 to Figure 17 The training method and the process of the code generation method of the above-mentioned cloud service-based code generation model are illustrated by examples. Fig.16 is a schematic diagram of the stages of a cloud service-based code generation method provided in an embodiment of the present application, such as Fig.16 As shown in the figure, the implementation process of the code generation method based on cloud services includes the model training stage, the fine-tuning stage and the reasoning stage. In the model training stage, the cloud server constructs available and secure training samples from the corpus and organizes them into the form of "natural language description-function implementation code" sentence pairs. Multi-stage pre-training is performed on the deep model to obtain the model pre-training weights that can be used for reasoning. In the fine-tuning stage where specific scenario optimization is required, the black box fine-tuning method is adopted, and the pre-training weights are used as a black box method. The prompt fine-tuning technology of non-gradient optimization can separate the prompt from the pre-training weight. In the reasoning stage, the user calls the reasoning access service through the remote API or integrated method in the integrated development tool, and extracts the "natural language description-function implementation code" from the context content on the user side and calls the reasoning interface of the model. On the server side, the model infers the code generation task description input by the user based on the prompt, and obtains multiple candidates for generating code snippets. After the code candidates are screened for security vulnerabilities and other post-processing methods, the available candidates are retained and fed back to the user.

[0160] Above Fig.16 As an example, the implementation process of the code generation method based on cloud services is introduced. Figure 1 From the implementation environment introduced, it can be seen that different functions such as processing the code snippets to be screened and the natural language description to obtain the samples required for the code generation model training, training the code generation model, deploying the trained code generation model, providing online code generation services for the terminal, and sending the generated code snippets to the terminal can be completed by different servers and their associated devices. Fig.17 Another implementation environment for realizing the above-mentioned cloud service-based code generation method is introduced. Fig.17 is a schematic diagram of an implementation environment of another cloud service-based code generation method provided in an embodiment of the present application, such as Fig.17 As shown, the implementation environment includes a data acquisition and processing device, a server 1, a server 2 and a terminal. The data collection device obtains the code snippets and natural language descriptions to be screened through various means, such as manual input, network search, etc., and performs data security cleaning and filtering, file-level data deduplication, function-level code snippet extraction, function-level code deduplication and natural language-function implementation code extraction, etc., to obtain multiple first samples. The process is the same as the process shown in the above steps 301 to 303, which will not be repeated. The data acquisition and processing device transmits the multiple first samples to the server 1. After receiving the multiple first samples, the server 1 performs the first-stage training of the code generation model based on the multiple first samples to obtain the code generation model after the first-stage training, performs data enhancement on the multiple first samples to obtain multiple third samples, and performs the second-stage training of the code generation model based on the multiple third samples to obtain the code generation model after the second-stage training. The process is the same as the process shown in the above steps 304 to 306, which will not be repeated. The server device 1 sends the weight of the code generation model to the server device 2, and the server device 2 performs the code generation and code completion inference tasks. The terminal stores the second sample and prompt required for the fine-tuning stage, and fine-tunes the prompt in combination with the model weights saved in the server device 2, updates the prompt, and saves the updated prompt in the terminal. This process is the same as the process shown in the above steps 307 to 308, and will not be repeated. In the code generation task in the user-defined scenario, the terminal obtains the code generation task description entered by the user through the IDE, sends the code generation task description and prompt to the server 2 for processing, and generates a code snippet; the server 2 screens the generated code snippet based on the code quality conditions, which can also be understood as the server 2 providing a code quality assurance service to obtain candidate code snippets that meet the code quality conditions, and sends the candidate code snippets to the terminal. This process is the same as the process shown in the above steps 901 to 903, and will not be repeated. Among them, the task of server 2 can also be completed directly by server 1.

[0161] In summary, after the cloud server receives the code generation task description and prompt sent by the terminal, the code generation model decodes the code generation task description based on the prompt to obtain at least one fifth code snippet, which can improve the accuracy of the generated function-level code snippet. In addition, since the prompt is updated and saved by the terminal side, the privatization of user weights and user data can be achieved, thereby improving user data security and avoiding the risk of code privacy leakage.

[0162] Fig.18 It is a structural block diagram of a training device for a code generation model based on a cloud service provided in an embodiment of the present application, which is applied to a cloud server. The device includes a training module 1801, a receiving module 1802 and an updating module 1803.

[0163] A training module 1801 is used to train a code generation model based on a plurality of first samples, wherein the first samples include a first code snippet at a function level and a first natural language description corresponding to the first code snippet, and the code generation model includes a plurality of decoding layers;

[0164] A receiving module 1802 is used to receive a prompt and multiple second samples sent by a terminal, where the second sample includes a second code snippet at the function level in a target application scenario and a second natural language description corresponding to the second code snippet, and the prompt includes multiple tensors, each tensor corresponds to a decoding layer of a code generation model, and each tensor is used to be spliced ​​with an output of a layer above the decoding layer corresponding to the tensor to serve as an input of the corresponding decoding layer;

[0165] The updating module 1803 is used to generate a model through the code and process the plurality of second samples based on the prompt, and update the prompt based on the processing results of the plurality of second samples and each second sample.

[0166] Optionally, the training module 1801 is used to:

[0167] Based on multiple first samples, a first-stage training is performed on the code generation model, and the loss function used in the first-stage training is obtained based on causal language modeling of a third code snippet and a third natural language description output during the first-stage training; data enhancement is performed on the multiple first samples to obtain multiple third samples, and the third samples satisfy at least one of the length of the natural language description being within a first length range and the length of the code snippet being within a second length range; based on the multiple third samples, a second-stage training is performed on the code generation model after the first-stage training, and the loss function used in the second-stage training is obtained based on causal language modeling of a fourth code snippet output during the second-stage training.

[0168] Optionally, the device further comprises a screening module, wherein the screening module comprises at least one of the following:

[0169] A filtering unit, used to filter out code snippets and natural language descriptions that do not meet data security conditions from the multiple code snippets to be filtered and the multiple natural language descriptions to be filtered, to obtain multiple first code snippets and multiple first natural language descriptions, and to obtain multiple first samples based on the multiple first code snippets and the multiple first natural language descriptions;

[0170] a determining unit, configured to determine a plurality of function-level first code snippets from a plurality of code snippets to be screened, determine a plurality of first natural language descriptions corresponding to the first code snippets from a plurality of natural language descriptions to be screened, and obtain a plurality of first samples based on the plurality of first code snippets and the plurality of first natural language descriptions;

[0171] The normalization unit is used to respectively form sequences with the first natural language descriptions corresponding to the first code fragments, and normalize the sequences to obtain a plurality of first samples.

[0172] Optionally, the filtration unit is used for at least one of the following:

[0173] Filter out a first code snippet and a first natural language description including a code open source certificate from multiple code snippets to be screened and multiple natural language descriptions to be screened; filter out code snippets and natural language descriptions including security vulnerabilities from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple first code snippets and multiple first natural language descriptions; filter out code snippets and natural language descriptions including sensitive information from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple first code snippets and multiple first natural language descriptions; filter out code snippets and natural language descriptions including semantic defects from multiple code snippets to be screened and multiple natural language descriptions to be screened, and obtain multiple first code snippets and multiple first natural language descriptions.

[0174] Optionally, the determining unit is used for any of the following:

[0175] Based on function definition rules of different programming languages, a first code snippet at the function level is identified from multiple code snippets to be screened; an abstract syntax tree analysis is performed on the multiple code snippets to be screened, and the first code snippet at the function level is identified from the multiple code snippets to be screened; and the first code snippet at the function level is identified from the multiple code snippets to be screened through a natural language processing method.

[0176] Optionally, the determining unit is used for any of the following:

[0177] Based on annotation rules and language character rules, multiple first natural language descriptions corresponding to each first code snippet are determined from multiple natural language descriptions to be screened; abstract syntax tree analysis is performed on the multiple natural language descriptions to be screened, and the first natural language descriptions corresponding to each first code snippet are determined from the multiple natural language descriptions to be screened; through a natural language processing method, the first natural language description corresponding to each first code snippet is determined from the multiple natural language descriptions to be screened.

[0178] Optionally, the normalization unit is used for at least one of the following:

[0179] An abstract syntax tree analysis is performed on each sequence to obtain an analysis result of each sequence; based on the code syntax specification and each analysis result, code syntax normalization is performed on the first code fragment in each sequence; based on the code format specification, code format normalization is performed on the first code fragment in each sequence to obtain multiple first samples.

[0180] Among them, the training module 1801, the receiving module 1802 and the updating module 1803 can all be implemented by software, or can be implemented by hardware. Exemplarily, the implementation of the training module 1801 is introduced below by taking the training module 1801 as an example. Similarly, the implementation of the receiving module 1802 and the updating module 1803 can refer to the implementation of the training module 1801.

[0181] As an example of a software functional unit, the training module 1801 may include code running on a computing instance. Among them, the computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Furthermore, the above-mentioned computing instance may be one or more. For example, the training module 1801 may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code can be distributed in the same region (region) or in different regions. Furthermore, the multiple hosts / virtual machines / containers used to run the code can be distributed in the same availability zone (AZ) or in different AZs, each AZ including one data center or multiple data centers with close geographical locations. Among them, usually a region can include multiple AZs.

[0182] Similarly, multiple hosts / virtual machines / containers used to run the code can be distributed in the same virtual private cloud (VPC) or in multiple VPCs. Usually, a VPC is set up in a region. For cross-region communication between two VPCs in the same region and between VPCs in different regions, a communication gateway needs to be set up in each VPC to achieve interconnection between VPCs through the communication gateway.

[0183] As an example of a hardware functional unit, the training module 1801 may include at least one computing device, such as a server, etc. Alternatively, the training module 1801 may also be a device implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD), etc. The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.

[0184] The multiple computing devices included in the training module 1801 can be distributed in the same region or in different regions. The multiple computing devices included in the training module 1801 can be distributed in the same AZ or in different AZs. Similarly, the multiple computing devices included in the training module 1801 can be distributed in the same VPC or in multiple VPCs. The multiple computing devices can be any combination of computing devices such as servers, ASICs, PLDs, CPLDs, FPGAs, and GALs.

[0185] It should be noted that, in other embodiments, the steps that the above modules are responsible for implementing can be specified as needed, and the above modules can respectively implement different steps in the above-mentioned cloud service-based code generation model training method to realize all the functions of the above-mentioned device. That is, the cloud service-based code generation model training device provided in the above embodiment only uses the division of the above-mentioned functional modules as an example when implementing the cloud service-based code generation model training method. In actual applications, the above-mentioned functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the device provided in the above embodiment belongs to the same concept as the corresponding method embodiment. The specific implementation process is detailed in the method embodiment, which will not be repeated here.

[0186] Fig.19 It is a structural block diagram of a cloud service-based code generation device provided in an embodiment of the present application, which is applied to a cloud server. The device includes a receiving module 1901, a decoding module 1902 and a sending module 1903.

[0187] A receiving module 1901 is used to receive a code generation request sent by a terminal, the code generation request carries a code generation task description and a prompt in a target scenario, the code generation task description includes a function signature and a natural language description of a code snippet to be generated, and the prompt includes multiple tensors, each tensor corresponds to a decoding layer of a code generation model, and each tensor is used to be spliced ​​with the output of a layer above the decoding layer corresponding to the tensor to serve as an input of the corresponding decoding layer;

[0188] A decoding module 1902 is used to decode the code generation task description based on the prompt through the code generation model to obtain at least one fifth code fragment;

[0189] The sending module 1903 is configured to determine at least one candidate code segment from at least one fifth code segment based on the code quality condition, and send the candidate code segment to the terminal.

[0190] Optionally, the decoding module 1902 is used to:

[0191] If the generation mode indicated by the generation mode identifier is function-level code generation, the code generation task description is decoded based on prompts through the code generation model, and the generated function-level code snippet is output; if the generation mode indicated by the generation mode identifier is non-function-level code generation, in the process of decoding the code generation task description based on prompts through the code generation model, if the number of output line breaks reaches the target number, the generated non-function-level code snippet is output.

[0192] Optionally, the sending module 1903 is used for at least one of the following:

[0193] Filter out code fragments including security holes or sensitive information from at least one fifth code fragment to obtain at least one candidate code fragment; remove duplicate code fragments from at least one fifth code fragment to obtain at least one candidate code fragment.

[0194] Among them, the receiving module 1901, the interface module 1902 and the sending module 1903 can all be implemented by software, or can be implemented by hardware. Exemplarily, the implementation of the receiving module 1901 is introduced below by taking the receiving module 1901 as an example. Similarly, the implementation of the interface module 1902 and the sending module 1903 can refer to the implementation of the receiving module 1901.

[0195] As an example of a software functional unit, the receiving module 1901 may include code running on a computing instance. Fig.18 The same is true for the training module 1801 in , which will not be described in detail.

[0196] It should be noted that, in other embodiments, the steps that the above modules are responsible for implementing can be specified as needed, and all the functions of the above device can be realized by respectively implementing different steps in the above cloud service-based code generation method through the above modules. That is, the cloud service-based code generation device provided in the above embodiment only uses the division of the above functional modules as an example when implementing the cloud service-based code generation method. In actual applications, the above functions can be assigned to different functional modules as needed, that is, the internal structure of the device can be divided into different functional modules to complete all or part of the functions described above. In addition, the device provided in the above embodiment belongs to the same concept as the corresponding method embodiment. The specific implementation process is detailed in the method embodiment, which will not be repeated here.

[0197] The present application also provides a computing device 2000 . Fig. 20 is a schematic diagram of a computing device provided in an embodiment of the present application, such as Fig. 20 As shown, computing device 2000 includes: bus 2001, processor 2002, memory 2003 and communication interface 2004. Processor 2002, memory 2003 and communication interface 2004 communicate with each other through bus 2001. Computing device 2000 can be a server or a terminal device. It should be understood that the present application does not limit the number of processors and memories in computing device 2000.

[0198] The bus 2001 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Fig. 20 The bus 2001 may include a path for transmitting information between various components of the computing device 2000 (eg, the memory 2003, the processor 2002, and the communication interface 2004).

[0199] The processor 2002 may include any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).

[0200] The memory 2003 may include a volatile memory, such as a random access memory (RAM). The memory 2003 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard disk drive (HDD), or a solid state drive (SSD).

[0201] The memory 2003 stores executable program codes, and the processor 2002 executes the executable program codes to respectively implement the functions of the aforementioned training module 1801, the receiving module 1802, and the updating module 1803, thereby implementing the training method and the code generation method of the code generation model based on the cloud service. That is, the memory 2003 stores instructions for executing the training method of the code generation model based on the cloud service.

[0202] Alternatively, the memory 2003 stores executable program codes, and the processor 2002 executes the executable program codes to respectively implement the functions of the aforementioned receiving module 1901, the interface module 1902, and the sending module 1903, thereby implementing the cloud service-based code generation method. That is, the memory 2003 stores instructions for executing the cloud service-based code generation method.

[0203] The communication interface 2004 uses a transceiver module such as, but not limited to, a network interface card or a transceiver to implement communication between the computing device 2000 and other devices or communication networks.

[0204] The embodiment of the present application also provides a computing device cluster. Fig.21 is a schematic diagram of a computing device cluster provided in an embodiment of the present application, such as Fig.21 As shown, the computing device cluster includes at least one computing device 2000. The memory 2003 in one or more computing devices 2000 in the computing device cluster may store the same instructions for executing the training method of the cloud service-based code generation model.

[0205] In some possible implementations, the memory 2003 of one or more computing devices 2000 in the computing device cluster may also store partial instructions for executing the training method of the code generation model based on the cloud service. In other words, the combination of one or more computing devices 2000 can jointly execute the instructions for executing the training method of the code generation model based on the cloud service.

[0206] It should be noted that the memory 2003 in different computing devices 2000 in the computing device cluster can store different instructions, which are respectively used to execute part of the functions of the training device for the code generation model based on the cloud service. That is, the instructions stored in the memory 2003 in different computing devices 2000 can implement the functions of one or more modules in the training module 1801, the receiving module 1802 and the updating module 1803.

[0207] It should be understood that Fig.21 The functions of the computing device 2000 shown in FIG. 2000 may also be performed by multiple computing devices 2000 .

[0208] In some possible implementations, one or more computing devices in the computing device cluster may be connected via a network, which may be a wide area network or a local area network, etc. Fig. 22 A possible implementation is shown. Fig. 22 is a schematic diagram of a possible implementation of a computing device cluster provided in an embodiment of the present application, such as Fig. 22 As shown, two computing devices 2000 are connected via a network. Specifically, they are connected to the network via a communication interface in each computing device.

[0209] The present application embodiment also provides another computing device cluster. The connection relationship between the computing devices in the computing device cluster can be similar to that of Fig.21 or Fig. 22 The difference is that the memory 2003 in one or more computing devices 2000 in the computing device cluster may store the same instructions for executing the code generation method based on the cloud service.

[0210] In some possible implementations, the memory 2003 of one or more computing devices 2000 in the computing device cluster may also store partial instructions for executing the code generation method based on cloud services. In other words, the combination of one or more computing devices 2000 may jointly execute instructions for executing the code generation method based on cloud services.

[0211] It should be noted that the memory 2003 in different computing devices 2000 in the computing device cluster can store different instructions, which are respectively used to execute part of the functions of the code generation device based on cloud services. That is, the instructions stored in the memory 2003 in different computing devices 2000 can implement the functions of one or more modules of the receiving module 1901, the decoding module 1902 and the sending module 1903.

[0212] The present application implements a computer program product including instructions, which may be software or a program product including instructions that can be run on a computing device or stored in any available medium. When the computer program product is run on at least one computing device, the at least one computing device is enabled to perform the training method of the cloud service-based code generation model provided in the above embodiment, or to execute the cloud service-based code generation method.

[0213] The embodiment of the present application provides a computer-readable storage medium, which can be any available medium that a computing device can store or a data storage device such as a data center containing one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk). The computer-readable storage medium includes instructions, and when the instructions are executed by a computing device cluster, the computing device cluster executes the training method of the cloud service-based code generation model provided in the above embodiment, or executes the cloud service-based code generation method.

[0214] It should be noted that the information (including but not limited to user device information, user personal information, etc.), data (including but not limited to data used for analysis, stored data, displayed data, etc.) and signals involved in this application are all authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions. For example, the code snippets involved in this application are all obtained with full authorization.

[0215] Those of ordinary skill in the art will appreciate that the various method steps and units described in the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the steps and components of each embodiment have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Those of ordinary skill in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0216] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0217] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the unit is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interfaces, devices or units, or it can be an electrical, mechanical or other form of connection.

[0218] The unit described as a separate component may or may not be physically separated, and the component displayed as a unit may or may not be a physical unit, that is, it may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiment of the present application.

[0219] In addition, each unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or software unit.

[0220] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computing device (which can be a personal computer, a server, or a computing device, etc.) to execute all or part of the steps of the method in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc., various media that can store program codes.

[0221] In the present application, the terms "first", "second", etc. are used to distinguish between identical or similar items having substantially the same effects and functions. It should be understood that there is no logical or temporal dependency between "first", "second", and "nth", nor is there a limitation on quantity and execution order. It should also be understood that although the following description uses the terms first, second, etc. to describe various elements, these elements should not be limited by the terms. These terms are only used to distinguish one element from another. For example, without departing from the scope of the various examples, a first sample may be referred to as a second sample, and similarly, a second sample may be referred to as a first sample. Both the first sample and the second sample may be samples, and in some cases, may be separate and different samples.

[0222] In this application, the term "at least one" means one or more, and the term "multiple" means two or more, for example, multiple second samples means two or more second samples. The terms "system" and "network" are often used interchangeably herein.

[0223] It should also be understood that the term "if" may be interpreted to mean "when" or "upon" or "in response to determining" or "in response to detecting." Similarly, the phrase "if it is determined that ..." or "if [a stated condition or event] is detected" may be interpreted to mean "upon determining that ..." or "in response to determining that ..." or "upon detecting [a stated condition or event]" or "in response to detecting [a stated condition or event]," depending on the context.

[0224] The above description is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of various equivalent modifications or replacements within the technical scope disclosed in the present application, and these modifications or replacements should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

[0225] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer program instructions. When the computer program instructions are loaded and executed on a computer, the process or function in accordance with the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices.

[0226] The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer program instructions may be transmitted from one website, computer, server or data center to another website, computer, server or data center by wired or wireless means. The computer-readable storage medium may be any available medium that can be accessed by a computer or a data storage device such as a server or data center that includes one or more available media integrated therein. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a magnetic tape), an optical medium (e.g., a digital video disc (DVD), or a semiconductor medium (e.g., a solid-state drive)), etc.

[0227] A person skilled in the art will understand that all or part of the steps to implement the above embodiments may be accomplished by hardware or by instructing related hardware through a program, and the program may be stored in a computer-readable storage medium, and the above-mentioned storage medium may be a read-only memory, a disk or an optical disk, etc.

[0228] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, a person skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.< / eoc> < / descr> < / eod> < / python> < / python> < / doc> < / doc>

Claims

1. A training method for a code generation model based on cloud services, characterized in that: Applied to a cloud server, the method comprises: Training a code generation model based on a plurality of first samples, wherein the first samples include a first code snippet at a function level and a first natural language description corresponding to the first code snippet, and the code generation model includes a plurality of decoding layers; A prompt and multiple second samples sent by a receiving terminal, wherein the second sample includes a second code snippet at the function level in a target application scenario and a second natural language description corresponding to the second code snippet, the prompt includes multiple tensors, each of the tensors corresponds to a decoding layer of the code generation model, and each of the tensors is used to be spliced ​​with the output of a layer above the decoding layer corresponding to the tensor to serve as an input of the corresponding decoding layer; The plurality of second samples are processed based on the prompt through the code generation model, and the prompt is updated based on the processing results of the plurality of second samples and each second sample.

2. The method according to claim 1, characterized in that The step of training a code generation model based on a plurality of first samples comprises: Based on the plurality of the first samples, a first-stage training is performed on the code generation model, wherein the loss function used in the first-stage training is obtained based on causal language modeling of a third code snippet and a third natural language description output during the first-stage training process; Performing data enhancement on the multiple first samples to obtain multiple third samples, wherein the third samples satisfy at least one of the following conditions: a length of a natural language description is within a first length range and a length of a code snippet is within a second length range; Based on the plurality of the third samples, the code generation model after the first stage training is subjected to second stage training, and the loss function adopted in the second stage training is obtained based on causal language modeling of the fourth code snippet output during the second stage training.

3. The method according to claim 1, characterized in that The method further comprises at least one of the following: Filtering out code snippets and natural language descriptions that do not meet data security conditions from a plurality of code snippets to be screened and a plurality of natural language descriptions to be screened, obtaining a plurality of first code snippets and a plurality of first natural language descriptions, and obtaining a plurality of first samples based on the plurality of first code snippets and the plurality of first natural language descriptions; Determine a plurality of first code snippets from the plurality of code snippets to be screened, determine a plurality of first natural language descriptions corresponding to the first code snippets from the plurality of natural language descriptions to be screened, and obtain a plurality of first samples based on the plurality of first code snippets and the plurality of first natural language descriptions; Each of the first code snippets and the first natural language description corresponding to each of the first code snippets are respectively composed into sequences, and each of the sequences is normalized to obtain a plurality of the first samples.

4. The method according to claim 3, characterized in that The filtering out code snippets and natural language descriptions that do not meet the data security condition from the plurality of code snippets to be screened and the plurality of natural language descriptions to be screened to obtain the plurality of first code snippets and the plurality of first natural language descriptions includes at least one of the following: Filtering out the first code snippet and the first natural language description including the code open source certificate from the multiple code snippets to be screened and the multiple natural language descriptions to be screened; Filtering out code snippets and natural language descriptions including security vulnerabilities from the plurality of code snippets to be screened and the plurality of natural language descriptions to be screened, to obtain a plurality of first code snippets and a plurality of first natural language descriptions; Filtering out code snippets and natural language descriptions including sensitive information from the plurality of code snippets to be screened and the plurality of natural language descriptions to be screened, to obtain a plurality of first code snippets and a plurality of first natural language descriptions; The code snippets and natural language descriptions including semantic defects are filtered out from the multiple code snippets to be screened and the multiple natural language descriptions to be screened, so as to obtain multiple first code snippets and multiple first natural language descriptions.

5. The method according to claim 3, characterized in that: The determining of the plurality of first code snippets from the plurality of code snippets to be screened includes any of the following: Based on function definition rules of different programming languages, identifying the first code snippet from the plurality of code snippets to be screened; Performing an abstract syntax tree analysis on the multiple code snippets to be screened, and identifying the first code snippet from the multiple code snippets to be screened; The first code snippet is identified from the plurality of code snippets to be screened by using a natural language processing method.

6. The method according to claim 3, characterized in that The determining of a plurality of first natural language descriptions corresponding to the first code snippet from the plurality of natural language descriptions to be screened includes any of the following: Based on the annotation rule and the language character rule, determining a plurality of the first natural language descriptions corresponding to the first code snippet from the plurality of the natural language descriptions to be screened; Performing an abstract syntax tree analysis on the multiple natural language descriptions to be screened, and determining a plurality of the first natural language descriptions corresponding to the first code snippet from the multiple natural language descriptions to be screened; By using a natural language processing method, a plurality of first natural language descriptions corresponding to the first code snippet are determined from a plurality of natural language descriptions to be screened.

7. The method according to claim 3, characterized in that The step of normalizing each of the sequences to obtain a plurality of the first samples includes at least one of the following: Performing an abstract syntax tree analysis on each of the sequences to obtain an analysis result of each of the sequences, and performing code syntax normalization on the first code snippet in each of the sequences based on a code syntax specification and each of the analysis results to obtain a plurality of the first samples; Based on a code format specification, the code format of the first code fragment in each of the sequences is normalized to obtain a plurality of the first samples.

8. A code generation method based on cloud service, characterized in that: Applied to a cloud server, the method comprises: A code generation request sent by a receiving terminal is carried by the code generation request. The code generation request carries a code generation task description and a prompt in a target scenario. The code generation task description includes a function signature and a natural language description of a code snippet to be generated. The prompt includes a plurality of tensors. Each of the tensors corresponds to a decoding layer of a code generation model. Each of the tensors is used to be concatenated with an output of a layer above the decoding layer corresponding to the tensor to serve as an input of the corresponding decoding layer. Decoding the code generation task description based on the prompt using the code generation model to obtain at least one fifth code fragment; Based on the code quality condition, at least one candidate code segment is determined from the at least one fifth code segment, and the candidate code segment is sent to the terminal.

9. The method according to claim 8, characterized in that The code generation request further carries a generation mode identifier, the generation mode identifier indicates a generation mode of the code snippet, and the code generation task description is decoded based on the prompt by the code generation model to obtain at least one fifth code snippet, including: If the generation mode indicated by the generation mode identifier is function-level code generation, decoding the code generation task description based on the prompt through the code generation model, and outputting a generated function-level code snippet; If the generation mode indicated by the generation mode identifier is non-function-level code generation, in the process of decoding the code generation task description through the code generation model based on the prompt, if the number of output line breaks reaches the target number, the generated non-function-level code snippet is output.

10. The method according to claim 8, characterized in that The determining, based on the code quality condition, at least one candidate code fragment from the at least one fifth code fragment comprises at least one of the following: Filtering out code snippets including security vulnerabilities or sensitive information from the at least one fifth code snippet to obtain at least one candidate code snippet; Repetitive code fragments are removed from the at least one fifth code fragment to obtain at least one candidate code fragment.

11. A training device for a code generation model based on cloud services, characterized in that: Applied to a cloud server, the device comprises: A training module, configured to train a code generation model based on a plurality of first samples, wherein the first samples include a first code snippet at a function level and a first natural language description corresponding to the first code snippet, and the code generation model includes a plurality of decoding layers; A receiving module, configured to receive a prompt and a plurality of second samples sent by a terminal, wherein the second sample comprises a second code snippet at a function level in a target application scenario and a second natural language description corresponding to the second code snippet, wherein the prompt comprises a plurality of tensors, each of which corresponds to a decoding layer of the code generation model, and each of which is used to be concatenated with an output of a layer above the decoding layer corresponding to the tensor, so as to serve as an input of the corresponding decoding layer; An updating module is used to process the plurality of second samples based on the prompt through the code generation model, and update the prompt based on the processing results of the plurality of second samples and each second sample.

12. A code generation device based on cloud service, characterized in that: Applied to a cloud server, the device comprises: A receiving module, used to receive a code generation request sent by a terminal, wherein the code generation request carries a code generation task description and a prompt in a target scenario, wherein the code generation task description includes a function signature and a natural language description of a code snippet to be generated, and the prompt includes a plurality of tensors, each of which corresponds to a decoding layer of a code generation model, and each of which is used to be spliced ​​with an output of a layer above the decoding layer corresponding to the tensor, so as to serve as an input of the corresponding decoding layer; A decoding module, configured to decode the code generation task description based on the prompt through the code generation model to obtain at least one fifth code fragment; A sending module is used to determine at least one candidate code segment from the at least one fifth code segment based on a code quality condition, and send the candidate code segment to the terminal.

13. A computing device cluster, characterized in that: comprising at least one computing device, each computing device comprising a processor and a memory; The processor of the at least one computing device is used to execute instructions stored in the memory of the at least one computing device, so that the computing device cluster executes the training method of the cloud service-based code generation model as described in any one of claims 1 to 7.

14. A computing device cluster, characterized in that: comprising at least one computing device, each computing device comprising a processor and a memory; The processor of the at least one computing device is used to execute instructions stored in the memory of the at least one computing device, so that the computing device cluster executes the cloud service-based code generation method according to any one of claims 8 to 10.

15. A computer program product comprising instructions, characterized in that When the instructions are executed by a computing device cluster, the computing device cluster executes the cloud service-based code generation model training method according to any one of claims 1 to 7.

16. A computer program product comprising instructions, characterized in that When the instructions are executed by a computing device cluster, the computing device cluster executes the cloud service-based code generation method according to any one of claims 8 to 10.

17. A computer-readable storage medium, characterized in that: The method comprises computer program instructions. When the computer program instructions are executed by a computing device cluster, the computing device cluster executes the training method of the cloud service-based code generation model as described in any one of claims 1 to 7.

18. A computer-readable storage medium, characterized in that: The method comprises computer program instructions. When the computer program instructions are executed by a computing device cluster, the computing device cluster executes the cloud service-based code generation method according to any one of claims 8 to 10.

Citation Information

Cited By

  • Business rule intelligent conversion method and device for natural language to code conversion

    CN122963558A

  • Method for generating code, and device, medium and program product

    WO2026117992A1