Pre-training large model industrial code generation method based on retrieval enhancement and code static analysis

By using a method based on retrieval enhancement and code static analysis, industrial code that meets logical functions and actual operation requirements is generated, which solves the problem of inaccurate generation results of pre-trained large models and improves the accuracy and executability of code generation.

CN120780352APending Publication Date: 2025-10-14BEIJING UNIV OF POSTS & TELECOMM
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510876436.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-27
Publication Date
2025-10-14

AI Technical Summary

Technical Problem

Existing technologies have difficulty in generating industrial structured control language codes that meet both logical functional correctness and practical operational feasibility, especially in pre-trained large models, where the accuracy of the generated results is low.

Method used

A method based on retrieval enhancement and code static analysis is adopted. By constructing fine-grained code filling templates and hybrid retrieval technology, preliminary code results are generated, and static analysis checks and post-processing are performed to ensure the correctness and executableness of the generated code.

Benefits of technology

The format correctness and practical operability of the generated code are improved, common compilation errors are corrected, and the accuracy of industrial code generation is improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120780352A_ABST
    Figure CN120780352A_ABST
Patent Text Reader

Abstract

The invention discloses a pre-training large model industrial code generation method based on retrieval enhancement and code static analysis. Firstly, a program sample set is obtained, a few-sample example retrieval pool is constructed, and examples comprise code basic information, target code functions, a fine-grained code blank filling template and thinking chain statements. And obtaining question description, and constructing cue words by using a hybrid retrieval enhancement technology. And inputting the cue word into the pre-trained large model to generate a target code. And finally, performing static analysis and post-processing on the generated code to obtain a final result. According to the method, the information provided by the program sample can be effectively utilized to generate the code conforming to the problem description; and through the code blank filling template, the probability of generating codes with correct formats is improved. Through static analysis check and post-processing, common compiling errors in industrial code generation are corrected.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to a pre-training large model industrial code generation method based on retrieval enhancement and code static analysis, and belongs to the technical field of software automation, in particular to the technical field of industrial code automatic generation. BACKGROUND

[0002] In the field of natural language processing (NLP) and machine learning, the code generation task is an important and challenging problem. After the advent of large models, pre-training large models are mainly used to generate common programming languages such as python and java, but how to generate SCL and other industrial structured control languages while ensuring the correctness of the logical function of the generated results and the executability in actual operation still does not have an effective method.

[0003] How to solve the above problems in the field of industrial code automatic generation has become a technical problem that needs to be solved in the field of software automation. SUMMARY

[0004] Therefore, the purpose of the present application is to solve the problems and deficiencies in the prior art, and to provide a pre-training large model industrial code generation method based on retrieval enhancement and code static analysis, which can solve the problem of low accuracy of industrial code generation caused by insufficient training data of the model without modifying the parameters of the pre-training model.

[0005] In order to achieve the above purpose, the present application provides a pre-training large model industrial code generation method based on retrieval enhancement and code static analysis, which comprises the following operation steps:

[0006] Step (1) obtains a program example set containing problem description and correct answer; the problem description refers to basic information such as input and output interface of target code and functions to be implemented, that is, it describes the technical problems and tasks to be solved;

[0007] Step (2) constructs the program example into an example, and constructs a few-shot example retrieval pool;

[0008] Step (3) obtains the problem description of the target code to be generated;

[0009] Step (4) uses a hybrid retrieval enhancement technique to construct a prompt word;

[0010] Step (5) inputs the prompt word into the pre-training large model to obtain a preliminary result of the generated code;

[0011] Step (6) performs static analysis and inspection on the preliminary result of the generated code, if the inspection is passed, the code generation process is ended, otherwise go to step (7);

[0012] Step (7) post-processes the preliminary result of the code generation to obtain a final code, and the code generation process ends.

[0013] The specific content of constructing the program example into an example in step (2) is: first, based on the characteristics of industrial code, a fine-grained code fill-in-the-blank template is constructed; then the program example, the code fill-in-the-blank template and the thinking chain sentence are jointly composed into an example to guide the pre-trained large model to generate code with correct format framework and perfect logical function.

[0014] The specific content of constructing the prompt word using the hybrid retrieval enhancement technology in step (4) is: according to the current problem description and based on the characteristics of industrial code, a fine-grained code fill-in-the-blank template is constructed, and then the current problem description, the code fill-in-the-blank template and the example obtained by using the hybrid retrieval enhancement technology are jointly composed into a prompt word.

[0015] In step (4), the specific content of constructing the prompt word using the hybrid retrieval enhancement technology is: combining rule screening, sparse retrieval and vector retrieval, obtaining a hybrid retrieval result, and then constructing a few-sample prompt word.

