A retrieval augmentation hallucination detection method based on double-path internal state forcing
By constructing a hallucination detection method with dual-path internal state forcing, the tension of the Logit space and hidden state space of large language models is quantified, which solves the problems of high computational cost or insufficient accuracy in existing technologies and achieves efficient and accurate hallucination detection.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- HANGZHOU JUNTONG FUTURE TECHNOLOGY CO LTD
- Filing Date
- 2026-03-31
- Publication Date
- 2026-07-31
AI Technical Summary
Existing hallucination detection technologies suffer from high computational overhead or insufficient accuracy in large language models, making it difficult to effectively identify hallucinations generated by the model.
By constructing two inference paths, one with context and one without, and using a teacher-coercion mechanism to enable the model to reuse the generation trajectory with context under no-context conditions, the tension in the Logit space, hidden state space, and hierarchical evolution process is quantified. The heterogeneous state difference features are extracted and temporally aggregated, and then input into the binary classification model for decision-making.
It achieves accurate identification of model illusions with low computational overhead, significantly improves detection accuracy, reduces inference latency, and is suitable for real-time interactive systems.
Smart Images

Figure CN122490149A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of artificial intelligence and natural language processing technology, and in particular to a retrieval enhancement generation illusion detection method based on dual-path internal state coercion. Background Technology
[0002] In recent years, large language models have made groundbreaking progress in natural language generation tasks. To address the inherent problems of knowledge obsolescence and illusion in large models, Retrieval Augmented Generation (RAG) techniques have emerged. RAG retrieves relevant documents from external knowledge bases and uses them as context input to the model in order to generate fact-based answers.
[0003] However, RAG technology introduces a new risk—faithfulness hallucination. In this failure mode, although the model receives the correct retrieved document, its internal pre-trained parametric memory is too strong, or the model fails to correctly understand the context, causing it to ignore or misinterpret external evidence and instead generate erroneous information that does not match the retrieved content and relies solely on its internal memory.
[0004] Existing hallucination detection technologies have the following main drawbacks:
[0005] 1. Black-box sampling methods are too costly: Methods such as SelfCheckGPT rely on generating multiple (usually 5-20) random samples from the same query and detecting hallucinations by comparing the consistency between samples. Although this method does not require internal parameters, the computational cost increases linearly with the number of samplings, resulting in extremely high inference latency (usually above the second level), which cannot meet the needs of real-time interactive systems.
[0006] 2. Insufficient accuracy in single-inference white-box detection: Existing white-box methods (such as perplexity calculation or entropy) only utilize the probability distribution of a single inference. However, models trained with human feedback reinforcement learning (RLHF) often exhibit overconfidence, meaning they assign high confidence even when generating erroneous information. Due to the lack of a contrasting negative signal, these methods struggle to distinguish between certain facts and certain illusions.
[0007] Therefore, there is an urgent need for a RAG illusion detection technology that can not only delve into the microscopic generation mechanism of the model, but also maintain low computational overhead and effectively construct contrast signals. Summary of the Invention
[0008] The main objective of this invention is to provide a retrieval enhancement generation illusion detection method based on dual-path internal state forcing. This invention constructs two inference paths: one with context (CTX) and one without context (NOCTX). It utilizes a teacher forcing mechanism to force the model to reuse the generation trajectory with context under contextless conditions, thereby artificially creating a counterfactual comparative reference frame. By quantifying the tension of these two paths in the Logit space, hidden state space, and hierarchical evolution process, this invention can accurately identify when the model deviates from external evidence and slides into internal illusion, including the following steps:
[0009] S1: Obtain the input data for the retrieval enhancement generation task, including the user query, the retrieved external document set, and the response sequence to be detected generated by the large language model based on the user query and the external document set;
[0010] S2: Construct a dual parallel inference path, including a first input path and a second input path. The first input path is a context path that includes system instructions, user queries and external document sets, and the second input path is a context-free path that only includes system instructions and user queries.
[0011] S3: Control the large language model to propagate forward along the first input path and the second input path respectively. When reasoning along the second input path, force the words in the response sequence to be detected to be used as the input of the next time step in order, so that the two paths run under the same generation trajectory.
[0012] S4: During the inference process, extract the internal hidden state vectors of each layer and each time step of the large language model under the two paths and the Logit distribution of the output layer, and calculate the heterogeneous state difference features between the first input path and the second input path. The heterogeneous state difference features include knowledge conflict features, trajectory drift features and internal instability features.
[0013] Specifically, the calculation logic for knowledge conflict features is as follows:
[0014] Calculate target lexical The difference between the Logit values under the first input path and the second input path is denoted as the Logit deviation value. :
[0015]
[0016] When the Logit bias value is positive, it indicates that external documents have an increasing effect on the probability of generating the target token.
[0017] When the Logit deviation value is negative, obtain the absolute value of the Logit deviation value and compare the absolute value with the first preset threshold.
[0018] If the absolute value is greater than or equal to the first preset threshold, it is determined to be a potential knowledge conflict signal;
[0019] Simultaneously calculate the Jensen-Shannon divergence between the predicted probability distributions of the first and second input paths at the current time step;
[0020] When the Jensen-Shannon divergence value is greater than the second preset threshold, it is determined to be a potential knowledge conflict signal.
[0021] Specifically, trajectory drift characteristics include:
[0022] Based on hierarchical semantic dynamics, the hierarchical index of the Transformer model is regarded as a virtual time axis;
[0023] The hidden state vector of each Transformer layer is normalized.
[0024] Calculate the Euclidean distance between the normalized hidden state vectors of the two paths at each layer, and calculate its cumulative value across all layers. :
[0025]
[0026] Cumulative value The cost of internal semantic space adjustments that large language models are forced to make in order to maintain the currently generated content. For hierarchical indexes, This represents the total number of neural network layers in the large language model. This is the time step index for the currently generated content; This indicates that the large language model, under the first input path, is in the... Layer in time step The output hidden state vector; This indicates that the large language model is in the second input path, the first Layer in time step The output hidden state vector; Presentation layer normalization processing operation; This indicates the calculation and determination of the L2 norm.
[0027] Specifically, the internal instability characteristics include:
[0028] Select one or more intermediate layers of the large language model ;
[0029] Using Logit Lens technology, the middle layer The hidden states are mapped to the vocabulary space through the final embedding layer of the large language model, resulting in the intermediate layer prediction distribution. ;
[0030] Calculate the intermediate layer prediction distribution The final output layer prediction distribution of the large language model Kullback-Leibler divergence values between:
[0031]
[0032] The Kullback-Leibler divergence value is used to characterize the predictive power of the model as the inference depth increases. Figure 1 To the point of being responsive.
[0033] S5: Perform temporal aggregation processing on heterogeneous state difference features, and convert the word-level feature sequence into a response-level feature vector;
[0034] The temporal aggregation processing employs an adaptive sparse aggregation strategy, including:
[0035] Define the size of the dynamic filter window The calculation formula is as follows:
[0036]
[0037] in The total length of the response sequence, The preset sensitivity ratio coefficient, Minimum window value;
[0038] For each feature dimension, only the one with the largest value is extracted. Each lexical feature value is used to calculate its statistical value as the aggregated feature vector for that dimension.
[0039] S6: Input the response-level feature vector into a pre-trained binary classification model and output a judgment result on whether the response sequence is a hallucination;
[0040] The training phase of the binary classification model includes traversing a preset threshold range on the validation set and selecting the threshold that maximizes the F1 score as the decision threshold.
[0041] The binary classification model outputs a probability value between 0 and 1. The probability value is used to characterize the risk level of the response containing hallucinations. The judgment result is obtained by comparing the probability value with the judgment threshold.
[0042] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that the processor, when executing the program, implements a retrieval enhancement generation illusion detection method based on dual-path internal state forcing.
[0043] The present invention also provides a computer-readable storage medium having a computer program stored thereon, characterized in that, when the program is executed by a processor, it implements a retrieval enhancement generation illusion detection method based on dual-path internal state forcing.
[0044] The core innovation of this invention lies in:
[0045] 1. Construction of a counterfactual frame of reference: By constructing a shadow model (NOCTX path) that is deprived of the document but forced to say the same thing, a perfect frame of reference for contrast is created.
[0046] 2. Quantification of internal tension: Illusion is no longer an abstract concept, but is quantified as the tension between this shadow model and the real model in the Logit space (conflict), the hidden state space (drift), and the hierarchical evolution space (instability). Attached Figure Description
[0047] Figure 1 This is a schematic diagram of the overall process of a retrieval enhancement generation hallucination detection method based on dual-path internal state forcing provided by the present invention;
[0048] Figure 2 This is a schematic diagram of the architecture principle of dual-path construction and internal state forced reasoning in a retrieval enhancement generation illusion detection method based on dual-path internal state forcedness provided by the present invention.
[0049] Figure 3 This is a schematic diagram illustrating the principle of the Top-K sparse aggregation strategy in the retrieval enhancement generation illusion detection method based on dual-path internal state forcing provided by the present invention.
[0050] Figure 4 The diagram shows the hardware structure of the electronic device in the retrieval enhancement generation illusion detection method based on dual-path internal state forcing provided by the present invention. Detailed Implementation
[0051] The present invention will now be described in further detail with reference to the accompanying drawings and specific embodiments.
[0052] This invention proposes a retrieval enhancement generation hallucination detection method based on dual-path internal state coercion, which mainly includes six core steps: data acquisition, dual-path construction, internal state coercion reasoning, heterogeneous feature extraction, sparse aggregation, and hallucination judgment.
[0053] S1: Obtain the input data for the retrieval enhancement generation task, including the user query, the retrieved external document set, and the response sequence to be detected generated by the large language model based on the user query and the external document set;
[0054] S2: Construct a dual parallel inference path, including a first input path and a second input path. The first input path is a context path that includes system instructions, user queries and external document sets, and the second input path is a context-free path that only includes system instructions and user queries.
[0055] S3: Control the large language model to propagate forward along the first input path and the second input path respectively. When reasoning along the second input path, force the words in the response sequence to be detected to be used as the input of the next time step in order, so that the two paths run under the same generation trajectory.
[0056] S4: During the inference process, extract the internal hidden state vectors of each layer and each time step of the large language model under the two paths and the Logit distribution of the output layer, and calculate the heterogeneous state difference features between the first input path and the second input path. The heterogeneous state difference features include knowledge conflict features, trajectory drift features and internal instability features.
[0057] S5: Perform temporal aggregation processing on heterogeneous state difference features, and convert the word-level feature sequence into a response-level feature vector;
[0058] S6: Input the response-level feature vector into a pre-trained binary classification model and output the judgment result of whether the response sequence is a hallucination.
[0059] Reference Figure 1 In step S1, the data acquisition and preprocessing system first acquires the triplet input data for the retrieval enhancement generation task. ,in:
[0060] : User-input query;
[0061] The collection of external documents returned by the retrieval system.
[0062] Large language models based on user queries and external document collection The generated response sequence to be detected is represented as ,in For the first Tokens generated at each time step.
[0063] Reference Figure 1 In step S2, dual-path construction is performed to probe the dependence of a large language model on external documents. This invention constructs two parallel input paths:
[0064] Context Path (CTX Path): This is the original context generated by the large language model. The input Prompt template is designed to include system instructions, search documents, and user queries.
[0065] Example:
[0066] Prompt_CTX="[INST]You are a helpful assistant.Use the followingcontext to answer the question.Context:{D}Question:{q}[ / INST]"
[0067] No-Context Path: This is a counterfactual context used for comparison, deliberately stripping away external evidence. The input prompt template is designed to contain only system commands and user queries.
[0068] Example:
[0069] Prompt_NOCTX="[INST]You are a helpful assistant.Answer the questiondirectly.Question:{q}[ / INST]"
[0070] Reference Figure 1 and Figure 2 In step S3, under normal circumstances, the response generated by the large language model under system instructions without context path may be completely different from the response generated under system instructions with context path. In order to compare the internal state differences under the same semantic trajectory, the present invention adopts the teacher forcing mechanism.
[0071] Specifically,
[0072] Context path reasoning involves inputting the context path prompt word Prompt_CTX into a large language model. The context path prompt word Prompt_CTX is a complete input sequence consisting of a user query command and an external retrieved document / context. The model also includes the response sequence to be detected. As the target sequence; during the reasoning process, each layer is recorded. Each time step Hidden state and the Logit distribution of the output layer .
[0073] Context-free path reasoning involves inputting the context-free path prompt word Prompt_NOCTX into a large language model. Prompt_NOCTX contains only the user's query instruction and does not include external search documents. Regardless of the word with the highest probability predicted by the large language model in the current step, the target response sequence is forced to be included. Lexical elements in As the first The input is not subjected to new autoregressive sampling; in this way, the large language model can interpret and generate lexical units without browsing the document. The reason.
[0074] The hidden state extracted at this time Logit distribution of the output layer This reflects the perplexity or loss value of a large language model generating content solely from internal memory.
[0075] Reference Figure 1 In step S4, specifically, the knowledge conflict features are used to quantify the conflict between external evidence and internal memory. They consist of two parts: Logit bias features at the local lexical level and distribution divergence (JSD) features at the global prediction distribution level.
[0076] Furthermore, the formula for calculating Logit bias is:
[0077]
[0078] The sign and magnitude of the Logit bias are used to quantify the modulating effect of external documents on the generation probability. When the bias value is positive, its magnitude is positively correlated with the degree of evidence support, representing the boosting effect of external documents on the generation probability of the target token; when the bias value is negative, its absolute value is positively correlated with the degree of evidence deficiency, representing the suppressing effect of external documents on the generation probability. In this embodiment, a first preset threshold is used as the benchmark for hallucination determination. When the absolute value of the negative bias is greater than or equal to the first preset threshold, it is determined that the large language model forcibly generates the token without the support of valid evidence, constituting a strong signal of hallucination features.
[0079] It should be noted that the aforementioned first preset threshold is not a fixed value, and those skilled in the art can adaptively adjust it according to the actual situation. For example, in scenarios requiring high-sensitivity detection, the first preset threshold can be set to a relatively low value; while in scenarios requiring strong anti-interference capabilities, the first preset threshold can be set to a relatively high value. The specific value selection can be optimized by those skilled in the art through a limited number of conventional experiments, and will not be elaborated upon here.
[0080] Simultaneously, the Jensen-Shannon divergence value between the predicted probability distributions of the context path and the non-context path at the current time step is calculated to quantify the degree of perturbation of the external document on the overall prediction distribution of the large model. When the Jensen-Shannon divergence value is greater than the second preset threshold, it indicates that the external document has caused a serious perturbation to the overall prediction distribution of the large language model, and the large language model is in a state of cognitive instability, constituting a strong signal of hallucination features.
[0081] It should also be noted that the second preset threshold is not a fixed value. Those skilled in the art can adaptively adjust it according to the actual situation and the sensitivity requirements to distribution differences. For example, in scenarios requiring extremely high stability and consistency of model output, a smaller second preset threshold (i.e., low tolerance for differences) can be set; while in scenarios allowing for some distribution fluctuations, a larger second preset threshold can be set. The specific numerical selection can be optimized by those skilled in the art through a limited number of routine experiments testing the classification / detection accuracy at different thresholds, and will not be elaborated further here.
[0082] Specifically, drift features, based on layer-wise semantic dynamics (LSD), capture logical breaks in the reasoning process and transform the Transformer's hierarchy. It is considered as a timeline of semantic evolution.
[0083] Based on hierarchical semantic dynamics, the hierarchical index of the Transformer model is regarded as a virtual time axis;
[0084] The hidden state vector of each Transformer layer is normalized.
[0085] Calculate the Euclidean distance between the context path and the contextless path at each layer's normalized hidden state vector, and compute its cumulative value across all layers. :
[0086]
[0087] Cumulative value The cost of internal semantic space adjustments that large language models are forced to make in order to maintain the currently generated content. For hierarchical indexes, This represents the total number of neural network layers in the large language model. This is the time step index for the currently generated content; This indicates that the large language model, under the context path, is at the first... Layer in time step The output hidden state vector; This indicates that the large language model, in the absence of contextual paths, has the following characteristics: Layer in time step The output hidden state vector; Presentation layer normalization processing operation; This indicates the calculation and determination of the L2 norm.
[0088] In normal reasoning, the semantic trajectories of context-based reasoning and context-free reasoning should remain relatively parallel or gradually converge. If the cumulative value... The appearance of a spike at a certain point indicates that the large language model is forced to make drastic semantic space adjustments at a deep level in order to maintain the current generated content. This often corresponds to the large language model experiencing hallucinations.
[0089] Specifically, Instability Features utilize Logit Lens technology to detect instabilities within large language models.
[0090] The intermediate layer of the large language model is selected as the probe layer. For example, for a 32-layer model, select layer 16 and view its hidden states. By directly projecting onto the vocabulary space, the intermediate layer prediction distribution is obtained. .
[0091] Calculate the intermediate layer prediction distribution With the final layer output distribution The KL divergence is expressed as:
[0092]
[0093] For factual knowledge, large language models typically determine the answer at the shallow level, such as "The capital of France is..." -> "Paris," with deeper levels merely reinforcing this belief, resulting in low KL divergence. However, for illusory content, large language models often exhibit a reversal or uncertainty in predictive intent between shallow and deep levels, leading to high KL divergence.
[0094] Reference Figure 1 and Figure 3 In step S5, the heterogeneous state difference features are subjected to temporal aggregation processing to convert the word-level feature sequence into a response-level feature vector.
[0095] Specifically, the temporal aggregation process includes obtaining the temporal feature matrix. Subsequently, it needs to be compressed into a response-level vector. Since large model illusions often exhibit sparsity in long texts (e.g., only one entity error), traditional global average pooling will dilute the signal. This invention proposes an adaptive sparse aggregation strategy, including:
[0096] Dynamic window definition:
[0097] Define filter window :
[0098]
[0099] in For response length, The sensitivity coefficient, The minimum window is preferably set to a sensitivity coefficient of 0.1 and a minimum window value of 5. This calculation ensures that the aggregation window scales linearly with the text length, achieving scale invariance.
[0100] Top-K feature extraction:
[0101] For each column of the feature matrix, i.e., each feature dimension Select the largest value independently Calculate the mean of the given values:
[0102]
[0103] in,
[0104]
[0105] Statistical calculation:
[0106] In addition to the Top-K mean, it can also calculate the global mean, standard deviation and maximum value, etc., which together form the response-level feature vector. This strategy effectively solves the problem that sparse anomalous signals in long texts are submerged by background noise, and significantly improves the signal-to-noise ratio of detection.
[0107] Reference Figure 1 In step S6, the response-level feature vector is input into a pre-trained binary classification model, and the output is a judgment result on whether the response sequence is hallucinatory.
[0108] Preferably, the training phase of the binary classification model includes dynamically calibrating the decision threshold through cross-validation. Specifically, the training phase of the binary classification model includes traversing a preset threshold range on the validation set and selecting the threshold that maximizes the F1 score as the decision threshold.
[0109] The binary classification model outputs a probability value between 0 and 1. The probability value is used to characterize the risk level of the response containing hallucinations. The judgment result is obtained by comparing the probability value with the judgment threshold.
[0110] To verify the effectiveness of this invention, extensive tests were conducted on two datasets: RagTruth (General RAG Task) and HalluRAG (Knowledge Conflict Task), with test bases including LLaMA2-7B / 13B and LLaMA3-8B.
[0111] Table 1: Performance comparison of the present invention and existing state-of-the-art methods on the RagTruth dataset (LLaMA2-7B)
[0112] Uncertainty Perplexity 0.5307 0.6107 0.5124 -0.0644 81.5 Sampling method SelfCheckGPT 0.5970 0.5734 0.4787 0.1036 >2000 Explanatory ReDeEP 0.7522 0.7528 0.5059 0.1036 ~120 This invention DISF 0.7947 0.8133 0.6854 0.2043 211.4
[0113] The results show that:
[0114] The detection accuracy is significantly better than the baseline: the area under the ROC curve of this invention reaches 0.7947, which is 4.2% higher than the strongest white-box baseline RedeEP and nearly 20% higher than the expensive sampling method SelfCheckGPT. This proves that the dual-path forcing mechanism can effectively capture illusory signals that cannot be detected by single path.
[0115] Strong perception of the severity of hallucinations: The PCC (Pearson correlation coefficient) reached 0.2043, which is twice that of RedeEP, indicating that the score output by this invention is highly positively correlated with the proportion of hallucinations in the response.
[0116] High computational efficiency: Although this invention introduces a dual-path approach, increasing the inference latency to 211.4ms, this is only about 2.6 times that of native inference (81.5ms), and far lower than the sampling method (which requires multiple generations and has a delay of several seconds). This characteristic makes this invention feasible for real-time deployment in a production environment.
[0117] In summary, this invention, through a dual-path internal state forcing architecture, successfully constructs the contrast signal required for detecting RAG hallucinations without significantly increasing computational costs, solving the problem of traditional methods failing in the face of overconfidence hallucinations. It has significant technological advancements and practical value.
[0118] Reference Figure 4 The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that the processor, when executing the program, implements a retrieval enhancement generation illusion detection method based on dual-path internal state forcing.
[0119] The present invention also provides a computer-readable storage medium having a computer program stored thereon, characterized in that, when the program is executed by a processor, it implements a retrieval enhancement generation illusion detection method based on dual-path internal state forcing.
[0120] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of a necessary general-purpose hardware platform, or by a combination of hardware and software. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a computer product. The present invention can be implemented in the form of a computer program product on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0121] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Other embodiments may also be used. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. These modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A retrieval enhancement generation hallucination detection method based on dual-path internal state forcing, characterized in that, Includes the following steps: S1: Obtain the input data for the retrieval enhancement generation task, including user query, retrieved external document set, and the response sequence to be detected generated by the large language model based on the user query and the external document set; S2: Construct a dual parallel inference path, including a first input path and a second input path. The first input path is a context path that includes system instructions, the user query, and the external document set. The second input path is a context-free path that only includes the system instructions and the user query. S3: Control the large language model to propagate forward along the first input path and the second input path respectively. When reasoning along the second input path, force the words in the response sequence to be detected to be used as the input of the next time step in order, so that the two paths run under the same generation trajectory. S4: During the inference process, extract the internal hidden state vectors of each layer and each time step of the large language model under the two paths and the Logit distribution of the output layer, and calculate the heterogeneous state difference features between the first input path and the second input path. The heterogeneous state difference features include knowledge conflict features, trajectory drift features and internal instability features. S5: Perform temporal aggregation processing on the heterogeneous state difference features to convert the word-level feature sequence into a response-level feature vector; S6: Input the response-level feature vector into a pre-trained binary classification model and output a judgment result on whether the response sequence contains hallucinations.
2. The retrieval enhancement generation hallucination detection method based on dual-path internal state forcing according to claim 1, characterized in that, In step S4, the knowledge conflict features include Logit bias feature values and Jensen-Shannon divergence feature values, and the target wordpiece is calculated The difference between the Logit values under the first input path and the second input path is denoted as a Logit bias value : When the Logit deviation value is positive, it is determined that the external document has an increasing effect on the generation probability of the target token; When the Logit deviation value is negative, the absolute value of the Logit deviation value is obtained and compared with a first preset threshold. If the absolute value is greater than or equal to the first preset threshold, it is determined to be a potential knowledge conflict signal; Simultaneously calculate the Jensen-Shannon divergence value between the predicted probability distributions of the first input path and the second input path at the current time step; When the Jensen-Shannon divergence value is greater than the second preset threshold, it is determined to be a potential knowledge conflict signal.
3. The retrieval enhancement generation hallucination detection method based on dual-path internal state forcing according to claim 1, characterized in that, In step S4, the trajectory drift characteristics include: Based on hierarchical semantic dynamics, the hierarchical index of the Transformer model is regarded as a virtual time axis; The hidden state vector of each Transformer layer is normalized. Calculate the Euclidean distance between the normalized hidden state vectors of the two paths at each layer, and calculate its cumulative value across all layers. : The cumulative value The cost of internal semantic space adjustments that large language models are forced to make in order to maintain the currently generated content. For hierarchical indexes, This represents the total number of neural network layers contained in the large language model. This is the time step index for the currently generated content; This indicates that the large language model is in the first input path, the first... Layer in time step The output hidden state vector; This indicates that the large language model is in the second input path, the first... Layer in time step The output hidden state vector; Presentation layer normalization processing operation; This indicates the calculation and determination of the L2 norm.
4. The retrieval enhancement generation hallucination detection method based on dual-path internal state forcing according to claim 1, characterized in that, In step S4, the internal instability features include: Select one or more intermediate layers of the large language model ; Using Logit Lens technology, the intermediate layer The hidden states are mapped to the vocabulary space through the final embedding layer of the large language model, resulting in the intermediate layer prediction distribution. ; Calculate the intermediate layer prediction distribution The final output layer prediction distribution of the large language model Kullback-Leibler divergence values between , represented as: 。 5. The method for detecting hallucinations based on dual-path internal state forcing in retrieval enhancement according to claim 1, characterized in that, In step S5, the time-series aggregation processing employs an adaptive sparse aggregation strategy, including: Define the size of the dynamic filter window The calculation formula is as follows: in The total length of the response sequence, The preset sensitivity ratio coefficient, Minimum window value; For each feature dimension, only the one with the largest value is extracted. Each lexical feature value is used to calculate its statistical value as the aggregated feature vector for that dimension.
6. The retrieval enhancement generation hallucination detection method based on dual-path internal state forcing according to claim 1, characterized in that, In step S6, the training phase of the binary classification model includes traversing a preset threshold interval on the validation set and selecting the threshold that maximizes the F1 score as the decision threshold. The binary classification model outputs a probability value between 0 and 1, which is used to characterize the risk level of the response including hallucination. The judgment result is obtained based on the comparison of the probability value and the judgment threshold.
7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the method as described in any one of claims 1 to 6.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1 to 6.