A method for testing a knowledge graph enhanced large language model

By retrieving knowledge graph triples in large-scale language models in real time and employing a bidirectional hybrid attention mechanism, the problems of static knowledge fixation and high training costs are solved, enabling dynamic knowledge updates and non-intrusive enhancement, thereby improving the reasoning ability and generation fluency of the language model.

CN120492637BActive Publication Date: 2026-08-25SOUTHEAST UNIV
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510652032.8
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-05-20
Publication Date
2026-08-25
Estimated Expiration
2045-05-20

AI Technical Summary

Technical Problem

Existing technologies in large-scale language models suffer from problems such as static knowledge fixation, high training costs, decreased fluency of language generation, semantic alignment deviation, and high retrieval latency, making it difficult to achieve dynamic knowledge updates and non-intrusive enhancement.

Method used

The GraphRAG technology is used to retrieve knowledge graph triples in real time. The knowledge graph is dynamically integrated in the LLM inference stage through a bidirectional hybrid attention mechanism, which preserves the original capabilities of the model. The feature representation is enriched by multi-head attention mechanism and feedforward neural network, thus achieving non-intrusive knowledge enhancement.

Benefits of technology

It effectively enhances the factual reasoning ability of large-scale language models, solves the problems of lagging knowledge updates and decreased fluency of language generation, and has a highly adaptable and low-cost knowledge augmentation effect.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120492637B_ABST
    Figure CN120492637B_ABST
Patent Text Reader

Abstract

The application provides a method for testing knowledge graph enhanced large language model. The method proposes a new attention mechanism based on knowledge graph, and uses a bidirectional information flow aggregation method to improve the effect of the knowledge graph enhanced large language model, including the following steps: retrieving triplets from the knowledge graph as a knowledge graph triplet input sequence according to a text input sequence; converting the text input sequence and the knowledge graph triplet input sequence into embedding vectors, and calculating a query matrix, a key matrix and a value matrix; calculating a mixed attention; obtaining a more rich feature representation through a feedforward neural network; and generating a model prediction probability distribution according to the feature representation, so as to predict the next word.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of natural language processing and knowledge graph enhancement technology, specifically involving a method for dynamically integrating knowledge graphs (KG) during the reasoning stage of large language models (LLM). By designing a bidirectional hybrid attention mechanism, non-intrusive knowledge enhancement is achieved, improving the model's factual reasoning ability while retaining its original language generation performance. Background Technology

[0002] Knowledge graphs provide interpretable symbolic knowledge support for language models through structured semantic networks of entities, relations, and entities. While large language models excel in text generation and comprehension tasks, they still face core challenges such as factual errors, lagging temporal knowledge updates, and broken complex reasoning chains. Research on knowledge graph augmentation for large language models has been extensive. By combining the explicit knowledge system of knowledge graphs with the implicit semantic representations of LLMs, the proportion of fictitious content in model generation is reduced, improving the credibility of answers in complex question-answering tasks. Therefore, knowledge graph augmentation of large language models has become an important research direction for enhancing the capabilities of large language models.

[0003] Currently, the academic community has proposed several methods for augmenting large language models using knowledge graphs. The first and most direct method involves injecting a large amount of structured knowledge graph knowledge into the pre-training data. This naturally achieves the goal of knowledge graph augmentation, but the cost is too high, and the knowledge is transformed into a static feature representation, which cannot solve the problem of temporal knowledge updates. Similarly, the second type of method involves invasively injecting knowledge graph knowledge by fine-tuning additional parameters, which is the KG augmentation method currently mainly relied upon by the academic community. This method reduces training costs, but such invasive architectural modifications disrupt the original attention pattern of LLM, impairing the model's inherent knowledge and capabilities, leading to a decrease in the fluency of language generation. In addition, graph-based retrieval-augmented generation (RAG) has also been widely applied in this field, becoming a new paradigm for knowledge injection methods due to its lower cost and greater flexibility. However, problems such as graph retrieval efficiency, the misalignment between structured data and natural language text semantics, and the retrieval quality and computational overhead inherent in traditional RAG still exist.

[0004] Therefore, the existing technological bottlenecks can be roughly summarized as follows:

[0005] Pre-training injection: Converting KG triples into text for pre-training (such as ERNIE) results in static solidification of knowledge, which cannot be dynamically updated, and the training cost is extremely high.

