Code generation method, code generation model training method, and apparatus, device and medium
By generating code by retrieving the description information of the target function from the function library, the problems of resource waste and poor flexibility caused by frequent training in the existing technology are solved, and the accuracy and flexibility of code generation are improved.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- ALIBABA (CHINA) CO LTD
- Filing Date
- 2025-12-12
- Publication Date
- 2026-06-18
AI Technical Summary
Existing code generation models require frequent training to adapt to updates in programming languages, resulting in a waste of computational and human resources, and they are also inflexible.
By retrieving the description information of the target function from the function library based on the functional description information, code is generated, avoiding frequent training of the code generation model, and adapting to function changes by utilizing updates to the function library.
It improves the accuracy and flexibility of code generation, reduces the training frequency of code generation models, and saves computational and human resources.
Smart Images

Figure CN2025142272_18062026_PF_FP_ABST
Abstract
Description
Code generation, training methods, devices, equipment and media for code generation models
[0001] This disclosure claims priority to Chinese Patent Application No. 202411846076.6, filed with the China Patent Office on December 13, 2024, entitled “Code Generation, Training Method, Apparatus, Device and Medium for Code Generation Model”, the entire contents of which are incorporated herein by reference. Technical Field
[0002] This disclosure relates to the field of computer technology, and in particular to a code generation, a method, apparatus, device, and medium for training a code generation model. Background Technology
[0003] With the development of artificial intelligence, code generation technology has gradually become widely used as an auxiliary tool for code development. Currently, existing code generation technologies mainly use pre-trained code generation models to automatically generate the required programming language code based on natural language instructions input by the user.
[0004] Currently, existing code generation models require extensive training data (including natural language instructions and their corresponding codes) before they can output code based on user natural language commands. However, due to the continuous development and updates of programming languages, new functions or changes in function functionality may occur (or user-defined functions may be added). Therefore, it is currently necessary to frequently retrain the code generation model using code including the updated functions to ensure the accuracy of code generation when using the model.
[0005] However, retraining the code generation model is not only time-consuming, but also requires a lot of computing resources and new training data, resulting in a waste of computing and human resources. Summary of the Invention
[0006] This disclosure provides a code generation method, apparatus, device, and medium for training a code generation model, which can improve the flexibility of code generation.
[0007] Firstly, this disclosure provides a code generation method, the method comprising:
[0008] Based on the functional description information of the code to be generated, the function library is searched to obtain the description information of the target function; the functional description information is used to describe the function that the code needs to implement, and the target function is a function that the code can use to implement the function;
[0009] The code is generated based on the functional description information and the description information of the target function.
[0010] Optionally, the function library pre-stores: a mapping relationship between the function description information and the semantic vector of the function description information; the step of retrieving the function library based on the functional description information of the code to be generated to obtain the description information of the target function includes:
[0011] Based on the functional description information, obtain the first semantic vector;
[0012] From the function library, obtain a second semantic vector that matches the first semantic vector, wherein the second semantic vector is a semantic vector of the function's description information;
[0013] Based on the second semantic vector and the mapping relationship, the description information of the objective function is obtained.
[0014] Optionally, obtaining the first semantic vector based on the functional description information includes:
[0015] The functional description information is decomposed to obtain multiple sub-description information; the sub-description information satisfies the following: the sub-code used to implement the function described by the sub-description information needs to use a function, or the sub-code used to implement the function described by the sub-description information does not need to use a function;
[0016] Obtain the semantic vectors corresponding to the plurality of sub-description information;
[0017] The first semantic vector is obtained based on the semantic vectors corresponding to the multiple sub-description information.
[0018] Optionally, obtaining the second semantic vector matching the first semantic vector from the function library includes:
[0019] The semantic vectors of the description information of functions in the function library whose similarity to the first semantic vector is greater than or equal to a preset similarity are used as the second semantic vector.
[0020] Optionally, the method further includes:
[0021] In response to the absence of a semantic vector in the function library that has a similarity to the first semantic vector greater than or equal to a preset similarity, the code is generated based on the functional description information.
[0022] Optionally, before retrieving the function library based on the functional description information of the code to be generated to obtain the description information of the target function, the method further includes:
[0023] Based on the functional description information, determine whether the code needs to use a function to implement the function;
[0024] The process of retrieving the function library based on the functional description information of the code to be generated, and obtaining the description information of the target function, includes:
[0025] If it is determined that the code needs to use a function to implement the function, then the function library is searched based on the function description information of the code to be generated to obtain the description information of the target function;
[0026] or,
[0027] If it is determined that the code does not require the use of a function to implement the function, then the code is generated based on the function description information.
[0028] Optionally, the method further includes:
[0029] Receive a function update instruction; the function update instruction includes: description information of the new function;
[0030] Obtain the semantic vector of the description information of the new function;
[0031] Establish a mapping relationship between the description information of the new function and the semantic vector of the description information of the new function;
[0032] The function library is updated based on the mapping relationship between the description information of the new function and the semantic vector of the description information of the new function.
[0033] Optionally, the description information of the objective function includes at least one of the following:
[0034] The library path required to call the target function, the name of the target function, the functional description of the target function, the parameter description of the target function, a usage example of the target function, and an explanation of the usage example.
[0035] Optionally, the description information of the target function includes: a functional description of the target function; before generating the code based on the functional description information and the description information of the target function, the method further includes:
[0036] The functional description of the target function includes: when the target function depends on the identifier of the function, obtaining the description information of the function that the target function depends on based on the identifier of the function that the target function depends on;
[0037] The step of generating the code based on the functional description information and the description information of the target function includes:
[0038] The code is generated based on the functional description information, the description information of the target function, and the description information of the functions on which the target function depends for execution.
[0039] Optionally, generating the code based on the functional description information and the description information of the target function includes:
[0040] Based on the concatenation result of the functional description information and the description information of the target function, a target prompt is obtained;
[0041] The target prompt is input into a pre-trained code generation model to obtain the code.
[0042] Secondly, this disclosure provides a code generation method, the method comprising:
[0043] Receive user instructions; the user instructions are used to instruct the functional description information to be translated into code; the functional description information is used to describe the function that the code needs to implement;
[0044] If the function library is searched based on the function description information to obtain the description information of the target function, then the code is generated by a pre-trained code generation model based on the function description information and the description information of the target function; the target function is a function that can be used by the code to implement the function.
[0045] Output the code.
[0046] Optionally, the code is a Structured Query Language (SQL) statement.
[0047] Thirdly, this disclosure provides a method for training a code generation model, the method comprising:
[0048] Obtain a sample dataset; the sample dataset includes: a first sample data group; the first sample data group includes: a first type of sample hint, and a first type of sample code corresponding to the first type of sample hint; the first type of sample hint includes: sample description information and sample function description information; the sample description information is used to describe the function that the code needs to implement, and the first type of sample code is used to call the sample function to implement the function;
[0049] Based on the sample dataset, the preset model is trained to obtain the code generation model;
[0050] The code generation model is used in the code generation method as described in the first / second aspect.
[0051] Optionally, the sample dataset further includes at least one of a second sample data group, a third sample data group, and a fourth sample data group;
[0052] The second sample data group includes: the first type of sample prompt, and the second type of sample code corresponding to the first type of sample prompt; the second type of sample code does not call any function to implement the function.
[0053] The third sample data group includes: a second type of sample prompt, and a first type of sample code corresponding to the second type of sample prompt; the second type of sample prompt includes: sample description information for describing the function that the code needs to implement;
[0054] The fourth sample data group includes: sample prompts of the second type, and sample codes of the second type corresponding to the sample prompts of the second type.
[0055] Fourthly, this disclosure provides a code generation apparatus, the apparatus comprising:
[0056] The retrieval module is used to search the function library based on the functional description information of the code to be generated, and obtain the description information of the target function; the functional description information is used to describe the function that the code needs to implement, and the target function is a function that the code can use to implement the function;
[0057] A generation module is used to generate the code based on the functional description information and the description information of the target function.
[0058] Fifthly, this disclosure provides an electronic device, including: a processor and a memory; the processor is communicatively connected to the memory;
[0059] The memory stores computer instructions;
[0060] The processor executes computer instructions stored in the memory to implement the method as described in any one of the first, second, and third aspects.
[0061] In a sixth aspect, this disclosure provides a computer-readable storage medium storing computer-executable instructions that, when executed by a processor, are used to implement the method as described in any one of the first, second, and third aspects.
[0062] In a seventh aspect, this disclosure provides a computer program product comprising a computer program that, when executed by a processor, implements the method as described in any one of the first, second, and third aspects.
[0063] The code generation method, apparatus, device, and medium disclosed herein, which provide code generation and code generation model training methods, allow the retrieval of description information of target functions that can be used to implement the corresponding functions from a function library using the functional description information of the code to be generated. Then, based on the aforementioned functional description information and the description information of the target functions, code can be generated. This function library stores the description information of the target functions required for the code to implement the aforementioned functions. Therefore, if the target function changes, only the description information of that target function in the function library needs to be changed, and subsequent code generation can be based on the modified description information, avoiding frequent retraining of the code generation model due to function updates. Therefore, using the above method, even when functions are updated, code can be generated promptly based on the updated functions without retraining the code generation model, improving the accuracy and flexibility of code generation. Attached Figure Description
[0064] To more clearly illustrate the technical solutions in this disclosure or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0065] Figure 1 is a flowchart illustrating a code generation method provided in this disclosure;
[0066] Figure 2 is a flowchart illustrating a method for obtaining descriptive information of an objective function provided in this disclosure;
[0067] Figure 3 is a flowchart illustrating the training method of the code generation model provided in this disclosure;
[0068] Figure 4 is a flowchart illustrating another code generation method provided in this disclosure;
[0069] Figure 5 is a schematic diagram of the structure of a code generation device provided in this disclosure;
[0070] Figure 6 is a schematic diagram of the structure of a training device for a code generation model provided in this disclosure;
[0071] Figure 7 is a schematic diagram of another code generation device provided in this disclosure;
[0072] Figure 8 is a schematic diagram of the hardware structure of an electronic device provided in this disclosure.
[0073] The accompanying drawings have illustrated specific embodiments of this disclosure, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concepts of this disclosure to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0074] To make the objectives, technical solutions, and advantages of this disclosure clearer, the technical solutions of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this disclosure, not all embodiments. Based on the embodiments of this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.
[0075] The following is an explanation of some of the terms and concepts used in this disclosure:
[0076] Code generation technology refers to the technology of converting natural language instructions into programming language.
[0077] Functions: It should be understood that the functions discussed in this disclosure refer to functions in the field of computer programming. A function can be a process used to perform a specific operation; that is, a series of codes can be encapsulated in a function. When a piece of code needs to be used multiple times, its functionality can be achieved directly by calling the function containing that piece of code.
[0078] Currently, existing code generation technologies primarily involve inputting a user's natural language commands into a pre-trained code generation model. This pre-trained model then outputs code corresponding to the natural language command. This code can then be used to implement the function described by the natural language command.
[0079] The code generation model described above needs to be trained offline on a large amount of training data before it can output corresponding codes based on user natural language commands. This training data may include, for example, natural language commands as training samples and sample codes as sample labels.
[0080] The content of the sample code, which serves as the sample label, determines the content of the code that the trained code generation model can generate during application. For example, assuming that all sample code uses only functions A, B, and C to implement the corresponding functionality, then the code generation model trained using that sample code can only generate code that implements the corresponding functionality by calling functions A, B, or C in subsequent applications.
[0081] However, due to the continuous development and updates of programming languages, new functions may be generated, or the functionality of functions may change. There may also be user-defined functions, as well as the insertion of functions from third-party libraries. Therefore, when coding manually, the functions called to achieve the same function may be constantly changing, or the functions implemented by calling functions with the same name may also change.
[0082] Therefore, in order to ensure the accuracy of the code generation model, it is necessary to frequently retrain the code generation model using code including the updated functions, so that the retrained code generation model can generate code including the updated functions.
[0083] However, retraining the code generation model is not only time-consuming, but also requires a lot of computing resources and new training data, resulting in a waste of computing and human resources. Therefore, existing code generation methods have poor flexibility.
[0084] Considering that the existing code generation methods suffer from the aforementioned problems due to the need for frequent retraining of the code generation model, this disclosure proposes a code generation method that does not require retraining of the code generation model when the function is updated. With this method, code can be generated based on the updated function even without frequent retraining of the code generation model, thereby improving the accuracy and flexibility of code generation.
[0085] It should be understood that the execution subject of the code generation method provided in this disclosure can be any electronic device with processing capabilities, such as a terminal or a server, and this disclosure does not limit it. In some embodiments, the execution subject of the code generation method provided in this disclosure can also be a code generation system, or a code generation service, a code generation model (the code generation model mentioned here can execute the following embodiments, which are different from the aforementioned code generation models that require model training before application), etc.
[0086] Taking the code generation system as the executing entity of this code generation method as an example, the technical solution of this disclosure will be described in detail below with reference to specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments.
[0087] Figure 1 is a flowchart illustrating a code generation method provided in this disclosure. As shown in Figure 1, the method may include the following steps:
[0088] S101. Based on the functional description information of the code to be generated, the function library is searched to obtain the description information of the target function.
[0089] The above functional description information describes the functions that the code needs to implement.
[0090] Optionally, this disclosure does not limit the data modality of the aforementioned functional description information. For example, the functional description information may include data of at least one modality. For instance, the functional description information may be text-based data. Alternatively, the functional description information may also include text-based data and image-based data. Or, the functional description information may also include speech-based data, etc.
[0091] It should be understood that this disclosure does not limit how the code generation system obtains the aforementioned functional description information. For example, the code generation system can receive user instructions. These user instructions can be used to instruct the functional description information to be converted into code. That is, the code generation system can obtain the functional description information of the code to be generated based on user instructions. Alternatively, the code generation system can, for example, read its own stored data, obtain a document containing the aforementioned functional description information, and read the document to obtain the functional description information.
[0092] Taking the acquisition of the aforementioned functional description information based on user commands as an example, assuming the user command is "remove leading and trailing spaces from the string," the code generation system can, for instance, directly use this user command as the functional description information for the code to be generated. Alternatively, the code generation system can also perform keyword feature extraction on the user command, using the keyword features of the user command as the functional description information for the code to be generated.
[0093] The aforementioned function library can be, for example, a database that pre-stores function descriptions. For instance, the function library may store descriptions of functions in at least one programming language. In some embodiments, technicians or users can also update the configuration of the function library, such as adding, deleting, modifying, or querying the function descriptions in the library.
[0094] The target function described above can be a function that can be used to implement the functionality described in the above "functional description information".
[0095] The description of the objective function described above can be used to describe the functionality that the objective function can achieve. For example, the description of the objective function may include the name of the objective function, a description of the objective function's function, and examples of how the objective function can be used.
[0096] As one possible implementation, the code generation system could, for example, retrieve the description information of the target function from the function library using the function library's retrieval interface, based on the semantic vector corresponding to the function description information. As another possible implementation, the code generation system could also retrieve the description information of the target function from the function library using the function library's retrieval interface, based on keywords or other information related to the function description information.
[0097] S102. Generate the above code based on the functional description information and the description information of the objective function.
[0098] As one possible implementation, the code generation system could, for example, concatenate the function description information and the target function description information to obtain prompt information, and then call a model capable of code generation to generate code based on the prompt information.
[0099] As another possible implementation, taking the functional description information and the objective function description information as text modal data as an example, the code generation system could first use any existing text encoder to encode the functional description information and the objective function description information to obtain the encoded fusion features of the functional description information and the objective function description information. Then, the code generation system can input these encoded fusion features into a large language model capable of code generation to generate the aforementioned code.
[0100] As another possible implementation, the code generation system could first extract keyword features from the description information of the target function to obtain the keywords of the target function's description information. Then, the code generation system could concatenate the keywords of the function description information and the target function's description information to obtain the concatenated result. Finally, it could call any existing code generation model to generate the code based on the concatenated result.
[0101] It should be understood that this disclosure does not limit how the code generated by the code generation system is processed. For example, the code generation system may output the code after generation. Alternatively, the code generation system may run the code after generation to automatically implement the function described in the functional description information. Or, the code generation system may store the code after generation.
[0102] It should be understood that this disclosure does not limit the programming language of the above code. In some embodiments, the programming language of the code may be, for example, Python, Structured Query Language (SQL), or Java. Taking SQL as an example, in some embodiments, the code may also be referred to as an SQL statement.
[0103] In this embodiment, the description information of the target function that can be used to implement the corresponding function can be retrieved from the function library using the functional description information of the code to be generated. Then, based on the functional description information and the description information of the target function, the code can be generated. This function library stores the description information of the target function required to implement the above function. Therefore, if the target function changes, only the description information of the target function in the function library needs to be changed, and subsequent code generation can be based on the modified description information, avoiding frequent retraining of the code generation model due to function updates. Therefore, using the above method, even when the function is updated, the code generation model can be retrained in a timely manner based on the updated function, improving the accuracy and flexibility of code generation.
[0104] The following section details how the code generation system searches the function library based on the functional description information of the code to be generated, and obtains the description information of the target function:
[0105] As one possible implementation, taking the example of a function library pre-stored a mapping relationship between function description information and the semantic vector of the function description information, Figure 2 is a schematic flowchart of a method for obtaining the description information of a target function provided in this disclosure. As shown in Figure 2, the method may include the following steps:
[0106] S201. Based on the above functional description information, obtain the first semantic vector.
[0107] The first semantic vector can be used to characterize the semantics of the functional description information. In other words, the first semantic vector can express the function that the code needs to implement.
[0108] In some embodiments, taking the functional description information as text modality as an example, the code generation system can, for example, use a text encoding (i.e., text embedding) model to encode the functional description information to obtain the first semantic vector.
[0109] Optionally, the specific implementation of this text encoding model in encoding functional description information into a first semantic vector can refer to any existing method for encoding text into a semantic vector, which will not be elaborated here.
[0110] In some embodiments, before performing semantic vector conversion, the code generation system may, for example, decompose the functional description information into tasks to obtain semantic vectors based on the resulting atomic tasks (or atomic functions, which typically refer to the smallest indivisible functional unit in a task or operation. Atomic tasks are the basic building blocks for implementing more complex tasks and can usually be implemented through a single function or a simple block of code).
[0111] For example, the code generation system can first break down the functional description information into multiple sub-description information. Then, the code generation system can obtain the semantic vectors corresponding to each of these sub-description information, and based on these semantic vectors, obtain the aforementioned first semantic vector.
[0112] The function described by this sub-description information is, for example, an atomic function. That is, the sub-description information can satisfy either the following: the sub-code used to implement the function described by the sub-description information needs to use a function, or the sub-code used to implement the function described by the sub-description information does not need to use a function.
[0113] For example, the code generation system can decompose the functional description information using a pre-trained large language model with task decomposition capabilities. For instance, the code generation system can input the functional description information into the large language model. The large language model can then output the task decomposition results, i.e., the aforementioned multiple sub-description information, based on the functional description information. Alternatively, the code generation system can implement the decomposition of the functional description information into multiple sub-description information by referring to any existing method for decomposing tasks into multiple atomic tasks, which will not be elaborated upon here.
[0114] For example, taking the function description "multiply the maximum value among 1, 2, 3, 4, 5, and 6 by 13" as an example, by breaking down this function description, the sub-description information can include, for example, sub-description information 1 "create a list containing the numbers 1, 2, 3, 4, 5, and 6", sub-description information 2 "find the maximum value in the list", sub-description information 3 "multiply the found maximum value by 13", and sub-description information 4 "output the calculation result", etc. Among them, the sub-code of the function described by sub-description information 2 can, for example, use a function (i.e., a maximum value lookup function).
[0115] Optionally, the code generation system can, for example, obtain a sub-semantic vector corresponding to any one of the multiple sub-description information using the aforementioned method for obtaining semantic vectors. Then, the code generation system can, for example, concatenate the sub-semantic vectors corresponding to the multiple sub-description information to obtain the aforementioned first semantic vector. Alternatively, the code generation system can, for example, use the sub-semantic vectors corresponding to the multiple sub-description information as the first semantic vector.
[0116] By decomposing the functional description information, multiple sub-description information is obtained, thus achieving task decomposition of the function described by the functional description information. This task decomposition breaks down the complex functional implementation process described by the functional description information into multiple relatively simple tasks (i.e., the functions described by the sub-description information), reducing omissions in the functional description information. Then, based on the semantic vectors corresponding to these multiple sub-description information, the aforementioned first semantic vector is obtained. This improves the accuracy of the first semantic vector in representing the function described by the functional description information, thereby improving the accuracy of subsequent function retrieval based on the first semantic vector. Therefore, it improves the accuracy of code generation based on the description information of the retrieved target function.
[0117] In some embodiments, before obtaining the first semantic vector based on the aforementioned functional description information, the code generation system may, for example, perform query analysis on the functional description information through a query analysis module, and then obtain the first semantic vector based on the analysis results. For example, the query analysis may include methods such as rewriting (e.g., rewriting words like "last month" or "last year" in the functional description information into specific months or years, such as rewriting "last month" as March and "last year" as 2023), and extracting central terms, to improve the accuracy of retrieving the description information of the target function from the function library. In some embodiments, the query analysis method may also refer to the query analysis method used in any existing retrieval method, which will not be elaborated here.
[0118] S202. Obtain the second semantic vector that matches the first semantic vector from the function library.
[0119] The second semantic vector is a semantic vector representing the descriptive information of the function. This semantic vector can be used to characterize the descriptive information of the function.
[0120] Optionally, the semantic vector of the description information of the aforementioned "function matching the first semantic vector" can, for example, be obtained in advance based on the function's description information. For instance, taking the function's description information as text modal data, the code generation system can, for example, pre-encode the function's description information into text to obtain the semantic vector of the function's description information and store it in the function library.
[0121] It should be understood that the text encoding model used to text-encode the description information of the function can be the same model as the aforementioned text encoding model that encodes the function description information into a first semantic vector, so as to ensure that the first semantic vector can match the semantic vector of the function description information.
[0122] Optionally, the number of semantic vectors describing the "function matching the first semantic vector" can be, for example, one or more.
[0123] In some embodiments, the code generation system may, for example, use the semantic vector of the description information of the function in the function library that has the highest similarity to the first semantic vector as the semantic vector of the description information of the function that matches the first semantic vector.
[0124] Alternatively, in some embodiments, the code generation system may, for example, use a semantic vector of "function description information" in the function library whose similarity to the first semantic vector is greater than or equal to a preset similarity as the semantic vector of the function description information that matches the first semantic vector (i.e., the second semantic vector).
[0125] The aforementioned preset similarity can be, for example, pre-stored in the code generation system.
[0126] For example, assuming that there are semantic vectors 1, 2 and 3 for the description information of functions that have a similarity greater than or equal to the first semantic vector, the code generation system can use semantic vectors 1, 2 and 3 as semantic vectors for the description information of functions that match the first semantic vector.
[0127] Alternatively, taking the example of a code generation system using the sub-semantic vectors corresponding to the aforementioned multiple sub-description information as the first semantic vector, the system could, for any sub-semantic vector, obtain the semantic vectors of the description information of functions in the function library whose similarity to the sub-semantic vector is greater than or equal to a preset similarity, and use these as the semantic vectors of the description information of functions matching the sub-semantic vector. Then, the code generation system can use the semantic vectors of the description information of functions matching each sub-semantic vector as the semantic vectors of the description information of functions matching the first semantic vector.
[0128] By using the semantic vectors of the descriptive information of functions in the function library whose similarity to the first semantic vector is greater than or equal to a preset similarity, as the semantic vectors of the descriptive information of functions that match the first semantic vector, it is possible to determine the semantic vectors of the descriptive information of one or more functions that match the first semantic vector from the function library. This increases the amount of information in the descriptive information of the target function, thereby enriching the data on which code generation depends and further improving the flexibility of code generation.
[0129] In some embodiments, the function library may not contain a semantic vector of "function description information" that has a similarity greater than or equal to a preset similarity with the first semantic vector. Therefore, the code generation system may, for example, respond to the absence of a semantic vector of function description information in the function library that has a similarity greater than or equal to a preset similarity with the first semantic vector, generate the code based on the aforementioned function description information.
[0130] Taking the example of a code generation system using the sub-semantic vectors corresponding to the aforementioned multiple sub-description information as the first semantic vector, it should be understood that for any of the aforementioned sub-description information, the sub-semantic vector corresponding to that sub-description information may have a semantic vector in the function library that matches the description information of a function matching that sub-semantic vector; alternatively, the function library may not have a semantic vector in the description information of a function matching that sub-semantic vector. For example, if the function library does not have a semantic vector in the description information of a function matching the sub-semantic vector corresponding to any sub-description information, the code generation system may determine that the function library does not have a semantic vector in the description information of a function whose similarity to the first semantic vector is greater than or equal to a preset similarity. Alternatively, if the function library does have a semantic vector in the description information of a function matching the sub-semantic vector corresponding to at least one of the multiple sub-description information, the code generation system may determine that the function library does have a semantic vector in the description information of a function whose similarity to the first semantic vector is greater than or equal to a preset similarity.
[0131] Optionally, the code generation system can generate the above code based on the above functional description information. For example, it can refer to any existing method of code generation based on user instructions, which will not be elaborated here.
[0132] Using the above method, if there is no semantic vector in the function library that has a similarity to the first semantic vector greater than or equal to the preset similarity, it means that the code may not need to use a function to implement the function described in the function description information. Therefore, by generating the above code through the function description information, the flexibility of the generated code content is improved while ensuring that the code can implement the function described in the function description information.
[0133] In some embodiments, taking the acquisition of semantic vectors containing description information of multiple functions matching the first semantic vector from a function library as an example, the code generation system may further reorder the semantic vectors containing description information of the multiple functions matching the first semantic vector to obtain semantic vectors containing description information of functions that meet preset conditions and match the first semantic vector. It should be understood that the implementation of this reordering and the preset conditions can, for example, refer to any existing implementation of reordering after retrieval, and will not be elaborated upon here.
[0134] S203. Based on the second semantic vector and the mapping relationship mentioned above, the description information of the objective function is obtained.
[0135] For example, the mapping relationship between the description information of the above functions and the semantic vector of the description information of the functions can be shown in Table 1 below:
[0136] Table 1
[0137] Taking the mapping relationship shown in Table 1 as an example, assuming that the semantic vector of the description information of the function that matches the first semantic vector (i.e. the second semantic vector) is semantic vector 2, then the description information of the target function can be determined to be description information 2.
[0138] In this embodiment, the first semantic vector of the functional description information can be obtained from the functional description information of the code to be generated, thus realizing the extraction of semantic features of the functional description information. Then, by obtaining the semantic vector of the description information of the function that matches the first semantic vector from the function library, the description information of the function with similar semantic features to the functional description information can be used as the description information of the target function, laying the foundation for subsequent code generation based on the description information of the target function.
[0139] As another possible implementation, the code generation system could, for example, first determine whether the code needs to implement the function described in the function description information through a function.
[0140] For example, before searching the function library based on the functional description information of the code to be generated and obtaining the description information of the target function, the code generation system can first determine whether the code needs to use a function to implement the function based on the aforementioned functional description information.
[0141] For example, the code generation system can input the above-mentioned function description information, as well as the instruction to "determine whether the code used to implement the function needs to use a function based on the function description information", into a pre-trained large language model to obtain the judgment result of whether the code needs to use a function to implement the function.
[0142] If it is determined that the code needs to use a function to implement the function, the code generation system can, for example, execute step S101 and subsequent steps to obtain the description information of the target function, and generate code based on the description information of the target function and the functional description information.
[0143] Alternatively, if the code generation system determines that implementing the functionality does not require the use of a function, the system can generate the aforementioned code based on the functionality description information. Optionally, the implementation method by which the code generation system generates the aforementioned code based on the functionality description information can refer to any existing method for code generation based on user instructions, which will not be elaborated upon here.
[0144] By employing the above method, before retrieving the target function, the system determines whether code generation should proceed based on the judgment result of whether the function is required to implement the functionality. If it is determined that the function is not needed, the code can be generated based on the function description information, thus improving the flexibility and efficiency of code generation.
[0145] In some embodiments, as described above, if a function is updated, the function library can be updated directly to update the description information of the target function obtained during code generation. As one possible implementation, the function library can be updated via a code generation system.
[0146] For example, a code generation system can receive function update instructions. These function update instructions may include, for example, a description of the new function.
[0147] The aforementioned new function could refer to an existing function that has undergone functional or parameter updates. Alternatively, the new function could be a user-created function or a function added by a third-party library.
[0148] Then, the code generation system can, for example, obtain a semantic vector of the description information of the new function.
[0149] Optionally, the code generation system may encode the description information of the new function into a semantic vector, for example, using a text encoding model as described in any of the foregoing embodiments.
[0150] Then, the code generation system can, for example, establish a mapping relationship between the description information of the new function and the semantic vector of the description information of the new function, and update the function library based on the mapping relationship between the description information of the new function and the semantic vector of the description information of the new function.
[0151] For example, a code generation system can update the index of a function library based on the mapping relationship between the description information of the new function and the semantic vector of the description information of the new function, so that the mapping relationship between the description information of the new function and the semantic vector of the description information of the new function can be retrieved from the function library in the future.
[0152] Using the above method, when a function is updated, the code generation system can convert the description information of the new function into a semantic vector. Based on the mapping relationship between the description information of the new function and the semantic vector of the description information, the system updates the function library. This allows the description information of the new function to be retrieved from the function library, laying the foundation for generating code that uses the new function. Furthermore, by updating the function library, it is ensured that when a new function appears, the code generation model does not need to be retrained, improving the flexibility of code generation and reducing the waste of computing and human resources.
[0153] The following section provides a detailed explanation of how the code generation system generates code based on functional description information and the description information of the target function:
[0154] As one possible implementation, the code generation system could first obtain a target prompt based on the concatenation of functional description information and target function description information. Then, the code generation system could input this target prompt into a pre-trained code generation model to obtain the aforementioned code.
[0155] The code generation model can be a pre-trained model capable of generating code that implements the function described by the function description information, based on target prompts including functional description information and target function. In some embodiments, the code generation model can be a large language model. For example, the code generation system can input the target prompt into the code generation model and obtain the output of the code generation model as the aforementioned code.
[0156] In this embodiment, a target hint can be obtained by concatenating the functional description information and the target function description information. Then, a code generation model can be used to generate code based on this target hint. This allows the code generation model to refer to the target function description information in the target hint and generate code that can achieve the function described in the functional description information. Through this method, the code generation model can obtain the target function description information from the target hint without needing to pre-train the model using samples containing the target function description information, thus improving the flexibility of code generation based on the code generation model.
[0157] As another possible implementation, the description information of the target function may include at least one of the following: the library path required to call the target function, the name of the target function, the functional description of the target function, the parameter description of the target function, a usage example of the target function and an explanation of the usage example, etc.
[0158] In some embodiments, the content of the description information of the objective function may be related to, for example, the type of programming language of the code to be generated.
[0159] For example, taking the programming language of the code to be generated as SQL, the description information of the target function used by the SQL statement may include: the name of the target function (i.e., the name of the function that appears in the SQL statement), the functional description of the target function, the parameter description of the target function (e.g., the name, data type, and description of the parameters of the target function), and usage examples of the target function and explanations for those usage examples.
[0160] Taking Python as an example, the description information of the target function used by the Python code may include: the library path required to call the target function, the name of the target function, the functional description of the target function, the parameter description of the target function, the usage examples of the target function and the explanation of the usage examples, etc.
[0161] The library path mentioned above can refer to the path where the target function is located within a library. Optionally, the library path can be, for example, a physical path (location in the file system) or a logical path (location in the namespace of the programming language). It should be understood that this disclosure does not limit the library where the target function resides; for example, the library where the target function resides can be the standard library of the programming language used by the code (e.g., Python's standard library: math, os, sys, etc.), a third-party library, or a custom library, etc.
[0162] When the description of the target function includes the library path required to call the target function, subsequent code generation based on this description can directly use the library path to generate code that uses the target function, ensuring that the code can call the target function correctly during runtime and improving the success rate of code execution. When the description of the target function includes the name of the target function, it reduces the possibility of incorrectly writing the function name in the code generated based on this description, thus improving the accuracy of the code content. By including the functional description of the target function, the parameter description of the target function, usage examples of the target function, and explanations of those usage examples, the richness of the description information of the target function can be improved. This lays the foundation for retrieving the target function that can be used to implement the corresponding functionality from the function library, increasing the richness of the data on which code generation based on the description information of the target function depends, and thus further improving the accuracy of the functionality that the generated code can achieve.
[0163] Taking the description information of the target function as an example, in some embodiments, the code generation system may further search for which functions the code may still rely on to implement the aforementioned function before generating code based on the aforementioned functional description information and the description information of the target function, so as to further improve the success rate of the subsequently generated code.
[0164] For example, when the functional description of the target function includes the identifiers of the functions on which the target function depends, the code generation system can obtain the description information of the functions on which the target function depends based on the identifiers of the functions on which the target function depends.
[0165] For example, the identifier of the function upon which the target function depends can be, for instance, the function's name or other identifier that uniquely represents the function. Optionally, the identifier of the function upon which the target function depends may be located at a predetermined position in the functional description of the target function. The code generation system may, for example, use a field at this predetermined position as the identifier of the function upon which the target function depends if such a field exists. Alternatively, the code generation system may perform semantic analysis on the functional description of the target function to determine whether the functional description includes the identifier of the function upon which the target function depends.
[0166] Optionally, the code generation system can use the identifiers of the functions that the target function depends on to retrieve descriptive information about the functions that the target function depends on from the aforementioned function library. For example, if the identifier is the name of a function, and assuming that the function description includes the function name, then the descriptive information of the function whose name in the function library is the name of the function represented by the identifier can be used as the descriptive information of the functions that the target function depends on.
[0167] Then, as one possible implementation, the code generation system can generate code based on the aforementioned functional description information, the description information of the target function, and the description information of the functions on which the target function depends for its execution.
[0168] For example, the code generation system can first obtain the concatenated result of the function description information, the description information of the target function, and the description information of the functions on which the target function depends, and use this concatenated result as the target hint. Then, the code generation system can input the target hint into the aforementioned code generation model and obtain the output of the code generation model as the aforementioned code.
[0169] In this embodiment, by using the identifiers of the functions that the target function depends on in the functional description of the target function, the description information of the functions that the target function depends on can be obtained. Then, code can be generated based on the description information of the functions that the target function depends on, as well as the aforementioned functional description information and the description information of the target function. This allows the code to use the target function to implement the function described in the functional description information, and it can normally depend on the functions that the target function depends on to run, thus improving the success rate of the subsequently generated code.
[0170] As mentioned earlier, a pre-trained code generation model can be used to generate code based on target prompts. The following section details how to train this code generation model:
[0171] First, it should be understood that the execution subject of the training method for this code generation model can be the same as or different from the execution subject of the aforementioned code generation method; this disclosure does not impose any limitation on this. For example, the execution subject of the training method for this code generation model can also be any electronic device with processing capabilities, such as a server or terminal. The following detailed explanation uses an electronic device as an example to illustrate the training method for this code generation model.
[0172] As one possible implementation, the code generation model can be any existing large language model with code generation capabilities. By adding the description information of the target function to the target prompt, the code generation model can refer to the description information of the target function to generate code that uses the target function to implement the function described in the function description information. Optionally, the training method of this large language model with code generation capabilities can, for example, refer to the training method of any existing code generation model.
[0173] As another possible implementation, this disclosure also provides a training method for a code generation model. Figure 3 is a flowchart illustrating the training method for the code generation model provided in this disclosure. As shown in Figure 3, the method may include the following steps:
[0174] S301. Obtain a sample dataset. This sample dataset may include: a first sample data group. The first sample data group may include: a first type of sample hint, and a first type of sample code corresponding to the first type of sample hint. The first type of sample hint may include: sample description information and sample function description information.
[0175] The above sample description information can be used to describe the functionality that the code needs to implement. Accordingly, this first type of sample code can be used to call the above sample function to implement this functionality.
[0176] Optionally, the electronic device may obtain the aforementioned sample dataset input by the user through, for example, an application programming interface (API) or a graphical user interface (GUI).
[0177] Alternatively, electronic devices can also use data mining to extract user programming statements and comments on programming statements in logs to obtain sample description information, sample function description information, and corresponding sample code.
[0178] S302. Based on the sample dataset, train the preset model to obtain the code generation model.
[0179] Optionally, this code generation model can be applied to the code generation method described in any embodiment of this disclosure. For example, the code generation model can be used to output code based on input target prompts. The target prompts can refer to those described in any of the foregoing embodiments, and will not be repeated here.
[0180] For example, the aforementioned preset model can be any existing large language model, and this disclosure does not limit the preset model.
[0181] Optionally, the preset model can be trained using a sample dataset to obtain the implementation of the code generation model. For example, any existing method for training a model using a given sample dataset can be referenced, which will not be elaborated here.
[0182] In this embodiment, a preset model is trained using sample description information, sample function description information, and sample code that "calls the sample function to implement the function described in the sample description information." This allows the trained code generation model to generate code that "uses the target function to implement the function described in the function description information," based on target hints that include both function description information and target function description information. This lays the foundation for code generation based on the retrieved target function description information. Furthermore, by obtaining the target function description information from the function library, the code generation model does not require frequent training, reducing the waste of computational and human resources.
[0183] As another possible implementation, the above sample dataset may also include, for example, at least one of the following: the second sample data group, the third sample data group, and the fourth sample data group.
[0184] For example, the second, third, and fourth sample data groups can be shown in Table 2 below:
[0185] Table 2
[0186] As shown in Table 2, the second sample data group may include: the sample prompts of the first type mentioned above, and the sample codes of the second type corresponding to the sample prompts of the first type.
[0187] In this second type of sample code, no function was called to implement the function that "the sample code described in the sample description information must implement".
[0188] The second sample data set includes sample hints of the first type, namely, sample description information and sample function description information. Since the corresponding sample label is sample code that does not use the sample function to implement the function described in the sample description information, the preset model is trained using this sample dataset. This enables the trained code generation model to output code that does not use the target function, even when given a target hint including the target function description information. This allows the code generation model to generate code that does not use the function for some requirements that do not require calling a function to achieve the corresponding functionality. Therefore, the flexibility of the code generation model is further improved, and the generated code is more in line with human coding habits, thus also improving the accuracy of code generation.
[0189] The third sample data group may include: a second type of sample hint, and a first type of sample code corresponding to the second type of sample hint. The second type of sample hint may include: sample description information describing the functionality that the code needs to implement.
[0190] Because this third sample data set includes second-type sample prompts, namely sample description information, and because the corresponding sample label is sample code that uses a function to implement the function described in the sample description information, the preset model is trained using the sample dataset including this third sample data set. This enables the trained code generation model to output code that uses a function even when it receives a target prompt that does not include the description information of the target function. This allows the code generation model to generate code that uses a function even when the target prompt does not include the description information of the target function, thus further improving the flexibility of the code generation model.
[0191] The fourth sample data group may include: a second type of sample prompt, and the second type of sample code corresponding to the second type of sample prompt.
[0192] This fourth sample dataset includes second-type sample prompts, namely sample description information. Since the corresponding sample labels are sample code that does not use a function to implement the function described in the sample description information, the preset model is trained using this sample dataset. This enables the trained code generation model to output code that does not use a function even when a target prompt without a description of the target function is obtained. This allows the code generation model to handle needs that do not require function calls, and to avoid generating code that uses a function when the target prompt does not include a description of the target function, thus further improving the flexibility of the code generation model.
[0193] It should be understood that this disclosure does not limit the application scenarios of the code generation method described in any of the foregoing embodiments. For example, after generating code based on functional description information and target function description information, the code generation system can also output the code. Alternatively, after generating code based on functional description information and target function description information, the code generation system can also automatically execute the code to achieve the function that the code can perform. Or, for example, after generating code based on functional description information and target function description information, the code generation system can first receive an instruction to execute the code, and in response to the instruction, execute the code to achieve the function that the code can perform.
[0194] Figure 4 is a flowchart illustrating another code generation method provided in this disclosure. As shown in Figure 4, the method may include the following steps:
[0195] S401, Receive user instructions.
[0196] This user instruction can be used to instruct the translation of functional description information into code. As mentioned above, the functional description information describes the functionality that the code needs to implement.
[0197] For example, the code generation system may receive the aforementioned user instructions through a GUI or API, and this disclosure does not limit the implementation method of the code generation system receiving user instructions.
[0198] S402. If the function library is searched based on the functional description information to obtain the description information of the target function, then the code is generated by a pre-trained code generation model based on the functional description information and the description information of the target function.
[0199] As mentioned above, the objective function is the function that can be used to implement the above functionality in the code.
[0200] Optionally, the code generation system searches the function library based on the functional description information to obtain the implementation method of the description information of the target function, and generates the code implementation method based on the functional description information and the description information of the target function using a pre-trained code generation model. For example, the method described in the foregoing embodiments can be referred to, and will not be repeated here.
[0201] In some embodiments, the pre-trained code generation model may be, for example, a large language model trained using the code generation model training method described in any of the foregoing embodiments. Optionally, the code generation model training method may refer to the code generation model training method described in the foregoing embodiments.
[0202] In some embodiments, if a function library is searched based on the functional description information and no description information of the target function is obtained, the code generation system can use the aforementioned functional description information as a target prompt to generate code through a pre-trained code generation model.
[0203] S403, Output the above code.
[0204] By outputting the above code, the user can view the generated code. For example, the code generation system can output the code to a user's terminal so that the user can view it.
[0205] In this embodiment, functional description information can be obtained through user commands. Based on this description, the description information of the target function can be retrieved from the function library. Then, a pre-trained code generation model can generate and output code based on the functional description information and the target function's description information. Using this method, when the target function changes, only the description information of that target function in the function library needs to be modified. Subsequent code generation can be based on this modified description information, avoiding frequent retraining of the code generation model due to function updates. Therefore, using this method, even when a function is updated, code can be generated and output promptly based on the updated function without retraining the code generation model, improving the accuracy and flexibility of code generation and enhancing the user experience.
[0206] As one possible implementation, the above code could be an SQL statement.
[0207] The following example uses SQL statements to illustrate the method of generating SQL statements:
[0208] Optionally, the SQL statement generation method can be implemented, for example, through a retrieval module and a generation module. The input of the retrieval module can be, for example, a user's natural language instruction, such as: Calculate the total sales amount of all salespersons in Department A last year.
[0209] The output of the retrieval module can be, for example: the definition of the relevant function and usage examples (i.e., the description information of the above target function). For example: sum(col), e.g., select sum(salary) from my_revenue;
[0210] Before the retrieval module is applied online to code generation, offline processing is also required. Optionally, the offline processing process can include, for example:
[0211] 1. Obtain the documentation (i.e., description information) of the function.
[0212] The description information of each function can include, for example, the following content:
[0213] Function name: The name of the function that appears in SQL;
[0214] Function description: The functional description of the function;
[0215] Function parameter description: The name, data type, and description of the function parameters;
[0216] Usage examples and explanations.
[0217] 2. Convert the documentation of the function into semantic vectors so that it can be quickly retrieved and used in the subsequent online retrieval process. Exemplarily, for example, through a Text Embedding model, the description information of the function can be converted into an embedding, that is, a semantic vector, so as to obtain a data pair of <embedding, the description information of the function>.
[0218] 3. Use a vector database engine to index and reserve the above embeddings, and each time a new function (such as a user-defined function) is added, the index can be updated incrementally.
[0219] During the process of applying the retrieval module online to code generation, for example, the same Text Embedding model as the offline processing can be used to convert the user's instruction into an embedding. Then, the retrieval module can use the vector database retrieval interface to retrieve the most similar embedding from the index established in the offline processing, and further obtain the corresponding function description information based on the above data pair, that is, as the description information of the target function.
[0220] In some embodiments, the Text Embedding model described above may be a pre-trained module. For example, the training method for this Text Embedding model may include: 1. Data preparation, i.e., collecting a large amount of user instructions corresponding to the functions used, as a sample set. Then, any existing general embedding model training method (such as contrastive learning) can be used to fine-tune the general embedding model based on this sample set to obtain an embedding model suitable for function scenarios.
[0221] The input to the above-mentioned generation module may include: user natural language commands and descriptions of functions output by the above-mentioned retrieval module. The output of the generation module may be: an SQL statement, such as: select sum(Sales) from sales_record where section='A'.
[0222] For example, the generation module can concatenate the prompt in the following form and call the large language model to generate an SQL statement. For example, the concatenated prompt (i.e., the target hint) can be as follows:
[0223] Task: Translate user commands into SQL;
[0224] The following functions may be helpful for implementing SQL:
[0225] Function name: The name of the function appearing in the SQL;
[0226] Function description: A description of the function's purpose;
[0227] Function parameter description: The name, data type, and description of the function parameters;
[0228] Usage examples and explanations;
[0229] The user command is: Calculate the total sales revenue of all sales staff in Department A last year.
[0230] Before being applied to code generation, the aforementioned large language model can be trained to improve its performance in responding to SQL generation commands. The training data can, for example, refer to the aforementioned sample datasets, such as the first, second, third, and fourth sample datasets.
[0231] For example, training data may include: 1. SQL statements without functions and their corresponding natural language function descriptions (which can be manually written or obtained by mining comments in user logs); 2. SQL statements without functions and their corresponding natural language function descriptions and function descriptions; 3. SQL statements with function calls and their corresponding natural language function descriptions (which can be manually written or obtained by mining comments in user logs), etc.
[0232] By combining the above data and training the large language model, a large language model suitable for this SQL statement generation scenario can be obtained.
[0233] In this embodiment, for the use case of converting natural language instructions into built-in functions and user-defined functions in Natural Language to SQL (NL2SQL), compared with the prior art of training and solidifying function information into the model, this disclosure inputs the function description information into the model through retrieval information, thereby separating the function information from the model. This not only preserves the model's ability to use functions, but also allows the function information to be flexibly updated without retraining the model, improving the accuracy of function usage and enhancing the accuracy of SQL statement generation in complex scenarios using NL2SQL.
[0234] As another example, taking the above code as a Python programming statement, we will illustrate the code generation method in a demonstrative way:
[0235] The implementation of the retrieval module and the generation module of this code generation method can be found in the foregoing embodiments, and will not be repeated here.
[0236] For example, the input to this retrieval module can be a user's natural language command, such as: remove leading and trailing spaces from a string. The output of this retrieval module is a description of the target function, including its definition and usage examples, such as: trim(string), e.g., trim(a).
[0237] The input to this generation module may include, for example, the user's natural language instruction and the description of the target function, and the output may be, for example, the code: trim(a). For instance, the target prompt based on the user's natural language instruction and the description of the target function input to the trained large language model may be as follows:
[0238] Task: Translate user commands into Python code;
[0239] The following functions may be helpful for implementation:
[0240] Library path: The path of the function in the library;
[0241] Function name: The name of the function that appears in the code;
[0242] Function description: A description of the function's functionality;
[0243] Function parameter description: The name, data type, and description of the function parameters;
[0244] Usage examples and explanations.
[0245] The following related functions may be helpful for correctly calling the functions above:
[0246] Library path: The path of the function in the library;
[0247] Function name: The name of the function that appears in the code;
[0248] Function description: A description of the function's functionality;
[0249] Function parameter description: The name, data type, and description of the function parameters;
[0250] Usage examples and explanations.
[0251] The user command is: Remove leading and trailing spaces from the string.
[0252] The training data for the aforementioned large language model can also be obtained by referring to the methods described in the preceding embodiments, and will not be repeated here.
[0253] In this embodiment, for the use of library functions and user-defined functions in code generation, compared with the prior art of training and solidifying function information into the model, this disclosure inputs the function description information into the model through retrieval information (i.e., retrieval enhancement), thereby separating function information from the model. This not only preserves the model's ability to use functions, but also allows function information to be flexibly updated without retraining the model, improving the accuracy of function usage and enhancing the accuracy of code generation in complex scenarios.
[0254] Figure 5 is a schematic diagram of a code generation device provided in this disclosure. As shown in Figure 5, the code generation device may include: a retrieval module 51 and a generation module 52.
[0255] The retrieval module 51 is used to search the function library based on the functional description information of the code to be generated, and obtain the description information of the target function. The functional description information describes the function that the code needs to implement, and the target function is a function that the code can use to implement the function.
[0256] The generation module 52 is used to generate the code based on the functional description information and the description information of the target function.
[0257] Optionally, the function library pre-stores a mapping relationship between the function's description information and the semantic vector of the function's description information. Optionally, the retrieval module 51 is specifically used to obtain a first semantic vector based on the function description information; obtain a second semantic vector matching the first semantic vector from the function library; and obtain the description information of the target function based on the second semantic vector and the mapping relationship. Wherein, the second semantic vector is the semantic vector of the function's description information.
[0258] Optionally, the retrieval module 51 is specifically configured to decompose the functional description information based on the functional description information to obtain multiple sub-description information; obtain semantic vectors corresponding to the multiple sub-description information respectively; and obtain the first semantic vector based on the semantic vectors corresponding to the multiple sub-description information respectively. Wherein, the sub-description information satisfies the following conditions: the sub-code used to implement the function described by the sub-description information needs to use a function, or the sub-code used to implement the function described by the sub-description information does not need to use a function.
[0259] Optionally, the retrieval module 51 is specifically used to take the semantic vectors of the description information of functions in the function library whose similarity to the first semantic vector is greater than or equal to a preset similarity as the semantic vectors of the description information of functions that match the first semantic vector.
[0260] Optionally, the generation module 52 is further configured to generate the code based on the function description information in response to a semantic vector in the function library that does not contain a function whose similarity to the first semantic vector is greater than or equal to a preset similarity.
[0261] Optionally, the retrieval module 51 is further configured to, before retrieving the function library based on the functional description information of the code to be generated and obtaining the description information of the target function, determine, based on the functional description information, whether the code needs to use a function to implement the function. Optionally, the retrieval module 51 is specifically configured to, when it is determined that the code needs to use a function to implement the function, retrieve the function library based on the functional description information of the code to be generated and obtain the description information of the target function. Optionally, the generation module 52 is further configured to, when it is determined that the code does not need to use a function to implement the function, generate the code based on the functional description information.
[0262] Optionally, the device may further include a receiving module 53 for receiving a function update instruction. The function update instruction includes: description information of a new function. Optionally, the device may further include an update module 54 for obtaining the semantic vector of the description information of the new function; establishing a mapping relationship between the description information of the new function and the semantic vector of the description information of the new function; and updating the function library based on the mapping relationship between the description information of the new function and the semantic vector of the description information of the new function.
[0263] Optionally, the description information of the objective function includes at least one of the following:
[0264] The library path required to call the target function, the name of the target function, the functional description of the target function, the parameter description of the target function, a usage example of the target function, and an explanation of the usage example.
[0265] Taking the description information of the target function as an example, where the target function's functional description is included, optionally, the generation module 53 is further configured to, before generating the code based on the functional description information and the target function's description information, obtain the description information of the functions that the target function depends on, based on the identifiers of the functions that the target function depends on, when the target function's functional description includes the identifiers of the functions that the target function depends on. Optionally, the generation module 53 is specifically configured to generate the code based on the functional description information, the target function's description information, and the description information of the functions that the target function depends on.
[0266] Optionally, the generation module 53 is specifically used to obtain a target prompt based on the splicing result of the functional description information and the description information of the target function; and input the target prompt into a pre-trained code generation model to obtain the code.
[0267] The code generation apparatus provided in this disclosure is used to execute the aforementioned code generation method embodiments. Its implementation principle and technical effect are similar, and will not be described in detail here.
[0268] Figure 6 is a schematic diagram of the structure of a training device for a code generation model provided in this disclosure. As shown in Figure 5, the training device for the code generation model may include: an acquisition module 61 and a training module 62.
[0269] The acquisition module 61 is used to acquire a sample dataset. The sample dataset includes: a first sample data group; the first sample data group includes: a first type of sample hint, and a first type of sample code corresponding to the first type of sample hint; the first type of sample hint includes: sample description information and sample function description information; the sample description information describes the function that the code needs to implement, and the first type of sample code is used to call the sample function to implement the function;
[0270] Training module 62 is used to train a preset model based on the sample dataset to obtain the code generation model. The code generation model is applied to the code generation method described in any embodiment of this disclosure.
[0271] Optionally, the sample dataset further includes at least one of a second sample data group, a third sample data group, and a fourth sample data group; the second sample data group includes: a sample prompt of the first type, and sample code of the second type corresponding to the sample prompt of the first type; the sample code of the second type does not call a function to implement the function; the third sample data group includes: a sample prompt of the second type, and sample code of the first type corresponding to the sample prompt of the second type; the sample prompt of the second type includes: sample description information for describing the function that the code needs to implement; the fourth sample data group includes: a sample prompt of the second type, and sample code of the second type corresponding to the sample prompt of the second type.
[0272] The training apparatus for the code generation model provided in this disclosure is used to execute the aforementioned training method embodiment for the code generation model. Its implementation principle and technical effect are similar, and will not be described in detail here.
[0273] Figure 7 is a schematic diagram of another code generation device provided in this disclosure. As shown in Figure 7, the code generation device may include: a receiving module 71, a processing module 72, and an output module 73.
[0274] The receiving module 71 is used to receive user instructions. The user instructions instruct the translation of functional description information into code; the functional description information describes the function that the code needs to implement.
[0275] Processing module 72 is used to, when retrieving the function library based on the functional description information and obtaining the description information of the target function, generate the code based on the functional description information and the description information of the target function using a pre-trained code generation model. The target function is a function that the code can use to implement the functionality.
[0276] Output module 73 is used to output the code.
[0277] Optionally, the code is an SQL statement.
[0278] The code generation apparatus provided in this disclosure is used to execute the aforementioned code generation method embodiments. Its implementation principle and technical effect are similar, and will not be described in detail here.
[0279] Figure 8 is a schematic diagram of the hardware structure of an electronic device provided in this disclosure. The electronic device 80 shown in Figure 8 includes a memory 81, a processor 82, and a communication interface 83. The memory 81, processor 82, and communication interface 83 are communicatively connected to each other. For example, the memory 81, processor 82, and communication interface 83 can be connected via a network. Alternatively, the electronic device 80 may also include a bus 84. The memory 81, processor 82, and communication interface 83 are communicatively connected to each other via the bus 84. Figure 8 shows an electronic device 80 in which the memory 81, processor 82, and communication interface 83 are communicatively connected to each other via the bus 84.
[0280] The memory 81 can be a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 81 can store programs, and when the program stored in the memory 81 is executed by the processor 82, the processor 82 and the communication interface 83 are used to execute the code generation and / or code generation model training method described in any of the foregoing embodiments. The memory can also store data required for the code generation and / or code generation model training method.
[0281] The processor 82 can be a general-purpose CPU, microprocessor, application-specific integrated circuit (ASIC), graphics processing unit (GPU), or one or more integrated circuits.
[0282] The processor 82 can also be an integrated circuit chip with signal processing capabilities. During implementation, the code generation and / or code generation model training methods of this disclosure can be completed through integrated logic circuits in the hardware of the processor 82 or through software instructions. The processor 82 described above can also be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components, capable of implementing or executing the methods, steps, and logic block diagrams disclosed in the following embodiments of this disclosure. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in the following embodiments of this disclosure can be directly embodied in the execution of a hardware decoding processor, or executed by a combination of hardware and software modules in the decoding processor. The software modules can reside in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. The storage medium is located in memory 81. Processor 82 reads the information in memory 81 and, in conjunction with its hardware, completes the code generation and / or code generation model training method disclosed herein.
[0283] The communication interface 83 uses transceiver modules, such as, but not limited to, transceivers, to enable communication between the electronic device 80 and other devices or communication networks. For example, a dataset can be acquired through the communication interface 83.
[0284] When the aforementioned electronic device 80 includes a bus 84, the bus 84 may include a path for transmitting information between various components of the electronic device 80 (e.g., memory 81, processor 82, communication interface 83).
[0285] This disclosure also provides a computer-readable storage medium, which may include various media capable of storing program code, such as a USB flash drive, a portable hard drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk, or an optical disk. Specifically, the computer-readable storage medium stores program instructions that are used in the methods described in the above embodiments.
[0286] This disclosure also provides a program product including executable instructions stored in a readable storage medium. At least one processor of an electronic device can read the executable instructions from the readable storage medium, and the execution of the executable instructions by the at least one processor causes the electronic device to implement the code generation and / or code generation model training methods provided in the various embodiments described above.
[0287] The term "multiple" in this document refers to two or more. The term "and / or" is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A alone, A and B simultaneously, or B alone. Furthermore, the character " / " in this document generally indicates an "or" relationship between the preceding and following related objects; in formulas, the character " / " indicates a "division" relationship between the preceding and following related objects. Additionally, it should be understood that in the descriptions of this disclosure, terms such as "first" and "second" are used only for descriptive purposes and should not be construed as indicating or implying relative importance or order.
[0288] It is understood that the various numerical designations used in the embodiments of this disclosure are merely for descriptive convenience and are not intended to limit the scope of the embodiments of this disclosure.
[0289] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this disclosure, and are not intended to limit them. Although this disclosure has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this disclosure.
Claims
1. A code generation method, wherein, The method includes: Based on the functional description information of the code to be generated, the function library is searched to obtain the description information of the target function; the functional description information is used to describe the function that the code needs to implement, and the target function is a function that the code can use to implement the function; The code is generated based on the functional description information and the description information of the target function.
2. The method according to claim 1, wherein, The function library pre-stores a mapping relationship between the function description information and the semantic vector of the function description information; the step of retrieving the function library based on the functional description information of the code to be generated to obtain the description information of the target function includes: Based on the functional description information, obtain the first semantic vector; From the function library, obtain a second semantic vector that matches the first semantic vector, wherein the second semantic vector is a semantic vector of the function's description information; Based on the second semantic vector and the mapping relationship, the description information of the objective function is obtained.
3. The method according to claim 2, wherein, The step of obtaining the first semantic vector based on the functional description information includes: The functional description information is decomposed to obtain multiple sub-description information; the sub-description information satisfies the following: the sub-code used to implement the function described by the sub-description information needs to use a function, or the sub-code used to implement the function described by the sub-description information does not need to use a function; Obtain the semantic vectors corresponding to the plurality of sub-description information; The first semantic vector is obtained based on the semantic vectors corresponding to the multiple sub-description information.
4. The method according to claim 2 or 3, wherein, The step of obtaining a second semantic vector matching the first semantic vector from the function library includes: The semantic vectors of the description information of functions in the function library whose similarity to the first semantic vector is greater than or equal to a preset similarity are used as the second semantic vector.
5. The method according to claim 4, wherein, The method further includes: In response to the absence of a semantic vector in the function library that has a similarity to the first semantic vector greater than or equal to a preset similarity, the code is generated based on the functional description information.
6. The method according to any one of claims 1-3, wherein, Before retrieving the function library based on the functional description information of the code to be generated and obtaining the description information of the target function, the method further includes: Based on the functional description information, determine whether the code needs to use a function to implement the function; The process of retrieving the function library based on the functional description information of the code to be generated, and obtaining the description information of the target function, includes: If it is determined that the code needs to use a function to implement the function, then the function library is searched based on the function description information of the code to be generated to obtain the description information of the target function; or, If it is determined that the code does not require the use of a function to implement the function, then the code is generated based on the function description information.
7. The method according to claim 2 or 3, wherein, The method further includes: Receive a function update instruction; the function update instruction includes: description information of the new function; Obtain the semantic vector of the description information of the new function; Establish a mapping relationship between the description information of the new function and the semantic vector of the description information of the new function; The function library is updated based on the mapping relationship between the description information of the new function and the semantic vector of the description information of the new function.
8. The method according to any one of claims 1-3, wherein, The description of the objective function includes at least one of the following: The library path required to call the target function, the name of the target function, the functional description of the target function, the parameter description of the target function, a usage example of the target function, and an explanation of the usage example.
9. The method according to claim 8, wherein, The description information of the target function includes: a functional description of the target function; before generating the code based on the functional description information and the description information of the target function, the method further includes: The functional description of the target function includes: when the target function depends on the identifier of the function, obtaining the description information of the function that the target function depends on based on the identifier of the function that the target function depends on; The step of generating the code based on the functional description information and the description information of the target function includes: The code is generated based on the functional description information, the description information of the target function, and the description information of the functions on which the target function depends for execution.
10. The method according to any one of claims 1-3, wherein, The step of generating the code based on the functional description information and the description information of the target function includes: Based on the concatenation result of the functional description information and the description information of the target function, a target prompt is obtained; The target prompt is input into a pre-trained code generation model to obtain the code.
11. The method according to claim 1, wherein, The process of retrieving the function library based on the functional description information of the code to be generated to obtain the description information of the target function includes: Based on the keywords in the functional description information, the description information of the target function is obtained by searching the function library through the function library's search interface.
12. The method according to any one of claims 1-3, wherein, Before retrieving the function library based on the functional description information of the code to be generated and obtaining the description information of the target function, the method further includes: Receive user instructions; the user instructions are used to instruct the function description information to be translated into code; Based on the user instruction, obtain the function description information.
13. A code generation method, wherein, The method includes: Receive user instructions; the user instructions are used to instruct the functional description information to be translated into code; the functional description information is used to describe the function that the code needs to implement; If the function library is searched based on the function description information to obtain the description information of the target function, then the code is generated by a pre-trained code generation model based on the function description information and the description information of the target function; the target function is a function that can be used by the code to implement the function. Output the code.
14. The method according to claim 13, wherein, The code is a Structured Query Language (SQL) statement.
15. A training method for a code generation model, wherein, The method includes: Obtain a sample dataset; the sample dataset includes: a first sample data group; the first sample data group includes: a first type of sample hint, and a first type of sample code corresponding to the first type of sample hint; the first type of sample hint includes: sample description information and sample function description information; the sample description information is used to describe the function that the code needs to implement, and the first type of sample code is used to call the sample function to implement the function; Based on the sample dataset, the preset model is trained to obtain the code generation model; the code generation model is applied to the code generation method as described in claim 13 or 14.
16. The method according to claim 15, wherein, The sample dataset also includes at least one of the following: a second sample data group, a third sample data group, and a fourth sample data group; The second sample data group includes: the first type of sample prompt, and the second type of sample code corresponding to the first type of sample prompt; the second type of sample code does not call a function to implement the function. The third sample data group includes: a second type of sample prompt, and a first type of sample code corresponding to the second type of sample prompt; the second type of sample prompt includes: sample description information for describing the function that the code needs to implement; The fourth sample data group includes: sample prompts of the second type, and sample codes of the second type corresponding to the sample prompts of the second type.
17. A code generation apparatus, wherein, The device includes: The retrieval module is used to search the function library based on the functional description information of the code to be generated, and obtain the description information of the target function; the functional description information is used to describe the function that the code needs to implement, and the target function is a function that the code can use to implement the function; A generation module is used to generate the code based on the functional description information and the description information of the target function.
18. An electronic device, wherein, include: Processor and memory; The processor is communicatively connected to the memory; The memory stores computer instructions; The processor executes computer instructions stored in the memory to implement the method as described in any one of claims 1-14.
19. A computer-readable storage medium, wherein, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1-14.
20. A computer program product, wherein, The computer program product includes a computer program that, when executed by a processor, implements the method as described in any one of claims 1-14.