Code generation method and device, electronic equipment and storage medium

By using reference code information in the code database to generate text prompt word content in a large language model, the problem of code leakage risk in a multi-team environment is solved, and the accuracy and security of code generation are improved.

CN119493559APending Publication Date: 2025-02-21AGRICULTURAL BANK OF CHINA
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411730823.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-28
Publication Date
2025-02-21

AI Technical Summary

Technical Problem

When using large language models to generate code in a multi-team environment, there is a risk of team core code leakage, which affects the reliability and security of code generation applications.

Method used

By determining the text content of the pending code and its associated reference code information, the code information related to the pending code is extracted from the code database, and the text prompt word content of the code generation model is generated based on this, and the code generation model is called to complete the code. , make sure that the generated code text content revolves around the topic and avoids code leakage.

Benefits of technology

It effectively controls the scope of use of code information in the code database, improves the accuracy and security of code generation, ensures that the generated target code text content is more accurate and more in line with development specifications and code style, and avoids the risk of code leakage.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119493559A_ABST
    Figure CN119493559A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation method and device, electronic equipment and a storage medium. The method comprises the steps that to-be-processed code text content input into a code generation model is determined, wherein the to-be-processed code text content is incomplete to-be-complemented code function text content input in the code editing and generating process; determining reference code information associated with the to-be-processed code text content from code information included in a code database; generating text cue word content of a code generation model based on the reference code information; and based on the text cue word content of the code generation model, calling the code generation model to execute a code generation task to perform code completion on the to-be-processed code text content to obtain target code text content. According to the scheme, the target code text content is generated based on the text cue word content, the risk of code leakage is avoided, and meanwhile the accuracy of generation of the target code text content is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] Embodiments of the present invention relate to the field of computer technology, and in particular to a code generation method, device, electronic device, and storage medium. Background Art

[0002] In the context of today's rapid technological development, the code generation capability of large language models has become a research hotspot. It generally collects real code as training corpus and adjusts model parameters through training, thereby converting code data into model knowledge itself, and then achieving code generation function. Most of the related solutions use a large amount of code as training material, which enables the model to accumulate sufficient basic knowledge of code and helps to generate code that meets the needs of the team. However, the above solutions have serious drawbacks, especially when multiple teams share large language models, which will cause significant problems. The most prominent of these is the code security problem, which is easy to cause code leakage between teams and has serious security risks. This security risk may cause unpredictable losses to each team, seriously affecting the reliability and security of code generation applications of large language models in a multi-team environment. New solutions are urgently needed to overcome these problems. Summary of the invention

[0003] The present invention provides a code generation method, device, electronic device and storage medium to solve the problem of risk of team core code leakage when using a large language model to generate team code.

[0004] According to one aspect of the present invention, there is provided a code generation method, the method comprising:

[0005] Determine the code text content to be processed input to the code generation model, where the code text content to be processed is the incomplete code function text content to be completed input during the code editing and generation process;

[0006] Determine reference code information associated with the code text content to be processed from the code information included in the code database, the code validity of the reference code information is greater than the code validity of the remaining code information in the code database except the reference code information, the code validity is used to describe the code generation effect produced when the code information assists in completing the incomplete code function text content to be completed, the code information in the code database does not participate in the model training of the code generation model, and the code information in the code database needs to be authorized before access is allowed;

[0007] The text prompt word content of the code generation model is generated based on the reference code information. The code generation model can automatically generate complete code or complete the missing part of the code according to the given incomplete code fragment through learning and recognition of code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around the topic related to the code text content indicated in the text prompt word content during the code generation task.

[0008] Based on the text prompt word content of the code generation model, the code generation model is called to execute the code generation task to complete the code text content to be processed to obtain the target code text content.

[0009] According to another aspect of the present invention, there is provided a code generation device, the device comprising:

[0010] A first determination module is used to determine the to-be-processed code text content input to the code generation model, where the to-be-processed code text content is the incomplete to-be-completed code function text content input during the code editing and generation process;

[0011] A second determination module is used to determine reference code information associated with the to-be-processed code text content from the code information included in the code database, the code validity of the reference code information is greater than the code validity of the remaining code information in the code database except the reference code information, the code validity is used to describe the code generation effect produced when the code information assists in completing the incomplete code function text content to be completed, the code information in the code database does not participate in the model training of the code generation model, and the code information in the code database needs to be authorized before access is allowed;

[0012] A prompt word generation module is used to generate text prompt word content of a code generation model based on reference code information. The code generation model can automatically generate complete code or complete missing parts of the code according to a given incomplete code fragment through learning and identifying code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around a topic related to the code text content indicated in the text prompt word content during the process of executing the code generation task;

[0013] The code completion module is used to call the code generation model to execute the code generation task based on the text prompt word content of the code generation model to complete the code text content to be processed to obtain the target code text content.

[0014] According to another aspect of the present invention, there is provided an electronic device, the electronic device comprising:

[0015] at least one processor; and

[0016] a memory communicatively connected to at least one processor; wherein,

[0017] The memory stores a computer program that can be executed by at least one processor. The computer program is executed by at least one processor so that the at least one processor can execute the code generation method of any embodiment of the present invention.

[0018] According to another aspect of the present invention, a computer-readable storage medium is provided. The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the code generation method of any embodiment of the present invention when executed.