[0006] Fine-tuning injection: By adding an adapter module to fine-tune the model (such as K-Adapter), the model parameters are modified, which disrupts the original attention distribution and impairs the fluency of the language.

[0007] Retrieval Enhancement Generation (RAG): This method concatenates KG triples into a Prompt input (e.g., KG-GPT), but faces issues such as semantic alignment bias, high retrieval latency, and insufficient support for multi-hop inference.

[0008] Therefore, the core problem solved by this invention is as follows:

[0009] Dynamic nature: Real-time retrieval of KG during testing to prevent knowledge from becoming outdated.

[0010] Non-intrusive: It does not modify LLM parameters and retains its original capabilities.

[0011] Deep integration: Achieve two-way interaction between KG and text at the attention layer, surpassing the shallow splicing of traditional RAG. Summary of the Invention

[0012] The purpose of this invention is to address the shortcomings of the aforementioned background technology and provide a method for enhancing large language models using knowledge graphs during testing, which can effectively utilize knowledge graphs to enhance the capabilities of large language models.

[0013] The technical solution adopted in this invention is: a method for enhancing a large language model with a knowledge graph during testing, comprising the following steps:

[0014] Extract triples from the knowledge graph based on the text input sequence as the knowledge graph triple input sequence;

[0015] The text input sequence and the knowledge graph triple input sequence are converted into embedding vectors, and the query matrix, key matrix and value matrix are calculated.

[0016] Compute mixed attention;

[0017] A richer feature representation can be obtained through a feedforward neural network;

[0018] Based on the aforementioned feature representations, a generative model predicts the probability distribution, thereby predicting the next word. Specifically, GraphRAG technology is used to retrieve triples from structured knowledge graph data based on the text input sequence, serving as the knowledge graph triple input sequence.

[0019] The text input sequence and the knowledge graph triple input sequence are converted into embedding vectors. For a given network hidden layer, assuming its text input embedding matrix is ​​X and its knowledge graph triple embedding matrix is ​​z, the same parameter matrix W is used. Q W K W VThe query matrix, key matrix, and value matrix of the input text and knowledge graph are calculated as follows:

[0020]

[0021] Among them, Q X K X and V X Let Q represent the query matrix, key matrix, and value matrix generated from the input embedding matrix, respectively. Z K Z and V Z This represents the query matrix, key matrix, and value matrix generated by the triplet embedding matrix.

[0022] This method employs a multi-head attention mechanism and calculates hybrid attention through bidirectional information aggregation.

[0023] The transformed query matrix, key matrix, and value matrix are each divided into multiple headers, assuming there are h headers, each with dimension d. k Then we have:

[0024]

[0025] Where, d dim This is the embedding dimension of the model; therefore, the segmented query matrix, key matrix, and value matrix are as follows, where i represents the i-th attention head:

[0026] Q i =split(Q,i);K i =split(K,i); V i =split(V,i)

[0027] Among them, Q i K i and V i Let Q, K, and V represent the query matrix, key matrix, and value matrix of the i-th attention head after segmentation, respectively, while Q, K, and V represent the original query matrix, key matrix, and value matrix, respectively.

[0028] Among them, combining hybrid attention features with a feedforward neural network yields richer feature representations, and the calculation process is as follows:

[0029] FFN(X)=ReLU(X T W1)W2

[0030] Here, FFN is an abbreviation for Feed-Forward Network, X is the input feature matrix, and ReLU is an activation function defined as Max(0,X), which represents taking the maximum value between the input and 0. TW1 and W2 represent the transpose of the input matrix, while W1 and W2 are two linear transformation matrices in the feedforward neural network.

[0031] Among them, for any attention head in the multi-head attention mechanism, the hybrid attention aggregates three information flows: text input information flow based on self-attention, output information flow enhanced by knowledge graph, and knowledge graph information flow;

[0032] The three types of information flow output matrices are as follows:

[0033] The text input information stream output matrix based on self-attention is derived from the text input query matrix. and text input key matrix The self-attention weight matrix and the text input matrix The product obtained by multiplication:

[0034]

[0035] The output information flow of knowledge graph enhancement is derived from the input query matrix based on knowledge graph triples. and text input key matrix Attention weight matrix and text input matrix The product obtained by multiplication:

[0036]

