Interface code rapid generation method and device based on large language model
Generate SQL statements and code template technology in MyBatis syntax format through a large language model, which solves the problem that existing NL2CODE cannot generate complete code, realizes the generation of runnable source code directly from the table structure, and improves the development efficiency and code matching. Actual code style.
Patent Information
- Application Number
- CN202411064093.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-05
- Publication Date
- 2024-12-27
AI Technical Summary
The existing NL2CODE technology can only generate code snippets, cannot be directly applied to projects, and cannot generate code based on the table structure of existing development projects.
By entering the table structure into a large language model, SQL statements in MyBatis syntax format are generated, SQL statements are parsed to generate API interface message definitions, and Java code including controllers, service and mapper is automatically generated using code template technology.
It directly generates runnable and readable source code based on the table structure of the development project, improves development efficiency, and the generated code conforms to the code style of most J2EE projects.
Smart Images

Figure CN119201073A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of big data technology, and in particular to a method and device for quickly generating interface codes based on a large language model. Background Art
[0002] In recent years, with the development of large-scale pre-trained language models, the automatic generation method of executable code has attracted widespread attention. The existing technology is mainly NL2CODE (Natural Language to Code), which is a technology that directly converts tasks described in natural language into executable code.
[0003] Its basic goal is to automatically generate corresponding program codes by understanding human natural language instructions, so as to improve the efficiency of software development and reduce coding errors. It may also lower the threshold of programming, so that non-professional programmers can also realize their ideas through simple descriptions. This technology combines the research results of multiple fields such as natural language processing (Neuro-Linguistic Programming, NLP), machine learning, and deep learning, especially relying on advanced neural network models such as sequence-to-sequence (Seq2Seq) models and Transformer architectures. These models usually contain an "encoder" to understand the input natural language description and a "decoder" to generate the corresponding programming code.
[0004] In recent years, the NL2Code task has made significant progress with the development of large-scale pre-trained language models (such as the GPT series, BERT, etc.). These models are trained on a large number of code datasets and learn a rich set of programming patterns, grammatical structures, and logical reasoning capabilities, enabling them to generate more complex and accurate code snippets.
[0005] However, the current NL2CODE has the following problems:
[0006] 1. Only code snippets can be generated and cannot be directly applied to projects;
[0007] 2. It is impossible to generate code based on the table structure of an existing development project. Summary of the invention
[0008] In order to solve the above problems in the prior art, the present invention provides a method and device for quickly generating interface codes based on a large language model, which can directly obtain executable and highly readable source code according to the table structure of the development project.
[0009] In order to achieve the above object, the technical solution adopted by the present invention is:
[0010] In a first aspect, the present invention provides a method for quickly generating interface code based on a large language model, comprising the steps of:
[0011] S1. Input the table structure into the large language model, and use prompt words to make the large language model generate an SQL statement corresponding to the table structure, wherein the SQL statement is in the MyBatis syntax format;
[0012] S2. parse the SQL statement and generate an API interface message definition;
[0013] S3. Using code template technology, automatically generate Java code including controller+service+mapper according to the API interface message definition.
[0014] The beneficial effect of the present invention is that: SQL statements are generated through a large language model, and backend interface definitions are obtained by parsing SQL statements. In combination with code template technology, code is generated for a specified backend interface, and a purely automated code generation function has been realized. The generated code is more in line with the actual code and conforms to the code style of most existing J2EE projects. Therefore, the present invention can directly obtain executable and highly readable source code according to the table structure of the development project.
[0015] Optionally, the step S2 is specifically:
[0016] Variables of the SQL statement are parsed, the variables are designated as API interface parameters, and an API interface message definition is generated. The API interface message definition includes an HTTP request message and an HTTP response message.
[0017] Optionally, step S2 includes the following steps:
[0018] Parse the part of the SQL statement that conforms to the mybatis syntax format into request parameters to generate an HTTP request message;
[0019] Determine whether the SQL statement is a SELECT statement. If so, specify the interface return value of the identification field of the select clause in the SQL statement to generate an HTTP response message. Otherwise, return whether it is successful as the interface return value to generate an HTTP response message.
[0020] Optionally, the part conforming to the mybatis syntax format includes #{} and ${}.
[0021] Optionally, the prompt words include business requirements and function descriptions.
[0022] In a second aspect, the present invention provides a device for quickly generating interface codes based on a large language model, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the following steps when executing the computer program:
[0023] S1. Input the table structure into the large language model, and use prompt words to make the large language model generate an SQL statement corresponding to the table structure, wherein the SQL statement is in the MyBatis syntax format;
[0024] S2. parse the SQL statement and generate an API interface message definition;
[0025] S3. Using code template technology, automatically generate Java code including controller+service+mapper according to the API interface message definition.
[0026] Optionally, the processor executes step S2 in the computer program specifically as follows:
[0027] Variables of the SQL statement are parsed, the variables are designated as API interface parameters, and an API interface message definition is generated. The API interface message definition includes an HTTP request message and an HTTP response message.
[0028] Optionally, the processor executes the step S2 in the computer program including the following steps:
[0029] Parse the part of the SQL statement that conforms to the mybatis syntax format into request parameters to generate an HTTP request message;
[0030] Determine whether the SQL statement is a SELECT statement. If so, specify the interface return value of the identification field of the select clause in the SQL statement to generate an HTTP response message. Otherwise, return whether it is successful as the interface return value to generate an HTTP response message.
[0031] Optionally, the part conforming to the mybatis syntax format includes #{} and ${}.
[0032] Optionally, the prompt words include business requirements and function descriptions.
[0033] Among them, the technical effect corresponding to the device for quickly generating interface code based on a large language model provided in the second aspect refers to the relevant description of the method for quickly generating interface code based on a large language model provided in the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0034] Figure 1A schematic diagram of the main flow of a method for quickly generating interface code based on a large language model according to an embodiment of the present invention;
[0035] Figure 2 It is a schematic diagram of the overall implementation of a method for quickly generating interface codes based on a large language model according to an embodiment of the present invention;
[0036] Figure 3 A schematic diagram of an interface for generating SQL statements from a large language model involved in an embodiment of the present invention;
[0037] Figure 4 A schematic diagram of an interface for automatically generating Java code according to an API interface message definition according to an embodiment of the present invention;
[0038] Figure 5 The present invention is a schematic diagram of a framework of a device for quickly generating interface codes based on a large language model according to an embodiment of the present invention.
[0039] Description of reference numerals:
[0040] 1. A device for quickly generating interface codes based on a large language model;
[0041] 2. Processor;
[0042] 3. Memory. DETAILED DESCRIPTION
[0043] In order to better understand the above technical solution, exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be limited by the embodiments described herein. On the contrary, these embodiments are provided to enable a clearer and more thorough understanding of the present invention and to fully convey the scope of the present invention to those skilled in the art.
[0044] Embodiment 1
[0045] This embodiment is applicable to application scenarios that require automatic generation of interface codes. The NL2CODE in the prior art has the problem that it cannot generate code that can be directly applied to the project based on the table structure of the existing development project. The traditional large language model can only generate code snippets, such as asking it to help write a simple sorting algorithm, or asking the large language model to make one or two lines of code association guesses, but it cannot generate the code required by the actual business. This embodiment smoothly converts the SQL statements generated by the large language model into HTTP, and automatically generates Java code for HTTP through code templates, so that it can directly obtain executable and highly readable source code based on the table structure of the development project. See the following description for details.
[0046] Please refer to Figures 1 to 4 , a method for quickly generating interface code based on a large language model, comprising the steps of:
[0047] S1. Input the table structure into the large language model, and use the prompt words to make the large language model generate SQL statements corresponding to the table structure. The SQL statements are in the MyBatis syntax format.
[0048] Among them, the prompt words include business requirements and functional descriptions.
[0049] Reference Figure 2 It can be seen that before step S1, the interface developer needs to enter the information required for code generation into the database, and then the data table can be read from the database.
[0050] Reference Figure 3 It can be seen that this embodiment takes Tongyi Qianwen as an example, and the actual chat dialog box is provided by the actual system, and the interface connected to the large language model can be used. Among them, the interface developer inputs these tables and the business requirement "Please generate an SQL statement for me to query sales records based on employee names, and the variable expression method of mybatis is required", and the corresponding SQL statement is obtained.
[0051] S2. Parse SQL statements and generate API interface message definitions;
[0052] Wherein, step S2 is specifically as follows:
[0053] Parse the variables of the SQL statement, specify the variables as API interface parameters, and generate API interface message definitions. The API interface message definitions include HTTP request messages and HTTP response messages.
[0054] Wherein, step S2 comprises the following steps:
[0055] S21, parse the part of the SQL statement that conforms to the mybatis syntax format into request parameters to generate an HTTP request message;
[0056] Among them, the parts that conform to the mybatis syntax format include #{} and ${}.
[0057] That is, this step is to parse the part of the SQL statement that conforms to the mybatis syntax into request parameters, and then generate an HTTP request message.
[0058] S22. Determine whether the SQL statement is a SELECT statement. If so, specify the interface return value of the identification field of the select clause in the SQL statement to generate an HTTP response message. Otherwise, a fixed return value of success is used as the interface return value to generate an HTTP response message.
[0059] In addition to the SELECT statement, SQL statements can also include INSERT statements, UPDATE statements, and DELETE statements.
[0060] In combination with step S21 and step S22, in this embodiment, examples of SELECT statements, INSERT statements, UPDATE statements, and DELETE statements are as follows:
[0061]
[0062] The part of the SQL statement that complies with the mybatis syntax is "employeeName", so it becomes the request parameter in the HTTP request message. The identification field of the select clause in the SQL statement is "s.*". By parsing the SQL statement, we know that s.* refers to all fields of the s table, and s is an alias for the sales table. Therefore, s.* can be parsed into the arrangement of all fields of the sales table, and thus becomes the interface return value in the HTTP response message.
[0063]
[0064]
[0065]
[0066] If it is an INSERT statement, UPDATE statement, or DELETE statement, the part of the SQL statement that conforms to the mybatis syntax is parsed into request parameters. For example, #{employeeld}, #{productld}, and #{state} in the INSERT statement correspond to employee_id, product_id, and state in the HTTP request message. The return value of the three interfaces is fixed to return result, indicating whether the SQL statement is executed successfully.
[0067] S3. Through code template technology, Java code including controller+service+mapper is automatically generated according to the API interface message definition.
[0068] Reference Figure 4 It can be seen that the code template technology in step S3 can automatically generate Java code including controller+service+mapper from the API interface message definition using existing software.
[0069] Among them, controller is the controller, service is the database service, and mapper is data persistence. In the data processing system, the three correspond to the controller layer, service layer, and mapper layer. Specifically, the controller layer is used to receive foreground data and return page request information. The service layer receives information from the controller layer for business processing and logical judgment. At the same time, it calls the API of the mapper layer. The mapper layer is used to interact with the database. If you want to access the database and operate it, you can only send SQL statements to the database through the mapper layer, pass these results to the service layer through the interface, and perform data persistence operations on the database.
[0070] In summary, this embodiment can well realize the conversion of business requirements and functional descriptions into SQL statements by means of the ability of the large language model to generate SQL; by parsing SQL statements and mapping them into HTTP interfaces, business requirements and functional descriptions can be further converted into backend interface definitions. In combination with code template technology, code is generated for the specified backend interface, and a purely automated code generation function has been realized. The generated code is more in line with the actual code and conforms to the code style of most existing J2EE projects. Therefore, the present invention can directly obtain executable and highly readable source code based on the table structure of the development project, which will greatly speed up the coding development efficiency of developers.
[0071] Embodiment 2
[0072] Please refer to Figure 5 A device 1 for quickly generating interface codes based on a large language model comprises a memory 3, a processor 2 and a computer program stored in the memory 3 and executable on the processor 2. When the processor 2 executes the computer program, the steps in the above-mentioned embodiment 1 are implemented.
[0073] Since the system / device described in the above embodiments of the present invention is a system / device used to implement the method of the above embodiments of the present invention, a person skilled in the art can understand the specific structure and deformation of the system / device based on the method described in the above embodiments of the present invention, and thus will not be described in detail here. All systems / devices used in the method of the above embodiments of the present invention belong to the scope of protection of the present invention.
[0074] It will be appreciated by those skilled in the art that embodiments of the present invention may be provided as methods, devices or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0075] The present invention is described with reference to the flowcharts and / or block diagrams of the methods, devices (apparatus) and computer program products according to the embodiments of the present invention. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions.
[0076] It should be noted that in the claims, any reference numerals placed between brackets shall not be construed as limiting the claims. The word "comprising" does not exclude the presence of components or steps not listed in the claims. The word "a" or "an" preceding a component does not exclude the presence of a plurality of such components. The invention may be implemented by means of hardware comprising several different components and by means of a suitably programmed computer. In the claims enumerating several means, several of these means may be embodied by the same hardware. The use of the words first, second, third, etc., is for convenience of expression only and does not indicate any order. These words may be understood as part of the component name.
[0077] In addition, it should be noted that, in the description of this specification, the description of the terms "one embodiment", "some embodiments", "embodiment", "example", "specific example" or "some examples" etc. means that the specific features, structures, materials or characteristics described in conjunction with the embodiment or example are included in at least one embodiment or example of the present invention. In this specification, the schematic representations of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described may be combined in any one or more embodiments or examples in a suitable manner. In addition, those skilled in the art may combine and combine the different embodiments or examples described in this specification and the features of the different embodiments or examples, unless they are contradictory.
[0078] Although the preferred embodiments of the present invention have been described, those skilled in the art may make other changes and modifications to these embodiments after knowing the basic creative concept. Therefore, the claims should be interpreted as including the preferred embodiments and all changes and modifications falling within the scope of the present invention.
[0079] Obviously, those skilled in the art can make various modifications and variations to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention should also include these modifications and variations.
Claims
1. A method for quickly generating interface code based on a large language model, characterized in that: Includes steps: S1. Input the table structure into the large language model, and use prompt words to make the large language model generate an SQL statement corresponding to the table structure, wherein the SQL statement is in the MyBatis syntax format; S2. parse the SQL statement and generate an API interface message definition; S3. Using code template technology, automatically generate Java code including controller+service+mapper according to the API interface message definition.
2. The method for quickly generating interface code based on a large language model according to claim 1, characterized in that: The step S2 is specifically as follows: Variables of the SQL statement are parsed, the variables are designated as API interface parameters, and an API interface message definition is generated. The API interface message definition includes an HTTP request message and an HTTP response message.
3. The method for quickly generating interface code based on a large language model according to claim 2, characterized in that: The step S2 comprises the following steps: Parse the part of the SQL statement that conforms to the mybatis syntax format into request parameters to generate an HTTP request message; Determine whether the SQL statement is a SELECT statement. If so, specify the interface return value of the identification field of the select clause in the SQL statement to generate an HTTP response message. Otherwise, return whether it is successful as the interface return value to generate an HTTP response message.
4. The method for quickly generating interface code based on a large language model according to claim 3, characterized in that: The parts that conform to the mybatis syntax format include #{} and ${}.
5. A method for quickly generating interface code based on a large language model according to any one of claims 1 to 4, characterized in that: The prompt words include business requirements and function descriptions.
6. A device for quickly generating interface codes based on a large language model, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the following steps are implemented: S1. Input the table structure into the large language model, and use prompt words to make the large language model generate an SQL statement corresponding to the table structure, wherein the SQL statement is in the MyBatis syntax format; S2. parse the SQL statement and generate an API interface message definition; S3. Using code template technology, automatically generate Java code including controller+service+mapper according to the API interface message definition.
7. The device for quickly generating interface code based on a large language model according to claim 6, characterized in that: The processor executes step S2 in the computer program specifically as follows: Variables of the SQL statement are parsed, the variables are designated as API interface parameters, and an API interface message definition is generated. The API interface message definition includes an HTTP request message and an HTTP response message.
8. The device for quickly generating interface code based on a large language model according to claim 7, characterized in that: The processor executes the step S2 in the computer program including the following steps: Parse the part of the SQL statement that conforms to the mybatis syntax format into request parameters to generate an HTTP request message; Determine whether the SQL statement is a SELECT statement. If so, specify the interface return value of the identification field of the select clause in the SQL statement to generate an HTTP response message. Otherwise, return whether it is successful as the interface return value to generate an HTTP response message.
9. The device for quickly generating interface code based on a large language model according to claim 8, characterized in that: The parts that conform to the mybatis syntax format include #{} and ${}.
10. A device for quickly generating interface code based on a large language model according to any one of claims 6 to 9, characterized in that: The prompt words include business requirements and function descriptions.
Citation Information
Patent Citations
Data processing method and device and storage medium
CN115202663A
Method and system for converting SQL (Structured Query Language) statement into HTTP (Hyper Text Transport Protocol) interface and readable medium
CN117055895A
Data query method for realizing conversion from natural language to SQL (Structured Query Language) based on large language model
CN117708161A
Cited By
Method and apparatus for managing application programming interface, and device and medium
WO2026178711A1