A code generation method, apparatus, electronic device, and readable storage medium

By obtaining the target component pattern entries from the component pattern information library and using the large language model to generate program pattern definitions, converting them into an abstract syntax tree, and finally generating accurate program code, the accuracy problem of large language model when generating long text code is solved, and the high accuracy and practicality of code generation is achieved.

CN119536695BActive Publication Date: 2025-08-01CHINA TELECOM ARTIFICIAL INTELLIGENCE TECHNOLOGY (BEIJING) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411448391.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-10-16
Publication Date
2025-08-01
Estimated Expiration
2044-10-16

AI Technical Summary

Technical Problem

In the prior art, the accuracy of large language models decreases as the text length increases when generating code, resulting in lower accuracy of generating code.

Method used

By obtaining target component pattern entries from the component pattern information base based on program description information, using the target large language model to generate program pattern definitions, and converting them into abstract syntax tree, and finally generating program code, combining the correction process of abstract syntax tree and large language model, improving the accuracy of code generation.

Benefits of technology

When facing the needs of complete program code generation, the accuracy and practicality of code generation are improved, the randomness and uncertainty of large language models are suppressed, and the generated code is more accurate and practical.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119536695B_ABST
    Figure CN119536695B_ABST
Patent Text Reader

Abstract

An embodiment of the present invention provides a code generation method, apparatus, electronic device, and readable storage medium, relating to the technical field of data processing, including: obtaining a target component pattern entry from a component pattern information library based on program description information; inputting the program description information and the target component pattern entry into a target large language model to obtain a program pattern definition output by the target large language model; generating an abstract syntax tree corresponding to the program description information based on the program pattern definition; and generating program code corresponding to the program description information according to the abstract syntax tree. It can make full use of the semantic understanding ability of the large language model during the automatic code generation process. When the large language model can fully understand the program development requirements, the randomness and uncertainty of the large language model are suppressed through the defined regularized component patterns, improving the accuracy and practicability of the automatically generated program code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of data processing technology, and in particular relates to a code generation method, device, electronic device and readable storage medium. Background Art

[0002] Automatic code generation technology is gaining increasing attention. This technology aims to reduce developer workload and improve development efficiency by automatically generating code. In doing so, automatic code generation simplifies the software development process and reduces the technical expertise required of developers. Consequently, it is gaining widespread application and adoption across a wide range of software projects.

[0003] In related technologies, a pre-trained large language model is typically used to generate code based on user-provided functional requirements. Users input natural language descriptions into the large language model, which automatically parses the requirements and generates corresponding code snippets.

[0004] However, the processing accuracy of large language models decreases as the length of the text increases, resulting in the above method being only applicable to the generation of shorter code snippets, and there is a problem of low accuracy in the generated code. Summary of the Invention

[0005] The present invention provides a code generation method, device, electronic device and readable storage medium to solve the technical problem of low accuracy of automatic code generation in the prior art.

[0006] In a first aspect, the present invention provides a code generation method, the method comprising:

[0007] Obtaining a target component pattern entry from a component pattern information library based on the program description information;

[0008] Inputting the program description information and the target component pattern entry into a target large language model to obtain a program pattern definition output by the target large language model;

[0009] Generate an abstract syntax tree corresponding to the program description information based on the program pattern definition;

[0010] Generate program code corresponding to the program description information according to the abstract syntax tree.

[0011] Optionally, generating program code corresponding to the program description information according to the abstract syntax tree includes:

[0012] Parsing the abstract syntax tree to obtain a first code;

[0013] Input the first code and the program description information into the target large language model, and correct the first code through the target large language model to obtain the program code output by the target large language model.

[0014] Optionally, the step of inputting the first code and the program description information into the target large language model, correcting the first code through the target large language model, and obtaining the program code output by the target large language model includes:

[0015] Input the first code into the target large language model to obtain the code description information output by the target large language model;

[0016] Generate a code correction instruction according to the functional difference between the code description information and the program description information;

[0017] Input the first code, the program description information, and the code correction instruction into the target large language model to obtain the program code output by the target large language model.

[0018] Optionally, the method further includes:

[0019] Obtain the program description media information and the entry function description information corresponding to the component pattern information library;

[0020] Input the program description media information and the entry function description information into the target large language model to obtain the program description information output by the target large language model; wherein, the keywords included in the program description information match the keywords included in the entry function description information.

[0021] Optionally, the step of obtaining the target component pattern entry from the component pattern information library based on the program description information includes:

[0022] Perform semantic segmentation processing on the program description information and extract the segmentation semantic features corresponding to the segmentation result;

[0023] Match the segmentation semantic features with the entry semantic features corresponding to each component pattern entry in the component pattern information library to obtain the target component pattern entry.

[0024] Optionally, the step of obtaining the target component pattern entry from the component pattern information library based on the program description information includes:

[0025] Obtain the entry name information corresponding to each component pattern entry in the component pattern information library;

[0026] Input the entry name information and the program description information into the target large language model to obtain the target entry name corresponding to the component pattern entry required for implementing the target program corresponding to the program description information output by the target large language model;

[0027] Obtain the target component pattern entry from the component pattern information library based on the target entry name.

[0028] Optionally, inputting the program description information and the target component pattern entry into the target large language model to obtain the program pattern definition output by the target large language model includes:

[0029] Determine the target program type corresponding to the target program described by the program description information;

[0030] Match the target prompt text from the prompt text database based on the target program type;

[0031] Input the program description information, the target component pattern entry, and the target prompt text into the target large language model to obtain the program pattern definition output by the target large language model.

[0032] In a second aspect, the present invention provides a code generation device, and the device includes:

[0033] An entry module for obtaining a target component pattern entry from a component pattern information library based on program description information;

[0034] A pattern definition module for inputting the program description information and the target component pattern entry into a target large language model to obtain a program pattern definition output by the target large language model;