[0037] The knowledge graph information flow output matrix is ​​based on the text input query matrix. and knowledge graph triple input key matrix Attention weight matrix and knowledge graph triple input matrix The product obtained by multiplication:

[0038]

[0039] Specifically, GraphRAG is used as a tool to retrieve specific and relevant knowledge graph triples from the knowledge graph.

[0040] In this process, the text input information stream and the knowledge graph-enhanced output information stream of the hybrid attention method are introduced with nonlinear complex changes through a feedforward neural network to obtain new feature representations. The knowledge graph information stream skips one layer of the feedforward neural network and is directly aggregated with the changed input information stream. The aggregated feature representations are then fused. The improved fusion process is as follows:

[0041]

[0042] Where, x n This represents the representation vector of the nth word element after fusing the knowledge graph triples, where Exp refers to the exponential function. Let represent the inner product of the query feature of the nth input term and the key feature of the i-th input term. This represents the inner product of the query feature of the nth input word element and the key feature of the jth triplet. and Then, these represent the feature vectors of the i-th input word and the j-th triplet word, respectively.

[0043] An electronic device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor, when executing the program, implements the method for a test-time knowledge graph augmented large language model.

[0044] A computer-readable storage medium having computer instructions stored thereon, which, when executed by a processor, implement the method described above for a test-time knowledge graph augmentation large language model.

[0045] Furthermore, this invention fully utilizes GraphRAG technology to retrieve triples from structured knowledge graph data based on the text input sequence as the knowledge graph triple input sequence. This non-intrusive knowledge injection method can solve the problem of temporal knowledge updating while preserving the original expressive power of the model.

[0046] Furthermore, the present invention does not change the parameter matrix W. Q W K W V They can be used to directly compute the query matrix, key matrix, and value matrix, saving the cost of training or fine-tuning, and compared to fine-tuning, they do not destroy the model's inherent knowledge and capabilities.

[0047] Furthermore, this invention employs a multi-head attention mechanism to hybridize attention. Compared to traditional single-head attention, the multi-head attention mechanism does not increase complexity but significantly enhances the model's ability to model complex relationships.

[0048] Furthermore, this invention achieves bidirectional mutual lookup between the input stream and the knowledge graph stream by mixing attention through a bidirectional information aggregation method, ensuring the coherence of adaptive knowledge retrieval and context.

[0049] Furthermore, this invention rationally combines hybrid attention features with feedforward neural network layers to obtain new feature representations, making the model output more reasonable.

[0050] Furthermore, the hybrid attention mechanism proposed in this invention aggregates three information flows: a self-attention-based text input information flow, a knowledge graph-enhanced output information flow, and a knowledge graph information flow. Besides the traditional self-attention-based text input information flow, the knowledge graph-enhanced output information flow in the hybrid attention mechanism utilizes information from the structured semantic set of the knowledge graph to locate specific fields in the text input and increase the attention weight of these fields. The knowledge graph information flow represents the text input's query of knowledge graph information. Combining these three elements achieves a bidirectional, mutually querying information aggregation hybrid attention mechanism.

[0051] Furthermore, the hybrid attention-based text input information stream and knowledge graph-enhanced output information stream proposed in this invention introduce nonlinear complex changes through a feedforward neural network to obtain new feature representations. The knowledge graph information stream skips one layer of the feedforward neural network and directly aggregates with the other two transformed information streams. The aggregated feature representations are then normalized, making the model's output more reasonable for complex problems.

[0052] Compared with the prior art, the advantages of the present invention are as follows:

[0053] 1. The knowledge graph enhancement method proposed in this invention is a test-time enhancement method that does not modify any parameters of the model, thus avoiding the catastrophic forgetting problem caused by traditional fine-tuning methods.

[0054] 2. The knowledge graph enhancement method proposed in this invention has very strong adaptability and can effectively adapt to the dynamic changes in knowledge graphs.

[0055] 3. Because it does not involve parameter updates and has strong adaptability, the method proposed in this invention can be effectively and quickly integrated into current large models, and has high practical application value. Attached Figure Description

[0056] Figure 1 This is a schematic diagram of the knowledge graph-guided attention module of the present invention;

[0057] Figure 2 A schematic diagram of the overall structure of the attention module guided by the knowledge graph. Detailed Implementation

[0058] The present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments to facilitate a clear understanding of the present invention, but these descriptions do not constitute a limitation on the present invention.

[0059] Example 1