[0016] The specific content of the static analysis check on the preliminary result of the code generation in step (6) is: using a static analysis checking program, analyzing the abstract syntax tree of the preliminary result of the code generation, and screening the code containing variable undefined error; extracting a keyword table from the automatic software platform document, and screening the code containing API error.

[0017] The specific content of the post-processing operation in step (7) includes the following specific operation steps:

[0018] (71) Extracting raw data from the corresponding industrial document, performing data cleaning, obtaining the API keyword table of the corresponding industrial code, and all API keywords not in the table can be determined as error keywords;

[0019] (72) For the preliminary result of the code generation, extracting a constant variable list from the constant variable definition part, parsing the abstract syntax tree AST of the code logic function part, dividing the corresponding industrial code according to the top node of the AST to obtain a code block list, and post-processing the code block as a unit;

[0020] (73) For each code block, using a regular expression to extract the constant variable in the block, and comparing it with the aforementioned constant variable list, if an undefined constant or variable is found, the block is deleted from the code block list; using a regular expression to extract the API keyword in the block, and comparing it with the API keyword table of the corresponding industrial code, if an error API keyword is found, the block is deleted from the code block list;

[0021] (74) After traversing the entire code block list, the remaining blocks are connected to obtain the processed result code.

[0022] The beneficial effects of the present application are that, compared with the prior art, the pre-trained large model industrial code generation method based on retrieval enhancement and code static analysis provided by the present application can effectively utilize the information provided by the program examples to generate codes that meet the problem description. At the same time, through the code fill-in-the-blank template, the probability of generating format-correct codes is improved. Through static analysis checking and post-processing, common compilation errors in industrial code generation are corrected, and the executability of the codes in actual operation is improved. BRIEF DESCRIPTION OF DRAWINGS

[0023] Figure 1 A flowchart of the method proposed by the present application is shown in the figure;

[0024] Figure 2 An example of an embodiment of the present application and a few-sample prompt word diagram is shown in the figure;

[0025] Figure 3 A code fill-in-the-blank template diagram of an embodiment of the present application is shown in the figure;

[0026] Figure 4 A flowchart of post-processing based on code static analysis is shown in the figure. DETAILED DESCRIPTION

[0027] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below with reference to the accompanying drawings. It should be understood that these embodiments are only used to illustrate the present application and not to limit the scope of the present application. After reading the present application, those skilled in the art can make various modifications to the present application, and all such modifications fall within the scope defined by the claims attached hereto.

[0028] Referring to Figure 1 , the pre-trained large model industrial code generation method based on retrieval enhancement and code static analysis proposed by the present application is introduced, which includes the following operation steps:

[0029] Step (1) obtaining a program example set containing problem description and correct answer; the problem description refers to basic information such as input and output interface of target code and functions to be implemented, i.e. describing technical problems and tasks to be solved;

[0030] Step (2) constructing the program example as an example to construct a few-sample example retrieval pool;

[0031] Step (3) obtaining the problem description of the target code to be generated;

[0032] Step (4) using hybrid retrieval enhancement technology to construct a prompt word;

[0033] Step (5) inputs the prompt word into the pre-trained large model to obtain a preliminary result of generated code;

[0034] Step (6) performs static analysis checking on the preliminary result of generated code, and if the checking is passed, the code generation process ends, otherwise, it goes to step (7);

[0035] Step (7) post-processes the preliminary result of generated code to obtain the final code, and the code generation process ends.

[0036] Taking the generation of SCL code for Siemens TIAPortal platform as an example, the program sample and the code blank filling template and the thinking chain sentence jointly constitute an example for constructing a few-sample example retrieval pool. The problem description of the target code to be generated is obtained from the application environment, and the problem description is used as a query statement, combined with rule screening, sparse retrieval and vector retrieval, 9 examples are obtained from the retrieval pool. As shown in Figure 2 , the 9 examples, the problem description, and the code blank filling template jointly constitute the prompt word, which is input into the pre-trained language model to generate the SCL code.

[0037] The specific content of constructing the program sample into an example in step (2) is: first, based on the characteristics of industrial code, a fine-grained code blank filling template is constructed; then the program sample, the code blank filling template and the thinking chain sentence jointly constitute an example to guide the pre-trained large model to generate code with correct format framework and perfect logic function.

[0038] The specific content of constructing the prompt word using the hybrid retrieval enhancement technology in step (4) is: according to the current problem description, and based on the characteristics of industrial code, a fine-grained code blank filling template is constructed, and then the current problem description, the code blank filling template and the example obtained by using the hybrid retrieval enhancement technology are jointly constructed into the prompt word.

[0039] In one specific embodiment of the present application, constructing the code blank filling template according to the problem description includes the following steps:

[0040] First, analyze the characteristics of SCL code:

