A method, device, equipment and medium for automatically generating code
By understanding the semantic intentions of user code development requests and generating business logic processing processes, combining large language models to generate flexible code and test cases, the problem of lack of scalability of code generation and inability to support multiple associated object analysis in the existing technology is solved. , realizes efficient software development and testing.
Patent Information
- Application Number
- CN202411286464.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-13
- Publication Date
- 2025-02-07
- Estimated Expiration
- 2044-09-13
AI Technical Summary
The prior art cannot dynamically generate diverse code in software development, and lacks scalability, so it cannot support input of multiple different types of associated objects and analyze them.
By understanding the semantic intention of user code development requests, a business logic processing process is generated, and the associated input parameters are determined based on this, and flexible code and test cases are generated using a large language model.
It realizes logical reasoning like a software developer, dynamically generates complex and professional code based on the structure of input, output, and data source, improves development efficiency, and generates test cases to improve testing efficiency.
Smart Images

Figure CN118796180B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software development, and in particular to a method, device, equipment and medium for automatically generating codes. Background Art
[0002] In software development, code can be automatically generated based on fixed data structures. The purpose of automatic code generation is to improve software development efficiency and reduce errors. By automatically generating code, software developers can save a lot of time and energy. Compared with manual code writing, code generation can quickly generate a large amount of template code, reducing repetitive work.
[0003] However, the code generated by the above method of automatically generating code based on a fixed data structure is relatively fixed and rigid, and has no subsequent scalability. It only supports the input of one structural object to generate the corresponding code for the object, and does not support the input and analysis of multiple different types of related objects. Therefore, the above method cannot perform logical reasoning like software developers and dynamically generate diversified codes based on the structure of input, output, and data sources combined with the text description of requirements. Summary of the invention
[0004] In view of this, an embodiment of the present invention provides a method, apparatus, device and medium for automatically generating codes, so as to solve the problem that the existing automatically generated codes have no scalability and do not support input and analysis of multiple related objects of different types.
[0005] According to a first aspect, an embodiment of the present invention provides a method for automatically generating a code, the method comprising:
[0006] Obtaining a code development request from a user; the code development request includes an application scenario specified by the user and a full logical processing flow of the application scenario;
[0007] Perform semantic intent understanding on the code development request to obtain an intent understanding result, and use the full logic processing flow and the intent understanding result to match with a preset knowledge base, complete the full logic processing flow, and generate a business logic processing flow; the intent understanding result represents the personal intent understanding of the user's code development request;
[0008] Based on the business logic processing flow, determining the associated input parameters required to complete the business logic processing flow; the associated input parameters include uniform resource locator path parameters, table parameters and text description parameters;
[0009] Performing callback call processing on all the associated input parameters to obtain callback data for each associated input parameter, and using the code generation chain of the trained large language model to generate a first teleprompter template and a first user message template for the callback data respectively;
[0010] Processing the callback data based on the first teleprompter template and the first user message template to generate first input data, and inputting the first input data into the large language model to obtain a code output by the large language model; the context information of the first input data includes the associated input parameters and the business logic processing flow;
[0011] A test case generation chain of the large language model is used to generate a second teleprompter template and a second user message template for the code, respectively. The code is processed based on the second teleprompter template and the second user message template to generate second input data. The second input data is input into the large language model to obtain a test case for the code output by the large language model.
[0012] In combination with the first aspect, in the first implementation of the first aspect, the intent understanding result is obtained through a trained semantic understanding model, and the semantic understanding model is trained based on sample text, sample intent understanding results corresponding to the sample text, and knowledge background description text, and the knowledge background description text represents the feature information of the natural language description of the knowledge background field corresponding to the request text.
[0013] In combination with the first implementation of the first aspect, in the second implementation of the first aspect, the intention understanding result is obtained by the following steps:
[0014] Converting the code development request into a request text;
[0015] The request text is input into the trained semantic understanding model to obtain the intent understanding result output by the semantic understanding model.
[0016] In combination with the second implementation of the first aspect, in the third implementation of the first aspect, the semantic understanding model is trained by the following steps:
[0017] Acquire the sample text, and determine the sample intention understanding result of the sample text;
[0018] Matching the sample text with a preset knowledge base to obtain the knowledge background description text corresponding to the sample text;
[0019] The sample text and the knowledge background description text are used as input data for training, and the sample intent understanding results are used as label data. A supervised training method is adopted to train a semantic understanding model for outputting intent understanding results of code development requests.
[0020] In combination with the first aspect, in a fourth implementation of the first aspect, determining the associated input parameters required to complete the business logic processing flow based on the business logic processing flow specifically includes:
[0021] Based on the business logic process flow, determine the process characteristics required to complete the business logic process flow;
[0022] Based on the process characteristics, a first sub-parameter and a second sub-parameter of each associated input parameter are obtained; the first sub-parameter is a necessary input parameter, and the second sub-parameter is a non-necessary input parameter.
[0023] In combination with the first aspect, in a fifth implementation of the first aspect, the method generates a first teleprompter template, a second teleprompter template, a first user message template, a second user message template, a code generation chain, and a test case generation chain through a LangChain corresponding to the large language model:
[0024] In combination with the first aspect, in a sixth implementation of the first aspect, the method further includes the following steps:
[0025] Deploy the code generation environment, determine the service type of the code generation system, and set the address of the system's standard resources when the service type meets the preset type, and determine the database table structure and the list of at least one database table that the address request of the standard resource needs to operate.
[0026] According to a third aspect, an embodiment of the present invention further provides an automatic code generation device, the device comprising:
[0027] A request acquisition module is used to acquire a user's code development request; the code development request includes an application scenario specified by the user and the entire logical processing flow of the application scenario;
[0028] A process determination module is used to understand the semantic intent of the code development request, obtain an intent understanding result, and use the full logic processing flow and the intent understanding result to match with a preset knowledge base, complete the full logic processing flow, and generate a business logic processing flow; the intent understanding result represents the personal intent understanding of the user's code development request;
[0029] A parameter determination module, used to determine the associated input parameters required to complete the business logic processing flow based on the business logic processing flow; the associated input parameters include uniform resource locator path parameters, table parameters and text description parameters;
[0030] The data calling module is used to perform callback calling processing on all the associated input parameters, obtain callback data of each associated input parameter, and use the code generation chain of the trained large language model to generate a first teleprompter template and a first user message template of the callback data respectively;
[0031] a code generation module, configured to process the callback data based on the first teleprompter template and the first user message template to generate first input data, and input the first input data into the large language model to obtain a code output by the large language model; the context information of the first input data includes the associated input parameters and the business logic processing flow;
[0032] A code testing module is used to use the test case generation chain of the large language model to generate a second teleprompter template and a second user message template for the code, process the code based on the second teleprompter template and the second user message template to generate second input data, and input the second input data into the large language model to obtain a test case for the code output by the large language model.
[0033] According to a third aspect, an embodiment of the present invention further provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the steps of the automatic code generation method as described in any one of the above are implemented.
[0034] According to a fourth aspect, an embodiment of the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of any of the above-described methods for automatically generating codes.
[0035] According to a fifth aspect, an embodiment of the present invention further provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the steps of the method for automatically generating code as described in any one of the above items are implemented.
[0036] The automatic code generation method, device, equipment and medium of the present invention understand the semantic intent of the user code development request to obtain the intent understanding result, and complete the entire logical processing flow to obtain the business logic processing flow, and then determine the associated input parameters required to complete the business logic processing flow based on the business logic processing flow, and then explore the associated objects that may be needed in the user's software development process by understanding the specific requirements of each user, so as to facilitate the subsequent display and implementation of richer and more complete processing flows and the processing return results corresponding to each processing flow. In this way, logical reasoning can be performed like software developers, and according to the structure of input, output, and data sources, combined with the text description of the requirements, greater flexibility and scalability are provided, and then the logical reasoning ability of the large language model is utilized to complete the generation of flexible code containing logical processing by constructing an appropriate call chain, and at the same time, the test cases corresponding to the code can be generated, thereby improving software development efficiency. Compared with the existing code generation solutions that only support the input of one structural object, it can dynamically generate diversified, more complex and more professional codes based on multiple associated input parameters and business logic processing flows. Through this automated code generation process, development efficiency can be greatly improved. At the same time, test cases for the code are generated, thereby improving testing efficiency. Testers no longer need to write test cases manually, but can directly use the generated test cases. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] The features and advantages of the present invention will be more clearly understood by referring to the accompanying drawings, which are schematic and should not be construed as limiting the present invention in any way. In the accompanying drawings:
[0038] Figure 1 One of the flow charts of the automatic code generation method provided by the present invention is shown;
[0039] Figure 2 The second flowchart of the method for automatically generating codes provided by the present invention is shown;
[0040] Figure 3 A schematic diagram showing the structure of the automatic code generation device provided by the present invention;
[0041] Figure 4 It is a schematic diagram of the hardware structure of the electronic device provided in the embodiment of the present application. DETAILED DESCRIPTION
[0042] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution 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 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 those skilled in the art without creative work are within the scope of protection of the present invention.
[0043] In software development, code can be automatically generated based on a fixed data structure. The purpose of automatic code generation is to improve software development efficiency and reduce errors.
[0044] By automatically generating code, software developers can save a lot of time and energy. Compared with manual code writing, code generation can quickly generate a large amount of template code, reducing repetitive work. It is precisely because software developers are prone to errors in the process of manually writing code, and the automatic code generation method can ensure that the generated code conforms to the expected data structure and specifications. This can reduce human errors and improve the quality and reliability of the code. Automatic code generation can also generate a unified code style based on predefined templates and rules, which can ensure the consistency of the code style throughout the project, improve the readability and maintainability of the code, and can also abstract commonly used code snippets into reusable templates, so that these code templates can be reused in different projects to improve the reusability and maintainability of the code.
[0045] In some cases, when the data structure needs to change, manually modifying the code may be very tedious and error-prone, while automatic code generation can automatically update the generated code according to the new data structure, thereby quickly adapting to the changes.
[0046] In short, the method of automatically generating code based on fixed data structures can improve development efficiency, reduce errors, unify code style, support code reuse and rapid iteration. It is a very useful technology in software development.
[0047] For example, the MyBatis Plus tool is an enhanced tool based on MyBatis, which provides many convenient functions to simplify the software development process. One of them is the automatic code generator, which can automatically generate corresponding entity classes, Mapper interfaces and XML mapping files according to the database table structure.
[0048] Specifically, the automatic code generator principle of the MyBatis Plus tool is as follows:
[0049] First, you need to configure the database connection information, including database type, URL, user name, password, etc. After that, the code generator will connect to the database through the Java language (Java DataBase Connectivity, JDBC) to obtain the database table structure information, including table name, column name, data type, primary key, etc., to parse the database (single) table structure. After that, the automatic code generator will convert the database table structure into the corresponding entity class, Mapper interface and XML mapping file according to the configured template. The template generally includes the skeleton and placeholders of the code, and the final code is generated by replacing the placeholders. In the process of generating code, some additional customization can be made through the configuration file, such as specifying the package path of the generated code, whether to generate comments, whether to generate paging query methods, etc. Finally, the automatic code generator will output the generated code to the specified directory for software developers to use.
[0050] Existing methods for automatically generating code based on fixed data structures require a known fixed structure input, such as a table structure, a Web Services Description Language (WSDL) interface definition, etc., so that each parameter or variable in the predefined input structure can be extracted, combined with a predefined model engine and the logic of generating code, to generate executable code of a unified style. The basic process is to convert the variables defined in the template into a field in the input and implement simple loop and branch processing.
[0051] However, the code generated by the method of automatically generating code based on a fixed data structure is relatively fixed and rigid, and has no subsequent scalability. It only supports the input of one structural object to generate the corresponding code for the object, and does not support the input and analysis of multiple different types of related objects. Therefore, the above method cannot perform logical reasoning like software developers and dynamically generate diversified codes based on the structure of input, output, and data sources combined with the text description of requirements.
[0052] In order to solve the above problems, a method for automatically generating codes is provided in this specification, which aims to dynamically generate diversified, more complex and more professional codes, greatly improve development efficiency, and generate test cases for the codes to improve testing efficiency. The method for automatically generating codes provided in this specification can be applied to electronic devices with software development functions. The electronic devices may include notebooks, desktop computers, smart phones, smart wearable devices (virtual reality glasses, smart watches, etc.), tablet computers, etc. Of course, the method for automatically generating codes provided in this specification can also be applied to applications running in the above-mentioned electronic devices. For example, the method for automatically generating codes can be applied to browsers with software development functions, or to browsers with software development functions. Figure 1 is a flow chart of a method for automatically generating code according to an embodiment of the present invention, such as Figure 1 As shown, the method may include the following steps:
[0053] S20. Obtain a code development request from a user. In this embodiment, the code development request at least includes: an application scenario specified by the user and a full logical processing flow of the application scenario, that is, a complete logical processing flow.
[0054] To give a specific example, a code generation system (tool) using this method can provide users, i.e., software developers, with at least one human-computer interaction method to allow users to input corresponding code development requests. For example, an input text box can be provided to users to allow users to input text-type code development requests. It can also be a variety of human-computer interaction methods such as voice input, gesture input, etc.
[0055] S30. Understand the semantic intent of the code development request to obtain the intent understanding result, and use the full logical processing flow and the intent understanding result to match with the preset knowledge base, complete the full logical processing flow, and generate a business logic processing flow.
[0056] The intent understanding result represents the personal intent understanding of the user's code development request, and is used to represent the code that the user expects to generate and implement. The full logical processing flow given by the user himself may not include all the processing flows of the service in the business scenario. Therefore, in this embodiment, the semantic intent of the user's code development request is understood to understand the specific requirements of each user, and then field matching is performed based on the preset knowledge base that has been formed to mine the related objects that may be needed in the user's software development process, so as to facilitate the subsequent display and implementation of richer and more complete processing flows and the processing return results corresponding to each processing flow.
[0057] In this embodiment, the preset knowledge base is a database constructed based on historical software development codes, expert experience data, business historical processing flows, knowledge domain data and other data, so that the entire logical processing flow and intent understanding can be obtained. The preset knowledge base can be used to complete the entire logical processing flow to form a richer and more complete business processing flow, thereby generating more complex and professional codes.
[0058] Taking the user-specified application scenario of "product outbound query" as an example, the full logical processing flow given by the user is as follows: according to the product number, verify its correctness in the product table and determine whether the product status is normal; if the inventory is sufficient, update the product inventory quantity in the inventory system, create an outbound record and save it to the inbound and outbound record table; return a successful response, including a successful outbound message or a unique identifier of the outbound record; if the outbound fails, such as insufficient inventory or other errors, return an appropriate failure response, including an error message, etc.
[0059] When the semantic intent understanding of the user's code development request shows that the user's expected implementation is "product outbound query", the above-mentioned full logical processing flow contained in the user's code development request is matched with the intent understanding result obtained after the semantic intent understanding and the preset knowledge base by vector field matching, etc., and the processing flow of "checking whether there is enough product inventory in the inventory system to meet the outbound request" can be added. At this time, the business logic processing flow is the user's full logical processing flow plus the processing flow of "checking whether there is enough product inventory in the inventory system to meet the outbound request".
[0060] In this embodiment, a user's code development request is parsed by a trained semantic understanding model, thereby obtaining the above-mentioned intention understanding result of the user's code development request.
[0061] Preferably, the intention understanding result is obtained by the following steps:
[0062] A10. Convert the code development request into a request text.
[0063] A20. Input the request text into the trained semantic understanding model to obtain the intent understanding result output by the semantic understanding model, wherein the semantic understanding model is trained based on sample text, sample intent understanding results corresponding to the sample text, and knowledge background description text. Such a trained semantic understanding model can understand the semantic intent of the information contained in the request text and extract the user's real software development needs.
[0064] The sample intent understanding result corresponding to the sample text is obtained by manual annotation by the user, and can also be obtained by annotation combined with expert experience. In addition, the sample intent understanding result can be understood as the label information used in the training process of the semantic understanding model.
[0065] In this embodiment, the knowledge background description text represents the feature information (text) of the natural language description of the knowledge background field corresponding to the request text. Such a trained semantic understanding model can perform more refined semantic understanding and intent extraction based on the specific application scenarios in user software development, fully understand and explore the user's development needs, and then assist in generating more complex and professional code.
[0066] More specifically, the training process of the semantic understanding model is:
[0067] Obtaining a sample text, and determining a sample intention understanding result of the sample text;
[0068] Match the sample text with the above-mentioned preset knowledge base to obtain the knowledge background description text corresponding to the sample text;
[0069] The sample text and the knowledge background description text are used as input data for training, and the sample intent understanding results are used as label data. A supervised training method is adopted to train a semantic understanding model for outputting the intent understanding results of code development requests.
[0070] During the training process, the preset knowledge base will still be used to find and match relevant knowledge background information to obtain the knowledge background description text. At the same time, the knowledge background description text can also be used to supplement and improve the data of the preset knowledge base.
[0071] S40, based on the business logic processing flow, determine the associated input parameters required to complete the business logic processing flow. Since the business logic processing flow is a complete and clear business flow for a certain application scenario, the associated input parameters can be obtained according to the business logic processing flow, and the specific content of the associated input parameters is different according to different application scenarios. These associated input parameters are related to each other so as to cooperate and realize each process in the business logic processing flow.
[0072] In this embodiment, the associated input parameters include a Uniform Resource Locator (URL) path parameter, a table parameter, and a text description parameter.
[0073] Let's take the user-specified application scenario of "product outbound query" as an example. The URL path parameter is used to define the interface unique identifier, that is, to determine the URL path for providing external services. It can include sub-parameters such as product number (product_id), outbound quantity (quantity), outbound reason (reason), success response, and failure response. The table parameter is used to define the table name involved in the service, which can include sub-parameters such as product table, inventory table, and inbound and outbound record table. The text description parameter is used to describe each processing process of the service, which is a descriptive text information for each processing process.
[0074] S50, performing callback call processing on all associated input parameters obtained, obtaining callback data of each associated input parameter, and using the code generation chain of the trained large language model (LLM) to generate a first teleprompter template and a first user message template of the callback data respectively.
[0075] S60. Process the callback data based on the first teleprompter template and the first user message template to generate the first input data, and input the first input data into the large language model to generate code. Specifically, the user can use an API document editor tool such as Swagger Edit to manage the interface definition based on the designed interface, and can generate a Swagger format export file. The corresponding interface callback function can provide an interface query service based on the export file, and input at least one URL to return the definition of the interface, including the URL, request parameters, response parameters, etc. The user can also directly use the database to obtain the table structure definition based on the completed database table structure design, or use the database export tool to export the table structured query language (SQL) into a file, and then provide a table structure query service based on the data source, and input at least one table name to return the definition information of the table, including the table name, field type, field name, primary key, index information, etc.
[0076] S70, using the test case generation chain of the trained large language model, respectively generate a second teleprompter template and a second user message template for the code, process the code based on the second teleprompter template and the second user message template, generate second input data, and input the second input data into the large language model to generate a test case for the code. The test case is data obtained by combining context information, different teleprompter information and generated code similar to the code, the purpose is to verify the correctness and coverage of the code, and different situations can be tested by simulating different inputs and expected outputs. The test cases generated in this way can be expanded and modified according to actual conditions.
[0077] LLM is a natural language processing model based on deep learning, which usually has billions or even hundreds of billions of parameters. These models are able to understand and generate natural language texts and perform various language tasks such as text generation, translation, question answering, summarization, sentiment analysis, etc. by training on large-scale text data. LLM can accept a piece of text as input data, and then predict the possibility of the next word (single time) or a piece of text to generate a coherent sentence. This makes LLM widely used in natural language processing tasks such as machine translation, text summarization, dialogue generation, etc. Some well-known large language models include the GPT (Generative Pre-trained Transformer) series of models and the BERT (Bidirectional Encoder Representations from Transformers) model. These models utilize a large amount of Internet text data during training, which enables them to generate high-quality natural language texts and achieve significant performance improvements on multiple natural language processing tasks.
[0078] Typically, an LLM has the following abilities:
[0079] Language expression ability: LLM has strong language expression ability and can understand and generate natural language text. It can receive instructions or questions input by humans and answer or perform corresponding tasks in the form of natural language. This ability enables the large language model to have natural and fluent conversations with humans, so as to better understand user needs and provide accurate responses;
[0080] Rich knowledge: LLM has accumulated a rich knowledge base by training on a large amount of text data. It can provide a wide range of domain knowledge and information through learning and understanding this knowledge, enabling LLM to answer various questions, provide detailed explanations and background knowledge, and have high professionalism in applications in different fields;
[0081] Logical reasoning ability: LLM has a certain logical reasoning ability, can infer and analyze based on the given information, can identify and understand the logical relationship between sentences, so as to be able to solve problems, infer and reason. This ability enables LLM to deal with complex problems and provide reasonable solutions or infer conclusions.
[0082] In LLM, a prompt is a piece of text or instruction that the user inputs to LLM to guide LLM to generate the corresponding output. Specifically, a prompt can be a question, an incomplete sentence, a task description, or any other form of text that is intended to clearly tell LLM what the user wants it to generate. The design and quality of the prompt have a great impact on the accuracy and relevance of the generated results. Prompts are usually clear, specific, and contextual, which can help LLM better understand the user's needs and generate more expected output.
[0083] In particular, Prompt consists of the following four elements: instruction, context, input data to be processed, and output data. Output data is also an output indicator, which is used to specify the type or format of the output.
[0084] In this embodiment, the context information of the first input data includes the above-mentioned associated input parameters and business logic processing flow.
[0085] In this embodiment, since LLM is the core of processing, it is necessary to select a suitable LLM for processing. If data security is involved, it is necessary to deploy a suitable large language model locally and provide a running environment and corresponding calling API: If data security is not involved in the use of the tool, you can use the API to call advanced large language models such as gpt-4o, qwen2-max, GPT3.5 for reasoning. Different LLMs will determine the specific content of the output data, i.e. code, and the test cases of the code.
[0086] Let's take the user-specified application scenario of "product outbound query" as an example to illustrate. The generated code is as follows:
[0087] # System message template (language programming language, framework programming framework)
[0088] template = """You are a {language} code generator. Please use {framework} as the basic framework to generate detailed, elegant, and well-structured {language} implementation code based on the given interface document definition, the table structure of the interface operations, and the interface business logic design. Please be careful not to miss any details."""
[0089] # Used for message templates (interfaceDefine interface definition, tableDefine table structure, interfaceDesc interface business logic)
[0090] human_template = """This is the interface definition: {interfaceDefine}
[0091] This is the interface involving the table structure: {tableDefine}
[0092] This is the interface business logic: {interfaceDesc}"""
[0093] In this way, source code of different languages and frameworks can be generated, such as Java code under the SpringBoot framework, Python code under the Flask framework, etc. The generated code is one or more, generated according to the convention of the corresponding teleprompter.
[0094] # System message template (language programming language, testFramework test case framework)
[0095] template = """You are a {language} code test case generator. Please generate test cases based on the given {language} code. Use {testFramework} as the test framework. Please generate test cases for the given code. The higher the test coverage, the better."""
[0096] # For message templates (code generated by generateCode)
[0097] human_template = "The code is as follows: {generateCode}"
[0098] In this embodiment, the corresponding teleprompter template, user message template, code generation chain and test case generation chain are generated by LangChain supporting LLM. The generated code generation chain and test case generation chain have high scalability.
[0099] LangChain is a framework for developing LLM-driven applications. LangChain allows LLM to connect to other data sources, interact with the environment, and achieve more powerful functions. Users can use LangChain's ability to form call links with multiple connection objects to help developers build end-to-end applications using language models. It provides a set of tools, components, and interfaces that simplify the process of creating applications powered by large language models. LangChain is an intermediate layer that links between user-oriented programs and LLMs, and customizes users' own LLMs by inputting relevant knowledge bases.
[0100] It should be noted that, of course, the LangChain tool can be omitted, and the user can encode the above teleprompter template, user message template, code generation chain, and test case generation chain by themselves.
[0101] The automatic code generation method of the present invention understands the semantic intent of the user's code development request to obtain the intent understanding result, and completes the entire logical processing flow to obtain the business logic processing flow, and then determines the associated input parameters required to complete the business logic processing flow based on the business logic processing flow, and then mines the associated objects that may be needed in the user's software development process by understanding the specific requirements of each user, so as to facilitate the subsequent display and implementation of richer and more complete processing flows and the processing return results corresponding to each processing flow. In this way, logical reasoning can be performed like software developers, and according to the structure of input, output, and data sources, combined with the text description of the requirements, greater flexibility and scalability are provided, and then the logical reasoning ability of the large language model is utilized to complete the generation of flexible code containing logical processing by constructing an appropriate call chain, and at the same time, the test case corresponding to the code can be generated, thereby improving software development efficiency. Compared with the existing code generation solutions that only support the input of one structural object, it can dynamically generate diversified, more complex and more professional codes based on multiple associated input parameters and business logic processing flows. Through this automated code generation process, development efficiency can be greatly improved. At the same time, test cases for the code are generated, thereby improving testing efficiency. Testers no longer need to write test cases manually, but can directly use the generated test cases.
[0102] Combine the following Figure 2 The automatic generation method of the code of the present invention is described, and the method also includes the following steps:
[0103] S10, deploying a code generation environment, which can be understood as a pre-environment for automatic code generation. In this embodiment, it is necessary to determine the type of system, specifically, the system for generating code needs to be a Web service type, and set the address of the system's standard resources, i.e., the URL path. The software developer also needs to determine the database table structure and the address of the standard resource request to operate at least one database table.
[0104] After completing the above operations, the deployment of the code generation environment can be completed, so as to clarify the context information of the LLM during the subsequent code generation. The deployed code generation environment can also be added to the context information of the first input data.
[0105] In this embodiment, step S40 specifically includes:
[0106] S41. Based on the business logic processing flow, determine the process characteristics required to complete the business logic processing flow.
[0107] S42. Based on the process characteristics, obtain a first sub-parameter and a second sub-parameter of each associated input parameter, wherein the first sub-parameter is a necessary input parameter and the second sub-parameter is a non-necessary input parameter.
[0108] For example, the product number (product_id) in the URL path parameter indicates the unique identifier of the product to be shipped, and the quantity (quantity) indicates the quantity of the product to be shipped. These two sub-parameters are the first sub-parameters of the URL path parameter. The reason for shipping indicates the reason for shipping the product, such as sales, returns, etc. This parameter is the second sub-parameter of the URL path parameter. The product table and inventory table are the first sub-parameters of the table parameter, and the in-and-out record table can be the second sub-parameter of the table parameter.
[0109] In this embodiment, after obtaining the associated input parameters of each item, the integrity and validity of each sub-parameter (the first and second sub-parameters) in the associated input parameters will be verified to ensure that the required fields exist and are legal. In particular, in the text description parameter, for example, according to the product number, its correctness is verified in the product table and whether the status of the product is normal; check whether there is enough inventory in the inventory system to meet the outbound request; if the inventory is sufficient, update the inventory quantity of the product in the inventory system; create an outbound record and save it to the inbound and outbound record table; return a successful response, including a message of successful outbound or a unique identifier of the outbound record; if the outbound fails, such as insufficient inventory or other errors, return an appropriate failure response, including an error message, etc., and the integrity and validity of the processing flow will also be verified.
[0110] The following is a description of an automatic code generation device provided by an embodiment of the present invention. The automatic code generation device described below and the automatic code generation method described above can refer to each other.
[0111] In order to solve the above problems, this specification provides an automatic code generation device, which aims to provide a database performance optimization solution with high efficiency, low cost, high automation and good scalability. Figure 3 is a schematic diagram of the structure of an automatic code generation device according to an embodiment of the present invention, such as Figure 3 As shown, the device may include:
[0112] The request acquisition module 10 is used to acquire a user's code development request. In this embodiment, the code development request at least includes: an application scenario specified by the user and a full logical processing flow of the application scenario, that is, a complete logical processing flow.
[0113] To give a specific example, a code generation system (tool) using this method can provide users, i.e., software developers, with at least one human-computer interaction method to allow users to input corresponding code development requests. For example, an input text box can be provided to users to allow users to input text-type code development requests. It can also be a variety of human-computer interaction methods such as voice input, gesture input, etc.
[0114] The process determination module 20 is used to understand the semantic intent of the code development request, obtain the intent understanding result, and use the full logical processing flow and the intent understanding result to match with the preset knowledge base, complete the full logical processing flow, and generate a business logic processing flow.
[0115] The intent understanding result represents the personal intent understanding of the user's code development request, and is used to represent the code that the user expects to generate and implement. The full logical processing flow given by the user himself may not include all the processing flows of the service in the business scenario. Therefore, in this embodiment, the semantic intent of the user's code development request is understood to understand the specific requirements of each user, and then field matching is performed based on the preset knowledge base that has been formed to mine the related objects that may be needed in the user's software development process, so as to facilitate the subsequent display and implementation of richer and more complete processing flows and the processing return results corresponding to each processing flow.
[0116] In this embodiment, the preset knowledge base is a database constructed based on historical software development codes, expert experience data, business historical processing flows, knowledge domain data and other data, so that the entire logical processing flow and intent understanding can be obtained. The preset knowledge base can be used to complete the entire logical processing flow to form a richer and more complete business processing flow, thereby generating more complex and professional codes.
[0117] In this embodiment, a user's code development request is parsed by a trained semantic understanding model, thereby obtaining the above-mentioned intention understanding result of the user's code development request.
[0118] The parameter determination module 30 is used to determine the associated input parameters required to complete the business logic processing flow based on the business logic processing flow. Since the business logic processing flow is a complete and clear business flow for a certain application scenario, the associated input parameters can be obtained according to the business logic processing flow, and the specific content of the associated input parameters is different according to different application scenarios. These associated input parameters are related to each other so as to cooperate and realize each process in the business logic processing flow.
[0119] In this embodiment, the associated input parameters include URL path parameters, table parameters, and text description parameters.
[0120] The data calling module 40 is used to perform callback calling processing on all the associated input parameters obtained, obtain callback data of each associated input parameter, and use the trained LLM code generation chain to generate a first teleprompter template and a first user message template of the callback data respectively.
[0121] The code generation module 50 is used to process the callback data based on the first teleprompter template and the first user message template to generate first input data, and input the first input data into the large language model to generate code.
[0122] The code testing module 60 is used to use the test case generation chain of the trained large language model to generate a second teleprompter template and a second user message template for the code, process the code based on the second teleprompter template and the second user message template, generate second input data, and input the second input data into the large language model to generate a test case for the code. The test case is data obtained by combining context information, different teleprompter information and generated code similar to the code, and the purpose is to verify the correctness and coverage of the code. Different situations can be tested by simulating different inputs and expected outputs. The test cases generated in this way can be expanded and modified according to actual conditions.
[0123] LLM is a natural language processing model based on deep learning, which usually has billions or even hundreds of billions of parameters. These models are trained on large-scale text data and can understand and generate natural language texts and perform various language tasks such as text generation, translation, question answering, summarization, sentiment analysis, etc.
[0124] In LLM, a prompt is a piece of text or instruction that the user inputs to LLM to guide LLM to generate the corresponding output. Specifically, a prompt can be a question, an incomplete sentence, a task description, or any other form of text that is intended to clearly tell LLM what the user wants it to generate. The design and quality of the prompt have a great impact on the accuracy and relevance of the generated results. Prompts are usually clear, specific, and contextual, which can help LLM better understand the user's needs and generate more expected output.
[0125] In particular, Prompt consists of the following four elements: instruction, context, input data to be processed, and output data. Output data is also an output indicator, which is used to specify the type or format of the output.
[0126] In this embodiment, the context information of the first input data includes the above-mentioned associated input parameters and business logic processing flow.
[0127] In this embodiment, since LLM is the core of processing, it is necessary to select a suitable LLM for processing. If data security is involved, it is necessary to deploy a suitable large language model locally and provide a running environment and corresponding calling API: If data security is not involved in the use of the tool, you can use the API to call advanced large language models such as gpt-4o, qwen2-max, GPT3.5 for reasoning. Different LLMs will determine the specific content of the output data, i.e. code, and the test cases of the code.
[0128] In this embodiment, the corresponding teleprompter template, user message template, code generation chain and test case generation chain are generated by LangChain supporting LLM. The generated code generation chain and test case generation chain have high scalability.
[0129] LangChain is a framework for developing LLM-driven applications. LangChain allows LLM to connect to other data sources, interact with the environment, and achieve more powerful functions. Users can use LangChain's ability to form call links with multiple connection objects to help developers build end-to-end applications using language models. It provides a set of tools, components, and interfaces that simplify the process of creating applications powered by large language models. LangChain is an intermediate layer that links between user-oriented programs and LLMs, and customizes users' own LLMs by inputting relevant knowledge bases.
[0130] It should be noted that, of course, the LangChain tool can be omitted, and the user can encode the above teleprompter template, user message template, code generation chain, and test case generation chain by themselves.
[0131] The automatic code generation device method of the present invention understands the semantic intent of the user code development request to obtain the intent understanding result, and completes the entire logical processing flow to obtain the business logic processing flow, and then determines the associated input parameters required to complete the business logic processing flow based on the business logic processing flow, and then mines the associated objects that may be needed in the user's software development process by understanding the specific requirements of each user, so as to facilitate the subsequent display and implementation of richer and more complete processing flows and the processing return results corresponding to each processing flow. In this way, logical reasoning can be performed like software developers, and according to the structure of input, output, and data sources, combined with the text description of the requirements, greater flexibility and scalability are provided, and then the logical reasoning ability of the large language model is utilized to complete the generation of flexible code containing logical processing by constructing an appropriate call chain, and at the same time, the test cases corresponding to the code can be generated, thereby improving software development efficiency. Compared with the existing code generation solutions that only support the input of one structural object, it can dynamically generate diversified, more complex and more professional codes based on multiple associated input parameters and business logic processing flows. Through this automated code generation process, development efficiency can be greatly improved. At the same time, test cases for the code are generated, thereby improving testing efficiency. Testers no longer need to write test cases manually, but can directly use the generated test cases.
[0132] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4 As shown, the electronic device may include: a processor 410 (processor), a communication interface 420 (Communications Interface), a memory 430 (memory) and a communication bus 440, wherein the processor 410, the communication interface 420, and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic command in the memory 430 to execute the automatic code generation method, which includes:
[0133] Obtaining a code development request from a user; the code development request includes an application scenario specified by the user and a full logical processing flow of the application scenario;
[0134] Perform semantic intent understanding on the code development request to obtain an intent understanding result, and use the full logic processing flow and the intent understanding result to match with a preset knowledge base, complete the full logic processing flow, and generate a business logic processing flow; the intent understanding result represents the personal intent understanding of the user's code development request;
[0135] Based on the business logic processing flow, determining the associated input parameters required to complete the business logic processing flow; the associated input parameters include uniform resource locator path parameters, table parameters and text description parameters;
[0136] Performing callback call processing on all the associated input parameters to obtain callback data for each associated input parameter, and using the code generation chain of the trained large language model to generate a first teleprompter template and a first user message template for the callback data respectively;
[0137] Processing the callback data based on the first teleprompter template and the first user message template to generate first input data, and inputting the first input data into the large language model to obtain a code output by the large language model;
[0138] Utilize the test case generation chain of the large language model to generate a second teleprompter template and a second user message template for the code, respectively; process the code based on the second teleprompter template and the second user message template to generate the second input data; and input the second input data into the large language model to obtain a test case for the code output by the large language model.
[0139] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present invention is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memor), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.
[0140] On the other hand, the present invention further provides a computer program product, the computer program product comprising a computer program stored on a non-transitory computer-readable storage medium, the computer program comprising program instructions, and when the program instructions are executed by a computer, the computer can execute the automatic code generation method provided by the above methods, the method comprising:
[0141] Obtaining a code development request from a user; the code development request includes an application scenario specified by the user and a full logical processing flow of the application scenario;
[0142] Perform semantic intent understanding on the code development request to obtain an intent understanding result, and use the full logic processing flow and the intent understanding result to match with a preset knowledge base, complete the full logic processing flow, and generate a business logic processing flow; the intent understanding result represents the personal intent understanding of the user's code development request;
[0143] Based on the business logic processing flow, determining the associated input parameters required to complete the business logic processing flow; the associated input parameters include uniform resource locator path parameters, table parameters and text description parameters;
[0144] Performing callback call processing on all the associated input parameters to obtain callback data for each associated input parameter, and using the code generation chain of the trained large language model to generate a first teleprompter template and a first user message template for the callback data respectively;
[0145] Processing the callback data based on the first teleprompter template and the first user message template to generate first input data, and inputting the first input data into the large language model to obtain a code output by the large language model;
[0146] Utilize the test case generation chain of the large language model to generate a second teleprompter template and a second user message template for the code, respectively; process the code based on the second teleprompter template and the second user message template to generate the second input data; and input the second input data into the large language model to obtain a test case for the code output by the large language model.
[0147] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, which is implemented when the computer program is executed by a processor to perform the above-mentioned method for automatically generating an executable code, the method comprising:
[0148] Obtaining a code development request from a user; the code development request includes an application scenario specified by the user and a full logical processing flow of the application scenario;
[0149] Perform semantic intent understanding on the code development request to obtain an intent understanding result, and use the full logic processing flow and the intent understanding result to match with a preset knowledge base, complete the full logic processing flow, and generate a business logic processing flow; the intent understanding result represents the personal intent understanding of the user's code development request;
[0150] Based on the business logic processing flow, determining the associated input parameters required to complete the business logic processing flow; the associated input parameters include uniform resource locator path parameters, table parameters and text description parameters;
[0151] Performing callback call processing on all the associated input parameters to obtain callback data for each associated input parameter, and using the code generation chain of the trained large language model to generate a first teleprompter template and a first user message template for the callback data respectively;
[0152] Processing the callback data based on the first teleprompter template and the first user message template to generate first input data, and inputting the first input data into the large language model to obtain a code output by the large language model;
[0153] Utilize the test case generation chain of the large language model to generate a second teleprompter template and a second user message template for the code, respectively; process the code based on the second teleprompter template and the second user message template to generate the second input data; and input the second input data into the large language model to obtain a test case for the code output by the large language model.
[0154] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.
[0155] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.
[0156] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for automatically generating a code, characterized in that: The method comprises: Obtaining a code development request from a user; the code development request includes an application scenario specified by the user and a full logical processing flow of the application scenario; Perform semantic intent understanding on the code development request to obtain an intent understanding result, and use the full logic processing flow and the intent understanding result to match with a preset knowledge base, complete the full logic processing flow, and generate a business logic processing flow; the intent understanding result represents the personal intent understanding of the user's code development request; Based on the business logic processing flow, determining the associated input parameters required to complete the business logic processing flow; the associated input parameters include uniform resource locator path parameters, table parameters and text description parameters; Performing callback call processing on all the associated input parameters to obtain callback data for each associated input parameter, and using the code generation chain of the trained large language model to generate a first teleprompter template and a first user message template for the callback data respectively; Processing the callback data based on the first teleprompter template and the first user message template to generate first input data, and inputting the first input data into the large language model to obtain a code output by the large language model; the context information of the first input data includes the associated input parameters and the business logic processing flow; A test case generation chain of the large language model is used to generate a second teleprompter template and a second user message template for the code, respectively. The code is processed based on the second teleprompter template and the second user message template to generate second input data. The second input data is input into the large language model to obtain a test case for the code output by the large language model.
2. The automatic code generation method according to claim 1, characterized in that: The intention understanding result is obtained through a trained semantic understanding model, and the semantic understanding model is trained based on sample text, sample intention understanding results corresponding to the sample text, and knowledge background description text. The knowledge background description text represents the feature information of the natural language description of the knowledge background field corresponding to the request text.
3. The automatic code generation method according to claim 2, characterized in that: The intention understanding result is obtained by the following steps: Converting the code development request into a request text; The request text is input into the trained semantic understanding model to obtain the intent understanding result output by the semantic understanding model.
4. The automatic code generation method according to claim 3, characterized in that: The semantic understanding model is trained by the following steps: Acquire the sample text, and determine the sample intention understanding result of the sample text; Matching the sample text with a preset knowledge base to obtain the knowledge background description text corresponding to the sample text; The sample text and the knowledge background description text are used as input data for training, and the sample intent understanding results are used as label data. A supervised training method is adopted to train a semantic understanding model for outputting intent understanding results of code development requests.
5. The method for automatically generating a code according to claim 1, characterized in that: The determining of the associated input parameters required to complete the business logic processing flow based on the business logic processing flow specifically includes: Based on the business logic process flow, determine the process characteristics required to complete the business logic process flow; Based on the process characteristics, a first sub-parameter and a second sub-parameter of each associated input parameter are obtained; the first sub-parameter is a necessary input parameter, and the second sub-parameter is a non-necessary input parameter.
6. The method for automatically generating a code according to claim 1, characterized in that: In this method, a first teleprompter template, a second teleprompter template, a first user message template, a second user message template, a code generation chain, and a test case generation chain are generated through the LangChain corresponding to the large language model.
7. The method for automatically generating a code according to claim 1, characterized in that: The method further comprises the following steps: Deploy the code generation environment, determine the service type of the code generation system, and set the address of the system's standard resources when the service type meets the preset type, and determine the database table structure and the list of at least one database table that the address request of the standard resource needs to operate.
8. A device for automatically generating a code, characterized in that: The device comprises: A request acquisition module is used to acquire a user's code development request; the code development request includes an application scenario specified by the user and the entire logical processing flow of the application scenario; A process determination module is used to understand the semantic intent of the code development request, obtain an intent understanding result, and use the full logic processing flow and the intent understanding result to match with a preset knowledge base, complete the full logic processing flow, and generate a business logic processing flow; the intent understanding result represents the personal intent understanding of the user's code development request; A parameter determination module, used to determine the associated input parameters required to complete the business logic processing flow based on the business logic processing flow; the associated input parameters include uniform resource locator path parameters, table parameters and text description parameters; The data calling module is used to perform callback calling processing on all the associated input parameters, obtain callback data of each associated input parameter, and use the code generation chain of the trained large language model to generate a first teleprompter template and a first user message template of the callback data respectively; a code generation module, configured to process the callback data based on the first teleprompter template and the first user message template to generate first input data, and input the first input data into the large language model to obtain a code output by the large language model; the context information of the first input data includes the associated input parameters and the business logic processing flow; A code testing module is used to use the test case generation chain of the large language model to generate a second teleprompter template and a second user message template for the code, process the code based on the second teleprompter template and the second user message template to generate second input data, and input the second input data into the large language model to obtain a test case for the code output by the large language model.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the method for automatically generating codes according to any one of claims 1 to 7 are implemented.
10. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method for automatically generating codes according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Business logic code-free development method based on natural language understanding and conversion
CN113791791A
Intelligent software agile development method and system based on large language model
CN118276829A