[0019] The technical solution of the embodiment of the present invention determines the code text content to be processed input into the code generation model, the code text content to be processed is the incomplete code function text content to be completed input in the code editing and generation process; determines the reference code information associated with the code text content to be processed from the code information included in the code database, the code validity of the reference code information is greater than the code validity of the remaining code information in the code database except the reference code information, the code validity is used to describe the code generation effect produced when the code information assists in completing the incomplete code function text content to be completed, the code information in the code database does not participate in the model training of the code generation model, and the code information in the code database is authorized to be accessed, thereby realizing the extraction of code information associated with the code text content to be processed from the code database, thereby effectively controlling the use scope of the code information in the code database; the text prompt word content of the code generation model is generated based on the reference code information, and the code generation The model can automatically generate complete code or complete the missing parts of the code according to the given incomplete code fragments through learning and identifying code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around the topics related to the code text content indicated in the text prompt word content during the code generation task, and the code information of the code database is used to fill the inadequacy of the code generation model's understanding of the code text content to be processed, thereby improving the accuracy of code generation; based on the text prompt word content of the code generation model, the code generation model is called to perform the code generation task to complete the code text content to be processed to obtain the target code text content, thereby generating the target code text content based on the text prompt word content, and the code information of the code database will not participate in the training of the code generation model, thereby avoiding the risk of code leakage, and also making the generated target code text content more accurate and more in line with development specifications and code style.

[0020] It should be understood that the contents described in this section are not intended to identify the key or important features of the embodiments of the present invention, nor are they intended to limit the scope of the present invention. Other features of the present invention will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0022] Figure 1 A flowchart of a code generation method provided in Embodiment 1 of the present invention;

[0023] Figure 2 A flowchart of a code generation method provided in Embodiment 2 of the present invention;

[0024] Figure 3 A schematic diagram of a storage data structure provided in Embodiment 2 of the present invention;

[0025] Figure 4 A flowchart of a system interaction provided in Embodiment 2 of the present invention;

[0026] Figure 5 A schematic diagram of the structure of a code generation device provided in Embodiment 3 of the present invention;

[0027] Figure 6 A schematic diagram of the structure of an electronic device for implementing a code generation method provided in a fourth embodiment of the present invention. DETAILED DESCRIPTION

[0028] In order to enable those skilled in the art to better understand the scheme of the present invention, the technical scheme in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.

[0029] It should be noted that the terms "first", "second", etc. in the specification and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0030] It is understandable that before using the technical solutions disclosed in the embodiments of the present invention, the type, scope of use, usage scenarios, etc. of the personal information involved in the present invention should be informed to the user and the user's authorization should be obtained in an appropriate manner in accordance with relevant laws and regulations.

[0031] For example, in response to receiving an active request from a user, a prompt message is sent to the user to clearly prompt the user that the operation requested to be performed will require obtaining and using the user's personal information. Thus, the user can autonomously choose whether to provide personal information to software or hardware such as an electronic device, application, server, or storage medium that performs the operation of the technical solution of the present invention according to the prompt message.

[0032] As an optional but non-limiting implementation, in response to receiving an active request from the user, the prompt information may be sent to the user in the form of a pop-up window, in which the prompt information may be presented in text form. In addition, the pop-up window may also carry a selection control for the user to choose "agree" or "disagree" to provide personal information to the electronic device.

[0033] It is understandable that the above notification and user authorization process are only illustrative and do not limit the implementation of the present invention. Other methods that meet relevant laws and regulations may also be applied to the implementation of the present invention.

[0034] It is understandable that the data involved in this technical solution (including but not limited to the data itself, the acquisition or use of the data) shall comply with the requirements of relevant laws, regulations and relevant provisions.

[0035] Embodiment 1

[0036] Figure 1This is a flowchart of a code generation method provided in the first embodiment of the present invention. The embodiment of the present invention is applicable to the case where a large language model is used to generate code text. The method can be executed by a code generation device, which can be implemented in the form of hardware and / or software and can be configured in an electronic device that implements the code generation method. Figure 1 As shown, the method includes:

[0037] S101. Determine the code text content to be processed that is input into the code generation model, where the code text content to be processed is the incomplete code function text content to be completed that is input during the code editing and generation process.

[0038] In the embodiment of the present invention, the code generation model may refer to a large language model (LLM) with code generation capability. The code generation model may be obtained by training the LLM for code generation using a supercomputer and distributed computing technology.

[0039] Specifically, in a programming scenario, incomplete code snippets that have not been completed during the team development process can be used as the pending code text content input to the code generation model. The incomplete parts in the pending code text content need to be completed by the code generation model.

[0040] As an optional method, the process of determining the code generation model includes the following steps A1-A2:

[0041] Step A1, obtain candidate code corpus for model fine-tuning, the candidate code corpus is a collection of code snippets or code text content that has been collected and organized for model training, the code snippets or code files in the candidate code corpus do not need authorization to support access and do not include code snippets or code files that require authorization to allow access, the code corpus is code data formed based on the complete code text extracted according to the separated code file text.

[0042] Step A2: Use the candidate code corpus to fine-tune the large language model to generate a code generation model, so that the code generation model has the function of completing the missing code parts of the incomplete code fragments to be completed.

