Attention-attribution-based dynamic retrieval augmentation generation method and system
By calculating illusion scores using attention attribution matrices and constructing keywords using semantic graphs, the illusion problem in large language models when generating answers is solved, enabling dynamic retrieval-enhanced generation and improving the accuracy and credibility of the question-answering system.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-30
- Publication Date
- 2026-03-24
AI Technical Summary
When generating answers in question-answering tasks, existing large-scale language models may exhibit an illusion phenomenon due to exceeding knowledge boundaries. Existing retrieval strategies lack dynamic adaptability and semantic attention, resulting in limited generation efficiency and accuracy.
By using a dynamic retrieval enhancement generation method based on attention attribution, an attention attribution matrix is used to calculate illusion scores, dynamically assess the reliability of generated content, and combine semantic graphs to construct keywords for retrieval queries, thereby optimizing the answer generation process.
It enables accurate identification and efficient mitigation of hallucination risks, improves the accuracy and credibility of the question-and-answer system's answers, and ensures the reliability and efficiency of generated content within the knowledge boundary.
Smart Images

Figure CN121029954B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of data processing technology, and specifically to a dynamic retrieval enhancement generation method and system based on attention attribution. Background Technology
[0002] The statements in this section are merely background information related to the present invention and do not necessarily constitute prior art.
[0003] Large Language Models (LLMs) have achieved remarkable results in question answering (QA) tasks, generating fluent and seemingly reasonable answers. However, a key challenge is that when exceeding the model's knowledge boundaries, LLMs sometimes generate content that contradicts the input context or objective facts. This "illusion" phenomenon severely undermines the reliability and usability of QA systems, especially in applications requiring high accuracy. To address this issue, researchers have proposed various knowledge injection methods. Among them, the Retrieval-Augmented Generation (RAG) framework has become a mainstream approach. The RAG framework introduces information from an external knowledge base before or during answer generation, providing the model with more reliable evidence and thus reducing the occurrence of illusions.
[0004] However, existing methods still have significant shortcomings in their retrieval strategies. Some methods employ static strategies, such as retrieving content only in the later stages of generation or at specific steps, which lack dynamic adaptability and cannot be flexibly adjusted according to the quality of the generated content. At the same time, existing methods often do not fully consider key semantic information in the retrieved content, resulting in limited retrieval efficiency and effectiveness. Summary of the Invention
[0005] To address the shortcomings of existing technologies, this invention provides a dynamic retrieval enhancement generation method and system based on attention attribution. By comprehensively evaluating the internal attention patterns and external generation confidence of a large language model during the answer generation process, it achieves accurate and efficient dynamic assessment of hallucination risk, thereby optimizing the reliability and accuracy of the question-answering system.
[0006] To achieve the above objectives, the present invention adopts the following technical solution:
[0007] In a first aspect, the present invention provides a dynamic retrieval enhancement generation method based on attention attribution.
[0008] A dynamic retrieval enhancement generation method based on attention attribution includes the following process:
[0009] For a given input question, a large language model is used to generate multiple different sequences of candidate answers;
[0010] Extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix;
[0011] When the hallucination score is less than or equal to a set threshold, it is determined that there is no risk of hallucination, and the optimal answer in the candidate answer sequence is used as the output answer corresponding to the input question;
[0012] When the hallucination score exceeds a set threshold, a hallucination risk is identified. The input question is then concatenated with the optimal answer from the candidate answer sequence to generate a complete semantic input. The complete semantic input is then segmented into sentences, and a hallucination score is calculated for each sentence. The sentences with the lowest hallucination scores are retained, and a semantic graph is constructed based on the retained sentences. Keywords are obtained from the semantic graph, and the final search query is formed based on the keywords to obtain the final output answer.
[0013] In one implementation of the first aspect of the present invention, calculating the illusion score based on the attention submatrix includes:
[0014] Determine the attribution score of each input word in the attention submatrix. Based on the attribution score of each input word, determine the uncertainty score of each input word. Combine the uncertainty scores to calculate the perplexity as the final illusion score.
[0015] As a further limitation of the first aspect of the invention, determining the attribution score of each input word based on the attention submatrix includes:
[0016] Each row of the attention submatrix represents a different output term, and each column of the attention submatrix represents a different input term. The average semantic contribution of each input term is calculated as the attribution score.
[0017] As a further limitation of the first aspect of the invention, determining the uncertainty score of each input word based on the attribution score of each input word includes:
[0018] The standardized coefficient of variation is calculated based on the attribution score of each input term, and the uncertain score is calculated based on the standardized coefficient of variation.
[0019] In one implementation of the first aspect of the present invention, extracting the corresponding attention attribution matrix based on each candidate answer sequence includes:
[0020] The candidate answer sequence is merged with the input question and fed into a large language model to extract the attention attribution matrix.
[0021] In one implementation of the first aspect of the present invention, calculating the illusion score for any sentence includes:
[0022] Calculate the predicted probability of each word in the sentence, multiply the predicted probability of each word in the sentence by the logarithm of the predicted probability, and then sum them to obtain the illusion score of the sentence.
[0023] Secondly, the present invention provides a dynamic retrieval enhancement generation system based on attention attribution.
[0024] A dynamic retrieval enhancement generation system based on attention attribution, comprising:
[0025] The candidate answer generation unit is configured to generate multiple different sequences of candidate answers for a given input question using a large language model.
[0026] The hallucination score calculation unit is configured to: extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix.
[0027] The first hallucination processing unit is configured to: when the hallucination score is less than or equal to a set threshold, determine that there is no risk of hallucination, and use the best answer in the candidate answer sequence as the output answer corresponding to the input question;
[0028] The second hallucination processing unit is configured to: determine the risk of hallucination when the hallucination score is greater than a set threshold; concatenate the input question with the best answer in the candidate answer sequence to generate a complete semantic input; segment the complete semantic input into sentences; calculate the hallucination score for each sentence; retain the sentences with the lowest set number of hallucination scores; construct a semantic graph based on the retained sentences; obtain keywords based on the semantic graph; and compose the final retrieval query statement based on the keywords to obtain the final output answer.
[0029] Thirdly, the present invention provides a computer device, comprising: a processor and a computer-readable storage medium;
[0030] A processor, adapted to execute computer programs;
[0031] A computer-readable storage medium storing a computer program, which, when executed by a processor, implements the attention-attribution-based dynamic retrieval enhancement generation method of the first aspect of the present invention.
[0032] Fourthly, the present invention provides a computer-readable storage medium storing a computer program adapted to be loaded by a processor and to execute the attention-attribution-based dynamic retrieval enhancement generation method of the first aspect of the present invention.
[0033] Fifthly, the present invention provides a computer program product, which includes a computer program that, when executed by a processor, implements the attention-attribution-based dynamic retrieval enhancement generation method of the first aspect of the present invention.
[0034] Compared with the prior art, the beneficial effects of the present invention are:
[0035] This invention innovatively proposes a dynamic retrieval enhancement generation method based on attention attribution. Using the illusion score as the core criterion, it directly reflects the correlation strength between the content generated by the large language model and its internal attention distribution. When the candidate answer sequence generated by the large language model exhibits attention dispersion or misfocus (e.g., the attention attribution matrix shows the model over-focusing on irrelevant context), the illusion score significantly increases, indicating that the generated content may exceed the model's inherent knowledge range and carries the risk of "fabricating facts." By setting a threshold, the system can automatically distinguish between "credible output" and "potential illusion." Below the threshold, the optimal answer is directly adopted to ensure efficient output; above the threshold, a dynamic retrieval mechanism is triggered. Through steps such as concatenating the question and preliminary answer, filtering low-scoring sentences by sentence segmentation, and constructing a semantic graph, knowledge gaps are accurately located, and targeted retrieval queries are generated. This process not only achieves a quantitative judgment of the model's knowledge boundaries (dynamically evaluating generation reliability through the illusion score) but also compensates for the model's information deficiencies outside the knowledge boundaries through iterative retrieval. Ultimately, while ensuring generation efficiency, it significantly improves the accuracy and credibility of the answers.
[0036] This invention, based on attribution scores extracted from attention submatrices, transforms the model's uncertainty regarding individual lexical units into a global uncertainty index, effectively capturing potential "fictional" tendencies during generation. It combines attention perturbation with generation perplexity (reflecting the difficulty of a large language model's response) as a hallucination score, comprehensively considering both the model's internal attentional distraction and the difficulty of external output. This allows the hallucination score to more sensitively reflect the risk of generated content exceeding the model's knowledge boundaries. This quantification method avoids the rigidity of a single threshold judgment. By dynamically fusing multi-dimensional uncertainty indicators, it achieves a more refined identification of "hallucination" generation. When the score exceeds the threshold, it indicates a bias or insufficient understanding of the model's attention to key input lexical units, requiring external retrieval to fill the knowledge gap; conversely, the generated result can be directly adopted. The entire process uses mathematical methods to transform the model's "reliability" into a calculable index, significantly improving the accuracy and interpretability of knowledge boundary judgment.
[0037] This invention, based on the row-column structure of the attention submatrix (rows corresponding to output words and columns corresponding to input words), calculates the average semantic contribution of all input words to each output word as the attribution score. This process directly reflects the degree to which the model utilizes the original input information during generation. If the attribution score of key input words is low, it indicates that the model has not paid sufficient attention to or correctly understood the input information, which may lead to the generated content deviating from the facts (i.e., hallucination). By quantifying the attribution score, it is possible to more sensitively capture the generation risk outside the model's knowledge boundary. At the same time, the calculation method of average semantic contribution avoids the noise interference of single word attribution and improves the stability of the attribution score through global averaging, making the hallucination judgment more reliable. Overall, this technology transforms the model's attention to input information into a calculable indicator through digital means, providing underlying support for dynamically identifying whether "generated content exceeds the knowledge boundary," ensuring that the retrieval mechanism can be accurately triggered when the hallucination score exceeds the threshold, ultimately improving the accuracy and credibility of the answer.
[0038] This invention utilizes a standardized coefficient of variation (the ratio of standard deviation to mean) to capture the fluctuation characteristics of input lexical attribution scores. A high coefficient of variation indicates that the large language model pays significant attention to different input lexical units (e.g., some lexical units contribute extremely highly, while others contribute very little), reflecting an imbalance in the model's utilization of input information during generation. This may lead to generated content relying on a few key lexical units while neglecting the overall semantics, increasing the risk of hallucination. By converting the coefficient of variation to logarithmic form, the influence of abnormally high values can be effectively suppressed, resulting in an uncertain score. The higher this score, the more unstable the model's attention to input information, and the more likely the generated results are to exceed the knowledge boundary. This process not only supplements the local judgment of a single attribution score but also enhances the robustness of hallucination detection through global discreteness analysis, making hallucination scores more sensitive to identify potential risks. This provides a more accurate triggering basis for subsequent dynamic retrieval mechanisms (such as filtering low-scoring sentences and constructing semantic graphs), ultimately ensuring generation efficiency while effectively controlling the tendency of fiction outside the knowledge boundary.
[0039] Advantages of additional aspects of the invention will be set forth in part in the description which follows, and in part will be obvious from the description, or may be learned by practice of the invention. Attached Figure Description
[0040] The accompanying drawings, which form part of this invention, are used to provide a further understanding of the invention. The illustrative embodiments of the invention and their descriptions are used to explain the invention and do not constitute an improper limitation of the invention.
[0041] Figure 1 A schematic diagram of a dynamic retrieval enhancement generation method based on attention attribution provided as an exemplary embodiment of the present invention;
[0042] Figure 2 A schematic diagram of a dynamic retrieval enhancement generation system based on attention attribution provided as an exemplary embodiment of the present invention;
[0043] Figure 3 A schematic diagram of a computer device provided for an exemplary embodiment of the present invention. Detailed Implementation
[0044] The present invention will be further described below with reference to the accompanying drawings and embodiments.
[0045] It should be noted that the following detailed descriptions are exemplary and intended to provide further illustration of the invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this invention pertains.
[0046] Early RAG methods (such as SR-RAG, FL-RAG, and FS-RAG) relied on static, rule-based heuristics to trigger retrieval. These methods often exhibited significant biases across different question-answering scenarios. Recent methods (such as FLARE and DRAGIN) employ confidence-based triggering mechanisms, determining whether retrieval is necessary by monitoring the probability of the next output lexical unit; retrieval is triggered when the confidence level falls below a preset threshold. However, localized low confidence does not always indicate a lack of knowledge; it may stem from language variations or discourse incoherence. Lexical-level confidence is often too fine-grained and prone to false triggers due to local uncertainties. Some researchers have mitigated this problem by introducing an MLP-based external classifier trained to detect hallucinations from semantic drift. However, this approach suffers from limited generalization ability and a high dependence on manually labeled training data. Therefore, a key challenge lies in how to leverage the generative model's own behavior to estimate its knowledge boundaries and trigger retrieval without relying on an additional classifier. In light of this, this implementation proposes a dynamic retrieval enhancement generation method based on attention attribution, such as... Figure 1 As shown, the process includes the following:
[0047] S101: For a given input question, use a large language model to generate multiple different sequences of candidate answers;
[0048] S102: Extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix;
[0049] S103: When the hallucination score is less than or equal to the set threshold, it is determined that there is no risk of hallucination, and the optimal answer in the candidate answer sequence is used as the output answer corresponding to the input question;
[0050] S104: When the hallucination score is greater than the set threshold, it is determined that there is a risk of hallucination. The input question is concatenated with the best answer in the candidate answer sequence to generate a complete semantic input. The complete semantic input is segmented into sentences. The hallucination score is calculated for each generated sentence. The sentences with the lowest hallucination scores are retained. A semantic graph is constructed based on the retained sentences. Keywords are obtained from the semantic graph. The final search query is formed based on the keywords to obtain the final output answer.
[0051] More specifically, this implementation captures both the uncertainty of semantic propagation and the uncertainty of generation. As the core decision-making mechanism for dynamic retrieval triggering, this invention adopts a few-sample prompting strategy to guide the large language model to perform Transformer decoding. In order to measure the consistency of attention flow, multiple samples are generated through sampling decoding.
[0052] In step S101 of the present invention, it is set that This indicates an input problem. This indicates that a large language model is used to generate multiple different candidate answer sequences.
[0053] In step S102 of the present invention, the attention attribution matrix is extracted from the first attention head of the last layer. ,in, , Representing the The total number of lexical elements in the candidate answer sequence, Represents the set of real numbers. Representing the The attention attribution matrix corresponds to each candidate answer sequence; more specifically, for each generated candidate sequence, it is merged with the input question, input into a large language model, and the attention attribution matrix is extracted from the Transformer structure of the large language model (preferably the first attention head of the last layer). These attention attribution matrices are lower triangular matrices that encode causal relationships. This invention focuses on the causal region between input and output, which represents the semantic contribution of each input lexical to each output lexical. Specifically, this invention extracts the following attention sub-matrices:
[0054] (1);
[0055] in, Representing the The attention attribution matrix corresponding to each candidate answer sequence, within the range This represents the input token. Indicates the output word, The dividing point between the input and output is marked. To output the length of the answer, To input the length of the question, This represents the total number of candidate answer sequences.
[0056] This invention calculates the average attention weight along the rows of each attention submatrix to obtain the average semantic contribution (i.e., as the attribution score) of all input words:
[0057] (2);
[0058] Each of them It is a column vector that reflects the first... The average contribution strength of input terms in the generation process of each candidate answer sequence.
[0059] To evaluate attribution consistency among samples, for each input lexical unit... Calculate the mean Standard deviation and coefficient of variation :
[0060] (3);
[0061] (4);
[0062] (5);
[0063] in, Representing the The nth candidate answer sequence Attribution scores for each word element. Used to prevent division by zero errors. is the coefficient of variation, representing the relative volatility of attention, and N represents the total number of candidate answer sequences.
[0064] This invention further introduces logarithmic transformation:
[0065] (6);
[0066] in, Representing the large language model for the first The uncertain score of each generated lexical unit, This reflects the consistency of the model in using input lexical units as a basis during the generation process; The higher the score, the more inconsistent the large language model's reliance on input information when generating answers, and the more likely it is to produce illusions.
[0067] To capture external generation uncertainty, this invention introduces word-level perplexity under greedy decoding. A deterministic answer sequence is generated using the same model through greedy decoding, and the perplexity (PPL) of the sequence based on attention attribution is calculated. Perplexity measures the model's prediction uncertainty for the sequence, and a higher score indicates that the model is more uncertain.
[0068] More specifically, in steps S103 and S104 of the present invention, when generating each output word... At that time, the most likely word is selected and its probability is recorded. The two indicators are combined to obtain a unified hallucination score:
[0069] (7);
[0070] in, Represents the number of output tokens; Represents the context of the input. Represents output tokens The predicted probability; It captures the instability of internal semantic propagation, and perplexity captures the uncertainty of external behavior; a higher perplexity... The score indicates that the model lacks sufficient knowledge to support its generation, and an external retrieval should be triggered. Represents the generation of the first Output words The sequence of terms that has already been generated.
[0071] if If the generated answer is deemed to have a high risk of hallucination, an external retrieval mechanism (such as RAG) is triggered to obtain more contextual information to assist in the generation process.
[0072] if If the generated answer is deemed to have a low risk of illusion, the answer can be accepted or the generation process can continue.
[0073] Once the need for external retrieval is identified, DARAG's next step is to construct high-quality retrieval queries. Existing methods typically extract low-confidence words from the generated text as query keywords to guide the retrieval process. However, this confidence-based strategy has two main limitations: (1) low-confidence words may not be the true source of missing semantic information and may introduce noise or irrelevant content; (2) these methods often ignore syntactic structure and semantic dependencies, failing to capture broader contextual meaning.
[0074] To improve the semantic relevance and context alignment of constructed queries, DARAG introduces a graph-based keyword extraction method. Specifically, this invention constructs a semantic graph to identify context-aware and semantically representative keywords from the generated content. First, the original question is concatenated with the initially generated answer to form a complete semantic input. Since the initial generation may contain illusions, this invention proposes a sentence-level illusion filtering mechanism. This invention uses SpaCy to segment the complete semantic input into sentences and then filters each sentence... Calculate illusion score The longer the sentence and the more uncertain the prediction, the higher the likelihood that it will be considered a hallucination.
[0075] Specifically, including:
[0076] (8);
[0077] in, Representative sentence The Each word element, Representative sentence lexical number, This represents the predicted probability.
[0078] Based on the calculated hallucination score The process involves deleting a predetermined number of sentences with low scores (i.e., filtering out sentences with high illusion scores). After illusion filtering, a semantic graph is constructed based on the remaining sentences. Specifically, candidate keywords from the remaining sentences are used as nodes to form a node set. If two candidate words co-occur within the sliding window, an edge is created between them, forming an edge set. To further capture the strength of semantic associations, TF-IDF-based weights are assigned to the edges, resulting in a weighted semantic graph. :
[0079] (9);
[0080] in, , It is a node and The weights between nodes are then determined. Subsequently, this invention applies the Text Rank algorithm (a graph-based ranking method) to calculate an importance score for each node. In the The importance score is updated as follows in the next iteration:
[0081] (10);
[0082] in, It is 0.85. The part represents the probability of randomly selecting other nodes (to ensure algorithm convergence). The part multiplied represents the same as All related weights, express and The incoming edges of the nodes are connected. represent point to The weights are symmetric (undirected). In Indicates all with The nodes connected by outgoing edges, in the formula for Summation was performed, and the fraction on the right is equivalent to calculating from... Out pointing The probability, Represents the calculation of the previous iteration Importance score, through Importance score and point to The probability is calculated. Importance score represent point to The weight.
[0083] Continue until convergence, and finally, the present invention from Choose the highest score Each node serves as the final keyword, forming the search query. :
[0084] (11);
[0085] in, This represents sorting the set of scores S of the nodes and selecting the elements corresponding to the top K highest scores. Representative node The score.
[0086] Based on the hallucination detection results, this invention further introduces semantic correction to improve the factual accuracy and language quality of the generated answers. Specifically, this invention designs two different prompting strategies to guide the Large Language Model (LLM) to semantically enrich and reconstruct the original response.
[0087] For answers that are factually correct but incomplete or unclear (i.e., answers that do not contain obvious hallucinations but whose information expression is incomplete), a conclusion-oriented prompt is adopted to encourage the model to generate concise and clear answers based on existing content (i.e., more refined and clear conclusive answers). For questions containing hallucinatory information, false statements, or factual inconsistencies, this invention adopts a reading comprehension-based prompt. This prompt utilizes external document information retrieved by the upstream module to guide the large language model to revise the original answer based on an understanding of the full text context through error correction, information supplementation, and semantic rewriting, within the context of a broader understanding of source materials. This makes semantic refinement more accurate and context-aware, thereby achieving more precise semantic correction.
[0088] Figure 2 An attention-attribution-based dynamic retrieval enhancement generation system is shown, comprising:
[0089] The candidate answer generation unit 201 is configured to generate multiple different candidate answer sequences using a large language model for a given input question.
[0090] The hallucination score calculation unit 202 is configured to: extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix.
[0091] The first hallucination processing unit 203 is configured to: when the hallucination score is less than or equal to a set threshold, determine that there is no risk of hallucination, and use the best answer in the candidate answer sequence as the output answer corresponding to the input question;
[0092] The second hallucination processing unit 204 is configured to: determine the risk of hallucination when the hallucination score is greater than a set threshold; concatenate the input question with the best answer in the candidate answer sequence to generate a complete semantic input; segment the complete semantic input into sentences; calculate the hallucination score for each sentence; retain the initial generated sentences with the lowest set number of hallucination scores and the complete input question, arrange them in the original order; construct a semantic graph based on the retained sentences; obtain keywords based on the semantic graph; and compose the final retrieval query statement based on the keywords to obtain the final output answer.
[0093] It is understood that the aforementioned units can be individually or entirely merged into one or more other units, or some of the units can be further divided into multiple functionally smaller units. This achieves the same operation without affecting the technical effects of the embodiments of the present invention. The aforementioned units are based on logical functional division. In practical applications, the function of one unit can be implemented by multiple units, or the function of multiple units can be implemented by one unit. In other embodiments of the present invention, the system may also include other units. In practical applications, these functions can also be implemented with the assistance of other units, and can be implemented collaboratively by multiple units.
[0094] According to another embodiment of the present invention, the system of this embodiment can be constructed by running a computer program (including program code) capable of performing the steps involved in the corresponding method of the present invention on a general-purpose computing device, such as a computer, which includes processing elements and storage elements such as a central processing unit (CPU), random access memory (RAM), and read-only memory (ROM). The computer program can be recorded on, for example, a computer-readable recording medium, loaded into the aforementioned computing device through the computer-readable recording medium, and run therein.
[0095] Figure 3 A computer device is shown, which includes a processor 301, a communication interface 302, and a computer-readable storage medium 303. The processor 301, communication interface 302, and computer-readable storage medium 303 can be connected via a bus or other means.
[0096] The communication interface 302 is used to receive and send data. The computer-readable storage medium 303 can be stored in the memory of the electronic device. The computer-readable storage medium 303 is used to store computer programs, which include program instructions. The processor 301 is used to execute the program instructions stored in the computer-readable storage medium 303.
[0097] The processor 301 is the computing and control core of the electronic device. It is suitable for implementing one or more instructions, specifically for loading and executing one or more instructions to achieve the corresponding method flow or corresponding function.
[0098] Processor 301 is configured to perform the following procedure:
[0099] For a given input question, a large language model is used to generate multiple different sequences of candidate answers;
[0100] Extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix;
[0101] When the hallucination score is less than or equal to a set threshold, it is determined that there is no risk of hallucination, and the optimal answer in the candidate answer sequence is used as the output answer corresponding to the input question;
[0102] When the hallucination score exceeds a set threshold, a hallucination risk is identified. The input question is then concatenated with the optimal answer from the candidate answer sequence to generate a complete semantic input. The complete semantic input is then segmented into sentences, and a hallucination score is calculated for each sentence. The sentences with the lowest hallucination scores are retained, and a semantic graph is constructed based on the retained sentences. Keywords are obtained from the semantic graph, and the final search query is formed based on the keywords to obtain the final output answer.
[0103] This invention also provides a computer-readable storage medium, which is a memory device in an electronic device for storing programs and data. It is understood that the computer-readable storage medium here may include both built-in storage media in the electronic device and extended storage media supported by the electronic device. The computer-readable storage medium provides storage space for storing the processing system of the electronic device.
[0104] Furthermore, this storage space also contains one or more instructions suitable for loading and execution by the processor. These instructions can be one or more computer programs (including program code). It should be noted that the computer-readable storage medium here can be high-speed RAM memory or unstable memory, such as at least one disk storage device; optionally, it can also be at least one computer-readable storage medium located remotely from the aforementioned processor.
[0105] In one embodiment, the computer-readable storage medium stores one or more instructions; the processor loads and executes the one or more instructions stored in the computer-readable storage medium to perform the following process:
[0106] For a given input question, a large language model is used to generate multiple different sequences of candidate answers;
[0107] Extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix;
[0108] When the hallucination score is less than or equal to a set threshold, it is determined that there is no risk of hallucination, and the optimal answer in the candidate answer sequence is used as the output answer corresponding to the input question;
[0109] When the hallucination score exceeds a set threshold, a hallucination risk is identified. The input question is then concatenated with the optimal answer from the candidate answer sequence to generate a complete semantic input. The complete semantic input is then segmented into sentences, and a hallucination score is calculated for each sentence. The sentences with the lowest hallucination scores are retained, and a semantic graph is constructed based on the retained sentences. Keywords are obtained from the semantic graph, and the final search query is formed based on the keywords to obtain the final output answer.
[0110] The present invention also provides a computer program product or computer program comprising computer instructions stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the electronic device to perform the following process:
[0111] For a given input question, a large language model is used to generate multiple different sequences of candidate answers;
[0112] Extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix;
[0113] When the hallucination score is less than or equal to a set threshold, it is determined that there is no risk of hallucination, and the optimal answer in the candidate answer sequence is used as the output answer corresponding to the input question;
[0114] When the hallucination score exceeds a set threshold, a hallucination risk is identified. The input question is then concatenated with the optimal answer from the candidate answer sequence to generate a complete semantic input. The complete semantic input is then segmented into sentences, and a hallucination score is calculated for each sentence. The sentences with the lowest hallucination scores are retained, and a semantic graph is constructed based on the retained sentences. Keywords are obtained from the semantic graph, and the final search query is formed based on the keywords to obtain the final output answer.
[0115] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed in this invention can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can implement the described functions using different methods for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0116] In the above embodiments, implementation can be achieved, in whole or in part, through software, hardware, firmware, or any combination thereof. When implemented in software, it can be implemented, in whole or in part, as a computer program product. A 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 flow or function according to the embodiments of the present invention is generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in or transmitted through a computer-readable storage medium. The computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic cable, digital cable) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data processing device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid-state drive), etc.
[0117] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A dynamic retrieval enhancement generation method based on attention attribution, characterized in that, The process includes the following: For a given input question, a large language model is used to generate multiple different sequences of candidate answers; Extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix; When the hallucination score is less than or equal to a set threshold, it is determined that there is no risk of hallucination, and the optimal answer in the candidate answer sequence is used as the output answer corresponding to the input question; When the hallucination score is greater than a set threshold, a hallucination risk is determined. The input question is concatenated with the best answer in the candidate answer sequence to generate a complete semantic input. The complete semantic input is segmented into sentences, and a hallucination score is calculated for each sentence. The sentences with the lowest hallucination scores are retained. A semantic graph is constructed based on the retained sentences. Keywords are obtained from the semantic graph. The final search query is formed based on the keywords to obtain the final output answer.
2. The dynamic retrieval enhancement generation method based on attention attribution as described in claim 1, characterized in that, The illusion score is calculated based on the attention submatrix, including: Determine the attribution score of each input word in the attention submatrix. Based on the attribution score of each input word, determine the uncertainty score of each input word. Calculate the perplexity of the input question. Combine the uncertainty score with the perplexity score to calculate the final illusion score.
3. The dynamic retrieval enhancement generation method based on attention attribution as described in claim 2, characterized in that, ; in, Represents the number of output tokens. Represents the context of the input. Represents output tokens The predicted probability, Representing the large language model for the first Uncertain score for each input word, Represents the generation of the first each word element The already generated output word sequence, Represents illusion score, This represents an exponential function.
4. The dynamic retrieval enhancement generation method based on attention attribution as described in claim 2, characterized in that, Determine the attribution score for each input word in the attention submatrix, including: Each row of the attention submatrix represents a different output term, and each column of the attention submatrix represents a different input term. The average semantic contribution of all input terms to each output term is calculated as the attribution score.
5. The dynamic retrieval enhancement generation method based on attention attribution as described in claim 2, characterized in that, Based on the attribution scores of each input word, determine the uncertainty score for each input word in the attention submatrix. ,include: ; in, Represents the coefficient of variation, according to the... The nth candidate answer sequence The attribution score of the input word is calculated. The mean of the input words and standard deviation According to the The mean of the input words and standard deviation Determine the coefficient of variation .
6. The dynamic retrieval enhancement generation method based on attention attribution as described in any one of claims 1-5, characterized in that, Extract the corresponding attention attribution matrix from each candidate answer sequence, including: The candidate answer sequence is merged with the input question and fed into a large language model to extract the attention attribution matrix.
7. The dynamic retrieval enhancement generation method based on attention attribution as described in any one of claims 1-5, characterized in that, Calculate the illusion score for each sentence, including: Calculate the predicted probability of each word in the sentence, multiply the predicted probability of each word in the sentence by the logarithm of the predicted probability, and then sum them to obtain the illusion score of the sentence.
8. The dynamic retrieval enhancement generation method based on attention attribution as described in claim 7, characterized in that, ; in, Representative sentence The Each word element, Representative sentence The total number of lexical units, Represents the predicted probability. Representative sentence The illusion of points.
9. The dynamic retrieval enhancement generation method based on attention attribution as described in any one of claims 1-5, characterized in that, A semantic graph is constructed based on the retained sentences. Keywords are obtained from the semantic graph. The final search query is then formed based on the keywords to obtain the final output answer, including: Using candidate keywords from the retained sentences as nodes, a node set is formed. If two candidate words co-occur within the sliding window, an edge is created between them, forming an edge set. We assign weights to the edges between nodes to obtain a weighted semantic graph. ,in, , It is a node and The weights between nodes are used to iteratively calculate the importance score of each node until convergence. Choose the highest score Each node serves as the final keyword, forming the search query statement.
10. The dynamic retrieval enhancement generation method based on attention attribution as described in claim 9, characterized in that, node In the Importance score in the next iteration for: ; in, Represents the weighting coefficient. express and The incoming edges of the nodes are connected. represent point to The weight, In Indicates all with The nodes connected by the outgoing edges. Representing the The next iteration is calculated Importance score, represent point to The weight.
11. A dynamic retrieval enhancement generation system based on attention attribution, characterized in that, include: The candidate answer generation unit is configured to generate multiple different sequences of candidate answers for a given input question using a large language model. The hallucination score calculation unit is configured to: extract the corresponding attention attribution matrix based on each candidate answer sequence, extract the attention sub-matrix based on the attention attribution matrix, and calculate the hallucination score based on the attention sub-matrix. The first hallucination processing unit is configured to: when the hallucination score is less than or equal to a set threshold, determine that there is no risk of hallucination, and use the best answer in the candidate answer sequence as the output answer corresponding to the input question; The second hallucination processing unit is configured to: determine the risk of hallucination when the hallucination score is greater than a set threshold; concatenate the input question with the best answer in the candidate answer sequence to generate a complete semantic input; segment the complete semantic input into sentences; calculate the hallucination score for each sentence; retain the sentences with the lowest set number of hallucination scores; construct a semantic graph based on the retained sentences; obtain keywords based on the semantic graph; and compose the final retrieval query statement based on the keywords to obtain the final output answer.
12. The dynamic retrieval enhancement generation system based on attention attribution as described in claim 11, characterized in that, In the second illusion processing unit, a semantic map is constructed based on the retained sentences. Keywords are obtained from the semantic map, and the final retrieval query is formed based on the keywords to obtain the final output answer, including: Using candidate keywords from the retained sentences as nodes, a node set is formed. If two candidate words co-occur within the sliding window, an edge is created between them, forming an edge set. We assign weights to the edges between nodes to obtain a weighted semantic graph. ,in, , It is a node and The weights between them are used to iteratively calculate the illusion score for each node, continuing until convergence. Choose the highest score Each node serves as the final keyword, forming the search query statement.
13. A computer device, characterized in that, include: Processor and computer-readable storage media; A processor, adapted to execute computer programs; A computer-readable storage medium storing a computer program that, when executed by the processor, implements the attention-attribution-based dynamic retrieval enhancement generation method as described in any one of claims 1 to 10.
14. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program adapted to be loaded by a processor and executed as described in any one of claims 1 to 10 for the dynamic retrieval enhancement generation method based on attention attribution.
15. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the attention-attribution-based dynamic retrieval enhancement generation method as described in any one of claims 1 to 10.
Citation Information
Patent Citations
Fine-grained large model illusion processing method and system based on zero resource framework
CN119670760A
RAG query method and system capable of inhibiting hallusion problem of generative large model, medium and product
CN120744049A