A method and system for compiling and generating prompt words for large language models

By constructing an abstract syntax tree through the PDL reduction and compilation mechanism and performing semantic verification and structural optimization, the problem of low efficiency in the design of prompt words for large language models is solved, and high-quality, executable prompt word generation is achieved, which is suitable for multi-domain and multi-task scenarios.

CN120848895BActive Publication Date: 2026-01-30BEIJING WENYIN INTERNET TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511109784.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-08-08
Publication Date
2026-01-30
Estimated Expiration
2045-08-08

AI Technical Summary

Technical Problem

Existing technologies lack a general-purpose prompt word compilation framework for large language models. Prompt word design relies on experience, resulting in low efficiency and poor transferability, making it difficult to adapt to complex scenarios in multiple domains and tasks.

Method used

It adopts a compilation mechanism based on PDL specification, constructs an abstract syntax tree (AST) through lexical and syntactic analysis, and generates high-quality, executable prompt words by combining semantic verification and structural optimization.

Benefits of technology

It improves the efficiency and quality of prompt word development, achieves multi-model adaptation and cross-task flexibility, reduces development and maintenance costs, and is suitable for various application scenarios such as intelligent dialogue and document processing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120848895B_ABST
    Figure CN120848895B_ABST
Patent Text Reader

Abstract

This invention relates to the fields of artificial intelligence and natural language processing, and particularly to a method and system for compiling and generating prompt words for large language models. The method includes responding to a compilation call request and receiving a PDL specification as the compilation input text; performing lexical and syntactic analysis on the compilation input text, and constructing a corresponding Abstract Syntax Tree (AST) as an intermediate representation using a context-free method; performing semantic verification and structural correction on the AST, and performing structural optimization when the semantics are valid to generate an optimized AST; traversing the optimized AST, matching prompt word templates in the prompt word generation rule base, mapping semantic nodes to structured text fragments, concatenating the structured text fragments to form the final prompt word text, and outputting a compilation result consistent with the semantics of the PDL specification. By introducing a PDL-oriented compilation mechanism, the method achieves automated parsing of the input specification, structural optimization, and high-quality prompt word generation before prompt word generation for large language models, improving the efficiency of prompt word development and output quality.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the field of artificial intelligence and natural language processing, and in particular to a prompt word compiling and generating method and system for large language models. BACKGROUND

[0002] With the continuous development of natural language processing and artificial intelligence technology, large-scale pre-training language models (LLM) have shown significant capabilities in tasks such as text generation, dialogue question answering, information extraction, and code interpretation. In order to effectively control the output behavior of LLM, developers need to design prompts as input guides. The structure, wording, and context organization of the prompt have a decisive influence on the quality of the model output. Therefore, prompt construction and optimization have become a key technical link in the application landing process of LLM.

[0003] Current prompts mainly rely on developers to manually write based on experience and are optimized through multiple rounds of trial and error. Although there are tools such as prompt templates and prompt libraries to assist in design, there are still problems such as strong structural rigidity, weak adaptability, and difficulty in covering complex scenarios, making it difficult to meet the high requirements for semantic integrity, format specification, and logical consistency of prompts in multiple domains and tasks. In addition, some research attempts to introduce automated methods such as reinforcement learning and model self-optimization to improve the efficiency of prompt generation, but most of them are at the strategy level and lack a unified modeling and generation framework.

[0004] In summary, the existing technology has not established a general prompt compiling framework for large language models, and lacks a complete technical path for structuring, semantic analysis, optimization conversion, and generating LLM executable prompts for user high-level semantic intent, including task goals, context data, style requirements, and output constraints. SUMMARY

[0005] To solve the above problems, the present application aims to provide a prompt compiling and generating method and system for large language models to automatically convert user high-level semantic requirements into structured, executable, and adaptable high-quality prompts for multiple models, thereby overcoming the defects of prompt design relying on experience, low efficiency, and poor transferability in the prior art. The above invention purpose of the present application is realized by the following technical solutions:

[0006] The present application provides a prompt compiling and generating method for large language models, comprising:

[0007] Step S1: In response to a compiling call request, receiving PDL specification containing task goals, context information, and constraint information as compiling input text;

[0008] Step S2: Perform lexical and syntax analysis on the compiled input text, and construct the corresponding abstract syntax tree AST as an intermediate representation according to the context-free method;