[0043] In an embodiment of the present invention, a code corpus may refer to a collection of corpus data produced using a large amount of public code data, and the corpus data may include code snippets or code files. The code snippets or code files contained in the code corpus do not require authorization to allow access, and the code corpus does not contain team-private code snippets or code files that require authorization to allow access. The code corpus can separate the code file text according to the method, and extract the complete method text as a set of code data. Large language model fine-tuning may refer to using the code corpus to train some parameters in a large language model, so that the large language model has the function of completing the incomplete parts of the code.

[0044] Specifically, a large amount of public code data can be collected, and code snippets or code texts that can be used for fine-tuning large language models can be sorted and extracted, thereby making a collection of code snippets or code texts as candidate code corpora. Then, the candidate code corpus is used to fine-tune the large language model, so that the large language model has code generation capabilities and becomes a code generation model. Exemplarily, the fine-tuning training of the large language model can be low-rank adaptation training (Low-Rank Adaptation, LoRA).

[0045] S102. Determine reference code information associated with the code text content to be processed from the code information included in the code database. The code validity of the reference code information is greater than the code validity of the remaining code information in the code database except the reference code information. The code validity is used to describe the code generation effect produced when the code information assists in completing the incomplete code function text content to be completed. The code information in the code database does not participate in the model training of the code generation model. The code information in the code database requires authorization before access is allowed.

[0046] In an embodiment of the present invention, a code database may refer to a database storing team private code information that requires authorization to access, and the stored code information will not participate in the fine-tuning training of large language models. Code validity may refer to the validity of the completed code function text generated after completing the incomplete code function text to be completed using code information, and can describe the completion effect of the code information on the incomplete code to be completed. Reference code information may refer to the code information in the code database that has the maximum code validity for the code text content to be processed.

[0047] Specifically, the code data that requires authorization to access can be scanned, and the code information can be collected and stored in a database to form a code database. Furthermore, based on the code text content to be processed, the code information with the maximum code validity with the code text content to be processed can be searched in the code database as the reference code information associated with the code text content to be processed.

[0048] S103. Generate text prompt word content of the code generation model based on the reference code information. The code generation model can automatically generate complete code or complete the missing part of the code according to the given incomplete code fragment through learning and recognition of code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around the topic related to the code text content indicated in the text prompt word content during the execution of the code generation task.

[0049] In an embodiment of the present invention, the code generation model can automatically generate complete code or complete the missing part of the code according to the given incomplete code fragment to be completed by learning and pattern recognition of a large amount of code data. The code generation model can understand the semantics of the incomplete code by learning the grammatical rules, semantic information and common programming logic of the code, and infer the meaning of the incomplete code by analyzing the existing code fragments, including variable definitions, function calls, control flow statements and other parts, so as to complete the incomplete code.

[0050] Specifically, the code text content to be processed and the reference code information associated with the code text content to be processed can be assembled as the text prompt word content of the code generation model. The text prompt word can guide the code generation model to generate the code text to be completed in the code text content to be processed, ensure that the generated code text to be completed is related to the code text content indicated in the text prompt word content, and ensure the validity of the code text to be completed.

[0051] Optionally, the reference code information associated with the text content of the code to be processed can also be added to the In-Context Learning (ICL) example as the text prompt content. ICL does not need to adjust the parameters of the code generation model, and only a few examples of upstream and downstream tasks can be added to obtain the desired results, avoiding the risk of code leakage.

[0052] S104 , based on the text prompt word content of the code generation model, calling the code generation model to execute the code generation task to complete the code text content to be processed to obtain the target code text content.

[0053] Specifically, the code generation model can generate the code text to be completed required by the code text content to be processed according to the input text prompt word content, and after the code text to be completed completes the code text content to be processed, the required target code text content can be obtained. In the process of completing the code text content to be processed, the text prompt word, as the key guiding information, can accurately point to the missing or required parts in the code to be processed. The code generation model intelligently completes the code to be processed based on the key guiding information, so that the code is more complete and accurate in terms of logic, syntax and function.

[0054] As an optional method, based on the text prompt word content of the code generation model, calling the code generation model to execute the code generation task to complete the code text content to be processed to obtain the target code text content includes the following steps B1-B2:

[0055] Step B1: parsing key attribute information of the code to be processed through a code generation model, where the key attribute information of the code includes at least one of code style, functional requirements, and programming language.

[0056] Step B2: Process the code text content to be processed and the text prompt word content of the code generation model into a content format supported by the code generation model through the code generation model, and use the code generation model to complete the code text content to be processed according to the text prompt word content of the code generation model and the learned code pattern to obtain the target code text content.

[0057] In an embodiment of the present invention, when the code generation model completes the text content of the code to be processed, it is necessary to identify the key attribute information of the code to be processed. Among them, the key attribute information may include at least one of the code style, functional requirements and programming language. The code style may refer to a series of conventions followed when writing code to ensure the readability, consistency and maintainability of the code. Functional requirements may refer to the specific functions that the code needs to implement. The programming language may refer to the semantics that can be recognized by the computer, such as Python, Java or C++.

[0058] Specifically, when the code generation model completes the text content of the code to be processed, the code generation model processes the text content of the code to be processed and the text prompt word content into a content format supported by the code generation model. Furthermore, the code generation model can complete the text content of the code to be processed based on the text prompt word content and the learned code pattern to obtain the target code text content.

