Cross-domain illusion detection method and system for large language model

By selecting the optimal template and optimizing the feature extraction process, the problem of insufficient generalization ability of large language models in cross-domain hallucination detection is solved, and efficient and low-cost hallucination detection generalization is achieved.

CN121413613APending Publication Date: 2026-01-27NAT INNOVATION INST OF DEFENSE TECH PLA ACAD OF MILITARY SCI
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511239620.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-09-01
Publication Date
2026-01-27

AI Technical Summary

Technical Problem

Large language models struggle to effectively detect hallucinations in cross-domain applications, and existing methods suffer from insufficient detector generalization ability and high computational resource consumption.

Method used

By selecting the optimal template and optimizing feature extraction using the realism direction variance ratio of the candidate template, and combining it with a supervised learning framework to train a linear probe or neural network classifier, efficient cross-domain hallucination detection can be achieved.

Benefits of technology

It improves the detector's generalization performance in unknown domains, reduces computational resource consumption, and increases detection accuracy and efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121413613A_ABST
    Figure CN121413613A_ABST
Patent Text Reader

Abstract

The invention provides a large language model cross-domain illusion detection method and system, and the method comprises the steps: screening an optimal template from a candidate template set according to the authenticity direction variance ratio of each candidate template; splicing the unknown field test text with the optimal template to obtain a spliced test text; performing feature extraction on the spliced test text through a large language model to obtain a to-be-detected feature vector; and detecting the to-be-detected feature vector through the trained detector to obtain reliability data of the cross-domain test. Compared with the prior art that a cross-domain consistency mode is difficult to capture by features directly extracted by LLM and generalization ability of a detector is limited, the method has the advantages that the optimal template is determined according to the authenticity direction variance ratio of the candidate template, and the plug-and-play optimal template is used as a prompt template to optimize the feature extraction process, so that the accuracy of feature extraction is improved. The significance and cross-domain consistency of the authenticity related structure are enhanced, and the generalization performance of the detector in the unknown field is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of natural language processing technology, and in particular to a method and system for detecting illusions across domains using large-scale language models. Background Technology

[0002] In recent years, large language models (LLMs) have demonstrated remarkable capabilities in multiple fields; however, the text they generate may contain factual errors or misleading content, a phenomenon known as the "illusion" problem. To address this issue, commonly used techniques are mainly divided into two categories: unsupervised and supervised methods.

[0003] Unsupervised methods detect threats by analyzing the confidence level of the content generated by the model. Examples include methods based on generated lexical probabilities or evaluation through multi-response consistency. Methods relying on generation probability are susceptible to domain distribution biases and struggle to generalize across domains.

[0004] Supervised methods utilize the internal states of an LLM (Multilayer Perceptron), such as activation values ​​and embedding vectors, to train a classifier. For example, SAPLMA employs a multilayer perceptron, while the MM method uses linear probing to distinguish between true and false statements. However, after training in a specific domain, the truth-related structures in the internal states of supervised detectors are easily masked by domain-specific information, leading to a significant decrease in cross-domain generalization performance. For instance, research by Burger et al. (2024) shows that the grammatical structure of statements, such as affirmative and negative sentences, alters the structure of the internal states, causing the detector to fail in cross-structural scenarios.

[0005] The core reason is that while the internal state of an LLM implicitly contains realistic information, its pre-training objective is primarily language modeling. This leads to a high degree of coupling between realistically relevant structures and non-critical information, such as domain features and grammatical forms. Directly extracted features struggle to capture consistent patterns across domains, limiting the detector's generalization ability. Currently, there is an urgent need for a method to improve the detector's cross-domain performance when only single-domain data is available. Summary of the Invention

[0006] This invention provides a method and system for cross-domain hallucination detection using large-scale language models. It addresses the shortcomings of existing technologies where features directly extracted by LLMs are difficult to capture cross-domain consistency patterns, thus limiting the generalization ability of detectors. This invention enhances the saliency and cross-domain consistency of realistic related structures, thereby achieving efficient and low-cost hallucination detection generalization.

[0007] This invention provides a cross-domain hallucination detection method for large-scale language models, comprising the following steps: The optimal template is selected from the candidate template set based on the true orientation variance ratio of each candidate template; the candidate template set includes multiple candidate templates. The test text in the unknown domain is concatenated with the optimal template to obtain the concatenated test text; features are extracted from the concatenated test text using a large language model to obtain the feature vector to be detected. By using a trained detector, the feature vector to be detected is detected, and reliability data for cross-domain testing is obtained.

