A PLC code generation and verification method based on a large language model
By employing a multi-stage, multi-modal input and automated verification process, the problems of low complexity and accuracy in PLC code generation are solved, enabling efficient and accurate PLC code generation and deployment, and improving the work efficiency of cybersecurity researchers.
Patent Information
- Application Number
- CN202411144236.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-20
- Publication Date
- 2026-02-06
- Estimated Expiration
- 2044-08-20
AI Technical Summary
Existing technologies generate complex, inefficient, and difficult-to-understand PLC code with low accuracy, high coupling, and low reusability. The degree of automated deployment and verification is insufficient, which affects the work efficiency of cybersecurity researchers and the reliability of the code.
PLC code is generated using multi-stage, multi-modal input data, including P&ID diagrams and natural language, to enhance the domain knowledge of the large language model. Code generation is optimized through a standardized knowledge base and code block repository. An automated verification process is introduced, including syntax checking, logic verification, compilation testing, and execution testing, and finally, automated deployment is performed.
The generated PLC code is highly accurate, modularly designed for easy understanding and maintenance, highly automated, reduces manual inspection time, improves code reliability and reusability, and simplifies the deployment process.
Smart Images

Figure CN119201059B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of computer technology, and particularly relates to a PLC code generation and verification method based on a large language model. BACKGROUND
[0002] In industrial automation, programmable logic controllers (PLC) are the core devices used to control machines and processes. With the increasing networking of industrial control systems (ICS), the security of industrial control systems has become increasingly prominent. An industrial control system range is a facility dedicated to testing, practicing, and researching the network security of industrial control systems. It helps enterprises and professionals improve their understanding of security risks and protection capabilities for industrial control systems by simulating real industrial control system environments. In order to test and verify the security of industrial control systems, network security researchers often need to build industrial control simulation scenarios. However, since PLC programming involves specific programming languages and logic, it is a complex and time-consuming task for network security researchers who are not familiar with PLC code to write and debug PLC code. Traditional PLC programming methods usually rely on manual coding and debugging, which is not only inefficient but also prone to human error.
[0003] With the development of artificial intelligence technology, large language models (LLM) have shown strong capabilities in natural language processing. LLM can understand natural language descriptions and generate complex code logic text through reasoning based on the descriptions. Using a large language model to automatically generate PLC code and perform verification will greatly improve the efficiency of network security researchers and the reliability of PLC code, allowing them to focus more on discovering and solving security problems in industrial control systems.
[0004] Currently, there are some methods that utilize large language models to generate PLC code. 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 utilizing user feedback and external verification tools to guide the large language model 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 IEC 61131-3 standards; in addition, user feedback and external verification tools (including syntax checkers, compilers, and SMV verifiers) can perform multi-level verification and optimization on the PLC code generated by the large language model to ensure the correctness and effectiveness of the generated code.
[0005] The existing methods of generating PLC code using large language models still have some deficiencies, mainly in the following aspects:
[0006] (1) The generated PLC code is complex, inefficient, and difficult to understand. The code generated by LLM often contains excessive logic and steps, making code reading and maintenance difficult. This complexity not only increases the execution time of the code, reducing 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 existing methods may have logical errors or not meet 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. Lack of modular design further reduces the maintainability and scalability of the code, increasing the difficulty of later modification and upgrade.
[0008] (3) The generated PLC code has low degree of automation in deployment and verification. Although LLM can generate code, existing methods still lack in the automation of code generation, deployment, and verification. The lack of effective automated testing and verification processes requires manual testing and debugging of the generated code, increasing the workload and potentially introducing human errors. At the same time, the imperfection of verification tools and methods also limits the comprehensive checking of code correctness and security. SUMMARY
[0009] The main purpose of the embodiments of the present application is to provide a PLC code generation and verification method based on large language models with high accuracy and high automation.
[0010] To achieve the above objectives, one aspect of this invention proposes a PLC code generation and verification method based on a large language model, comprising the following steps:
[0011] Multi-stage multimodal input data is generated, which is used to characterize user needs and formulate system design schemes based on the results of needs analysis; the multi-stages include the needs analysis stage and the design stage of the complete engineering cycle; the multimodal inputs include P&ID diagrams and natural language.
[0012] Based on the multi-stage, multi-modal input data, PLC code is generated;
[0013] The generated PLC code is subjected to verification tests, including 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 multimodal input data includes the following steps:
[0016] For situations where security personnel only have a single natural language description of their requirements:
[0017] Configure initial requirements to obtain a prompt, which will be used to obtain a natural language description of the user's requirements.
[0018] A semantically enhanced prompt is formed by semantic supplementation using a large language model, so that the large language model can generate PLC code.
[0019] Before inputting the natural language requirement description into the large language model, a search and matching process 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 a set threshold, the new natural language requirement description is replaced with the description in the standard knowledge base.
[0020] In some embodiments, generating multi-stage multimodal input data further includes the following steps:
[0021] Regarding situations where safety personnel possess design specifications:
[0022] Obtain the P&ID diagram from the aforementioned design specification;
[0023] Input the P&ID diagram and natural language requirement description into the LLM, and 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 techniques, 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, the generating PLC code according to the multi-stage multi-modal input data comprises the following steps:
[0026] Enhancing the domain knowledge of the LLM;
[0027] Inputting the multi-stage multi-modal input data 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] Building a specification knowledge base to establish a specification knowledge base containing industrial control system domain knowledge, 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 diagram, the model retrieves similar content in the specification knowledge base and provides it to the LLM for reference;
[0031] Inserting the retrieved relevant knowledge into the generation process of the LLM to ensure that the model can utilize these professional knowledge for code generation;
[0032] The 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 containing verified PLC code modules will be retrieved, and the large language model will infer whether there is similar content in the requirement description according to the code block repository, and if there is similar content, the code in the code block repository will be integrated into the generated code;
[0034] Then, the large language model will retrieve the specification knowledge base to perfect the generated code;
[0035] When generating the code, the large language model will preferentially search for code blocks in the code block repository that match the input requirements to ensure the reliability of the generated code and reusability; 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 of generating new code.
[0036] In some embodiments, the verifying and testing the generated PLC code comprises the following steps:
[0037] Syntax checking: Perform syntax checking on the generated PLC code to ensure that the code complies with the syntax rules of the PLC programming language. Automatically 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. Use simulation tools and automated test frameworks to analyze and simulate the code to verify that the code functions as designed. The logic verification includes testing of control flow, data processing, and interface interaction to ensure that the generated code is logically correct and free of vulnerabilities.
[0039] Compilation testing: Input the code that has passed the logic verification into the PLC compiler for compilation. The compiler will check the structure and data types of the code. If there are errors, the compiler will provide detailed error information. After successful compilation, an executable file is generated.
[0040] Execution testing: After successful compilation, deploy the generated code to a simulation PLC for execution testing. Through simulation running, detect the performance of the code in actual running to ensure that there are no errors and unexpected behaviors during running.
[0041] In some embodiments, after completing the verification test of the PLC code, deploying the PLC code includes the following steps:
[0042] Build a simulation PLC environment and select appropriate deployment tools to perform final checks and prepare configuration files for the code.
[0043] Upload the code to the simulation PLC environment, apply the configuration file, start the code running, and perform real-time monitoring and log recording through the tool.
[0044] Perform real-time monitoring and fault handling of the system, perform performance evaluation and optimization, and regularly update and upgrade the code.
[0045] Another aspect of the embodiment of the application also provides a PLC code generation and verification device based on a large language model, comprising:
[0046] A first module for generating multi-stage multi-modal input data, the multi-stage multi-modal input data being used to represent user requirements and to develop system design schemes based on demand analysis results; the multi-stage including a demand analysis stage and a design stage of a complete engineering cycle; the multi-modal input including P&ID diagrams and natural language;
[0047] A second module for generating PLC code based on the multi-stage multi-modal input data;
[0048] a third module configured to perform a verification test on the generated PLC code, the verification test including code verification and compiler verification;
[0049] a fourth module configured to deploy the PLC code after the verification test on the PLC code is completed.
[0050] Another aspect of the embodiments of the present application further provides an electronic device, including a processor and a memory;
[0051] The memory is configured to store a program.
[0052] The processor executes the program to implement the method as described above.
[0053] Another aspect of the embodiments of the present application further provides a computer readable storage medium, the storage medium stores a program, and the program is executed by a processor to implement the method as described above.
[0054] The embodiments of the present application further disclose a computer program product or a computer program, which comprises computer instructions stored in a computer readable storage medium. A processor of a computer device can read the computer instructions from the computer readable storage medium, and the processor executes the computer instructions to enable the computer device to perform the method as described above.
[0055] The embodiments of the present application at least have the following beneficial effects: the present application provides a PLC code generation and verification method based on a large language model, which generates multi-stage multi-modal input data, the multi-stage multi-modal input data is used to represent user demand and formulate a system design scheme according to demand analysis results; the multi-stage includes a demand analysis stage and a design stage in a complete engineering cycle; the multi-modal input includes a P&ID diagram and natural language; then, the PLC code is generated according to the multi-stage multi-modal input data; then, the generated PLC code is tested, the verification test includes code verification and compiler verification; finally, after the verification test on the PLC code is completed, the PLC code is deployed. The embodiments of the present application have high accuracy and high automation, and can provide a network security researcher with an efficient and accurate PLC code deployment method. BRIEF DESCRIPTION OF DRAWINGS
[0056] Figure 1 is an implementation environment schematic diagram provided by the embodiments of the present application;
[0057] Figure 2 is a flowchart of the overall steps provided by the embodiments of the present application;
[0058] Figure 3 is an overall framework schematic diagram provided by the embodiments of the present application;
[0059] Figure 4 is a PLC code generation flowchart provided by the embodiment of the present application;
[0060] Figure 5 is a hardware structure schematic diagram of an electronic device provided by the embodiment of the present application. DETAILED DESCRIPTION
[0061] In order to make the objects, 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 and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not intended to limit the present application. When the following description refers to the accompanying drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementation described in the following exemplary embodiments does not represent all the implementations consistent with the embodiments of the present application, and they are only examples of devices and methods consistent with some aspects of the embodiments of the present application as described in the appended claims.
[0062] It can be understood that the terms "first", "second", and the like used in the present application can be used herein to describe various concepts, but unless specifically stated, 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 application, the first information can also be referred to as the second information, and similarly, the second information can also be referred to as the first information. Depending on the context, the word "if" as used herein can be interpreted as "when" or "when" or "in response to determining".
[0063] The terms "at least one", "multiple", "each", "any" and the like used in the present application include one, two or more than two, multiple includes two or more than two, 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 understood by those skilled in the art to which the present application belongs. The terms used herein are only for the purpose of describing the embodiments of the present application and are not intended to limit the present application.
[0065] The PLC code generation and verification method based on the large language model provided by the embodiment of the present application relates to the technical field of computers. The PLC code generation and verification method based on the large language model provided by the embodiment of the present application can be applied to a terminal, can also be applied to a server, and can also be software running in the terminal or the server. In some embodiments, the terminal can be a smart phone, a tablet computer, a notebook computer, a desktop computer, a smart speaker, a smart watch, a vehicle-mounted terminal, and the like, but is not limited thereto; the server end can be configured as a stand-alone physical server, can also be configured as a server cluster or a distributed system formed by multiple physical servers, can also be configured as a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDNs, and big data and artificial intelligence platforms, and the server can also be a node server in a blockchain network; the software can be an application for implementing the PLC code generation and verification method based on the large language model, and the like, but is not limited to the above forms.
[0066] The present application 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 electronics, network PCs, minicomputers, mainframe computers, distributed computing environments including any of the above systems or devices, and the like. The present application 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, and the like that perform specific tasks or implement specific abstract data types. The present application can also be practiced in a distributed computing environment in which tasks are performed by remote processing devices connected by a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media, including storage devices.
[0067] As shown in Figure 1 , it is an implementation environment schematic diagram provided by the embodiment of the present application. Referring to 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 by wireless or wired means for network connection to complete data transmission and exchange.
[0068] The server 101 can be a standalone physical server, a server cluster composed of multiple physical servers, or a distributed system, and can also be a cloud server providing cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and basic cloud computing services such as big data and artificial intelligence platforms.
[0069] In addition, the server 101 can also be a node server in a blockchain network. The blockchain is a new application mode of distributed data storage, peer-to-peer transmission, consensus mechanism, and encryption algorithm.
[0070] The terminal 102 can be a smartphone, a tablet computer, a notebook computer, a desktop computer, a smart speaker, a smart watch, and the like. The terminal 102 can also be a vehicle terminal of the various device types exemplified above, but is not limited thereto. The terminal 102 and the server 101 can be connected directly or indirectly through wired or wireless communication, and the present embodiment is not limited thereto.
[0071] Exemplarily based on the implementation environment shown, Figure 1 The present embodiment provides a PLC code generation and verification method based on a large language model. The method can be applied to the server 101, and can also be applied to the terminal 102.
[0072] Referring to Figure 2 , Figure 2 The flowchart of the PLC code generation and verification method based on a large language model applied to the server provided by the present embodiment can be executed by any of the computer devices (including servers or terminals) described above. Referring to Figure 2 , the method can include the following steps:
[0073] Generating multi-stage multi-modal input data, wherein the multi-stage multi-modal input data is used to represent user requirements and to develop a system design scheme according to the demand analysis results; the multi-stage includes a demand analysis stage and a design stage of a complete engineering cycle; the multi-modal input includes a P&ID diagram and natural language;
[0074] Generating PLC code according to the multi-stage multi-modal input data;
[0075] Verifying the generated PLC code, wherein the verification test includes code verification and compiler verification;
[0076] After completing the verification test on the PLC code, the PLC code is deployed.
[0077] In some embodiments, the generating multi-stage multi-modal input data comprises the following steps:
[0078] For the case where the security personnel only has a natural language requirement description:
[0079] Configure an initial requirement acquisition prompt to obtain a user-input natural language requirement description;
[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] Wherein, before inputting the natural language requirement description into the large language model, search and match in the specification knowledge base, use a Word2Vec model to vectorize the requirement description, calculate the cosine similarity between the specification knowledge base vector and the requirement description vector, to determine the same requirement module, if the cosine similarity is higher than the set threshold, replace the new natural language requirement description with the description in the specification knowledge base.
[0082] In some embodiments, the generating multi-stage multi-modal input data further comprises the following steps:
[0083] For the case where the security personnel holds a design specification:
[0084] Obtain a P&ID diagram from the design specification;
[0085] Input the P&ID diagram and the natural language requirement description into the LLM, combine the P&ID diagram and the natural language requirement description to form a complete and detailed requirement statement;
[0086] Understand and analyze the information of the P&ID diagram through image recognition and natural language processing technology, and integrate the P&ID diagram with the natural language requirement description to form a code generation enhanced semantic prompt to generate PLC code.
[0087] In some embodiments, the generating PLC code according to the multi-stage multi-modal input data comprises the following steps:
[0088] Enhance the domain knowledge of the LLM;
[0089] Input the multi-stage multi-modal input data 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] Building a specification knowledge base, establishing a specification knowledge base containing knowledge in the field of industrial control systems, including PLC programming specifications, common control logic, standardized modules and practical cases;
[0092] Similar description content retrieval: after the user inputs natural language and P&ID diagrams, the model retrieves similar content in the specification knowledge base and provides it to the LLM for reference;
[0093] Inserting the retrieved relevant knowledge into the generation process of the LLM ensures that the model can utilize this professional knowledge for code generation;
[0094] The method for generating PLC code by inputting the multi-stage multi-modal input data into the LLM large language model includes the following steps:
[0095] Before generating the code, the code block repository will be retrieved, which contains verified PLC code modules. The large language model infers whether there is similar content in the requirement description based on the code block repository. If there is similar content, the code in the code block repository will be integrated into the generated code;
[0096] Then, the large language model retrieves the specification knowledge base to perfect the generated code;
[0097] When generating the code, the large language model will preferentially search the code block repository for code blocks that match the input requirements, ensuring 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, reducing the time and potential errors of generating new code.
[0098] In some embodiments, the verification and testing of the generated PLC code includes the following steps:
[0099] Syntax checking: the generated PLC code is checked for syntax errors to ensure that the code complies with the syntax rules of the PLC programming language. Automatic tools are used 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. Simulation tools and automated testing frameworks are used to analyze and simulate the code to verify whether the code functions as designed. The logic verification includes testing of control flow, data processing and interface interaction to ensure that the generated code is logically correct and free of vulnerabilities;
[0101] Compilation testing: the code that passes the logic verification is input into the PLC compiler for compilation. The compiler checks the structure and data types of the code. If there are errors, the compiler provides detailed error information. After compilation, an executable file is generated;
[0102] Test execution: After the compilation is passed, the generated code is deployed to the simulation PLC for test execution. Through simulation running, the performance of the code in actual running is detected to ensure that there is no error and unexpected behavior during running.
[0103] In some embodiments, the deploying the PLC code after completing the verification test of the PLC code comprises the following steps:
[0104] Build a simulation PLC environment and select appropriate deployment tools to perform final checks on the code and prepare configuration files.
[0105] Upload the code to the simulation PLC environment, apply the configuration file, start the code running, and perform real-time monitoring and log recording through the tool.
[0106] Perform real-time monitoring and fault handling of the system, perform performance evaluation and optimization, and regularly update and upgrade the code.
[0107] The specific implementation process of the present application will be described in detail below in conjunction with the accompanying drawings of the specification:
[0108] Firstly, the following problems exist in the prior art:
[0109] (1) The general large language model lacks professional domain knowledge and the code design does not consider the complete engineering life cycle. Specifically, the LLM lacks a deep understanding of industrial control system professional knowledge and cannot accurately reflect the actual control requirements and logic. The generated code may contain logical errors or not conform to engineering specifications. In addition, the code design does not fully consider the various stages of the engineering life cycle, including requirement analysis, design, implementation, testing, deployment and maintenance, resulting in complex and inefficient generated code that is difficult to understand, which may affect the accuracy and maintainability of the generated code.
[0110] (2) The relationship between code modules in complex industrial control scenarios is not fully considered. Industrial control systems usually involve complex control logic and multiple interdependent modules. Existing methods may not fully consider the relationship between these modules, resulting in generated code that cannot work properly in actual applications, increasing the complexity and error probability of the system.
[0111] (3) Large language models and PLC code compilation software exist barriers. The code generated by LLM needs to be compiled and deployed through PLC compilation software, but existing methods may fail to effectively integrate these two aspects. The compilation software may not fully understand or support the code generated by LLM, resulting in compilation errors or incorrect execution. Lack of effective feedback mechanism makes it difficult to find problems in the code generation, compilation and deployment process and feedback to LLM in time, thus affecting the iterative optimization of the code.
[0112] Therefore, in the background that network security researchers are not familiar with PLC code and industrial control target field construction, in view of the problems of complex, low accuracy and lack of automatic verification of PLC code generated by large language model, the purpose of the present application is to propose a PLC code generation and verification method based on large language model, which comprises:
[0113] 1. From the complete engineering life cycle, the generated PLC code is simple, effective and easy to understand.
[0114] 2. Fully consider the relationship between different code modules, so that the generated PLC code can be correctly called, and the manual checking time is reduced.
[0115] 3. Correctness verification and automatic deployment of generated code to make the code meet the task requirements and reduce the deployment threshold.
[0116] The technical problems to be solved by the present application can include:
[0117] (1) Closed-source large language model knowledge enhancement method and industrial control field model design based on open-source large language model. Since the closed-source large language model (such as GPT-4) cannot be adjusted by fine-tuning methods such as low-rank adaptation (LoRA), it is necessary to explore solutions to make it suitable for PLC code generation tasks under the complete engineering life cycle. At the same time, based on the existing open-source large language model, explore the use of fine-tuning technology to build a professional model suitable for PLC code generation. Through training of the open-source model on a domain-specific dataset, optimize its generation logic to make it 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 PLC code and the reusability of the code. By designing and planning the calling relationship between modules in PLC code in detail, modularization, low coupling and high reusability are ensured. Explore how to divide complex control logic into independent functional modules; in the code generation process, how to clearly define the interface and interaction relationship of the module 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 undergo correctness analysis and automated deployment to facilitate security researchers in building industrial control testbeds. To ensure the generated PLC code meets actual control needs, a multi-layered correctness analysis and automated verification process needs to be introduced after code generation. For the generated code, an automated deployment mechanism has been established to enable rapid and secure deployment to the actual industrial control environment, reducing manual debugging and deployment time. This facilitates network security researchers in quickly building industrial control testbeds, improving work efficiency and code reliability.
[0120] In summary, this invention uses a large language model for PLC code generation, such as... Figure 3 As shown, the PLC code generation and verification method framework based on a large language model consists of four parts:
[0121] 1. Multi-stage multimodal input section: Multi-stage includes the requirements analysis and design phases of the complete engineering cycle. Multimodal input combines P&ID diagrams and natural language to refine the requirements statements.
[0122] 2. PLC code generation section: For closed-source large language models, we perform retrieval enhancement to enrich the model knowledge. For open-source large language models, we fine-tune and enhance them with retrieval to improve the model's knowledge of industrial control domains and improve the accuracy of code generation. At the same time, we add a code block repository. When facing the same functional requirements, the model will prioritize calling the code in the repository to facilitate code reuse.
[0123] 3. Verification and Testing Phase: The code generated by the PLC is verified and tested using 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] This invention aims to provide cybersecurity researchers with a method for rapidly constructing PLC code. The specific implementation of the PLC code generation and verification method based on a large language model consists of the following four steps.
[0126] Step 1: Multi-stage multimodal input.
[0127] In the PLC code generation and verification method framework based on large language model, the multi-level multi-modal input part is the starting point and key link of the whole system, ensures that the generated PLC code can accurately reflect the user's demand, demand analysis is the primary stage of engineering project, the purpose is to clarify the target, function and performance requirements of the project, the design stage is to develop the detailed design scheme of the system according to the demand analysis result, the present application is to organically combine the two stages, and fully exert the reasoning and semantic completion ability of the large language model. Specifically, there are two cases, the first case is that the safety personnel only have a natural language demand description, because for network security researchers, they may not be familiar with the specific requirements, therefore, an initial demand prompt1 is set, prompting the user to input the corresponding content, and the large language model is used to form an enhanced semantic prompt2, and then the large language model is used to generate PLC code; The second case is that the safety personnel hold a design specification, generally speaking, the design specification will contain P&ID diagram, P&ID diagram is an important icon tool in industrial control system, which details the pipes, equipment, instruments and control logic in the system, after inputting P&ID diagram and demand description to LLM, LLM combines P&ID diagram and natural language description to form a complete and detailed demand sentence. Through image recognition and natural language processing technology, the model can understand and analyze the information of P&ID image, and integrate it with natural language processing language description to form a code generation enhanced semantic prompt3. This multi-modal input can make the large language model better understand the relationship between the upper and lower code blocks, so as to ensure the correct calling between the code blocks.
[0128] The demand descriptions of the above two cases are further searched and matched in the specification knowledge base before being input into the large model, the demand description is vectorized using Word2Vec model, the cosine similarity of the vector of the specification knowledge base and the vector of the demand description is calculated, to determine whether they belong to the same demand module, if the cosine similarity is higher than the threshold value set by the 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 field knowledge enhancement
[0132] For closed-source LLMs like GPT-4, it is not possible to directly adjust the model through methods such as LoRA. Therefore, a retrieval-enhanced method is adopted to enrich the closed-source LLM's industrial control field knowledge. First, a specification knowledge base needs to be built: a specification knowledge base containing industrial control system field knowledge is established, including PLC programming specifications, common control logic, standardized modules, and actual cases, etc. The IEC 61131-3 specification is coded as a vector knowledge base. Then, similar description content retrieval: after the user inputs natural language and P&ID graph, the model will retrieve similar content in the specification knowledge base and provide it to the LLM for reference. Langchain can be referenced to implement this part, in this way, the closed-source LLM can combine the retrieved professional knowledge when generating code, improving the accuracy and professionalism of the code. Finally, insertion and supplementation: relevant knowledge retrieved is inserted into the LLM's generation process to ensure that the model can utilize these professional knowledge for code generation. This method enhances the model's knowledge ability in the industrial control field without changing the internal structure of the closed-source LLM.
[0133] For open-source large language models, the combination of fine-tuning and search enhancement can improve their ability in PLC code generation. Based on existing open-source large language models, such as Llama 2, the model is fine-tuned using a large amount of data in the industrial control system field, such as the LoRA method, to enable the model to generate professional PLC code. These data include PLC code examples, industrial control cases, and standardized programming templates, etc. On this basis, further enhance the code generation ability of large language models through specification retrieval enhancement.
[0134] 2.2, Code generation: retrieval enhancement, retrieval reuse.
[0135] After the first part is completed, the complete requirements are input into the large language model, such as Figure 4 As shown in the PLC code generation flowchart, the model will first retrieve the code block repository before generating code. The code block repository contains verified PLC code modules, and the model will infer whether the requirement description is similar to the code block repository based on the code block repository. If similar, the code in the repository will be integrated into the generated code to achieve code block reusability; then the model will retrieve the specification knowledge base to further refine the generated code based on the reasoning ability of the large model. When generating code, the model will preferentially search the repository for code blocks that match the input requirements to ensure the reliability of the generated code and reusability. If the requirements are insufficient, the model can directly call the code blocks in the repository, reducing the time and potential errors of generating new code, and improving the reusability and efficiency of the code.
[0136] Step 3: Verification test.
[0137] To ensure that the generated PLC code meets the actual requirements and does not have errors, the present application adopts multi-level verification. Mainly including code verification and compiler two verification aspects.
[0138] First is the syntax check: syntax check of the generated PLC code, ensure that the code conforms to the syntax rules of PLC programming language (such as ST language in IEC 61131-3 standard). Through the automatic tool to check the code line by line, find and correct the syntax error, avoid the failure of compilation due to syntax error.
[0139] Then is the logic verification: after passing the syntax check, further logic verification is carried out. Using simulation tools and automatic test framework to carry out logic analysis and simulation running of the code, verify whether the function of the code meets the design requirements. Logic verification includes the test of control flow, data processing and interface interaction, to ensure that the generated code is logical and has no vulnerability.
[0140] Then is the compilation test: the code passing 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, and if there is an error, the compiler will provide detailed error information. After the compilation is passed, the executable file is generated.
[0141] Finally, the execution test: after the compilation is passed, the generated code is deployed to the simulation PLC for execution test. Through the simulation running, the performance of the code in the actual running is detected, to ensure that there is no error and unexpected behavior in the running. The present application takes OpenPLC as an example, OpenPLC generates.ST file while compiling the code, which is convenient for subsequent automatic deployment.
[0142] Step 4: deployment.
[0143] After the generation and verification of PLC code are completed, deployment is the 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, so a deployment simulation executor is written with script to upload the file to the simulation PLC. Before starting the deployment, a simulation PLC environment needs to be built first. The present application uses OpenPLC and other tools to build the simulation environment, and OpenPLC can directly upload and run the.ST file.
[0144] The entire deployment process is as follows: first, build a simulation 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 simulation PLC environment, apply the configuration file, start the code running, and perform real-time monitoring and logging through the tool. Finally, perform real-time monitoring and fault handling of the system, perform performance evaluation and optimization, and regularly update and upgrade the code. Through these steps, the generated PLC code can be ensured to run stably and reliably in actual industrial control systems, 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 a large language model has the following beneficial effects:
[0146] 1. The present application combines P&ID diagrams and natural language descriptions through a multi-stage multi-modal input part, improving the demand analysis and design stage. This method not only makes the demand description more complete and specific, but also makes the generated PLC code more accurate in function and logic, reducing the possibility of human error.
[0147] 2. The present application uses large language model enhancement technology in the PLC code generation process, which enhances and fine-tunes the large language model to have specialized knowledge in the industrial control field, and adds a code block repository to make the large language model fully consider the calling relationship and reusability between code blocks. The use of this technology 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 application introduces an automated verification process in the code verification and testing stage, which ensures the correctness and reliability of the generated PLC code through compiler compilation and code verification methods. This automated verification technology reduces the time for manual debugging, improves work efficiency, and ensures the stability and security of the code in actual application. On this basis, the present application also designs a deployment simulation executor that can automatically deploy the generated code to a simulation PLC. This technology not only simplifies the deployment process, but also greatly reduces the deployment threshold, allowing network security researchers to quickly build an industrial control target field and focus on security risk research and protection.
[0149] Another aspect of the embodiment of the present application also provides a PLC code generation and verification device based on a large language model, comprising:
[0150] The first module is configured to generate multi-stage multi-modal input data for representing user requirements and formulating a system design scheme according to a requirement analysis result; the multi-stage includes a requirement analysis stage and a design stage in a complete engineering cycle; and the multi-modal input includes P&ID diagrams and natural language.
[0151] The second module is configured to generate PLC code according to the multi-stage multi-modal input data.
[0152] The third module is configured to perform verification testing on the generated PLC code, and the verification testing includes code verification and compiler verification.
[0153] The fourth module is configured to deploy the PLC code after completing the verification testing on the PLC code.
[0154] It can be understood that the content in the above method embodiments is applicable to the device embodiments, the device embodiments specifically implement the functions of the above method embodiments, and achieve the same beneficial effects as the above method embodiments.
[0155] The embodiment of the present application further provides an electronic device, which comprises a memory and a processor, the memory stores a computer program, and the processor implements the above 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 content in the above method embodiments is applicable to the device embodiments, the device embodiments specifically implement the functions of the above method embodiments, and achieve the same beneficial effects as the above method embodiments.
[0157] Please refer to Figure 5 , Figure 5 The hardware structure of the electronic device of another embodiment is illustrated, and the electronic device comprises:
[0158] The processor 501 can be implemented in the form of a general CPU (Central Processing Unit), a microprocessor, an ASIC (Application Specific Integrated Circuit), or one or more integrated circuits, and is used to execute related programs to implement the technical solutions provided by the embodiments of the present application.
[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), etc. The memory 502 can store an operating system and other application programs, and when the technical solutions provided by the embodiments of the present specification are implemented by software or firmware, the related program codes are stored in the memory 502 and are called and executed by the processor 501 to implement the large language model-based PLC code generation and verification method of the embodiments of the present application;
[0160] The input / output interface 503 is used to realize information input and output.
[0161] The communication interface 504 is used to realize the communication interaction between the device and other devices, which can realize communication through wired mode (such as USB, network cable, etc.) or wireless mode (such as mobile network, WI FI, Bluetooth, etc.);
[0162] The bus 505 transmits information between various components (such as the processor 501, the memory 502, the input / output interface 503 and the communication interface 504) of the device.
[0163] The processor 501, the memory 502, the input / output interface 503 and the communication interface 504 are connected to each other through the bus 505 for internal communication in the device.
[0164] The embodiments of the present application also provide a computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to implement the above-mentioned large language model-based PLC code generation and verification method.
[0165] It can be understood that the contents in 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 by the present storage medium embodiments are also the same as those achieved by the above method embodiments.
[0166] The memory, as a non-transitory computer readable storage medium, can be used to store non-transitory software programs and non-transitory computer executable programs. In addition, the memory can include a high-speed random access memory, and can also include a non-transitory memory, such as at least one magnetic disk storage device, a flash memory device, or other non-transitory solid-state memory device. In some embodiments, the memory can optionally include a memory remotely arranged relative to the processor, and these remote memories can be connected to the processor through a network. Examples of the above network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network and a combination thereof.
[0167] It should be noted that in various specific embodiments of the present application, when relevant processing needs to be performed on data related to the identity or characteristics of the user, such as user information, user behavior data, user history data, and user location information, the user's permission or consent is obtained first, and the collection, use, and processing of such data comply with relevant laws, regulations, and standards. In addition, when the embodiments of the present application need to obtain sensitive personal information of the user, the separate permission or separate consent of the user is obtained through a pop-up window or a jump to a confirmation page, and after obtaining the separate permission or separate consent of the user, the necessary user-related data for enabling the embodiments of the present application to normally operate is obtained.
[0168] The embodiments described in the embodiments of the present application are for more clearly illustrating the technical solutions of the embodiments of the present application, and do not constitute a limitation on the technical solutions provided by the embodiments of the present application. Those skilled in the art can know that, with the evolution of technology and the appearance of new application scenarios, the technical solutions provided by the embodiments of the present application are also applicable to similar technical problems.
[0169] Those skilled in the art can understand that the technical solutions shown in the figures do not constitute a limitation on the embodiments of the present application, and can include more or fewer steps than shown in the figures, or combine certain steps, or different steps.
[0170] The device embodiments described above are only schematic, and the units described as separate components can or can not be physically separate, that is, they can be located in one place, or distributed on multiple network units. Part or all of the modules can be selected according to actual needs to achieve the purpose of the embodiments of the present application.
[0171] Those skilled in the art can understand that all or some of the steps in the above disclosed method, the function modules / units in the system and the device can be implemented as software, firmware, hardware and their appropriate combinations.
[0172] The terms "first", "second", "third", "fourth" and the like in the specification of the present application and the above-described drawings, if any, are used to distinguish similar objects, and do not necessarily have to be used to describe a particular order or sequence. It should be understood that the data thus used can be interchanged under appropriate circumstances, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion, for example, a process, method, system, product or device including a series of steps or units does not have to be limited to those steps or units clearly listed, but can include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0173] It should be understood that, in the present application, "at least one" means one or more, and "multiple" means two or more. "And / or" is used to describe the relationship between associated objects, which means that there can be three relationships, for example, "A and / or B" can mean: only A, only B, and A and B exist at the same time, where A and B can be singular or plural. The character " / " generally represents an "or" relationship between the associated objects. "At least one of the following" or the like means any combination of these items, including single or multiple items. For example, at least one of a, b or c can mean: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, and c can be single or multiple.
[0174] In several embodiments provided by the present application, 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 illustrative, for example, the division of the above units is only a logical function division, and actual implementation can have another division manner, for example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between the displayed or discussed units can be indirect coupling or communication connection through some interfaces, devices or units, which can be electrical, mechanical or other forms.
[0175] The units described above as separate components can or can not be physically separated, and the components shown as units can or can not be physical units, that is, they can be located in one place, or they can be distributed on multiple network units. According to actual needs, some or all of the units can be selected to achieve the purpose of the embodiment scheme.
[0176] In addition, each functional unit in each embodiment of the present application can be integrated into a processing unit, or each unit can exist physically, or two or more units can be integrated into one unit. The integrated unit can be realized in the form of hardware or in the form of a software functional unit.
[0177] The integrated unit, if implemented in the form of a software function unit and sold or used as an independent product, can be stored in a computer readable storage medium. Based on such understanding, the technical solutions of the present application, essentially or in other words, the part that contributes to the prior art or the whole or part of the technical solutions can be embodied in the form of a software product. The computer software product is stored in a storage medium, and includes multiple instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods of the various embodiments of the present application. The aforementioned storage medium includes: a U disk, a mobile hard disk, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, and various program storage media.
[0178] The preferred embodiments of the embodiments of the present application are described above with reference to the accompanying drawings, and are not limited to the scope of the embodiments of the present application. Any modifications, equivalent replacements and improvements made by those skilled in the art without departing from the scope and essence of the embodiments of the present application shall be within the scope of the embodiments of the present application.
Claims
1. A method for PLC code generation and verification based on a large language model, characterized in that, Includes the following steps: Multi-stage multimodal input data is generated, which is used to characterize user needs and formulate system design schemes based on the results of needs analysis; the multi-stages include the needs analysis stage and the design stage of the complete engineering cycle; the multimodal inputs include P&ID diagrams and natural language. Based on the multi-stage, multi-modal input data, PLC code is generated; The generated PLC code is subjected to verification tests, including code verification and compiler verification. After completing the verification test of the PLC code, the PLC code is deployed; The generation of multi-stage, multi-modal input data includes the following steps: For situations where security personnel only have a natural language requirement description: Configure an initial requirement acquisition prompt to obtain the user-input natural language requirement description; use an LLM (Large Language Model) to semantically supplement and form an enhanced semantic prompt, so that the LLM generates PLC code; wherein, before inputting the natural language requirement description into the LLM, a search and matching 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 that they belong to the same requirement module. If the cosine similarity is higher than a set threshold, the new natural language requirement description is replaced with the description in the standard knowledge base; The generation of multi-stage, multi-modal input data further includes the following steps: For cases where safety personnel possess design specifications: obtain the P&ID diagram from the design specifications; input the P&ID diagram and natural language requirement description into the LLM large language model, combine the P&ID diagram and natural language requirement description to form a complete and detailed requirement statement; understand and parse the information in the P&ID diagram using image recognition and natural language processing technologies, and integrate the P&ID diagram with the natural language requirement description to form a code generation enhanced semantic prompt to generate PLC code; The method further includes: establishing a standardized knowledge base containing knowledge in the field of industrial control systems, including PLC programming standards, common control logic, standardized modules, and practical cases; after the user inputs natural language, the model will search and match in the standardized knowledge base and provide the matched content to the LLM large language model for reference.
2. The 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 based on the multi-stage multimodal input data includes the following steps: Enhance the domain knowledge of the large language model in LLM; The multi-stage, multi-modal input data is input into the LLM large language model to generate PLC code.
3. The PLC code generation and verification method based on a large language model according to claim 2, characterized in that, The enhanced domain knowledge of the LLM large language model includes the following steps: Build a standardized knowledge base, including PLC programming standards, common control logic, standardized modules, and practical cases; Description of content retrieval: After the user inputs natural language, the model will search and match in the above-mentioned knowledge base and provide the matched content to the LLM large language model for reference; The retrieved relevant knowledge is inserted into the generation process of the LLM large language model to ensure that the model can use this expertise to generate code; Next, the LLM large language model will search the canonical knowledge base and improve the generated code; When generating code, the LLM Large Language Model prioritizes searching for code blocks in the code block repository that match the input requirements, ensuring the generated code is reliable and reusable. If the requirements are insufficient, the LLM Large Language Model can directly call code blocks in the code block repository, reducing the time for generating new code and potential errors.
4. The PLC code generation and verification method based on a large language model according to claim 1, characterized in that, The verification and testing of the generated PLC code includes the following steps: Syntax check: Perform a syntax check on the generated PLC code to ensure that the code conforms to the syntax rules of the PLC programming language. Use automated tools to check the code line by line to find and correct syntax errors. Logical verification: After passing the syntax check, further logical verification is performed. Simulation tools and automated testing frameworks are used to analyze and simulate the code to verify whether the code's functionality meets the design requirements. The logical verification includes testing the control flow, data processing, and interface interaction to ensure that the generated code is logically correct and free of vulnerabilities. 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 types of the code are correct. If there are errors, the compiler will provide detailed error information. After successful compilation, an executable file will be generated. Execution testing: After successful compilation, the generated code is deployed to the simulated PLC for execution testing. Through simulation, the code's performance in actual operation is detected to ensure that there are no errors or unexpected behaviors during runtime.
5. The 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, the deployment of the PLC code includes the following steps: Set up a simulated PLC environment and select a suitable deployment tool; perform a final check on the code and prepare the configuration files. Upload the code to the simulation PLC environment, apply the configuration file, start the code execution, and log the process using the tool. Perform real-time system monitoring and fault handling, conduct performance evaluation and optimization, and regularly update and upgrade the code.
6. 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 multimodal input data, which is used to characterize user needs and formulate system design schemes based on the results of needs analysis. The multi-stage process includes the requirements analysis stage and the design stage of the entire engineering cycle; the multimodal inputs include P&ID diagrams and natural language. The second module is used to generate PLC code based on the multi-stage multimodal input data; The third module is used to verify and test the generated PLC code, including code verification and compiler verification. The fourth module is used to deploy the PLC code after the verification test of the PLC code is completed; The first module is used for situations where security personnel only have a natural language requirement description: It configures an initial requirement acquisition prompt to obtain the user-input natural language requirement description; it then uses an LLM (Large Language Model) to semantically supplement the requirement description, forming an enhanced semantic prompt, so that the LLM generates PLC code. Specifically, before inputting the natural language requirement description into the LLM, a search and matching process is performed in the standard knowledge base. The requirement description is vectorized using a Word2Vec model, and the cosine similarity between the standard knowledge base vector and the requirement description vector is calculated to determine if they belong to the same requirement module. If the cosine similarity is higher than a set threshold, the new natural language requirement description is replaced with the description from the standard knowledge base. The first module is also configured to, for cases where safety personnel possess design specifications: obtain a P&ID diagram from the design specifications; input the P&ID diagram and natural language requirement description into the LLM large language model, combine the P&ID diagram and natural language requirement description to form a complete and detailed requirement statement; understand and parse the information of the P&ID diagram through image recognition and natural language processing technology, and integrate the P&ID diagram with the natural language requirement description to form a code generation enhanced semantic prompt to generate PLC code; The PLC code generation and verification device based on the large language model is also used to establish a standardized knowledge base containing knowledge in the field of industrial control systems. The standardized knowledge base includes PLC programming specifications, common control logic, standardized modules, and practical cases. After the user inputs natural language, the model will search and match in the standardized knowledge base and provide the matched content to the LLM large language model for reference.
7. An electronic device, characterized in that, Including the processor and memory; The memory is used to store programs; The processor executes the program to implement the method as described in any one of claims 1 to 5.
8. A computer-readable storage medium, characterized in that, The storage medium stores a program that is executed by a processor to implement the method as described in any one of claims 1 to 5.
Citation Information
Patent Citations
Code construction method, model fine tuning method, equipment and storage medium
CN117369796A
Text2SQL semantic parsing method for domain large language model
CN118377796A