[0059] The technical solution of the embodiment of the present invention determines the code text content to be processed input into the code generation model, the code text content to be processed is the incomplete code function text content to be completed input in the code editing and generation process; determines the reference code information associated with the code text content to be processed from the code information included in the code database, the code validity of the reference code information is greater than the code validity of the remaining code information in the code database except the reference code information, the code validity is used to describe the code generation effect produced when the code information assists in completing the incomplete code function text content to be completed, the code information in the code database does not participate in the model training of the code generation model, and the code information in the code database is authorized to be accessed, thereby realizing the extraction of code information associated with the code text content to be processed from the code database, thereby effectively controlling the use scope of the code information in the code database; the text prompt word content of the code generation model is generated based on the reference code information, and the code generation The model can automatically generate complete code or complete the missing parts of the code according to the given incomplete code fragments through learning and identifying code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around the topics related to the code text content indicated in the text prompt word content during the code generation task, and the code information of the code database is used to fill the inadequacy of the code generation model's understanding of the code text content to be processed, thereby improving the accuracy of code generation; based on the text prompt word content of the code generation model, the code generation model is called to perform the code generation task to complete the code text content to be processed to obtain the target code text content, thereby generating the target code text content based on the text prompt word content, and the code information of the code database will not participate in the training of the code generation model, thereby avoiding the risk of code leakage, and also making the generated target code text content more accurate and more in line with development specifications and code style.

[0060] Embodiment 2

[0061] Figure 2 This is a flowchart of a code generation method provided in the second embodiment of the present invention. The technical solution of this embodiment is based on the technical solution of the above embodiment, and further optimizes the reference code information associated with the code text content to be processed from the code information included in the code database in the above embodiment. The scheme not described in detail in this embodiment can be found in the above embodiment. This embodiment can be combined with various optional schemes in one or more of the above embodiments. Figure 2 As shown, the method includes:

[0062] S201. Determine the code text content to be processed that is input into the code generation model, where the code text content to be processed is the incomplete code function text content to be completed that is input during the code editing and generation process.

[0063] S202. Determine reference code function text content associated with the code text content to be processed from multiple code function text contents included in the code information in the code database, the similarity between the reference code function text content and the code text content to be processed is greater than the similarity between the remaining code function text content in the code database except the reference code function text content and the code text content to be processed, and the code information stored in the code database includes code function text content that is pre-edited and generated to meet the requirements of the code generation task and access is allowed only after authorization.

[0064] In an embodiment of the present invention, the code information in the code database may include: the text content of the code function, the text content of the function called in the code function, and the text content of the function called by the code function. The text content of the function called in the code function may refer to the relevant text content of other functions called in the code function, which needs to be searched from inside the other called functions. The text content of the function called by the code function refers to the relevant text content of other functions called, which needs to be searched from inside the other called functions, and the code function is called in the text content of the other called functions. It should be noted that the text content of the code function, the text content of the function called in the code function, and the text content of the function called by the code function included in the code information are all pre-edited and generated to meet the needs of the code generation model to generate complete code texts, and they need to be authorized to access.

[0065] Specifically, a similarity comparison is performed between the code text content to be processed and multiple code function text contents included in the code information of the code database. Further, according to the similarity comparison result, the code function text content with the greatest similarity to the code text content to be processed is determined from the multiple code function text contents, and the code function text content with the greatest similarity is used as the reference code function text content associated with the code text content to be processed.

[0066] As an optional method, determining the reference code function text content associated with the code text content to be processed from the multiple code function text contents included in the code database includes the following steps C1-C3:

[0067] Step C1, determine the code vector to be processed corresponding to the code text content to be processed, determine the reference code vector associated with the code vector to be processed from the multiple code vector data included in the code database, and the similarity between the reference code vector and the code vector to be processed is greater than the similarity between the remaining code vectors in the code database except the reference code vector and the code vector to be processed.

[0068] Step C2: determine the reference code function identifier hash summary value corresponding to the reference code vector from the code vector data included in the code database, and there is a matching code vector in the code information in the code database.

[0069] Step C3: According to the reference code function identifier hash summary value corresponding to the reference code vector, query the reference code function text content associated with the code text content to be processed from the code information included in the code database.

[0070] In an embodiment of the present invention, the code text content can be represented in the form of a vector to form a code vector, so as to improve the efficiency of similarity comparison of the code text content. Accordingly, the code vector to be processed may refer to a vector representing the code text content to be processed, and the reference code vector may refer to a vector representing the reference code function text content included in the code information of the code database. The hash summary value may refer to a digital fingerprint established for any data, which is used to distinguish different code function text contents, and the unique code function text content can be found through the hash summary value. The code vector data may store a code vector corresponding to the code function text content included in the code information of the code database and a corresponding code function identification hash summary value.

[0071] Specifically, the code vector to be processed of the code text content to be processed can be obtained by calculating the code database, and the similarity between the code vector to be processed and each code vector data in the code database can be calculated, so that the code vector data with the greatest similarity is used as the reference code vector associated with the code vector to be processed. Exemplary code vector similarity calculation methods include: TF-IDF, cosine algorithm based on space vectors, and implicit semantic indexing. Furthermore, the reference code function identifier hash summary value corresponding to the reference code vector can be determined from the code vector data, and the reference code function text content most similar to the code text content to be processed can be queried from the code information included in the code database according to the reference code function identifier hash summary value.

[0072] As an option, before determining the reference code vector associated with the code vector to be processed from the plurality of code vector data included in the code database, the method further includes:

[0073] Vector calculation is performed on the code function text content included in the code information of the code database to obtain the code vector corresponding to each code function text content and associate and store it into the code vector data.

