Method and device for processing large language model with introduction of conditional constraints
By introducing a multi-rule state prediction model and a constraint decoding control module in a large language model, a single-step conditional constraint on the generated text is realized, which solves the problem of the lack of conditional constraints in the generated text by the existing model, and improves the efficiency of the model and Adaptability.
Patent Information
- Application Number
- CN202411493187.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-24
- Publication Date
- 2025-02-18
- Estimated Expiration
- 2044-10-24
AI Technical Summary
The existing large language models lack effective support for conditional constraints when generating text, resulting in the generation of text that may not comply with specific semantics, grammar and formatting rules. The existing solutions increase model complexity and computational costs, and have poor timeliness. .
A large language model with conditional constraints was designed, and a single-step text constraint capability was realized by pre-training rules to detect state prediction models, and a multi-rule state prediction model and constraint decoding control module were added to the large language model based on Transformer architecture.
This method effectively reduces the computational volume and design complexity of the model, improves the flexibility of adapting rules and the timeliness of task processing, and does not require secondary pre-training or fine-tuning of the model.
Smart Images

Figure CN119476208A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data processing technology, and in particular to a processing method and device for a large language model with conditional constraints introduced. Background Art
[0002] Large Language Models (LLMs) are widely used in a variety of natural language processing (NLP) tasks. Most of the current mainstream large language models are based on the Transformer architecture, such as the GPT series models, BERT series models, T5 series models, etc. After large-scale pre-training and directional field fine-tuning, these large language models can have stable language understanding and language generation capabilities. However, based on the implementation principle of the model infrastructure (i.e., the Transformer architecture), we know that these large language models do not have the ability to conditionally constrain the generated text. This may cause the generated text of the model to not meet some specific rule requirements, such as some specific semantics, grammar, and formatting expression rules.
[0003] To solve this problem, some researchers have added conditional / rule constraint encoding steps to the encoding process of traditional large language models to improve the conditional / rule constraint ability of the model; the specific approach is simply to add a conditional encoder to perform conditional vector encoding on the specified conditional rules (such as semantic rules, grammatical rules, formatted expression rules, etc.), and perform feature fusion on the conditional vector encoding and text encoding, and then perform decoding and generation processing based on the fusion features through the text generation module (or decoding module). Other researchers have added a series of post-processing modules to the backend of the traditional large language model to improve the conditional / rule constraint ability of the model; the specific approach is simply to build a corresponding text post-processing module in advance through the specified conditional rules (such as semantic rules, grammatical rules, formatted expression rules, etc.), and then use this text post-processing module to perform corresponding rule checks on the generated text of the large language model each time, and when it is found that the generated text does not meet the corresponding rule requirements, call the model's decoding module to generate a new text again and check the obtained new text again until the latest text can meet the corresponding rule requirements. However, through practical applications, we found that both solutions have some problems: 1) The former solution mechanism will increase the complexity of the model, and the model must be pre-trained twice or fine-tuned in the secondary directional field every time the conditional rules are switched or increased or decreased, which will increase the complexity of model design, increase the cost of model training, and the model's lack of flexibility in adapting to the rules. Problems; 2) The latter solution mechanism only performs a full sequence check based on the final output sequence and performs multiple repetitive text generation processes when the check does not meet the standards. This type of inspection method that does not perform single-step interference in the process has a higher probability of discovering errors, which means that the probability of repeated decoding of the entire sequence will be higher, which will naturally bring about problems such as large computational complexity and poor timeliness. Summary of the invention
[0004] The purpose of the present invention is to provide a processing method, device, electronic device and computer-readable storage medium for a large language model that introduces conditional constraints in response to the defects of the prior art. The present invention pre-customizes a rule detection state prediction model, i.e., a first state prediction model, for each type of conditional rule and trains it; then any type of large language model that is implemented based on the Transformer architecture and has completed pre-training and fine-tuning is recorded as the first large language model, and one or more first state prediction models corresponding to known conditional rules are selected to build a corresponding multi-rule state prediction model, and on the basis of the first large language model, a multi-rule state prediction model and a constraint decoding control module are added to form a constraint text generation model that can conditionally constrain each step of generated text in a single-step detection manner, which is recorded as the second large language model; and based on the second large language model, the corresponding constraint text generation processing is performed according to the user input text and the corresponding constraint generated text is output to the user for feedback. The second largest language model designed by the present invention has the ability of single-step text constraint, and its corresponding calculation amount can be effectively reduced compared with the conventional post-processing mechanism, and its corresponding timeliness can be effectively improved compared with the conventional post-processing mechanism; the second largest language model designed by the present invention does not need to modify the encoding / decoding components of the large language model, and when switching conditional rules, it is only necessary to dynamically replace / add / remove the first state prediction model in the multi-rule state prediction model, and there is no need to perform secondary pre-training or secondary directional fine-tuning on the model; through the present invention, not only can the design complexity of the model be reduced, the model training cost can be saved, and the model's flexibility in adapting to the rules be improved, but also the calculation amount of the model can be reduced and the task processing timeliness of the model can be improved.
[0005] To achieve the above-mentioned purpose, a first aspect of an embodiment of the present invention provides a method for processing a large language model with conditional constraints, the method comprising:
[0006] Step 1, customizing a corresponding first state prediction model for each first condition rule in a preset first condition rule set; creating a corresponding model training data set for each first condition rule to obtain a corresponding first data set; and training the corresponding first state prediction model based on each first data set;
[0007] Among them, the first conditional rule set includes multiple first conditional rules; the rule types of the first conditional rules include at least semantic rules, grammatical rules, sentiment rules, and formatted expression rules; each of the first state prediction models is formed by sequentially connecting the corresponding convolutional neural network, the fully connected network, and the activation layer, the convolutional neural network is composed of one convolutional layer or multiple convolutional layers connected in sequence, and the fully connected network is composed of one fully connected layer or multiple fully connected layers connected in sequence; each of the first state prediction models is used to perform corresponding single-step rule state prediction according to the text sequence input by the model to obtain the corresponding single-step prediction state; the single-step prediction state includes an acceptable state and an unacceptable state;
[0008] Step 2: any type of large language model that is implemented based on the Transformer architecture and has completed pre-training and fine-tuning is recorded as the corresponding first large language model;
[0009] Step 3, selecting one or more of the first conditional rules from the first conditional rule set to form a corresponding current rule set; and constructing a corresponding multi-rule state prediction model based on all the first state prediction models corresponding to the current rule set; and adding the multi-rule state prediction model and the constraint decoding control module to the first large language model to form a corresponding second large language model;
[0010] Step 4: Based on the second largest language model, corresponding constraint text generation processing is performed according to the user input text, and the corresponding constraint generated text is output to the user for feedback.
[0011] Preferably, the first data set includes multiple first data records; the first data record includes a first training text sequence and a first label state vector; the first training text sequence is formed by sequentially sorting multiple first training texts, and the first training text is a number, symbol, single character, word or phrase; the first label state vector is formed by sequentially sorting multiple first label states; the first label state includes an acceptable state and an unacceptable state; the first label state corresponds one-to-one to the first training text.
[0012] Preferably, the step of creating a corresponding model training data set for each of the first conditional rules to obtain a corresponding first data set specifically includes:
[0013] Step 31, recording any of the first conditional rules as the corresponding current conditional rule;
[0014] Step 32, enumerate all rule items of the current conditional rule to obtain a corresponding current rule item set; perform regular expression conversion on each first rule item of the current rule item set to obtain a corresponding first regular expression; and form a corresponding current expression set from all the obtained first regular expressions;
[0015] The current rule entry set includes a plurality of the first rule entries; the current expression set includes a plurality of the first regular expressions; the first regular expressions correspond one to one with the first rule entries;
[0016] Step 33, selecting a type of automaton that is adapted to the current conditional rule from multiple types of automatons as the corresponding first automaton; and identifying the automaton type of the first automaton; if the automaton type is a deterministic finite automaton or a non-deterministic finite automaton, configuring the quintuple of the first automaton based on the current expression set; if the automaton type is a pushdown automaton, configuring the sextuple of the first automaton based on the current expression set;
[0017] The multiple types of automata include at least deterministic finite automata, non-deterministic finite automata and push-down automata; the quintuple of the deterministic finite automata includes a finite state set Q, a finite alphabet ∑, a transition function δ, a starting state q 0 and accepting state set F; the quintuple of the non-deterministic finite automaton includes a finite state set Q, a finite alphabet ∑, a transition function δ, a starting state q start and accept state q accept The six-tuple of the push-down automaton includes a state set Q, an input alphabet ∑, a stack alphabet Γ, a transition function δ, a starting state q 0 and an acceptance state set F; the first automaton is used to determine the compliance state of the input text sequence and obtain a corresponding determination result; the determination result includes an acceptable state and an unacceptable state;
[0018] Step 34, based on the text features corresponding to the current conditional rule and according to a preset first positive-negative sample ratio, corresponding positive and negative sample corpora are collected to obtain a corresponding first text corpus;
[0019] Wherein, the first text corpus includes a plurality of first text corpora; the first text corpus should be at least a single sentence corpus; the ratio of positive and negative sample text corpora in the first text corpus satisfies the first positive and negative sample ratio;
[0020] Step 35, performing word segmentation processing on each of the first text corpora to obtain a corresponding molecular sequence as the corresponding first training text sequence;
[0021] Step 36, taking each of the first training text sequences as the corresponding current training text sequence; initializing the corresponding first input sequence and first output sequence to be empty; and performing a round of sequential traversal on all the first training texts of the current training text sequence; and in the traversal process, adding the first training text currently traversed to the end of the first input sequence, and inputting the first input sequence that has been added this time into the corresponding first automaton to perform compliance state determination and obtain the corresponding current determination result and add it to the end of the first output sequence; and at the end of this round of traversal, using the obtained first output sequence as the corresponding first label state vector;
[0022] Step 37: Each of the first training text sequences and the corresponding first label state vector forms a corresponding first data record; and all the obtained first data records form a corresponding first data set.
[0023] Preferably, the training of the corresponding first state prediction model based on each of the first data sets specifically includes:
[0024] Step 41, record any of the first data sets as the corresponding current data set; record the first state prediction model corresponding to the current data set as the corresponding current state prediction model; and divide the current data set into two sub-data sets based on a preset first division ratio and record them as the corresponding first training set and first evaluation set;
[0025] Wherein, both the first training set and the first evaluation set are composed of a plurality of the first data records; the ratio of the total number of records in the first training set to the total number of records in the first evaluation set satisfies the first segmentation ratio;
[0026] Step 42, extracting the first first data record of the first training set as the corresponding current training record;
[0027] Step 43, taking the first training text sequence of the current training record as the corresponding current training text sequence; initializing the corresponding second input sequence and second output sequence to be empty; and performing a round of sequential traversal on all the first training texts of the current training text sequence; and in the traversal process, adding the first training text currently traversed to the end of the second input sequence, and inputting the second input sequence that has been added this time into the current state prediction model to perform the corresponding single-step rule state prediction to obtain the corresponding first single-step prediction state and add it to the end of the second output sequence; and at the end of this round of traversal, using the obtained second output sequence as the corresponding first prediction state vector;
[0028] Among them, the expression of the single-step rule state prediction corresponding to the current state prediction model is:
[0029] s=F con (Y 2 ; β),
[0030] F con () is the single-step rule state prediction function expression of the current state prediction model, Y 2 is the second input sequence, β is the model parameter of the current state prediction model, s is the first single-step prediction state, and the first single-step prediction state s includes an acceptable state and an unacceptable state;
[0031] The first prediction state vector is formed by sequentially sorting a plurality of the first single-step prediction states s; the first single-step prediction states s correspond one-to-one to the first training text;
[0032] Step 44, bringing the first predicted state vector and the first label state vector of the current training record into a preset first model loss function; and performing a round of optimization on the model parameter β of the current state prediction model in a direction that minimizes the first model loss function based on a preset first model optimizer;
[0033] Wherein, the first model loss function includes L1 loss function, L2 loss function and cross entropy loss function; the first model optimizer includes at least SGD optimizer and ADAM optimizer;
[0034] Step 45, identifying whether the current training record is the last first data record of the first training set; if so, going to step 46; if not, extracting the next first data record of the first training set as the new current training record and returning to step 43;
[0035] Step 46, perform a round of traversal on all the first data records of the first evaluation set; and in this round of traversal, use the first data record currently traversed as the corresponding current evaluation record; and initialize the third input sequence and the third output sequence to be empty; and sequentially poll all the first training texts of the first training text sequence of the current evaluation record; and in the polling process, add the first training text currently polled to the end of the third input sequence, and input the third input sequence that has been added this time into the current state prediction model to perform the corresponding single-step rule state prediction to obtain the corresponding second single-step prediction state and add it to the end of the third output sequence; and at the end of the polling, use the obtained third output sequence as the corresponding second prediction state vector; and form a corresponding first prediction-label pair by the second prediction state vector and the first label state vector of the current evaluation record; and at the end of this round of traversal, construct a confusion matrix based on all the obtained first prediction-label pairs to obtain a corresponding first confusion matrix; and calculate the corresponding accuracy, precision, recall and F1 score based on the first confusion matrix to obtain the corresponding first accuracy, first precision, first recall and first F1 score;
[0036] Step 47, identifying the first accuracy, the first precision, the first recall rate and the first F1 score; if the first accuracy does not meet the preset first accuracy range or the first accuracy does not meet the preset first accuracy range or the first recall rate does not meet the preset first recall rate range or the first F1 score does not meet the preset first F1 score range, returning to step 42 to continue training; if the first accuracy meets the first accuracy range and the first accuracy meets the first precision range and the first recall rate meets the first recall rate range and the first F1 score meets the first F1 score range, stopping training and confirming that the current state prediction model training is completed.
[0037] Preferably, the first language model is used to understand the natural language features of the model input text and perform corresponding text generation processing based on the understood features and output the corresponding model-generated text;
[0038] The first large language model includes a first encoding module and a first decoding module;
[0039] In the first large language model, the input end of the first encoding module is connected to the model input end, and the output end is connected to the encoding feature input end of the first decoding module; the output end of the first decoding module is connected to the model output end;
[0040] In the first language model, the first encoding module is implemented based on the encoder structure of the Transformer model or based on another type of deep learning model for feature encoding; the first encoding module is used to perform word segmentation processing on the model input text to obtain a corresponding word segmentation sequence; and perform feature encoding processing on the obtained word segmentation sequence to obtain a corresponding encoding vector X and send it to the first decoding module;
[0041] In the first large language model, the first decoding module is implemented based on a decoder structure of a Transformer model or based on another type of text generator model for feature decoding and text generation;
[0042] In the first large language model, the first decoding module is used to set the single-step index t=1 at the initial moment of receiving the encoding vector X, and set a corresponding initial character y based on a preset starting character 0 , and initialize the previous step to generate the sequence Y as {y 0}; and each time the single-step index t is set to change, a single-step text generation process is performed based on the encoding vector X, the single-step index t and the corresponding previous step generation sequence Y to obtain the corresponding single-step generated text y t ; and generate text y for the single step t Whether it matches the preset end character for identification; if the single step generates text y t If it does not match the preset end character, the single-step generated text y t Add to the previous step to generate the sequence Y{y 0 ,y 1 ,…y t-1} to get the reset sequence Y{y 0 ,y 1 ,…y t-1 ,y t}, and add 1 to the single-step index t, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y; if the single-step generated text y t If the preset end character is matched, the initial character y 0 Generate a sequence Y{y 0 ,y 1 ,…y t-1} remove the reset sequence Y{y 1 ,…y t-1}, and the sequence Y{y 1 ,…y t-1}Generate text as the corresponding model and output;
[0043] The expression of the single-step text generation process of the first decoding module is: t =F dec (Y,X;θ), where F dec () is the function expression of the single-step text generation process, Y is the sequence generated in the previous step, X is the encoding feature output by the first encoding module, θ is the model parameter of the first decoding module, and y t The single-step generated text for step t.
[0044] Preferably, the second largest language model is used to understand the natural language features of the model input text and perform corresponding constraint text generation processing based on the understood features and output the corresponding model generated text;
[0045] The second large language model includes the first encoding module, the first decoding module, the multi-rule state prediction model and the constraint decoding control module;
[0046] In the second large language model, the input end of the first encoding module is connected to the model input end, and the output end is connected to the first input end of the first decoding module; the single-step decoding output end of the first decoding module is connected to the input end of the multi-rule state prediction model; the output end of the multi-rule state prediction model is connected to the input end of the constraint decoding control module; the first output end of the constraint decoding control module is connected to the second input end of the first decoding module, and the second output end is connected to the model output end;
[0047] In the second language model, the first encoding module is used to perform word segmentation processing on the model input text to obtain a corresponding word segmentation sequence; and perform feature encoding processing on the obtained word segmentation sequence to obtain a corresponding encoding vector X and send it to the first decoding module;
[0048] In the second language model, the first decoding module is used to set the single-step index t=1 at the initial moment of receiving the encoding vector X, and initialize the previous step generation sequence Y to {y 0}; and each time the single-step index t is set to change, a single-step text generation process is performed based on the encoding vector X, the single-step index t and the corresponding previous step generation sequence Y to obtain the corresponding single-step generated text y t Sending to the multi-rule state prediction model;
[0049] In the second large language model, the multi-rule state prediction model is composed of a sequence splicing module, one or more parallel first state prediction models and a multi-rule state fusion module; the input end of the sequence splicing module is connected to the input end of the multi-rule state prediction model, and the output end is respectively connected to the input end of each of the first state prediction models; the output end of each of the first state prediction models is connected to an input end of the multi-rule state fusion module; the output end of the multi-rule state fusion module is connected to the output end of the multi-rule state prediction model;
[0050] In the second language model, the multi-rule state prediction model is used to receive each single-step generated text y t When the sequence splicing module receives the single-step generated text y t Add to the previous step to generate the sequence Y{y 0 ,y 1 ,…y t-1} at the end of the corresponding current generated sequence {y 0 ,…y t} is sent to each of the first state prediction models; and each of the first state prediction models generates a sequence {y 0 ,y 1 ,…y t-1 ,y t} Perform the corresponding single-step rule state prediction to obtain the corresponding single-step prediction state s i,t Send to the multi-rule state fusion module, 1≤ rule index i≤ total number of rules N, the total number of rules N is the total number of the first state prediction model; and the multi-rule state fusion module will obtain N single-step prediction states s i,t Form a corresponding single-step state vector S t Sending to the constraint decoding control module;
[0051] The single-step rule state prediction expression corresponding to each of the first state prediction models is: i,t =F con,i ({y 0 ,…y t}; β i ), where F con,i () is the single-step rule state prediction function expression of the i-th first state prediction model, β i is the model parameter of the i-th first state prediction model, and the single-step prediction state s i,t Includes acceptable and unacceptable states;
[0052] In the second language model, the constraint decoding control module is used to receive a single-step state vector S t When the single-step state vector S currently received is t With the preset label state vector S tag The vector difference modulus is calculated to obtain the corresponding first modulus value a=|S t -S tag |; and identifying whether the first modulus value a is zero;
[0053] If the first modulus value a is not zero, then the objective function is set to Based on the preset second model optimizer, the model parameter θ of the first decoding module is fine-tuned in the direction of minimizing the objective function, and at the end of this round of fine-tuning, the single-step generated text y generated in the previous step t-1 is t-1 Generate a sequence Y{y 0 ,y 1 ,…y t-1} remove the reset sequence Y{y 1 ,…y t-2}, and decrement the single-step index t by 1, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y; the second model optimizer includes at least an SGD optimizer and an ADAM optimizer;
[0054] If the first modulus value a is zero, the single-step generated text y t Whether it matches the preset end character for identification; if the single step generates text y t If it does not match the preset end character, the single-step generated text y t Add to the previous step to generate the sequence Y{y 0 ,y 1 ,…y t-1} to get the reset sequence Y{y 0 ,y 1 ,…y t-1 ,y t}, and add 1 to the single-step index t, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y; if the single-step generated text y t If the preset end character is matched, the initial character y 0 Generate a sequence Y{y 0 ,y 1 ,…y t-1} remove the reset sequence Y{y1 ,…y t-1}, and the sequence Y{y 1 ,…y t-1} is used as the corresponding model to generate text and output it.
[0055] A second aspect of an embodiment of the present invention provides a device for implementing the processing method of a large language model with conditional constraints introduced in the first aspect, the device comprising: a state prediction model preparation module, a large language model selection module, a large language model transformation module and a transformation model application module;
[0056] The state prediction model preparation module is used to customize a corresponding first state prediction model for each first conditional rule in the preset first conditional rule set; and to create a corresponding model training data set for each first conditional rule to obtain a corresponding first data set; and to train the corresponding first state prediction model based on each first data set; wherein, the first conditional rule set includes multiple first conditional rules; the rule types of the first conditional rules include at least semantic rules, grammatical rules, emotional rules, and formatted expression rules; each of the first state prediction models is formed by sequentially connecting a corresponding convolutional neural network, a fully connected network, and an activation layer, the convolutional neural network is composed of a layer of convolutional layers or multiple layers of sequentially connected convolutional layers, and the fully connected network is composed of a layer of fully connected layers or multiple layers of sequentially connected fully connected layers; each of the first state prediction models is used to perform a corresponding single-step rule state prediction according to a text sequence input by the model to obtain a corresponding single-step prediction state; the single-step prediction state includes an acceptable state and an unacceptable state;
[0057] The large language model selection module is used to record any type of large language model that is implemented based on the Transformer architecture and has completed pre-training and fine-tuning as the corresponding first large language model;
[0058] The large language model transformation module is used to select one or more of the first conditional rules from the first conditional rule set to form a corresponding current rule set; and to construct a corresponding multi-rule state prediction model based on all the first state prediction models corresponding to the current rule set; and to add the multi-rule state prediction model and the constraint decoding control module to the first large language model to form a corresponding second large language model;
[0059] The transformation model application module is used to perform corresponding constraint text generation processing according to the user input text based on the second largest language model and output the corresponding constraint generation text to feedback to the user.
[0060] A third aspect of an embodiment of the present invention provides an electronic device, including: a memory, a processor, and a transceiver;
[0061] The processor is used to be coupled to the memory, read and execute instructions in the memory, so as to implement the method steps described in the first aspect above;
[0062] The transceiver is coupled to the processor, and the processor controls the transceiver to send and receive messages.
[0063] A fourth aspect of an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer instructions. When the computer instructions are executed by a computer, the computer executes the instructions of the method described in the first aspect above.
[0064] The embodiment of the present invention provides a processing method, device, electronic device and computer-readable storage medium for a large language model that introduces conditional constraints. As can be seen from the above content, the embodiment of the present invention pre-customizes a rule detection state prediction model, i.e., a first state prediction model, for each type of conditional rule and trains it; then any type of large language model implemented based on the Transformer architecture and that has completed pre-training and fine-tuning is recorded as the first large language model, and one or more first state prediction models corresponding to known conditional rules are selected to construct a corresponding multi-rule state prediction model, and on the basis of the first large language model, a multi-rule state prediction model and a constraint decoding control module are added to form a constraint text generation model that can conditionally constrain each step of generated text in a single-step detection manner, which is recorded as the second large language model; and based on the second large language model, the corresponding constraint text generation processing is performed according to the user input text and the corresponding constraint generated text is output to the user for feedback. The second largest language model designed in the embodiment of the present invention has a single-step text constraint capability, and its corresponding calculation amount is effectively reduced compared with a conventional post-processing mechanism, and its corresponding timeliness is effectively improved compared with a conventional post-processing mechanism; the second largest language model designed in the embodiment of the present invention does not need to modify the encoding / decoding components of the large language model, and when switching conditional rules, it is only necessary to dynamically replace / add / remove the first state prediction model in the multi-rule state prediction model, and there is no need to perform secondary pre-training or secondary directional fine-tuning on the model; the embodiment of the present invention not only reduces the design complexity of the model, saves the model training cost, and improves the model's flexibility in adapting to the rules, but also reduces the model's calculation amount and improves the timeliness of model task processing. BRIEF DESCRIPTION OF THE DRAWINGS
[0065] Figure 1 A schematic diagram of a processing method for a large language model with conditional constraints provided in the first embodiment of the present invention;
[0066] Figure 2 A module structure diagram of a first state prediction model provided in Embodiment 1 of the present invention;
[0067] Figure 3 A module structure diagram of the first and second largest language models provided in the first embodiment of the present invention;
[0068] Figure 4 A flowchart of a text generation process of a first language model provided in Embodiment 1 of the present invention;
[0069] Figure 5 A schematic diagram of a flow chart of a process flow for generating a constrained text of a second language model provided in the first embodiment of the present invention;
[0070] Figure 6 A module structure diagram of a processing device for a large language model with conditional constraints provided in Embodiment 2 of the present invention;
[0071] Figure 7 A schematic diagram of the structure of an electronic device provided in Embodiment 3 of the present invention. DETAILED DESCRIPTION
[0072] In order to make the purpose, technical solution and advantages of the present invention clearer, the present invention will be further described in detail below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0073] Embodiment 1 of the present invention provides a method for processing a large language model with conditional constraints, such as Figure 1 A schematic diagram of a processing method for a large language model with conditional constraints provided in the first embodiment of the present invention is shown. The method mainly includes the following steps:
[0074] Step 1, customizing a corresponding first state prediction model for each first condition rule in a preset first condition rule set; creating a corresponding model training data set for each first condition rule to obtain a corresponding first data set; and training a corresponding first state prediction model based on each first data set;
[0075] Specifically comprising: step 11, customizing a corresponding first state prediction model for each first condition rule in the preset first condition rule set;
[0076] Here, the first conditional rule set of the embodiment of the present invention includes multiple first conditional rules; the rule types of the first conditional rules include at least semantic rules, grammatical rules, emotional rules, and formatted expression rules;
[0077] like Figure 2As shown in the module structure diagram of the first state prediction model provided in the first embodiment of the present invention, the model structure of each first state prediction model in the embodiment of the present invention is formed by sequentially connecting the corresponding convolutional neural network, the fully connected network and the activation layer; wherein the convolutional neural network is composed of one convolutional layer or multiple convolutional layers connected in sequence, and the fully connected network is composed of one fully connected layer or multiple fully connected layers connected in sequence; it should be noted that the first state prediction model in the embodiment of the present invention may also be formed by only sequentially connecting the fully connected network and the activation layer;
[0078] Each first state prediction model of the embodiment of the present invention is used to perform a corresponding single-step rule state prediction according to a text sequence input by the model to obtain a corresponding single-step prediction state; wherein the single-step prediction state includes an acceptable state and an unacceptable state; when the single-step prediction state is an acceptable state, it means that all texts and text sorting methods of the text sequence input by the model meet the corresponding first condition rule; when the single-step prediction state is an unacceptable state, it means that at least one sequence text or a text sorting order in the text sequence input by the model does not meet the corresponding first condition rule;
[0079] Step 12: creating a corresponding model training data set for each first condition rule to obtain a corresponding first data set;
[0080] The first data set includes a plurality of first data records; the first data record includes a first training text sequence and a first label state vector; the first training text sequence is formed by sequentially sorting a plurality of first training texts, and the first training text is a number, a symbol, a single character, a word or a phrase; the first label state vector is formed by sequentially sorting a plurality of first label states; the first label state includes an acceptable state and an unacceptable state; the first label state corresponds to the first training text one by one;
[0081] Specifically, it includes: step 121, recording any first condition rule as the corresponding current condition rule;
[0082] Step 122, enumerate all rule items of the current conditional rule to obtain a corresponding current rule item set; perform regular expression conversion on each first rule item of the current rule item set to obtain a corresponding first regular expression; and form a corresponding current expression set from all the obtained first regular expressions;
[0083] The current rule entry set includes a plurality of first rule entries; the current expression set includes a plurality of first regular expressions; the first regular expressions correspond to the first rule entries one by one;
[0084] Step 123, selecting a type of automaton that is adapted to the current conditional rule from multiple types of automata as the corresponding first automaton; and identifying the automaton type of the first automaton; if the automaton type is a deterministic finite automaton or a non-deterministic finite automaton, configuring the quintuple of the first automaton based on the current expression set; if the automaton type is a pushdown automaton, configuring the sextuple of the first automaton based on the current expression set;
[0085] Among them, multiple types of automata include at least deterministic finite automata (DFA), non-deterministic finite automata (NFA) and push down automata (PDA); the five-tuple of deterministic finite automata includes a finite state set Q, a finite alphabet ∑, a transition function δ, a starting state q 0 and accepting state set F; the quintuple of a non-deterministic finite automaton includes a finite state set Q, a finite alphabet ∑, a transition function δ, a starting state q start and accept state q accept ; The six-tuple of the push-down automaton includes the state set Q, the input alphabet ∑, the stack alphabet Γ, the transition function δ, the starting state q 0 and an acceptance state set F; the first automaton is used to determine the compliance state of the input text sequence and obtain a corresponding determination result, wherein the determination result includes an acceptable state and an unacceptable state;
[0086] Here, the automata used in the embodiments of the present invention are all commonly used automata. It can be known from the technical data disclosed by each type of automata that: given a type of language rule A, a corresponding automata A can be configured based on the type of language rule A, and after the configuration is completed, a status check can be performed based on the automata A on whether the input text / text sequence meets the language rule A and a corresponding determination result can be output, and the determination result includes an acceptable state and an unacceptable state;
[0087] Step 124, based on the text features corresponding to the current conditional rule and according to a preset first positive-negative sample ratio, corresponding positive and negative sample corpora are collected to obtain a corresponding first text corpus;
[0088] Among them, the first positive-negative sample ratio is a preset ratio parameter, such as 1:1; the first text corpus includes multiple first text corpora; if the first text corpus is a positive sample text corpus, it means that all texts of the first text corpus and the sorting method of all texts meet the current condition rules; if the first text corpus is a negative sample text corpus, it means that at least some texts or the sorting order of some texts in the first text corpus do not meet the current condition rules; the first text corpus should be at least a single sentence corpus; the ratio of positive and negative sample text corpora of the first text corpus meets the first positive-negative sample ratio;
[0089] Step 125, performing word segmentation processing on each first text corpus to obtain a corresponding molecular sequence as a corresponding first training text sequence;
[0090] Step 126, taking each first training text sequence as the corresponding current training text sequence; initializing the corresponding first input sequence and first output sequence to be empty; and performing a round of sequential traversal on all first training texts of the current training text sequence; and in the traversal process, adding the first training text currently traversed to the end of the first input sequence, and inputting the first input sequence that has been added this time into the corresponding first automaton to perform compliance state determination and obtain the corresponding current determination result and add it to the end of the first output sequence; and at the end of this round of traversal, the obtained first output sequence is used as the corresponding first label state vector;
[0091] Step 127, each first training text sequence and the corresponding first label state vector form a corresponding first data record; and all the obtained first data records form a corresponding first data set;
[0092] Step 13, training a corresponding first state prediction model based on each first data set;
[0093] Specifically, it includes: step 131, recording any first data set as the corresponding current data set; recording the first state prediction model corresponding to the current data set as the corresponding current state prediction model; and dividing the current data set into two sub-data sets based on a preset first division ratio and recording them as the corresponding first training set and the first evaluation set;
[0094] The first segmentation ratio is a preset ratio parameter, such as 8:2; the first training set and the first evaluation set are both composed of a plurality of first data records; the ratio of the total number of records in the first training set to the total number of records in the first evaluation set satisfies the first segmentation ratio;
[0095] Step 132, extracting the first first data record of the first training set as the corresponding current training record;
[0096] Step 133, taking the first training text sequence of the current training record as the corresponding current training text sequence; initializing the corresponding second input sequence and second output sequence to be empty; and performing a round of sequential traversal on all the first training texts of the current training text sequence; and in the traversal process, adding the first training text of the current traversal to the end of the second input sequence, and inputting the second input sequence that has been added this time into the current state prediction model to perform the corresponding single-step rule state prediction to obtain the corresponding first single-step prediction state and add it to the end of the second output sequence; and at the end of this round of traversal, using the obtained second output sequence as the corresponding first prediction state vector;
[0097] Here, the expression of the single-step rule state prediction corresponding to the current state prediction model is: s = F con (Y 2 ; β); where F con () is the single-step rule state prediction function expression of the current state prediction model, Y 2 is the second input sequence, β is the model parameter of the current state prediction model, s is the first single-step prediction state, and the first single-step prediction state s includes an acceptable state and an unacceptable state;
[0098] The first prediction state vector is formed by sequentially sorting a plurality of first single-step prediction states s; the first single-step prediction states s correspond one-to-one to the first training text;
[0099] Step 134, bringing the first predicted state vector and the first label state vector of the current training record into a preset first model loss function; and performing a round of optimization on the model parameter β of the current state prediction model in a direction that minimizes the first model loss function based on a preset first model optimizer;
[0100] The first model loss function includes an L1 loss function, an L2 loss function, and a cross entropy loss function; the first model optimizer includes at least an SGD optimizer and an ADAM optimizer;
[0101] Step 135, identifying whether the current training record is the last first data record of the first training set; if so, going to step 136; if not, extracting the next first data record of the first training set as the new current training record and returning to step 133;
[0102] Step 136, perform a round of traversal on all first data records of the first evaluation set; and in this round of traversal, use the first data record currently traversed as the corresponding current evaluation record; and initialize the third input sequence and the third output sequence to be empty; and sequentially poll all first training texts of the first training text sequence of the current evaluation record; and in the polling process, add the first training text currently polled to the end of the third input sequence, and input the third input sequence that has been added this time into the current state prediction model to perform the corresponding single-step rule state prediction to obtain the corresponding second single-step prediction state and add it to the end of the third output sequence; and at the end of the polling, use the obtained third output sequence as the corresponding second prediction state vector; and form a corresponding first prediction-label pair by the second prediction state vector and the first label state vector of the current evaluation record; and at the end of this round of traversal, construct a confusion matrix based on all the obtained first prediction-label pairs to obtain a corresponding first confusion matrix; and calculate the corresponding accuracy, precision, recall and F1 score based on the first confusion matrix to obtain the corresponding first accuracy, first precision, first recall and first F1 score;
[0103] Step 137, identify the first accuracy, the first precision, the first recall rate and the first F1 score; if the first accuracy does not meet the preset first accuracy range or the first accuracy does not meet the preset first accuracy range or the first recall rate does not meet the preset first recall rate range or the first F1 score does not meet the preset first F1 score range, return to step 132 to continue training; if the first accuracy meets the first accuracy range and the first accuracy meets the first precision range and the first recall meets the first recall rate range and the first F1 score meets the first F1 score range, stop training and confirm that the current state prediction model training is completed.
[0104] Here, the first accuracy range, the first precision range, the first recall range and the first F1 score range are four preset numerical range parameters.
[0105] Step 2: Any large language model that is implemented based on the Transformer architecture and has completed pre-training and fine-tuning is recorded as the corresponding first large language model.
[0106] Here, the first language model of the embodiment of the present invention includes at least a GPT series model, a BERT series model, a T5 series model, etc. The first language model is used to understand the natural language features of the model input text, perform corresponding text generation processing based on the understood features, and output the corresponding model-generated text.
[0107] like Figure 3As shown in the module structure diagram of the first and second large language models provided in Example 1 of the present invention, the core model components of the first large language model include a first encoding module and a first decoding module; the connection relationship of the core model components is: the input end of the first encoding module is connected to the model input end, and the output end is connected to the encoding feature input end of the first decoding module; the output end of the first decoding module is connected to the model output end.
[0108] The component information of the first language model core model component is as follows:
[0109] 1) The first encoding module is implemented based on the encoder structure of the Transformer model or based on another type of deep learning model for feature encoding;
[0110] The first encoding module is used to perform word segmentation processing on the model input text to obtain a corresponding word segmentation sequence; and perform feature encoding processing on the obtained word segmentation sequence to obtain a corresponding encoding vector X and send it to the first decoding module;
[0111] 2) The first decoding module is implemented based on a decoder structure of a Transformer model or based on another type of text generator model for feature decoding and text generation;
[0112] The first decoding module is used to set the single-step index t=1 at the initial moment of receiving the encoding vector X, and set a corresponding initial character y based on the preset starting character 0 , and initialize the previous step to generate the sequence Y as {y 0}; and every time the single-step index t is set to change, a single-step text generation process is performed based on the encoding vector X, the single-step index t and the corresponding previous step generation sequence Y to obtain the corresponding single-step generated text y t ; and generate text y for a single step t Whether to match the preset end character for recognition; if the text y is generated in a single step t If it does not match the preset end character, the text y will be generated in a single step t Add to the previous step to generate the sequence Y{y 0 ,y 1 ,…y t-1} to get the reset sequence Y{y 0 ,y 1 ,…y t-1 ,y t}, and add 1 to the single-step index t, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y; if the single-step generated text y t If it matches the preset end character, the initial character y 0 Generate a sequence Y{y0 ,y 1 ,…y t-1} remove the reset sequence Y{y 1 ,…y t-1}, and the sequence Y{y 1 ,…y t-1}Generate text as the corresponding model and output;
[0113] Among them, the expression of the single-step text generation process of the first decoding module is:
[0114] y t =F dec (Y,X;θ);
[0115] F dec () is the function expression of the single-step text generation process, Y is the sequence generated in the previous step, X is the encoding feature output by the first encoding module, θ is the model parameter of the first decoding module, and y t The single-step generated text for step t.
[0116] In order to intuitively understand the text generation process implemented by the first decoding module, the embodiment of the present invention is as follows: Figure 4 The flowchart of the text generation process flow of the first language model provided in the first embodiment of the present invention illustrates and explains the above-mentioned text generation process flow.
[0117] Step 3, selecting one or more first conditional rules from the first conditional rule set to form a corresponding current rule set; and constructing a corresponding multi-rule state prediction model based on all first state prediction models corresponding to the current rule set; and adding a multi-rule state prediction model and a constraint decoding control module to the first large language model to form a corresponding second large language model;
[0118] Specifically comprising: step 31, selecting one or more first conditional rules from the first conditional rule set to form a corresponding current rule set;
[0119] Step 32, and build a corresponding multi-rule state prediction model based on all the first state prediction models corresponding to the current rule set; and add a multi-rule state prediction model and a constraint decoding control module on the basis of the first large language model to form a corresponding second large language model.
[0120] Here, the second largest language model of the embodiment of the present invention is used to understand the natural language features of the model input text and perform corresponding constraint text generation processing based on the understood features and output the corresponding model-generated text.
[0121] like Figure 3As shown, the core model components of the second largest language model include a first encoding module, a first decoding module, a multi-rule state prediction model and a constraint decoding control module; the connection relationship of the core model components is: the input end of the first encoding module is connected to the model input end, and the output end is connected to the first input end of the first decoding module; the single-step decoding output end of the first decoding module is connected to the input end of the multi-rule state prediction model; the output end of the multi-rule state prediction model is connected to the input end of the constraint decoding control module; the first output end of the constraint decoding control module is connected to the second input end of the first decoding module, and the second output end is connected to the model output end.
[0122] The component information of the second largest language model core model component is as follows:
[0123] 1) The first encoding module is used to perform word segmentation processing on the model input text to obtain a corresponding word segmentation sequence; and perform feature encoding processing on the obtained word segmentation sequence to obtain a corresponding encoding vector X and send it to the first decoding module;
[0124] 2) The first decoding module is used to set the single-step index t=1 at the initial moment of receiving the encoding vector X, and initialize the previous step generation sequence Y to {y 0}; and every time the single-step index t is set to change, a single-step text generation process is performed based on the encoding vector X, the single-step index t and the corresponding previous step generation sequence Y to obtain the corresponding single-step generated text y t Send to the multi-rule state prediction model;
[0125] 3) The multi-rule state prediction model is composed of a sequence splicing module, one or more parallel first state prediction models and a multi-rule state fusion module; wherein the input end of the sequence splicing module is connected to the input end of the multi-rule state prediction model, and the output end is respectively connected to the input end of each first state prediction model; the output end of each first state prediction model is connected to an input end of the multi-rule state fusion module; the output end of the multi-rule state fusion module is connected to the output end of the multi-rule state prediction model;
[0126] The multi-rule state prediction model is used to generate a single-step text y each time it receives a t When the sequence concatenation module receives the single-step generated text y t Add to the previous step to generate the sequence Y{y 0 ,y 1 ,…y t-1} at the end of the corresponding current generated sequence {y 0 ,…y t} is sent to each first state prediction model; and each first state prediction model generates a sequence {y 0 ,y1 ,…y t-1 ,y t} Perform the corresponding single-step rule state prediction to obtain the corresponding single-step prediction state s i,t Send to the multi-rule state fusion module, 1≤ rule index i≤ total number of rules N, the total number of rules N is the total number of the first state prediction model; and the multi-rule state fusion module will obtain N single-step prediction states s i,t Form a corresponding single-step state vector S t Send to the constraint decoding control module;
[0127] Here, the expression of the single-step rule state prediction corresponding to each first state prediction model is: i,t =F con,i ({y 0 ,…y t}; β i ), where F con,i () is the single-step rule state prediction function expression of the i-th first state prediction model, β i is the model parameter of the i-th first state prediction model, and the single-step prediction state s i,t Includes acceptable and unacceptable states;
[0128] 4) The constraint decoding control module is used to receive a single-step state vector S t When the currently received single-step state vector S t With the preset label state vector S tag The vector difference modulus is calculated to obtain the corresponding first modulus value a=|S t -S tag |; and identify whether the first modulus value a is zero;
[0129] If the first modulus value a is not zero, then set the objective function to Based on the preset second model optimizer, the model parameter θ of the first decoding module is fine-tuned in the direction of minimizing the objective function, and at the end of this round of fine-tuning, the single-step generated text y generated in the previous step t-1 is t-1 Generate a sequence Y{y 0 ,y 1 ,…y t-1} remove the reset sequence Y{y 1 ,…y t-2}, and reduce the single-step index t by 1, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y;
[0130] If the first modulus value a is zero, then the single-step generated text y tWhether to match the preset end character for recognition; if the text y is generated in a single step t If it does not match the preset end character, the text y will be generated in a single step t Add to the previous step to generate the sequence Y{y 0 ,y 1 ,…y t-1} to get the reset sequence Y{y 0 ,y 1 ,…y t-1 ,y t}, and add 1 to the single-step index t, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y; if the single-step generated text y t If it matches the preset end character, the initial character y 0 Generate a sequence Y{y 0 ,y 1 ,…y t-1} remove the reset sequence Y{y 1 ,…y t-1}, and the sequence Y{y 1 ,…y t-1}Generate text as the corresponding model and output;
[0131] Among them, the label state vector S tag is a preset vector parameter, the label state vector S tag The vector length is the same as the single-step state vector S t The vector length is consistent, consisting of N single-step label states s tag,i Sort by order, single-step label state s tag,i With the single-step prediction state s i,t One-to-one correspondence between index i and each single-step label state s tag,i are all in acceptable states; the second model optimizer includes at least an SGD optimizer and an ADAM optimizer.
[0132] In order to intuitively understand the constraint text generation process linked by the first decoding module, the multi-rule state prediction model and the constraint decoding control module, the embodiment of the present invention is as follows: Figure 5 A flowchart of the constraint text generation process of the second largest language model provided in the first embodiment of the present invention is shown and explained.
[0133] By comparing Figure 4 , Figure 5It can be clearly seen that the embodiment of the present invention does not make any changes to the first encoding module of the first large language model nor does it make any substantial changes to the first decoding module. It only embeds two removable components (multi-rule state prediction model and constraint decoding control module) to perform conditional compliance checks on each step output of the first decoding module. If the check passes each time, the first decoding module is prompted to perform t+1 step text generation processing. If the check fails, the first decoding module is fine-tuned by a small specification fine-tuning method to fine-tune the decoding strategy of the first decoding module and prompt the first decoding module to regenerate the t-step text. It should be noted that the embodiment of the present invention can also set an error check threshold and count the number of consecutive regenerations of each t-step text. If the number of consecutive regenerations exceeds the error check threshold, the first decoding module is prompted to fall back to the t-1 step to regenerate the t-1 step text. This can not only reduce the probability of overfitting of the model, but also indirectly improve the diversity of the generated text.
[0134] Step 4: Based on the second largest language model, corresponding constraint text generation processing is performed according to the user input text, and the corresponding constraint generation text is output to the user for feedback.
[0135] Here, from the functional description of the second largest language model in the above text, it can be known that the use of the second largest language model can perform corresponding text generation processing based on the user's input text and can perform conditional constraint processing on the generated text. The constraint generated text obtained by the second largest language model will have greater compliance than the generated text output by the first largest language model.
[0136] Figure 6 The module structure diagram of a processing device for a large language model with conditional constraints provided in the second embodiment of the present invention is a terminal device or server that implements the aforementioned method embodiment, and can also be a device that enables the aforementioned terminal device or server to implement the aforementioned method embodiment. For example, the device can be a device or chip system of the aforementioned terminal device or server. Figure 6 As shown, the device includes: a state prediction model preparation module 201, a large language model selection module 202, a large language model transformation module 203 and a transformation model application module 204.
[0137] The state prediction model preparation module 201 is used to customize a corresponding first state prediction model for each first condition rule in the preset first condition rule set; and to create a corresponding model training data set for each first condition rule to obtain a corresponding first data set; and to train the corresponding first state prediction model based on each first data set; wherein the first condition rule set includes multiple first condition rules; the rule types of the first condition rules include at least semantic rules, grammatical rules, emotional rules, and formatted expression rules; each first state prediction model is composed of a corresponding convolutional neural network, a fully connected network, and an activation layer connected in sequence, the convolutional neural network is composed of one convolutional layer or multiple convolutional layers connected in sequence, and the fully connected network is composed of one fully connected layer or multiple fully connected layers connected in sequence; each first state prediction model is used to perform a corresponding single-step rule state prediction according to a text sequence input by the model to obtain a corresponding single-step prediction state; the single-step prediction state includes an acceptable state and an unacceptable state.
[0138] The large language model selection module 202 is used to record any type of large language model that is implemented based on the Transformer architecture and has completed pre-training and fine-tuning as the corresponding first large language model.
[0139] The large language model transformation module 203 is used to select one or more first conditional rules from the first conditional rule set to form a corresponding current rule set; and to build a corresponding multi-rule state prediction model based on all first state prediction models corresponding to the current rule set; and to add a multi-rule state prediction model and a constraint decoding control module on the basis of the first large language model to form a corresponding second large language model.
[0140] The transformation model application module 204 is used to perform corresponding constraint text generation processing according to the user input text based on the second largest language model and output the corresponding constraint generated text to feedback to the user.
[0141] An embodiment of the present invention provides a processing device for a large language model with conditional constraints, which can execute the method steps in the above method embodiment. Its implementation principle and technical effects are similar and will not be repeated here.
[0142] It should be noted that it should be understood that the division of the various modules of the above device is only a division of logical functions. In actual implementation, they can be fully or partially integrated into one physical entity, or they can be physically separated. And these modules can all be implemented in the form of software called by processing elements; they can also be all implemented in the form of hardware; some modules can also be implemented in the form of software called by processing elements, and some modules can be implemented in the form of hardware. For example, the state prediction model preparation module can be a separately established processing element, or it can be integrated in a chip of the above device. In addition, it can also be stored in the memory of the above device in the form of program code, and called and executed by a processing element of the above device. The function of the above determination module. The implementation of other modules is similar. In addition, these modules can be integrated together in whole or in part, or they can be implemented independently. The processing element described here can be an integrated circuit with signal processing capabilities. In the implementation process, each step of the above method or each module above can be completed by an integrated logic circuit of hardware in the processor element or instructions in the form of software.
[0143] For example, the above modules may be one or more integrated circuits configured to implement the above methods, such as one or more application specific integrated circuits (ASIC), or one or more digital signal processors (DSP), or one or more field programmable gate arrays (FPGA). For another example, when a module above is implemented in the form of a processing element scheduling program code, the processing element may be a general-purpose processor, such as a central processing unit (CPU) or other processor that can call program code. For another example, these modules may be integrated together and implemented in the form of a system-on-a-chip (SOC).
[0144] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the above method embodiments are generated. The above computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The above-mentioned computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the above-mentioned computer instructions can be transmitted from one website, computer, server or data center to another website, computer, server or data center through wired (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, Bluetooth, microwave, etc.) methods. The above-mentioned computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device such as a server or data center that includes one or more available media integrated. The above-mentioned available medium can be a magnetic medium (such as a floppy disk, a hard disk, a tape), an optical medium (such as a DVD), or a semiconductor medium (such as a solid state disk (SSD)), etc.
[0145] Figure 7 This is a schematic diagram of the structure of an electronic device provided in Embodiment 3 of the present invention. The electronic device may be a terminal device or a server for implementing the method of the aforementioned embodiment, or may be a terminal device or a server for implementing the method of the aforementioned embodiment connected to the aforementioned terminal device or server. Figure 7 As shown, the electronic device may include: a processor 301 (such as a CPU), a memory 302, and a transceiver 303; the transceiver 303 is coupled to the processor 301, and the processor 301 controls the transceiver 303. Various instructions may be stored in the memory 302 to complete various processing functions and implement the processing steps described in the aforementioned embodiment method. Preferably, the electronic device involved in the embodiment of the present invention also includes: a power supply 304, a system bus 305 and a communication port 306. The system bus 305 is used to realize the communication connection between components. The above-mentioned communication port 306 is used for connecting and communicating between the electronic device and other peripherals.
[0146] exist Figure 7The system bus 305 mentioned in the figure can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The system bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The communication interface is used to realize the communication between the database access device and other devices (such as clients, read-write libraries, and read-only libraries). The memory may include random access memory (RAM), and may also include non-volatile memory (Non-Volatile Memory), such as at least one disk storage.
[0147] The above-mentioned processor can be a general-purpose processor, including a central processing unit CPU, a network processor (Network Processor, NP), a graphics processing unit (Graphics Processing Unit, GPU), etc.; it can also be a digital signal processor DSP, an application-specific integrated circuit ASIC, a field programmable gate array FPGA or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0148] It should be noted that an embodiment of the present invention further provides a computer-readable storage medium, in which instructions are stored. When the computer-readable storage medium is run on a computer, the computer executes the method and processing process provided in the above embodiments.
[0149] The embodiment of the present invention provides a processing method, device, electronic device and computer-readable storage medium for a large language model that introduces conditional constraints. As can be seen from the above content, the embodiment of the present invention pre-customizes a rule detection state prediction model, i.e., a first state prediction model, for each type of conditional rule and trains it; then any type of large language model implemented based on the Transformer architecture and that has completed pre-training and fine-tuning is recorded as the first large language model, and one or more first state prediction models corresponding to known conditional rules are selected to construct a corresponding multi-rule state prediction model, and on the basis of the first large language model, a multi-rule state prediction model and a constraint decoding control module are added to form a constraint text generation model that can conditionally constrain each step of generated text in a single-step detection manner, which is recorded as the second large language model; and based on the second large language model, the corresponding constraint text generation processing is performed according to the user input text and the corresponding constraint generated text is output to the user for feedback. The second largest language model designed in the embodiment of the present invention has a single-step text constraint capability, and its corresponding calculation amount is effectively reduced compared with a conventional post-processing mechanism, and its corresponding timeliness is effectively improved compared with a conventional post-processing mechanism; the second largest language model designed in the embodiment of the present invention does not need to modify the encoding / decoding components of the large language model, and when switching conditional rules, it is only necessary to dynamically replace / add / remove the first state prediction model in the multi-rule state prediction model, and there is no need to perform secondary pre-training or secondary directional fine-tuning on the model; the embodiment of the present invention not only reduces the design complexity of the model, saves the model training cost, and improves the model's flexibility in adapting to the rules, but also reduces the model's calculation amount and improves the timeliness of model task processing.
[0150] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0151] The specific implementation methods described above further illustrate the objectives, technical solutions and beneficial effects of the present invention in detail. It should be understood that the above description is only a specific implementation method of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for processing a large language model with conditional constraints, characterized in that: The method comprises: Step 1, customizing a corresponding first state prediction model for each first condition rule in a preset first condition rule set; creating a corresponding model training data set for each first condition rule to obtain a corresponding first data set; and training the corresponding first state prediction model based on each first data set; Among them, the first conditional rule set includes multiple first conditional rules; the rule types of the first conditional rules include at least semantic rules, grammatical rules, sentiment rules, and formatted expression rules; each of the first state prediction models is formed by sequentially connecting the corresponding convolutional neural network, the fully connected network, and the activation layer, the convolutional neural network is composed of one convolutional layer or multiple convolutional layers connected in sequence, and the fully connected network is composed of one fully connected layer or multiple fully connected layers connected in sequence; each of the first state prediction models is used to perform corresponding single-step rule state prediction according to the text sequence input by the model to obtain the corresponding single-step prediction state; the single-step prediction state includes an acceptable state and an unacceptable state; Step 2: any type of large language model that is implemented based on the Transformer architecture and has completed pre-training and fine-tuning is recorded as the corresponding first large language model; Step 3, selecting one or more of the first conditional rules from the first conditional rule set to form a corresponding current rule set; and constructing a corresponding multi-rule state prediction model based on all the first state prediction models corresponding to the current rule set; and adding the multi-rule state prediction model and the constraint decoding control module to the first large language model to form a corresponding second large language model; Step 4: Based on the second largest language model, corresponding constraint text generation processing is performed according to the user input text, and the corresponding constraint generated text is output to the user for feedback.
2. The method for processing a large language model with conditional constraints according to claim 1, characterized in that: The first data set includes multiple first data records; the first data record includes a first training text sequence and a first label state vector; the first training text sequence is formed by sequentially sorting multiple first training texts, and the first training text is a number, a symbol, a single character, a word or a phrase; the first label state vector is formed by sequentially sorting multiple first label states; the first label state includes an acceptable state and an unacceptable state; the first label state corresponds one-to-one to the first training text.
3. The method for processing a large language model with conditional constraints according to claim 2, characterized in that: The step of creating a corresponding model training data set for each of the first conditional rules to obtain a corresponding first data set specifically includes: Step 31, recording any of the first conditional rules as the corresponding current conditional rule; Step 32, enumerate all rule items of the current conditional rule to obtain a corresponding current rule item set; perform regular expression conversion on each first rule item of the current rule item set to obtain a corresponding first regular expression; and form a corresponding current expression set from all the obtained first regular expressions; The current rule entry set includes a plurality of the first rule entries; the current expression set includes a plurality of the first regular expressions; the first regular expressions correspond one to one with the first rule entries; Step 33, selecting a type of automaton that is adapted to the current conditional rule from multiple types of automatons as the corresponding first automaton; and identifying the automaton type of the first automaton; if the automaton type is a deterministic finite automaton or a non-deterministic finite automaton, configuring the quintuple of the first automaton based on the current expression set; if the automaton type is a pushdown automaton, configuring the sextuple of the first automaton based on the current expression set; The multiple types of automata include at least deterministic finite automata, non-deterministic finite automata and push-down automata; the quintuple of the deterministic finite automata includes a finite state set Q, a finite alphabet ∑, a transition function δ, a starting state q0 and an accepting state set F; the quintuple of the non-deterministic finite automata includes a finite state set Q, a finite alphabet ∑, a transition function δ, a starting state q0 and an accepting state set F. start and accept state q accept ; The six-tuple of the push-down automaton includes a state set Q, an input alphabet ∑, a stack alphabet Γ, a transition function δ, a starting state q0 and an acceptance state set F; the first automaton is used to perform compliance state determination on the input text sequence and obtain a corresponding determination result; the determination result includes an acceptable state and an unacceptable state; Step 34, based on the text features corresponding to the current conditional rule and according to a preset first positive-negative sample ratio, corresponding positive and negative sample corpora are collected to obtain a corresponding first text corpus; Wherein, the first text corpus includes a plurality of first text corpora; the first text corpus should be at least a single sentence corpus; the ratio of positive and negative sample text corpora in the first text corpus satisfies the first positive and negative sample ratio; Step 35, performing word segmentation processing on each of the first text corpora to obtain a corresponding molecular sequence as the corresponding first training text sequence; Step 36, taking each of the first training text sequences as the corresponding current training text sequence; initializing the corresponding first input sequence and first output sequence to be empty; and performing a round of sequential traversal on all the first training texts of the current training text sequence; and in the traversal process, adding the first training text currently traversed to the end of the first input sequence, and inputting the first input sequence that has been added this time into the corresponding first automaton to perform compliance state determination and obtain the corresponding current determination result and add it to the end of the first output sequence; and at the end of this round of traversal, using the obtained first output sequence as the corresponding first label state vector; Step 37: Each of the first training text sequences and the corresponding first label state vector forms a corresponding first data record; and all the obtained first data records form a corresponding first data set.
4. The method for processing a large language model with conditional constraints according to claim 2, characterized in that: The training of the corresponding first state prediction model based on each of the first data sets specifically includes: Step 41, record any of the first data sets as the corresponding current data set; record the first state prediction model corresponding to the current data set as the corresponding current state prediction model; and divide the current data set into two sub-data sets based on a preset first division ratio and record them as the corresponding first training set and first evaluation set; Wherein, both the first training set and the first evaluation set are composed of a plurality of the first data records; the ratio of the total number of records in the first training set to the total number of records in the first evaluation set satisfies the first segmentation ratio; Step 42, extracting the first first data record of the first training set as the corresponding current training record; Step 43, taking the first training text sequence of the current training record as the corresponding current training text sequence; initializing the corresponding second input sequence and second output sequence to be empty; and performing a round of sequential traversal on all the first training texts of the current training text sequence; and in the traversal process, adding the first training text currently traversed to the end of the second input sequence, and inputting the second input sequence that has been added this time into the current state prediction model to perform the corresponding single-step rule state prediction to obtain the corresponding first single-step prediction state and add it to the end of the second output sequence; and at the end of this round of traversal, using the obtained second output sequence as the corresponding first prediction state vector; Among them, the expression of the single-step rule state prediction corresponding to the current state prediction model is: s=F con (Y2;β), F con () is the single-step rule state prediction function expression of the current state prediction model, Y2 is the second input sequence, β is the model parameter of the current state prediction model, s is the first single-step prediction state, and the first single-step prediction state s includes an acceptable state and an unacceptable state; The first prediction state vector is formed by sequentially sorting a plurality of the first single-step prediction states s; the first single-step prediction states s correspond one-to-one to the first training text; Step 44, bringing the first predicted state vector and the first label state vector of the current training record into a preset first model loss function; and performing a round of optimization on the model parameter β of the current state prediction model in a direction that minimizes the first model loss function based on a preset first model optimizer; Wherein, the first model loss function includes L1 loss function, L2 loss function and cross entropy loss function; the first model optimizer includes at least SGD optimizer and ADAM optimizer; Step 45, identifying whether the current training record is the last first data record of the first training set; if so, going to step 46; if not, extracting the next first data record of the first training set as the new current training record and returning to step 43; Step 46, perform a round of traversal on all the first data records of the first evaluation set; and in this round of traversal, use the first data record currently traversed as the corresponding current evaluation record; and initialize the third input sequence and the third output sequence to be empty; and sequentially poll all the first training texts of the first training text sequence of the current evaluation record; and in the polling process, add the first training text currently polled to the end of the third input sequence, and input the third input sequence that has been added this time into the current state prediction model to perform the corresponding single-step rule state prediction to obtain the corresponding second single-step prediction state and add it to the end of the third output sequence; and at the end of the polling, use the obtained third output sequence as the corresponding second prediction state vector; and form a corresponding first prediction-label pair by the second prediction state vector and the first label state vector of the current evaluation record; and at the end of this round of traversal, construct a confusion matrix based on all the obtained first prediction-label pairs to obtain a corresponding first confusion matrix; and calculate the corresponding accuracy, precision, recall and F1 score based on the first confusion matrix to obtain the corresponding first accuracy, first precision, first recall and first F1 score; Step 47, identifying the first accuracy, the first precision, the first recall rate and the first F1 score; if the first accuracy does not meet the preset first accuracy range or the first accuracy does not meet the preset first accuracy range or the first recall rate does not meet the preset first recall rate range or the first F1 score does not meet the preset first F1 score range, returning to step 42 to continue training; if the first accuracy meets the first accuracy range and the first accuracy meets the first precision range and the first recall rate meets the first recall rate range and the first F1 score meets the first F1 score range, stopping training and confirming that the current state prediction model training is completed.
5. The method for processing a large language model with conditional constraints according to claim 1, characterized in that: The first language model is used to understand the natural language features of the model input text, perform corresponding text generation processing based on the understood features, and output the corresponding model-generated text; The first large language model includes a first encoding module and a first decoding module; In the first large language model, the input end of the first encoding module is connected to the model input end, and the output end is connected to the encoding feature input end of the first decoding module; the output end of the first decoding module is connected to the model output end; In the first language model, the first encoding module is implemented based on the encoder structure of the Transformer model or based on another type of deep learning model for feature encoding; the first encoding module is used to perform word segmentation processing on the model input text to obtain a corresponding word segmentation sequence; and perform feature encoding processing on the obtained word segmentation sequence to obtain a corresponding encoding vector X and send it to the first decoding module; In the first large language model, the first decoding module is implemented based on a decoder structure of a Transformer model or based on another type of text generator model for feature decoding and text generation; In the first large language model, the first decoding module is used to set the single-step index t=1 at the initial moment of receiving the encoding vector X, set a corresponding initial character y0 based on a preset starting character, and initialize the previous step generation sequence Y to {y0}; and when the single-step index t is set to change once, a single-step text generation process is performed based on the encoding vector X, the single-step index t and the corresponding previous step generation sequence Y to obtain the corresponding single-step generated text y t ; and generate text y for the single step t Whether it matches the preset end character for identification; if the single step generates text y t If it does not match the preset end character, the single-step generated text y t Add to the previous step to generate the sequence Y{y0,y1,…y t-1 } to get the reset sequence Y{y0,y1,…y t-1 ,y t }, and add 1 to the single-step index t, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y; if the single-step generated text y t If the initial character y0 matches the preset end character, the sequence Y{y0,y1,…y t-1 } to obtain the reset sequence Y{y1,…y t-1 }, and the sequence Y{y1,…y t-1 }Generate text as the corresponding model and output; The expression of the single-step text generation process of the first decoding module is: t =F dec (Y,X;θ), where F dec () is the function expression of the single-step text generation process, Y is the sequence generated in the previous step, X is the encoding feature output by the first encoding module, θ is the model parameter of the first decoding module, and y t The single-step generated text for step t.
6. The method for processing a large language model with conditional constraints according to claim 5, characterized in that: The second largest language model is used to understand the natural language features of the model input text, perform corresponding constraint text generation processing based on the understood features, and output the corresponding model generated text; The second large language model includes the first encoding module, the first decoding module, the multi-rule state prediction model and the constraint decoding control module; In the second large language model, the input end of the first encoding module is connected to the model input end, and the output end is connected to the first input end of the first decoding module; the single-step decoding output end of the first decoding module is connected to the input end of the multi-rule state prediction model; the output end of the multi-rule state prediction model is connected to the input end of the constraint decoding control module; the first output end of the constraint decoding control module is connected to the second input end of the first decoding module, and the second output end is connected to the model output end; In the second language model, the first encoding module is used to perform word segmentation processing on the model input text to obtain a corresponding word segmentation sequence; and perform feature encoding processing on the obtained word segmentation sequence to obtain a corresponding encoding vector X and send it to the first decoding module; In the second large language model, the first decoding module is used to set the single-step index t=1 at the initial moment of receiving the encoding vector X, and initialize the previous step generation sequence Y to {y0}; and each time the single-step index t is set to change, a single-step text generation process is performed based on the encoding vector X, the single-step index t and the corresponding previous step generation sequence Y to obtain the corresponding single-step generated text y t Sending to the multi-rule state prediction model; In the second large language model, the multi-rule state prediction model is composed of a sequence splicing module, one or more parallel first state prediction models and a multi-rule state fusion module; the input end of the sequence splicing module is connected to the input end of the multi-rule state prediction model, and the output end is respectively connected to the input end of each of the first state prediction models; the output end of each of the first state prediction models is connected to an input end of the multi-rule state fusion module; the output end of the multi-rule state fusion module is connected to the output end of the multi-rule state prediction model; In the second language model, the multi-rule state prediction model is used to receive each single-step generated text y t When the sequence splicing module receives the single-step generated text y t Add to the previous step to generate the sequence Y{y0,y1,…y t-1 } at the end of the corresponding current generated sequence {y0,…y t } is sent to each of the first state prediction models; and each of the first state prediction models generates a sequence {y0, y1, ... y t-1 ,y t } Perform the corresponding single-step rule state prediction to obtain the corresponding single-step prediction state s i,t Send to the multi-rule state fusion module, 1≤ rule index i≤ total number of rules N, the total number of rules N is the total number of the first state prediction model; and the multi-rule state fusion module will obtain N single-step prediction states s i,t Form a corresponding single-step state vector S t Sending to the constraint decoding control module; The single-step rule state prediction expression corresponding to each of the first state prediction models is: i,t =F con,i ({y0,…y t }; β i ), where F con,i () is the single-step rule state prediction function expression of the i-th first state prediction model, β i is the model parameter of the i-th first state prediction model, and the single-step prediction state s i,t Includes acceptable and unacceptable states; In the second language model, the constraint decoding control module is used to receive a single-step state vector S t When the single-step state vector S currently received is t With the preset label state vector S tag The vector difference modulus is calculated to obtain the corresponding first modulus value a=|S t -S tag |; and identifying whether the first modulus value a is zero; If the first modulus value a is not zero, then the objective function is set to Based on the preset second model optimizer, the model parameter θ of the first decoding module is fine-tuned in the direction of minimizing the objective function, and at the end of this round of fine-tuning, the single-step generated text y generated in the previous step t-1 is t-1 Generate a sequence Y{y0,y1,…y t-1 } to obtain the reset sequence Y{y1,…y t-2 }, and decrement the single-step index t by 1, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y; the second model optimizer includes at least an SGD optimizer and an ADAM optimizer; If the first modulus value a is zero, the single-step generated text y t Whether it matches the preset end character for identification; if the single step generates text y t If it does not match the preset end character, the single-step generated text y t Add to the previous step to generate the sequence Y{y0,y1,…y t-1 } to get the reset sequence Y{y0,y1,…y t-1 ,y t }, and add 1 to the single-step index t, and continue to perform the next single-step text generation process based on the encoding vector X, the new single-step index t and the previous step generation sequence Y; if the single-step generated text y t If the initial character y0 matches the preset end character, the sequence Y{y0,y1,…y t-1 } to obtain the reset sequence Y{y1,…y t-1 }, and the sequence Y{y1,…y t-1 } is used as the corresponding model to generate text and output it.
7. A device for executing the method for processing a large language model with conditional constraints as described in any one of claims 1 to 6, characterized in that: The device comprises: a state prediction model preparation module, a large language model selection module, a large language model transformation module and a transformation model application module; The state prediction model preparation module is used to customize a corresponding first state prediction model for each first conditional rule in the preset first conditional rule set; and to create a corresponding model training data set for each first conditional rule to obtain a corresponding first data set; and to train the corresponding first state prediction model based on each first data set; wherein, the first conditional rule set includes multiple first conditional rules; the rule types of the first conditional rules include at least semantic rules, grammatical rules, emotional rules, and formatted expression rules; each of the first state prediction models is formed by sequentially connecting a corresponding convolutional neural network, a fully connected network, and an activation layer, the convolutional neural network is composed of a layer of convolutional layers or multiple layers of sequentially connected convolutional layers, and the fully connected network is composed of a layer of fully connected layers or multiple layers of sequentially connected fully connected layers; each of the first state prediction models is used to perform a corresponding single-step rule state prediction according to a text sequence input by the model to obtain a corresponding single-step prediction state; the single-step prediction state includes an acceptable state and an unacceptable state; The large language model selection module is used to record any type of large language model that is implemented based on the Transformer architecture and has completed pre-training and fine-tuning as the corresponding first large language model; The large language model transformation module is used to select one or more of the first conditional rules from the first conditional rule set to form a corresponding current rule set; and to construct a corresponding multi-rule state prediction model based on all the first state prediction models corresponding to the current rule set; and to add the multi-rule state prediction model and the constraint decoding control module to the first large language model to form a corresponding second large language model; The transformation model application module is used to perform corresponding constraint text generation processing according to the user input text based on the second largest language model and output the corresponding constraint generation text to feedback to the user.
8. An electronic device, characterized in that: include: memory, processors, and transceivers; The processor is used to couple with the memory, read and execute instructions in the memory, so as to implement the method according to any one of claims 1 to 6; The transceiver is coupled to the processor, and the processor controls the transceiver to send and receive messages.
9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and when the computer instructions are executed by a computer, the computer executes the method according to any one of claims 1 to 6.
Citation Information
Patent Citations
Method and device for training language model based on multiple word vectors, equipment and medium
CN111737995A
Text classification method and related device
CN112214605A
Text marking method and device
CN116089614A
Data processing instruction generation method and device, large model training method and device and electronic equipment
CN117112572A
Emotion analysis method and device and big language model training method and device
CN117436438A
Cited By
Processing method and device of dense encoder realized based on large language model
CN120353916A