[0060] like Figure 2 As shown, this invention provides a method for enhancing a large language model using a knowledge graph during testing, comprising the following steps:

[0061] Extract triples from the knowledge graph based on the text input sequence as the knowledge graph triple input sequence;

[0062] The text input sequence and the knowledge graph triple input sequence are converted into embedding vectors, and the query matrix, key matrix and value matrix are calculated.

[0063] Compute mixed attention;

[0064] A richer feature representation can be obtained through a feedforward neural network;

[0065] Based on the above features, a model is generated to predict the probability distribution, thereby predicting the next word.

[0066] Specifically, GraphRAG technology is used to retrieve triples from structured knowledge graph data based on the text input sequence as the knowledge graph triple input sequence.

[0067] Figure 1 The core module of this invention, the knowledge graph-guided attention module, is described. As shown in the figure, this module contains three information flows. The first information flow is the information flow from the original self-attention module, which is the flow in which the model aggregates the input value matrix V through self-attention. X The information in the first part is the knowledge graph fusion information flow, which is obtained through the input query feature matrix Q. X To reassemble the triplet value matrix V Z The information in the graph is used to complete the knowledge graph fusion. The third information flow utilizes the knowledge graph query matrix Q. Z To re-aggregate the input value matrix V X This allows for the re-aggregation of input features, removing noise and retaining important information.

[0068] Figure 2 This describes the overall structure of our proposed knowledge graph-guided attention module. Its fusion process roughly consists of two steps. The first step utilizes Knowledge Graph Attention (KGA) to aggregate knowledge graph information and then performs normalization (norm). Next, the normalized features are input into a feedforward neural network (FFN) to further enrich the features, and finally, normalization (norm) is performed again to output the result.

[0069] Specifically, the text input sequence and the knowledge graph triple input sequence are converted into embedding vectors. Assuming the text input embedding vector is X and the knowledge graph triple embedding vector is Z, the same parameter matrix W is used... Q W K W V The query matrix, key matrix, and value matrix are calculated as follows:

[0070]

[0071] Specifically, this invention employs a multi-head attention mechanism and calculates hybrid attention through bidirectional information aggregation. The transformed query matrix, key matrix, and value matrix are each divided into multiple heads. Assume we have h heads, each head having a dimension of d. k Then we have:

[0072]

[0073] Where, d dim This is the embedding dimension of the model. Therefore, the segmented query matrix, key matrix, and value matrix are as follows, where i represents the i-th attention head:

[0074] Q i =split(Q,i);K i =split(K,i); V i =split(V,i)

[0075] Specifically, for any attention head in the multi-head attention mechanism, hybrid attention aggregates three information flows: text input information flow based on self-attention, output information flow enhanced by knowledge graph, and knowledge graph information flow.

[0076] Specifically, the calculation of the three information flow output matrices is as follows:

[0077] The text input information stream output matrix based on self-attention is derived from the text input query matrix. and text input key matrix The self-attention weight matrix and the text input matrix The product obtained by multiplication:

[0078]

[0079] The output information flow of knowledge graph enhancement is derived from the input query matrix based on knowledge graph triples. and text input key matrix Attention weight matrix and text input matrix The product obtained by multiplication:

[0080]

[0081] The knowledge graph information flow output matrix is ​​based on the text input query matrix. and knowledge graph triple input key matrix Attention weight matrix and knowledge graph triple input matrix The product obtained by multiplication:

[0082]

[0083] Specifically, a new feature representation is obtained by combining hybrid attention features with a feedforward neural network. The hybrid attention text input information stream and the knowledge graph-enhanced output information stream are introduced with nonlinear complex changes through the feedforward neural network to obtain the new feature representation. The knowledge graph information stream skips one layer of the feedforward neural network and is directly aggregated with the other two transformed information streams, and the aggregated feature representation is normalized.

[0084] The contents not described in detail in this specification are existing technologies known to those skilled in the art.

[0085] It should be noted that the above embodiments are not intended to limit the scope of protection of the present invention. Equivalent transformations or substitutions made based on the above technical solutions all fall within the scope of protection of the claims of the present invention.

Claims