[0009] Step S3: Based on the preset semantic rule set and symbol table, perform semantic checking and structure correction on the AST, and when the semantics are valid, perform structure optimization including context redundancy merging, constant folding, and model adaptability structure rewriting to generate an optimized AST;

[0010] Step S4: Traverse the optimized AST, match the prompt word templates in the prompt word generation rule library, map the semantic nodes to structured text segments, and concatenate the structured text segments to form the final prompt word text. Output the compilation result consistent with the PDL semantic specification to drive large language model task execution.

[0011] Further, step S1 includes:

[0012] In response to a compilation call request, enter the compilation preparation process;

[0013] Receive the compilation input text written in the predefined PDL specification, which includes task objectives, context information, and constraint information. The constraint information includes the output language style, content format requirements, and optional target model specific instructions.

[0014] Further, step S2 includes:

[0015] The PDL specification is read and processed character by character by the lexical analyzer, and based on the predefined lexical rule set, the PDL specification is parsed into non-divisible lexical units Token. Each Token is labeled and identified by type, and Token includes semantic keywords for dividing semantic structures, corresponding identifiers, and string values associated with identifiers. The semantic keywords include TASK for task objectives, CONTEXT for context information, and CONSTRAINT for constraint conditions.

[0016] The identified Token is grouped into a Token sequence according to the logical order in the original PDL specification, which is used as input for the syntax analyzer;

[0017] The syntax analyzer performs structured matching on the Token sequence based on the context-free grammar, identifies the syntax structure units and their nesting relationships, and reduces the Token sequence to the syntax nodes corresponding to the syntax rules in the structured matching process, and constructs the corresponding AST;

[0018] Each syntax node in the AST is classified as a task node, a context node, or a constraint node, and the semantic mapping information in the PDL specification is preserved. The constructed AST is output as an intermediate representation for compilation.

[0019] Further, step S3 comprises:

[0020] Traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and build a symbol table; the attribute information includes the name, type, scope, and default value of the identifier;

[0021] Based on the symbol table and combined with the preset semantic rule set, the semantic analyzer performs semantic checking on each syntax node of the AST structure; semantic checking includes type matching checking, scope resolution checking, and constraint consistency checking of identifiers;

[0022] When the semantic checking passes, the AST is input to the structure optimizer for structure modification and optimization, and the optimized AST is output;

[0023] When the semantic checking fails, semantic error information is generated and the compilation process is terminated.

[0024] Further, in step S3, the AST is input to the structure optimizer for structure modification and optimization, and the optimized AST is output, comprising:

[0025] Identify context nodes with semantic redundancy or structural duplication in the AST, and perform node merging according to the merging rules;

[0026] For constant expression substructures in the AST, perform operation or replacement processing in advance to complete constant folding;

[0027] According to the preset model adaptability rewriting rules, the structures in the AST that are not conducive to language model parsing are replaced semantically equivalently, and the AST after structure optimization is output.

[0028] Further, step S4 comprises:

[0029] Traverse each syntax node in the optimized AST, identify the corresponding semantic type, and retrieve the prompt word template corresponding to the semantic type from the preset prompt word generation rule library;

[0030] Map the field values in each syntax node to the corresponding structured text segment according to the prompt word template format;

[0031] According to the logical structure and node order of the AST, sequentially splice to construct a complete prompt word, and after format preprocessing, output the final prompt word text consistent with the PDL specification semantics as the input of the large language model task execution.

[0032] Based on the same inventive concept, the present application also provides a prompt word compiling and generating system for a large language model, which executes the prompt word compiling and generating method for a large language model as described above, comprising,

[0033] a user interface module, configured to receive a PDL specification containing a task target, context information and constraint information as a compilation input in response to a compilation invocation request;

[0034] a parsing module, configured to perform lexical and syntax analysis on the PDL specification, and construct a corresponding abstract syntax tree (AST) as an intermediate representation according to a context-free method;

[0035] an optimization module, configured to perform semantic checking and structural correction on the AST based on a preset semantic rule set and a symbol table, and when the semantics are valid, perform structural optimization including context redundancy merging, constant folding and model adaptability structural rewriting, and generate an optimized AST;

[0036] a generating module, configured to traverse the optimized AST, match a prompt word template in a generating rule library, and map semantic nodes to structured text segments;