[0035] An abstract syntax tree module for generating an abstract syntax tree corresponding to the program description information based on the program pattern definition;

[0036] A code module for generating program code corresponding to the program description information according to the abstract syntax tree.

[0037] Optionally, the code module includes:

[0038] A parsing sub-module for parsing the abstract syntax tree to obtain the first code;

[0039] A first input sub-module for inputting the first code and the program description information into the target large language model, and correcting the first code through the target large language model to obtain the program code output by the target large language model.

[0040] Optionally, the first input sub-module includes:

[0041] A code description information unit for inputting the first code into a target large language model to obtain code description information output by the target large language model;

[0042] A code correction instruction unit for generating a code correction instruction according to the functional difference between the code description information and the program description information;

[0043] A first input unit for inputting the first code, the program description information, and the code correction instruction into a target large language model to obtain the program code output by the target large language model.

[0044] Optionally, the device further includes:

[0045] A first acquisition module for acquiring program description media information and entry function description information corresponding to the component mode information library;

[0046] A program description information module for inputting the program description media information and the entry function description information into a target large language model to obtain program description information output by the target large language model; wherein, keywords included in the program description information match keywords included in the entry function description information.

[0047] Optionally, the entry module includes:

[0048] A semantic feature sub-module for performing semantic segmentation processing on the program description information and extracting segmented semantic features corresponding to the segmentation result;

[0049] A first entry acquisition sub-module for matching the segmented semantic features with entry semantic features corresponding to each component mode entry in the component mode information library to obtain the target component mode entry.

[0050] Optionally, the entry module includes:

[0051] A second acquisition sub-module for acquiring entry name information corresponding to each component mode entry in the component mode information library;

[0052] A second input sub-module for inputting the entry name information and the program description information into a target large language model to obtain a target entry name corresponding to a component mode entry required for implementing the target program corresponding to the program description information output by the target large language model;

[0053] A second entry acquisition sub-module for acquiring a target component mode entry from the component mode information library based on the target entry name.

[0054] Optionally, the mode definition module includes:

[0055] A program type sub-module for determining a target program type corresponding to a target program described by the program description information;

[0056] A prompt text sub-module for matching target prompt text from a prompt text database based on the target program type;

[0057] A mode definition determination sub-module for inputting the program description information, the target component mode entry, and the target prompt text into a target large language model to obtain a program mode definition output by the target large language model.

[0058] In a third aspect, the present invention provides an electronic device, including: a processor, a memory, and a computer program stored on the memory and executable on the processor, characterized in that when the processor executes the program, the above-mentioned code generation method is implemented.

[0059] In a fourth aspect, the present invention provides a readable storage medium, when instructions in the storage medium are executed by a processor of an electronic device, enabling the electronic device to execute the above-mentioned code generation method.

[0060] In an embodiment of the present invention, it includes: obtaining a target component mode entry from a component mode information library based on program description information; inputting the program description information and the target component mode entry into a target large language model to obtain a program mode definition output by the target large language model; generating an abstract syntax tree corresponding to the program description information based on the program mode definition; generating program code corresponding to the program description information according to the abstract syntax tree. It is possible to screen the defined component models to obtain a small number of target component mode entries that meet the program description information, and then use the large language model to generate a program mode definition with higher accuracy according to the target component mode entries, and then convert the program mode definition into an abstract syntax tree, and directly generate accurate program code through the abstract syntax tree. The semantic understanding ability of the large language model is fully utilized in the process of automatic code generation. When the large language model can fully understand the program development requirements, the randomness and uncertainty of the large language model are suppressed through the defined regularized component modes, improving the accuracy and practicality of the automatically generated program code, and still having high accuracy when facing the requirement of generating complete program code. BRIEF DESCRIPTION OF THE DRAWINGS

[0061] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.

[0062] Figure 1 It is a flowchart of steps of a code generation method provided by an embodiment of the present invention;

[0063] Figure 2 It is a flowchart of steps of another code generation method provided by an embodiment of the present invention;

[0064] Figure 3 It is a flowchart of an example of code generation provided by an embodiment of the present invention;

[0065] Figure 4 It is a structural diagram of a code generation device provided by an embodiment of the present invention;

[0066] Figure 5 It is a structural diagram of an electronic device provided by an embodiment of the present invention. Detailed implementation manners

[0067] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present invention.

[0068] Refer to Figure 1 , Figure 1 It is a flowchart of steps of a code generation method provided by an embodiment of the present invention. As Figure 1 shown, the method may include:

[0069] Step 101, obtain a target component mode entry from a component mode information library based on program description information.

[0070] In the embodiments of the present application, the program description information may represent a document for describing the program form and development requirements. The above program may include a front-end program, such as a client, a web page, a UI interface, etc., or may include a back-end program, such as a database, a server, etc., or may be other programs, which are not specifically limited in the embodiments of the present application. The program description information may be written by a developer or provided by a requester. Exemplarily, the program description information of a client may include contents such as the positions, forms, functions, and jump relationships of each UI control.

[0071] The component pattern information library represents a database for storing component pattern (Schema) information, where Schema is a specification for defining and describing data structures or models, which can specify the organization method, data type, relationship, etc. of program component data through hierarchies and rules. Schema can be used to define program components, such as UI definition, API definition, data exchange definition, functional module definition, service definition, etc. The component pattern information library can store component pattern entries corresponding to each program component. The component pattern entries can describe the program components through specific information (such as text, code, low code). For example, when the component pattern is a user interface pattern (UI Schema), the above-mentioned program components may include but are not limited to UI composition, layout, interaction, appearance, etc.

[0072] In an embodiment of the present application, the program description information can be analyzed, and the target component pattern entries required for the corresponding program can be recalled (retrieval) from the component pattern information library. Specifically, the index information corresponding to each component pattern entry in the component pattern information library can be subjected to text retrieval matching based on the program description information to determine the target component pattern entry. The above-mentioned text retrieval method may include but is not limited to term frequency-inverse document frequency (Term Frequency-Inverse Document Frequency, TF-IDF), best matching 25 (Best Matching 25, BM25), dense passage retrieval (DPR), etc., which are not specifically limited in the embodiment of the present application. Among them, the index information corresponding to the component pattern entry may include but is not limited to name information, description information, feature information, instruction information, etc.