[0008] According to the cross-domain illusion detection method for large-scale language models provided by the present invention, the optimal template is selected from the candidate template set based on the variance ratio of the realism direction of each candidate template, including: Based on multiple basic instruction templates, a candidate template set is determined; Based on the true orientation of each candidate template in the labeled dataset, the true orientation variance ratio is calculated; based on the true orientation variance ratio of each candidate template, multiple candidate templates are sorted in descending order. Select a predetermined number of candidate templates from the sorted candidate templates as templates to be screened; determine the optimal template based on the results of manual review of the multiple templates to be screened.

[0009] According to the cross-domain illusion detection method for large-scale language models provided by the present invention, a candidate template set is determined based on multiple basic instruction templates, including: For each basic instruction template, perform the same semantic sentence transformation to obtain an intermediate template; For each intermediate template, perform syntax correction and semantic alignment to obtain the processed intermediate template; Multiple processed templates are deduplicated to obtain a candidate template set.

[0010] According to the large-scale language model cross-domain illusion detection method provided by the present invention, the variance ratio of the realism direction is calculated based on the realism direction of each candidate template on the labeled dataset, including: For each candidate template, each labeled text is concatenated with the candidate template to obtain the concatenated labeled text; features are extracted from the concatenated labeled text using a large language model to obtain the context embedding vector; the labeled dataset includes multiple labeled texts; each labeled text is labeled as true or false; Based on the multiple context embedding vectors corresponding to each candidate template and the labels of each labeled text, the average value of true statements and the average value of false statements in the candidate template are calculated. The direction of truthfulness of the candidate template is obtained by the difference between the average value of true statements and the average value of false statements in the candidate template. The true orientation variance ratio is calculated based on the projection of the candidate template's true orientation onto the data variance and the total variance of the multiple context embedding vectors corresponding to each candidate template.

[0011] The large-scale language model cross-domain hallucination detection method provided by the present invention includes a detector comprising: a classifier and a neural network; the method further includes: The training set text is concatenated with the optimal template, and features are extracted from the concatenated text to obtain the training set embedding vector; where the training set includes multiple training set texts. Based on multiple training set embedding vectors, the true orientation of the optimal template on the training set is calculated; The classifier calculates the projection value based on the realism orientation of the optimal template on the training set; the projection value is then compared with a preset threshold to obtain the true / false classification result. The detection result is obtained by performing classification calculations based on the training set embedding vectors and the true / false classification results through a neural network; the detector parameters are then optimized based on the detection result to obtain a trained detector.

[0012] According to the cross-domain illusion detection method for large-scale language models provided by the present invention, reliability data includes positive correlation results and index data; by using a trained detector to detect the feature vector to be detected, the reliability data of the cross-domain test is obtained, including: Based on the authenticity orientation of domain datasets in different fields, a cross-domain cosine similarity matrix is ​​obtained; the mean of the cross-domain cosine similarity matrix is ​​compared with the baseline to obtain a positive correlation result; The trained detector is used to detect the feature vectors to be detected, and the index data is obtained.

[0013] This invention also provides a large-scale cross-domain hallucination detection system for language models, comprising the following modules: The optimal template determination module is used to select the optimal template from the candidate template set based on the true orientation variance ratio of each candidate template; wherein, the candidate template set includes multiple candidate templates; The feature extraction module is used to concatenate the test text in the unknown domain with the optimal template to obtain the concatenated test text; and to extract features from the concatenated test text using a large language model to obtain the feature vector to be detected. The detection module is used to detect the feature vectors to be detected using a trained detector, thereby obtaining reliability data for cross-domain testing.

[0014] According to the present invention, a cross-domain hallucination detection system for large-scale language models is provided, wherein the optimal template determination module includes: The candidate template set determination submodule is used to determine the candidate template set based on multiple basic instruction templates; The Reality Orientation Variance Ratio Calculation Submodule is used to calculate the Reality Orientation Variance Ratio based on the reality orientation of each candidate template in the labeled dataset; and to sort multiple candidate templates in descending order based on the Reality Orientation Variance Ratio of each candidate template. The optimal template determination submodule is used to select a preset number of candidate templates from multiple sorted candidate templates as templates to be screened; and to determine the optimal template based on the results of manual review of multiple templates to be screened.

[0015] According to the large-scale language model cross-domain illusion detection system provided by the present invention, a candidate template set determination submodule is used to perform the same semantic sentence transformation on each basic instruction template to obtain an intermediate template; for each intermediate template, grammatical correction and semantic alignment are performed to obtain a processed intermediate template; and multiple processed templates are deduplicated to obtain a candidate template set.