[0037] an output module, configured to splice the structured text segments to form a final prompt word text, output a compilation result consistent with the semantics of the PDL specification, and drive a large language model task execution.

[0038] Further, the PDL specification includes a task target, context information and constraint information; the constraint information includes an output language style, content format requirements and optional target model specific instructions.

[0039] Further, the parsing module comprises:

[0040] a lexical analysis unit, configured to perform character-by-character reading processing on the PDL specification, and simultaneously parse the PDL specification into indivisible lexical units Token based on a predefined lexical rule set, mark the type and identify the content of each Token, the Token includes semantic keywords for dividing semantic structures, corresponding identifiers and string values associated with the identifiers, the semantic keywords include a TASK representing a task target, a CONTEXT representing context information and a CONSTRAINT representing constraint conditions; and the identified Token is grouped into a Token sequence according to the logical order in the original PDL specification, serving as an input of a syntax analyzer;

[0041] The syntax analysis unit is configured to perform structural matching on the Token sequence based on a context-free grammar, identify syntax structure units and their nesting relationship, and sequentially reduce the Token sequence to syntax nodes corresponding to syntax rules in the structural matching process, and construct an AST corresponding to the syntax nodes.

[0042] Further, the optimization module comprises:

[0043] The symbol table construction unit is configured to traverse the syntax nodes in the AST, extract identifiers and their associated attribute information defined in the PDL specification, and construct a symbol table; the attribute information includes the name, type, scope and default value of the identifier.

[0044] The semantic analysis unit is configured to perform semantic verification on each syntax node of the AST structure based on the symbol table and in combination with a preset semantic rule set; the semantic verification includes type matching verification, scope resolution verification and constraint consistency verification; when the semantic verification passes, the AST is input to the structure optimizer for structure modification and optimization, and an optimized AST is output; when the semantic verification fails, semantic error information is generated and the compilation process is terminated.

[0045] Compared with the prior art, the present application has at least one of the following beneficial effects:

[0046] The present application introduces a PDL-oriented compilation mechanism to realize automatic parsing, structural optimization and high-quality prompt word generation of input specifications before large language model prompt word generation, and improves the development efficiency and output quality of prompt words. Therefore, compared with the existing prompt word design method which relies on manual writing, the present application has the following beneficial effects: (1) greatly reduces the development and maintenance cost of prompt words, significantly improves the engineering efficiency and application deployment speed; (2) through semantic verification and structural optimization, the consistency, controllability and logical accuracy of the generated prompt words are improved; (3) through the abstract syntax tree (AST) and rule-driven mechanism, flexible adaptation to various large language models and task types is realized, and good universality and expansibility are achieved; (4) widely used in intelligent dialogue, document processing, natural language programming and other application scenarios, and has certain commercial value and promotion prospect. BRIEF DESCRIPTION OF DRAWINGS

[0047] Figure 1 A step flowchart of the prompt word compilation and generation method for large language models according to the present application;

[0048] Figure 2 A prompt word generation compilation flowchart based on a PDL specification in an embodiment of the present application. DETAILED DESCRIPTION

[0049] In order to make the purposes, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are only some but not all of the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work fall within the scope of protection of the present application.

[0050] It can be understood by those skilled in the art that, unless specifically stated, the singular forms "a", "an" and "the" used herein include plural forms. It should be further understood that the use of the term "including" in the specification of the present application means that the features, integers, steps, operations, elements and / or components described exist, but do not exclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof.

[0051] First embodiment

[0052] In the prior art, the construction of prompt words mainly depends on domain knowledge, manual experience and repeated debugging. Since the LLM is highly sensitive to the selection of wording, the arrangement of structure and the organization of context in the prompt input, the design of prompt words becomes a task with high threshold, high cost, low efficiency and difficult to scale. Although some solutions try to introduce prompt templates or prompt word libraries to reduce the cost of writing by using preset structures or example prompts, there are still the following limitations: the construction and updating process of prompt templates rely on manual experience and lack intelligent optimization ability; although the prompt word library can provide retrieval and reduce repetitive labor to some extent, how to automatically match task intent and adapt to context still needs manual decision and still relies on manual judgment and multiple experiments, which does not fundamentally solve the problem; the above methods are difficult to adapt to the prompt construction requirements in complex context of cross-task, cross-domain and multi-modal.