1. A method for enhancing a large language model with knowledge graphs during testing, characterized in that: Includes the following steps: Retrieve triples from the knowledge graph based on the text input sequence as the knowledge graph triple input sequence; The text input sequence and the knowledge graph triple input sequence are converted into embedding vectors, and the query matrix, key matrix and value matrix are calculated. Compute mixed attention; A richer feature representation can be obtained through a feedforward neural network; Based on the above feature representations, a model is generated to predict the probability distribution, thereby predicting the next word; The calculation of hybrid attention is specifically as follows: A multi-head attention mechanism is employed, and hybrid attention is calculated using a bidirectional information aggregation method. The transformed query matrix, key matrix, and value matrix are each divided into multiple headers, assuming there are... Each head has a dimension of [number]. Then we have: in, This is the embedding dimension of the model; therefore, the segmented query matrix, key matrix, and value matrix are as follows, where... Indicates the first One point of attention: in, , and They represent the first and second parts after the division. The query matrix, key matrix, and value matrix of each attention head, and , and These represent the original query matrix, key matrix, and value matrix, respectively. For any attention head in a multi-head attention mechanism, hybrid attention aggregates three information flows: a text input information flow based on self-attention, an output information flow enhanced by knowledge graphs, and a knowledge graph information flow. The three types of information flow output matrices are as follows: The text input information stream output matrix based on self-attention is derived from the text input query matrix. and text input key matrix The self-attention weight matrix and the text input matrix The product obtained by multiplication: The output information flow of knowledge graph enhancement is composed of the input query matrix based on knowledge graph triples. and text input key matrix Attention weight matrix and text input matrix The product obtained by multiplication: The knowledge graph information flow output matrix is ​​based on the text input query matrix. and knowledge graph triple input key matrix Attention weight matrix and knowledge graph triple input matrix The product obtained by multiplication: 。 2. The method for enhancing a large language model using a knowledge graph during testing, as described in claim 1, is characterized in that... The GraphRAG technique is used to retrieve triples from structured knowledge graph data based on the text input sequence, which are then used as the input sequence for knowledge graph triples.

3. The method for enhancing a large language model using a knowledge graph during testing, as described in claim 1, is characterized in that... The text input sequence and the knowledge graph triple input sequence are converted into embedding vectors. For a given network hidden layer, assume its text input embedding matrix is... The knowledge graph triple embedding matrix is Based on the same parameter matrix , , The query matrix, key matrix, and value matrix of the input text and knowledge graph are calculated as follows: in, , and These represent the query matrix, key matrix, and value matrix generated from the input embedding matrix, respectively. , and This represents the query matrix, key matrix, and value matrix generated by the triplet embedding matrix.

4. The method for enhancing a large language model using a knowledge graph during testing, as described in claim 1, is characterized in that... By combining hybrid attention features with a feedforward neural network, a richer feature representation can be obtained. The calculation process is as follows: FFN is an abbreviation for Feed-Forward Network, representing a feedforward neural network. The input feature matrix is ​​ReLU, which is an activation function defined as Max(0,X), meaning it takes the maximum value between the input and 0. This represents the transpose of the input matrix. and These are two linear transformation matrices in a feedforward neural network.

5. The method for enhancing a large language model with a knowledge graph during testing according to claim 2, characterized in that, GraphRAG is used as a tool to retrieve specific and relevant knowledge graph triples from a knowledge graph.

6. The method for enhancing a large language model with a knowledge graph during testing according to claim 5, characterized in that, In the hybrid attention model, the text input information stream and the knowledge graph-enhanced output information stream are introduced with nonlinear complex changes through a feedforward neural network to obtain new feature representations. The knowledge graph information stream skips one layer of the feedforward neural network and is directly aggregated with the changed input information stream. The aggregated feature representations are then fused. The improved fusion process is as follows: in, This represents the representation vector of the nth word element after fusing the knowledge graph triples, where Exp refers to the exponential function. Indicates the first The input lexical query features and the first The inner product of the input word key features, Indicates the first The input lexical query features and the first The inner product of the metakey features of each triplet word. and Then they respectively represent the first The input word and the first input word The feature vector of the value of each triplet word element.

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 a method for testing a knowledge graph-enhanced large language model as described in any one of claims 1 to 6.

8. A computer-readable storage medium storing computer instructions thereon, characterized in that, When the computer instructions are executed by the processor, they implement a method for test-time knowledge graph augmentation of a large language model as described in any one of claims 1-6.

Citation Information

Patent Citations

  • False news identification method combining knowledge graph and attention mechanism

    CN119537609A