[0016] According to the large-scale language model cross-domain illusion detection system provided by the present invention, the truth orientation variance ratio calculation submodule is used to concatenate each labeled text with the candidate template for each candidate template to obtain concatenated labeled text; extract features from the concatenated labeled text using a large-scale language model to obtain context embedding vectors; wherein, the labeled dataset includes multiple labeled texts; each labeled text is labeled as true or false; based on the multiple context embedding vectors corresponding to each candidate template and the label of each labeled text, the average value of true statements and the average value of false statements in the candidate template are calculated; based on the difference between the average value of true statements and the average value of false statements in the candidate template, the truth orientation of the candidate template is obtained; based on the projection of the truth orientation of the candidate template onto the data variance and the total variance of the multiple context embedding vectors corresponding to each candidate template, the truth orientation variance ratio is calculated.

[0017] The large-scale language model cross-domain hallucination detection system provided by the present invention includes a detector comprising: a classifier and a neural network; the system further includes: The training module is used to concatenate the training set text with the optimal template and extract features from the concatenated text to obtain the training set embedding vector; wherein, the training set includes multiple training set texts; Based on multiple training set embedding vectors, the true orientation of the optimal template on the training set is calculated; the projection value is obtained by calculating the true orientation of the optimal template on the training set using a classifier; the projection value is compared with a preset threshold to obtain the true / false classification result; the detection result is obtained by performing classification calculations based on the training set embedding vectors and the true / false classification result using a neural network; the parameters of the detector are optimized based on the detection result to obtain the trained detector.

[0018] The large-scale language model cross-domain hallucination detection system provided by the present invention includes reliability data, including positive correlation results and index data; and a detection module, comprising: The cross-domain correlation calculation submodule is used to calculate the cross-domain cosine similarity matrix based on the authenticity direction of the domain datasets in different domains; the mean of the cross-domain cosine similarity matrix is ​​compared with the baseline to obtain the positive correlation result; The indicator result calculation submodule is used to detect the feature vector to be detected using a trained detector and obtain indicator data.

[0019] 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, wherein the processor executes the computer program to implement the large language model cross-domain illusion detection method as described above.

[0020] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the large-scale language model cross-domain illusion detection method as described above.

[0021] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the large language model cross-domain illusion detection method as described above.

[0022] This invention provides a large-scale language model-based cross-domain hallucination detection method and system. The method selects the optimal template from a candidate template set based on the realism direction variance ratio of each candidate template. It then concatenates the unknown domain test text with the optimal template to obtain the concatenated test text. A large-scale language model is used to extract features from the concatenated test text, yielding a feature vector to be detected. Finally, a trained detector is used to detect the feature vector, obtaining reliable cross-domain test data. Compared to existing technologies where LLM-based feature extraction struggles to capture cross-domain consistency patterns, limiting the detector's generalization ability, this method determines the optimal template based on the realism direction variance ratio of candidate templates and optimizes the feature extraction process using a plug-and-play optimal template as a cue template. This enhances the saliency and cross-domain consistency of realism-related structures, improves the detector's generalization performance in unknown domains, and achieves efficient and low-cost hallucination detection generalization. Attached Figure Description

[0023] To more clearly illustrate the technical solutions in this invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0024] Figure 1 This is a flowchart illustrating the cross-domain illusion detection method for large-scale language models provided by the present invention.

[0025] Figure 2 This is a schematic diagram of the structure of the large-scale language model cross-domain illusion detection system provided by the present invention.

[0026] Figure 3 This is a schematic diagram of the structure of the electronic device provided by the present invention. Detailed Implementation

[0027] To make the objectives, technical solutions, and advantages of this invention clearer, the technical solutions of this invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this invention. All other embodiments obtained by those skilled in the art based on the embodiments of this invention without creative effort are within the scope of protection of this invention.

[0028] The following is combined with Figures 1-3 This invention is described.

[0029] The large model illusion problem severely restricts the reliability of generated content. The purpose of this invention is to overcome the shortcomings of existing large language model illusion detection technologies, such as insufficient cross-domain generalization ability, high dependence on labeled data, and high consumption of computational resources. By optimizing the structural features related to text authenticity in the internal state of the model, efficient and low-cost cross-domain detection generalization can be achieved.

[0030] Figure 1 This is a flowchart illustrating the cross-domain hallucination detection method for large-scale language models provided by this invention, as shown below. Figure 1 As shown, the method includes the following: Step 101: Select the optimal template from the candidate template set based on the variance ratio of the true direction of each candidate template; wherein, the candidate template set includes multiple candidate templates.