[0053] Further, some frontier researches have begun to explore automated prompt optimization methods, such as using reinforcement learning or evolutionary algorithms to iteratively fine-tune prompts, or using the reasoning ability of LLM itself to gradually rewrite initial prompts. These methods are mostly at the strategy level and fail to provide a systematic and engineered compilation framework. In addition, in some specific field patent documents, there are also technical solutions for generating interactive dialogues using pre-defined scripts in dialogue systems or intelligent customer service scenarios. However, these solutions are usually tightly coupled with specific business logic, and their design ideas and technical architectures lack generality, making them unable to support general LLM-oriented and cross-domain prompt writing requirements, especially lacking a unified framework for structuring, compiling, and optimizing different types of instructions, complex context information, and multi-level constraints.

[0054] Based on the deep understanding of the existing prompt construction relying on manual work, low efficiency, and lack of scalability, the inventors propose a prompt automatic generation method based on a formalized compilation system. By designing a prompt description language PDL to express user requirements, and building a complete compilation process covering lexical analysis, syntax analysis, semantic verification, and structure optimization, the automatic conversion from abstract semantic intent to structured and high-quality prompts is realized, improving the accuracy, stability, and generality of prompt generation, and breaking through the limitations of traditional manual design in efficiency and adaptability. By constructing a compiler-like prompt automatic generation method, the user intent is described in PDL language, and high-quality prompts that can be input to LLM are generated through a structured compilation process, thereby solving the problems of high manual cost, poor adaptability, and difficult quality control in traditional prompt design, and promoting the prompt engineering into an automated and scalable new stage.

[0055] To achieve the above-mentioned purpose, the present application discloses a large language model prompt compilation method and its corresponding execution system. The method is independently completed by a single compilation subject, such as a compilation system deployed on a server or a user device. The core is to draw on and modify the principles of traditional program language compilers, build a compilation process for natural language processing tasks, and convert user input abstract requirements into executable natural language instructions that have been structurally optimized, semantically enhanced, and adapted to specific large language models. The specific implementation is as follows:

[0056] As shown in Figure 1 , 2 , the present application provides a prompt compilation and generation method for large language models, including:

[0057] Step S1: In response to a compilation call request, receive PDL specification containing task target, context information, and constraint information as compilation input text; wherein step S1 includes:

[0058] In response to a compilation call request, enter the compilation preparation process;

[0059] receiving a compilation input text written in a predefined PDL specification, the PDL specification including a task objective, context information and constraint information; the constraint information including a language style of an output, a content format requirement and optionally a target model specific instruction.

[0060] In particular, the compilation process starts with receiving a PDL specification text submitted by a user, which conforms to a predefined prompt description language grammar rule and describes various types of information required for the compilation task in a structured form. In the PDL specification, the task objective part is used to explicitly define the core function or output purpose that the large language model should complete; the context information part is used to provide background knowledge, input data or dialogue history related to the task for the model to understand and reference; the constraint information part includes requirements for the output language style, such as formal, concise, creative, etc., content structure, such as JSON format, Markdown list, code snippet, etc., and calling instructions of the adapted model, such as system prompt, role attribute, etc. Before parsing the PDL text, the compilation system needs to complete the basic environment initialization, specification legality verification and necessary context preprocessing to provide data preparation for subsequent syntax and semantic analysis steps.

[0061] Step S2: performing lexical and syntax analysis on the compilation input text, and constructing an abstract syntax tree (AST) as an intermediate representation according to the context-free grammar; wherein step S2 includes:

[0062] The PDL specification is read and processed character by character by the lexical analyzer, and based on a predefined set of lexical rules, the PDL specification is parsed into non-divisible lexical units (Tokens). Each Token is labeled with a type and its content is identified. Tokens include semantic keywords for dividing semantic structures, corresponding identifiers and string values associated with identifiers. The semantic keywords include TASK for task objective, CONTEXT for context information and CONSTRAINT for constraint condition.

[0063] The identified Tokens are grouped into a Token sequence according to their logical order in the original PDL specification, which is used as input for the syntax analyzer;

[0064] The syntax analyzer performs structured matching on the Token sequence based on the context-free grammar, identifies the syntax structure units and their nesting relationships, and reduces the Token sequence to syntax nodes corresponding to the syntax rules in the structured matching process, constructing the corresponding AST;

