PLC code generation and verification method based on large language model
Through multi-stage multi-modal input data and automated verification process, the problem of low complexity and correctness of PLC code generation is solved, efficient and accurate PLC code generation and deployment is achieved, and the work efficiency of network security researchers is improved.
Patent Information
- Application Number
- CN202411144236.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-20
- Publication Date
- 2024-12-27
- Estimated Expiration
- 2044-08-20
AI Technical Summary
The existing PLC code generation method based on large language models generates complex, inefficient and difficult to understand, low correctness, high coupling, and lacks automated deployment and verification processes, which affects the work efficiency of network security researchers and the reliability of code. sex.
Multi-stage multi-modal input data, including P&ID diagrams and natural languages, PLC code is generated through large language models, and syntax checks, logic verification and compilation tests are carried out to ensure the correctness and reusability of the code, and finally automated deployment is carried out.
It improves the accuracy and automation of PLC code, simplifies the deployment process, reduces the possibility of manual errors, and improves the work efficiency of network security researchers and the reliability of code.
Smart Images

Figure CN119201059A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a PLC code generation and verification method based on a large language model. Background Art
[0002] In industrial automation, programmable logic controllers (PLC) are core devices used to control machines and processes. With the increasing networking of industrial control systems (ICS), the security issues of ICS have become more and more prominent. The ICS range is a facility dedicated to testing, rehearsing and studying the cybersecurity of ICS. It helps enterprises and professionals improve their awareness and protection of ICS security risks by simulating a real ICS environment. In order to test and verify the security of ICS, cybersecurity researchers usually need to build ICS simulation scenarios. However, since PLC programming involves specific programming languages and logic, writing and debugging PLC code is a complex and time-consuming task for cybersecurity researchers who are not familiar with PLC code. Traditional PLC programming methods usually rely on manual coding and debugging, which is not only inefficient but also prone to human errors.
[0003] With the development of artificial intelligence technology, large language models (LLMs) have demonstrated powerful capabilities in natural language processing. LLMs can understand natural language descriptions and generate complex code logic texts through reasoning based on the descriptions. Using large language models to automatically generate PLC code and verify it will greatly improve the work efficiency of network security researchers and the reliability of PLC code, allowing them to focus more on discovering and solving security issues in industrial control systems.
[0004] At present, there are some methods for generating PLC code using large language models. These methods include directly inputting natural language to generate PLC code, using image recognition technology to generate IEC 61131-3 structured text control logic source code from piping and instrumentation diagrams (P&IDs), and using user feedback and external verification tools to guide large language models to generate more accurate PLC code. Specifically, by directly inputting natural language descriptions, users can easily convert their control requirements into PLC code; image recognition technology can extract key information from P&ID diagrams and automatically generate control logic source code that complies with the IEC 61131-3 standard; in addition, user feedback and external verification tools (including syntax checkers, compilers, and SMV validators) can perform multi-level verification and optimization on the PLC code generated by large language models to ensure the correctness and effectiveness of the generated code.
[0005] The existing methods of generating PLC codes using large language models still have some shortcomings, which are mainly manifested in the following aspects:
[0006] (1) The generated PLC code is complex, inefficient, and difficult to understand. The code generated by LLM often contains too much logic and steps, making it difficult to read and maintain the code. This complexity not only increases the execution time of the code and reduces efficiency, but also makes it difficult for network security researchers to understand the logic of the code, affecting the security analysis and maintenance of the system.
[0007] (2) The generated PLC code has low correctness, high coupling and low reusability. The code generated by the existing methods may contain logical errors or not meet the actual control requirements, resulting in low correctness of the code. In addition, the coupling between code modules may be too high, limiting the reusability of the code in different control scenarios. The lack of modular design further reduces the maintainability and scalability of the code, and increases the difficulty of subsequent modification and upgrade.
[0008] (3) The generated PLC code has a low level of automated deployment and verification. Although LLM can generate code, existing methods are still insufficient in terms of automated deployment and verification after code generation. The lack of an effective automated testing and verification process requires a lot of manual testing and debugging of the generated code, which increases the workload and may introduce human errors. At the same time, the imperfections of verification tools and methods also limit the comprehensive inspection of code correctness and security. Summary of the invention
[0009] The main purpose of the embodiment of the present invention is to propose a PLC code generation and verification method based on a large language model with high accuracy and high automation.
[0010] To achieve the above object, an embodiment of the present invention provides a PLC code generation and verification method based on a large language model, comprising the following steps:
[0011] Generate multi-stage multi-modal input data, the multi-stage multi-modal input data is used to characterize user needs and formulate a system design plan according to the needs analysis results; the multi-stages include the needs analysis stage and the design stage of the complete engineering cycle; the multi-modal input includes P&ID diagrams and natural language;
[0012] Generate PLC code according to the multi-stage multi-modal input data;
[0013] Performing verification testing on the generated PLC code, wherein the verification testing includes code verification and compiler verification;
[0014] After completing the verification test of the PLC code, the PLC code is deployed.
[0015] In some embodiments, generating multi-stage multi-modal input data comprises the following steps:
[0016] For security personnel who only have one natural language description of their needs:
[0017] Configure the initial requirement acquisition prompt to obtain the natural language requirement description entered by the user;
[0018] Perform semantic supplementation through a large language model to form an enhanced semantic prompt, so that the large language model generates PLC code;
[0019] Before the natural language requirement description is input into the large language model, a search and match is performed in the standard knowledge base, the requirement description is vectorized using the Word2Vec model, and the cosine similarity between the standard knowledge base vector and the requirement description vector is calculated to determine whether they belong to the same requirement module. If the cosine similarity is higher than the set threshold, the new natural language requirement description is replaced with the description in the standard knowledge base.
[0020] In some embodiments, the generating of multi-stage multi-modal input data further includes the following steps:
[0021] For the case where the safety personnel have the design instructions:
[0022] Obtaining a P&ID drawing from the design specification;
[0023] Input the P&ID diagram and natural language requirement description into LLM, combine the P&ID diagram and natural language requirement description to form a complete and detailed requirement statement;
[0024] Through image recognition and natural language processing technology, the information of the P&ID diagram is understood and parsed, and the P&ID diagram is integrated with the natural language requirement description to form a code generation enhanced semantic prompt to generate PLC code.
[0025] In some embodiments, generating PLC code according to the multi-stage multi-modal input data comprises the following steps:
[0026] Enhance the domain knowledge of LLM;
[0027] The multi-stage multi-modal input data is input into the LLM large language model to generate PLC code.
[0028] In some embodiments, the enhancing the domain knowledge of the LLM comprises the following steps:
[0029] Build a normative knowledge base, which includes knowledge in the field of industrial control systems, including PLC programming specifications, common control logic, standardized modules and actual cases;
[0030] Similar description content retrieval: After the user inputs natural language and P&ID diagrams, the model will search for content similar to the input in the specification knowledge base and provide it to LLM for reference;
[0031] Insert the retrieved relevant knowledge into the generation process of LLM to ensure that the model can use this expertise for code generation;
[0032] The step of inputting the multi-stage multi-modal input data into the LLM large language model to generate PLC code comprises the following steps:
[0033] Before generating the code, the code block repository is retrieved first. The code block repository contains verified PLC code modules. The large language model infers whether the requirement description has similar content based on the code block repository. If so, the code in the code block repository is integrated into the generated code.
[0034] Next, the large language model searches the canonical knowledge base to improve the generated code;
[0035] When generating code, the large language model will prioritize searching the code blocks that match the input requirements in the code block repository to ensure the reliability and reusability of the generated code; if the requirements are insufficient, the large language model can directly call the code blocks in the code block repository to reduce the time and potential errors in generating new code.
[0036] In some embodiments, the verification test of the generated PLC code comprises the following steps:
[0037] Syntax check: Perform syntax check on the generated PLC code to ensure that the code complies with the syntax rules of the PLC programming language. Use automated tools to check the code line by line to find and correct syntax errors.
[0038] Logic verification: After passing the syntax check, further logic verification is performed, using simulation tools and automated testing frameworks to perform logic analysis and simulation on the code to verify whether the code functions meet the design requirements; the logic verification includes testing of control flow, data processing, and interface interaction to ensure that the generated code logic is correct and has no loopholes;
[0039] Compilation test: Input the code that has passed the logic verification into the PLC compiler for compilation. The compiler will check whether the structure and data type of the code are correct. If there is an error, the compiler will provide detailed error information. After the compilation is passed, an executable file will be generated;
[0040] Execution test: After compilation, the generated code is deployed to the simulation PLC for execution test. Through simulation operation, the performance of the code in actual operation is detected to ensure that there are no errors and unexpected behaviors during operation.
[0041] In some embodiments, after completing the verification test of the PLC code, deploying the PLC code comprises the following steps:
[0042] Build a simulated PLC environment and select appropriate deployment tools, perform a final check on the code and prepare configuration files;
[0043] Upload the code to the simulated PLC environment, apply the configuration file, start the code running, and use the tool for real-time monitoring and logging;
[0044] Conduct real-time system monitoring and troubleshooting, perform performance evaluation and optimization, and regularly update and upgrade the code.
[0045] Another aspect of the embodiment of the present invention further provides a PLC code generation and verification device based on a large language model, comprising:
[0046] The first module is used to generate multi-stage multi-modal input data, wherein the multi-stage multi-modal input data is used to characterize user needs and formulate a system design plan according to the needs analysis results; the multi-stages include the needs analysis stage and the design stage of the complete engineering cycle; the multi-modal input includes P&ID diagrams and natural language;
[0047] A second module is used to generate PLC code according to the multi-stage multi-modal input data;
[0048] The third module is used to perform verification test on the generated PLC code, wherein the verification test includes code verification and compiler verification;
[0049] The fourth module is used to deploy the PLC code after completing the verification test of the PLC code.
[0050] Another aspect of an embodiment of the present invention further provides an electronic device, including a processor and a memory;
[0051] The memory is used to store programs;
[0052] The processor executes the program to implement the method described above.
[0053] Another aspect of the embodiments of the present invention further provides a computer-readable storage medium, wherein the storage medium stores a program, and the program is executed by a processor to implement the method described above.
[0054] The embodiment of the present invention also discloses a computer program product or a computer program, which includes a computer instruction stored in a computer-readable storage medium. A processor of a computer device can read the computer instruction from the computer-readable storage medium, and the processor executes the computer instruction, so that the computer device executes the above method.
[0055] The embodiments of the present invention include at least the following beneficial effects: the present invention provides a PLC code generation and verification method based on a large language model, which generates multi-stage multi-modal input data, and the multi-stage multi-modal input data is used to characterize user needs and formulate a system design scheme according to the results of the demand analysis; the multi-stage includes the demand analysis stage and the design stage of the complete engineering cycle; the multi-modal input includes P&ID diagrams and natural language; then, according to the multi-stage multi-modal input data, the PLC code is generated; then, the generated PLC code is verified and tested, and the verification test includes code verification and compiler verification; finally, after completing the verification test of the PLC code, the PLC code is deployed. The embodiments of the present invention have high accuracy and high automation, and can provide network security researchers with an efficient and accurate PLC code deployment method. BRIEF DESCRIPTION OF THE DRAWINGS
[0056] Figure 1 It is a schematic diagram of an implementation environment provided by an embodiment of the present invention;
[0057] Figure 2 is a flow chart of the overall steps provided by an embodiment of the present invention;
[0058] Figure 3 It is a schematic diagram of the overall framework provided by an embodiment of the present invention;
[0059] Figure 4 is a PLC code generation flow chart provided by an embodiment of the present invention;
[0060] Figure 5 It is a schematic diagram of the hardware structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0061] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention. When the following description refers to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the embodiments of the present invention. They are only examples of devices and methods consistent with some aspects of the embodiments of the present invention as detailed in the attached claims.
[0062] It is understood that the terms "first", "second", etc. used in the present invention may be used herein to describe various concepts, but unless otherwise specified, these concepts are not limited by these terms. These terms are only used to distinguish one concept from another. For example, without departing from the scope of the embodiments of the present invention, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the words "if" and "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0063] The terms "at least one", "multiple", "each", "any", etc. used in the present invention, at least one includes one, two or more, multiple includes two or more, each refers to each of the corresponding multiple, and any refers to any one of the multiple.
[0064] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which the present invention belongs. The terms used herein are only for the purpose of describing the embodiments of the present invention and are not intended to limit the present invention.
[0065] The PLC code generation and verification method based on a large language model provided in an embodiment of the present invention relates to the field of computer technology. The PLC code generation and verification method based on a large language model provided in an embodiment of the present invention can be applied to a terminal, can also be applied to a server, and can also be software running in a terminal or a server. In some embodiments, the terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, and a car terminal, etc., but is not limited to this; the server side can be configured as an independent physical server, or a server cluster or distributed system composed of multiple physical servers, and can also be configured to provide cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms and other basic cloud computing services. The server can also be a node server in a blockchain network; the software can be an application that implements a PLC code generation and verification method based on a large language model, etc., but is not limited to the above forms.
[0066] The present invention can be used in many general or special computer system environments or configurations. For example: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronic devices, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, etc. The present invention can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present invention can also be practiced in distributed computing environments, in which tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.
[0067] like Figure 1 FIG. 1 is a schematic diagram of an implementation environment provided by an embodiment of the present invention. Figure 1 The implementation environment includes at least one terminal 102 and a server 101. The terminal 102 and the server 101 can be connected to a network wirelessly or wired to complete data transmission and exchange.
[0068] Server 101 can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers. It can also be a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, CDN (Content Delivery Network), as well as big data and artificial intelligence platforms.
[0069] In addition, the server 101 can also be a node server in the blockchain network. Blockchain is a new application model of computer technologies such as distributed data storage, peer-to-peer transmission, consensus mechanism, and encryption algorithm.
[0070] The terminal 102 may be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, etc. The terminal 102 may also be a vehicle-mounted terminal of various device types described above, but is not limited thereto. The terminal 102 and the server 101 may be directly or indirectly connected via wired or wireless communication, which is not limited in this embodiment of the present invention.
[0071] Based on the example Figure 1 In the implementation environment shown, an embodiment of the present invention provides a PLC code generation and verification method based on a large language model. The following is an example of applying the PLC code generation and verification method based on a large language model in a server 101. It can be understood that the method can also be applied to a terminal 102.
[0072] Reference Figure 2 , Figure 2 The flowchart of the method for generating and verifying PLC code based on a large language model and applied to a server provided in an embodiment of the present invention, the execution subject of the method can be any of the aforementioned computer devices (including a server or a terminal). Figure 2 , the method may include the following steps:
[0073] Generate multi-stage multi-modal input data, the multi-stage multi-modal input data is used to characterize user needs and formulate a system design plan according to the needs analysis results; the multi-stages include the needs analysis stage and the design stage of the complete engineering cycle; the multi-modal input includes P&ID diagrams and natural language;
[0074] Generate PLC code according to the multi-stage multi-modal input data;
[0075] Performing verification testing on the generated PLC code, wherein the verification testing includes code verification and compiler verification;
[0076] After completing the verification test of the PLC code, the PLC code is deployed.
[0077] In some embodiments, generating multi-stage multi-modal input data comprises the following steps:
[0078] For security personnel who only have one natural language description of their needs:
[0079] Configure the initial requirement acquisition prompt to obtain the natural language requirement description entered by the user;
[0080] Perform semantic supplementation through a large language model to form an enhanced semantic prompt, so that the large language model generates PLC code;
[0081] Before the natural language requirement description is input into the large language model, a search and match is performed in the standard knowledge base, the requirement description is vectorized using the Word2Vec model, and the cosine similarity between the standard knowledge base vector and the requirement description vector is calculated to determine whether they belong to the same requirement module. If the cosine similarity is higher than the set threshold, the new natural language requirement description is replaced with the description in the standard knowledge base.
[0082] In some embodiments, the generating of multi-stage multi-modal input data further includes the following steps:
[0083] For the case where the safety personnel have the design instructions:
[0084] Obtaining a P&ID drawing from the design specification;
[0085] Input the P&ID diagram and natural language requirement description into LLM, combine the P&ID diagram and natural language requirement description to form a complete and detailed requirement statement;
[0086] Through image recognition and natural language processing technology, the information of the P&ID diagram is understood and parsed, and the P&ID diagram is integrated with the natural language requirement description to form a code generation enhanced semantic prompt to generate PLC code.
[0087] In some embodiments, generating PLC code according to the multi-stage multi-modal input data comprises the following steps:
[0088] Enhance the domain knowledge of LLM;
[0089] The multi-stage multi-modal input data is input into the LLM large language model to generate PLC code.
[0090] In some embodiments, the enhancing the domain knowledge of the LLM comprises the following steps:
[0091] Build a normative knowledge base, which includes knowledge in the field of industrial control systems, including PLC programming specifications, common control logic, standardized modules and actual cases;
[0092] Similar description content retrieval: After the user inputs natural language and P&ID diagrams, the model will search for content similar to the input in the specification knowledge base and provide it to LLM for reference;
[0093] Insert the retrieved relevant knowledge into the generation process of LLM to ensure that the model can use this expertise for code generation;
[0094] The step of inputting the multi-stage multi-modal input data into the LLM large language model to generate PLC code comprises the following steps:
[0095] Before generating the code, the code block repository is retrieved first. The code block repository contains verified PLC code modules. The large language model infers whether the requirement description has similar content based on the code block repository. If so, the code in the code block repository is integrated into the generated code.
[0096] Next, the large language model searches the canonical knowledge base to improve the generated code;
[0097] When generating code, the large language model will prioritize searching the code blocks that match the input requirements in the code block repository to ensure the reliability and reusability of the generated code; if the requirements are insufficient, the large language model can directly call the code blocks in the code block repository to reduce the time and potential errors in generating new code.
[0098] In some embodiments, the verification test of the generated PLC code comprises the following steps:
[0099] Syntax check: Perform syntax check on the generated PLC code to ensure that the code complies with the syntax rules of the PLC programming language. Use automated tools to check the code line by line to find and correct syntax errors.
[0100] Logic verification: After passing the syntax check, further logic verification is performed, using simulation tools and automated testing frameworks to perform logic analysis and simulation on the code to verify whether the code functions meet the design requirements; the logic verification includes testing of control flow, data processing, and interface interaction to ensure that the generated code logic is correct and has no loopholes;
[0101] Compilation test: Input the code that has passed the logic verification into the PLC compiler for compilation. The compiler will check whether the structure and data type of the code are correct. If there is an error, the compiler will provide detailed error information. After the compilation is passed, an executable file will be generated;
[0102] Execution test: After compilation, the generated code is deployed to the simulation PLC for execution test. Through simulation operation, the performance of the code in actual operation is detected to ensure that there are no errors and unexpected behaviors during operation.
[0103] In some embodiments, after completing the verification test of the PLC code, deploying the PLC code comprises the following steps:
[0104] Build a simulated PLC environment and select appropriate deployment tools, perform a final check on the code and prepare configuration files;
[0105] Upload the code to the simulated PLC environment, apply the configuration file, start the code running, and use the tool for real-time monitoring and logging;
[0106] Conduct real-time system monitoring and troubleshooting, perform performance evaluation and optimization, and regularly update and upgrade the code.
[0107] The specific implementation process of the present invention is described in detail below in conjunction with the accompanying drawings:
[0108] First, there are the following problems in the prior art:
[0109] (1) LLM lacks professional domain knowledge and the code design does not consider the complete engineering life cycle. Specifically, LLM lacks an in-depth understanding of industrial control system expertise and cannot accurately reflect actual control requirements and logic. The generated code may contain logical errors or fail to meet engineering specifications. In addition, the code design does not fully consider all stages of the engineering life cycle, including requirements analysis, design, implementation, testing, deployment, and maintenance, resulting in complex, inefficient, and difficult-to-understand generated code. This may result in the generated code not being able to accurately reflect actual control requirements and logic, affecting the accuracy and maintainability of the code.
[0110] (2) Insufficient consideration of the relationship between code modules in complex industrial control scenarios. Industrial control systems usually involve complex control logic and multiple interdependent modules. Existing methods may fail to fully consider the relationship between these modules, resulting in the generated code not being able to work together properly in actual applications, increasing the complexity of the system and the possibility of errors.
[0111] (3) There is a barrier between large language models and PLC code compiler software. The code generated by LLM needs to be compiled and deployed by PLC compiler software, but existing methods may fail to effectively integrate these two links. The compiler software may not fully understand or support the code generated by LLM, resulting in compilation errors or failure to execute correctly. The lack of an effective feedback mechanism makes it difficult to promptly feedback problems found during code generation, compilation, and deployment to LLM, thus affecting the iterative optimization of the code.
[0112] Therefore, in the context of network security researchers being unfamiliar with PLC codes and building industrial control target ranges, the purpose of the present invention is to propose a PLC code generation and verification method based on a large language model, which includes:
[0113] 1. Starting from the complete engineering life cycle, make the generated PLC code simple, effective and easy to understand.
[0114] 2. Fully consider the relationship between different code modules so that the generated PLC codes can correctly call each other and reduce manual inspection time.
[0115] 3. Verify the correctness of the generated code and automatically deploy it to ensure that the code meets task requirements and lower the deployment threshold.
[0116] The technical problems to be solved by the present invention may include:
[0117] (1) Knowledge enhancement methods for closed-source large language models and design of industrial control domain models based on open-source large language models. Since closed-source large language models (such as GPT-4) cannot be adjusted through fine-tuning methods such as low-rank adaptation (LoRA), it is necessary to explore solutions to make them suitable for PLC code generation tasks under the complete engineering life cycle. At the same time, based on the existing open-source large language models, explore the use of fine-tuning technology to build professional models suitable for PLC code generation. By training the open-source model with domain-specific data sets and optimizing its generation logic, it can more accurately generate PLC code that meets actual needs, thereby improving the practicality and reliability of the model.
[0118] (2) The calling relationship between modules in the PLC code and the realization of code reusability. Ensure modularity, low coupling and high reusability through detailed design and planning of the calling relationship between modules in the PLC code. Explore how to divide complex control logic into independent functional modules; in the code generation process, how to clarify the interface and interaction relationship of the modules to ensure that the calling logic between modules is clear and does not interfere with each other.
[0119] (3) The generated code must meet actual requirements, and perform correctness analysis and automated deployment to facilitate security researchers to build industrial control test sites. In order to make the generated PLC code meet actual control requirements, a multi-level correctness analysis and automated verification process needs to be introduced after code generation. For the generated code, how to establish an automated deployment mechanism so that the code can be quickly and safely deployed to the actual industrial control environment, reducing the time for manual debugging and deployment. This makes it easier for network security researchers to quickly build industrial control test sites, improving work efficiency and code reliability.
[0120] In summary, the present invention uses a large language model for the generation of PLC code, such as Figure 3 As shown in the figure, the framework of the PLC code generation and verification method based on the large language model is divided into four parts, namely:
[0121] 1. Multi-stage and multi-modal input part: The multi-stage includes the requirements analysis and design stages of the complete engineering cycle. The multi-modal input combines the P&ID diagram and natural language to improve the requirement statement.
[0122] 2.PLC code generation part: For the closed-source large language model, search and enhance the model knowledge, and fine-tune the open-source large language model and perform search enhancement to make the model's industrial control domain knowledge improve the accuracy of code generation. At the same time, add a code block repository. When facing the same functional requirements, the model will give priority to calling the code in the library to facilitate code reuse.
[0123] 3. Verification and testing phase: The code generated by the PLC is verified and tested through code verification methods and compiler compilation methods, and the results are fed back to the large language model until the correct code is generated.
[0124] 4. In the deployment phase, a deployment simulation executor is built to automatically deploy the generated code to the simulation PLC.
[0125] The present invention aims to provide a method for network security researchers to quickly construct PLC code. The specific implementation of the PLC code generation and verification method based on the large language model is divided into the following four steps.
[0126] Step 1: Multi-stage multi-modal input.
[0127] In the framework of the PLC code generation and verification method based on the large language model, the multi-level multi-modal input part is the starting point and key link of the entire system, ensuring that the generated PLC code can accurately reflect the needs of users. The demand analysis is the primary stage of the engineering project, and its purpose is to clarify the goals, functions and performance requirements of the project. The design stage is to formulate a detailed design plan for the system based on the results of the demand analysis. The present invention organically combines the two stages to give full play to the reasoning and semantic completion capabilities of the large language model. There are two specific situations. The first situation is that the security personnel only have a natural language requirement description. Because network security researchers may not be familiar with the specific requirements, it is necessary to set an initial requirement prompt1 to prompt the user to enter the corresponding content, and let the large language model perform semantic supplementation to form enhanced semantic prompt2, and then let the large language model generate PLC code; the second situation is that the security personnel have a design specification. Generally speaking, the design specification will contain a P&ID diagram. The P&ID diagram is an important icon tool in the industrial control system. It shows the pipelines, equipment, instruments and control logic in the system in detail. After inputting the P&ID diagram and requirement description to the LLM, the LLM combines the P&ID diagram with the natural language description to form a complete and detailed requirement statement. Through image recognition and natural language processing technology, the model can understand and parse the information of the P&ID image, and integrate it with the natural language processing language description to form a code generation enhanced semantic prompt3. This multi-modal input can enable the large language model to better understand the relationship between the upper and lower code blocks, thereby ensuring the correct call between the code blocks.
[0128] Before entering the large model, the requirement descriptions in the above two cases must be further searched and matched in the specification knowledge base. The Word2Vec model is used to vectorize the requirement descriptions, and the cosine similarity between the specification knowledge base vector and the requirement description vector is calculated to determine whether they belong to the same requirement module. If the cosine similarity is higher than the threshold set by this method, the description in the specification is replaced.
[0129] Step 2: PLC code generation.
[0130] Taking the generation of ST language as an example, the specification is IEC 61131-3 standard.
[0131] 2.1. LLM domain knowledge enhancement
[0132] For closed-source LLMs, such as GPT-4, the model cannot be directly adjusted through methods such as LoRA. Therefore, a retrieval enhancement method is used to enrich the industrial control field knowledge of closed-source LLMs. First, it is necessary to build a standard knowledge base: establish a standard knowledge base containing industrial control system field knowledge, including PLC programming specifications, common control logic, standardized modules and actual cases, etc. The present invention encodes the IEC 61131-3 specification into a vector knowledge base. Then there is similar description content retrieval: after the user enters natural language and P&ID diagrams, the model will retrieve content similar to the input in the standard knowledge base and provide it to the LLM for reference. Langchain can be cited to implement this part. In this way, closed-source LLM can combine the retrieved expertise when generating code to improve the accuracy and professionalism of the code. Finally, insertion and supplementation: insert the retrieved relevant knowledge into the generation process of LLM to ensure that the model can use this expertise for code generation. This method enhances the knowledge ability of the model in the field of industrial control without changing the internal structure of the closed-source LLM.
[0133] For open source large language models, their ability to generate PLC code can be improved through a combination of fine-tuning and search enhancement. Based on existing open source large language models, such as L l ama 2, a large amount of data in the field of industrial control systems is used to fine-tune the model, such as the LoRA method, so that the model has the ability to generate professional PLC code. These data include PLC code examples, industrial control cases, and standardized programming templates. On this basis, standard retrieval enhancement is added to further enhance the code generation ability of the large language model.
[0134] 2.2. Code generation: retrieval enhancement and retrieval reuse.
[0135] After the first part is completed, the complete requirement is input into the large language model, such as Figure 4 As shown in the PLC code generation flow chart, the model will first retrieve the code block repository before generating code. The code block repository contains verified PLC code modules. The model infers whether the requirement description is similar to the code block repository based on the code block repository. If it is similar, the code in the repository will be integrated into the generated code to achieve the purpose of code block reusability; then the model will retrieve the specification knowledge base and further improve the generated code based on the reasoning ability of the large model. When generating code, the model will first search the repository for code blocks that match the input requirements to ensure the reliability and reusability of the generated code. If the requirements are insufficient, the model can directly call the code blocks in the repository to reduce the time and potential errors in generating new code and improve the reusability and efficiency of the code.
[0136] Step 3: Verification Test.
[0137] In order to ensure that the generated PLC code meets the actual requirements and does not have errors, the present invention adopts multi-level verification, which mainly includes two verification aspects: code verification and compiler verification.
[0138] The first is syntax checking: Syntax checking is performed on the generated PLC code to ensure that the code complies with the syntax rules of the PLC programming language (such as the ST language in the IEC 61131-3 standard). The code is checked line by line through automated tools to find and correct syntax errors and avoid compilation failures caused by syntax errors.
[0139] Then comes logic verification: After passing the syntax check, further logic verification is performed. Use simulation tools and automated testing frameworks to perform logic analysis and simulation on the code to verify whether the code functions meet the design requirements. Logic verification includes testing of control flow, data processing, and interface interaction to ensure that the generated code is logically correct and has no loopholes.
[0140] Next is the compilation test: the code that has passed the logic verification is input into the PLC compiler for compilation. The compiler will check whether the structure and data type of the code are correct. If there is an error, the compiler will provide detailed error information. After the compilation is passed, an executable file is generated.
[0141] Finally, the execution test: after the compilation is successful, the generated code is deployed to the simulation PLC for execution test. Through simulation operation, the performance of the code in actual operation is detected to ensure that there are no errors and unexpected behaviors during operation. The present invention takes OpenPLC as an example. OpenPLC generates .ST files while compiling the code, which is convenient for subsequent automated deployment.
[0142] Step 4: Deployment.
[0143] After the generation and verification of the PLC code is completed, deployment is a key step to ensure that the code can run correctly in the actual industrial control system. The deployment process not only includes the uploading and execution of the code, but also involves the monitoring and management of the code to ensure the stability and security of the system. Therefore, it is necessary to write a deployment simulation executor with a script to upload the file to the simulated PLC. Before starting the deployment, it is necessary to build a simulated PLC environment. The present invention uses tools such as OpenPLC to build a simulation environment. OpenPLC can directly upload and run .ST files.
[0144] The entire deployment process is as follows: First, build a simulated PLC environment and select a suitable deployment tool, perform a final check on the code and prepare the configuration file. Then, upload the code to the simulated PLC environment, apply the configuration file, start the code running, and use the tool for real-time monitoring and logging. Finally, perform real-time monitoring and troubleshooting of the system, perform performance evaluation and optimization, and regularly update and upgrade the code. Through these steps, it is ensured that the generated PLC code can run stably and reliably in the actual industrial control system, providing network security researchers with an efficient and accurate PLC code deployment method.
[0145] In summary, compared with the prior art, the PLC code generation method combined with the large language model of the present invention has the following beneficial effects:
[0146] 1. The present invention combines P&ID diagrams and natural language descriptions through a multi-stage multi-modal input section, thus improving the requirements analysis and design stages. This method not only makes the requirements description more complete and specific, but also makes the generated PLC code more precise in function and logic, reducing the possibility of human error.
[0147] 2. The present invention adopts the enhancement technology of large language model in the process of PLC code generation. By searching, enhancing and fine-tuning the large language model, it has the specialized knowledge in the field of industrial control, and adds a code block repository, so that the large language model fully considers the calling relationship and reusability between code blocks. The application of this technical means greatly improves the accuracy and effectiveness of the generated code, making the generated PLC code more in line with actual needs.
[0148] 3. The present invention introduces an automated verification process in the code verification and testing phase, and ensures the correctness and reliability of the generated PLC code through compiler compilation and code verification methods. This automated verification technology reduces the time of manual debugging, improves work efficiency, and ensures the stability and security of the code in practical applications. On this basis, the present invention also designs a deployment simulation executor that can automatically deploy the generated code to the simulated PLC. This technical means not only simplifies the deployment process, but this automated deployment technology greatly reduces the deployment threshold, allowing network security researchers to quickly build an industrial control target range and focus on the research and protection of security risks.
[0149] Another aspect of the embodiment of the present invention further provides a PLC code generation and verification device based on a large language model, comprising:
[0150] The first module is used to generate multi-stage multi-modal input data, wherein the multi-stage multi-modal input data is used to characterize user needs and formulate a system design plan according to the needs analysis results; the multi-stages include the needs analysis stage and the design stage of the complete engineering cycle; the multi-modal input includes P&ID diagrams and natural language;
[0151] A second module is used to generate PLC code according to the multi-stage multi-modal input data;
[0152] The third module is used to perform verification test on the generated PLC code, wherein the verification test includes code verification and compiler verification;
[0153] The fourth module is used to deploy the PLC code after completing the verification test of the PLC code.
[0154] It can be understood that the contents of the above method embodiments are all applicable to the present device embodiments, the functions specifically implemented by the present device embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.
[0155] The embodiment of the present invention further provides an electronic device, the electronic device includes a memory and a processor, the memory stores a computer program, and the processor implements the above-mentioned PLC code generation and verification method based on a large language model when executing the computer program. The electronic device can be any intelligent terminal including a tablet computer, a vehicle-mounted computer, etc.
[0156] It can be understood that the contents of the above method embodiments are all applicable to the present device embodiments, the functions specifically implemented by the present device embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.
[0157] See also Figure 5 , Figure 5 The hardware structure of an electronic device of another embodiment is illustrated, and the electronic device includes:
[0158] The processor 501 may be implemented by a general-purpose CPU (Central Processing Unit), a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of the present invention.
[0159] The memory 502 can be implemented in the form of a read-only memory (ROM), a static storage device, a dynamic storage device, or a random access memory (RAM). The memory 502 can store an operating system and other application programs. When the technical solution provided in the embodiment of this specification is implemented by software or firmware, the relevant program code is stored in the memory 502, and the processor 501 calls and executes the PLC code generation and verification method based on a large language model in the embodiment of the present invention;
[0160] Input / output interface 503, used to implement information input and output;
[0161] Communication interface 504, used to realize communication interaction between the device and other devices, which can be realized through wired mode (such as USB, network cable, etc.) or wireless mode (such as mobile network, WI FI, Bluetooth, etc.);
[0162] A bus 505 that transmits information between the various components of the device (e.g., the processor 501, the memory 502, the input / output interface 503, and the communication interface 504);
[0163] The processor 501 , the memory 502 , the input / output interface 503 and the communication interface 504 are connected to each other in communication within the device via the bus 505 .
[0164] An embodiment of the present invention further provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, the above-mentioned PLC code generation and verification method based on a large language model is implemented.
[0165] It can be understood that the contents of the above method embodiments are all applicable to the present storage medium embodiments, the functions specifically implemented by the present storage medium embodiments are the same as those of the above method embodiments, and the beneficial effects achieved are also the same as those achieved by the above method embodiments.
[0166] The memory, as a non-transient computer-readable storage medium, can be used to store non-transient software programs and non-transient computer executable programs. In addition, the memory may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some embodiments, the memory may optionally include a memory remotely disposed relative to the processor, and these remote memories may be connected to the processor via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0167] It should be noted that in various specific embodiments of the present invention, when it comes to the need to perform relevant processing based on data related to user identity or characteristics such as user information, user behavior data, user historical data, and user location information, the user's permission or consent will be obtained first, and the collection, use, and processing of these data will comply with relevant laws, regulations, and standards. In addition, when the embodiment of the present invention needs to obtain the user's sensitive personal information, it will obtain the user's separate permission or consent through a pop-up window or jump to a confirmation page, and after clearly obtaining the user's separate permission or consent, it will obtain the necessary user-related data for the normal operation of the embodiment of the present invention.
[0168] The embodiments described in the embodiments of the present invention are intended to more clearly illustrate the technical solutions of the embodiments of the present invention, and do not constitute a limitation on the technical solutions provided by the embodiments of the present invention. Those skilled in the art can appreciate that with the evolution of technology and the emergence of new application scenarios, the technical solutions provided by the embodiments of the present invention are also applicable to similar technical problems.
[0169] Those skilled in the art will appreciate that the technical solutions shown in the figures do not limit the embodiments of the present invention and may include more or fewer steps than shown in the figures, or a combination of certain steps, or different steps.
[0170] The device embodiments described above are merely illustrative, and the units described as separate components may or may not be physically separated, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0171] Those skilled in the art will appreciate that all or some of the steps in the methods disclosed above, and the functional modules / units in the systems and devices may be implemented as software, firmware, hardware, or a suitable combination thereof.
[0172] The terms "first", "second", "third", "fourth", etc. (if any) in the specification of the present invention and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0173] It should be understood that in the present invention, "at least one (item)" refers to one or more, and "plurality" refers to two or more. "And / or" is used to describe the association relationship of associated objects, indicating that three relationships may exist. For example, "A and / or B" can represent: only A exists, only B exists, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following items" or similar expressions refers to any combination of these items, including any combination of single items or plural items. For example, at least one of a, b or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, c can be single or multiple.
[0174] In the several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the above units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0175] The units described above as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0176] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0177] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium, including multiple instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the methods of various embodiments of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc., various media that can store programs.
[0178] The preferred embodiments of the present invention are described above with reference to the accompanying drawings, but the scope of the rights of the embodiments of the present invention is not limited thereby. Any modification, equivalent substitution and improvement made by those skilled in the art without departing from the scope and essence of the embodiments of the present invention shall be within the scope of the rights of the embodiments of the present invention.
Claims
1. A PLC code generation and verification method based on a large language model, characterized in that: The following steps are involved: Generate multi-stage multi-modal input data, the multi-stage multi-modal input data is used to characterize user needs and formulate a system design plan according to the needs analysis results; the multi-stages include the needs analysis stage and the design stage of the complete engineering cycle; the multi-modal input includes P&ID diagrams and natural language; Generate PLC code according to the multi-stage multi-modal input data; Performing verification testing on the generated PLC code, wherein the verification testing includes code verification and compiler verification; After completing the verification test of the PLC code, the PLC code is deployed.
2. A PLC code generation and verification method based on a large language model according to claim 1, characterized in that: The generating of multi-stage multi-modal input data comprises the following steps: For security personnel who only have one natural language description of their needs: Configure the initial requirement acquisition prompt to obtain the natural language requirement description entered by the user; Perform semantic supplementation through a large language model to form an enhanced semantic prompt, so that the large language model generates PLC code; Before the natural language requirement description is input into the large language model, a search and match is performed in the standard knowledge base, the requirement description is vectorized using the Word2Vec model, and the cosine similarity between the standard knowledge base vector and the requirement description vector is calculated to determine whether they belong to the same requirement module. If the cosine similarity is higher than the set threshold, the new natural language requirement description is replaced with the description in the standard knowledge base.
3. A PLC code generation and verification method based on a large language model according to claim 2, characterized in that: The generating of multi-stage multi-modal input data further comprises the following steps: For the case where the safety personnel have the design instructions: Obtaining a P&ID drawing from the design specification; Input the P&ID diagram and natural language requirement description into LLM, combine the P&ID diagram and natural language requirement description to form a complete and detailed requirement statement; Through image recognition and natural language processing technology, the information of the P&ID diagram is understood and parsed, and the P&ID diagram is integrated with the natural language requirement description to form a code generation enhanced semantic prompt to generate PLC code.
4. A PLC code generation and verification method based on a large language model according to claim 1, characterized in that: The step of generating PLC code according to the multi-stage multi-modal input data comprises the following steps: Enhance LLM’s domain knowledge; The multi-stage multi-modal input data is input into the LLM large language model to generate PLC code.
5. A PLC code generation and verification method based on a large language model according to claim 4, characterized in that: The enhanced LLM domain knowledge comprises the following steps: Build a normative knowledge base, which includes knowledge in the field of industrial control systems, including PLC programming specifications, common control logic, standardized modules and actual cases; Similar description content retrieval: After the user inputs natural language and P&ID diagrams, the model will search for content similar to the input in the specification knowledge base and provide it to LLM for reference; Insert the retrieved relevant knowledge into the generation process of LLM to ensure that the model can use this expertise for code generation; The step of inputting the multi-stage multi-modal input data into the LLM large language model to generate PLC code comprises the following steps: Before generating the code, the code block repository is retrieved first. The code block repository contains verified PLC code modules. The large language model infers whether the requirement description has similar content based on the code block repository. If so, the code in the code block repository is integrated into the generated code. Next, the large language model searches the canonical knowledge base to improve the generated code; When generating code, the large language model will prioritize searching the code blocks that match the input requirements in the code block repository to ensure the reliability and reusability of the generated code; if the requirements are insufficient, the large language model can directly call the code blocks in the code block repository to reduce the time and potential errors in generating new code.
6. A PLC code generation and verification method based on a large language model according to claim 1, characterized in that: The verification test of the generated PLC code comprises the following steps: Syntax check: Perform syntax check on the generated PLC code to ensure that the code complies with the syntax rules of the PLC programming language. Use automated tools to check the code line by line to find and correct syntax errors. Logic verification: After passing the syntax check, further logic verification is performed, using simulation tools and automated testing frameworks to perform logic analysis and simulation on the code to verify whether the code functions meet the design requirements; the logic verification includes testing of control flow, data processing, and interface interaction to ensure that the generated code logic is correct and has no loopholes; Compilation test: Input the code that has passed the logic verification into the PLC compiler for compilation. The compiler will check whether the structure and data type of the code are correct. If there is an error, the compiler will provide detailed error information. After the compilation is passed, an executable file will be generated; Execution test: After compilation, the generated code is deployed to the simulation PLC for execution test. Through simulation operation, the performance of the code in actual operation is detected to ensure that there are no errors and unexpected behaviors during operation.
7. A PLC code generation and verification method based on a large language model according to claim 1, characterized in that: After completing the verification test of the PLC code, deploying the PLC code comprises the following steps: Build a simulated PLC environment and select appropriate deployment tools, perform a final check on the code and prepare configuration files; Upload the code to the simulated PLC environment, apply the configuration file, start the code running, and use the tool for real-time monitoring and logging; Conduct real-time system monitoring and troubleshooting, perform performance evaluation and optimization, and regularly update and upgrade the code.
8. A PLC code generation and verification device based on a large language model, characterized in that: include: The first module is used to generate multi-stage multi-modal input data, wherein the multi-stage multi-modal input data is used to characterize user needs and formulate a system design plan according to the needs analysis results; The multi-stages include the demand analysis stage and the design stage of the complete engineering cycle; the multi-modal input includes P&ID diagrams and natural language; A second module is used to generate PLC code according to the multi-stage multi-modal input data; The third module is used to perform verification test on the generated PLC code, wherein the verification test includes code verification and compiler verification; The fourth module is used to deploy the PLC code after completing the verification test of the PLC code.
9. An electronic device, characterized in that: including a processor and a memory; The memory is used to store programs; The processor executes the program to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that: The storage medium stores a program, and the program is executed by a processor to implement the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Model-based PLC control code generation method, system and device and storage medium
CN113534730A
Emotion recognition method, system and device based on multi-modal feature fusion and medium
CN113807249A
Code construction method, model fine tuning method, equipment and storage medium
CN117369796A
Text2SQL semantic parsing method for domain large language model
CN118377796A
Computer-implemented verification of a hardware design implementation against a natural language description of the hardware design or software code against a natural language description of a software application
US20230148108A1
Cited By
Code generation optimization method based on adaptive planning framework
CN120085846A
Extensible structured text generation method and device in PLC field, equipment and medium
CN120085869A
Automatic control code generation and verification method and device, equipment and storage medium
CN120406914A
Automation control code generation and verification method, device and equipment and storage medium
CN120406914B
Multi-agent-based programmable logic controller structured text generation method
CN120450033A