A large language model joint reasoning method based on knowledge graph-enhanced thought chain prompts
By introducing graph reasoning models and thought chain prompts into large language models, the accuracy and interpretability issues of large language models in the knowledge enhancement process are solved, and efficient and accurate answers to complex problems are achieved.
Patent Information
- Application Number
- CN202410983442.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-22
- Publication Date
- 2025-09-09
- Estimated Expiration
- 2044-07-22
AI Technical Summary
Large language models suffer from parameter errors, high training costs, cognitive differences, and missing knowledge topology information during the knowledge augmentation process, resulting in insufficient reasoning accuracy and interpretability.
By extracting multi-hop neighborhood knowledge triples of problem entities to form a local knowledge subgraph, the graph reasoning model is used for explicit decomposition and encoding. Combined with the cross-attention and matrix transformation modules, thought chain prompts are generated to assist the large language model in accurate reasoning.
It improves the accuracy and interpretability of multi-hop reasoning of large language models, reduces time complexity, enhances the ability to analyze complex problems, reduces the misleading of irrelevant reasoning paths, and provides access to multi-source heterogeneous knowledge.
Smart Images

Figure CN118940840B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the technical field related to natural language reasoning, and more specifically, relates to a large language model joint reasoning method based on knowledge graph-enhanced thought chain prompts. Background Art
[0002] Knowledge augmentation of large language models is a crucial task in natural language processing. With the development of large-scale pre-trained language models, traditional static reasoning models that rely on parameter knowledge are susceptible to hallucinations when handling knowledge-intensive tasks, often outputting factually incorrect responses.
[0003] To address the hallucination problem caused by the black-box nature of large language models, integrating external knowledge graphs is an effective solution. Knowledge graphs store structured knowledge triples, providing context and background knowledge for large language model reasoning, thereby improving the accuracy and interpretability of reasoning. However, due to differences in data structure, large language models struggle to directly retrieve and utilize structured knowledge graphs. Therefore, the task of enhancing large language models with knowledge graphs has been proposed. Its goal is to provide effective methods for retrieval of structured knowledge and enhancement of large language models, ensuring that large language models can effectively utilize structured external knowledge to generate accurate and reliable natural language responses.
[0004] Currently, knowledge enhancement of large language models is usually achieved using pluggable retrieval enhancement methods. However, knowledge enhancement of large language models faces the following problems:
[0005] (1) Due to the huge parameter size and high training cost of large language models, they are not trained frequently. Therefore, the parameter knowledge within the model is often wrong or outdated, making it difficult to ensure the accuracy of the thinking process in multi-hop reasoning, and thus difficult to ensure the reliability of the generated answers;
[0006] (2) There is a cognitive difference between large language models and knowledge retrievers. Knowledge retrievers usually retrieve related facts based on the similarity of embedded representations. However, in complex reasoning tasks, the relevance of knowledge is not equivalent to its usefulness in the reasoning process. This phenomenon leads to the large language model being misled, which in turn aggravates the problem of hallucinations.
[0007] (3) Large language models cannot directly use structured knowledge triples. Directly serializing discrete knowledge triples will lead to the loss of topological information of knowledge. The continuity of reasoning thinking cannot be guaranteed during the reasoning process, making it difficult to deal with complex problems. Summary of the Invention
[0008] In response to the above-mentioned defects or improvement needs of the existing technology, the present invention provides a large language model joint reasoning method based on knowledge graph enhanced thought chain prompts, which aims to improve the accuracy and explainability of large language model joint reasoning.
[0009] To achieve the above objectives, according to a first aspect of the present invention, a large language model joint reasoning method based on knowledge graph-enhanced thought chain prompts is provided, which includes:
[0010] Extract the question entity from the original question text, and extract all knowledge triples within the multi-hop neighborhood of the question entity from the knowledge graph to form a local knowledge subgraph. The number of jump steps in the multi-hop neighborhood is T, and each step corresponds to an inference stage. T is a positive integer greater than 1.
[0011] Decomposing the original question text into S sub-question texts and concatenating the original question text and the S sub-question texts to obtain a concatenated question text, where S≤T;
[0012] Input the splicing problem text into the graph reasoning model to obtain the weighted entity distribution The graph reasoning model includes:
[0013] Question encoder, used to convert the concatenated question text into a question embedding representation p and a hidden layer representation H q ;
[0014] T cross-attention modules, used to calculate the cross-attention representation of p t is the number of the inference stage, t = 1, 2, ..., T;
[0015] Represents the fusion module, used for fusion and p t-1 , get the reasoning embedding representation p of stage t t ;
[0016] Matrix transformation module, used to make the p t After being processed by multi-layer perceptron and nonlinear activation function, the relationship distribution matrix R is obtained. t , R t Different elements in represent the confidence of different connection edges in the knowledge graph;
[0017] Matrix transfer module for t Construct the inference transfer matrix W t , the W t Different elements in represent the confidence of different connection edges in the local knowledge subgraph;
[0018] Entity distribution output module, used to distribute entity t-1 With the Wt Multiply them together to get the entity distribution e representing the confidence of each entity in step t t Finally, the entity distribution of all stages is weighted and summed to obtain the weighted entity distribution And output, Each element in represents the confidence that a different entity is the answer entity;
[0019] From the weighted entity distribution Extract the top G answer entities with the highest confidence, use the reasoning transfer matrix to trace back the reasoning process of each answer entity, and generate a reasoning path from the question entity to the corresponding answer entity, where G is a positive integer;
[0020] The inference path is used to assist the large language model in predicting the answer to the original question text.
[0021] In some embodiments, the graph reasoning model is obtained through training, and the training method includes:
[0022] Construct an initial natural language question-answering sample set, where each sample in the sample set contains the original text question, the knowledge graph, and the corresponding answer entity;
[0023] Extract the question entity from the original question text, extract all knowledge triples within the multi-hop neighborhood of the question entity from the knowledge graph, form a local knowledge subgraph, and include the corresponding answer entity in the local knowledge subgraph to obtain the target entity distribution a as the label;
[0024] Decomposing the original question text into S sub-question texts and concatenating the original question text and the S sub-question texts to obtain a concatenated question text;
[0025] The splicing problem text is input into the graph reasoning model for training, and the training loss function is:
[0026]
[0027] Where L is the graph reasoning model training loss, is the weighted entity distribution, and a is the target entity distribution.
[0028] In some embodiments, in the entity distribution output module, performing weighted summation of entity distributions of all stages includes:
[0029] The problem embedding representation p is processed by multi-layer perceptron and normalization function in turn to obtain T weights β t ~β T ;
[0030] The weighted entity distribution is obtained by weighting the entity distribution of all stages using the following formula:
[0031]
[0032] Where, β t is the tth weight.
[0033] In some embodiments, the t-th cross-attention module calculates the cross-attention representation of p The process includes:
[0034] Compute the attention query Q for p t And calculate the H q Attention key K t and attention value V t ,
[0035] Regarding the Q t , K t and V t Perform attention calculation to obtain cross attention representation The calculation formula is:
[0036]
[0037] Where, (K t ) T Indicates K t Perform matrix transposition, dim represents the dimension of the problem embedding representation p.
[0038] In some embodiments, the relationship distribution matrix R t Contains the confidence of all directed connections between entities in the knowledge graph, R t The kth element in Represents the confidence of the edge with index k in the knowledge graph;
[0039] According to the R t Construct the inference transfer matrix W t The process includes:
[0040] Get the adjacency matrix representation M of the local knowledge subgraph, where the elements in the i-th and j-th rows of M are M ij , when there is no connecting edge between the head entity i and the tail entity j, M ij = -1, when there is a connecting edge between the head entity i and the tail entity j, M ij is the index of the connecting edge between the head entity i and the tail entity j;
[0041] Construct the inference transfer matrix W t , W t The elements in row i and row j are
[0042]
[0043] Among them, the inference transfer matrix W t The matrix dimensions of are the same as the matrix dimensions of the adjacency matrix representation M.
[0044] In some embodiments, the weighted entity distribution Extract the top G answer entities with the highest confidence;
[0045] For the g-th extracted answer entity E g , g=1,2,……,G; Use the inference transfer matrix to trace back the reasoning process of each answer entity and generate the equation from the question entity to the answer entity E g The reasoning path of the process includes:
[0046] Set T path sets Store from question entity to answer entity E g The path length is t hops, path,score are The path set and score set in ; for any implement:
[0047] Step S41: Initialization operation, set path = E g , score=0, step=t;
[0048] Step S42: for each path in the current path x , x is the path index, with the current path x The entity at the end of the backtracking direction is the tail entity V, which is transferred by the inference matrix W step Find all head entities that have connecting edges with the tail entity V, and add the found head entities to the current path along the backtracking direction x , get the updated path, and change the current path x score x Add the confidence score of the newly added edge to get the score of the path where the newly added edge is located;
[0049] Step S43: Determine whether the current step is less than or equal to 1. If not, set step = step - 1 and jump to step S42. Otherwise, end the iteration and output the updated
[0050] Get the updated set of T paths Select the preset number of paths with the highest scores as the paths from the question entity to the answer entity Eg The reasoning path of
[0051] Get the answer entities E1~E from the question entity respectively G The reasoning path.
[0052] In some embodiments, for each answer entity E g , select a path with the highest confidence as prompt information to assist the large language model prediction.
[0053] According to a second aspect of the present invention, an electronic device is provided, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of any one of the above methods when executing the computer program.
[0054] According to a third aspect of the present invention, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above methods are implemented.
[0055] According to a fourth aspect of the present invention, there is provided a computer program product comprising a computer program or instructions, which implement the steps of any of the above methods when executed by a processor.
[0056] In general, compared with the prior art, the above technical solution conceived by the present invention has the following beneficial effects:
[0057] 1. The present invention first adopts a graph reasoning model to obtain answer entities with higher confidence in the local knowledge subgraph, and then uses the reasoning transfer matrix to backtrack the reasoning process of each answer entity, generating a reasoning path from the question entity to the corresponding answer entity, and using the reasoning path to assist the large language model in predicting the answer to the original question text. In this way, compared with the general breadth-first search algorithm, the time complexity of the power function level can be reduced to linear time complexity, thereby improving the efficiency of searching for answers.
[0058] 2. Before using the graph reasoning model, the present invention first decomposes the original question text into sub-questions and then splices them together, and then inputs the spliced question text into the graph reasoning model. In this way, by explicitly decomposing and encoding complex problems and utilizing the semantic characteristics of the sub-questions implicit in the problem embedding representation in the high-dimensional vector space, the graph reasoning model can be guided to focus on different knowledge triples at different reasoning stages, significantly reducing the problem of the graph reasoning model falling into over-reasoning due to the continuous accumulation of calculation errors in the relationship distribution, improving the accuracy of multi-hop graph reasoning, and thereby reducing the negative impact of irrelevant reasoning paths on the reasoning of the large language model, thereby ensuring the factuality and reliability of the reasoning of the large language model.
[0059] 3. The graph reasoning model used in the present invention includes a question encoder, a cross-attention module, a representation fusion module, a matrix transformation module, a matrix transfer module, and an entity distribution output module. Through the designed graph reasoning model, the large language model's ability to analyze complex problems can be fully utilized. Through explicit sub-questions and cross-attention modules, the graph reasoning model is guided to assign higher transfer probabilities to the relationships in the local knowledge subgraph that are highly supportive of reasoning in each step of the reasoning process, and reduce the interference of irrelevant relationships on the reasoning process, significantly reducing the deviation of the transfer probability that accumulates along the reasoning path and misleads the reasoning process. The representation fusion module introduced combines the cross-attention representation of the current stage with the reasoning embedding representation of the previous stage, and simulates the continuity of human thinking when reasoning complex problems, so that the graph reasoning process of the current step can pay attention to the reasoning history of the previous stage. It not only matches the thinking process of the large language model, but also makes full use of the unique relevance and hierarchy of the knowledge graph, enhances the continuity of the graph reasoning process, and further improves the accuracy of reasoning.
[0060] 4. In a specific embodiment, before applying the graph reasoning model, a loss function is introduced Training the graph reasoning model can make it converge quickly to expectations, so that the graph reasoning model can accurately predict the confidence of each answer entity based on the input question text.
[0061] 5. In a specific embodiment, a method for constructing the inference transfer matrix W is proposed. t The specific formula of is used to quickly construct the inference transfer matrix and improve the data processing speed.
[0062] 6. In a specific embodiment, a specific method for generating thought chain prompts is proposed. Through this method, an effective way to access multi-source heterogeneous knowledge data is provided for large language models that can only accept text sequences as input. This eliminates the limitations on the application of large language models due to differences in data modalities, allowing large language models to access external data using a universal solution for different types of tasks and different modal data. BRIEF DESCRIPTION OF THE DRAWINGS
[0063] Figure 1 This is a flowchart of the steps of a large language model joint reasoning method based on knowledge graph-enhanced thought chain prompts in one embodiment of the present invention;
[0064] Figure 2 is a relationship diagram of a local knowledge subgraph in one embodiment of the present invention;
[0065] Figure 3is a data processing diagram of a graph reasoning model in one embodiment of the present invention;
[0066] Figure 4 This is a schematic diagram of the process of generating thought chain prompts in one embodiment of the present application. DETAILED DESCRIPTION
[0067] In order to make the objectives, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely for the purpose of explaining the present invention and are not intended to limit the present invention. In addition, the technical features involved in the various embodiments of the present invention described below may be combined with each other as long as they do not conflict with each other.
[0068] like Figure 1 The figure shows a step flow chart of a large language model joint reasoning method based on knowledge graph enhanced thought chain prompts in one embodiment of the present invention, which mainly includes steps S1 to S5. Each step is described in detail below.
[0069] Step S1: Extract the question entity from the original question text, and extract all knowledge triples within the multi-hop neighborhood of the question entity from the knowledge graph to form a local knowledge subgraph. The number of jump steps in the multi-hop neighborhood is T steps, each step corresponds to an inference stage, and T is a positive integer greater than 1.
[0070] In this step, the input question text and knowledge graph data need to be preprocessed. Specifically, the question entities involved in the question text are extracted through the entity recognition method, and the question entities are linked to the knowledge graph through the entity linking method. All knowledge triples within the multi-hop neighborhood of the question entity are extracted to form a local knowledge subgraph. The entities in the local knowledge subgraph represent the text content. Figure 2 This figure shows a relationship diagram of a local knowledge subgraph in one embodiment of the present invention. Each jump corresponds to a reasoning stage, representing the process of finding the tail entity associated with the entity at the end of the current subgraph as the head entity. Furthermore, local knowledge subgraphs can be pruned using inference templates or representation similarity to reduce storage space overhead.
[0071] Step S2: Decompose the original question text into S sub-question texts and concatenate the original question text and the S sub-question texts to obtain a concatenated question text.
[0072] Among them, S≤T.
[0073] In one embodiment, a large language model can be used to decompose the question text into multiple inference sub-questions and concatenate them with the question text to obtain a concatenated question text. Specifically, this step can include the following process:
[0074] Step S21: Input the question text, question decomposition instruction (the question decomposition instruction is manually constructed by the user) and the upper limit T of the number of sub-questions into the large language model LLM, and instruct the large language model LLM to decompose the question text q into at most T sub-questions. After decomposition, S sub-question texts q1, q2, ..., q S , where S≤T is the number of sub-question texts obtained after the large language model LLM decomposes the question text q. The formula is as follows:
[0075] q1,q2,......,q S =LLM(q,instruct,T)
[0076] Step S22: Question text q and sub-question texts q1, q2, ..., q S Perform splicing to obtain the splicing question text [q:q1,q2,......,q S ].
[0077] Step S3: Input the concatenated question text into the graph reasoning model to obtain the weighted entity distribution
[0078] The graph reasoning model includes: question encoder, T cross-attention modules, representation fusion module, matrix transformation module and entity distribution output module.
[0079] like Figure 3 The figure shows the data processing process of the graph reasoning model in one embodiment of the present invention.
[0080] The question encoder is used to convert the concatenated question text into the question embedding representation p and the hidden layer representation H q In one embodiment, the question encoder can use the existing pre-trained language model BERT. Specifically, the question text [q:q1,q2,......,q S ] Input the pre-trained language model BERT and get a question embedding representation p with a dimension of 1×dim and a hidden layer representation H with a dimension of |q|×dim q =(h1,h2…,h |q| ), |q| is the concatenated question text [q:q1,q2,......,q S ] The total number of characters contained in h i is the parameter of the i-th hidden layer, and dim is the dimension of the embedded representation output by the pre-trained language model BERT.
[0081] p,(h1,…,h |q| )=BERT([q:q1,q2,......,q S ])
[0082] T cross-attention modules are used to calculate the cross-attention representation of p t is the number of the reasoning stage, t = 1, 2, ..., T. Since the graph reasoning process on the local knowledge subgraph in step S1 is divided into T stages, T cross-attention modules are also set up in the graph reasoning model. The question embedding representation p is input into T cross-attention modules respectively to obtain T cross-attention representations t=1,2,……,T. In a specific embodiment, the process of each cross-attention module calculating the cross-attention representation includes:
[0083] First, calculate the attention query Q of p t And calculate H q Attention key K t and attention value V t , specifically, using the weight matrix of stage t (dimensions are dim×dim) can be used to calculate the attention query at stage t Attention key and attention value
[0084]
[0085] Then, use the attention calculation formula to calculate
[0086]
[0087] Among them, (K t ) T The meaning is for the attention key K t The matrix transpose operation is performed, and dim is the dimension of the embedding representation output by the pre-trained language model BERT.
[0088] Among them, the dimension of the question embedding p is 1×dim, and the hidden layer represents H q The dimension is |q|×dim, the weight matrix of stage t The dimensions are all dim×dim, so the attention query Q t The dimension is 1×dim, and the attention key is K t The dimension is |q|×dim, and the attention value is V t The dimension is |q|×dim. t (K t ) T The dimension is 1×|q|, and the result after Softmax normalization is The dimension is 1×|q|, because the attention value V t The dimension of is |q|×dim, so The resulting dimension is 1×dim, that is, It is 1×dim dimension.
[0089] Indicates that the fusion module is used to fuse and p t-1 , get the reasoning embedding representation p of stage t t The reasoning embedding representation p at stage t t is the cross attention representation of stage t and the inference embedding representation p at stage t-1 t-1 The sum of the vectors is specifically expressed as:
[0090]
[0091] In this paper, by combining the cross attention representation of the current stage and the reasoning embedding representation of the previous stage to enhance the continuity of the graph reasoning process. Since t = 1, 2, ..., T, the reasoning embedding representation p at stage t = 0 can be set 0 is a zero vector of dimension 1×dim to satisfy the computational requirements of reasoning about the embedding representation at the initial stage t=1.
[0092] The matrix transformation module is used to make p t After being processed by multi-layer perceptron and nonlinear activation function, the relationship distribution matrix R is obtained. t , R t The different elements in represent the confidence of different connection edges in the knowledge graph. Specifically, the reasoning embedding representation p at stage t is used t , the calculation is processed by multi-layer perceptron and nonlinear activation function in turn, and the relationship distribution matrix R of the t-th jump is obtained t , R t Each element in represents the probability of different connection relationships in the original knowledge graph, which can be expressed as:
[0093] R t =Sigmoid(MLP(p t ))
[0094] In a specific embodiment, the relationship distribution matrix R t Contains the confidence of all directed connections between entities in the knowledge graph, R t The kth element in Represents the confidence of the edge with index k in the knowledge graph. That is, if there are h entities in the knowledge graph, the relationship distribution matrix R t The dimension can be h×h. The number of connecting edges between the head entity and the tail entity formed by any entity as the head entity and any entity as the tail entity is h×h. The relationship distribution matrix R tThe h×h elements in correspond one to one to the confidence of the h×h connecting edges.
[0095] The matrix transfer module is used to t Construct the inference transfer matrix W t , W t Different elements in represent the confidence of different connecting edges in the local knowledge subgraph. t The confidence of all connected edges in the knowledge graph is predicted in step S1, so it also includes the confidence of the connected edges formed at each stage of the local knowledge subgraph formed in step S1. The role of the matrix transfer module is to transfer the confidence from R t The confidence of the connecting edges of the local knowledge subgraph is extracted.
[0096] In the specific operation, the local knowledge subgraph is stored in the form of an adjacency matrix representation M. Assuming that the local knowledge subgraph has r entities, the number of connecting edges between the head entity and the tail entity formed by any entity as the head entity and any entity as the tail entity is r×r, the dimension of the adjacency matrix representation M can be r×r, and the r×r elements of the adjacency matrix representation M correspond one-to-one to the indexes of the r×r connecting edges. The elements of the i-th and j-th rows in M are M ij , which corresponds to the index of the connecting edge between the head entity i and the tail entity j. In the local knowledge subgraph, the index of the actual connecting edge can be numbered starting from 1. For example, if there are 10 connecting edges in total, the indexes are 1 to 10. If there is no connecting edge between the head entity i and the tail entity j in the local knowledge subgraph, then M in M ij It can be marked as an invalid value, for example, set to a negative value, specifically -1. If there is a connecting edge between the head entity i and the tail entity j in the local knowledge subgraph, then M ij is the index of the connecting edge between the head entity i and the tail entity j. Therefore, the adjacency matrix representation M actually expresses the local knowledge subgraph constructed in step S1.
[0097] According to R t Construct the inference transfer matrix W of stage t t The process includes:
[0098] Obtain the adjacency matrix representation M of the local knowledge subgraph;
[0099] Construct the inference transfer matrix W t , W t The elements in row i and row j are
[0100]
[0101] Among them, the inference transfer matrix W tThe matrix dimensions of M are the same as the matrix dimensions of the adjacency matrix representation M. That is, if the adjacency matrix representation M, M ij The value of the connection edge index k, then from R t Find the element with edge index k in M ij Replace with If the adjacency matrix represents M, M ij The value of is invalid value -1, which means that the connection edge does not exist in the local knowledge subgraph, then M ij Set to 0, after all elements are replaced, the replaced matrix is the inference transfer matrix W t .
[0102] Since the previous article formed T relationship distribution matrices R according to T stages 1 ~R T , so through the above process, T inference transfer matrices W are also formed 1 ~W T .
[0103] The entity distribution output module is used to distribute the entity t-1 With W t Multiply to get the entity distribution e t Finally, the entity distribution of all stages is weighted and summed to obtain the weighted entity distribution And output, each element in the entity distribution represents the confidence that a different entity is the answer entity.
[0104] Specifically, the sub-problem reasoning process is converted into multi-hop reasoning in the local knowledge subgraph. The confidence scores of the entities in the local knowledge subgraph at different reasoning stages are obtained through matrix operations. The weighted entity distribution is expressed as:
[0105] e t =e t-1 W t
[0106] β=Softmax(MLP(p))
[0107]
[0108] Among them, t is the number of the inference stage, e t is the entity distribution at stage t, W t is the inference transfer matrix at stage t, β is the attention weight at different inference stages, Softmax is the normalized exponential function, q is the embedding representation of the input problem, is the weighted entity distribution.
[0109] When t=1, e 0It corresponds to the distribution of problem entities, and its length is the number of entities in the local knowledge subgraph, where only the elements corresponding to the problem entity nodes are 1, and the rest are 0. Figure 1 There are 4 nodes, namely nodes 1 to 4, 1 is the problem entity, then:
[0110] e 0 =[1 0 0 0]
[0111]
[0112] e 1 It represents the confidence of each entity node at the first hop;
[0113] By analogy, e t It represents the confidence of each entity node at the t-th hop;
[0114] After completing T-step jumps, the confidence of each entity node corresponding to each jump step is weighted and summed to obtain the confidence of each entity as the answer entity.
[0115] It is understandable that the above inference model is obtained through training. The training method includes:
[0116] Construct an initial natural language question-answering sample set, where each sample in the sample set contains the original text question, the knowledge graph, and the corresponding answer entity;
[0117] Extract the question entity from the original question text, extract all knowledge triples within the multi-hop neighborhood of the question entity from the knowledge graph, form a local knowledge subgraph, and include the corresponding answer entity in the local knowledge subgraph to obtain the target entity distribution a as the label;
[0118] Decompose the original question text into S sub-question texts and concatenate the original question text and the S sub-question texts to obtain a concatenated question text;
[0119] The splicing problem text is input into the graph reasoning model for training and the graph reasoning model parameters are updated using gradient descent until convergence. The training loss function is:
[0120]
[0121] Where L is the graph reasoning model training loss, is the weighted entity distribution, and a is the target entity distribution.
[0122] Step S4: From the weighted entity distribution The first G answer entities with the highest confidence are extracted, and the reasoning process of each answer entity is traced back using the reasoning transfer matrix to generate the reasoning path from the question entity to the corresponding answer entity.
[0123] G is a positive integer.
[0124] From weighted entity distribution Extract the top G answer entities with the highest confidence, which are E1~E G .
[0125] like Figure 4 The figure shows the process of generating thought chain prompts in an embodiment of the present application. In step S3, starting from the question entity, the reasoning transfer matrix W is sequentially passed from the 1st to the Tth jump. t Perform calculations to find each answer entity. Therefore, in this step, you can follow the inverse calculation of step S3, take the answer entity as the starting point, and perform inverse calculations through the reasoning transfer matrix from the Tth jump to the 1st jump, trace back to the question entity, obtain the reasoning path from the question entity to the corresponding answer entity, and get the thinking chain prompt.
[0126] In the specific operation, for the g-th extracted answer entity E g , use the reasoning transfer matrix to trace back the reasoning process of each answer entity and generate the equation from the question entity to the answer entity E g The reasoning path of the process includes:
[0127] Set T path sets Store from question entity to answer entity E g The path length is t hops, path,score are The path set and score set in implement:
[0128] Step S41: Initialization operation, set path = E g , score=0, step=t;
[0129] Step S42: for each path in the current path x , x is the path index, with the current path x The entity at the end of the backtracking direction is the tail entity V, which is transferred by the inference matrix W step Find all head entities that have connecting edges with the tail entity V, and add the found head entities to the current path along the backtracking direction x , get the updated path, and change the current path x score x Add the confidence score of the newly added edge to get the score of the path where the newly added edge is located;
[0130] Step S43: Determine whether the current step is less than or equal to 1. If not, set step = step - 1 and jump to step S42. Otherwise, end the iteration and output the updated
[0131] Get the updated set of T paths Select the preset number of paths with the highest scores as the paths from the question entity to the answer entity E g The reasoning path of
[0132] Get the answer entities E1~E from the question entity respectively G The reasoning path.
[0133] In some embodiments, considering the storage cost, in step S42, after obtaining the updated paths, the paths are pruned according to the scores of the paths, and only the paths with the highest scores are retained.
[0134] In some embodiments, after obtaining the updated T path sets Only the path with the highest score is selected as the path from the question entity to the answer entity E g The reasoning path, at this time, from the question entity to the answer entity E1~E G There are G reasoning paths in total.
[0135] The following describes this process in detail.
[0136] Based on weighted entity distribution Select G answer entities E1, E2, ..., E with the largest confidence G (The size of G is set by the user), for each answer entity E g , we initialize T path sets Used to store the answer entity E g 1-hop, 2-hop, ..., T-hop reasoning paths to the problem entity, where each initial path set Where path0 = E m Represents the answer entity E m is the 0-hop path of the tail entity, score0 is the confidence score of the current path, and the initial value is 0 (since the initialization starts from the answer entity, it can be regarded as the answer entity E m A path that is the tail entity and has no preceding entity. New preceding entities will be added to form a path based on it during the backtracking process.
[0137] For the backtracking step step=t,t-1,......,1, for the path set Each path in (x is the number of the current path), take out the tail entity V of the path and the entity number index(V) of the entity, then you can backtrack the inference transfer matrix W of step step step Find the current path x The set of all preceding entities Specifically: Take out the inference transfer matrix W of the backtracking step step step The index(V) column of (where j = index(V), Z is the number of entities in the local knowledge subgraph), if If the value of is not 0, it means that there is an edge starting from entity i∈{i} along the relationship M ij , arrive at entity j(M ij It is the relationship number between the head entity i and the tail entity j, and entity j is the current path x Then, entity i is considered as the previous node of the current path and added to the current path x , updated It can be expressed as:
[0138]
[0139] k=M ij
[0140]
[0141] Where → means adding entity i as the preceding entity to the path path x , R step is the relationship distribution matrix of the backtracking step step, M ij Number the relationship between the head entity i and the tail entity j, the new path The confidence score is the original confidence score score x Plus the relationship M connecting entity i and entity j ij Probability
[0142] Step S5: Use the inference path to assist the large language model in predicting the answer to the original question text.
[0143] The final reasoning path P1, P2, ..., P obtained in the previous step S4 G , which is the thinking chain prompt from the question entity to the answer entity. We call these final reasoning paths thinking chain prompts based on knowledge graph enhancement. We call the question text q and the thinking chain prompts based on knowledge graph enhancement P1, P2, ..., P GInput into the large language model LLM to generate the final answer:
[0144] Answer=LLM(q,P1,P2,......,P G )
[0145] Since the local knowledge subgraph is a directed cyclic graph, if we start directly from the problem entity and use the general breadth-first search algorithm, the time complexity of path generation is at the power function level O(n T ), where n is the number of all knowledge triples in the local knowledge subgraph, and T is the number of jump steps in the multi-hop neighborhood. That is, as the number of knowledge triples n in the local knowledge subgraph increases, the running time of the algorithm increases at the power of n to the power of T, which is unacceptable for the program. Therefore, the present invention proposes the above-mentioned path backtracking algorithm based on beam search. For candidate entities with higher confidence, the search tree can be pruned in combination with the reasoning transfer matrix, and then a multi-hop reasoning process is generated by backtracking from the answer entity, generating a multi-hop reasoning path from the question entity to the answer entity, reducing the time complexity of the power function level to linear time complexity.
[0146] In the present invention, by extracting the question entity in the original question text, all knowledge triples within the multi-hop neighborhood of the question entity are extracted from the knowledge graph to form a local knowledge subgraph; the large language model is used to explicitly decompose and encode complex questions; the question encoder is combined to obtain the embedded representation of the original question and sub-questions in the high-dimensional vector space; the cross-attention module and the representation fusion module are combined to focus on the hierarchy and continuity of the graph reasoning process; based on the matrix transformation module and the matrix transfer module, multi-hop reasoning is implemented on the local knowledge subgraph to obtain the final entity distribution; then, based on the entity distribution, the reasoning process of each answer entity is backtracked using the reasoning transfer matrix, and a reasoning path from the question entity to the corresponding answer entity is generated, which serves as a knowledge graph-enhanced thinking chain prompt, guiding the large language model to use these retrieved external knowledge to perform continuous and reasonable question reasoning, thereby improving the accuracy and interpretability of the predicted answer.
[0147] Example 2
[0148] The present invention also relates to an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the above method when executing the computer program.
[0149] The electronic device may be a computing device such as a desktop computer, a notebook, a PDA, or a cloud server. The processor may be a central processing unit (CPU), or other general-purpose processors, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The memory may be used to store computer programs and / or modules, and the processor may perform various functions of the electronic device by running or executing the computer programs and / or modules stored in the memory, and calling the data stored in the memory.
[0150] Example 3
[0151] The present invention also relates to a computer-readable storage medium having a computer program stored thereon, which implements the steps of the above method when the computer program is executed by a processor.
[0152] Specifically, the memory may include a high-speed random access memory, and may also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage device.
[0153] Example 4
[0154] An embodiment of the present invention provides a computer program product or computer program, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the steps of the method of the above embodiment of the present invention.
[0155] The technical features of the above-described embodiments may be combined in any manner. To simplify the description, not all possible combinations of the technical features in the above-described embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification. It should be noted that the phrases "in one embodiment," "for example," "and another example," etc., of the present invention are intended to illustrate the present invention and are not intended to limit the present invention.
[0156] The above-described embodiments merely illustrate several implementations of the present invention, and while the descriptions are relatively specific and detailed, they should not be construed as limiting the scope of the patent application. It should be noted that a person skilled in the art would be able to make numerous variations and improvements without departing from the spirit of the present invention, and all such variations and improvements fall within the scope of protection of the present invention.
Claims
1. A large language model joint reasoning method based on knowledge graph enhanced thought chain prompts, characterized by: include: Extract the question entity from the original question text, and extract all knowledge triples within the multi-hop neighborhood of the question entity from the knowledge graph to form a local knowledge subgraph. The number of jump steps in the multi-hop neighborhood is T, and each step corresponds to an inference stage. T is a positive integer greater than 1. Decomposing the original question text into S sub-question texts and concatenating the original question text and the S sub-question texts to obtain a concatenated question text, where S≤T; Input the splicing problem text into the graph reasoning model to obtain the weighted entity distribution The graph reasoning model includes: Question encoder, used to convert the concatenated question text into a question embedding representation p and a hidden layer representation H q ; T cross-attention modules, used to calculate the cross-attention representation of p t is the number of the inference stage, t = 1, 2, ..., T; Represents the fusion module, used for fusion and p t-1 , get the reasoning embedding representation p of stage t t ; Matrix transformation module, used to make the p t After being processed by multi-layer perceptron and nonlinear activation function, the relationship distribution matrix R is obtained. t , R t Different elements in represent the confidence of different connection edges in the knowledge graph; Matrix transfer module for t Construct the inference transfer matrix W t , the W t Different elements in represent the confidence of different connection edges in the local knowledge subgraph; Entity distribution output module, used to distribute entity t-1 With the W t Multiply them together to get the entity distribution e representing the confidence of each entity in step t t Finally, the entity distribution of all stages is weighted and summed to obtain the weighted entity distribution And output, Each element in represents the confidence that a different entity is the answer entity; From the weighted entity distribution Extract the top G answer entities with the highest confidence, use the reasoning transfer matrix to trace back the reasoning process of each answer entity, and generate a reasoning path from the question entity to the corresponding answer entity, where G is a positive integer; The inference path is used to assist the large language model in predicting the answer to the original question text.
2. The large language model joint reasoning method according to claim 1, characterized in that: The graph reasoning model is obtained through training, and the training method includes: Construct an initial natural language question-answering sample set, where each sample in the sample set contains the original text question, the knowledge graph, and the corresponding answer entity; Extract the question entity from the original question text, extract all knowledge triples within the multi-hop neighborhood of the question entity from the knowledge graph, form a local knowledge subgraph, and include the corresponding answer entity in the local knowledge subgraph to obtain the target entity distribution a as the label; Decomposing the original question text into S sub-question texts and concatenating the original question text and the S sub-question texts to obtain a concatenated question text; The splicing problem text is input into the graph reasoning model for training, and the training loss function is: Where L is the graph reasoning model training loss, is the weighted entity distribution, and a is the target entity distribution.
3. The large language model joint reasoning method according to claim 1, characterized in that: In the entity distribution output module, the weighted summation of entity distributions in all stages includes: The problem embedding representation p is processed by multi-layer perceptron and normalization function in turn to obtain T weights β t ~β T ; The weighted entity distribution is obtained by weighting the entity distribution of all stages using the following formula: Where, β t is the tth weight.
4. The large language model joint reasoning method according to claim 1, characterized in that: The tth cross attention module calculates the cross attention representation of p The process includes: Compute the attention query Q for p t And calculate the H q Attention key K t and attention value V t , Regarding the Q t , K t and V t Perform attention calculation to obtain cross attention representation The calculation formula is: Where, (K t ) T Indicates K t Perform matrix transposition, dim represents the dimension of the problem embedding representation p.
5. The large language model joint reasoning method according to claim 1, characterized in that: The relationship distribution matrix R t Contains the confidence of all directed connections between entities in the knowledge graph, R t The kth element in Represents the confidence of the edge with index k in the knowledge graph; According to the R t Construct the inference transfer matrix W t The process includes: Get the adjacency matrix representation M of the local knowledge subgraph, where the elements in the i-th and j-th rows of M are M ij , when there is no connecting edge between the head entity i and the tail entity j, M ij = -1, when there is a connecting edge between the head entity i and the tail entity j, M ij is the index of the connecting edge between the head entity i and the tail entity j; Construct the inference transfer matrix W t , W t The elements in row i and row j are Among them, the inference transfer matrix W t The matrix dimensions of are the same as the matrix dimensions of the adjacency matrix representation M.
6. The large language model joint reasoning method according to claim 1, characterized in that: From the weighted entity distribution Extract the top G answer entities with the highest confidence; For the g-th extracted answer entity E g , g=1,2,……,G; Use the inference transfer matrix to trace back the reasoning process of each answer entity and generate the equation from the question entity to the answer entity E g The reasoning path of the process includes: Set T path sets Store from question entity to answer entity E g The path length is t hops, path,score are The path set and score set in implement: Step S41: Initialization operation, set path = E g , score=0, step=t; Step S42: for each path in the current path x , x is the path index, with the current path x The entity at the end of the backtracking direction is the tail entity V, which is transferred by the inference matrix W step Find all head entities that have connecting edges with the tail entity V, and add the found head entities to the current path along the backtracking direction x , get the updated path, and change the current path x score x Add the confidence score of the newly added edge to get the score of the path where the newly added edge is located; Step S43: Determine whether the current step is less than or equal to 1. If not, set step = step - 1 and jump to step S42. Otherwise, end the iteration and output the updated Get the updated set of T paths Select the preset number of paths with the highest scores as the paths from the question entity to the answer entity E g The reasoning path of Get the answer entities E1~E from the question entity respectively G The reasoning path.
7. The large language model joint reasoning method according to claim 6, characterized in that: For each answer entity E g , select a path with the highest confidence as prompt information to assist the large language model prediction.
8. An electronic device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer program product comprising a computer program or instructions, characterized in that When the computer program or instruction is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Visual question and answer method, system and device based on thinking chain and storage medium
CN117891965A
Multi-modal reasoning method and device based on large language model and knowledge graph
CN118193684A