[0065] Each syntax node in the AST is classified as a task node, a context node, or a constraint node, and semantic mapping information in the PDL specification is retained, and the completed AST is output as a compilation intermediate representation.

[0066] Specifically, the received PDL specification text is first scanned character by character by a syntax analyzer, and the text content is divided into multiple lexical units Token according to a preset lexical rule set, such as a Token mode defined by a regular expression. Each Token includes a semantic keyword such as TASK, CONTEXT, and CONSTRAINT, an identifier such as a task name or a variable name, and a corresponding string value such as a task description or an input segment. The lexical analyzer is also responsible for attaching a type label to each Token and performing syntax role annotation to provide a basis for subsequent syntax analysis.

[0067] Subsequently, the syntax analyzer (Lexical Analyzer or Scanner) performs a structural reduction operation on the Token sequence based on a set of well-defined context-free grammar CFG rules. This operation includes shift-reduce processing of the Token sequence, detection of legal syntax structures, parsing of nested levels and logical branches, and the like. The final output is an abstract syntax tree AST, which is composed of a number of syntax nodes, each corresponding to a high-level semantic element such as a “task target description node”, a “context injection node”, or an “output constraint node”. The AST structure can completely retain the semantic logic of the original PDL specification and serve as an intermediate representation layer for subsequent semantic analysis and hint generation optimization. Regarding the intermediate representation, each node represents a semantic unit such as an instruction node, a context node, or a constraint node, and the tree structure clearly expresses the parent-child and sibling relationships between them.

[0068] Step S3: Based on the preset semantic rule set and the symbol table, the AST is semantically verified and structurally modified. When the semantics are valid, structural optimization including context redundancy merging, constant folding, and model adaptability structural rewriting is performed to generate an optimized AST. Step S3 includes:

[0069] Traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and construct a symbol table. The attribute information includes the name, type, scope, and default value of the identifier;

[0070] Based on the symbol table and in combination with the preset semantic rule set, the semantic analyzer performs semantic verification on each syntax node of the AST structure. Semantic verification includes type matching verification, scope resolution verification, and constraint consistency verification of identifiers;

[0071] When the semantic check passes, the AST is input to the structure optimizer for structure modification and optimization, and the optimized AST is output.

[0072] When the semantic check fails, semantic error information is generated and the compilation process is terminated.

[0073] Further, in step S3, the AST is input to the structure optimizer for structure modification and optimization, and the optimized AST is output, including:

[0074] Identify the context nodes with semantic redundancy or structural repetition in the AST, and perform node merging according to the merging rules;

[0075] For substructures in the AST that are constant expressions, perform operation or replacement processing in advance to complete constant folding;

[0076] According to the preset model adaptability rewriting rules, the structures in the AST that are not conducive to the analysis of the language model are replaced semantically equivalently, and the AST after structure optimization is output.

[0077] Specifically, after the AST is constructed, the semantic analysis and structure optimization phase is entered. First, the AST is traversed from top to bottom, all identifiers defined in the PDL reduction are identified, and their name, type, scope, default value and other attribute information are extracted to build a symbol table as the context basis for subsequent semantic checking. Subsequently, the semantic analyzer performs semantic checking on each syntax node in the AST according to the preset semantic rule set. The checking process includes: ① type matching check to ensure that each identifier assignment or reference conforms to its declared type; ② scope resolution verification to ensure that all used identifiers are defined in their legal scope; ③ constraint consistency check to ensure that there is no logical conflict between output format, style, structure and other constraints. If a semantic error is found, the system will record the error information and terminate the compilation process. After all the semantic checks pass, the structure optimizer starts to optimize the AST. The optimization includes but is not limited to the following strategies: (1) context redundancy merging: identify repeated or semantically equivalent context injection nodes in the AST, and perform node merging according to the merging rules to reduce redundant context nodes; (2) constant folding: for statically resolvable substructures such as fixed input format and constant variables, calculate their results in advance and replace them with specific values to simplify the subsequent generation process; (3) model adaptability structure rewriting: according to the predefined structure rewriting rule library, replace AST structures that are not conducive to the processing of the target LLM, such as long nesting and inefficient prompt structure, with semantically equivalent but more optimal versions, to improve the prompt word resolvability and response stability. After optimization, the structure-optimized AST is output as the input for the subsequent generation phase.

