Text generation method and system based on model editing and knowledge fusion
By introducing soft instructions and external knowledge retrieval mechanisms into the language model, the problems of pre-trained language models in long text generation and knowledge fusion are solved, achieving more accurate and smooth text generation, while reducing the cost of model updates.
Patent Information
- Application Number
- CN202411293814.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-14
- Publication Date
- 2025-02-25
AI Technical Summary
The existing pre-trained language models have problems of duplication, logical inconsistency, bias and error messages when dealing with problems such as long text generation, knowledge fusion and high real-time performance. The cost of model updates is high, making it difficult to effectively solve model illusions and knowledge boundaries.
Using a text generation method based on model editing and fusion knowledge, soft instructions are designed to insert into the attention module of the language model, and external knowledge is used in a search and enhanced way to promote the interaction between instructions and problems, and realize the understanding and utilization of new facts.
It improves the model's ability to utilize external knowledge, corrects error information in the text generated content, generates more accurate and fluent text content, realizes the integration and utilization of new knowledge, and maintains the robustness and efficiency of the model.
Smart Images

Figure CN119514499A_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field of natural language processing, and in particular relates to a text generation method and system based on model editing and fusion knowledge. Background Art
[0002] In recent years, with the rapid development of Large-Scale Language Models (LLMs), text generation and knowledge fusion technologies have made remarkable progress. These technologies have not only significantly improved the capabilities of natural language processing, but also played an important role in various application fields. However, despite these achievements, text generation and knowledge fusion still face some challenges.
[0003] First, text generation technology still has difficulties in dealing with contextual understanding and generating coherent and logical long texts. Although the model performs well in short text generation, it is prone to problems such as repetition, logical inconsistency, and lack of context in long text generation. In addition, these models are highly sensitive to input data and may generate content with bias or false information, affecting the reliability and credibility of their application. Secondly, the current status of knowledge fusion technology also has certain challenges. Knowledge fusion aims to integrate heterogeneous knowledge from multiple sources to provide more comprehensive and accurate information. However, since the data format, quality, and semantics of different knowledge bases may vary significantly, how to effectively integrate this knowledge has become a difficult problem. In addition, in the process of knowledge fusion, how to ensure that the fused knowledge can be updated and expanded in a timely manner while maintaining high quality and accuracy is also an urgent problem to be solved.
[0004] In order to better study text generation methods and alleviate the hallucination problem in the model, researchers have continuously expanded the scale of pre-training data and used more computing resources to develop (super) large-scale language models such as ChatGPT, GPT4, LLaMA, ChatGLM, and Wenxinyiyan, which have once again broken through the performance ceiling of various natural language processing tasks. At the same time, the excellent language understanding, text generation, knowledge reasoning and other capabilities of large models have attracted widespread attention and application in academia and industry, and are considered to be a major step towards general artificial intelligence. However, while massive training data and computing resources give large models powerful computing and reasoning capabilities, they also bring the following problems: (1) The large and low-quality data scale makes it difficult for the model to accurately characterize and express knowledge, resulting in a series of model hallucination problems (knowledge memory problems). (2) Negative and non-standard training data also causes the model to produce unethical results such as negativity and bias (knowledge security problems). (3) Models trained on static data sets are difficult to effectively solve high-real-time problems, and models trained on general domain data are difficult to apply to different fields (knowledge boundary problems).
[0005] At the same time, in order to solve the above problems, the high computational cost required to retrain the model to correct or update the knowledge in the model makes it unrealistic. Therefore, how to efficiently update the specific knowledge in the model and constrain the model behavior to alleviate the errors or illusions in the model-generated content becomes the key to solving the problem.
[0006] To this end, researchers have proposed a model editing method, which aims to implement constraints on specific behaviors of the model in an efficient way without compromising the performance of the model in other behaviors. Unlike fine-tuning the model, model editing not only requires correcting incorrect knowledge characterizations in the model and supplementing missing knowledge, but also requires that it should not affect information that is not related to the edited knowledge. Since model editing was proposed in 2021, this method has gradually attracted the attention of more and more scholars. Especially after the emergence of large-scale pre-trained language models such as ChatGPT, model editing has become an effective way to solve model hallucinations and achieve efficient knowledge updates and fusion of models. A successful model edit must meet the following basic properties:
[0007] (1) Reliability: The model can successfully modify or update incorrect or missing knowledge. For example, the model can update the answer to the question “Who is the 2022 World Cup champion?” from “France” to “Argentina”.
[0008] (2) Generalization: The model can successfully update knowledge that is identical or related to the edited facts, which is called fact generalization and reasoning generalization.
[0009] Factual generalization means that when the model answers the question "When was the last time Argentina won the World Cup?", it should get the answer "2022" based on updated facts. Reasoning generalization means that when the model answers the question "What is the capital of the 2022 World Cup champion country?", it should be able to answer "Buenos Aires" instead of "Paris" based on updated facts.
[0010] (3) Locality: When answering questions that are not related to the modified facts, the model must be consistent with the results of the model before the modification. For example, when the model answers any question that is not related to the knowledge of the triple “(Argentina, World Cup champion, 2022)”, the answer cannot change.
[0011] Specifically, f(·; θ) represents a large-scale pre-trained model with parameter θ. For a data e = (x e ,y e ,a),x e is the input, y e is the original output of the model, and a is the updated output.
[0012] Model editing aims to update the modified data e by editing the model E(·) while maintaining the data o=(x o ,y o )'s output remains unchanged.
[0013]
[0014] Among them, ~ indicates that the data are semantically consistent or related.
[0015] The above formula can be used to obtain the following indicators for evaluating the editing model:
[0016] (1) The modification success rate (ACC) refers to the successful update of the knowledge that needs to be modified, that is, E(f(x e ;θ),x e ,y e ,a)=a; (2) Edit generalization (Gen) refers to the knowledge that is successfully updated and modified in a consistent manner, that is, E(f(x e g ;θ),x e ,y e ,a)=a, where x e g For x e The problem of semantic consistency.
[0017] (3) Editing locality (Loc) means that the model should keep the output unchanged without modifying the data, that is, E(f(x o ;θ),x e ,y e ,a)=y o , for example, the model wants to maintain the answer to the question "Who is the founder of Apple?" as "Steve Jobs".
[0018] (4) Edit consistency (Con) refers to the ability of the model to use updated facts when performing reasoning calculations, that is, E(f(x e c ;θ),x e ,y e ,a)=a, where x e For x e c For related questions, such as when the model answers "In which continent is the country that won the 2022 World Cup championship?", it should consider "The country that won the 2022 World Cup championship is Argentina", and thus the answer is "South America".
[0019] In view of the above evaluation indicators, more and more model editing methods have been proposed to achieve more efficient editing. Depending on whether the language model parameters are modified, they can be divided into two categories: model editing based on parameter update and model editing based on knowledge enhancement. The former achieves knowledge update by making targeted modifications to the parameters related to the modified facts in the model. The latter, while keeping the model parameters unchanged, enhances the model's knowledge representation and understanding through additional modules or fact inputs, thereby updating the model results.
[0020] In addition, according to the form of facts modified, it can be divided into: batch model editing method and continuous model editing method. Batch model editing requires the model to update multiple factual information at one time, and each time different batches of knowledge are updated, the previous modifications will be forgotten. Continuous model editing requires the model to be able to continuously edit the knowledge in the model, which requires the model to not only update the current input fact information but also keep the memory of the previously updated facts. In addition, more and more researchers continue to explore the potential of model editing and propose new editing methods from multiple perspectives to better realize the update of model knowledge and behavior.
[0021] At present, many works have been proposed to solve the problem of model editing in different scenarios. According to the different update methods, they can be divided into two categories: (1) parameter modification-based methods and (2) external enhancement-based methods. The parameter modification-based method attempts to locate the parameter modules in the model related to the knowledge modification, and updates the parameters in a targeted manner to achieve the update of knowledge. The external enhancement-based method adds additional modules to different parts of the model to update the model output while freezing the model parameters, thereby achieving the effect of knowledge update.
[0022] However, it is still unknown whether the updated model can learn how to use the updated knowledge. A truly meaningful update requires not only correcting the current output of the model, but also guiding the model to learn how to use the knowledge for reasoning calculations, thereby ensuring the consistency of text generation and the degree of utilization of external knowledge. Summary of the invention
[0023] In view of the knowledge conflict problem caused by overconfidence in existing pre-trained language models, the present invention provides a text generation method and system based on model editing and fusion knowledge. Under the premise of providing external knowledge, the method of the present invention designs soft instructions to be inserted into the attention module of the language model, and uses them in a retrieval-enhanced manner to promote the interaction between instructions and questions, and realize the understanding and utilization of new facts. This method can enhance the model's ability to utilize external knowledge, improve the editing performance in single-hop (multi-hop) model editing, thereby correcting erroneous information in the text generation content and achieving more accurate and fluent text generation.
[0024] In order to achieve the above object, the present invention adopts the following technical solutions:
[0025] A text generation method based on model editing and fusion knowledge includes the following steps:
[0026] Step 1: construct a dataset. For each given question q and answer sentence y, construct a sentence containing the updated answer a, a sentence g that is consistent with the question semantics, a question lq that is completely unrelated to the updated question, and an answer la that is completely unrelated to the updated question; the purpose is to obtain user input instructions;
[0027] Step 2: Initialize soft instructions and set hyperparameters to use soft instructions at specific layers; design hard instructions in the form of the concatenation of question q and updated answer a; aiming to retrieve knowledge in combination with input conditions;
[0028] Step 3: Input the constructed data set, concatenate the hard instruction with the question q, the statement g that is consistent with the question semantics, and the question lq that is completely unrelated to the update question, and use the tokenizer to tokenize the hard instruction with the answer a and the answer la that is completely unrelated to the update question.
[0029] Step 4, context encoding: For a given sentence and retrieved update facts, a deep contextualized encoder is used to obtain the hidden representation of each sentence and each update fact.
[0030] Step 5, soft instruction encoding: Encode the trained soft instruction token through the same encoder as the context encoding, and concatenate them in the order of context soft instructions, update facts, and input questions at a specific layer; the purpose is to pass the encoded results to the editing model;
[0031] Step 6, instruction interaction: Based on the set hyperparameters, the soft instructions are applied to the attention module of the model in the specified layer of the model, and the interaction between the soft instructions and the context is realized through the attention mechanism;
[0032] Step 7, model prediction: Use the context representation after interaction as the final sentence representation, and the representation of the last word in the sentence as the input for predicting the representation of the next word. Iterate in a generative manner and repeat steps 4 to 7 until a preset number of words are generated to complete text generation.
[0033] Furthermore, the format of the data set in step 1 is:
[0034] {“question”: “q”,
[0035] "Original answer": "y",
[0036] "Update answer": "a",
[0037] "Consistency issue": "g",
[0038] "Irrelevant question": "lq",
[0039] "irrelevant answer":"la"}.
[0040] Further, the specific operation of step 2 is:
[0041] Initialization soft instruction means: T = t0, t1, ..., t i ,in It contains i tokens in total, and each token has a dimension of Set the soft command action layer to L s =10,L e =20,L s Indicates the starting layer of the splicing soft instruction, L e Indicates the end layer of the spliced soft instructions, so the total number of soft instruction tokens used is 20*i; set the hard instruction: I = [q; a].
[0042] Further, the specific operations of step 3 include:
[0043] Construct different input data and obtain I by the retrieval method in the reasoning stage;
[0044] Src=[I,g] (2)
[0045] Loc=[I,lq] (3)
[0046] Among them, Src is the update input and Loc is the locality input for evaluating the model.
[0047] Further, the specific operations of step 4 include:
[0048] For a given sentence x=x0,x1,...,x m-1 And the updated fact u retrieved based on sentence x:
[0049] u=Retrieval(x)=u0,u1,...,u n-1 (4)
[0050] The retrieval process uses Encoder to encode the input sentence and the external knowledge base M, and selects the results with the highest similarity and exceeding a certain threshold as the retrieval results:
[0051] Step1:de=Encoder(input_sentence)
[0052] Step2: dM=Encoder(data∈M)
[0053] Step3:score=similarity(de,dM)
[0054] Step4:result=argmax(score)if max(score) <threshold
[0055] Use a modified language model (such as LLaMA or GPT2) to obtain the hidden representation h of the sentence i ,h i The dimension is d h , which is calculated as follows:
[0056] X in =h0,h1,...,h m+n-2 =Encoder(u0,u1,...,u n-1 ,x0,x1,...,x m-1 ) (5)
[0057] Among them, x i (0≤i≤m-1) is the i-th token in sentence x, h i (0≤i≤m-1) is the representation of the i-th token in sentence x; h0,h1,...,h n-1 For hard instructions, h n ,h n+1 ,...,h m+n-2 Indicates the problem.
[0058] Use the model's front L s The layer computes two representations:
[0059] Out = LayerNorm(x in +MultiHead(x in ,x in ,x in )) (6)
[0060] Among them, the calculation of MultiHead attention includes attention (Attention), feedforward network (FeedforwardNetwork) and regularized residual connection (Layer Normalization and Residual Connections), which can be specifically expressed as:
[0061] 1.Attention:
[0062] MultiHead(Q,K,V)=Concat(head1,...,head h )W
[0063] where head i =Attention(QW i Q ,KW i K ,VW i V )
[0064]
[0065] 2. Feedforward Network:
[0066] FFN(x)=max(0,xW1+b1)W2+b2
[0067] 3.Layer Normalization and Residual Connections:
[0068] Normalization=LayerNorm(x+Sublayer(x)).
[0069] Further, the specific operations of step 5 include:
[0070] The trained soft instruction token T=t0,t1,...,t i The encoding is performed by the same encoder as the context encoding, and the context is concatenated in the order of soft instructions, update facts, and input questions as input. The calculation method is as follows:
[0071] X=t0,t1,t i ,h0,h1,...,h m+n-2 (7)
[0072] According to the pre-set hyperparameter L s and L e , if and only if the number of layers in the current model L is less than L e and greater than L s When X is used as input, otherwise the context representation X is used in as input.
[0073] Further, the step 6 specifically includes:
[0074] For L s and L e In the layer between, soft instructions will realize the interaction between instructions and input context through the Attenion mechanism, so as to realize the utilization and update of instruction knowledge, which is expressed as:
[0075] Query l=Linear q (X in ), (8)
[0076] Key l =Linear q (X), (9)
[0077] Value l =Linear q (X), (10)
[0078] Among them, Query, Key and Value are encoders in the Attenion mechanism, l is the current layer number,
[0079] By calculating the Key and Value between different inputs,
[0080] First, according to Calculate the attention weight matrix between different inputs,
[0081] Further based on Get the hidden representation of the output token.
[0082] Finally, the obtained token representation is used to complete subsequent calculations using the language model; the calculated scores are normalized to the probability distribution using the softmax function to obtain the sentence representation after interaction;
[0083] Further, step 7 specifically includes: according to the attention interaction mechanism in step 6, using soft instructions T=t0,t1,...,t i It realizes the interaction between the problem input h and the instruction u, and finally calculates X in The last tokenX next The probability distribution of the next token is obtained, and the token is compared with X in and X, and repeat steps 4 to 7 until a predetermined number of tokens are generated. The calculation formula is shown in (12).
[0084] x m =f(X next ) (11)
[0085] X,X in =[X; x m ],[X in ;x m ] (12)
[0086] Among them, [*; *] represents the vector concatenation operation, and the final obtained X in That is the result of model generation. The calculation method of f(x) is:
[0087]
[0088] Among them, Score uses the Softmax function;
[0089] Through the above steps, the model will update the answer to question q based on soft instructions and hard instructions, while keeping the model's answer to question lq that is completely unrelated to the updated question unchanged.
[0090] A text generation system based on model editing and fusion knowledge, including a user input module, a retrieval module, an editing module, and a language model text generation module;
[0091] The user input module is used to receive user questions or related instructions and process the user input information; for example, using a tokenizer to tokenize the input text and convert the original text into a token sequence that the model can process.
[0092] The retrieval module is used to retrieve relevant information from an external knowledge base or document; the retrieval module encodes the user's input information, obtains knowledge fragments or background information related to the user input from the knowledge base through methods such as keyword matching and semantic understanding, and passes this information to the editing module;
[0093] The editing module is used to process and integrate the retrieved knowledge; the editing module integrates the external knowledge with the user's input instructions, designs soft instructions and inserts them into the attention module of the language model; through the design and insertion of soft instructions, the editing module can guide the language model to better understand and utilize external knowledge, solve knowledge conflict problems, and promote interaction between instructions and questions;
[0094] The language model text generation module is used to generate the final text content; the language model text generation module uses the edited knowledge and user input to generate fluent and accurate text through the language model; during the generation process, the language model understands and applies new facts according to the instructions and knowledge provided by the editing module, thereby improving the accuracy and fluency of text generation;
[0095] When in use, the user inputs text information, and the user input module encodes the information input by the user, and the encoded representation is input into the retrieval module and the editing module respectively; the editing module calculates the input information and the results returned by the retrieval module to obtain the soft instruction information required for editing; and the user input information, retrieval results and soft instruction information are input into the language model text generation module for reasoning and generation; the final generated text content is output to the user. The text content combines the instructions input by the user and external knowledge, realizes the integration and utilization of new knowledge, and thus generates more accurate and fluent text.
[0096] Compared with the prior art, the present invention has the following advantages:
[0097] In terms of security, the method of the present invention retains the original parameters of the language model and updates the model in a plug-and-play manner through retrieval enhancement, thereby maximally avoiding the catastrophic problems caused by updating the model parameters, thereby ensuring the consistency and accuracy of the content generated by the model;
[0098] In terms of robustness, instruction fine-tuning is used to guide the model from the root on how to integrate knowledge and utilize updated knowledge;
[0099] In terms of efficiency, batch updates and continuous updates can be achieved after a single training, and the inference speed is basically the same as the original model.
[0100] On three benchmark datasets, the method proposed in this invention is significantly superior to existing methods in terms of editing reasoning and efficiency. The model after integrating knowledge can generate more accurate and fluent text content, realizing the integration and utilization of new knowledge. BRIEF DESCRIPTION OF THE DRAWINGS
[0101] Figure 1 A framework for text generation methods based on model editing and knowledge fusion;
[0102] Figure 2 Attention interaction module based on soft instruction fine-tuning;
[0103] Figure 3 Flowchart of text generation system based on model editing and knowledge fusion;
[0104] Figure 4 Specific flow chart of the text generation system based on model editing and knowledge fusion. DETAILED DESCRIPTION
[0105] In order to gain a deeper understanding of the present invention, a comprehensive and detailed description will be given. However, the present invention has multiple implementations and is not limited to the specific examples listed herein. The presentation of these examples is intended to deepen a comprehensive understanding of the disclosure of the present invention.
[0106] A text generation method based on model editing and fusion knowledge includes the following steps:
[0107] Step 1: Build a dataset. For each given question q and answer sentence y, construct a sentence containing the updated answer a, a sentence g that is consistent with the question semantics, a question lq that is completely unrelated to the updated question, and an answer la that is completely unrelated to the updated question.
[0108] The format of the dataset is:
[0109] {“question”: “q”,
[0110] "Original answer": "y",
[0111] "Update answer": "a",
[0112] "Consistency issue": "g",
[0113] "Irrelevant question": "lq",
[0114] "irrelevant answer": "la"};
[0115] Step 2: Initialize soft instructions and set hyperparameters to use soft instructions at specific layers; design hard instructions in the form of the concatenation of question q and updated answer a;
[0116] Initialization soft instruction means: T = t0, t1, ..., t i ,in It contains i tokens in total, and each token has a dimension of Set the soft command action layer to L s =10,L e =20,L s Indicates the starting layer of the splicing soft instruction, L e Indicates the end layer of the spliced soft instruction, so the total number of soft instruction tokens used is 20*i; set hard instructions: I = [q; a];
[0117] Step 3: Input the constructed data set, concatenate the hard instruction with the question q, the statement g that is consistent with the question semantics, and the question lq that is completely unrelated to the update question, and use the tokenizer to tokenize the hard instruction with the answer a and the answer la that is completely unrelated to the update question.
[0118] Construct different input data and obtain I by the retrieval method in the reasoning stage;
[0119] Src=[I,g] (14)
[0120] Loc=[I,lq] (15)
[0121] Among them, Src is the update input and Loc is the locality input for evaluating the model.
[0122] Step 4, context encoding: For a given sentence and retrieved update facts, a deep contextualized encoder is used to obtain the hidden representation of each sentence and each update fact.
[0123] For a given sentence x=x0,x1,...,x m-1 And the updated fact u retrieved based on sentence x:
[0124] u=Retrieval(x)=u0,u1,...,u n-1 (16)
[0125] The retrieval process uses Encoder to encode the input sentence and the external knowledge base M, and selects the results with the highest similarity and exceeding a certain threshold as the retrieval results:
[0126] Step1:de=Encoder(input_sentence)
[0127] Step2: dM=Encoder(data∈M)
[0128] Step3:score=similarity(de,dM)
[0129] Step4:result=argmax(score)if max(score) <threshold
[0130] Use a modified language model (such as LLaMA or GPT2) to obtain the hidden representation h of the sentence i ,h i The dimension is d h , which is calculated as follows:
[0131] X in =h0,h1,...,h m+n-2 =Encoder(u0,u1,...,u n-1 ,x0,x1,...,x m-1 ) (17)
[0132] Among them, x i (0≤i≤m-1) is the i-th token in sentence x, h i (0≤i≤m-1) is the representation of the i-th token in sentence x;
[0133] At this time, h0,h1,...,h n-1For hard instructions, h n ,h n+1 ,...,h m+n-2 For the problem representation;
[0134] Use the model's front L s The layer computes two representations:
[0135] Out = LayerNorm(x in +MultiHead(x in ,x in ,x in )) (18)
[0136] Among them, the calculation of MultiHead attention includes attention, feedforward network FeedforwardNetwork and regularized residual connection Layer Normalization and Residual Connections, which is specifically expressed as:
[0137] 1.Attention:
[0138] MultiHead(Q,K,V)=Concat(head1,...,head h )W
[0139] where head i =Attention(QW i Q ,KW i K ,VW i V )
[0140]
[0141] 2. Feedforward Network:
[0142] FFN(x)=max(0,xW1+b1)W2+b2
[0143] 3.Layer Normalization and Residual Connections:
[0144] Normalization=LayerNorm(x+Sublayer(x)).
[0145] Step 5, soft instruction encoding: Encode the trained soft instruction token through the same encoder as the context encoding, and concatenate them in the order of context soft instructions, update facts, and input questions at a specific layer;
[0146] The trained soft instruction tokenT=t0,t1,...,t i The encoding is performed by the same encoder as the context encoding, and the context is concatenated in the order of soft instructions, update facts, and input questions as input. The calculation method is as follows:
[0147] X=t0,t1,t i ,h0,h1,...,h m+n-2 (19)
[0148] According to the pre-set hyperparameter L s and L e , if and only if the number of layers in the current model L is less than L e and greater than L s When X is used as input, otherwise the context representation X is used in as input.
[0149] Step 6, instruction interaction: Based on the set hyperparameters, the soft instructions are applied to the attention module of the model in the specified layer of the model, and the interaction between the soft instructions and the context is realized through the attention mechanism;
[0150] For L s and L e In the layer between, soft instructions will realize the interaction between instructions and input context through the Attenion mechanism, so as to realize the utilization and update of instruction knowledge, which is expressed as:
[0151] Query l =Linear q (X in ), (20)
[0152] Key l =Linear q (X), (21)
[0153] Value l =Linear q (X), (22)
[0154] Among them, Query, Key and Value are encoders in the Attenion mechanism, l is the current layer number,
[0155] By calculating the Key and Value between different inputs,
[0156] First, according to Calculate the attention weight matrix between different inputs,
[0157] Finally, according to Get the hidden representation of the output token.
[0158] Step 7, model prediction: Use the context representation after interaction as the final sentence representation, and the representation of the last word in the sentence as the input for predicting the representation of the next word. Iterate in a generative manner and repeat steps 4 to 7 until a preset number of words are generated to complete text generation.
[0159] The obtained token representation is used to complete subsequent calculations using a language model; the calculated scores are normalized to a probability distribution using a softmax function to obtain the sentence representation after interaction;
[0160] According to the attention interaction mechanism in step 6, with the help of soft instructions T = t0, t1, ..., t i It realizes the interaction between the problem input h and the instruction u, and finally calculates X in The last tokenX next The probability distribution of the next token is obtained, and the token is compared with X in and X, and repeat steps 4 to 7 until a predetermined number of tokens are generated. The calculation formula is shown in (24).
[0161] x m =f(X next ) (twenty three)
[0162] X,X in =[X; x m ],[X in ;x m ] (twenty four)
[0163] Among them, [*; *] represents the vector concatenation operation, and the final obtained X in That is the result of model generation. The calculation method of f(x) is:
[0164]
[0165] Among them, Score uses the Softmax function;
[0166] Through the above steps, the model will update the answer to question q based on soft instructions and hard instructions, while keeping the model's answer to question lq that is completely unrelated to the updated question unchanged.
[0167] A text generation system based on model editing and fusion knowledge, including a user input module, a retrieval module, an editing module, and a language model text generation module;
[0168] The user input module is used to receive user questions or related instructions and process the user input information; for example, using a tokenizer to tokenize the input text and convert the original text into a token sequence that the model can process.
[0169] The retrieval module is used to retrieve relevant information from an external knowledge base or document; the retrieval module encodes the user's input information, obtains knowledge fragments or background information related to the user input from the knowledge base through methods such as keyword matching and semantic understanding, and passes this information to the editing module;
[0170] The editing module is used to process and integrate the retrieved knowledge; the editing module integrates the external knowledge with the user's input instructions, designs soft instructions and inserts them into the attention module of the language model; through the design and insertion of soft instructions, the editing module can guide the language model to better understand and utilize external knowledge, solve knowledge conflict problems, and promote interaction between instructions and questions;
[0171] The language model text generation module is used to generate the final text content; the language model text generation module uses the edited knowledge and user input to generate fluent and accurate text through the language model; during the generation process, the language model understands and applies new facts according to the instructions and knowledge provided by the editing module, thereby improving the accuracy and fluency of text generation;
[0172] When in use, the user inputs text information, and the user input module encodes the information input by the user, and the encoded representation is input into the retrieval module and the editing module respectively; the editing module calculates the input information and the results returned by the retrieval module to obtain the soft instruction information required for editing; and the user input information, retrieval results and soft instruction information are input into the language model text generation module for reasoning and generation; the final generated text content is output to the user. The text content combines the instructions input by the user and external knowledge, realizes the integration and utilization of new knowledge, and thus generates more accurate and fluent text.
[0173] The present invention conducts comparative experiments on model editing tasks with existing methods on three datasets: ZsRE, CounterFact and MQuAKE.
[0174] The ZsRE (Zero-shot Relation Extraction) dataset is a dataset specifically used to evaluate and train relation extraction models. Relation extraction is a key task in natural language processing, which aims to identify and extract the relationships between entities from text. During testing, the entity extraction task is changed to a text generation method, and the entity relationships are acquired through generation. The uniqueness of the ZsRE dataset is that it is mainly used in zero-shot learning scenarios. This means that the model can infer the existence of new relationships based on its existing knowledge and understanding without having seen certain relationship instances.
[0175] The CounterFact dataset is a dataset designed specifically to evaluate and improve the performance of large-scale language models in generating false or counterfactual information. Counterfactual information refers to information that contradicts known facts and is often used to test the model's ability to understand facts and maintain factual consistency. This dataset contains various counterfactual statements and their corresponding real information, which can better test the model's ability to integrate knowledge.
[0176] The experimental results are shown in Tables 1-2 below.
[0177] Table 1 Results of ZSRE dataset of LLaMA1-7B model
[0178]
[0179] Table 2 Results of the CounterFact dataset of the LLaMA1-7B model
[0180]
[0181] Ours represents the method proposed in this invention, +R means the result obtained by using the retrieval enhancement method, Score and H are the mean and arithmetic mean of other indicators respectively.
[0182] Overall, the method of the present invention has achieved the best results in both Score and H. As for the Por index, which is the degree of utilization of the model for reasoning knowledge, the method proposed in the present invention has significant advantages. The editing experimental results of the present invention on the above two data sets prove the effectiveness of the method in model editing and reasoning based on updated knowledge. In addition, the Gen index shows that after correcting the erroneous knowledge in the original text generated content, the corrected model can generate more accurate and fluent text content, realizing the integration and utilization of new knowledge.
[0183] The present invention is also tested on different language models, and the results are shown in Table 3 and Table 4. The overall results are close to the performance on LLama1, which illustrates the versatility and robustness of the method of the present invention.
[0184] Table 3 Results of the CounterFact dataset of the LLaMA2-7B model
[0185]
[0186] Table 4 Results of ZsRE dataset of LLaMA2-7B model
[0187]
[0188] Table 5 Multi-hop reasoning results of LLaMA2-7B on the MQuAKE dataset
[0189]
[0190] It can be seen from the above table that the method of the present invention can also achieve good results in multi-hop reasoning of editing. Through this experiment, it is demonstrated that the method of the present invention can integrate more complex knowledge into text generation, which once again verifies the superiority of the method of the present invention in utilizing updated and integrated knowledge.
[0191] The contents not described in detail in the specification of the present invention belong to the prior art known to the professional and technical personnel in the field. Although the illustrative specific embodiments of the present invention are described above to facilitate the understanding of the present invention by the technical personnel in the field, it should be clear that the present invention is not limited to the scope of the specific embodiments. For the ordinary technical personnel in the field, as long as various changes are within the spirit and scope of the present invention defined and determined by the attached claims, these changes are obvious, and all inventions and creations using the concept of the present invention are protected.
Claims
1. A text generation method based on model editing and fusion knowledge, characterized in that: The following steps are involved: Step 1: Build a dataset. For each given question q and answer sentence y, construct a sentence containing the updated answer a, a sentence g that is consistent with the question semantics, a question lq that is completely unrelated to the updated question, and an answer la that is completely unrelated to the updated question. Step 2: Initialize soft instructions and set hyperparameters to use soft instructions at specific layers; design hard instructions in the form of the concatenation of question q and updated answer a; Step 3: Input the constructed data set, concatenate the hard instruction with the question q, the statement g that is consistent with the question semantics, and the question lq that is completely unrelated to the update question, and use the tokenizer to tokenize the hard instruction with the answer a and the answer la that is completely unrelated to the update question. Step 4, context encoding: For a given sentence and retrieved update facts, a deep contextualized encoder is used to obtain the hidden representation of each sentence and each update fact. Step 5, soft instruction encoding: Encode the trained soft instruction token through the same encoder as the context encoding, and concatenate them in the order of context soft instructions, update facts, and input questions at a specific layer; Step 6, instruction interaction: Based on the set hyperparameters, the soft instructions are applied to the attention module of the model in the specified layer of the model, and the interaction between the soft instructions and the context is realized through the attention mechanism; Step 7, model prediction: Use the context representation after interaction as the final sentence representation, and the representation of the last word in the sentence as the input for predicting the representation of the next word. Iterate in a generative manner and repeat steps 4 to 7 until a preset number of words are generated to complete text generation.
2. The text generation method based on model editing and fusion knowledge according to claim 1, characterized in that: The format of the data set in step 1 is:
3. The text generation method based on model editing and fusion knowledge according to claim 1, characterized in that: The specific operations of step 2 are: Initialization soft instruction means: T = t0, t1, ..., t i ,in It contains i tokens in total, and each token has a dimension of Set the soft command action layer to L s =10,L e =20,L s Indicates the starting layer of the splicing soft instruction, L e Indicates the end layer of the spliced soft instructions, so the total number of soft instruction tokens used is 20*i; set the hard instruction: I = [q; a].
4. The text generation method based on model editing and fusion knowledge according to claim 1, characterized in that: The step 3 specifically includes: Construct different input data and obtain I by the retrieval method in the reasoning stage; Src=[I,g] (1) Loc=[I,lq] (2) Among them, Src is the update input and Loc is the locality input for evaluating the model.
5. The text generation method based on model editing and fusion knowledge according to claim 1, characterized in that: The step 4 specifically includes: For a given sentence x=x0,x1,...,x m-1 And the updated fact u retrieved based on sentence x: u=Retrieval(x)=u0,u1,...,u n-1 (3) The retrieval process uses Encoder to encode the input sentence and the external knowledge base M, and selects the results with the highest similarity and exceeding a certain threshold as the retrieval results: Step1:de=Encoder(input_sentence) Step2: dM=Encoder(data∈M) Step3:score=similarity(de,dM) Step4:result=argmax(score)if max(score) <threshold Use the modified language model to obtain the hidden representation h of the sentence i ,h i The dimension is d h , which is calculated as follows: X in =h0,h1,...,h m+n-2 =Encoder(u0,u1,...,u n-1 ,x0,x1,...,x m-1 ) (4) Among them, x i (0≤i≤m-1) is the i-th token in sentence x, h i (0≤i≤m-1) is the representation of the i-th token in sentence x; At this time, h0,h1,...,h n-1 For hard instructions, h n ,h n+1 ,...,h m+n-2 For the problem representation; Use the model's front L s The layer computes two representations: Out=LayerNorm(x in +MultiHead(x in ,x in ,x in )) (5) Among them, the calculation of MultiHead attention includes attention, feedforward network, and regularized residual connection Layer Normalization and Residual Connections, which is specifically expressed as: 1.Attention: MultiHead(Q,K,V)=Concat(head1,...,head h )W where head i =Attention(QW i Q ,KW i K ,VW i V ) 2. Feedforward Network: FFN(x)=max(0,xW1+b1)W2+b2 3.Layer Normalization and Residual Connections: Normalization=LayerNorm(x+Sublayer(x)).
6. The text generation method based on model editing and fusion knowledge according to claim 1, characterized in that: The specific operations of step 5 include: The trained soft instruction token T=t0,t1,...,t i The encoding is performed by the same encoder as the context encoding, and the context is concatenated in the order of soft instructions, update facts, and input questions as input. The calculation method is as follows: X=t0,t1,t i ,h0,h1,...,h m+n-2 (6) According to the pre-set hyperparameter L s and L e , if and only if the number of layers in the current model L is less than L e and greater than L s When X is used as input, otherwise the context representation X is used in as input.
7. The text generation method based on model editing and fusion knowledge according to claim 1, characterized in that: The step 6 specifically includes: For L s and L e In the layer between, soft instructions will realize the interaction between instructions and input context through the Attenion mechanism, so as to realize the utilization and update of instruction knowledge, which is expressed as: Query l =Linear q (X in ), (7) Key l =Linear q (X), (8) Value l =Linear q (X), (9) Among them, Query, Key and Value are encoders in the Attenion mechanism, l is the current layer number, By calculating the Key and Value between different inputs, First, according to Calculate the attention weight matrix between different inputs, Further based on Get the hidden representation of the output token; Finally, the obtained token representation is used to complete subsequent calculations using the language model; the calculated scores are normalized to the probability distribution using the softmax function to obtain the sentence representation after interaction.
8. The text generation method based on model editing and fusion knowledge according to claim 1, characterized in that: The step 7 specifically includes: According to the attention interaction mechanism in step 6, with the help of soft instructions T = t0, t1, ..., t i It realizes the interaction between the problem input h and the instruction u, and finally calculates X in The last token X next The probability distribution of the next token is obtained, and the token is compared with X in and X, repeat steps 4 to 7 until a predetermined number of tokens are generated. The calculation formula is shown in (11); x m =f(X next ) (10) X,X in =[X;x m ],[X in ;x m ] (11) Among them, [*; *] represents the vector concatenation operation, and the final obtained X in That is the result of model generation. The calculation method of f(x) is: Among them, Score uses the Softmax function; Through the above steps, the model will update the answer to question q according to soft instructions and hard instructions, while keeping the model's answer to question lq that is completely unrelated to the updated question unchanged.
9. A text generation system based on model editing and fusion knowledge, characterized by: It includes user input module, retrieval module, editing module, and language model text generation module; The user input module is used to receive questions or related instructions from users and process user input information; The retrieval module is used to retrieve relevant information from an external knowledge base or document; The retrieval module encodes the user's input information, obtains knowledge fragments or background information related to the user's input from the knowledge base, and passes this information to the editing module; The editing module is used to process and integrate the retrieved knowledge; The editing module integrates external knowledge with the user's input instructions, designs soft instructions and inserts them into the attention module of the language model; Through the design and insertion of soft instructions, the editing module can guide the language model to better understand and utilize external knowledge, resolve knowledge conflicts, and promote the interaction between instructions and questions; The language model text generation module is used to generate the final text content; the language model text generation module uses the edited knowledge and user input to generate fluent and accurate text through the language model; during the generation process, the language model understands and applies new facts according to the instructions and knowledge provided by the editing module, thereby improving the accuracy and fluency of text generation; When in use, the user inputs text information, and the user input module encodes the information input by the user, and the encoded representation is input into the search module and the editing module respectively; the editing module calculates the input information and the result returned by the search module to obtain the soft instruction information required for editing; The user input information, search results and soft instruction information are input into the language model text generation module for reasoning and generation; the final generated text content is output to the user.
Citation Information
Cited By
Large language model knowledge editing method and interaction method based on knowledge representation decoupling
CN120181205A
Retrieval enhancement generation anti-noise method and system based on dynamic reasoning intervention
CN121365728A