[0074] Specifically, before obtaining the reference code vector associated with the code vector to be processed, it is necessary to calculate the code vector of the code function text content included in the code information of the code database, and store the code vector corresponding to each code function text content into the code vector data.

[0075] S203. Determine, from the code information in the code database, the calling function text content in the reference code function and the called function text content of the reference code function associated with the code text content to be processed. The code information stored in the code database also includes several calling function text contents in the code function and several called function text contents of the code function, which are pre-edited and generated to meet the requirements of the code generation task and require authorization for access.

[0076] Specifically, the code database can be used to scan the text content of the code to be processed and collect the code information of the code to be completed. Then, the corresponding calling function text content list in the code information of the code database can be queried and extracted according to the code information, as the calling function text content in the reference code function associated with the code text content to be processed. At the same time, the corresponding called function text content list in the code information of the code database can be queried and extracted according to the code information, as the called function text content of the reference code function associated with the code text content to be processed.

[0077] As an optional method, determining the calling function text content in the reference code function and the called function text content of the reference code function associated with the code text content to be processed from the code database includes the following steps D1-D3:

[0078] Step D1, parse the code text content to be processed through the code abstract syntax tree AST parsing tool to obtain AST data corresponding to the code text content to be processed.

[0079] Step D2: According to the AST data corresponding to the code text content to be processed, query the hash digest value of the calling function in the code function and the hash digest value of the called function of the code function associated with the code text content to be processed from the code information included in the code database.

[0080] Step D3: Search the code information included in the code database for the reference code function's calling function text content and the reference code function's called function text content associated with the code text content to be processed, based on the hash summary value of the calling function within the code function and the hash summary value of the called function of the code function associated with the code text content to be processed.

[0081] In an embodiment of the present invention, the code abstract syntax tree (Abstract Syntax Tree, AST) refers to the tree-like representation of the abstract syntax structure of the code. Each node of the AST represents a structure in the code, such as variables, functions, and expressions, and the node may contain child nodes representing smaller parts of the structure. When the code is scanned by the AST parsing tool, the method node of the code is first extracted, the corresponding code text is obtained, and an identification hash summary value is generated for this method. Secondly, all expression nodes under this method can be obtained by the AST parsing tool, and the calling method node can be found by calling the class name, method name, parameters and other information of each expression, and an identification hash summary value is generated for the calling method node and recorded to form a calling method identification hash summary value list. Finally, the called method node of this method is obtained by the AST parsing tool, and an identification hash summary value is generated for the called method node and recorded to form a called method identification hash summary value list, completing the construction of AST data.

[0082] Specifically, the AST parsing tool can be used to parse the code text content to be processed to obtain the AST data corresponding to the code text content to be processed. Then, according to the AST data, the code function calling function hash digest value and the code function called function hash digest value associated with the code text content to be processed are determined from the code database, so that the function text content associated with the code function calling function hash digest value is used as the reference code function calling function text content, and the function text content associated with the code function called function hash digest value is used as the reference code function called function text content.

[0083] For example, Figure 3 A schematic diagram of a storage data structure provided in Embodiment 2 of the present invention. Figure 3 As shown, the code text content to be processed takes aText as an example, which contains user code function a, calls user code function α, and is called by user code function A. The AST data obtained by the AST parsing tool for parsing the code text content to be processed is the code information data CodeInfo, and the most similar reference code function text content is found to be bText, the hash summary value of the function called in the code function is αHashList, and the hash summary value of the function called by the code function is AHashList.

[0084] S204: Determine reference code information associated with the code text content to be processed based on the reference code function text content, the calling function text content within the reference code function, and the called function text content of the reference code function.

[0085] Specifically, the reference code function text content, the calling function text content within the reference code function, and the called function text content of the reference code function may be concatenated to form reference code information associated with the code text content to be processed.

[0086] S205. Generate text prompt word content of the code generation model based on the reference code information. The code generation model can automatically generate complete code or complete the missing part of the code according to the given incomplete code fragment through learning and recognition of code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around the topic related to the code text content indicated in the text prompt word content during the execution of the code generation task.

[0087] S206 , based on the text prompt word content of the code generation model, calling the code generation model to execute the code generation task to complete the code text content to be processed to obtain the target code text content.

[0088] For example, Figure 4 This is a flow chart of a system interaction provided by Embodiment 2 of the present invention. Figure 4 As shown, a code database is formed by scanning the team code information that can only be accessed with authorization. The code database contains code information data and code vector data. When the code database receives the code generation instruction sent by the user, it calculates and obtains the associated information data of the code text content to be processed, that is, the reference code information, and returns the associated information data to the user. The user assembles the associated information data and the code text content to be processed into text prompt word content, and provides the text prompt word content as an example in ICL to the large language model. Then, the large language model returns the generated target code text content to the user. Among them, the large language model needs to undergo external code training in advance, that is, fine-tuning training based on the candidate code corpus, so that it has the ability to generate code and becomes a code generation model.