[0031] In step 101 above, the truth orientation reflects the separation direction of true / false statements in the embedding space. The geometric meaning of the truth orientation θ is: assuming the embedding distribution of true / false statements is... With N( If ,Σ), then θ is approximately the difference between the means of the two distributions: .

[0032] This invention dynamically generates and filters optimal cue templates, utilizing variance ratios to quantify the templates' ability to guide the authenticity of features within the model's internal state, thereby selecting cue templates that enhance cross-domain consistency. Combined with a supervised learning framework, it extracts the optimized low-dimensional embedding features of the cue templates and trains linear probes or neural network classifiers to achieve efficient cross-domain illusion detection.

[0033] Compared to traditional methods, this invention significantly improves feature structure consistency and detection generalization by decoupling the semantic authenticity of internal states from domain noise through prompting engineering. It achieves high-precision detection in unknown domains with only single-domain labeled data and supports single-inference feature extraction, reducing computational resource consumption. Experiments show that this method improves the F1 score by 15%-22% on cross-domain test sets such as medical and legal fields, and the feature direction cosine similarity is improved by more than 30% compared to the baseline method. It can be widely applied to generation scenarios with high reliability requirements, such as fact-checking and intelligent question answering.

[0034] Optionally, step 101 includes steps A1 to A3: Step A1: Determine the candidate template set based on multiple basic instruction templates.

[0035] Step A2: Calculate the variance ratio of the true orientation for each candidate template on the labeled dataset; sort the candidate templates in descending order based on the variance ratio of the true orientation for each candidate template.

[0036] Step A3: Select a preset number of candidate templates from the sorted candidate templates as templates to be screened; determine the optimal template based on the results of manual review of the multiple templates to be screened.

[0037] In steps A1 to A3 above, the basic template is constructed by manually designing the basic instruction template. For example, "Determine whether the following statement is true: '[statement]'", to ensure basic semantic integrity.

[0038] Optionally, step A1 includes steps A11 to A13: Step A11: Perform the same semantic sentence transformation on each basic instruction template to obtain an intermediate template.

[0039] Step A12: For each intermediate template, perform syntax correction and semantic alignment to obtain the processed intermediate template.

[0040] Step A13: Remove duplicates from multiple processed templates to obtain a candidate template set.

[0041] In steps A11 to A13 above, large models such as GPT-4 are invoked to perform semantically preserved sentence transformation. Then, the generated candidate templates are deduplicated, grammatically corrected, and semantically aligned to form a candidate pool and generate a candidate template set. Example of a generation instruction: "Generate a variant that has the same semantics as the given template but a different sentence structure, while retaining the [statement] placeholder."

[0042] Optionally, step A2 calculates the variance ratio of the ground truth orientation based on the ground truth orientation of each candidate template on the labeled dataset, including steps A21 to A24: Step A21: For each candidate template, concatenate each labeled text with the candidate template to obtain the concatenated labeled text; use a large language model to extract features from the concatenated labeled text to obtain the context embedding vector; wherein, the labeled dataset includes multiple labeled texts; the label of each labeled text is either true or false.

[0043] Step A22: Calculate the average value of true statements and the average value of false statements in the candidate templates based on the multiple context embedding vectors corresponding to each candidate template and the labels of each labeled text.

[0044] Step A23: Based on the difference between the average value of true statements and the average value of false statements in the candidate template, obtain the truthfulness direction of the candidate template.

[0045] Step A24: Calculate the variance ratio of the true orientation based on the projection of the candidate template's true orientation onto the data variance and the total variance of the multiple context embedding vectors corresponding to each candidate template.

