Multi-agent-based programmable logic controller structured text generation method
Through multi-agent collaboration and retrieval enhancement generation technology, a knowledge base for programming task solutions for programmable logic controllers is built, which solves the problems of low automation and text generation in the existing technology, and achieves efficient and reliable structured text generation.
Patent Information
- Application Number
- CN202510374493.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-27
- Publication Date
- 2025-08-08
AI Technical Summary
The prior art has low degree of automation when generating structured text of programmable logic controllers, and lacks integration of various links and tools, resulting in poor scalability and inability to effectively verify the correctness of generated text and complex task processing.
Using multi-agent collaboration methods, a knowledge base for programming task solutions for programmable logic controllers is built, knowledge retrieval, task planning, structured text generation, verification and error correction agents are designed, and the retrieval enhancement generation technology is combined with external tools to perform grammar checking and formal verification to ensure the correctness of the generated structured text.
Improve the success rate and scalability of automated generation of structured text of programmable logic controllers, reduce hallucination problems, and ensure the correctness and reliability of generated text.
Smart Images

Figure CN120450033A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of artificial intelligence and industrial control systems, and in particular to a method for generating structured text for a programmable logic controller based on multi-agents. Background Art
[0002] A programmable logic controller (PLC) is a computer system specifically designed for industrial automation control, enabling real-time monitoring and control of mechanical equipment and production processes. Its advantages include high reliability, ease of programming, and scalability, making it widely used in manufacturing, transportation, energy, and other fields. The IEC 61131-3 standard provides five standard programming languages for PLCs: three graphical languages: ladder diagram, function block diagram, and sequential function chart; and two text languages: structured text and instruction list. Structured text, similar in syntax and structure to traditional programming languages, offers greater flexibility and readability, supporting common programming constructs such as loops and conditionals. Therefore, it is widely used in scenarios requiring complex mathematical operations, data processing, and advanced control algorithms. With the development of intelligent manufacturing, structured text holds a promising future, complementing other programming languages to enhance the flexibility and efficiency of PLC systems.
[0003] Currently, the writing of structured text still relies primarily on professionals, with a low level of automation. Emerging large language models for code generation may perform well when generating code in mainstream high-level languages (such as C or Python), but due to the scarcity of industrial control code, they perform poorly in generating structured text. Secondly, structured text is used to manage industrial operations, and ensuring its functional correctness is crucial. However, current research can only verify the correctness of specifications at the design level, and cannot ensure the correctness of structured text generated by large language models. Furthermore, existing work lacks the flexibility to integrate various links and tools to achieve fully automated structured text generation, resulting in poor scalability and an inability to handle complex tasks. Summary of the Invention
[0004] The purpose of the embodiment of the present application is to provide a method for generating structured text of a programmable logic controller based on multiple agents. Through the mutual collaboration of multiple agents and the flexible calling of various tools and models, and the use of retrieval enhanced generation technology, the lack of knowledge of large language models in programmable logic controller programming is compensated, and the hallucination problem that occurs in the process of generating structured text of programmable logic controllers by large language models is effectively reduced, thereby improving the success rate and scalability of automatically generating structured text of programmable logic controllers.
[0005] According to a first aspect of an embodiment of the present application, a method for generating structured text of a programmable logic controller based on a multi-agent is provided, comprising: Build a knowledge base of solutions to programmable logic controller programming tasks; Design prompt words separately, call the large language model, and use external tools to build five large model agents, including knowledge retrieval agent, task planning agent, structured text generation agent, verification agent, and error correction agent; Receive an input programming task description, call the knowledge retrieval agent, and retrieve relevant question-answer pairs in the programmable logic controller programming task solution knowledge base according to the programming task description; Calling the task planning agent to convert the programming task description and related question-answer pairs into task planning prompt words, and using the task planning prompt words to prompt the large model to obtain the programming task solution steps; Invoking the structured text generation agent to convert the programming task description, related question-answer pairs, and programming task solution steps into structured text generation prompt words, and using the structured text generation prompt words to prompt the large model to obtain unverified structured text; The verification agent is called, and a grammar checking tool is used to perform grammar checking and compilation on the unverified structured text to obtain corresponding compilation feedback information. If the compilation fails, the error correction agent is called; if the compilation is successful, the programming task description and the unverified structured text are converted into specification generation prompt words, and the specification generation prompt words are used to prompt the large model to obtain a specification set, and a formal verification tool is used to perform formal verification on the specification set and the unverified structured text to obtain corresponding formal verification feedback information. If the verification is successful, the structured text is output; if the verification fails, the error correction agent is called; wherein, the error correction agent converts the programming task description, programming task solution steps, compilation feedback information or formal verification feedback information into error correction prompt words, and the error correction prompt words are used to prompt the large model to obtain a repair patch, and the repaired unverified structured text is obtained according to the repair patch, and the verification agent is continued to be called.
[0006] According to a second aspect of an embodiment of the present application, a multi-agent-based programmable logic controller structured text generation device is provided, comprising: A first building module is used to build a knowledge base of programmable logic controller programming task solutions; The second building block is used to design prompt words, call the large language model, and use external tools to build five large model agents, including a knowledge retrieval agent, a task planning agent, a structured text generation agent, a verification agent, and an error correction agent. A knowledge retrieval module is configured to receive an input programming task description, invoke the knowledge retrieval agent, and retrieve relevant question-answer pairs from the programmable logic controller programming task solution knowledge base according to the programming task description; A task planning module is used to call the task planning agent, convert the programming task description and related question-answer pairs into task planning prompt words, and use the task planning prompt words to prompt the large model to obtain the programming task solution steps; a code generation module, configured to call the structured text generation agent, convert the programming task description, related question-answer pairs, and programming task solution steps into structured text generation prompt words, and prompt the large model with the structured text generation prompt words to obtain unverified structured text; The verification and error correction module is used to call the verification agent, use the grammar checking tool to perform grammar checking and compilation on the unverified structured text, obtain corresponding compilation feedback information, and call the error correction agent if the compilation fails; if the compilation is successful, the programming task description and the unverified structured text are converted into specification generation prompt words, and the specification generation prompt words are used to prompt the large model to obtain a specification set, and the formal verification tool is used to perform formal verification on the specification set and the unverified structured text to obtain corresponding formal verification feedback information. If the verification is successful, the structured text is output; if the verification fails, the error correction agent is called; wherein, the error correction agent converts the programming task description, programming task solution steps, compilation feedback information or formal verification feedback information into error correction prompt words, and uses the error correction prompt words to prompt the large model to obtain a repair patch, and obtains the repaired unverified structured text according to the repair patch, and continues to call the verification agent.
[0007] According to a third aspect of an embodiment of the present application, there is provided an electronic device, including: one or more processors; a memory for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in the first aspect.
[0008] According to a third aspect of an embodiment of the present application, a computer-readable storage medium is provided, on which computer instructions are stored. When the instructions are executed by a processor, the steps of the method described in the first aspect are implemented.
[0009] The technical solutions provided by the embodiments of the present application may have the following beneficial effects: It can be seen from the above embodiments that the present invention of the present application uses a large language model with multiple agents to automatically generate programmable logic controller structured text. Through the mutual collaboration of multiple agents and the flexible and autonomous calling of various tools and models, and the use of retrieval-enhanced generation technology, the lack of knowledge of the large language model in programmable logic controller programming is compensated, and the hallucination problem that occurs in the process of generating programmable logic controller structured text by the large language model is effectively reduced, thereby improving the success rate and scalability of automatically generating programmable logic controller structured text.
[0010] It should be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present application. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0012] Figure 1 The present invention is a flowchart of a method for generating structured text of a programmable logic controller based on multiple agents according to an exemplary embodiment.
[0013] Figure 2 The present invention is a flowchart of a method for generating structured text of a programmable logic controller based on multiple agents according to an exemplary embodiment.
[0014] Figure 3 It is a structural schematic diagram of a multi-agent based programmable logic controller structured text generation device according to an exemplary embodiment. DETAILED DESCRIPTION
[0015] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all embodiments consistent with the present application. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present application, as detailed in the appended claims.
[0016] The terms used in this application are for the purpose of describing specific embodiments only and are not intended to limit this application. As used in this application and the appended claims, the singular forms "a," "an," "the," and "the" are intended to include the plural forms, unless the context clearly indicates otherwise. It should also be understood that the term "and / or" as used herein refers to and encompasses any and all possible combinations of one or more of the associated listed items.
[0017] It should be understood that although the terms first, second, third, etc. may be used in this application to describe various information, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of this application, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0018] Figure 1 and Figure 2 FIG. 1 is a flow chart of a method for generating structured text of a programmable logic controller based on a multi-agent system according to an exemplary embodiment. Figure 1 and Figure 2 As shown, the method is applied in a terminal and may include the following steps: S1: Build a knowledge base of programmable logic controller programming task solutions.
[0019] Since general or large code language models lack PLC programming language expertise, the generated PLC structured code is poor. Therefore, it is necessary to build a PLC programming task solution knowledge base to alleviate this problem.
[0020] Specifically, based on data sources such as the GitHub website, papers related to generating structured text, and books related to programmable logic controller programming, the programmable logic controller structured text programming corpus is obtained through online search and reading related books.
[0021] Extract programming question-answer pairs from the structured text programming corpus of programmable logic controllers, denoted as ,in Indicates the Programming Q&A pairs, Indicates the Programming task description for a programming question-answer pair, Indicates the Solutions to programming Q&A pairs.
[0022] Use text embedding models (such as gte-Qwen2-7B-instruct model) to embed each programming question-answer pair Programming task description Convert it into a semantic embedding vector, then store the semantic embedding vector in a vector database and create an index.
[0023] This design can make up for the lack of knowledge of large language models in programmable logic controller programming and improve the accuracy and reliability of structured text generation.
[0024] S2: Design prompt words separately, call the large language model, and use external tools to build five large model agents, including knowledge retrieval agent, task planning agent, structured text generation agent, verification agent, and error correction agent.
[0025] Specifically, the prompt word of the knowledge retrieval agent is the programming task description input by the user. It calls a text embedding model (such as the gte-Qwen2-7B-instruct model) to convert the programming task description into a semantic embedding vector, and uses the Faiss library to retrieve relevant question-answer pairs in the programmable logic controller programming task solution knowledge base.
[0026] The prompt for the task planning agent is: Given a programming problem that needs to be solved using IEC 61131-3 structured text , you need to refer to the relevant programming task solutions ,generate A specific coding plan to solve the problem should be provided. This plan should include the following steps: 1. Clearly define the programming problem to be solved, breaking it down into manageable submodules; identify the predefined function blocks or functions to be used. 2. Outline the structure of the function blocks, including their names, variable declarations, inputs, outputs, and internal variables. 3. Provide step-by-step instructions for writing structured text code, from variable declarations to algorithm implementation. Simply provide a plan to solve the problem. Do not add additional explanations or text. This agent should select a large language model that excels at logical reasoning and planning, such as the DeepSeek-R1 model.
[0027] The prompt words for the structured text generation agent are: Given a programming problem that needs to be solved using IEC 61131-3 structured text , you need to refer to the relevant programming task solutions , follow the steps to solve this problem , to generate structured text that solves the problem. Please note the following points: 1. If necessary, comments should be framed using brackets and asterisks "(* comment *)". 2. When defining variables and writing code, never use ellipsis and comments to omit content. 3. Every variable that appears in the program must be declared, including loop variables. 4. When calling a function or function block, the specific code of the corresponding function or function block needs to be given. Please think step by step and output the structured text that solves the problem in the format of ```st your_code_here ```. This intelligent agent needs to choose a large language model that is good at code generation and format control, such as the DeepSeekCoder series of models.
[0028] The prompt word for the verification agent is: Given a programming problem that needs to be solved using IEC 61131-3 structured text , you have generated a structured text To ensure that it is safe and usable and meets the functional requirements of the question, please follow the rules of the formal verification tool PLCverif Generate a specification for each functional requirement and output the specification set in JSON format Please think step by step and only generate a set of JSON-formatted specifications based on the PLCverif usage rules. Do not add additional explanations or text. For example, the following example contains two specifications: This agent needs to select a large language model that excels at logical reasoning and knowledge reasoning, such as the DeepSeek-R1 model. The agent can call syntax checking and compilation tools on demand, such as the programmable logic controller integrated development environment CODESYS or open source compilers MATIEC and Rusty, to perform syntax checking and compilation on unverified structured text. It can also call existing formal verification tools for programmable logic controller programming languages, such as PLCverif, and use models such as SMV and CBMC to formally verify unverified structured text.
[0029] The prompt word for the error correction agent is: Given a programming problem that needs to be solved using IEC 61131-3 structured text , you have generated a structured text , but its syntax is wrong or it fails to meet the functional requirements of the question. The error feedback information is Please follow the steps to solve this problem. , thinking step by step according to the error feedback information to generate a repair patch to modify the previously generated structured text. This intelligent agent needs to use a large language model that excels at code modification and text editing, such as the DeepSeek-V3 model.
[0030] By customizing and designing a variety of large-model intelligent agents, each agent has its own strengths, and they can divide the work and collaborate efficiently to complete tasks such as knowledge retrieval, task planning, structured text generation, structured text verification, and structured text error correction, thereby realizing the automatic generation of reliable programmable logic controller structured text.
[0031] S3: Receive the programming task description input by the user, call the knowledge retrieval agent, and retrieve relevant question and answer pairs in the programmable logic controller programming task solution knowledge base according to the programming task description.
[0032] Specifically, the programming task input by the user is described as , using text embedding models (such as the gte-Qwen2-7B-instruct model) to describe programming tasks Converted into semantic embedding vector ,Through this semantic embedding vector and the index of the programmable logic controller programming task solution knowledge base, the programming question and answer pairs related to the programming task description input by the user are obtained for the subsequent generation of an enhanced large model.
[0033] Among them, according to the programming task description, relevant question-answer pairs are retrieved from the programmable logic controller programming task solution knowledge base, including: The semantic embedding vector is used to calculate the cosine similarity of each programming task description, so as to retrieve the programming task description in the programmable logic controller programming task solution knowledge base that is consistent with the user input. A list of related programming task descriptions, denoted as ,in Indicates the Programming task description for a programming question-answer pair,
[0034] Specifically, the cosine similarity between the programming task description input by the user and the programming task description of each programming question-answer pair is calculated. For example, the programming task description input by the user Hedi Programming task description for programming question-answer pairs The cosine similarity between as follows:
[0035] in, and The programming task description and The semantic embedding vector of the programming task description of the programming question-answer pair. The closer the cosine similarity value is to 1, the more similar the two vectors are. The top vector with the highest cosine similarity to the programming task description entered by the user is selected. Programming task descriptions for programming question-answer pairs, .
[0036] According to the index, the retrieved programming task description is paired with the corresponding solution, and the knowledge retrieval agent returns the relevant question-answer pair, which is recorded as ,in Indicates the Programming Q&A pairs, .
[0037] Specifically, the programmable logic controller programming task solution knowledge base is constructed as a dictionary, denoted as ,in is a key-value pair, Indicates the Programming Q&A pairs, Indicates the Programming task description for a programming question-answer pair, Indicates the The solution to the programming question and answer pair can be quickly found according to the index of these key-value pairs.
[0038] This design only needs to embed the programming task description, and can quickly find relevant question-answer pairs in a large-scale knowledge base through semantic embedding vector calculation and fast matching of indexes, reducing the time overhead of traditional rule-based or keyword-based searches and improving retrieval efficiency and accuracy.
[0039] S4: Invoke the task planning agent to convert the programming task description and related question-answer pairs into task planning prompt words, and use the task planning prompt words to prompt the large model to obtain the programming task solution steps; this step includes the following sub-steps: S41: Based on the programming knowledge of programmable logic controllers, summarize the structured text programming process and compare it with the programming task description entered by the user Related question and answer pairs retrieved Combined, they form the task planning prompt words; Specifically, the task planning prompt words provided in this embodiment are as follows: Given a programming problem that needs to be solved using IEC 61131-3 structured text , you need to refer to the relevant programming task solutions ,generate A specific coding plan should be developed to solve the problem. This plan should include the following steps: 1. Clearly define the programming problem to be solved, breaking it down into manageable submodules; identify the predefined function blocks or functions to be used. 2. Outline the structure of the function blocks, including their names, variable declarations, inputs, outputs, and internal variables. 3. Provide step-by-step instructions for writing structured text code, from variable declarations to algorithm implementation. Simply provide a plan for solving the problem. Do not add additional explanations or text.
[0040] S42: Input the task planning prompt words into the large language model, first generate candidate programming task solution plans, each of which contains different programming task solution steps, and then the candidate programming task solution plans are ranked in terms of feasibility, difficulty, etc., and the programming task solution steps in the optimal programming task solution plan are output. .
[0041] Specifically, the task planning prompt words are input into the large language model (such as the DeepSeek-R1 model) to obtain candidate programming task plans , design solution sorting prompt words, input the solution sorting prompt words into the large language model to obtain the programming task solution steps in the optimal programming task solution plan The solution provided in this embodiment provides the following sorting prompts: Given candidate programming task solutions , each plan contains different steps for solving programming tasks. Please sort these plans according to the following evaluation criteria and output the steps for solving programming tasks in the optimal programming task solution plan. The evaluation criteria include: 1. Feasibility, whether the plan can effectively solve the problem and whether it complies with the specifications of IEC 61131-3 structured text. 2. Difficulty, the implementation complexity of the plan, whether it is easy to understand, write and maintain. 3. Efficiency, whether the plan can efficiently solve the problem and avoid redundant or inefficient code. 4. Maintainability, evaluate the maintainability of each solution plan in long-term use, including the clarity, scalability, ease of debugging and modification of the code, etc.
[0042] This design can evaluate and select the generated solutions from multiple dimensions, which not only ensures the technicality and effectiveness of the solutions, avoids large models from generating low-quality or inapplicable solutions, but also takes into account long-term maintainability and ease of use, thereby reducing later problems and debugging work.
[0043] S5: Invoking the structured text generation agent to convert the programming task description, related question-answer pairs, and programming task solution steps into structured text generation prompt words, and using the structured text generation prompt words to prompt the large model to obtain unverified structured text; this step includes the following sub-steps: S51: Based on the programming knowledge of programmable logic controllers, summarize key information such as structured text grammatical rules and constraints, and combine them with the programming task description entered by the user , the retrieved relevant question and answer pairs and steps to solve programming tasks Combined with the structured text, it is transformed into a prompt word; Specifically, the structured text generation prompt words provided in this embodiment are as follows: Given a programming problem that needs to be solved using IEC 61131-3 structured text , you need to refer to the relevant programming task solutions , follow the steps to solve this problem , to generate structured text that solves the problem. Please note the following: 1. If necessary, comments should be enclosed in brackets and asterisks (* comment *) . 2. When defining variables and writing code, never use ellipsis or comments to omit content. 3. Every variable that appears in the program must be declared, including loop variables. 4. When calling a function or function block, provide the specific code for the corresponding function or function block. Please think step by step and output the structured text that solves the problem in the format ```st your_code_here```.
[0044] S52: Inputting the structured text generation prompt word into the large language model, and parsing the output of the large language model using a regular expression to obtain an unverified structured text; Specifically, the output of a large language model may not only contain unverified structured text, but also include other explanations and suggestions. Therefore, the regular expression ```st\s*(.*?)``` is needed to match and extract unverified structured text from the output of a large language model. For example, the following output is: This function block automatically controls the heater based on the current temperature. When the temperature is below 18°C, the heater turns on; when the temperature reaches or exceeds 24°C, the heater turns off. The HeaterOn variable reflects the heater status.
[0045] ```st FUNCTION_BLOCK HeatingSystemControl VAR Temperature: REAL; (* Current temperature value, unit ℃ *) HeaterOn: BOOL:=FALSE; (*heater status, off by default*) END_VAR (* Main control logic *) IF Temperature<18.0 THEN HeaterOn := TRUE; (* Start heating when the temperature is lower than 18℃*) ELSIF Temperature>= 24.0 THEN HeaterOn := FALSE; (* Turn off heating when temperature ≥ 24℃*) END_IF; END_FUNCTION_BLOCK ``` Using the regular expression ```st\s*(.*?)```, we can extract the structured text part from the above output, namely: FUNCTION_BLOCK HeatingSystemControl VAR Temperature: REAL; (* Current temperature value, unit ℃ *) HeaterOn: BOOL:=FALSE; (*heater status, off by default*) END_VAR (* Main control logic *) IF Temperature<18.0 THEN HeaterOn := TRUE; (* Start heating when the temperature is lower than 18℃*) ELSIF Temperature>= 24.0 THEN HeaterOn := FALSE; (* Turn off heating when temperature ≥ 24℃*) END_IF; END_FUNCTION_BLOCK This design can not only fully utilize the generative capabilities of large language models, but also ensure that the output structured text meets the expected requirements through clear rules and hierarchical processing, thereby improving the quality and efficiency of automated programming task solutions.
[0046] S6: Call the verification agent, use the grammar checking tool to perform grammar checking and compilation on the unverified structured text, and obtain corresponding compilation feedback information. If the compilation fails, call the error correction agent; if the compilation is successful, convert the programming task description and the unverified structured text into specification generation prompt words, use the specification generation prompt words to prompt the large model to obtain a specification set, use the formal verification tool to perform formal verification on the specification set and the unverified structured text, and obtain corresponding formal verification feedback information. If the verification is successful, output the structured text; if the verification fails, call the error correction agent.
[0047] Specifically, if the unvalidated structured text If the compilation is successful, the summarized specification template and the programming task description will be , unvalidated structured text The combination is transformed into a specification generation prompt word, and the specification generation prompt word prompts the large model to obtain a specification set, which represents the functions that the unverified structured text needs to implement.
[0048] The method of using a grammar checking tool to perform grammar checking and compiling on the unverified structured text includes: Call syntax checking tools and compilation tools such as the programmable logic controller integrated development environment CODESYS or open source compilers MATIEC and Rusty to perform syntax checking and compilation on the unverified structured text and output compilation feedback information.
[0049] The formal verification of the specification set and the unverified structured text using a formal verification tool includes: Call existing formal verification tools for programmable logic controller programming languages, such as PLCverif, and use models such as SMV and CBMC to formally verify unverified structured texts, and output formal verification feedback information to determine whether they meet the specification set and have the required functions.
[0050] Among them, the error correction agent converts the programming task description, programming task solution steps, and compilation feedback information (or formal verification feedback information) into error correction prompt words, prompts the large model through the error correction prompt words to obtain a repair patch, and obtains the repaired unverified structured text based on the repair patch, and continues to call the verification agent.
[0051] Specifically, if the error correction agent is called, it means that an error has occurred in the unverified structured text, and corresponding compilation feedback information or formal verification feedback information needs to be provided to the large language model for error correction.
[0052] The unverified structured text after repair is obtained according to the repair patch, and the verification agent is continued to be called, including: The programming task description entered by the user , Steps to solve programming tasks , repair patches and unvalidated structured text Summarize the words as prompts to prompt the large language model to obtain the repaired unverified structured text; Specifically, the prompt words provided in this embodiment are as follows: Given a programming problem that needs to be solved using IEC 61131-3 structured text , you have generated a structured text , but its syntax is incorrect or it fails to meet the functional requirements of the question. Please follow the steps to solve this problem , follow the fix patch to modify the previously generated structured text. Please note the following: 1. If necessary, comments should be enclosed in brackets and asterisks (*comment *)." 2. When defining variables and writing code, never use ellipsis or comments to omit content. 3. Every variable that appears in a program must be declared, including loop variables. 4. When calling a function or function block, the specific code for the corresponding function or function block must be provided. Please think step by step and output the structured text that solves the problem in the format ```st your_code_here```.
[0053] If the verification agent has never been called, set the number of times the verification agent is called ; If the verification agent needs to be called and the verification agent calls the number of times , then the number of agent calls will be verified Add one; if the verification agent needs to be called and the verification agent calls the number of times , then the output is structured text. The maximum number of times to call the verification agent.
[0054] Specifically, each time the structured text output by the large model is obtained, the verification agent must be called to confirm whether the syntax and implemented functions of the structured text are correct. Therefore, the number of times the verification agent is called is the number of times the large model generates structured text. Limiting this number can make the entire verification process more stable and controllable, and save computing resources and time.
[0055] This design significantly improves the efficiency and quality of code generation through automation, intelligence, and iterative optimization, while also lowering development barriers and resource consumption. It combines multiple technologies, including syntax checking, compilation, formal verification, and intelligent error correction, and supports a variety of verification tools to ensure that the final output code is not only syntactically correct but also meets functional requirements, providing an efficient, reliable, and controllable code generation and verification process.
[0056] It can be seen from the above embodiments that the present invention of the present application uses a large language model with multiple agents to automatically generate programmable logic controller structured text. Through the mutual collaboration of multiple agents and the flexible and autonomous calling of various tools and models, and the use of retrieval-enhanced generation technology, the lack of knowledge of the large language model in programmable logic controller programming is compensated, and the hallucination problem that occurs in the process of generating programmable logic controller structured text by the large language model is effectively reduced, thereby improving the success rate and scalability of automatically generating programmable logic controller structured text.
[0057] Corresponding to the aforementioned embodiment of the method for generating structured text of a programmable logic controller based on multi-agents, the present application also provides an embodiment of an apparatus for generating structured text of a programmable logic controller based on multi-agents.
[0058] Figure 3 This is a block diagram of a structured text generation device for a programmable logic controller based on multiple agents according to an exemplary embodiment. Figure 3 , the device comprises: The first building module 1 is used to build a knowledge base of programmable logic controller programming task solutions; The second construction module 2 is used to design prompt words, call the large language model, and use external tools to build five large model agents, including a knowledge retrieval agent, a task planning agent, a structured text generation agent, a verification agent, and an error correction agent; The knowledge retrieval module 3 is configured to receive an input programming task description, invoke the knowledge retrieval agent, and retrieve relevant question-answer pairs from the programmable logic controller programming task solution knowledge base according to the programming task description; Task planning module 4 is used to call the task planning agent, convert the programming task description and related question-answer pairs into task planning prompt words, and use the task planning prompt words to prompt the large model to obtain the programming task solution steps; A code generation module 5 is configured to call the structured text generation agent to convert the programming task description, related question-answer pairs, and programming task solution steps into structured text generation prompt words, and to prompt the large model with the structured text generation prompt words to obtain an unverified structured text; The verification and error correction module 6 is used to call the verification agent, use the grammar checking tool to perform grammar checking and compilation on the unverified structured text, and obtain corresponding compilation feedback information. If the compilation fails, the error correction agent is called; if the compilation is successful, the programming task description and the unverified structured text are converted into specification generation prompt words, and the specification generation prompt words are used to prompt the large model to obtain a specification set, and the formal verification tool is used to perform formal verification on the specification set and the unverified structured text to obtain corresponding formal verification feedback information. If the verification is successful, the structured text is output; if the verification fails, the error correction agent is called; wherein, the error correction agent converts the programming task description, programming task solution steps, compilation feedback information or formal verification feedback information into error correction prompt words, and uses the error correction prompt words to prompt the large model to obtain a repair patch, and obtains the repaired unverified structured text according to the repair patch, and continues to call the verification agent.
[0059] Regarding the apparatus in the above embodiment, the specific manner in which each module performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.
[0060] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to the partial description of the method embodiments. The device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the present application scheme. A person of ordinary skill in the art can understand and implement it without paying any creative work.
[0061] Accordingly, the present application also provides an electronic device, comprising: one or more processors; a memory for storing one or more programs; when the one or more programs are executed by the one or more processors, the one or more processors implement the above-mentioned multi-agent-based programmable logic controller structured text generation method.
[0062] Correspondingly, the present application also provides a computer-readable storage medium on which computer instructions are stored. When the instructions are executed by a processor, the method for generating structured text of a programmable logic controller based on multiple agents as described above is implemented.
[0063] Those skilled in the art will readily appreciate other embodiments of the present application after considering the specification and practicing the contents disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present application that follow the general principles of the present application and include common knowledge or customary techniques in the art not disclosed herein. The description and examples are to be considered merely as exemplary, and the true scope and spirit of the present application are indicated by the claims.
[0064] It should be understood that the present application is not limited to the exact structures described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present application is limited only by the appended claims.
Claims
1. A method for generating structured text of a programmable logic controller based on multi-agent, characterized in that: include: Build a knowledge base of solutions to programmable logic controller programming tasks; Design prompt words separately, call the large language model, and use external tools to build five large model agents, including knowledge retrieval agent, task planning agent, structured text generation agent, verification agent, and error correction agent; Receive an input programming task description, call the knowledge retrieval agent, and retrieve relevant question-answer pairs in the programmable logic controller programming task solution knowledge base according to the programming task description; Calling the task planning agent to convert the programming task description and related question-answer pairs into task planning prompt words, and using the task planning prompt words to prompt the large model to obtain the programming task solution steps; Invoking the structured text generation agent to convert the programming task description, related question-answer pairs, and programming task solution steps into structured text generation prompt words, and using the structured text generation prompt words to prompt the large model to obtain unverified structured text; The verification agent is called, and a syntax checking tool is used to perform syntax checking and compilation on the unverified structured text to obtain corresponding compilation feedback information. If the compilation fails, the error correction agent is called; if the compilation succeeds, the programming task description and the unverified structured text are converted into specification generation prompt words, and the specification generation prompt words are used to prompt the large model to obtain a specification set. The specification set and the unverified structured text are formally verified using a formal verification tool to obtain corresponding formal verification feedback information. If the verification succeeds, the structured text is output; If the verification fails, the error correction agent is called; wherein, the error correction agent converts the programming task description, programming task solution steps, compilation feedback information or formal verification feedback information into error correction prompt words, prompts the large model through the error correction prompt words to obtain a repair patch, and obtains the repaired unverified structured text based on the repair patch, and continues to call the verification agent.
2. The method according to claim 1, characterized in that Build a knowledge base of solutions to PLC programming tasks, including: Obtain structured text programming corpus of programmable logic controllers by searching the Internet and reading relevant books; Programming question-answer pairs are extracted from the programmable logic controller structured text programming corpus and are denoted as ,in Indicates the Programming Q&A pairs, Indicates the Programming task description for a programming question-answer pair, Indicates the Solutions to programming Q&A pairs; Use text embedding models to describe the programming task of each programming question-answer pair The data are converted into semantic vectors and stored in a vector database, and an index is established to complete the construction of a knowledge base for programmable logic controller programming task solutions.
3. The method according to claim 1, characterized in that Relevant question-answer pairs are retrieved from the programmable logic controller programming task solution knowledge base according to the programming task description, including: Taking the programming task description as input, the knowledge retrieval agent retrieves a list of programming task descriptions related to the current programming task description in the programmable logic controller programming task solution knowledge base, denoted as ,in Indicates the Programming task description for a programming question-answer pair, ; The retrieved programming task description is paired with the corresponding solution according to the index, and the knowledge retrieval agent returns the relevant question-answer pair, which is recorded as ,in Indicates the A programming question and answer pair.
4. The method according to claim 1, wherein The programming task description, related question-answer pairs, and programming task solution steps are converted into structured text to generate prompt words, including: Based on the programming knowledge of programmable logic controllers, key information such as structured text grammatical rules and restrictions is summarized, and then combined with the previous programming task description, relevant question and answer pairs, and programming task solution steps to transform it into structured text to generate prompt words.
5. The method according to claim 1, wherein The unverified structured text is repaired according to the repair patch, and the verification agent is continued to be called, including: Summarize the programming task description, programming task solution steps, repair patch and unverified structured text into prompt words, thereby prompting the large model to obtain the repaired unverified structured text; If the verification agent has never been called, set the number of times the verification agent is called ; If the verification agent needs to be called and the verification agent calls the number of times , then the number of agent calls will be verified Add one; if the verification agent needs to be called and the verification agent calls the number of times , then the output is structured text; The maximum number of times to call the verification agent.
6. A multi-agent based programmable logic controller structured text generation device, characterized in that: include: A first building module is used to build a knowledge base of programmable logic controller programming task solutions; The second building block is used to design prompt words, call the large language model, and use external tools to build five large model agents, including a knowledge retrieval agent, a task planning agent, a structured text generation agent, a verification agent, and an error correction agent. A knowledge retrieval module is configured to receive an input programming task description, invoke the knowledge retrieval agent, and retrieve relevant question-answer pairs from the programmable logic controller programming task solution knowledge base according to the programming task description; A task planning module is used to call the task planning agent, convert the programming task description and related question-answer pairs into task planning prompt words, and use the task planning prompt words to prompt the large model to obtain the programming task solution steps; a code generation module, configured to call the structured text generation agent, convert the programming task description, related question-answer pairs, and programming task solution steps into structured text generation prompt words, and prompt the large model with the structured text generation prompt words to obtain unverified structured text; A verification and error correction module is used to call the verification agent, use a grammar checking tool to perform grammar checking and compile the unverified structured text, obtain corresponding compilation feedback information, and call the error correction agent if the compilation fails; if the compilation is successful, convert the programming task description and the unverified structured text into specification generation prompt words, use the specification generation prompt words to prompt the large model to obtain a specification set, use a formal verification tool to perform formal verification on the specification set and the unverified structured text, obtain corresponding formal verification feedback information, and output the structured text if the verification is successful; If the verification fails, the error correction agent is called; wherein, the error correction agent converts the programming task description, programming task solution steps, compilation feedback information or formal verification feedback information into error correction prompt words, prompts the large model through the error correction prompt words to obtain a repair patch, and obtains the repaired unverified structured text based on the repair patch, and continues to call the verification agent.
7. An electronic device, characterized in that: include: one or more processors; a memory for storing one or more programs; When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 5.
8. A computer-readable storage medium having computer instructions stored thereon, characterized in that: When the instruction is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.
Citation Information
Patent Citations
Large language model automatic penetration testing method based on multiple agents
CN119025878A
Code generation method based on demand refinement and consistency verification
CN119045831A
PLC code generation and verification method based on large language model
CN119201059A
Cited By
Intelligent agent-based applet code automatic generation system and control method thereof
CN120653231A
A Mini Program Code Automatic Generation System Based on Intelligent Agents and Its Control Method
CN120653231B
Table-to-text generation method based on multi-agent collaboration
CN120724976A
Intelligent instruction execution method, system and equipment and medium
CN121742820A
Method for generating ladder diagram of programmable controller based on large language model
CN122018423A