[0089] The technical solution of the embodiment of the present invention determines the code text content to be processed input to the code generation model, the code text content to be processed is the incomplete code function text content to be completed input in the code editing and generation process; determines the reference code function text content associated with the code text content to be processed from multiple code function text contents included in the code information in the code database; determines the calling function text content and the called function text content of the reference code function associated with the code text content to be processed from the code information in the code database; determines the reference code information associated with the code text content to be processed based on the reference code function text content, the calling function text content in the reference code function and the called function text content of the reference code function, so as to supplement the code structure information missing from the code text content to be processed by using the code database, provide the calling function and called function information of the code text content to be processed for the code generation model, thereby improving the accuracy of code generation; generates the code generation model based on the reference code information Text prompt word content, the code generation model can automatically generate complete code or complete the missing parts of the code according to the given incomplete code fragments to be completed through learning and identification of code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around the topics related to the code text content indicated in the text prompt word content during the execution of the code generation task, and realize the use of code information in the code database to fill the inadequacy of the code generation model's understanding of the code text content to be processed, thereby improving the accuracy of code generation; based on the text prompt word content of the code generation model, the code generation model is called to execute the code generation task to complete the code text content to be processed to obtain the target code text content, thereby realizing the generation of the target code text content based on the text prompt word content, and the code information in the code database will not participate in the training of the code generation model, thereby avoiding the risk of code leakage, and also making the generated target code text content more accurate and more in line with development specifications and code style.

[0090] Embodiment 3

[0091] Figure 5 This is a schematic diagram of the structure of a code generation device provided in the third embodiment of the present invention. The embodiment of the present invention is applicable to the case where a large language model is used to generate code text. The device can be implemented in the form of hardware and / or software and can be configured in an electronic device that implements the code generation method. Figure 5 As shown, the device comprises:

[0092] The first determination module 301 is used to determine the code text content to be processed input to the code generation model, where the code text content to be processed is the incomplete code function text content to be completed input during the code editing and generation process;

[0093] A second determination module 302 is used to determine reference code information associated with the to-be-processed code text content from the code information included in the code database, the code validity of the reference code information being greater than the code validity of the remaining code information in the code database except for the reference code information, the code validity being used to describe the code generation effect produced when the code information assists in completing the incomplete code function text content to be completed, the code information in the code database does not participate in the model training of the code generation model, and the code information in the code database is only allowed to be accessed after authorization;

[0094] A prompt word generation module 303 is used to generate text prompt word content of a code generation model based on reference code information. The code generation model can automatically generate complete code or complete missing parts in the code according to a given incomplete code fragment to be completed through learning and identifying code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around a topic related to the code text content indicated in the text prompt word content during the process of executing the code generation task;

[0095] The code completion module 304 is used to call the code generation model to execute the code generation task based on the text prompt word content of the code generation model to complete the code text content to be processed to obtain the target code text content.

[0096] Optionally, the process of determining the code generation model includes:

[0097] Obtain candidate code corpora for model fine-tuning. The candidate code corpus is a collection of code snippets or code text contents that have been collected and organized for model training. Code snippets or code files in the candidate code corpus can be accessed without authorization and do not include code snippets or code files that require authorization to access. The code corpus is code data formed based on the complete code text extracted according to the separated code file text;

[0098] The candidate code corpus is used to fine-tune the large language model to generate a code generation model, so that the code generation model has the function of completing the missing code parts of the incomplete code fragments to be completed.

[0099] Optionally, determining reference code information associated with the code text content to be processed from the code information included in the code database includes:

[0100] Determine reference code function text content associated with the code text content to be processed from a plurality of code function text contents included in the code information in the code database, wherein the similarity between the reference code function text content and the code text content to be processed is greater than the similarity between the remaining code function text contents in the code database except for the reference code function text content and the code text content to be processed, and the code information stored in the code database includes code function text content that is pre-edited and generated to meet the requirements of the code generation task and that requires authorization for access;

[0101] Determine, from the code information in the code database, the calling function text content in the reference code function and the called function text content of the reference code function associated with the code text content to be processed, wherein the code information stored in the code database also includes a plurality of calling function text contents in the code function and a plurality of called function text contents of the code function that are pre-edited and generated to meet the requirements of the code generation task and that require authorization before access is allowed;

[0102] The reference code information associated with the code text content to be processed is determined based on the reference code function text content, the calling function text content within the reference code function and the called function text content of the reference code function.

[0103] Optionally, determining a reference code function text content associated with the code text content to be processed from a plurality of code function text contents included in the code database includes:

[0104] Determine a code vector to be processed corresponding to the content of the code text to be processed, determine a reference code vector associated with the code vector to be processed from a plurality of code vector data included in a code database, and the similarity between the reference code vector and the code vector to be processed is greater than the similarity between the remaining code vectors in the code database except the reference code vector and the code vector to be processed;

[0105] Determine a reference code function identifier hash summary value corresponding to a reference code vector from code vector data included in a code database, where a matching code vector exists in the code information in the code database;

[0106] According to the reference code function identifier hash summary value corresponding to the reference code vector, the reference code function text content associated with the code text content to be processed is queried from the code information included in the code database.

[0107] Optionally, before determining the reference code vector associated with the code vector to be processed from the plurality of code vector data included in the code database, the method further includes:

[0108] Vector calculation is performed on the code function text content included in the code information of the code database to obtain the code vector corresponding to each code function text content and associate and store it into the code vector data.

[0109] Optionally, determining from the code database the calling function text content in the reference code function and the called function text content of the reference code function associated with the code text content to be processed includes:

[0110] The code text content to be processed is parsed by the code abstract syntax tree AST parsing tool to obtain the AST data corresponding to the code text content to be processed;