[0046] In steps A21 to A24 above, the labeled dataset is... (True / False labels). Each text With candidate templates Concatenate as input ( Input the concatenated text into an LLM (e.g., LLaMA2-7B-Chat) and extract the context embedding vector of the last token in the last layer. (d=4096).

[0047] Each template The true orientation variance ratio on dataset A is achieved using formula (1): Among them, the direction of authenticity This can be achieved through formula (2): Wherein, the covariance matrix Σ is derived from the decentralized feature matrix. Calculate, where, This can be achieved through formula (3): Candidate templates by Sort the templates in descending order, select Top-K templates (e.g., K=3) for manual review, and finally select the optimal template. .

[0048] Step 102: Concatenate the unknown domain test text with the optimal template to obtain the concatenated test text; use a large language model to extract features from the concatenated test text to obtain the feature vector to be detected.

[0049] In step 102 above, test text T in the unknown domain is generated. (T) and extract the feature vector v∈ When d is too large, the feature dimension can be controlled by performing PCA dimensionality reduction on the feature vector v, retaining 95% of the variance and reducing the computational cost of the detector.

[0050] Optionally, the large-scale language model cross-domain hallucination detection method provided by the present invention includes a detector comprising a classifier and a neural network; the method further includes steps B1 to B4: Step B1: Concatenate the training set text with the optimal template, and extract features from the concatenated text to obtain the training set embedding vector; wherein, the training set includes multiple training set texts.

[0051] Step B2: Calculate the true orientation of the optimal template on the training set based on the embedding vectors of multiple training sets.

[0052] Step B3: Using a classifier, calculate the projection value based on the realism direction of the optimal template on the training set; compare the projection value with a preset threshold to obtain the true / false classification result.

[0053] Step B4: Using a neural network, perform classification calculations based on the training set embedding vectors and the true / false classification results to obtain the detection results; optimize the detector parameters based on the detection results to obtain the trained detector.

[0054] In steps B1 to B4 above, this embodiment of the invention first performs feature space reconstruction: performing cue injection and feature extraction, first processing the training set text. By splicing ( Input LLM and extract the embedding vector. .

[0055] The classifier architecture consists of two parts: a linear probe (MM) that directly utilizes... (Training set realism direction) Calculate projection value The label is determined by a preset threshold, for example, 0.5. The neural network (SAPLMA) is an MLP network D(v;W) with a layer structure of d→256→128→64→2, an activation function of ReLU, an output layer of Softmax, and a loss function of cross-entropy.

[0056] In this embodiment of the invention, before training the detector, data partitioning is performed: the labeled dataset is divided into training, validation, and test sets in an 8:1:1 ratio. Optimizer: Adam (learning rate 1e-4, batch size 32) with an early stopping strategy (patience = 5 rounds). Regularization: Dropout (probability 20%) and L2 weight decay (coefficient 1e-5).

[0057] This invention requires only single-domain labeled data to train the detector, eliminating the need for additional multi-domain data collection or complex feature filtering, thus significantly reducing deployment costs. It also reduces data requirements and labeling costs.

[0058] Step 103: Detect the feature vector to be detected using the trained detector to obtain reliability data for cross-domain testing.

[0059] Optionally, reliability data, including positive correlation results and indicator data; step 103, including steps C1 to C2: Step C1: Calculate the cross-domain cosine similarity matrix based on the authenticity direction of the domain datasets in different domains; compare the mean of the cross-domain cosine similarity matrix with the baseline to obtain a positive correlation result.

[0060] Step C2: Detect the feature vector to be detected using the trained detector to obtain index data.

[0061] In steps C1 to C2 above, structural consistency verification is first performed on multiple domain datasets. , ,..., Calculate their authenticity direction respectively Calculate the cross-domain cosine similarity matrix. ,in Verify whether the mean of C is significantly higher than the baseline (without prompts).

[0062] Next, detector transfer testing is performed in the training domain. Training the detector directly in the unknown domain The test was conducted, and metrics such as accuracy and F1 score were recorded.

[0063] This invention optimizes the feature extraction process through plug-and-play prompt templates, is compatible with linear probing (such as the MM method) and neural network classifiers (such as SAPLMA), requires no modification to the model architecture, and completes feature extraction in a single inference, reducing computational overhead. It is compatible with existing detection methods and improves efficiency.

[0064] This invention enhances the saliency of realistic structures within internal states, providing more reliable feature support for confidence-based or unsupervised detection and reducing false positive rates. This, in turn, improves the detection accuracy of unsupervised methods.

[0065] To further explain the present invention, the following specific examples are provided. The implementation process of the technical solution of the present invention is described in detail with reference to embodiments, code snippets, and mathematical derivations. The implementation environment is the PyTorch framework, the LLM used is LLaMA2-7B-Chat, and the hardware configuration is an NVIDIA A100 GPU.

[0066] Example 1: Prompt Template Generation and Filtering Objective: To select the optimal template from the candidate templates for feature extraction.

[0067] Step 1: Candidate Template Generation # Input: Initial template generation command prompt_seed = "Generate 10 prompt templates related to 'judging the truthfulness of statements', each containing the [statement] keyword, and in various formats." candidate_prompts = query_gpt4(prompt_seed) # Generate candidate templates using GPT-4 # Output example: candidates = [ Please verify that the following statement is accurate: [statement] "Determine the truth or falsehood of this statement: [statement]", Is this statement true? # ... Other candidate templates (10 in total) ] Step 2: Feature Extraction and Variance Ratio Calculation def compute_variance_ratio(model, dataset, prompt_template): # Input: model, dataset, hint template # Output: Variance ratio R embeddings = [] labels = [] For text, label in dataset: prompted_text = prompt_template.replace("[statement]", text) with torch.no_grad(): outputs = model(input_ids=tokenize(prompted_text)) last_token_embed = outputs.last_hidden_state[:, -1, :] # Extract the embedding of the last token embeddings.append(last_token_embed) labels.append(label) embeddings = torch.cat(embeddings, dim=0) # shape[N, d] # Calculate θ (true direction) theta = embeddings[labels==1].mean(0) - embeddings[labels==0].mean(0)# [d] # Calculate the covariance matrix Σ X = embeddings - embeddings.mean(0) Sigma = (XT @ X) / (X.shape[0] - 1) # [d, d] # Calculate the variance ratio V_theta = (theta.T @ Sigma @ theta) / (theta.norm()**2) V_total = torch.trace(Sigma) R = V_theta / V_total return R.item() # Calculate R for each candidate template R_scores = [compute_variance_ratio(model, train_data, p) for p incandidates] optimal_prompt = candidates[torch.argmax(R_scores)] # Select the template with the highest R score Example 2: Detector Training and Testing Objective: To train a SAPLMA detector based on a selected cue template and test it on cross-domain data.

[0068] Step 1: Feature Extraction and Dataset Construction # Extracting features from the training set train_features = [] for text, label in train_dataset: prompted_text = optimal_prompt.replace("[statement]", text) embed = model.encode_last_token(prompted_text) # [d] train_features.append(embed) train_X = torch.stack(train_features) # [N_train, d] train_y = torch.tensor([label for _, label in train_dataset]) # [N_train] # Extracting features from the test set (cross-domain) test_features = [model.encode_last_token(optimal_prompt.replace("[statement]", t)) for t in test_texts] test_X = torch.stack(test_features) # [N_test, d] Step 2: MLP detector training class SAPLMA(nn.Module): def __init__(self, input_dim=4096): super().__init__() self.layers = nn.Sequential( nn.Linear(input_dim, 256), nn.ReLU(), nn.Dropout(0.2), nn.Linear(256, 128), nn.ReLU(), nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 2) ) def forward(self, x): return self.layers(x) # Training Configuration model = SAPLMA().cuda() criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) # Training Loop for epoch in range(10): for batch_x, batch_y in DataLoader(TensorDataset(train_X, train_y),batch_size=32): pred = model(batch_x.cuda()) loss = criterion(pred, batch_y.cuda()) optimizer.zero_grad() loss.backward() optimizer.step() Step 3: Cross-domain testing # Evaluation on test sets in unknown domains model.eval() with torch.no_grad(): test_pred = model(test_X.cuda()) test_acc = (test_pred.argmax(1) == test_y.cuda()).float().mean() print(f"Cross-domain test accuracy: {test_acc:.4f}") # Example output: 0.8276 This invention provides a cross-domain hallucination detection method using a large-scale language model. The method selects the optimal template from a candidate template set based on the realism direction variance ratio of each candidate template. It then concatenates the unknown domain test text with the optimal template to obtain the concatenated test text. A large-scale language model is used to extract features from the concatenated test text, yielding a feature vector to be detected. Finally, a trained detector is used to detect the feature vector, obtaining reliable cross-domain test data. Compared to existing technologies where LLM-based feature extraction struggles to capture cross-domain consistency patterns, limiting the detector's generalization ability, this method determines the optimal template based on the realism direction variance ratio of candidate templates and optimizes the feature extraction process using a plug-and-play optimal template as a cue template. This enhances the saliency and cross-domain consistency of realism-related structures, improves the detector's generalization performance in unknown domains, and achieves efficient and low-cost hallucination detection generalization.

[0069] The cross-domain illusion detection system for large language models provided by this invention is described below. The cross-domain illusion detection system for large language models described below can be referred to in correspondence with the cross-domain illusion detection method for large language models described above.

[0070] Figure 2 This is a flowchart illustrating the large-scale language model cross-domain hallucination detection system provided by the present invention, as shown below. Figure 2 As shown, the system includes the following: The optimal template determination module 201 is used to select the optimal template from the candidate template set based on the variance ratio of the true direction of each candidate template; wherein, the candidate template set includes multiple candidate templates.

[0071] The feature extraction module 202 is used to concatenate the test text in the unknown domain with the optimal template to obtain the concatenated test text; and to extract features from the concatenated test text using a large language model to obtain the feature vector to be detected.

[0072] The detection module 203 is used to detect the feature vector to be detected using a trained detector to obtain reliability data for cross-domain testing.

[0073] Optionally, the optimal template determination module 201 includes: The candidate template set determination submodule is used to determine the candidate template set based on multiple basic instruction templates; The Reality Orientation Variance Ratio Calculation Submodule is used to calculate the Reality Orientation Variance Ratio based on the reality orientation of each candidate template in the labeled dataset; and to sort multiple candidate templates in descending order based on the Reality Orientation Variance Ratio of each candidate template. The optimal template determination submodule is used to select a preset number of candidate templates from multiple sorted candidate templates as templates to be screened; and to determine the optimal template based on the results of manual review of multiple templates to be screened.

[0074] Optionally, the candidate template set determines a submodule, which performs the same semantic sentence transformation on each basic instruction template to obtain an intermediate template; for each intermediate template, performs syntax correction and semantic alignment to obtain a processed intermediate template; and for multiple processed templates, performs deduplication to obtain a candidate template set.

[0075] Optionally, the truth orientation variance ratio calculation submodule is used to concatenate each labeled text with the candidate template for each candidate template to obtain concatenated labeled text; extract features from the concatenated labeled text using a large language model to obtain context embedding vectors; wherein, the labeled dataset includes multiple labeled texts; each labeled text is labeled as true or false; calculate the average value of true statements and the average value of false statements in the candidate template based on the multiple context embedding vectors corresponding to each candidate template and the label of each labeled text; obtain the truth orientation of the candidate template based on the difference between the average value of true statements and the average value of false statements in the candidate template; and calculate the truth orientation variance ratio based on the projection of the truth orientation of the candidate template onto the data variance and the total variance of the multiple context embedding vectors corresponding to each candidate template.

[0076] Optionally, the detector includes: a classifier and a neural network; the system also includes: The training module is used to concatenate the training set text with the optimal template and extract features from the concatenated text to obtain the training set embedding vector; wherein, the training set includes multiple training set texts; Based on multiple training set embedding vectors, the true orientation of the optimal template on the training set is calculated; the projection value is obtained by calculating the true orientation of the optimal template on the training set using a classifier; the projection value is compared with a preset threshold to obtain the true / false classification result; the detection result is obtained by performing classification calculations based on the training set embedding vectors and the true / false classification result using a neural network; the parameters of the detector are optimized based on the detection result to obtain the trained detector.

[0077] Optionally, reliability data, including positive correlation results and indicator data; detection module 203, including: The cross-domain correlation calculation submodule is used to calculate the cross-domain cosine similarity matrix based on the authenticity direction of the domain datasets in different domains; the mean of the cross-domain cosine similarity matrix is ​​compared with the baseline to obtain the positive correlation result; The indicator result calculation submodule is used to detect the feature vector to be detected using a trained detector and obtain indicator data.

[0078] This invention provides a large-scale language model-based cross-domain hallucination detection system. The system selects the optimal template from a candidate template set based on the realism direction variance ratio of each candidate template. It then concatenates the unknown domain test text with the optimal template to obtain the concatenated test text. A large-scale language model is used to extract features from the concatenated test text, yielding a feature vector to be detected. Finally, a trained detector is used to detect the feature vector, obtaining reliable cross-domain test data. Compared to existing technologies where LLM-based feature extraction struggles to capture cross-domain consistency patterns, limiting the detector's generalization ability, this system determines the optimal template based on the realism direction variance ratio of candidate templates and optimizes the feature extraction process using a plug-and-play optimal template as a cue template. This enhances the saliency and cross-domain consistency of realism-related structures, improves the detector's generalization performance in unknown domains, and achieves efficient and low-cost hallucination detection generalization.

[0079] Figure 3 An example is a schematic diagram of the physical structure of an electronic device, such as... Figure 3 As shown, the electronic device may include a processor 810, a communications interface 820, a memory 830, and a communication bus 840, wherein the processor 810, the communications interface 820, and the memory 830 communicate with each other via the communication bus 840. The processor 810 can call logical instructions in the memory 830 to execute a large-scale language model cross-domain hallucination detection method.

[0080] Furthermore, the logical instructions in the aforementioned memory 830 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0081] On the other hand, the present invention also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer is able to execute the large-scale language model cross-domain illusion detection method provided by the above methods.

[0082] In another aspect, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to perform the large-scale language model cross-domain illusion detection method provided by the methods described above.

[0083] The system embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0084] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. 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 software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.

