Text processing method and device, equipment, storage medium and program product
By pre-configuring grammar rules to verify and combine the text units generated by the large language model, the problem of non-compliant output of the large language model is solved, and efficient and accurate text generation is achieved to meet industry compliance requirements.
Patent Information
- Application Number
- CN202510202318.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-21
- Publication Date
- 2025-09-26
AI Technical Summary
Large language models have problems with randomness, hallucinations, and content violations in text generation, resulting in output content that does not meet rule requirements, and post-processing compliance verification is inefficient and costly.
Text units are verified and combined through pre-configured grammar rules, and text is generated using a large language model to form target information that meets the constraint requirements. A stack-like data structure is used to achieve fast verification and compliant output.
Improves the accuracy and efficiency of text generation, ensures that output content meets industry requirements, reduces the probability of illegal content, and reduces post-processing delays and costs.
Smart Images

Figure CN120706534A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence, and in particular to a text processing method, apparatus, device, storage medium, and program product. Background Art
[0002] In areas where there are strict requirements for the content of published information, it is necessary to conduct rule-based verification of the content to be output before publishing the information. Related technologies fine-tune large language models or post-process the format of the output compliant data. Although large language models can greatly improve the output quality of the model and meet industry requirements, the text generation of large language models has problems such as randomness, hallucinations, and content violations, resulting in ineffective corrections. Alternatively, after outputting content to the application end, the application end confirms the compliance of the content through algorithmic strategies and outputs content that complies with the rules. For content that does not comply with the rules, it corrects the error, calls it again, or outputs default content. This post-processing rule verification will increase response delays, and the cost of repeated calls is high and the efficiency is low. Summary of the Invention
[0003] The embodiments of the present application provide a text processing method, apparatus, device, storage medium, and program product that can improve the accuracy and output efficiency of target information.
[0004] The technical solution of the embodiment of the present application is implemented as follows:
[0005] This embodiment of the present application provides a text processing method, the method comprising:
[0006] Performing text generation processing on the first prompt word to obtain a first text unit;
[0007] Performing verification processing on the first text unit based on preconfigured grammatical rules, and determining a second text unit based on a portion of the first text unit that passes the verification;
[0008] combining the first prompt word and the second text unit into a second prompt word;
[0009] performing text generation processing on the second prompt word to obtain a third text unit;
[0010] The third text unit is decoded to obtain target information.
[0011] The present invention provides a text processing device, including:
[0012] A generating module, configured to perform text generation processing on the first prompt word to obtain a first text unit;
[0013] The verification module is configured to verify the first text unit based on preconfigured grammatical rules, determine a second text unit based on the verified portion of the first text unit, combine the first prompt word with the second text unit to form a second prompt word, perform text generation processing on the second prompt word to obtain a third text unit, and decode the third text unit to obtain target information.
[0014] An embodiment of the present application provides an electronic device, comprising:
[0015] a memory for storing computer-executable instructions or computer programs;
[0016] The processor is used to implement the text processing method provided in the embodiment of the present application when executing the computer-executable instructions or computer programs stored in the memory.
[0017] An embodiment of the present application provides a computer-readable storage medium storing a computer program or computer-executable instructions for implementing the text processing method provided in the embodiment of the present application when executed by a processor.
[0018] An embodiment of the present application provides a computer program product, including a computer program or computer-executable instructions. When the computer program or computer-executable instructions are executed by a processor, the text processing method provided in the embodiment of the present application is implemented.
[0019] The embodiments of the present application have the following beneficial effects:
[0020] The first prompt word is processed for text generation to obtain multiple first text units. These multiple first text units are then verified based on preconfigured grammatical rules. The verified portion of the first text dimension is used as the second text unit. This verification process can screen out text units that meet grammatical specifications, improving the overall text quality. The first prompt word is combined with the second text unit to form a second prompt word. The second prompt word is processed for text generation to obtain a third text unit. The verified text unit is used as the new prompt word. This can more accurately indicate the next round of text generation, reduce redundant information in subsequent steps, and improve processing efficiency. The third text unit is decoded to obtain target information, improving the relevance and accuracy of the target information and the prompt word. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] Figure 1 This is a schematic diagram of an application mode of the text processing method provided in an embodiment of the present application;
[0022] Figure 2 is a structural diagram of an electronic device provided in an embodiment of the present application;
[0023] Figure 3AThis is a first flow chart of the text processing method provided in an embodiment of the present application;
[0024] Figure 3B This is a second flow chart of the text processing method provided in an embodiment of the present application;
[0025] Figure 4 3 is a schematic diagram of a third flow chart of the text processing method provided in an embodiment of the present application;
[0026] Figure 5 It is a structural diagram of the large language model provided in the embodiment of the present application.
[0027] It should be pointed out that the above-mentioned "first" and "second" are only used to distinguish different solutions, and do not represent the degree of distinction between the advantages and disadvantages of the solutions or the priority in the implementation process. DETAILED DESCRIPTION
[0028] In order to make the purpose, technical solutions and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limiting this application. All other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0029] In the following description, reference is made to “some embodiments”, which describes a subset of all possible embodiments, but it will be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0030] In the following description, the terms "first\second\third" involved are merely used to distinguish similar objects and do not represent a specific ordering of the objects. It can be understood that "first\second\third" can be interchanged with a specific order or sequence where permitted, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein.
[0031] In the embodiments of the present application, the term "module" or "unit" refers to a computer program or a part of a computer program that has a predetermined function and works together with other related parts to achieve a predetermined goal, and can be implemented in whole or in part by using software, hardware (such as processing circuits or memories) or a combination thereof. Similarly, a processor (or multiple processors or memories) can be used to implement one or more modules or units. In addition, each module or unit can be part of an overall module or unit that includes the function of the module or unit.
[0032] Unless otherwise defined, all technical and scientific terms used in the embodiments of the present application have the same meanings as those commonly understood by those skilled in the art. The terms used in the embodiments of the present application are only for the purpose of describing the embodiments of the present application and are not intended to limit the present application.
[0033] The collection and processing of relevant data (for example, prompt words and pre-configured grammatical rules) in the embodiments of this application should be strictly in accordance with the requirements of relevant laws and regulations when applied in instances, and the informed consent or separate consent of the personal information subject should be obtained. Subsequent data use and processing should be carried out within the scope of authorization of laws and regulations and the personal information subject.
[0034] Before further explaining the embodiments of the present application in detail, the nouns and terms involved in the embodiments of the present application are explained. The nouns and terms involved in the embodiments of the present application are subject to the following interpretations.
[0035] 1) Large Language Models (LLMs): These are pre-trained language models with large parameter sizes that can understand and generate natural language. These models are typically based on deep learning techniques and are trained on large amounts of text data to learn the statistical laws and patterns of language.
[0036] 2) Constrained Decoding: This refers to applying certain restrictions and rules when decoding and generating content in a large model to ensure that the generated content meets specific requirements or constraints.
[0037] 3) Large Model Hallucination: Large model hallucination occurs when a large model generates inaccurate information or answers. The generated information may appear plausible, but in reality, it contains facts or details that do not exist. When generating text or processing information, large language models may produce inaccurate content or completely inconsistent with reality due to algorithmic imperfections or limited training data. This phenomenon typically occurs when the model generates text based on probabilistic assumptions, but the generated results may differ from expectations or reality.
[0038] 4) Large model violations: This refers to the generation of content by models that bypass model security and ethical constraints, generating inappropriate or unethical content. Large language models, when performing tasks, exceed their design scope or expected behavior, performing unauthorized or harmful operations. This typically occurs when the model is manipulated or its designed security mechanisms are bypassed.
[0039] 5) Fine-tuning: A term used in model training. Based on the pre-trained model, the model is further trained using a smaller dataset so that the model can better adapt to specific tasks and help adjust the characteristics of the pre-trained model.
[0040] 6) Streaming output: A technique used when generating text that allows the model to output text as it is generated, rather than waiting for the entire text to be generated before outputting it all at once. This can improve the response speed of user interactions.
[0041] 7) Backus Normal Form (BNF): A context-free grammar that uses a series of symbols and expressions to create string generation rules. It can be simply defined as three key concepts: terminals, non-terminals, and generation rules.
[0042] 8)Terminator: It can be understood as a basic symbol or lexical symbol. It is irreplaceable, exists naturally, and cannot be generated through grammar rules.
[0043] 9) Nonterminal symbols, also known as syntactic variables, are symbols used in a grammar to represent a grammatical rule or structure. They do not directly correspond to specific symbols in the input string, but rather represent a set of possible structures or symbol strings. Nonterminal symbols can be replaced by other symbol strings in grammar rules. These symbol strings can be more complex structures composed of terminals and nonterminals.
[0044] 10) Generation rules: define how symbols are converted and replaced. The left side of the rule is the rule header, which is the symbol that can be replaced, and the right side is the rule body, which is the specific content. For example: <digit>::="0"|"1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9" is a production rule, and the rule header is <number>, the rule body is <digit> | <digit> <number>, this rule defines that a number can be a single digit character, or a digit character followed by another digit.
[0045] 11) Stack: A Last In First Out (LIFO) data structure that allows insertion and deletion operations at only one end, which is called the top of the stack (Top), and the other end is called the base (Base). Inserting a new element into a stack is also called pushing, pushing, or pushing, which is to place the new element on top of the top element of the stack, making it the new top element; deleting an element from a stack is also called popping or unpopping, which is to delete the top element of the stack and make its adjacent element the new top element. In the embodiment of the present application, the stack used is generated by parsing a constraint file of preconfigured grammatical rules, which is used to match character encoding rules.
[0046] 12) Finite State Machine (FSM): A mathematical model that represents a finite number of states and the transitions and actions between them. A FSM has a finite number of states, each of which can transition to zero or more states. The input string determines which state to transition to.
[0047] In scenarios where rule verification of output content is required, related technologies employ fine-tuning large language models or post-processing to output compliant data in a format. Large language models can improve output quality and meet industry requirements, but they can also introduce issues such as randomness, hallucinations, and content violations. After content is output to the application, the application verifies its compliance using algorithmic policies. This post-processing rule verification increases response latency, makes repeated calls costly, and reduces efficiency.
[0048] Embodiments of the present application provide a text processing method, a text processing device, an electronic device, a computer-readable storage medium, and a computer program product, which can improve the accuracy and output efficiency of target information.
[0049] The following describes exemplary applications of the electronic devices provided in the embodiments of the present application. The devices provided in the embodiments of the present application can be implemented as various types of terminals, such as laptops, tablet computers, desktop computers, set-top boxes, smartphones, smart speakers, smart watches, smart TVs, and in-vehicle terminals. They can also be implemented as servers. The following describes exemplary applications when the devices are implemented as terminals or servers.
[0050] See also Figure 1 , Figure 1 This is a schematic diagram of an application mode of the text processing method provided in an embodiment of the present application, for example, to support a text processing application. Figure 1 The server 200, network 300, terminal device 400 and database 500 are involved. The terminal device 400 is connected to the server 200 via the network 300. The network 300 can be a wide area network or a local area network, or a combination of the two.
[0051] In some embodiments, the user may be a technician in this field, the server 200 is a server for executing the text processing method, the terminal device 400 is a terminal operated by the user, the terminal device 400 is pre-configured with an application for displaying target information, and the database 500 stores prompt words, pre-configured grammar rules and text units.
[0052] For example, the user triggers the terminal device 400 to send the input text to be processed to the server 200 via the network 300. The server 200 converts the text to be processed into a first prompt word for text generation processing, performs verification processing on the generated text unit using the pre-configured grammar rules in the database 500, combines the verified text unit with the verification result into a second prompt word, and the server 200 performs text generation processing on the second prompt word to obtain a generated text unit. The generated text unit is decoded to obtain target information. The server 200 sends the target information to the terminal device 400 via the network 300, and displays the target information that answers the question to be answered on the terminal device 400.
[0053] In some embodiments, the text processing method of the embodiments of the present application can also be applied in the following application scenarios:
[0054] 1. In the scenario of publishing financial industry information: the target information obtained by the text processing method of the embodiment of the present application is used as the published industry information and displayed to people and users in related fields for reading. The published industry information complies with the requirements of relevant financial regulations and relevant laws and regulations.
[0055] 2. In the scenario of publishing news information: the target information obtained by the text processing method of the embodiment of the present application is used as the news information to be published and displayed to people and users in related fields for reading. The published news information complies with the regulations of the news field and relevant laws and regulations.
[0056] The embodiments of the present application can be implemented using database technology. A database, in short, can be thought of as an electronic filing cabinet storing electronic files, allowing users to add, query, update, and delete data within the files. A "database" is a collection of data that is stored together in a specific manner, can be shared by multiple users, has minimal redundancy, and is independent of applications.
[0057] A database management system (DBMS) is a computer software system designed for managing databases, typically providing basic functions such as storage, retrieval, security, and backup. DBMSs can be categorized by the database model they support, such as relational or XML (Extensible Markup Language); by the type of computer they support, such as server clusters or mobile phones; by the query language they use, such as SQL or XQuery; by performance priorities, such as maximum scale or maximum speed; or by other classification methods. Regardless of the classification method used, some DBMSs are cross-category, for example, supporting multiple query languages simultaneously.
[0058] See also Figure 2 , Figure 2 is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. The electronic device may be Figure 1 Server 200, Figure 2 The server 200 shown includes: at least one processor 410, a memory 450, and at least one network interface 420. The various components in the server 200 are coupled together via a bus system 440. It is understood that the bus system 440 is used to achieve connection and communication between these components. In addition to the data bus, the bus system 440 also includes a power bus, a control bus, and a status signal bus. However, for the sake of clarity, the bus system 440 is not described in detail. Figure 2 Various buses are labeled as bus system 440 .
[0059] The processor 410 can be an integrated circuit chip with signal processing capabilities, such as a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc., where the general-purpose processor can be a microprocessor or any conventional processor, etc.
[0060] The memory 450 may be removable, non-removable, or a combination thereof. Exemplary hardware devices include solid-state memory, hard drives, optical drives, etc. The memory 450 may optionally include one or more storage devices that are physically remote from the processor 410.
[0061] The memory 450 includes volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. The non-volatile memory may be a read-only memory (ROM), and the volatile memory may be a random access memory (RAM). The memory 450 described in the embodiments of the present application is intended to include any suitable type of memory.
[0062] In some embodiments, the memory 450 can store data to support various operations, examples of which include programs, modules, and data structures, or a subset or superset thereof, as exemplified below.
[0063] Operating system 451, including system programs for processing various basic system services and performing hardware-related tasks, such as the framework layer, core library layer, and driver layer, which are used to implement various basic services and process hardware-based tasks;
[0064] The network communication module 452 is used to reach other electronic devices via one or more (wired or wireless) network interfaces 420 . Exemplary network interfaces 420 include Bluetooth, Wireless LAN (WiFi), and Universal Serial Bus (USB).
[0065] In some embodiments, the apparatus provided in the embodiments of the present application may be implemented in software. Figure 2 A text processing device 455 stored in memory 450 is shown. This device can be software in the form of a program or plug-in, and includes the following software modules: a generation module 4551 and a verification module 4552. These modules are logical and can be arbitrarily combined or further separated according to the functions they implement. The functions of each module will be described below.
[0066] In some embodiments, the terminal or server can implement the text processing method provided by the embodiment of the present application by running various computer executable instructions or computer programs. For example, the computer executable instructions can be commands, machine instructions or software instructions at the microprogram level. The computer program can be a native program or software module in the operating system; it can be a local (Native) application (APPlication, APP), that is, a program that needs to be installed in the operating system to run; it can also be a small program that can be embedded in any APP, that is, a program that can be run only by downloading it to a browser environment. In short, the above-mentioned computer executable instructions can be instructions in any form, and the above-mentioned computer program can be an application, module or plug-in in any form.
[0067] The text processing method provided in the embodiment of the present application will be described in conjunction with the exemplary application and implementation of the terminal provided in the embodiment of the present application.
[0068] The following describes the text processing method provided by the embodiments of the present application. As previously mentioned, the electronic device implementing the text processing method of the embodiments of the present application can be a terminal, a server, or a combination of the two. Therefore, the execution entity of each step will not be repeated below.
[0069] See also Figure 3A , Figure 3A This is a first flow chart of the text processing method provided in the embodiment of the present application, which will be combined with Figure 3A The steps shown are explained.
[0070] In step 301, a text generation process is performed on the first prompt word to obtain a first text unit.
[0071] For example, the text generation process is implemented by a machine learning model. In an embodiment of the present application, the machine learning model is a large language model. The large language model is a pre-trained language model that can understand and generate natural language. The large language model is trained on a large amount of text data based on deep learning technology to learn the statistical laws and patterns of language. The first prompt word is used to input the language model. The first prompt word is a text generated based on the user's question to be answered or query conditions. The first prompt word contains context and relevant information, such as: the relevant background of the input question, specific requirements or format instructions. The language model predicts the answer to the input first prompt word, obtains the predicted answer result, and uses the predicted answer as the first text unit. The first text unit is a sequence of multiple first characters (tokens). The first text unit is converted to an 8-bit (UTF-8) character set. 8-bit (Universal Character Set / Unicode Transformation For mat, UTF-8) is a variable-length character encoding for Unicode. It can be used to represent any character in the Unicode standard, so that the software that originally processed the American Standard Code for Information Interchange (ASCII) can continue to be used without or with only minor modifications.
[0072] In some embodiments, step 301 can be implemented by the following method: encoding the first prompt word to obtain a first encoding feature of the first prompt word; performing multiple character prediction processes based on the encoding feature to obtain multiple first characters; and combining the multiple first characters into a first text unit according to the order in which the multiple first characters are predicted.
[0073] For example, the encoding process is implemented by a large language model, which performs word segmentation on the input question text and converts it into a series of first prompt words. The first prompt words are converted into embedding vectors to obtain a series of word vector embeddings as the first encoding features. Based on the encoding features, multiple character prediction processes are performed, the encoding features are nonlinearly transformed, the vectors after nonlinear transformation are decoded, an output sequence is generated, the decoded vectors are mapped to the linear layer, the output of the linear layer is converted through an activation function (for example: softmax function), and multiple first characters after character prediction processing are output. The first encoding features contain multiple, so multiple character predictions are performed, and the input of each character prediction process includes: the first character that has been predicted and the encoding features, and then each first character is combined into a first text unit in the form of a sequence according to the prediction order of each first character.
[0074] In this embodiment, text generation is performed using a large language model. The large language model can quickly understand and process the input prompt word, improving the efficiency of text generation. Converting the first prompt word into an embedding vector can assist the large language model in obtaining rich semantic information. Through multiple character prediction processes, the large language model can generate answers character by character, improving the accuracy of the answers.
[0075] In step 302, the first text unit is verified based on preconfigured grammar rules, and a second text unit is determined based on the verified portion of the first text unit.
[0076] Here, the first text unit is a sequence consisting of a plurality of first characters.
[0077] For example, the preconfigured grammar rules are pre-set rules for constraining text units. The first text unit is verified based on the constraint rules, and the part of the first text unit that passes the verification is used as the second text unit to achieve screening of the first text unit based on the preconfigured grammar rules.
[0078] In some embodiments, see Figure 3B , Figure 3B This is a second flow chart of the text processing method provided in the embodiment of the present application. Figure 3A Step 302 in the above example can be accomplished by Figure 3B Steps 3021 to 3024 in the embodiment are implemented as described below.
[0079] In step 3021, each first character in the first text unit is verified based on preconfigured grammar rules to obtain a verification result for each first character.
[0080] For example, the preconfigured grammar rules and the first character are represented by a data structure in the form of a stack, each first character in the first text unit is verified based on the data structure, and a verification result of the first character is obtained according to a matching principle of the stack.
[0081] In some embodiments, step 3021 can be implemented by the following method: transcoding each first character in the first text unit to obtain a character encoding of a preconfigured format of the first character; performing rule matching on the character encoding based on preconfigured grammar rules to obtain a verification result, and the verification result indicates whether the first character conforms to or does not conform to the preconfigured grammar rules.
[0082] For example, rule matching is implemented through a data structure in a large language model. In an embodiment of the present application, the data structure is a stack in which preconfigured grammatical rules are written. Each first character in the first series of text units is transcoded to obtain a character encoding in a preconfigured format, and rule matching is performed layer by layer in the stack based on the transcoded character encoding. A pop operation is performed on the character encoding that does not meet the preconfigured grammatical rules through the large language model. The characters subjected to the pop operation do not meet the preconfigured grammatical rules, and the verification result is a failure, and the next layer of verification of the character encoding that failed the verification is stopped. The verification result of the character encoding that meets the preconfigured grammatical rules of each layer in the stack is a pass, and the partial character encoding that passed the verification is output.
[0083] In some embodiments, the verification result is also obtained by the following method: writing the preconfigured grammar rules into the data structure; calling each preconfigured grammar rule in the data structure in turn to perform rule matching on the character encoding; when the character encoding does not conform to any of the preconfigured grammar rules, the verification failure is used as the verification result of the character encoding; when the character encoding conforms to each preconfigured grammar rule, the first character corresponding to the character encoding is output, and the verification pass is used as the verification result of the character encoding.
[0084] For example, the data structure is a data structure in the form of a stack, which includes multiple layers. The pre-configured grammar rules are written into the data structure so that each grammar rule is stored in a different layer. The grammar rules of each layer in the stack are called in turn, and the character encoding loaded into the stack is matched with the rules. When it meets the grammar rules of the current layer, it enters the next layer to continue matching. When the character encoding does not meet the pre-configured grammar rules of any layer in the stack, the verification of the currently verified character encoding is stopped, and the verification result of the current character encoding is failed. When the character encoding meets the pre-configured grammar rules of each layer in the stack, it indicates that the verification result of the character encoding has passed the verification, and the first character corresponding to the verified character encoding is output from the data structure.
[0085] In some embodiments, before executing step 302, the preconfigured grammar rules are also written into the data structure by the following method: describing the preconfigured constraint text with a regular expression to obtain first constraint data; formatting the first constraint data according to a preconfigured formal language to obtain preconfigured grammar rules; and writing the preconfigured grammar rules into the data structure.
[0086] For example, regular expressions are a text processing tool that uses predefined character sequence patterns to search, match, and manipulate text. Regular expressions can be used for various text processing tasks, such as data validation, data extraction, string parsing, and complex text replacement. Constraint text is the text content corresponding to the constraint rule. The preconfigured constraint text is represented based on the regular expression form to obtain the first constraint data.
[0087] For example, consider a production rule consisting of "S->cAd; A->a or ab." Set (a, b, c, d) as lowercase terminals, (S, A) as uppercase nonterminals, and S->cAd, A->a, or ab as production rules. For the content "S->cAd," formal language transformations can generate grammatically correct content such as "cad" and "cabd."
[0088] For example, the preconfigured formal language is Backus Naur Form, which is a context-free grammar that uses a series of symbols and expressions to create string generation rules. The formal language of Backus Naur Form includes terminators, non-terminators, and production rules. The first constraint data pre-configured in different scenarios is formatted through the formal language to obtain preconfigured grammar rules, that is, a constraint file containing constraint conditions. The large language model calls the preconfigured grammar rules, parses the preconfigured grammar rules, and represents the parsed preconfigured grammar rules as a data structure in the form of a stack. The element type definition of the stack: rule definition end, alternative definition start, non-terminal symbol, terminal symbol, character reversal, arbitrary character and other rule types, and the elements of the stack are uniformly transcoded into UTF-8 character encoding. In the embodiment of the present application, the stack is to put the constraint files into a stack from back to front, and when the rule definition changes, the stack will be popped until the stack is empty.
[0089] For example, when defining the first constraint data, assume that a pre-configured grammar rule is generated to prevent the large language model from outputting words such as "yes" and "definitely". The content of the pre-configured grammar rule is as follows:
[0090] root::=^(?:(?!item).)*$;
[0091] item::=Yes|definitely.
[0092] Among them, according to the provisions of the formal language <non-terminal symbol>::=<production rule>, ^ indicates the beginning of the string, (?!item) is a negative lookahead assertion, which indicates that the subsequent character sequence cannot match the given pattern, ensuring that the following characters do not match the definition of item; ". " means matching any single character (except newline); "*" means that the previous expression (any single character) can be repeated zero or more times; "$" indicates the end of the string; "root" means matching any character sequence from the beginning to the end of the string, as long as this sequence does not contain any substring that meets the definition of item, "yes" and "definitely" are two terminal symbols, "|" means logical OR, which means that item can be either "yes" or "definitely". "root" matches any string that does not contain the two specific strings "yes" or "definitely", and the first constraint data is implemented in the form of pre-configured grammar rules.
[0093] In step 3022, if the verification result of the first character is that it does not comply with the preconfigured grammar rule, the first character is deleted.
[0094] For example, if the verification result of the first character is that it does not conform to the preconfigured grammatical rules, the first character that does not conform to the grammatical rules is deleted from the data structure, and subsequent verification of the first character is stopped. For example, if the first character to be verified is "invalid", "valid", and "valid", and the verification result of the first character "invalid" is that it does not conform to the grammatical rules, "invalid" is deleted from the first text unit, and the verification of the subsequent "valid" and "valid" is stopped.
[0095] In step 3023, if the verification result of the first character is that it complies with the preconfigured grammar rule, it is determined that the verification of the first character has passed.
[0096] For example, when the verification result of the first character is that it complies with the grammatical rules, it indicates that the first character complies with the grammatical rules of each layer in the data structure, the verification result of the first character is determined to be passed, and the first character that passes the verification is output from the data structure.
[0097] In step 3024, each first character that passes the verification is combined into a second text unit.
[0098] For example, the first text unit is a sequence of multiple first characters, and each first character that passes the verification is combined to obtain the second text unit.
[0099] In an embodiment of the present application, the first text unit is verified based on preconfigured grammatical rules, the preconfigured grammatical rules and the first text unit are represented as a data structure in the form of a stack, and the verification is performed through the attributes of the stack. The large language model can stop verification for characters that do not comply with the preconfigured grammatical rules by performing a stack pop operation, and the accuracy and efficiency of the first character that passes the verification are improved through layer-by-layer rule verification.
[0100] Continue to see Figure 3A In step 303, the first prompt word and the second text unit are combined into a second prompt word.
[0101] For example, the second text unit that passes the verification is output from the data structure and combined with the first prompt word. The combined prompt word is used as the second prompt word. The second prompt word is used to continue calling the large language model for text generation. The second prompt word contains the context information provided in the first prompt word.
[0102] In step 304, text generation processing is performed on the second prompt word to obtain a third text unit.
[0103] For example, the text generation processing is implemented through a machine learning model, which can be a large language model. The second prompt word is subjected to text generation processing through the large language model. The process of performing text generation processing on the second prompt word is the same as the implementation principle of performing text generation processing on the first prompt word.
[0104] In some embodiments, step 304 can be implemented by the following method: encoding the second prompt word to obtain a second encoding feature of the second prompt word; performing character prediction processing based on the second encoding feature to obtain multiple second characters; and combining the multiple second characters into a third text unit according to the order of the predicted multiple second characters.
[0105] For example, the second prompt word is encoded by a large language model, the large language model performs word segmentation on the input second prompt word, and the second prompt word is converted into an embedding vector to obtain a series of word vector embeddings corresponding to the second prompt word, and the converted word vector embedding is used as the second encoding feature. Multiple character prediction processes are performed based on the second encoding feature, the second encoding feature is nonlinearly transformed, and the vector after nonlinear transformation is decoded, and the decoded vector is mapped to a linear layer and the output is transformed through an activation function to output multiple second characters after character prediction processing. The third text unit is a sequence of multiple second characters, and each second character is combined into a third text unit in the form of a sequence according to the prediction order of each second character.
[0106] In step 305, the third text unit is decoded to obtain target information.
[0107] For example, a decoding operation is performed on the third text unit, and the decoding operation is implemented through a large language model. The content information obtained by decoding is output as target information. The format of the output target information meets the constraint requirements in the preconfigured grammar rules. The target information can be answer information or retrieval results, which is not limited in the embodiments of the present application.
[0108] In some embodiments, after executing step 303, the target information is also obtained by the following method: in response to the prompt word input operation, the prompt word input by the output operation is combined with the second prompt word into a third prompt word; text generation processing is performed on the third prompt word to obtain a fourth text unit; the fourth text unit is decoded by a machine learning model to obtain the target information.
[0109] For example, when the user inputs a new question to be answered related to the current question, the newly input question to be answered is used as a prompt word, and the input prompt word is combined with the current second prompt word as a new third prompt word. The third prompt word contains the context information in the second prompt word, which can provide a reference basis for background information for the text generation operation of the third prompt word. The third prompt word is processed by the large language model to generate text, and the fourth text unit is obtained and decoded to obtain the target information, thereby improving the accuracy of the target information obtained by text generation and the relevance to the prompt word.
[0110] In an embodiment of the present application, a large language model is used to perform text generation processing on a first prompt word to obtain a first text unit. The large language model can quickly understand and process the input prompt word, thereby improving the efficiency of text generation. Preconfigured grammar rules are generated through a formal language. The formal language allows flexible configuration and customization of constraint requirements to adapt to the specific needs of different industries and scenarios. The first text unit is verified based on the preconfigured grammar rules, and a second text unit is determined based on the part of the first text unit that passes the verification. The preconfigured grammar rules are represented as a data structure in the form of a stack. The verification processing is performed through the attributes of the stack, and the verification of non-compliant characters is stopped in time. The accuracy and efficiency of the verification are improved by layer-by-layer rule verification, ensuring that the output content meets the industry-specific format and compliance requirements, and reducing the probability of outputting illegal content. The prompt word is combined with the text unit as a new prompt word for multiple character predictions. The new prompt word contains the context information of the previous text unit, which improves the accuracy of the text unit generated by the text. By generating compliant target content in real time, users can quickly obtain target information that meets their expectations, thereby improving the user experience.
[0111] The following describes an exemplary application of the embodiments of the present application in a practical application scenario.
[0112] In the financial industry, news industry, or legal field, the content of published documents and answered questions must strictly comply with the requirements of their industry and have binding output requirements. For example, key tasks, position changes, foreign words, or trial results of cases need to be strictly verified or not allowed to be directly published. Therefore, content verification is required before publishing relevant information.
[0113] In related technologies, the text generation capabilities of large language models are used to constrain the output content. However, the text generation of large language models has problems such as randomness, hallucinations, and content violations. Therefore, the large language model is fine-tuned through high-quality compliance data, so that the output of the large language model is more inclined to the format of compliant data. This can improve the output quality of the model and meet industry requirements, but there is still a probability of outputting illegal content. In related technologies, after the content is output to the application end, the application end uses certain strategies to verify the compliance of the content, such as regular expressions, intent parsing algorithms, etc. to confirm the compliance of the content. If it is compliant, it is output. If it is not compliant, it is corrected, called again, or the default content is output to ensure the compliance of the output. However, the post-processing approach will increase the response delay. If streaming calls are used, the processing difficulty will increase. Repeated calls will cause problems of excessive cost and excessive time.
[0114] In an embodiment of the present application, a constraint file containing constraint requirements is formulated based on a formal language. The large language model parses the constraint requirements in the constraint file into a stack-like data structure, obtains the question text to be processed input by the user, encapsulates the question to be processed as prompt words and inputs it into the large language model. The large language model generates text units based on the prompt words, performs rule verification on the generated text units in the stack-like data structure, outputs the text units that pass the verification, and decodes the text units that pass the verification to obtain the target information. Intervening in the output of the large model at any time during the decoding stage avoids the problem of large model hallucinations and the inability to stream output, thereby improving the efficiency and accuracy of the output of the target information.
[0115] The following is a description with reference to the accompanying drawings. Figure 4 , Figure 4 This is a third flow chart of the text processing method provided in the embodiment of the present application. The execution subject may be a terminal device, a server, or a combination of the two. Figure 1 As an example, the server in the example is used as the execution subject. Figure 4 The steps shown are explained in detail.
[0116] In step 401, a constraint file is generated according to constraint requirements, and the constraint file is parsed into a grammar stack.
[0117] For example, Backus Normal Form (BNF) is a context-free grammar that uses a series of symbols and expressions to create string generation rules. Constraints are converted into constraint files based on this formal language. BNF generation rules include terminal symbols, non-terminal symbols, and production rules. Regular expressions are used to describe pre-configured constraints for different scenarios, and these constraints are converted into constraint files using this formal language.
[0118] For example, consider a production rule consisting of "S->cAd; A->a or ab." Set (a, b, c, d) as lowercase terminals, (S, A) as uppercase nonterminals, and S->cAd, A->a, or ab as production rules. For the content "S->cAd," formal language transformations can generate grammatically correct content such as "cad" and "cabd."
[0119] The constraint file is loaded into a large language model and parsed to generate a grammar stack. The grammar stack element types include rule types such as the end of a rule definition, the start of an alternative definition, a non-terminal symbol, a terminal symbol, a character reversal, and any character. The elements of the grammar stack are uniformly transcoded into UTF-8 character encoding. In this embodiment, the grammar stack push and pop principle includes: Constraint files are placed into a stack from back to front, and the stack is popped when the rule definition changes until the stack is empty.
[0120] For example, when defining a constraint file, assuming that the large model is not allowed to output "yes" and "certain", a constraint file is generated according to the preset value. The content of the constraint file is as follows:
[0121] root::=^(?:(?!item).)*$;
[0122] item::=Yes|definitely.
[0123] According to the formal language, <non-terminal symbol>::=<production rule>, ^ indicates the start of the string, (?!item) is a negative lookahead assertion, which indicates that the following character sequence cannot match the given pattern, ensuring that the following characters do not match the definition of item; "." matches any single character (except newline); "*" indicates that the previous expression (any single character) can be repeated zero or more times; "$" indicates the end of the string; "root" matches any character sequence from the beginning to the end of the string, as long as this sequence does not contain any substring that meets the definition of item; "yes" and "definitely" are two terminal symbols, and "|" represents logical OR, meaning that item can be either "yes" or "definitely". "root" matches any string that does not contain the two specific strings "yes" or "definitely". Preconfigured constraint requirements are implemented in the form of constraint files.
[0124] In some embodiments, a finite state machine (FSM) can be maintained during the decoding process, allowing the large language model to be decoded through the FSM's direction, thereby converting preconfigured constraint requirements into constraint text. A finite state machine (FSM) is a mathematical model that represents a finite number of states and behaviors such as transitions and actions between these states. A FSM has a finite number of states, each of which can transition to zero or more states. The input string determines which state transition is executed.
[0125] In step 402, the text to be processed is encapsulated into prompt words, and text units are generated based on the prompt words.
[0126] For example, the text to be processed is obtained from the user's description and encapsulated into prompt words. The prompt words are a piece of text generated based on user needs or queries, providing the context and relevant information of the text to be processed, guiding the large model on how to understand and respond. The user asks a question, and the text to be processed is converted into prompt words. The prompt words contain the original needs and possible additional information, such as relevant background, specific requirements, or format instructions. The large language model performs word segmentation on the text to be processed, obtaining a series of prompt words. The prompt words are converted into word vector embeddings. The embedded vectors represent the semantic and syntactic information of each prompt word in the prompt words. Based on the word vector embeddings, the text generation process of the target information is performed to generate the corresponding text units. The generated text unit may be a single character, word or subword unit, and is converted into an 8-bit (UTF-8) character set. 8-bit (Universal Character Set / Unicode Transformation Format, UTF-8) is a variable-length character encoding for Unicode. It can be used to represent any character in the Unicode standard, and the first byte in its encoding is still compatible with the American Standard Code for Information Interchange, so that the software that originally processed the American Standard Code for Information Interchange characters can continue to be used without or with only minor modifications.
[0127] In some embodiments, see Figure 5 , Figure 5 : It is a structural diagram of the large language model provided in an embodiment of the present application. The large language model 501 is composed of an input layer 5011, an encoding layer 5012, a decoding layer 5013 and an output layer 5014. The text to be processed is input into the large language model 501. The input layer 5011 performs word segmentation processing on the received processing text and converts it into a series of prompt words. Each prompt word is converted into an embedding vector to obtain a series of word vector embeddings. The encoding layer 5012 performs nonlinear conversion on the word vector embedding, and the decoding layer 5013 decodes the vector obtained after the nonlinear conversion to generate an output sequence. The output layer 5014 maps the decoded vector to the linear layer, converts the output of the linear layer through an activation function (for example: softmax function), and outputs the text unit after the text generation process as the target information.
[0128] In step 403, it is checked whether the generated text unit satisfies the rules corresponding to the grammar stack.
[0129] For example, based on the constraint requirements in the grammar stack, the generated text unit is verified to verify whether the generated text unit meets the rules in the grammar stack.
[0130] When the judgment result of step 403 is yes, step 404 is executed. In step 404, the text unit that has passed the verification is output and combined with the original prompt word to form an updated prompt word.
[0131] For example, if all generated text units pass verification, the text units that pass verification are output from the grammar stack, and the output text units are combined with the prompt words of the original text to be processed as new input for text generation.
[0132] When the judgment result of step 403 is no, step 405 is executed. In step 405, the generation probability of the text unit that fails the verification is changed to negative infinity, and an alternative text unit is selected.
[0133] For example, if a text unit fails verification at any level in the language stack, it is popped from the stack, verification of the text unit is stopped, the generation probability of the failed text unit is changed to negative infinity, and an alternative text unit is selected. The alternative text unit is a set of text units pre-provided by the large language model based on the context of the prompt word.
[0134] After executing step 404, step 406 is executed to generate text according to the updated prompt word to obtain a new text unit.
[0135] For example, a new input is generated by combining the output text unit with the prompt word of the original text to be processed. By converting the new input into a word vector embedding, a large language model is called to generate the text of the target information to obtain a new text unit.
[0136] After executing step 406 , step 408 is executed to output target information based on the new text unit.
[0137] For example, a decoding operation is performed on the new text unit, and the content information obtained by decoding is output as the target information. The format of the output target information meets the preconfigured constraint requirements.
[0138] In some embodiments, before step 408, step 407 is also performed to combine the relevant information input by the user with the new input as a new prompt word.
[0139] For example, when the user inputs new text to be processed that is related to the currently processed text, the newly input text to be processed is combined with the current input as a new prompt word. The new prompt word contains previous context information, which can provide a reference basis for generating target information for the new text to be processed, thereby improving the accuracy of the target information obtained by text generation.
[0140] The text processing method provided in the embodiment of the present application has the following beneficial effects:
[0141] Constraint files are formulated using a formal language that allows for flexible configuration and customization of constraint requirements to suit the specific needs of different industries and scenarios. The syntax stack is used to perform rule verification on the output of large language models to ensure that the output content meets industry-specific formatting and compliance requirements, reducing the probability of outputting illegal content. By generating compliant content in real time, users can quickly obtain content that meets their expectations, improving the user experience. By setting the generation probability of text units that fail verification to negative infinity, the system will select alternative text units, effectively reducing the output of erroneous information. When processing new text to be processed, the user's previous text and current input are combined as new prompts, allowing the model to better understand the context and provide more accurate information, thereby ensuring content compliance while improving the efficiency of generating target information.
[0142] The following continues to describe the exemplary structure of the text processing device 455 provided in the embodiment of the present application as a software module. In some embodiments, such as Figure 2 As shown, the software modules stored in the text processing device 455 of the memory 450 may include: a generation module 4551, used to perform text generation processing on the first prompt word to obtain a first text unit; a verification module 4552, used to perform verification processing on the first text unit based on preconfigured grammar rules, and determine a second text unit based on the verified part of the first text unit; combine the first prompt word and the second text unit into a second prompt word; perform text generation processing on the second prompt word to obtain a third text unit; and decode the third text unit to obtain target information.
[0143] In some embodiments, the generation module 4551 is also used to encode the first prompt word to obtain a first encoding feature of the first prompt word; perform character prediction processing based on the encoding feature to obtain multiple first characters; and combine the multiple first characters into a first text unit according to the order of the predicted multiple first characters.
[0144] In some embodiments, the first text unit is a sequence of multiple first characters; the verification module 4552 is also used to verify each first character in the first text unit based on preconfigured grammar rules to obtain a verification result for each first character; if the verification result of the first character is that it does not comply with the preconfigured grammar rules, the first character is deleted; if the verification result of the first character is that it complies with the preconfigured grammar rules, it is determined that the first character has passed the verification; and each first character that has passed the verification is combined into a second text unit.
[0145] In some embodiments, the verification module 4552 is also used to transcode each first character in the first text unit to obtain a character encoding of a preconfigured format for the first character; and perform rule matching on the character encoding based on preconfigured grammar rules to obtain a verification result, which indicates whether the first character conforms to or does not conform to the preconfigured grammar rules.
[0146] In some embodiments, the verification module 4552 is also used to write preconfigured grammar rules into a data structure; call each preconfigured grammar rule in the data structure in turn to perform rule matching on the character encoding; when the character encoding does not conform to any of the preconfigured grammar rules, the verification failure is used as the verification result of the character encoding; when the character encoding conforms to each preconfigured grammar rule, the first character corresponding to the character encoding is output, and the verification pass is used as the verification result of the character encoding.
[0147] In some embodiments, the verification module 4552 is also used to encode the second prompt word to obtain a second encoding feature of the second prompt word; perform character prediction based on the second encoding feature to obtain multiple second characters; and combine the multiple second characters into a third text unit according to the order of the predicted multiple second characters.
[0148] In some embodiments, after the first prompt word and the second text unit are combined into the second prompt word, the verification module 4552 is further used to respond to the prompt word input operation, combine the prompt word input by the output operation and the second prompt word into a third prompt word; perform text generation processing on the third prompt word to obtain a fourth text unit; and decode the fourth text unit through a machine learning model to obtain target information.
[0149] An embodiment of the present application provides a computer program product, which includes a computer program or computer-executable instructions stored in a computer-readable storage medium. A processor of an electronic device reads the computer-executable instructions from the computer-readable storage medium and executes the computer-executable instructions, causing the electronic device to perform the text processing method described in the embodiment of the present application.
[0150] The embodiment of the present application provides a computer-readable storage medium in which computer-executable instructions or computer programs are stored. When the computer-executable instructions or computer programs are executed by a processor, the processor will execute the text processing method provided by the embodiment of the present application, for example, Figure 3A The text processing method shown.
[0151] In some embodiments, the computer-readable storage medium may be a memory such as RAM, ROM, flash memory, magnetic surface memory, optical disk, or CD-ROM; or may be various devices including one or any combination of the above memories.
[0152] In some embodiments, computer-executable instructions may be in the form of a program, software, software module, script, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a stand-alone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.
[0153] As an example, computer-executable instructions may, but need not, correspond to a file in a file system, may be stored as part of a file that stores other programs or data, such as in one or more scripts in a HyperText Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple coordinating files (e.g., files storing one or more modules, subroutines, or code portions).
[0154] By way of example, computer-executable instructions may be deployed to be executed on one electronic device, or on multiple electronic devices located at one site, or on multiple electronic devices distributed across multiple sites and interconnected by a communication network.
[0155] In summary, the first prompt word is subjected to text generation processing through the embodiment of the present application to obtain the first text unit, thereby improving the text generation efficiency. Generating preconfigured grammatical rules through formal language can flexibly configure constraint requirements to adapt to the needs of different scenarios. The preconfigured grammatical rules are represented by a data structure in the form of a stack to perform verification processing on the text unit, and the accuracy and efficiency of the verification are improved through layer-by-layer rule verification to ensure compliance with the output target information. The prompt word and the text unit are combined as a new prompt word to perform multiple character predictions to improve the accuracy of generating target information.
[0156] The above description is merely an embodiment of the present application and is not intended to limit the scope of protection of the present application. Any modifications, equivalent replacements, and improvements made within the spirit and scope of the present application are included in the scope of protection of the present application.< / number> < / digit> < / digit> < / number> < / digit>
Claims
1. A text processing method, characterized in that: The method comprises: Performing text generation processing on the first prompt word to obtain a first text unit; Performing verification processing on the first text unit based on preconfigured grammatical rules, and determining a second text unit based on a portion of the first text unit that passes the verification; combining the first prompt word and the second text unit into a second prompt word; performing text generation processing on the second prompt word to obtain a third text unit; The third text unit is decoded to obtain target information.
2. The method according to claim 1, characterized in that The step of performing text generation processing on the first prompt word to obtain a first text unit includes: performing encoding processing on the first prompt word to obtain a first encoding feature of the first prompt word; Performing character prediction processing based on the coding features to obtain multiple first characters; The plurality of first characters are combined into the first text unit according to the predicted sequence of the plurality of first characters.
3. The method according to claim 1, characterized in that The first text unit includes a plurality of first characters; The verifying the first text unit based on the preconfigured grammar rules and determining the second text unit based on the verified portion of the first text unit includes: Performing verification processing on each of the first characters in the first text unit based on the preconfigured grammar rule to obtain a verification result for each of the first characters; If the verification result of the first character is that it does not comply with the preconfigured grammatical rule, deleting the first character; If the verification result of the first character is that it complies with the preconfigured grammatical rule, determining that the first character verification passes; Each of the first characters that passes the verification is combined into the second text unit.
4. The method according to claim 3, characterized in that The performing verification processing on each of the first characters in the first text unit based on the preconfigured grammar rule to obtain a verification result for each of the first characters includes: transcoding each of the first characters in the first text unit to obtain a character code of the first character in a preconfigured format; Rule matching is performed on the character encoding based on preconfigured grammar rules to obtain a verification result, where the verification result indicates whether the first character complies with or does not comply with the preconfigured grammar rules.
5. The method according to claim 4, characterized in that The character encoding is matched based on preconfigured grammar rules to obtain a verification result, including: Writing the preconfigured grammar rules into a data structure; calling each of the preconfigured grammar rules in the data structure in sequence to perform rule matching on the character encoding; If the character encoding does not conform to any of the preconfigured grammatical rules, taking verification failure as a verification result of the character encoding; In a case where the character encoding complies with each of the preconfigured grammatical rules, the first character corresponding to the character encoding is output, and passing the verification is used as a verification result of the character encoding.
6. The method according to claim 1, wherein The performing text generation processing on the second prompt word to obtain a third text unit includes: performing encoding processing on the second prompt word to obtain a second encoding feature of the second prompt word; Performing character prediction processing based on the second coding feature to obtain multiple second characters; The plurality of second characters are combined into the third text unit according to the predicted sequence of the plurality of second characters.
7. The method according to claim 1, characterized in that After combining the first prompt word and the second text unit into a second prompt word, the method further includes: In response to a prompt word input operation, combining the prompt word input by the output operation and the second prompt word into a third prompt word; Performing text generation processing on the third prompt word to obtain a fourth text unit; The fourth text unit is decoded to obtain target information.
8. An electronic device, characterized in that: The electronic device comprises: a memory for storing computer-executable instructions or computer programs; The processor is configured to implement the text processing method according to any one of claims 1 to 7 when executing the computer-executable instructions or computer programs stored in the memory.
9. A computer-readable storage medium storing computer-executable instructions or a computer program, characterized in that: When the computer executable instructions or computer program are executed by a processor, the text processing method according to any one of claims 1 to 7 is implemented.
10. A computer program product comprising computer executable instructions or a computer program, characterized in that When the computer executable instructions or computer program are executed by a processor, the text processing method according to any one of claims 1 to 7 is implemented.