[0111] According to the AST data corresponding to the code text content to be processed, query the hash digest value of the calling function in the code function and the hash digest value of the called function of the code function associated with the code text content to be processed from the code information included in the code database;

[0112] According to the hash summary value of the calling function in the code function and the hash summary value of the called function of the code function associated with the code text content to be processed, the code information included in the code database is searched for the calling function text content in the reference code function and the called function text content of the reference code function associated with the code text content to be processed.

[0113] Optionally, based on the text prompt word content of the code generation model, the code generation model is called to execute the code generation task to complete the code text content to be processed to obtain the target code text content, including:

[0114] parsing key attribute information of the code to be processed through a code generation model, where the key attribute information of the code includes at least one of a code style, a functional requirement, and a programming language;

[0115] The code generation model is used to process the text content of the code to be processed and the text prompt word content of the code generation model into a content format supported by the code generation model, and the code generation model is used to complete the code text content to be processed according to the text prompt word content of the code generation model and the learned code pattern to obtain the target code text content.

[0116] The technical solution of the embodiment of the present invention determines the code text content to be processed input into the code generation model, the code text content to be processed is the incomplete code function text content to be completed input in the code editing and generation process; determines the reference code information associated with the code text content to be processed from the code information included in the code database, the code validity of the reference code information is greater than the code validity of the remaining code information in the code database except the reference code information, the code validity is used to describe the code generation effect produced when the code information assists in completing the incomplete code function text content to be completed, the code information in the code database does not participate in the model training of the code generation model, and the code information in the code database is authorized to be accessed, thereby realizing the extraction of code information associated with the code text content to be processed from the code database, thereby effectively controlling the use scope of the code information in the code database; the text prompt word content of the code generation model is generated based on the reference code information, and the code generation The model can automatically generate complete code or complete the missing parts of the code according to the given incomplete code fragments through learning and identifying code data. The text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around the topics related to the code text content indicated in the text prompt word content during the code generation task, and the code information of the code database is used to fill the inadequacy of the code generation model's understanding of the code text content to be processed, thereby improving the accuracy of code generation; based on the text prompt word content of the code generation model, the code generation model is called to perform the code generation task to complete the code text content to be processed to obtain the target code text content, thereby generating the target code text content based on the text prompt word content, and the code information of the code database will not participate in the training of the code generation model, thereby avoiding the risk of code leakage, and also making the generated target code text content more accurate and more in line with development specifications and code style.

[0117] The code generation device provided in the embodiment of the present invention can execute the code generation method provided in any embodiment of the present invention, and has the functional modules and beneficial effects corresponding to the execution method.

[0118] Embodiment 4

[0119] Figure 6A schematic diagram of the structure of an electronic device for implementing a code generation method provided in Embodiment 4 of the present invention. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or required herein.

[0120] like Figure 6 As shown, the electronic device 10 includes at least one processor 11, and a memory connected to the at least one processor 11, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., wherein the memory stores a computer program that can be executed by at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 to the random access memory (RAM) 13. In the RAM 13, various programs and data required for the operation of the electronic device 10 can also be stored. The processor 11, the ROM 12, and the RAM 13 are connected to each other through a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.

[0121] A number of components in the electronic device 10 are connected to the I / O interface 15, including: an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.

[0122] The processor 11 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special artificial intelligence (AI) computing chips, various processors running machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as a code generation method.

[0123] In some embodiments, the code generation method may be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the code generation method described above may be performed. Alternatively, in other embodiments, the processor 11 may be configured to perform the code generation method in any other suitable manner (e.g., by means of firmware).

[0124] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), load programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0125] Computer programs for implementing the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that when the computer program is executed by the processor, the functions / operations specified in the flow chart and / or block diagram are implemented. The computer program may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.

[0126] In the context of the present invention, a computer-readable storage medium may be a tangible medium that may contain or store a computer program for use by or in combination with an instruction execution system, device or equipment. A computer-readable storage medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium may be a machine-readable signal medium. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0127] To provide interaction with a user, the systems and techniques described herein may be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and a pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices may also be used to provide interaction with the user; for example, the feedback provided to the user may be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user may be received in any form (including acoustic input, voice input, or tactile input).

[0128] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.

[0129] A computing system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The client and server relationship is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services.

[0130] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps described in the present invention can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solution of the present invention can be achieved, and this document does not limit this.

[0131] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.

Claims

1. A code generation method, characterized in that: The method comprises: Determine the code text content to be processed input to the code generation model, wherein the code text content to be processed is the incomplete code function text content to be completed input during the code editing and generation process; Determine reference code information associated with the code text content to be processed from the code information included in the code database, wherein the code validity of the reference code information is greater than the code validity of the remaining code information in the code database except the reference code information, and the code validity is used to describe the code generation effect generated when the code information assists in completing the incomplete code function text content to be completed, the code information in the code database does not participate in the model training of the code generation model, and the code information in the code database needs to be authorized before access is allowed; Generate text prompt word content of a code generation model based on the reference code information, the code generation model can automatically generate complete code or complete the missing part of the code according to the given incomplete code fragment to be completed through learning and recognition of code data, and the text prompt word content can guide the code generation model to ensure that the generated code text content is carried out around the theme related to the code text content indicated in the text prompt word content during the process of executing the code generation task; Based on the text prompt word content of the code generation model, the code generation model is called to execute the code generation task to complete the code text content to be processed to obtain the target code text content.