[0041] (a) SCL code can be divided into constant variable definition and logic function implementation;

[0042] (b) There are 6 types of constant variable definition, and the number is fixed;

[0043] (c) SCL code has strict format, and there is explicit separation between code blocks.

[0044] Then, based on the above characteristics, a code blank filling template is constructed as Figure 3The code filling template is filled in the function header, the INPUT variable definition block, the OUTPUT variable definition block, and the IN_OUT block using natural language processing technology to extract information in the question description. The framework of the code to be filled in is arranged, and two variable definition blocks, a constant definition block, and a logical function block are filled in by the model using special symbols "[ ]".

[0045] In step (4), the specific content of constructing the prompt word using the mixed retrieval enhancement technology is: combining rule screening, sparse retrieval, and vector retrieval to obtain mixed retrieval results, and then constructing a few-sample prompt word.

[0046] Sparse retrieval is an information retrieval method based on sparse representation, which usually uses inverted index and keyword matching for retrieval. Sparse retrieval method mainly depends on the exact matching of words, and the typical examples are TF-IDF and BM25 classic algorithms.

[0047] Vector retrieval is a retrieval method based on dense vectors, which uses a deep learning model to represent queries and documents as fixed-dimensional vectors, and then retrieves by calculating the similarity between these vectors. The core idea of vector retrieval is to encode the semantic information of text into vectors, so as to capture semantic similarity, rather than just matching surface words.

[0048] Retrieval enhancement technology refers to the combination of generative language models and information retrieval technology, which enhances the generation ability of language models by introducing external knowledge base or documents, to improve their ability to answer complex questions or provide more accurate and detailed answers.

[0049] In one specific embodiment of the present application, the specific process of mixed retrieval is as follows:

[0050] (1) Rule screening step: analyze the program example set, count the code length, API number, for loop, if statement and other syntax points, and select a fixed example set with rich information and easy to understand by the model. The fixed example set remains unchanged in the retrieval of different query statements, and provides basic information of SCL code for the pre-trained language model.

[0051] (2) Sparse retrieval step: for the keywords in the question description, sparse retrieval based on BM25 algorithm is adopted. BM25 algorithm helps to capture API information and find examples containing relevant API calls.

[0052] (3) Vector retrieval module step: for the semantic information in the question description, vector similarity-based retrieval is used. Specifically, the paraphrase-multilingual-MiniLM-L12-v2 model of the sentence-transformers library (see https: / / www.sbert.net / ) is used to encode the query statement and the examples in the retrieval pool into 384-dimensional vectors, calculate the similarity between the vectors, and select the top k examples with the highest similarity. Vector retrieval helps capture the semantic information in the problem description and find examples similar to the target code function to be generated.

[0053] Through the above process, a total of 9 examples are retrieved, and the number of examples retrieved in each step accounts for 1:1:1, so as to balance the proportion of basic information, keyword information and semantic information provided by the examples.

[0054] The specific content of the static analysis check of the preliminary result of the generated code in step (6) is: using a static analysis check program, analyzing the abstract syntax tree of the preliminary result of the generated code, and screening the code containing variable undefined error; extracting a keyword table from the automated software platform document, and screening the code containing API error.

[0055] Referring to Figure 4 , the specific content of the post-processing operation in step (7) includes the following specific operation steps:

[0056] (71) Extracting raw data from the corresponding industrial document, performing data cleaning, obtaining the API keyword table of the corresponding industrial code, and all API keywords not in the table can be determined as error keywords;

[0057] (72) Extracting the constant variable list from the constant variable definition part of the preliminary result of the generated code, parsing the abstract syntax tree AST of the code logic function part, and dividing the corresponding industrial code according to the top node of the AST to obtain a code block list, and post-processing the code block as a unit;

[0058] (73) For each code block, using regular expressions to extract the constant variables in the block, and comparing with the aforementioned constant variable list, if an undefined constant or variable is found, the block is deleted from the code block list; using regular expressions to extract the API keywords in the block, and comparing with the API keyword table of the corresponding industrial code, if an error API keyword is found, the block is deleted from the code block list;

[0059] (74) After traversing the entire code block list, the remaining blocks are connected to obtain the processed result code.

[0060] In one specific embodiment of the present application, the static analysis post-processing includes the following steps:

[0061] First, the original data is extracted from the Siemens TIAPortal document, data cleaning is performed, the API keyword table of the SCL code is obtained, and all API keywords not in the table can be determined as error keywords.

[0062] The generated code is obtained from the pre-trained language model, the constant variable list is extracted from the constant variable definition part, the abstract syntax tree (AST) of the code logic function part is parsed, the SCL code is divided according to the top node of the AST, and the code block list is obtained. Post-processing is done in code block units.