[0085] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, and not to limit them; 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; and 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 cross-domain hallucination detection method using a large-scale language model, characterized in that, include: The optimal template is selected from the candidate template set based on the true orientation variance ratio of each candidate template; wherein, the candidate template set includes multiple candidate templates; The test text in the unknown domain is concatenated with the optimal template to obtain the concatenated test text; features are extracted from the concatenated test text using a large language model to obtain the feature vector to be detected. The trained detector is used to detect the feature vector to be detected, thereby obtaining reliability data for cross-domain testing.

2. The method for cross-domain hallucination detection using a large language model according to claim 1, characterized in that, The step of selecting the optimal template from the candidate template set based on the true orientation variance ratio of each candidate template includes: Based on multiple basic instruction templates, a candidate template set is determined; Based on the true orientation of each candidate template in the labeled dataset, the true orientation variance ratio is calculated; based on the true orientation variance ratio of each candidate template, multiple candidate templates are sorted in descending order. A predetermined number of candidate templates are selected from the sorted candidate templates as templates to be screened; the optimal template is determined based on the results of manual review of the multiple templates to be screened.

3. The method for cross-domain hallucination detection using a large language model according to claim 2, characterized in that, The step of determining a candidate template set based on multiple basic instruction templates includes: For each basic instruction template, perform the same semantic sentence transformation to obtain an intermediate template; For each of the intermediate templates, perform syntax correction and semantic alignment to obtain the processed intermediate template; The multiple processed templates are deduplicated to obtain a candidate template set.