[0078] Step S4: traversing the optimized AST, matching the prompt word templates in the prompt word generation rule library, mapping the semantic nodes into structured text segments, splicing the structured text segments to form the final prompt word text, outputting the compilation results consistent with the PDL regulation semantics, and driving the large language model task execution. Wherein, step S4 includes:

[0079] Traverse each syntax node in the optimized AST, identify the corresponding semantic type, and retrieve the prompt word template corresponding to the semantic type from the pre-set prompt word generation rule library;

[0080] Map the field values in each syntax node to the corresponding structured text segment according to the prompt word template format;

[0081] According to the logical structure and node order of AST, sequentially splice, construct complete prompt words and perform format preprocessing to output the final prompt word text consistent with the PDL regulation semantics as the input of the large language model task execution.

[0082] Specifically, after the structure optimization is completed, the generator receives the optimized AST as input, and according to the pre-set traversal strategy such as depth-first traversal, the syntax tree is accessed and processed. When the generator accesses a semantic node in the AST, the system will call the built-in prompt word generation rule library, and select the matching generation template from it according to the type, attribute value and semantic context of the node. The generation template defines how to map the structured semantic node to the text segment of natural language expression. For example, if there is a node of type "task target" in the AST, and its value is "summary generation", the generator will match the language template similar to "Please complete the summary task according to the following information:" and generate the guide language accordingly. For example, when a constraint node with output format "JSON" is detected, the system will generate the corresponding text prompt, such as "Please ensure that your answer is a JSON object that strictly follows the RFC 8259 standard". In the text segment splicing stage, the generator not only concatenates the natural language expressions corresponding to the nodes, but also embeds specific starting markers, structured example prompts such as Few-shot format, role separation statements, format control annotations, etc. according to the format requirements of the target LLM, so that the generated prompt word is complete in semantics, clear in structure, and conforming to the input requirements of the target large language model in pragmatics. All text segments are integrated into a linear structure of prompt word string after format arrangement and content splicing, which is the high-quality prompt word output result consistent with the PDL regulation semantics and can be directly used to drive the LLM task execution.

[0083] Meanwhile, based on the original prompt word compiling scheme, the embodiment can further support incremental and dynamic compiling mechanisms to meet the efficient response needs of multi-round dialogue and other context evolution scenarios. Specifically, based on the AST structure and symbol table of the historical dialogue, when the user input is newly added or modified, a complete recompilation process is not performed, but only the AST child nodes affected by the changes are identified, and the local subtree is updated, optimized, and regenerated, thereby realizing on-demand allocation and dynamic adaptation of compiling resources. This process can be compared to JIT compilation, which significantly improves the real-time performance and computational efficiency of prompt word generation in complex dialogue flows while ensuring semantic consistency.

[0084] Second embodiment

[0085] Based on the same inventive concept, the present application also provides a prompt word compiling and generating system for large language models, which executes the prompt word compiling and generating method for large language models as described above, comprising,

[0086] a user interface module for receiving a PDL specification containing task objectives, context, and constraint information as a compilation input in response to a compilation call request;

[0087] a parsing module for performing lexical and syntax analysis on the PDL specification and constructing a corresponding abstract syntax tree (AST) as an intermediate representation according to the context-free method;

[0088] an optimization module for performing semantic checking and structural correction of the AST based on a preset semantic rule set and symbol table, and when the semantics are valid, performing structural optimization including context redundancy merging, constant folding, and model adaptability structure rewriting to generate an optimized AST;

[0089] a generation module for traversing the optimized AST, matching prompt word templates in the generation rule library, and mapping semantic nodes to structured text segments;

[0090] an output module for concatenating structured text segments to form a final prompt word text, outputting a compilation result consistent with the semantics of the PDL specification, and driving large language model task execution.

[0091] Further, the PDL specification includes task objectives, context information, and constraint information; the constraint information includes output language style, content format requirements, and optional target model specific instructions.

[0092] Further, the parsing module comprises:

[0093] The lexical analysis unit is configured to perform character-by-character reading processing on the PDL specification, and parse the PDL specification into non-divisible lexical units Token based on a predefined lexical rule set, and mark the type and identify the content of each Token, wherein the Token includes semantic keywords for dividing semantic structures, corresponding identifiers and string values associated with the identifiers, and the semantic keywords include a TASK representing a task target, a CONTEXT representing context information and a CONSTRAINT representing a constraint condition; and the identified Token is grouped into a Token sequence according to a logical order in the original PDL specification, and used as an input of the syntax analyzer.

[0094] The syntax analysis unit is configured to perform structured matching on the Token sequence based on a context-free grammar, identify a syntax structure unit and a nested relationship thereof, and sequentially reduce the Token sequence into syntax nodes corresponding to syntax rules in the structured matching process, and construct an AST corresponding to the syntax nodes.

[0095] Further, the optimization module includes:

[0096] The symbol table construction unit is configured to traverse the syntax nodes in the AST, extract identifiers defined in the PDL specification and attribute information associated with the identifiers, and construct a symbol table; the attribute information includes a name, a type, a scope and a default value of the identifier.

[0097] The semantic analysis unit is configured to perform semantic verification on the syntax nodes of the AST structure based on the symbol table and in combination with a preset semantic rule set; the semantic verification includes type matching verification, scope resolution verification and constraint consistency verification of the identifiers; when the semantic verification is passed, the AST is taken as an input to the structure optimizer for structure modification and optimization, and an optimized AST is output; and when the semantic verification is not passed, semantic error information is generated and the compiling process is terminated.

[0098] The above only describes preferred embodiments of the present application, and the protection scope of the present application is not limited to the above-described embodiments. Any technical solution falling within the concept of the present application shall fall within the protection scope of the present application. It should be noted that, for ordinary skilled persons in the art, some improvements and refinements without departing from the principles of the present application shall also be considered as falling within the protection scope of the present application.

[0099] It should be noted that the above embodiments can be freely combined as needed. The above only describes the preferred embodiments of the present application, and it should be pointed out that for those skilled in the art, without departing from the principles of the present application, a number of improvements and refinements can be made, which should also be considered as the protection scope of the present application.

Claims

1. A prompt compilation and generation method for a large language model, characterized in that, Comprise: Step S1: in response to a compilation call request, receiving a PDL specification containing task target, context information and constraint information as a compilation input text; Step S2: performing lexical and syntax analysis on the compilation input text, and constructing a corresponding abstract syntax tree AST as an intermediate representation according to the context-free grammar; wherein, step S2 comprises: Through the lexical analyzer, the PDL specification is processed character by character, and the PDL specification is parsed into non-divisible lexical units Token based on a predefined set of lexical rules, each Token is type labeled and content identified, the Token includes semantic keywords for dividing semantic structure, corresponding identifiers and string values associated with the identifiers, and the semantic keywords include TASK representing the task target, CONTEXT representing the context information and CONSTRAINT representing the constraint condition; The identified Token is grouped into a Token sequence according to the logical order in the original PDL specification, which is used as input for the syntax analyzer; The syntax analyzer performs structured matching on the Token sequence based on the context-free grammar, identifies the syntax structure units and their nesting relationship, and reduces the Token sequence to the syntax nodes corresponding to the syntax rules in turn during the structured matching process, and constructs the corresponding AST; Each syntax node in the AST is classified as a task node, a context node or a constraint node, and the semantic mapping information in the PDL specification is retained, and the constructed AST is output as a compilation intermediate representation; Step S3: based on a preset semantic rule set and a symbol table, the AST is semantically verified and structurally modified, when the semantics are valid, the structure optimization including context redundancy merging, constant folding and model adaptability structure rewriting is performed, and an optimized AST is generated; Step S4: traversing the optimized AST, matching the prompt word templates in the prompt word generation rule library, mapping the semantic nodes to structured text segments, concatenating the structured text segments to form the final prompt word text, outputting the compilation result consistent with the semantics of the PDL specification, and driving the large language model task execution.

2. The method of claim 1, wherein, Step S1 comprises: In response to the compilation call request, enter the compilation preparation process; Receiving the compilation input text written in the predefined PDL specification, the PDL specification including the task target, context information and constraint information; the constraint information including the output language style, content format requirement and optional target model specific instruction.