[0063] For each code block, the constant variables in the block are extracted using regular expressions, and compared with the above constant variable list. If an undefined constant or variable is found, the block is deleted from the code block list. The API keywords in the block are extracted using regular expressions, and compared with the API keyword table. If an error API keyword is found, the block is deleted from the code block list.

[0064] After traversing the entire code block list, the remaining blocks are connected to obtain the processed result code.

[0065] The inventors have carried out a large number of experiments on the method proposed in the present application, and the experimental conditions are as follows:

[0066] For the hybrid retrieval method, the effectiveness of the rule screening, sparse retrieval and vector retrieval parts is verified respectively, and the optimal proportion and total number of recall examples of the three retrieval methods are determined through experiments. For specific few-shot example construction, the effectiveness of the code fill-in-the-blank template is verified through ablation experiments. In terms of code generation, a variety of models such as GLM-4, GLM-4-flash, codegeex-4, etc. are compared, and the model instruction compliance ability, logical reasoning ability, code generation ability and reasoning speed are considered comprehensively, and finally GLM-4 is selected to generate industrial code. For static analysis post-processing, various code block granularity such as line level, function level and block level are tried, and finally the block level is determined as the optimal granularity. At the same time, through specific case analysis, two typical scenarios of keyword error and variable undefined are selected for code post-processing optimization, and the effectiveness of the post-processing is proved through experiments.

[0067] The experimental results prove that the method proposed in the present application is feasible and effective.

[0068] The present application has been described in detail in the above embodiments, and those skilled in the art can make various changes to the present application according to the above description, therefore, some details in the embodiments should not constitute a limitation on the present application, and the scope of protection of the present application will be defined by the appended claims.

Claims

1. A method for generating industrial code for a pre-trained large model based on retrieval enhancement and code static analysis, characterized by: The method comprises the following steps: Step (1) obtaining a set of program examples including a problem description and a correct answer; the problem description includes basic information such as the input and output interfaces of the target code and the functions to be implemented, that is, describing the technical problems and tasks to be solved; Step (2) constructing the program sample as an example to construct a few-sample example retrieval pool; Step (3) obtaining a problem description of the target code to be generated; Step (4) constructing prompt words using hybrid retrieval enhancement technology; Step (5) inputs the prompt word into the pre-trained large model to obtain the preliminary result of the generated code; Step (6) performs a static analysis check on the preliminary result of the generated code. If the check is passed, the code generation process ends; otherwise, the process proceeds to step (7); Step (7) performs post-processing on the preliminary result of the generated code to obtain the final code, and the code generation process ends.

2. The method according to claim 1, wherein: The specific content of constructing the program sample into an example in step (2) is: first, based on the characteristics of industrial code, a fine-grained code filling template is constructed; then the program sample, the code filling template and the thinking chain statement are combined into an example to guide the pre-trained large model to generate code with a correct format framework and complete logical functions.

3. The method according to claim 1, wherein: The specific content of constructing prompt words using hybrid retrieval enhancement technology described in step (4) is: constructing a fine-grained code filling template based on the current problem description and the characteristics of industrial codes, and then combining the current problem description, the code filling template and the examples obtained using hybrid retrieval enhancement technology to form prompt words.

4. The method according to claim 1, wherein: In step (4), the specific content of constructing prompt words using hybrid search enhancement technology is: combining three methods of rule screening, sparse search and vector search to obtain hybrid search results, and then construct a few-sample prompt words.

5. The method according to claim 1, wherein: The specific content of the static analysis check on the preliminary result of the generated code in step (6) is: using a static analysis checker to analyze the abstract syntax tree of the preliminary result of the generated code and filter out the code containing the variable undefined error; Extract keyword lists from automation software platform documentation to filter out codes containing API errors.

6. The method according to claim 1, wherein: The specific content of the post-processing operation described in step (7) includes the following specific operation steps: (71) Extracting raw data from the corresponding industrial documents, performing data cleaning, and obtaining the API keyword table corresponding to the industrial code. All API keywords not in the table can be determined as incorrect keywords; (72) For the preliminary results of the generated code, a constant variable list is extracted from the constant variable definition part, the abstract syntax tree AST of the code logic function part is parsed, the corresponding industrial code is split according to the top-level node of the AST, and a code block list is obtained, and post-processing is performed on the code block as a unit; (73) For each code block, a regular expression is used to extract constant variables in the block, and the constant variables are compared with the aforementioned constant variable list. If undefined constants or variables are found, the block is deleted from the code block list; a regular expression is used to extract API keywords in the block, and the API keyword table of the corresponding industrial code is compared. If an incorrect API keyword is found, the block is deleted from the code block list; (74) After traversing the entire code block list, the remaining blocks are connected to obtain the processed result code.