4. The method for cross-domain hallucination detection using a large language model according to claim 2, characterized in that, The step of calculating the variance ratio of the ground truth orientation based on the ground truth orientation of each candidate template in the labeled dataset includes: For each candidate template, each labeled text is concatenated with the candidate template to obtain the concatenated labeled text; features are extracted from the concatenated labeled text using a large language model to obtain a context embedding vector; wherein, the labeled dataset includes multiple labeled texts; each labeled text is labeled as true or false; Based on the multiple context embedding vectors corresponding to each candidate template and the labels of each labeled text, the average value of true statements and the average value of false statements in the candidate template are calculated. The direction of truthfulness of the candidate template is obtained by the difference between the average value of true statements and the average value of false statements in the candidate template. The true orientation variance ratio is calculated based on the projection of the candidate template's true orientation onto the data variance and the total variance of the multiple context embedding vectors corresponding to each candidate template.

5. The method for cross-domain hallucination detection using a large language model according to claim 1, characterized in that, The detectors include: Classifiers and neural networks; The method further includes: The training set text is concatenated with the optimal template, and features are extracted from the concatenated text to obtain the training set embedding vector; wherein, the training set includes multiple training set texts; Based on the multiple training set embedding vectors, the real orientation of the optimal template on the training set is calculated. The classifier calculates the projection value based on the realism orientation of the optimal template on the training set; the projection value is then compared with a preset threshold to obtain the true / false classification result. The detection result is obtained by performing classification calculations based on the training set embedding vector and the true / false classification result using a neural network; the detector parameters are then optimized based on the detection result to obtain a trained detector.

6. The method for cross-domain hallucination detection using a large language model according to claim 1, characterized in that, The reliability data includes positive correlation results and indicator data; the reliability data obtained by detecting the feature vector to be detected using a trained detector, including: Based on the authenticity orientation of domain datasets from different domains, a cross-domain cosine similarity matrix is ​​obtained; the mean of the cross-domain cosine similarity matrix is ​​compared with the baseline to obtain a positive correlation result; The trained detector is used to detect the feature vector to be detected, and index data is obtained.

7. A large-scale language model cross-domain hallucination detection system, characterized in that, include: The optimal template determination module is used to select the optimal template from the candidate template set based on the true orientation variance ratio of each candidate template; wherein, the candidate template set includes multiple candidate templates; The feature extraction module is used to concatenate the unknown domain test text with the optimal template to obtain the concatenated test text; and to extract features from the concatenated test text using a large language model to obtain the feature vector to be detected. The detection module is used to detect the feature vector to be detected using a trained detector to obtain reliability data for cross-domain testing.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it implements the cross-domain illusion detection method for large language models as described in any one of claims 1 to 6.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the cross-domain illusion detection method for large language models as described in any one of claims 1 to 6.

10. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the cross-domain illusion detection method for large language models as described in any one of claims 1 to 6.