2. The method according to claim 1, characterized in that The process of determining the code generation model includes: Obtain candidate code corpora for model fine-tuning, wherein the candidate code corpora are a collection of code snippets or code text contents that have been collected and organized for model training, wherein the code snippets or code files in the candidate code corpus can be accessed without authorization and do not include code snippets or code files that require authorization to be accessed, and the code corpus is code data formed based on the complete code text extracted according to the separated code file text; The candidate code corpus is used to fine-tune the large language model to generate the code generation model, so that the code generation model has the function of completing the missing code parts of the incomplete code fragments to be completed.

3. The method according to claim 1, characterized in that Determining reference code information associated with the code text content to be processed from the code information included in the code database includes: Determining reference code function text content associated with the code text content to be processed from a plurality of code function text contents included in the code information in the code database, wherein the similarity between the reference code function text content and the code text content to be processed is greater than the similarity between the remaining code function text contents in the code database except for the reference code function text content and the code text content to be processed, and the code information stored in the code database includes code function text content that is pre-edited and generated to meet the requirements of the code generation task and that requires authorization to be accessed; Determine, from the code information in the code database, the calling function text content in the reference code function and the called function text content in the reference code function associated with the code text content to be processed, wherein the code information stored in the code database also includes a plurality of calling function text contents in the code function and a plurality of called function text contents in the code function that are pre-edited and generated to meet the requirements of the code generation task and that require authorization before access is allowed; The reference code information associated with the code text content to be processed is determined based on the reference code function text content, the calling function text content within the reference code function and the called function text content of the reference code function.

4. The method according to claim 3, characterized in that Determining reference code function text content associated with the code text content to be processed from a plurality of code function text contents included in the code database includes: Determine a code vector to be processed corresponding to the content of the code text to be processed, and determine a reference code vector associated with the code vector to be processed from a plurality of code vector data included in a code database, wherein the similarity between the reference code vector and the code vector to be processed is greater than the similarity between the remaining code vectors in the code database except the reference code vector and the code vector to be processed; Determine a reference code function identifier hash summary value corresponding to the reference code vector from code vector data included in a code database, wherein a matching code vector exists in the code information in the code database; According to the reference code function identifier hash summary value corresponding to the reference code vector, the reference code function text content associated with the code text content to be processed is queried from the code information included in the code database.

5. The method according to claim 4, characterized in that Before determining the reference code vector associated with the code vector to be processed from the plurality of code vector data included in the code database, the method further includes: Vector calculation is performed on the code function text content included in the code information of the code database to obtain the code vector corresponding to each code function text content and associate and store it into the code vector data.

6. The method according to claim 3, characterized in that Determine from the code database the calling function text content in the reference code function and the called function text content of the reference code function associated with the code text content to be processed, including: Parse the code text content to be processed by using a code abstract syntax tree (AST) parsing tool to obtain AST data corresponding to the code text content to be processed; According to the AST data corresponding to the code text content to be processed, query the code information included in the code database for the hash digest value of the calling function in the code function associated with the code text content to be processed and the hash digest value of the called function of the code function; According to the hash summary value of the calling function in the code function and the hash summary value of the called function of the code function associated with the code text content to be processed, the code information included in the code database is searched for the calling function text content in the reference code function and the called function text content of the reference code function associated with the code text content to be processed.

7. The method according to claim 1, characterized in that Based on the text prompt word content of the code generation model, calling the code generation model to execute the code generation task to complete the code text content to be processed to obtain the target code text content, including: Parsing key code attribute information of the code to be processed by using the code generation model, wherein the key code attribute information includes at least one of code style, functional requirements, and programming language; The code generation model is used to process the code text content to be processed and the text prompt word content of the code generation model into a content format supported by the code generation model, and the code generation model is used to complete the code text content to be processed according to the text prompt word content of the code generation model and the learned code pattern to obtain the target code text content.

8. A code generating device, characterized in that: The device comprises: A first determination module is used to determine the to-be-processed code text content input to the code generation model, wherein the to-be-processed code text content is the incomplete to-be-completed code function text content input during the code editing and generation process; A second determination module is used to determine reference code information associated with the to-be-processed code text content from the code information included in the code database, wherein the code validity of the reference code information is greater than the code validity of the remaining code information in the code database except for the reference code information, and the code validity is used to describe the code generation effect generated when the code information assists in completing the incomplete code function text content to be completed, the code information in the code database does not participate in the model training of the code generation model, and the code information in the code database needs to be authorized before access is allowed; A prompt word generation module, used to generate text prompt word content of a code generation model based on the reference code information, wherein the code generation model can automatically generate complete code or complete missing parts in the code according to a given incomplete code fragment to be completed through learning and recognition of code data, and the text prompt word content can guide the code generation model to ensure that the generated code text content is generated and completed around a topic related to the code text content indicated in the text prompt word content during the process of executing the code generation task; The code completion module is used to call the code generation model to execute the code generation task based on the text prompt word content of the code generation model to complete the code text content to be processed to obtain the target code text content.

9. An electronic device, characterized in that: The electronic device comprises: at least one processor; and a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the code generation method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the code generation method according to any one of claims 1 to 7 when executed.

Citation Information

Cited By

  • Project-level code generation method and device based on file sorting, computer equipment and storage medium

    CN121116264A

  • Code generation method and device, computer equipment, storage medium and product

    CN121209887A