[0073] It should be noted that the Schema definition in the component schema information library can use a ready-made template or be written according to actual business needs. For example, a component schema entry about the appearance of a QR code in the component schema information library can be in the following form:

[0074] {"component":"page","body":[{"component":"barcode","backgroundColor":"#eee","foregroundColor":"#fff","value":"https: / / www.xxx.com","style":{"width":100,"height":50,"margin":5}}],"title":"Page containing QR code"}

[0075] The above component mode entry describes a page containing a QR code component and related layout, style, and other configurations. Among them, component: "page" indicates that the root component of the entire JSON structure is a page. The layout, content, and components of the entire page will be included within this page; body: [{...}] defines the main content of the page. In this example, the page content is a QR code (barcode) component; component: "barcode" defines a display area for a "barcode" or "QR code"; backgroundColor: "#eee" defines the background color of the QR code component; foregroundColor: "#fff" defines the foreground color of the QR code; value: "https: / / www.xxx.com" defines the content encoded in the QR code; style: {...} defines the style of the QR code component, including size and layout; title: "Page Containing QR Code" defines the title of the page where the QR code is located.

[0076] Step 102, input the program description information and the target component mode entry into the target large language model to obtain the program mode definition output by the target large language model.

[0077] In the embodiment of the present application, since the target component mode entry contains all the functions of the program that needs to be generated according to the program description information, therefore, by assembling the target component mode entry, the program mode definition (Schema definition) corresponding to the program that needs to be generated according to the program description information can be generated.

[0078] Since the component mode entry is a blueprint for describing the data structure or components of each part of the system or application, such as database Schema, UI Schema, API Schema, etc. These component mode entries define the low code of the data model, UI layout, interaction rules, API interfaces, etc. Therefore, using the above target component mode entry that matches the program description information, the program mode definition corresponding to the complete program required by the program description information can be generated, and the program mode definition can be manifested as the complete low code of the program described by the program description information.

[0079] Specifically, the program description information and the target component mode entry can be input into the target large language model, and the target large language model (Large Language Models, LLMs) can directly combine, arrange, adjust, modify, etc. each target component mode entry based on the program description information to generate the corresponding program mode definition. Among them, the target large language model can include but is not limited to GPT, LLaMA, MOSS, Pangu, etc., and the embodiments of the present application do not make specific limitations.

[0080] In an embodiment of the present application, the target large language model can be a general large language model. When using the general large language model to perform the above tasks, corresponding prompt information can be input, such as "The following is the program description information and the required schema data. Please output the complete schema definition of the program according to the program description information and the schema data." The target large language model can also be a task-specific model obtained by fine-tuning the general large language model, dedicated to the above tasks, so that the target large language model can perform the above tasks without inputting prompt information, improving the operation efficiency and the accuracy of the output of the program schema definition.

[0081] Step 103: Generate an abstract syntax tree corresponding to the program description information based on the program schema definition.