3. The method of claim 1, wherein, Step S3 comprises: Traverse the syntax nodes in the AST, extract the identifiers defined in the PDL specification and their associated attribute information, and construct a symbol table; the attribute information includes the name, type, scope and default value of the identifier; The semantic analyzer performs the semantic check on each syntax node of the AST based on the symbol table and in combination with the preset semantic rule set; the semantic check includes type matching check, scope resolution check, and constraint consistency check of the identifiers; When the semantic check passes, the AST is input to the structure optimizer for structure revision and optimization, and the optimized AST is output; When the semantic check fails, semantic error information is generated and the compiling process is terminated.

4. The method of claim 3, wherein, In step S3, the AST is input to the structure optimizer for structure revision and optimization, and the optimized AST is output, including: Identifying context nodes with semantic redundancy or structural repetition in the AST, and performing node merging according to merging rules; For substructures that are constant expressions in the AST, operation or replacement processing is performed in advance to complete constant folding; According to the preset model adaptability rewriting rule, the structure in the AST that is not conducive to language model parsing is replaced semantically equivalently, and the AST after structure optimization is output.

5. The method of claim 4, wherein, Step S4 includes: Traverse each syntax node in the optimized AST, identify the corresponding semantic type, and retrieve the prompt word template corresponding to the semantic type from the preset prompt word generation rule library; Map the field values in each syntax node to the corresponding structured text fragments according to the prompt word template format; According to the logical structure and node order of the AST, sequentially splice to construct a complete prompt word, and perform format preprocessing to output the final prompt word text consistent with the PDL specification semantics as the input of the large language model task execution.

6. A prompt compilation and generation system for a large language model, performing the prompt compilation and generation method for a large language model according to any one of claims 1 to 5, characterized in that, including, A user interface module for receiving a PDL specification containing task objectives, context, and constraint information as a compilation input in response to a compilation call request; An analysis module for performing lexical and syntax analysis on the PDL specification, and constructing a corresponding abstract syntax tree (AST) as an intermediate representation according to a context-free grammar; An optimization module for performing semantic checking and structure revision on the AST based on a preset semantic rule set and symbol table, and performing structure optimization including context redundancy merging, constant folding, and model adaptability structure rewriting when the semantics are valid, to generate an optimized AST; A generation module for traversing the optimized AST and matching prompt word templates in a generation rule library to map semantic nodes to structured text fragments; An output module for splicing the structured text fragments to form a final prompt word text, and outputting a compilation result consistent with the PDL specification semantics for driving large language model task execution.

7. The prompt compilation and generation system of claim 6, wherein, The PDL specification includes the task objectives, context information, and constraint information; the constraint information includes the output language style, content format requirements, and optional target model specific instructions.

8. The prompt compilation and generation system of claim 7, wherein, The analysis module includes: a lexical analysis unit configured to perform character-by-character reading on the PDL specification, and parse the PDL specification into non-divisible lexical units (Tokens) based on a predefined lexical rule set, each Token being labeled with a type and having a content, the Tokens including semantic keywords for dividing semantic structures, corresponding identifiers, and string values associated with the identifiers, the semantic keywords including a TASK keyword representing a task target, a CONTEXT keyword representing context information, and a CONSTRAINT keyword representing a constraint condition; and form a Token sequence by arranging the identified Tokens in a logical order in the original PDL specification, for serving as an input of a syntax analysis unit; the syntax analysis unit configured to perform structured matching on the Token sequence based on the context-free grammar, identify syntax structure units and their nesting relationships, and reduce the Token sequence into syntax nodes corresponding to syntax rules in a sequential manner during the structured matching, to construct the AST; each syntax node in the AST is classified as a task node, a context node, or a constraint node, and semantic mapping information in the PDL specification is retained, and the constructed AST is output as a compilation intermediate representation.

9. The prompt compilation and generation system of claim 8, wherein, The optimization module includes: a symbol table construction unit configured to traverse the syntax nodes in the AST, extract the identifiers and their associated attribute information defined in the PDL specification, and construct a symbol table; the attribute information includes a name, a type, a scope, and a default value of the identifier; a semantic analysis unit configured to perform the semantic verification on each syntax node of the AST based on the symbol table and in combination with the preset semantic rule set; the semantic verification includes type matching verification, scope resolution verification, and constraint consistency verification; when the semantic verification passes, the AST is input into a structure optimizer for structure modification and optimization, and the optimized AST is output; when the semantic verification fails, semantic error information is generated and the compilation process is terminated.