[0082] In an embodiment of the present application, the program schema definition can be parsed to generate an abstract syntax tree (AST) corresponding to the program description information. Specifically, since schema definitions are usually defined using different syntax types, such as JSON (JavaScript Object Notation) syntax, YAML (YAML Ain't Markup Language) syntax, etc., the corresponding AST conversion tool for the syntax type can be used to parse and convert it to obtain the abstract syntax tree. For program description information of the JSON type, tools such as JSON Schema to AST, such as Esprima, Acorn, JSON to AST, etc., can be used. For program description information of the YAML type, tools such as JSON to AST, such as yaml-ast-parser, etc., can be used. The embodiments of the present application do not make specific limitations.

[0083] In addition, instead of using existing tools, the program schema definition can be directly parsed and converted to improve the conversion efficiency. Specifically, the program schema definition can be first parsed to extract the defined structure, such as function names, parameter lists, function bodies, etc. The parser can be a JSON parser, a YAML parser, etc., depending on the specific syntax type of the program schema definition. During the parsing process, the root node (such as the starting point of the function definition) can be extracted according to the structure of the schema, and each attribute in the schema can be traversed. After parsing the schema, each attribute corresponds to a node in the AST, and the entire abstract syntax tree is established recursively.

[0084] Step 104: Generate program code corresponding to the program description information according to the abstract syntax tree.

[0085] In the embodiments of the present application, program code for the program required for generating program description information can be generated according to the abstract syntax tree. Specifically, each node of the AST can be recursively traversed, and corresponding code snippets can be generated according to the type of the node, and then the code snippets generated by each child node are combined into complete code.

[0086] In addition, ready-made tools and libraries can also be used to implement the conversion from the abstract syntax tree to program code, which can specifically include but are not limited to the Babel tool for JavaScript, the ast module for Python, ANTLR, etc. The embodiments of the present application do not make specific limitations.

[0087] It should be noted that the format type adopted by the program code can be flexibly selected by technicians according to actual needs, and can include but are not limited to JavaScript, Vue.js, React.js, etc. The embodiments of the present application do not make specific limitations.

[0088] In the embodiments of the present application, since the program description information can describe a complete program, such as the entire client program, or can also describe partial modules in the program, such as the UI interface in the client program, therefore, the generated program code can be directly compiled to obtain the corresponding program, or can be integrated with other code to form complete software code. The embodiments of the present application do not make specific limitations.

[0089] In summary, a code generation method provided by an embodiment of the present invention includes: obtaining a target component mode entry from a component mode information library based on program description information; inputting the program description information and the target component mode entry into a target large language model to obtain a program mode definition output by the target large language model; generating an abstract syntax tree corresponding to the program description information based on the program mode definition; and generating program code corresponding to the program description information according to the abstract syntax tree. It is possible to screen the defined component models to obtain a small number of target component mode entries that meet the program description information, and then use the large language model to generate a program mode definition with relatively high accuracy according to the target component mode entries, and then convert the program mode definition into an abstract syntax tree, and directly generate accurate program code through the abstract syntax tree. The semantic understanding ability of the large language model is fully utilized in the process of automatic code generation. When the large language model can fully understand the program development requirements, the randomness and uncertainty of the large language model are suppressed through the defined regularized component models, improving the accuracy and practicality of the automatically generated program code, and still having relatively high accuracy when facing the demand for generating complete program code.

[0090] Refer to Figure 2 , Figure 2 is the step flowchart of a code generation method provided by an embodiment of the present invention. AsFigure 2 As shown, the method may include:

[0091] Step 201, obtain the program description media information and the entry function description information corresponding to the component mode information library.

[0092] In the embodiments of the present application, the program requirer usually conceives the required program functions through means such as meetings, conversations, and mind maps. These conception processes will form program description media information such as images, audio, and video. By analyzing and summarizing this program description media information, program description information can be obtained. Among them, the program description media information may include, but is not limited to, meeting recordings, meeting videos, conversation recordings, mind maps, program design sketches, UI design diagrams, development requirement description texts, meeting record texts, etc., and the embodiments of the present application do not make specific limitations.

[0093] Specifically, since a program function may be implemented by multiple technical means, in the embodiments of the present application, the component model information library may record the entry function description information corresponding to the technical means that can be provided by the code generation automation process of this solution. The entry function description information can be used to refine and summarize the program description media information, and the functions included in the entry function description information can be used to generate the corresponding program description information of the program description media information. Among them, the entry function description information may be additional information corresponding to each component mode entry in the component mode information library for recording its functions, roles, and usage methods, or may be the name information of the component mode entry, and the embodiments of the present application do not make specific limitations.

[0094] Step 202, input the program description media information and the entry function description information into the target large language model to obtain the program description information output by the target large language model; wherein, the keywords included in the program description information match the keywords included in the entry function description information.

[0095] In an embodiment of the present application, the target large language model may be a multimodal large language model (MLLM). A multimodal large language model is an artificial intelligence model that can process and generate various different modal data (such as text, images, videos, audio, etc.). The program description media information and the entry function description information can be input into the target large language model to obtain the program description information output by the target large language model, so that the keywords included in the program description information match the keywords included in the entry function description information. Among them, the matching of the keywords included in the program description information and the keywords included in the entry function description information means that the program description information is described using the terms in the entry function description information. Exemplarily, if the program description media information includes "There are two buttons set on this page, one confirmation button and one cancellation button", and the entry function description information includes "Button control", then "Button control" can be used in the program description information to express "button".

[0096] In an embodiment of the present application, the target large language model may be a general large language model. When using the general large language model to perform the above tasks, corresponding prompt word information can be input, such as "The following is the program description media information and the describable development function information that can be used. Please summarize the program description media information based on these contents to obtain text-based program description information. The program description information should be based on the functions existing in the development function description." The target large language model can also be a task-specific model obtained by fine-tuning the general large language model, dedicated to the task of generating program description information based on the above program description media information and entry function description information, so that the target large language model can also perform the above tasks without inputting prompt word information.

[0097] By obtaining the program description media information and the entry function description information corresponding to the component pattern information library; inputting the program description media information and the entry function description information into the target large language model to obtain the program description information output by the target large language model. It is possible to automatically generate program description information according to the media-based development requirement information, which helps to further improve the development efficiency.

[0098] Step 203, obtain the target component pattern entry from the component pattern information library based on the program description information.

[0099] This step can refer to step 101, and the embodiments of the present invention will not be elaborated here.

[0100] Optionally, step 203 can be implemented through the following sub-steps:

[0101] Sub-step A1, perform semantic segmentation processing on the program description information, and extract the segmentation semantic features corresponding to the segmentation result.

[0102] In the embodiments of the present application, semantic segmentation processing can be performed on the program description information to obtain multiple segmentation results. Semantic Segmentation of Text aims to divide the text into different parts or paragraphs according to the semantic content in the text, and each part represents a semantic unit or topic. Specifically, semantic segmentation can include rule-based segmentation, such as dividing paragraphs according to format markers (such as line breaks, paragraph marks, punctuation marks) in the text; it can also include machine learning-based segmentation, that is, using natural language processing models (such as BERT, GPT, etc.) to identify natural pause points in semantics and divide the content into different semantic units. The embodiments of the present application do not specifically limit the above semantic segmentation methods.

[0103] After obtaining the semantic segmentation results, the segmented semantic features of the semantic segmentation results can be extracted through word embedding models, topic modeling, Transformer-based semantic feature extraction models, etc.

[0104] Sub-step A2: Match the segmented semantic features with the entry semantic features corresponding to each component pattern entry in the component pattern information library to obtain the target component pattern entry.

[0105] In the embodiments of the present application, for each component pattern entry in the component pattern information library, corresponding entry semantic features can be generated respectively. The entry semantic features can be generated when matching is required, or can be generated and stored in advance to improve the matching speed. The entry semantic features can be obtained by extracting semantic features based on relevant information (such as name, description, explanation) of the component pattern entry. Each segmented semantic feature can be matched with the entry semantic features corresponding to each component pattern entry to obtain the semantic similarity between each segmented semantic feature and each entry semantic feature.

[0106] In one implementation manner, among all the semantic similarities corresponding to a segmented semantic feature, the component pattern entries of the entry semantic features corresponding to one or more maximum similarities can be determined as the target component pattern entries. In another implementation manner, among all the semantic similarities corresponding to a segmented semantic feature, the component pattern entries of the entry semantic features corresponding to the semantic similarities greater than or equal to the preset similarity can be determined.

[0107] By performing semantic segmentation processing on the program description information, the segmentation semantic features corresponding to the segmentation results are extracted; the segmentation semantic features are matched with the entry semantic features corresponding to each component pattern entry in the component pattern information library to obtain the target component pattern entry. It is possible to determine the target component pattern entry based on the semantic features. Even when there are certain deviations between the program description information and the description of the component pattern entry, a relatively accurate target component pattern entry can still be determined.

[0108] Optionally, step 203 can also be implemented through the following sub-steps:

[0109] Sub-step B1, obtain the entry name information corresponding to each component pattern entry in the component pattern information library.

[0110] In the embodiments of the present application, since the number of component pattern entries in the component pattern information library may be relatively large, in order to improve the efficiency of determining the target entry name, it is also possible to only obtain the entry name information corresponding to each component pattern entry in the component pattern information library. The entry name information usually carries some information about the functions implemented by the entry. Among them, the entry name information can include the entry names corresponding to all component pattern entries in the component pattern information library.

[0111] Sub-step B2, input the entry name information and the program description information into the target large language model to obtain the target entry name corresponding to the component pattern entry required for the target program implementing the program description information output by the target large language model.

[0112] Since the amount of information contained in the entry name information is limited and may be relatively vague, in order to accurately determine the target component pattern entry based on the entry name information, a target large language model can be used. Input the entry name information and the program description information into the target large language model, so that the target large language model selects the target entry name corresponding to the target component pattern entry required by the program according to the entry name information.

[0113] In one implementation manner, a general large language model can be used to perform the above task of obtaining the target entry name. At this time, when inputting the entry name information and the program description information into the large language model, corresponding prompt word information can be input, such as "The following is the program description information and all Schema component entry names. Please select the required component entry names according to the program requirements and output them". The target large language model can also be a task-specific model obtained by fine-tuning the general large language model, dedicated to the above task of obtaining the target entry name, so that the target large language model can also perform the above task without inputting prompt word information.

[0114] Sub-step B3, obtain the target component pattern entry from the component pattern information library based on the target entry name.

[0115] By obtaining the entry name information corresponding to each component mode entry in the component mode information library; inputting the entry name information and the program description information into the target large language model to obtain the target entry name corresponding to the component mode entry required for the target program implementing the program description information output by the target large language model; and obtaining the target component mode entry from the component mode information library based on the target entry name. It is possible to determine the target component mode entry that matches the program description information based on the entry name information with a relatively small amount of data, and the entire process is based on a large language model with relatively high semantic recognition accuracy, which can improve the efficiency of determining the target component mode entry while maintaining relatively high accuracy.

[0116] Step 204, determine the target program type corresponding to the target program described by the program description information.

[0117] In the embodiments of the present application, the target program described by the program description information can be various different program types, including but not limited to user interface programs, front-end programs, back-end programs, data processing programs, web programs, etc. For different program types, different prompt texts can be used to improve the output accuracy of program mode definition. Therefore, the target program type corresponding to the target program described by the program description information can be determined according to the program description information.

[0118] Specifically, semantic feature extraction can be performed on the program description information to obtain program description semantic features, and the program description semantic features are subjected to similarity matching with the type semantic features corresponding to each preset program type to obtain the feature similarity between the program description semantic features and each type semantic feature. The program type corresponding to the type semantic feature with the highest similarity in the feature similarity of the program description semantic features is determined as the target program type corresponding to the program description information.

[0119] It is also possible to directly input all preset program types and the program description information into the large language model, and instruct the large language model to select the target program type that best matches the program description information from the preset program types through corresponding prompt words.

[0120] Step 205, match the target prompt text from the prompt text database based on the target program type.

[0121] In the embodiments of the present application, different prompting questions for generating program mode definitions can be set for different target program types. For example, for front-end program types, the corresponding prompting text can include qualifiers for interface styles, such as concise, beautiful, high contrast, etc. For algorithm program types, the corresponding prompting text can include qualifiers such as efficient and low resource occupancy rate. Thus, when the large language model generates program mode definitions for program description information of different program types, targeted optimization can be carried out, making it more in line with the usage scenarios of specific program types.

[0122] Specifically, the corresponding relationships between multiple program types and prompting text can be set in advance through expert experience and other means, and the above-mentioned target program type can be used to retrieve the corresponding relationship to obtain the corresponding target prompting text.

[0123] Step 206, input the program description information, the target component mode entry, and the target prompting text into the target large language model to obtain the program mode definition output by the target large language model.

[0124] By determining the target program type corresponding to the target program described by the program description information; matching the target prompting text from the prompting text database based on the target program type; inputting the program description information, the target component mode entry, and the target prompting text into the target large language model to obtain the program mode definition output by the target large language model. It can enable the large language model to perform targeted optimization when generating program mode definitions for program description information of different program types, making it more in line with the usage scenarios of specific program types and improving the matching degree between the program mode definition and the program development requirements.

[0125] Step 207, generate an abstract syntax tree corresponding to the program description information based on the program mode definition.

[0126] This step can refer to the above-mentioned step 103, and the embodiments of the present application will not elaborate further.

[0127] Step 208, parse the abstract syntax tree to obtain the first code.

[0128] In the embodiments of the present application, the abstract syntax tree can be parsed first to obtain the first code. The first code is directly parsed from the abstract syntax tree, and the program design content included in both is the same. The process of parsing the first code based on the abstract syntax tree can refer to the process of generating program code in step 104, and the embodiments of the present application will not elaborate further.

[0129] Step 209, input the first code and the program description information into the target large language model, and correct the first code through the target large language model to obtain the program code output by the target large language model.

[0130] Since the first code is generated based on the Schema definition, and there may be situations where the Schema definition lacks functions or is not suitable for the program, which may lead to imperfections in the first code. Therefore, the above-generated first code and program description information can also be input into the target large language model to correct the first code by the target large language model. Among them, the above corrections may include, but are not limited to, at least one of error correction, completion, optimization, adding annotation information, code adaptation, etc. Code adaptation means making necessary adjustments to the code so that it can meet the naming specifications, interface specifications, annotation specifications, format specifications, etc. of the current project or development team.

[0131] In one implementation, a general large language model can be used to perform the above correction task. At this time, when inputting the first code and program description information into the large language model, corresponding correction prompt word information can be input. The correction prompt word information can be flexibly set according to the correction requirements. For example, "The following is the program description information and the corresponding code. Please correct and optimize these codes according to the program requirements and add annotation information." The target large language model can also be a task-specific model obtained by fine-tuning the general large language model for exclusive use in the above code correction task, so that the target large language model can also perform the above task without inputting prompt word information.

[0132] By parsing the abstract syntax tree, the first code is obtained; the first code and the program description information are input into the target large language model, and the first code is corrected by the target large language model to obtain the program code output by the target large language model. It can improve the accuracy and practicality of the program code generated based on the program description information.

[0133] Optionally, step 209 may include:

[0134] Sub-step 2091, input the first code into the target large language model to obtain the code description information output by the target large language model.

[0135] In the embodiments of the present application, the first code can be input into the target large language model to obtain the code description information output by the target large language model, and the code description information is used to describe the actual functions and characteristics of the first code. In one implementation, a general large language model can be used to perform the above code description information generation task. At this time, when inputting the first code into the large language model, corresponding prompt word information can be input. For example, "Please describe the functions and characteristics of the following code in detail." The target large language model can also be a task-specific model obtained by fine-tuning the general large language model for exclusive use in the above code description information generation task, so that the target large language model can also perform the above task without inputting prompt word information.

[0136] Sub-step 2092: Generate a code correction instruction based on the functional differences between the code description information and the program description information.

[0137] In the embodiments of the present application, the code description information and the program description information can be compared to determine the functional differences between them, and then a code correction instruction can be generated based on the functional differences. Among them, the functional differences can be used to represent the differences between the software functions indicated by the code description information and the program description information. For example, the functional differences can include the missing functions and redundant functions of the code in the first code. Specifically, the code description information and the program description information can be analyzed by a natural language processing tool (NLP) to extract their semantic features, and the functional differences between them can be determined by comparing the semantic features. In addition, the functional differences between the two can also be determined based on a large language model, and the embodiments of the present application do not make specific limitations.

[0138] Exemplarily, if the code description information is "a user interface A, which includes a black QR code", and the program description information is "a user interface A, which includes a red QR code and a cancel button", then correspondingly, the functional differences between the two can include "the color difference of the QR code in the user interface A and the absence of the cancel button in the user interface A".

[0139] After obtaining the functional differences, a code correction instruction can be generated according to the functional differences. Specifically, the functional difference information can be concatenated with preset fixed information to obtain the code correction instruction. For example, the preset fixed information can be "The following are the problems existing in the code. Please correct and improve the code based on this." It is also possible to directly generate a code correction instruction based on the code description information and the program description information. For example, the code description information and the program description information can be directly input into a large language model, and prompt word information indicating how to modify the code description information to make it consistent with the program function indicated by the program description information can be input, and the code correction instruction output by the large language model can be directly obtained.

[0140] Sub-step 2093: Input the first code, the program description information, and the code correction instruction into the target large language model to obtain the program code output by the target large language model.

[0141] In the embodiments of the present application, the first code, the program description information, and the code correction instruction can be input into the target large language model, so that the large language model can specifically correct the first code based on the program description information and the code correction instruction, and obtain the program code output by the target large language model. Since the amount of information that the large language model can refer to when modifying the code is increased, it helps to improve the suppression effect of the model hallucination of the large language model during the correction process, thereby improving the code correction effect and obtaining a more accurate program code.

[0142] It should be noted that in the embodiments of the present application, since there may be differences in accuracy and efficiency among different large language models when performing different tasks, the target large language models performing different tasks in different steps may be the same or different. The same general large language model can be used to perform all tasks, different general large language models can be used to perform different tasks, or different fine-tuning methods can be used for different tasks to obtain different task-specific models. The embodiments of the present application do not make specific limitations.

[0143] Referring to Figure 3 , Figure 3 shows a flowchart of an example of code generation provided by the embodiments of the present application. Referring to Figure 3 , taking the generation of a client program as an example, the user can first describe the client requirements to obtain program description information, then retrieve the component pattern information library based on the program description information to recall the target component pattern entries, input the target component pattern entries and the program description information into a large model to obtain a program schema definition, then generate the first code according to the program schema definition, and finally input the first code and the program description information into the large model to enable the large model to correct the first code to obtain the complete program code of the client.

[0144] In summary, another code generation method provided by the embodiments of the present invention obtains target component pattern entries from the component pattern information library based on program description information; inputs the program description information and the target component pattern entries into a target large language model to obtain a program schema definition output by the target large language model; generates an abstract syntax tree corresponding to the program description information based on the program schema definition; and generates program code corresponding to the program description information according to the abstract syntax tree. It can screen the defined component models to obtain a small number of target component pattern entries that meet the program description information, then use the large language model to generate a program schema definition with higher accuracy according to the target component pattern entries, then convert the program schema definition into an abstract syntax tree, and directly generate accurate program code through the abstract syntax tree. In the process of automatic code generation, the semantic understanding ability of the large language model is fully utilized. When the large language model can fully understand the program development requirements, the randomness and uncertainty of the large language model are suppressed by the defined regular component patterns, improving the accuracy and practicality of the automatically generated program code, and still having high accuracy when facing the requirement of generating complete program code.

[0145] Referring to Figure 4 , Figure 4 is a structural diagram of a code generation device provided by the embodiments of the present invention. The code generation device 40 may include:

[0146] An entry module 41 for obtaining a target component pattern entry from a component pattern information library based on program description information;

[0147] A pattern definition module 42 for inputting the program description information and the target component pattern entry into a target large language model to obtain a program pattern definition output by the target large language model;

[0148] An abstract syntax tree module 43 for generating an abstract syntax tree corresponding to the program description information based on the program pattern definition;

[0149] A code module 44 for generating program code corresponding to the program description information according to the abstract syntax tree.

[0150] Optionally, the code module includes:

[0151] A parsing sub-module for parsing the abstract syntax tree to obtain first code;

[0152] A first input sub-module for inputting the first code and the program description information into a target large language model, and correcting the first code through the target large language model to obtain the program code output by the target large language model.

[0153] Optionally, the first input sub-module includes:

[0154] A code description information unit for inputting the first code into a target large language model to obtain code description information output by the target large language model;

[0155] A code correction instruction unit for generating a code correction instruction according to the functional difference between the code description information and the program description information;

[0156] A first input unit for inputting the first code, the program description information, and the code correction instruction into a target large language model to obtain the program code output by the target large language model.

[0157] Optionally, the device further includes:

[0158] A first acquisition module for acquiring program description media information and entry function description information corresponding to the component pattern information library;

[0159] A program description information module for inputting the program description media information and the entry function description information into a target large language model to obtain program description information output by the target large language model; wherein, keywords included in the program description information match keywords included in the entry function description information.

[0160] Optionally, the entry module includes:

[0161] A semantic feature sub-module for performing semantic segmentation processing on the program description information and extracting the segmented semantic features corresponding to the segmentation results;

[0162] A first entry acquisition sub-module for matching the segmented semantic features with the entry semantic features corresponding to each component pattern entry in the component pattern information library to obtain the target component pattern entry.

[0163] Optionally, the entry module includes:

[0164] A second acquisition sub-module for acquiring the entry name information corresponding to each component pattern entry in the component pattern information library;

[0165] A second input sub-module for inputting the entry name information and the program description information into a target large language model to obtain the target entry name corresponding to the component pattern entry required for implementing the target program corresponding to the program description information output by the target large language model;

[0166] A second entry acquisition sub-module for acquiring a target component pattern entry from the component pattern information library based on the target entry name.

[0167] Optionally, the pattern definition module includes:

[0168] A program type sub-module for determining the target program type corresponding to the target program described by the program description information;

[0169] A prompt text sub-module for matching a target prompt text from a prompt text database based on the target program type;

[0170] A pattern definition determination sub-module for inputting the program description information, the target component pattern entry, and the target prompt text into a target large language model to obtain the program pattern definition output by the target large language model.

[0171] In summary, the code generation device provided by the embodiments of the present invention includes: an item module for obtaining target component mode items from a component mode information library based on program description information; a mode definition module for inputting the program description information and the target component mode items into a target large language model to obtain a program mode definition output by the target large language model; an abstract syntax tree module for generating an abstract syntax tree corresponding to the program description information based on the program mode definition; and a code module for generating program code corresponding to the program description information according to the abstract syntax tree. It can screen the defined component models to obtain a small number of target component mode items that meet the program description information, and then use the large language model to generate a program mode definition with higher accuracy according to the target component mode items. Then, the program mode definition is converted into an abstract syntax tree, and accurate program code is directly generated through the abstract syntax tree. The semantic understanding ability of the large language model is fully utilized in the process of automatic code generation. When the large language model can fully understand the program development requirements, the randomness and uncertainty of the large language model are suppressed by the defined regular component mode, improving the accuracy and practicality of the automatically generated program code, and still having high accuracy when facing the complete program code generation requirements.

[0172] Referring to Figure 5 , the present invention also provides a structural diagram of an electronic device, as Figure 5 shown, including: a processor 1101, a memory 1102, and a computer program 11021 stored on the memory and executable on the processor. When the processor executes the program, it implements the code generation method of the foregoing embodiments.

[0173] The present invention also provides a readable storage medium. When the instructions in the storage medium are executed by the processor of the electronic device, the electronic device can execute the code generation method of the foregoing embodiments.

[0174] For the device embodiments, since they are basically similar to the method embodiments, the description is relatively simple. For the relevant parts, please refer to the partial description of the method embodiments.

[0175] It should be noted that all kinds of information and data obtained in the embodiments of the present invention are obtained under the authorization of the information / data holder.

[0176] The algorithms and displays provided herein are not inherently related to any particular computer, virtual system, or other device. A variety of general-purpose systems may also be used in conjunction with the teachings presented herein. The structure required to construct such systems will be apparent from the above description. Additionally, the present invention is not directed to any particular programming language. It should be understood that the teachings of the present invention described herein can be implemented in a variety of programming languages, and the description of a particular language above is for the purpose of disclosing the best mode of the present invention.

[0177] In the specification provided herein, numerous specific details are set forth. However, it is understood that embodiments of the present invention may be practiced without these specific details. In some instances, well-known methods, structures, and techniques have not been shown in detail so as not to obscure the understanding of this specification.

[0178] Similarly, it should be understood that in order to streamline the present invention and assist in understanding one or more of the various inventive aspects, in the above description of the exemplary embodiments of the present invention, the various features of the present invention are sometimes grouped together in a single embodiment, figure, or description thereof. However, the disclosed method should not be construed as reflecting an intention that the claimed invention requires more features than are expressly recited in each claim. Rather, as reflected in the following claims, the inventive aspects lie in less than all the features of the single foregoing disclosed embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into the detailed description, with each claim standing on its own as a separate embodiment of the present invention.

[0179] Those skilled in the art will appreciate that the modules in the devices in the embodiments can be adaptively changed and disposed in one or more devices different from the embodiments. The modules or units or components in the embodiments can be combined into one module or unit or component, and in addition, they can be divided into multiple sub-modules or sub-units or sub-components. Except for the fact that at least some of such features and / or processes or units are mutually exclusive, any combination can be used to combine all the features disclosed in this specification (including the accompanying claims, abstract, and drawings) and all the processes or units of any method or device so disclosed. Unless otherwise expressly stated, each feature disclosed in this specification (including the accompanying claims, abstract, and drawings) can be replaced by an alternative feature that provides the same, equivalent, or similar purpose.

[0180] Each component embodiment of the present invention can be implemented in hardware, or in software modules running on one or more processors, or in a combination thereof. Those skilled in the art should understand that a microprocessor or a digital signal processor (DSP) can be used in practice to implement some or all of the functions of some or all of the components in the sorting device according to the present invention. The present invention can also be implemented as a device or device program for executing part or all of the methods described herein. Such a program for implementing the present invention can be stored on a computer-readable medium, or can be in the form of one or more signals. Such signals can be downloaded from an Internet website, or provided on a carrier signal, or provided in any other form.

[0181] It should be noted that the above embodiments are illustrative of the present invention rather than restrictive of the present invention, and those skilled in the art can design alternative embodiments without departing from the scope of the appended claims. In the claims, any reference signs placed between parentheses shall not be construed as limiting the claim. The word "comprising" does not exclude the presence of elements or steps not listed in the claim. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The present invention can be implemented by means of hardware including several different elements and by means of a properly programmed computer. In the unit claims listing several devices, several of these devices can be embodied by the same item of hardware. The use of the words first, second, and third, etc. does not denote any order. These words can be interpreted as names.

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

[0183] The above are only the preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent replacements, and improvements made within the spirit and principles of the present invention shall be included in the protection scope of the present invention.

[0184] The above is only the specific implementation manner of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of changes or replacements within the technical scope disclosed by the present invention and should be covered by the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.

Claims

1. A code generation method, characterized in that, The method includes: Obtaining a target component pattern entry from a component pattern information library based on program description information, including: performing text retrieval and matching on index information corresponding to each component pattern entry in the component pattern information library based on the program description information to determine the target component pattern entry; the program description information is a document for describing program form and development requirements; the target component pattern entry is a low-code blueprint for describing the data structures or components of various parts of a system or application, defining data models, UI layouts, interaction rules, and API interfaces; Inputting the program description information and the target component pattern entry into a target large language model to obtain a program pattern definition output by the target large language model; the program pattern definition is the complete low-code of the program described by the program description information; Generating an abstract syntax tree corresponding to the program description information based on the program pattern definition; Parsing the abstract syntax tree to obtain first code; Inputting the first code into a target large language model to obtain code description information output by the target large language model; Generating a code correction instruction according to the functional difference between the code description information and the program description information; Inputting the first code, the program description information, and the code correction instruction into a target large language model to obtain program code output by the target large language model.

2. The method according to claim 1, wherein The method further includes: Obtaining program description media information and entry function description information corresponding to the component pattern information library; Inputting the program description media information and the entry function description information into a target large language model to obtain program description information output by the target large language model; wherein, keywords included in the program description information match keywords included in the entry function description information.

3. The method according to claim 1, wherein The obtaining a target component pattern entry from a component pattern information library based on program description information includes: Performing semantic segmentation processing on the program description information and extracting segmented semantic features corresponding to the segmentation result; Matching the segmented semantic features with entry semantic features corresponding to each component pattern entry in the component pattern information library to obtain the target component pattern entry.

4. The method according to claim 1, wherein The obtaining a target component pattern entry from a component pattern information library based on program description information includes: Obtaining entry name information corresponding to each component pattern entry in the component pattern information library; Inputting the entry name information and the program description information into a target large language model to obtain a target entry name corresponding to the component pattern entry required to implement the target program corresponding to the program description information output by the target large language model; Obtaining a target component pattern entry from the component pattern information library based on the target entry name.

5. The method according to claim 1, characterized in that The inputting the program description information and the target component pattern entry into a target large language model to obtain a program pattern definition output by the target large language model includes: Determining a target program type corresponding to the target program described by the program description information; Matching a target prompt text from a prompt text database based on the target program type; Input the program description information, the target component pattern entry, and the target prompt text into a target large language model to obtain a program pattern definition output by the target large language model.

6. A code generation device, characterized in that, The device includes: An entry module, configured to obtain a target component pattern entry from a component pattern information library based on program description information, including: performing text retrieval and matching on index information corresponding to each component pattern entry in the component pattern information library based on the program description information to determine the target component pattern entry; the program description information represents a document for describing program form and development requirements; the target component pattern entry is a blueprint for describing data structures or components of various parts of a system or application, and is low-code for defining data models, UI layouts, interaction rules, and API interfaces. A pattern definition module, configured to input the program description information and the target component pattern entry into a target large language model to obtain a program pattern definition output by the target large language model; the program pattern definition is the complete low-code of the program described by the program description information. An abstract syntax tree module, configured to generate an abstract syntax tree corresponding to the program description information based on the program pattern definition. A code module, configured to generate program code corresponding to the program description information according to the abstract syntax tree, including: a parsing sub-module, configured to parse the abstract syntax tree to obtain first code; a first input sub-module, including: a code description information unit, configured to input the first code into a target large language model to obtain code description information output by the target large language model; a code correction instruction unit, configured to generate a code correction instruction according to a functional difference between the code description information and the program description information; a first input unit, configured to input the first code, the program description information, and the code correction instruction into a target large language model to obtain program code output by the target large language model.

7. An electronic device, characterized in that, It includes: A processor, a memory, and a computer program stored on the memory and executable on the processor, and when the processor executes the program, it implements the method according to any one of claims 1-5.

8. A readable storage medium, characterized in that, When the instructions in the readable storage medium are executed by a processor of an electronic device, the electronic device is enabled to execute the method according to any one of claims 1-5.

Citation Information

Patent Citations

  • Structured query language generation method and device, electronic equipment and storage medium

    CN118093621A

  • Code generation method based on large language model, medium and equipment

    CN118363580A